blob: bcbd742149c64bd6beb61cb52d7d7cf219167d02 [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
Chon Ming Leeef9348c2014-04-09 13:28:18 +030044#define DIV_ROUND_CLOSEST_ULL(ll, d) \
45 ({ unsigned long long _tmp = (ll)+(d)/2; do_div(_tmp, d); _tmp; })
46
Daniel Vetter3dec0092010-08-20 21:40:52 +020047static void intel_increase_pllclock(struct drm_crtc *crtc);
Chris Wilson6b383a72010-09-13 13:54:26 +010048static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080049
Jesse Barnesf1f644d2013-06-27 00:39:25 +030050static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
51 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030052static void ironlake_pch_clock_get(struct intel_crtc *crtc,
53 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030054
Damien Lespiaue7457a92013-08-08 22:28:59 +010055static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
56 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080057static int intel_framebuffer_init(struct drm_device *dev,
58 struct intel_framebuffer *ifb,
59 struct drm_mode_fb_cmd2 *mode_cmd,
60 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020061static void intel_dp_set_m_n(struct intel_crtc *crtc);
62static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
63static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020064static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
65 struct intel_link_m_n *m_n);
66static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020067static void haswell_set_pipeconf(struct drm_crtc *crtc);
68static void intel_set_pipe_csc(struct drm_crtc *crtc);
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +020069static void vlv_prepare_pll(struct intel_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +010070
Jesse Barnes79e53942008-11-07 14:24:08 -080071typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040072 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -080073} intel_range_t;
74
75typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -040076 int dot_limit;
77 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -080078} intel_p2_t;
79
Ma Lingd4906092009-03-18 20:13:27 +080080typedef struct intel_limit intel_limit_t;
81struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -040082 intel_range_t dot, vco, n, m, m1, m2, p, p1;
83 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +080084};
Jesse Barnes79e53942008-11-07 14:24:08 -080085
Daniel Vetterd2acd212012-10-20 20:57:43 +020086int
87intel_pch_rawclk(struct drm_device *dev)
88{
89 struct drm_i915_private *dev_priv = dev->dev_private;
90
91 WARN_ON(!HAS_PCH_SPLIT(dev));
92
93 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
94}
95
Chris Wilson021357a2010-09-07 20:54:59 +010096static inline u32 /* units of 100MHz */
97intel_fdi_link_freq(struct drm_device *dev)
98{
Chris Wilson8b99e682010-10-13 09:59:17 +010099 if (IS_GEN5(dev)) {
100 struct drm_i915_private *dev_priv = dev->dev_private;
101 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
102 } else
103 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100104}
105
Daniel Vetter5d536e22013-07-06 12:52:06 +0200106static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400107 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200108 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200109 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400110 .m = { .min = 96, .max = 140 },
111 .m1 = { .min = 18, .max = 26 },
112 .m2 = { .min = 6, .max = 16 },
113 .p = { .min = 4, .max = 128 },
114 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700115 .p2 = { .dot_limit = 165000,
116 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700117};
118
Daniel Vetter5d536e22013-07-06 12:52:06 +0200119static const intel_limit_t intel_limits_i8xx_dvo = {
120 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200121 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200122 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200123 .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 },
128 .p2 = { .dot_limit = 165000,
129 .p2_slow = 4, .p2_fast = 4 },
130};
131
Keith Packarde4b36692009-06-05 19:22:17 -0700132static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400133 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200134 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200135 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400136 .m = { .min = 96, .max = 140 },
137 .m1 = { .min = 18, .max = 26 },
138 .m2 = { .min = 6, .max = 16 },
139 .p = { .min = 4, .max = 128 },
140 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700141 .p2 = { .dot_limit = 165000,
142 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700143};
Eric Anholt273e27c2011-03-30 13:01:10 -0700144
Keith Packarde4b36692009-06-05 19:22:17 -0700145static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400146 .dot = { .min = 20000, .max = 400000 },
147 .vco = { .min = 1400000, .max = 2800000 },
148 .n = { .min = 1, .max = 6 },
149 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100150 .m1 = { .min = 8, .max = 18 },
151 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400152 .p = { .min = 5, .max = 80 },
153 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700154 .p2 = { .dot_limit = 200000,
155 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700156};
157
158static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400159 .dot = { .min = 20000, .max = 400000 },
160 .vco = { .min = 1400000, .max = 2800000 },
161 .n = { .min = 1, .max = 6 },
162 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100163 .m1 = { .min = 8, .max = 18 },
164 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400165 .p = { .min = 7, .max = 98 },
166 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700167 .p2 = { .dot_limit = 112000,
168 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700169};
170
Eric Anholt273e27c2011-03-30 13:01:10 -0700171
Keith Packarde4b36692009-06-05 19:22:17 -0700172static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700173 .dot = { .min = 25000, .max = 270000 },
174 .vco = { .min = 1750000, .max = 3500000},
175 .n = { .min = 1, .max = 4 },
176 .m = { .min = 104, .max = 138 },
177 .m1 = { .min = 17, .max = 23 },
178 .m2 = { .min = 5, .max = 11 },
179 .p = { .min = 10, .max = 30 },
180 .p1 = { .min = 1, .max = 3},
181 .p2 = { .dot_limit = 270000,
182 .p2_slow = 10,
183 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800184 },
Keith Packarde4b36692009-06-05 19:22:17 -0700185};
186
187static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700188 .dot = { .min = 22000, .max = 400000 },
189 .vco = { .min = 1750000, .max = 3500000},
190 .n = { .min = 1, .max = 4 },
191 .m = { .min = 104, .max = 138 },
192 .m1 = { .min = 16, .max = 23 },
193 .m2 = { .min = 5, .max = 11 },
194 .p = { .min = 5, .max = 80 },
195 .p1 = { .min = 1, .max = 8},
196 .p2 = { .dot_limit = 165000,
197 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700198};
199
200static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700201 .dot = { .min = 20000, .max = 115000 },
202 .vco = { .min = 1750000, .max = 3500000 },
203 .n = { .min = 1, .max = 3 },
204 .m = { .min = 104, .max = 138 },
205 .m1 = { .min = 17, .max = 23 },
206 .m2 = { .min = 5, .max = 11 },
207 .p = { .min = 28, .max = 112 },
208 .p1 = { .min = 2, .max = 8 },
209 .p2 = { .dot_limit = 0,
210 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800211 },
Keith Packarde4b36692009-06-05 19:22:17 -0700212};
213
214static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700215 .dot = { .min = 80000, .max = 224000 },
216 .vco = { .min = 1750000, .max = 3500000 },
217 .n = { .min = 1, .max = 3 },
218 .m = { .min = 104, .max = 138 },
219 .m1 = { .min = 17, .max = 23 },
220 .m2 = { .min = 5, .max = 11 },
221 .p = { .min = 14, .max = 42 },
222 .p1 = { .min = 2, .max = 6 },
223 .p2 = { .dot_limit = 0,
224 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800225 },
Keith Packarde4b36692009-06-05 19:22:17 -0700226};
227
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500228static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400229 .dot = { .min = 20000, .max = 400000},
230 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700231 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400232 .n = { .min = 3, .max = 6 },
233 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700234 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400235 .m1 = { .min = 0, .max = 0 },
236 .m2 = { .min = 0, .max = 254 },
237 .p = { .min = 5, .max = 80 },
238 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700239 .p2 = { .dot_limit = 200000,
240 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700241};
242
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500243static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400244 .dot = { .min = 20000, .max = 400000 },
245 .vco = { .min = 1700000, .max = 3500000 },
246 .n = { .min = 3, .max = 6 },
247 .m = { .min = 2, .max = 256 },
248 .m1 = { .min = 0, .max = 0 },
249 .m2 = { .min = 0, .max = 254 },
250 .p = { .min = 7, .max = 112 },
251 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700252 .p2 = { .dot_limit = 112000,
253 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700254};
255
Eric Anholt273e27c2011-03-30 13:01:10 -0700256/* Ironlake / Sandybridge
257 *
258 * We calculate clock using (register_value + 2) for N/M1/M2, so here
259 * the range value for them is (actual_value - 2).
260 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800261static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700262 .dot = { .min = 25000, .max = 350000 },
263 .vco = { .min = 1760000, .max = 3510000 },
264 .n = { .min = 1, .max = 5 },
265 .m = { .min = 79, .max = 127 },
266 .m1 = { .min = 12, .max = 22 },
267 .m2 = { .min = 5, .max = 9 },
268 .p = { .min = 5, .max = 80 },
269 .p1 = { .min = 1, .max = 8 },
270 .p2 = { .dot_limit = 225000,
271 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700272};
273
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800274static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700275 .dot = { .min = 25000, .max = 350000 },
276 .vco = { .min = 1760000, .max = 3510000 },
277 .n = { .min = 1, .max = 3 },
278 .m = { .min = 79, .max = 118 },
279 .m1 = { .min = 12, .max = 22 },
280 .m2 = { .min = 5, .max = 9 },
281 .p = { .min = 28, .max = 112 },
282 .p1 = { .min = 2, .max = 8 },
283 .p2 = { .dot_limit = 225000,
284 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800285};
286
287static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700288 .dot = { .min = 25000, .max = 350000 },
289 .vco = { .min = 1760000, .max = 3510000 },
290 .n = { .min = 1, .max = 3 },
291 .m = { .min = 79, .max = 127 },
292 .m1 = { .min = 12, .max = 22 },
293 .m2 = { .min = 5, .max = 9 },
294 .p = { .min = 14, .max = 56 },
295 .p1 = { .min = 2, .max = 8 },
296 .p2 = { .dot_limit = 225000,
297 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800298};
299
Eric Anholt273e27c2011-03-30 13:01:10 -0700300/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800301static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .dot = { .min = 25000, .max = 350000 },
303 .vco = { .min = 1760000, .max = 3510000 },
304 .n = { .min = 1, .max = 2 },
305 .m = { .min = 79, .max = 126 },
306 .m1 = { .min = 12, .max = 22 },
307 .m2 = { .min = 5, .max = 9 },
308 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400309 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .p2 = { .dot_limit = 225000,
311 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800312};
313
314static const intel_limit_t intel_limits_ironlake_dual_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 = 3 },
318 .m = { .min = 79, .max = 126 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400322 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800325};
326
Ville Syrjälädc730512013-09-24 21:26:30 +0300327static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300328 /*
329 * These are the data rate limits (measured in fast clocks)
330 * since those are the strictest limits we have. The fast
331 * clock and actual rate limits are more relaxed, so checking
332 * them would make no difference.
333 */
334 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200335 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700336 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700337 .m1 = { .min = 2, .max = 3 },
338 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300339 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300340 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700341};
342
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300343static const intel_limit_t intel_limits_chv = {
344 /*
345 * These are the data rate limits (measured in fast clocks)
346 * since those are the strictest limits we have. The fast
347 * clock and actual rate limits are more relaxed, so checking
348 * them would make no difference.
349 */
350 .dot = { .min = 25000 * 5, .max = 540000 * 5},
351 .vco = { .min = 4860000, .max = 6700000 },
352 .n = { .min = 1, .max = 1 },
353 .m1 = { .min = 2, .max = 2 },
354 .m2 = { .min = 24 << 22, .max = 175 << 22 },
355 .p1 = { .min = 2, .max = 4 },
356 .p2 = { .p2_slow = 1, .p2_fast = 14 },
357};
358
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300359static void vlv_clock(int refclk, intel_clock_t *clock)
360{
361 clock->m = clock->m1 * clock->m2;
362 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200363 if (WARN_ON(clock->n == 0 || clock->p == 0))
364 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300365 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
366 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300367}
368
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300369/**
370 * Returns whether any output on the specified pipe is of the specified type
371 */
372static bool intel_pipe_has_type(struct drm_crtc *crtc, int type)
373{
374 struct drm_device *dev = crtc->dev;
375 struct intel_encoder *encoder;
376
377 for_each_encoder_on_crtc(dev, crtc, encoder)
378 if (encoder->type == type)
379 return true;
380
381 return false;
382}
383
Chris Wilson1b894b52010-12-14 20:04:54 +0000384static const intel_limit_t *intel_ironlake_limit(struct drm_crtc *crtc,
385 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800386{
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800387 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800388 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800389
390 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100391 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000392 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800393 limit = &intel_limits_ironlake_dual_lvds_100m;
394 else
395 limit = &intel_limits_ironlake_dual_lvds;
396 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000397 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800398 limit = &intel_limits_ironlake_single_lvds_100m;
399 else
400 limit = &intel_limits_ironlake_single_lvds;
401 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200402 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800403 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800404
405 return limit;
406}
407
Ma Ling044c7c42009-03-18 20:13:23 +0800408static const intel_limit_t *intel_g4x_limit(struct drm_crtc *crtc)
409{
410 struct drm_device *dev = crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800411 const intel_limit_t *limit;
412
413 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100414 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700415 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800416 else
Keith Packarde4b36692009-06-05 19:22:17 -0700417 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800418 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
419 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700420 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800421 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700422 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800423 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700424 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800425
426 return limit;
427}
428
Chris Wilson1b894b52010-12-14 20:04:54 +0000429static const intel_limit_t *intel_limit(struct drm_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800430{
431 struct drm_device *dev = crtc->dev;
432 const intel_limit_t *limit;
433
Eric Anholtbad720f2009-10-22 16:11:14 -0700434 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000435 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800436 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800437 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500438 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800439 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500440 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800441 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500442 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300443 } else if (IS_CHERRYVIEW(dev)) {
444 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700445 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300446 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100447 } else if (!IS_GEN2(dev)) {
448 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
449 limit = &intel_limits_i9xx_lvds;
450 else
451 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800452 } else {
453 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700454 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200455 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700456 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200457 else
458 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800459 }
460 return limit;
461}
462
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500463/* m1 is reserved as 0 in Pineview, n is a ring counter */
464static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800465{
Shaohua Li21778322009-02-23 15:19:16 +0800466 clock->m = clock->m2 + 2;
467 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200468 if (WARN_ON(clock->n == 0 || clock->p == 0))
469 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300470 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
471 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800472}
473
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200474static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
475{
476 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
477}
478
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200479static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800480{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200481 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800482 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200483 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
484 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300485 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
486 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800487}
488
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300489static void chv_clock(int refclk, intel_clock_t *clock)
490{
491 clock->m = clock->m1 * clock->m2;
492 clock->p = clock->p1 * clock->p2;
493 if (WARN_ON(clock->n == 0 || clock->p == 0))
494 return;
495 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
496 clock->n << 22);
497 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
498}
499
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800500#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800501/**
502 * Returns whether the given set of divisors are valid for a given refclk with
503 * the given connectors.
504 */
505
Chris Wilson1b894b52010-12-14 20:04:54 +0000506static bool intel_PLL_is_valid(struct drm_device *dev,
507 const intel_limit_t *limit,
508 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800509{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300510 if (clock->n < limit->n.min || limit->n.max < clock->n)
511 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800512 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400513 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800514 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400515 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800516 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400517 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300518
519 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
520 if (clock->m1 <= clock->m2)
521 INTELPllInvalid("m1 <= m2\n");
522
523 if (!IS_VALLEYVIEW(dev)) {
524 if (clock->p < limit->p.min || limit->p.max < clock->p)
525 INTELPllInvalid("p out of range\n");
526 if (clock->m < limit->m.min || limit->m.max < clock->m)
527 INTELPllInvalid("m out of range\n");
528 }
529
Jesse Barnes79e53942008-11-07 14:24:08 -0800530 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400531 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800532 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
533 * connector, etc., rather than just a single range.
534 */
535 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400536 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800537
538 return true;
539}
540
Ma Lingd4906092009-03-18 20:13:27 +0800541static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200542i9xx_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800543 int target, int refclk, intel_clock_t *match_clock,
544 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800545{
546 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800547 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800548 int err = target;
549
Daniel Vettera210b022012-11-26 17:22:08 +0100550 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800551 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100552 * For LVDS just rely on its current settings for dual-channel.
553 * We haven't figured out how to reliably set up different
554 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100556 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800557 clock.p2 = limit->p2.p2_fast;
558 else
559 clock.p2 = limit->p2.p2_slow;
560 } else {
561 if (target < limit->p2.dot_limit)
562 clock.p2 = limit->p2.p2_slow;
563 else
564 clock.p2 = limit->p2.p2_fast;
565 }
566
Akshay Joshi0206e352011-08-16 15:34:10 -0400567 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800568
Zhao Yakui42158662009-11-20 11:24:18 +0800569 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
570 clock.m1++) {
571 for (clock.m2 = limit->m2.min;
572 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200573 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800574 break;
575 for (clock.n = limit->n.min;
576 clock.n <= limit->n.max; clock.n++) {
577 for (clock.p1 = limit->p1.min;
578 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800579 int this_err;
580
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200581 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000582 if (!intel_PLL_is_valid(dev, limit,
583 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800584 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800585 if (match_clock &&
586 clock.p != match_clock->p)
587 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800588
589 this_err = abs(clock.dot - target);
590 if (this_err < err) {
591 *best_clock = clock;
592 err = this_err;
593 }
594 }
595 }
596 }
597 }
598
599 return (err != target);
600}
601
Ma Lingd4906092009-03-18 20:13:27 +0800602static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200603pnv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
604 int target, int refclk, intel_clock_t *match_clock,
605 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200606{
607 struct drm_device *dev = crtc->dev;
608 intel_clock_t clock;
609 int err = target;
610
611 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
612 /*
613 * For LVDS just rely on its current settings for dual-channel.
614 * We haven't figured out how to reliably set up different
615 * single/dual channel state, if we even can.
616 */
617 if (intel_is_dual_link_lvds(dev))
618 clock.p2 = limit->p2.p2_fast;
619 else
620 clock.p2 = limit->p2.p2_slow;
621 } else {
622 if (target < limit->p2.dot_limit)
623 clock.p2 = limit->p2.p2_slow;
624 else
625 clock.p2 = limit->p2.p2_fast;
626 }
627
628 memset(best_clock, 0, sizeof(*best_clock));
629
630 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
631 clock.m1++) {
632 for (clock.m2 = limit->m2.min;
633 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200634 for (clock.n = limit->n.min;
635 clock.n <= limit->n.max; clock.n++) {
636 for (clock.p1 = limit->p1.min;
637 clock.p1 <= limit->p1.max; clock.p1++) {
638 int this_err;
639
640 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800641 if (!intel_PLL_is_valid(dev, limit,
642 &clock))
643 continue;
644 if (match_clock &&
645 clock.p != match_clock->p)
646 continue;
647
648 this_err = abs(clock.dot - target);
649 if (this_err < err) {
650 *best_clock = clock;
651 err = this_err;
652 }
653 }
654 }
655 }
656 }
657
658 return (err != target);
659}
660
Ma Lingd4906092009-03-18 20:13:27 +0800661static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200662g4x_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
663 int target, int refclk, intel_clock_t *match_clock,
664 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800665{
666 struct drm_device *dev = crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800667 intel_clock_t clock;
668 int max_n;
669 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400670 /* approximately equals target * 0.00585 */
671 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800672 found = false;
673
674 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100675 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800676 clock.p2 = limit->p2.p2_fast;
677 else
678 clock.p2 = limit->p2.p2_slow;
679 } else {
680 if (target < limit->p2.dot_limit)
681 clock.p2 = limit->p2.p2_slow;
682 else
683 clock.p2 = limit->p2.p2_fast;
684 }
685
686 memset(best_clock, 0, sizeof(*best_clock));
687 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200688 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800689 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200690 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800691 for (clock.m1 = limit->m1.max;
692 clock.m1 >= limit->m1.min; clock.m1--) {
693 for (clock.m2 = limit->m2.max;
694 clock.m2 >= limit->m2.min; clock.m2--) {
695 for (clock.p1 = limit->p1.max;
696 clock.p1 >= limit->p1.min; clock.p1--) {
697 int this_err;
698
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200699 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000700 if (!intel_PLL_is_valid(dev, limit,
701 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800702 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000703
704 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800705 if (this_err < err_most) {
706 *best_clock = clock;
707 err_most = this_err;
708 max_n = clock.n;
709 found = true;
710 }
711 }
712 }
713 }
714 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800715 return found;
716}
Ma Lingd4906092009-03-18 20:13:27 +0800717
Zhenyu Wang2c072452009-06-05 15:38:42 +0800718static bool
Daniel Vetteree9300b2013-06-03 22:40:22 +0200719vlv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
720 int target, int refclk, intel_clock_t *match_clock,
721 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700722{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300723 struct drm_device *dev = crtc->dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300724 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300725 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300726 /* min update 19.2 MHz */
727 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300728 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700729
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300730 target *= 5; /* fast clock */
731
732 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700733
734 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300735 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300736 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300737 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300738 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300739 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700740 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300741 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300742 unsigned int ppm, diff;
743
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300744 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
745 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300746
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300747 vlv_clock(refclk, &clock);
748
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300749 if (!intel_PLL_is_valid(dev, limit,
750 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300751 continue;
752
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300753 diff = abs(clock.dot - target);
754 ppm = div_u64(1000000ULL * diff, target);
755
756 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300757 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300758 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300759 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300760 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300761
Ville Syrjäläc6861222013-09-24 21:26:21 +0300762 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300763 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300764 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300765 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700766 }
767 }
768 }
769 }
770 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700771
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300772 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700773}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700774
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300775static bool
776chv_find_best_dpll(const intel_limit_t *limit, struct drm_crtc *crtc,
777 int target, int refclk, intel_clock_t *match_clock,
778 intel_clock_t *best_clock)
779{
780 struct drm_device *dev = crtc->dev;
781 intel_clock_t clock;
782 uint64_t m2;
783 int found = false;
784
785 memset(best_clock, 0, sizeof(*best_clock));
786
787 /*
788 * Based on hardware doc, the n always set to 1, and m1 always
789 * set to 2. If requires to support 200Mhz refclk, we need to
790 * revisit this because n may not 1 anymore.
791 */
792 clock.n = 1, clock.m1 = 2;
793 target *= 5; /* fast clock */
794
795 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
796 for (clock.p2 = limit->p2.p2_fast;
797 clock.p2 >= limit->p2.p2_slow;
798 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
799
800 clock.p = clock.p1 * clock.p2;
801
802 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
803 clock.n) << 22, refclk * clock.m1);
804
805 if (m2 > INT_MAX/clock.m1)
806 continue;
807
808 clock.m2 = m2;
809
810 chv_clock(refclk, &clock);
811
812 if (!intel_PLL_is_valid(dev, limit, &clock))
813 continue;
814
815 /* based on hardware requirement, prefer bigger p
816 */
817 if (clock.p > best_clock->p) {
818 *best_clock = clock;
819 found = true;
820 }
821 }
822 }
823
824 return found;
825}
826
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300827bool intel_crtc_active(struct drm_crtc *crtc)
828{
829 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
830
831 /* Be paranoid as we can arrive here with only partial
832 * state retrieved from the hardware during setup.
833 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100834 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300835 * as Haswell has gained clock readout/fastboot support.
836 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000837 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300838 * properly reconstruct framebuffers.
839 */
Matt Roperf4510a22014-04-01 15:22:40 -0700840 return intel_crtc->active && crtc->primary->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100841 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300842}
843
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200844enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
845 enum pipe pipe)
846{
847 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
848 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
849
Daniel Vetter3b117c82013-04-17 20:15:07 +0200850 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200851}
852
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200853static void g4x_wait_for_vblank(struct drm_device *dev, int pipe)
Paulo Zanonia928d532012-05-04 17:18:15 -0300854{
855 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200856 u32 frame, frame_reg = PIPE_FRMCOUNT_GM45(pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300857
858 frame = I915_READ(frame_reg);
859
860 if (wait_for(I915_READ_NOTRACE(frame_reg) != frame, 50))
Jesse Barnes93937072014-04-04 16:12:09 -0700861 WARN(1, "vblank wait timed out\n");
Paulo Zanonia928d532012-05-04 17:18:15 -0300862}
863
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700864/**
865 * intel_wait_for_vblank - wait for vblank on a given pipe
866 * @dev: drm device
867 * @pipe: pipe to wait for
868 *
869 * Wait for vblank to occur on a given pipe. Needed for various bits of
870 * mode setting code.
871 */
872void intel_wait_for_vblank(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -0800873{
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700874 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes9db4a9c2011-02-07 12:26:52 -0800875 int pipestat_reg = PIPESTAT(pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700876
Ville Syrjälä57e22f42013-11-06 13:56:28 -0200877 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5) {
878 g4x_wait_for_vblank(dev, pipe);
Paulo Zanonia928d532012-05-04 17:18:15 -0300879 return;
880 }
881
Chris Wilson300387c2010-09-05 20:25:43 +0100882 /* Clear existing vblank status. Note this will clear any other
883 * sticky status fields as well.
884 *
885 * This races with i915_driver_irq_handler() with the result
886 * that either function could miss a vblank event. Here it is not
887 * fatal, as we will either wait upon the next vblank interrupt or
888 * timeout. Generally speaking intel_wait_for_vblank() is only
889 * called during modeset at which time the GPU should be idle and
890 * should *not* be performing page flips and thus not waiting on
891 * vblanks...
892 * Currently, the result of us stealing a vblank from the irq
893 * handler is that a single frame will be skipped during swapbuffers.
894 */
895 I915_WRITE(pipestat_reg,
896 I915_READ(pipestat_reg) | PIPE_VBLANK_INTERRUPT_STATUS);
897
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700898 /* Wait for vblank interrupt bit to set */
Chris Wilson481b6af2010-08-23 17:43:35 +0100899 if (wait_for(I915_READ(pipestat_reg) &
900 PIPE_VBLANK_INTERRUPT_STATUS,
901 50))
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700902 DRM_DEBUG_KMS("vblank wait timed out\n");
903}
904
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300905static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
906{
907 struct drm_i915_private *dev_priv = dev->dev_private;
908 u32 reg = PIPEDSL(pipe);
909 u32 line1, line2;
910 u32 line_mask;
911
912 if (IS_GEN2(dev))
913 line_mask = DSL_LINEMASK_GEN2;
914 else
915 line_mask = DSL_LINEMASK_GEN3;
916
917 line1 = I915_READ(reg) & line_mask;
918 mdelay(5);
919 line2 = I915_READ(reg) & line_mask;
920
921 return line1 == line2;
922}
923
Keith Packardab7ad7f2010-10-03 00:33:06 -0700924/*
925 * intel_wait_for_pipe_off - wait for pipe to turn off
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700926 * @dev: drm device
927 * @pipe: pipe to wait for
928 *
929 * After disabling a pipe, we can't wait for vblank in the usual way,
930 * spinning on the vblank interrupt status bit, since we won't actually
931 * see an interrupt when the pipe is disabled.
932 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700933 * On Gen4 and above:
934 * wait for the pipe register state bit to turn off
935 *
936 * Otherwise:
937 * wait for the display line value to settle (it usually
938 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100939 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700940 */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100941void intel_wait_for_pipe_off(struct drm_device *dev, int pipe)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700942{
943 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200944 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
945 pipe);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700946
Keith Packardab7ad7f2010-10-03 00:33:06 -0700947 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200948 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700949
Keith Packardab7ad7f2010-10-03 00:33:06 -0700950 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100951 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
952 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200953 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700954 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700955 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300956 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200957 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700958 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800959}
960
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000961/*
962 * ibx_digital_port_connected - is the specified port connected?
963 * @dev_priv: i915 private structure
964 * @port: the port to test
965 *
966 * Returns true if @port is connected, false otherwise.
967 */
968bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
969 struct intel_digital_port *port)
970{
971 u32 bit;
972
Damien Lespiauc36346e2012-12-13 16:09:03 +0000973 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +0200974 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000975 case PORT_B:
976 bit = SDE_PORTB_HOTPLUG;
977 break;
978 case PORT_C:
979 bit = SDE_PORTC_HOTPLUG;
980 break;
981 case PORT_D:
982 bit = SDE_PORTD_HOTPLUG;
983 break;
984 default:
985 return true;
986 }
987 } else {
Robin Schroereba905b2014-05-18 02:24:50 +0200988 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000989 case PORT_B:
990 bit = SDE_PORTB_HOTPLUG_CPT;
991 break;
992 case PORT_C:
993 bit = SDE_PORTC_HOTPLUG_CPT;
994 break;
995 case PORT_D:
996 bit = SDE_PORTD_HOTPLUG_CPT;
997 break;
998 default:
999 return true;
1000 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001001 }
1002
1003 return I915_READ(SDEISR) & bit;
1004}
1005
Jesse Barnesb24e7172011-01-04 15:09:30 -08001006static const char *state_string(bool enabled)
1007{
1008 return enabled ? "on" : "off";
1009}
1010
1011/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001012void assert_pll(struct drm_i915_private *dev_priv,
1013 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001014{
1015 int reg;
1016 u32 val;
1017 bool cur_state;
1018
1019 reg = DPLL(pipe);
1020 val = I915_READ(reg);
1021 cur_state = !!(val & DPLL_VCO_ENABLE);
1022 WARN(cur_state != state,
1023 "PLL state assertion failure (expected %s, current %s)\n",
1024 state_string(state), state_string(cur_state));
1025}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001026
Jani Nikula23538ef2013-08-27 15:12:22 +03001027/* XXX: the dsi pll is shared between MIPI DSI ports */
1028static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1029{
1030 u32 val;
1031 bool cur_state;
1032
1033 mutex_lock(&dev_priv->dpio_lock);
1034 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1035 mutex_unlock(&dev_priv->dpio_lock);
1036
1037 cur_state = val & DSI_PLL_VCO_EN;
1038 WARN(cur_state != state,
1039 "DSI PLL state assertion failure (expected %s, current %s)\n",
1040 state_string(state), state_string(cur_state));
1041}
1042#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1043#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1044
Daniel Vetter55607e82013-06-16 21:42:39 +02001045struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001046intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001047{
Daniel Vettere2b78262013-06-07 23:10:03 +02001048 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1049
Daniel Vettera43f6e02013-06-07 23:10:32 +02001050 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001051 return NULL;
1052
Daniel Vettera43f6e02013-06-07 23:10:32 +02001053 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001054}
1055
Jesse Barnesb24e7172011-01-04 15:09:30 -08001056/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001057void assert_shared_dpll(struct drm_i915_private *dev_priv,
1058 struct intel_shared_dpll *pll,
1059 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001060{
Jesse Barnes040484a2011-01-03 12:14:26 -08001061 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001062 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001063
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001064 if (HAS_PCH_LPT(dev_priv->dev)) {
1065 DRM_DEBUG_DRIVER("LPT detected: skipping PCH PLL test\n");
1066 return;
1067 }
1068
Chris Wilson92b27b02012-05-20 18:10:50 +01001069 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001070 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001071 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001072
Daniel Vetter53589012013-06-05 13:34:16 +02001073 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001074 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001075 "%s assertion failure (expected %s, current %s)\n",
1076 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001077}
Jesse Barnes040484a2011-01-03 12:14:26 -08001078
1079static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1080 enum pipe pipe, bool state)
1081{
1082 int reg;
1083 u32 val;
1084 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001085 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1086 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001087
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001088 if (HAS_DDI(dev_priv->dev)) {
1089 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001090 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001091 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001092 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001093 } else {
1094 reg = FDI_TX_CTL(pipe);
1095 val = I915_READ(reg);
1096 cur_state = !!(val & FDI_TX_ENABLE);
1097 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001098 WARN(cur_state != state,
1099 "FDI TX state assertion failure (expected %s, current %s)\n",
1100 state_string(state), state_string(cur_state));
1101}
1102#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1103#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1104
1105static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1106 enum pipe pipe, bool state)
1107{
1108 int reg;
1109 u32 val;
1110 bool cur_state;
1111
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001112 reg = FDI_RX_CTL(pipe);
1113 val = I915_READ(reg);
1114 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001115 WARN(cur_state != state,
1116 "FDI RX state assertion failure (expected %s, current %s)\n",
1117 state_string(state), state_string(cur_state));
1118}
1119#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1120#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1121
1122static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1123 enum pipe pipe)
1124{
1125 int reg;
1126 u32 val;
1127
1128 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001129 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001130 return;
1131
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001132 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001133 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001134 return;
1135
Jesse Barnes040484a2011-01-03 12:14:26 -08001136 reg = FDI_TX_CTL(pipe);
1137 val = I915_READ(reg);
1138 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1139}
1140
Daniel Vetter55607e82013-06-16 21:42:39 +02001141void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1142 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001143{
1144 int reg;
1145 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001146 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001147
1148 reg = FDI_RX_CTL(pipe);
1149 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001150 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1151 WARN(cur_state != state,
1152 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1153 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001154}
1155
Jesse Barnesea0760c2011-01-04 15:09:32 -08001156static void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1157 enum pipe pipe)
1158{
1159 int pp_reg, lvds_reg;
1160 u32 val;
1161 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001162 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001163
1164 if (HAS_PCH_SPLIT(dev_priv->dev)) {
1165 pp_reg = PCH_PP_CONTROL;
1166 lvds_reg = PCH_LVDS;
1167 } else {
1168 pp_reg = PP_CONTROL;
1169 lvds_reg = LVDS;
1170 }
1171
1172 val = I915_READ(pp_reg);
1173 if (!(val & PANEL_POWER_ON) ||
1174 ((val & PANEL_UNLOCK_REGS) == PANEL_UNLOCK_REGS))
1175 locked = false;
1176
1177 if (I915_READ(lvds_reg) & LVDS_PIPEB_SELECT)
1178 panel_pipe = PIPE_B;
1179
1180 WARN(panel_pipe == pipe && locked,
1181 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001182 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001183}
1184
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001185static void assert_cursor(struct drm_i915_private *dev_priv,
1186 enum pipe pipe, bool state)
1187{
1188 struct drm_device *dev = dev_priv->dev;
1189 bool cur_state;
1190
Paulo Zanonid9d82082014-02-27 16:30:56 -03001191 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001192 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001193 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001194 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001195
1196 WARN(cur_state != state,
1197 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1198 pipe_name(pipe), state_string(state), state_string(cur_state));
1199}
1200#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1201#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1202
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001203void assert_pipe(struct drm_i915_private *dev_priv,
1204 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001205{
1206 int reg;
1207 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001208 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001209 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1210 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001211
Daniel Vetter8e636782012-01-22 01:36:48 +01001212 /* if we need the pipe A quirk it must be always on */
1213 if (pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
1214 state = true;
1215
Imre Deakda7e29b2014-02-18 00:02:02 +02001216 if (!intel_display_power_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001217 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001218 cur_state = false;
1219 } else {
1220 reg = PIPECONF(cpu_transcoder);
1221 val = I915_READ(reg);
1222 cur_state = !!(val & PIPECONF_ENABLE);
1223 }
1224
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001225 WARN(cur_state != state,
1226 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001227 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001228}
1229
Chris Wilson931872f2012-01-16 23:01:13 +00001230static void assert_plane(struct drm_i915_private *dev_priv,
1231 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001232{
1233 int reg;
1234 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001235 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001236
1237 reg = DSPCNTR(plane);
1238 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001239 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1240 WARN(cur_state != state,
1241 "plane %c assertion failure (expected %s, current %s)\n",
1242 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001243}
1244
Chris Wilson931872f2012-01-16 23:01:13 +00001245#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1246#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1247
Jesse Barnesb24e7172011-01-04 15:09:30 -08001248static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1249 enum pipe pipe)
1250{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001251 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001252 int reg, i;
1253 u32 val;
1254 int cur_pipe;
1255
Ville Syrjälä653e1022013-06-04 13:49:05 +03001256 /* Primary planes are fixed to pipes on gen4+ */
1257 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001258 reg = DSPCNTR(pipe);
1259 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001260 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001261 "plane %c assertion failure, should be disabled but not\n",
1262 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001263 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001264 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001265
Jesse Barnesb24e7172011-01-04 15:09:30 -08001266 /* Need to check both planes against the pipe */
Damien Lespiau08e2a7d2013-07-11 20:10:54 +01001267 for_each_pipe(i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001268 reg = DSPCNTR(i);
1269 val = I915_READ(reg);
1270 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1271 DISPPLANE_SEL_PIPE_SHIFT;
1272 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001273 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1274 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001275 }
1276}
1277
Jesse Barnes19332d72013-03-28 09:55:38 -07001278static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1279 enum pipe pipe)
1280{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001281 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001282 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001283 u32 val;
1284
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001285 if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001286 for_each_sprite(pipe, sprite) {
1287 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001288 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001289 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001290 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001291 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001292 }
1293 } else if (INTEL_INFO(dev)->gen >= 7) {
1294 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001295 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001296 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001297 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001298 plane_name(pipe), pipe_name(pipe));
1299 } else if (INTEL_INFO(dev)->gen >= 5) {
1300 reg = DVSCNTR(pipe);
1301 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001302 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001303 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1304 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001305 }
1306}
1307
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001308static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001309{
1310 u32 val;
1311 bool enabled;
1312
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001313 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001314
Jesse Barnes92f25842011-01-04 15:09:34 -08001315 val = I915_READ(PCH_DREF_CONTROL);
1316 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1317 DREF_SUPERSPREAD_SOURCE_MASK));
1318 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1319}
1320
Daniel Vetterab9412b2013-05-03 11:49:46 +02001321static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1322 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001323{
1324 int reg;
1325 u32 val;
1326 bool enabled;
1327
Daniel Vetterab9412b2013-05-03 11:49:46 +02001328 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001329 val = I915_READ(reg);
1330 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001331 WARN(enabled,
1332 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1333 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001334}
1335
Keith Packard4e634382011-08-06 10:39:45 -07001336static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1337 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001338{
1339 if ((val & DP_PORT_EN) == 0)
1340 return false;
1341
1342 if (HAS_PCH_CPT(dev_priv->dev)) {
1343 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1344 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1345 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1346 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001347 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1348 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1349 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001350 } else {
1351 if ((val & DP_PIPE_MASK) != (pipe << 30))
1352 return false;
1353 }
1354 return true;
1355}
1356
Keith Packard1519b992011-08-06 10:35:34 -07001357static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1358 enum pipe pipe, u32 val)
1359{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001360 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001361 return false;
1362
1363 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001364 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001365 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001366 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1367 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1368 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001369 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001370 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001371 return false;
1372 }
1373 return true;
1374}
1375
1376static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1377 enum pipe pipe, u32 val)
1378{
1379 if ((val & LVDS_PORT_EN) == 0)
1380 return false;
1381
1382 if (HAS_PCH_CPT(dev_priv->dev)) {
1383 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1384 return false;
1385 } else {
1386 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1387 return false;
1388 }
1389 return true;
1390}
1391
1392static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1393 enum pipe pipe, u32 val)
1394{
1395 if ((val & ADPA_DAC_ENABLE) == 0)
1396 return false;
1397 if (HAS_PCH_CPT(dev_priv->dev)) {
1398 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1399 return false;
1400 } else {
1401 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1402 return false;
1403 }
1404 return true;
1405}
1406
Jesse Barnes291906f2011-02-02 12:28:03 -08001407static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001408 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001409{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001410 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001411 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001412 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001413 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001414
Daniel Vetter75c5da22012-09-10 21:58:29 +02001415 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1416 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001417 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001418}
1419
1420static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1421 enum pipe pipe, int reg)
1422{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001423 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001424 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001425 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001426 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001427
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001428 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001429 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001430 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001431}
1432
1433static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1434 enum pipe pipe)
1435{
1436 int reg;
1437 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001438
Keith Packardf0575e92011-07-25 22:12:43 -07001439 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1440 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1441 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001442
1443 reg = PCH_ADPA;
1444 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001445 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001446 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001447 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001448
1449 reg = PCH_LVDS;
1450 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001451 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001452 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001453 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001454
Paulo Zanonie2debe92013-02-18 19:00:27 -03001455 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1456 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1457 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001458}
1459
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001460static void intel_init_dpio(struct drm_device *dev)
1461{
1462 struct drm_i915_private *dev_priv = dev->dev_private;
1463
1464 if (!IS_VALLEYVIEW(dev))
1465 return;
1466
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001467 /*
1468 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1469 * CHV x1 PHY (DP/HDMI D)
1470 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1471 */
1472 if (IS_CHERRYVIEW(dev)) {
1473 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1474 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1475 } else {
1476 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1477 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001478}
1479
1480static void intel_reset_dpio(struct drm_device *dev)
1481{
1482 struct drm_i915_private *dev_priv = dev->dev_private;
1483
1484 if (!IS_VALLEYVIEW(dev))
1485 return;
1486
Imre Deake5cbfbf2014-01-09 17:08:16 +02001487 /*
1488 * Enable the CRI clock source so we can get at the display and the
1489 * reference clock for VGA hotplug / manual detection.
1490 */
Imre Deak404faab2014-01-09 17:08:15 +02001491 I915_WRITE(DPLL(PIPE_B), I915_READ(DPLL(PIPE_B)) |
Imre Deake5cbfbf2014-01-09 17:08:16 +02001492 DPLL_REFA_CLK_ENABLE_VLV |
Imre Deak404faab2014-01-09 17:08:15 +02001493 DPLL_INTEGRATED_CRI_CLK_VLV);
1494
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001495 if (IS_CHERRYVIEW(dev)) {
1496 enum dpio_phy phy;
1497 u32 val;
1498
1499 for (phy = DPIO_PHY0; phy < I915_NUM_PHYS_VLV; phy++) {
1500 /* Poll for phypwrgood signal */
1501 if (wait_for(I915_READ(DISPLAY_PHY_STATUS) &
1502 PHY_POWERGOOD(phy), 1))
1503 DRM_ERROR("Display PHY %d is not power up\n", phy);
1504
1505 /*
1506 * Deassert common lane reset for PHY.
1507 *
1508 * This should only be done on init and resume from S3
1509 * with both PLLs disabled, or we risk losing DPIO and
1510 * PLL synchronization.
1511 */
1512 val = I915_READ(DISPLAY_PHY_CONTROL);
1513 I915_WRITE(DISPLAY_PHY_CONTROL,
1514 PHY_COM_LANE_RESET_DEASSERT(phy, val));
1515 }
1516
1517 } else {
1518 /*
Jesse Barnes57021052014-05-23 13:16:40 -07001519 * If DPIO has already been reset, e.g. by BIOS, just skip all
1520 * this.
1521 */
1522 if (I915_READ(DPIO_CTL) & DPIO_CMNRST)
1523 return;
1524
1525 /*
1526 * From VLV2A0_DP_eDP_HDMI_DPIO_driver_vbios_notes_11.docx:
1527 * Need to assert and de-assert PHY SB reset by gating the
1528 * common lane power, then un-gating it.
1529 * Simply ungating isn't enough to reset the PHY enough to get
1530 * ports and lanes running.
1531 */
1532 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1533 false);
1534 __vlv_set_power_well(dev_priv, PUNIT_POWER_WELL_DPIO_CMN_BC,
1535 true);
1536
1537 /*
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001538 * From VLV2A0_DP_eDP_DPIO_driver_vbios_notes_10.docx -
1539 * 6. De-assert cmn_reset/side_reset. Same as VLV X0.
1540 * a. GUnit 0x2110 bit[0] set to 1 (def 0)
1541 * b. The other bits such as sfr settings / modesel may all
1542 * be set to 0.
1543 *
1544 * This should only be done on init and resume from S3 with
1545 * both PLLs disabled, or we risk losing DPIO and PLL
1546 * synchronization.
1547 */
1548 I915_WRITE(DPIO_CTL, I915_READ(DPIO_CTL) | DPIO_CMNRST);
1549 }
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001550}
1551
Daniel Vetter426115c2013-07-11 22:13:42 +02001552static void vlv_enable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001553{
Daniel Vetter426115c2013-07-11 22:13:42 +02001554 struct drm_device *dev = crtc->base.dev;
1555 struct drm_i915_private *dev_priv = dev->dev_private;
1556 int reg = DPLL(crtc->pipe);
1557 u32 dpll = crtc->config.dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001558
Daniel Vetter426115c2013-07-11 22:13:42 +02001559 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001560
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001561 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001562 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1563
1564 /* PLL is protected by panel, make sure we can write it */
1565 if (IS_MOBILE(dev_priv->dev) && !IS_I830(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001566 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001567
Daniel Vetter426115c2013-07-11 22:13:42 +02001568 I915_WRITE(reg, dpll);
1569 POSTING_READ(reg);
1570 udelay(150);
1571
1572 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1573 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1574
1575 I915_WRITE(DPLL_MD(crtc->pipe), crtc->config.dpll_hw_state.dpll_md);
1576 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001577
1578 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001579 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001580 POSTING_READ(reg);
1581 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001582 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001583 POSTING_READ(reg);
1584 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001585 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001586 POSTING_READ(reg);
1587 udelay(150); /* wait for warmup */
1588}
1589
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001590static void chv_enable_pll(struct intel_crtc *crtc)
1591{
1592 struct drm_device *dev = crtc->base.dev;
1593 struct drm_i915_private *dev_priv = dev->dev_private;
1594 int pipe = crtc->pipe;
1595 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001596 u32 tmp;
1597
1598 assert_pipe_disabled(dev_priv, crtc->pipe);
1599
1600 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1601
1602 mutex_lock(&dev_priv->dpio_lock);
1603
1604 /* Enable back the 10bit clock to display controller */
1605 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1606 tmp |= DPIO_DCLKP_EN;
1607 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1608
1609 /*
1610 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1611 */
1612 udelay(1);
1613
1614 /* Enable PLL */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001615 I915_WRITE(DPLL(pipe), crtc->config.dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001616
1617 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001618 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001619 DRM_ERROR("PLL %d failed to lock\n", pipe);
1620
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001621 /* not sure when this should be written */
1622 I915_WRITE(DPLL_MD(pipe), crtc->config.dpll_hw_state.dpll_md);
1623 POSTING_READ(DPLL_MD(pipe));
1624
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001625 mutex_unlock(&dev_priv->dpio_lock);
1626}
1627
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001629{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001630 struct drm_device *dev = crtc->base.dev;
1631 struct drm_i915_private *dev_priv = dev->dev_private;
1632 int reg = DPLL(crtc->pipe);
1633 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001634
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001635 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001636
1637 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001638 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001639
1640 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001641 if (IS_MOBILE(dev) && !IS_I830(dev))
1642 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001643
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001644 I915_WRITE(reg, dpll);
1645
1646 /* Wait for the clocks to stabilize. */
1647 POSTING_READ(reg);
1648 udelay(150);
1649
1650 if (INTEL_INFO(dev)->gen >= 4) {
1651 I915_WRITE(DPLL_MD(crtc->pipe),
1652 crtc->config.dpll_hw_state.dpll_md);
1653 } else {
1654 /* The pixel multiplier can only be updated once the
1655 * DPLL is enabled and the clocks are stable.
1656 *
1657 * So write it again.
1658 */
1659 I915_WRITE(reg, dpll);
1660 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001661
1662 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001663 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664 POSTING_READ(reg);
1665 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001666 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001667 POSTING_READ(reg);
1668 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001669 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001670 POSTING_READ(reg);
1671 udelay(150); /* wait for warmup */
1672}
1673
1674/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001675 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001676 * @dev_priv: i915 private structure
1677 * @pipe: pipe PLL to disable
1678 *
1679 * Disable the PLL for @pipe, making sure the pipe is off first.
1680 *
1681 * Note! This is for pre-ILK only.
1682 */
Daniel Vetter50b44a42013-06-05 13:34:33 +02001683static void i9xx_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001684{
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001685 /* Don't disable pipe A or pipe A PLLs if needed */
1686 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
1687 return;
1688
1689 /* Make sure the pipe isn't still relying on us */
1690 assert_pipe_disabled(dev_priv, pipe);
1691
Daniel Vetter50b44a42013-06-05 13:34:33 +02001692 I915_WRITE(DPLL(pipe), 0);
1693 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001694}
1695
Jesse Barnesf6071162013-10-01 10:41:38 -07001696static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1697{
1698 u32 val = 0;
1699
1700 /* Make sure the pipe isn't still relying on us */
1701 assert_pipe_disabled(dev_priv, pipe);
1702
Imre Deake5cbfbf2014-01-09 17:08:16 +02001703 /*
1704 * Leave integrated clock source and reference clock enabled for pipe B.
1705 * The latter is needed for VGA hotplug / manual detection.
1706 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001707 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001708 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001709 I915_WRITE(DPLL(pipe), val);
1710 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001711
1712}
1713
1714static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1715{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001716 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001717 u32 val;
1718
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001719 /* Make sure the pipe isn't still relying on us */
1720 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001721
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001722 /* Set PLL en = 0 */
1723 val = DPLL_SSC_REF_CLOCK_CHV;
1724 if (pipe != PIPE_A)
1725 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1726 I915_WRITE(DPLL(pipe), val);
1727 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001728
1729 mutex_lock(&dev_priv->dpio_lock);
1730
1731 /* Disable 10bit clock to display controller */
1732 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1733 val &= ~DPIO_DCLKP_EN;
1734 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1735
1736 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001737}
1738
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001739void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1740 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001741{
1742 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001743 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001744
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001745 switch (dport->port) {
1746 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001747 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001748 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001749 break;
1750 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001751 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001752 dpll_reg = DPLL(0);
1753 break;
1754 case PORT_D:
1755 port_mask = DPLL_PORTD_READY_MASK;
1756 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001757 break;
1758 default:
1759 BUG();
1760 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001761
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001762 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001763 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001764 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001765}
1766
Daniel Vetterb14b1052014-04-24 23:55:13 +02001767static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1768{
1769 struct drm_device *dev = crtc->base.dev;
1770 struct drm_i915_private *dev_priv = dev->dev_private;
1771 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1772
1773 WARN_ON(!pll->refcount);
1774 if (pll->active == 0) {
1775 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1776 WARN_ON(pll->on);
1777 assert_shared_dpll_disabled(dev_priv, pll);
1778
1779 pll->mode_set(dev_priv, pll);
1780 }
1781}
1782
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001783/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001784 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001785 * @dev_priv: i915 private structure
1786 * @pipe: pipe PLL to enable
1787 *
1788 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1789 * drives the transcoder clock.
1790 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001791static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001792{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001793 struct drm_device *dev = crtc->base.dev;
1794 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001795 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001796
Daniel Vetter87a875b2013-06-05 13:34:19 +02001797 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001798 return;
1799
1800 if (WARN_ON(pll->refcount == 0))
1801 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001802
Daniel Vetter46edb022013-06-05 13:34:12 +02001803 DRM_DEBUG_KMS("enable %s (active %d, on? %d)for crtc %d\n",
1804 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001805 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001806
Daniel Vettercdbd2312013-06-05 13:34:03 +02001807 if (pll->active++) {
1808 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001809 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001810 return;
1811 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001812 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001813
Daniel Vetter46edb022013-06-05 13:34:12 +02001814 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001815 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001816 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001817}
1818
Daniel Vettere2b78262013-06-07 23:10:03 +02001819static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001820{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001821 struct drm_device *dev = crtc->base.dev;
1822 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001823 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001824
Jesse Barnes92f25842011-01-04 15:09:34 -08001825 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001826 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001827 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001828 return;
1829
Chris Wilson48da64a2012-05-13 20:16:12 +01001830 if (WARN_ON(pll->refcount == 0))
1831 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001832
Daniel Vetter46edb022013-06-05 13:34:12 +02001833 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1834 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001835 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001836
Chris Wilson48da64a2012-05-13 20:16:12 +01001837 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001838 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001839 return;
1840 }
1841
Daniel Vettere9d69442013-06-05 13:34:15 +02001842 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001843 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001844 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001845 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001846
Daniel Vetter46edb022013-06-05 13:34:12 +02001847 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001848 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001849 pll->on = false;
Jesse Barnes92f25842011-01-04 15:09:34 -08001850}
1851
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001852static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1853 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001854{
Daniel Vetter23670b322012-11-01 09:15:30 +01001855 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001856 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001858 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001859
1860 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001861 BUG_ON(INTEL_INFO(dev)->gen < 5);
Jesse Barnes040484a2011-01-03 12:14:26 -08001862
1863 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001864 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001865 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001866
1867 /* FDI must be feeding us bits for PCH ports */
1868 assert_fdi_tx_enabled(dev_priv, pipe);
1869 assert_fdi_rx_enabled(dev_priv, pipe);
1870
Daniel Vetter23670b322012-11-01 09:15:30 +01001871 if (HAS_PCH_CPT(dev)) {
1872 /* Workaround: Set the timing override bit before enabling the
1873 * pch transcoder. */
1874 reg = TRANS_CHICKEN2(pipe);
1875 val = I915_READ(reg);
1876 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1877 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001878 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001879
Daniel Vetterab9412b2013-05-03 11:49:46 +02001880 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001881 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001882 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001883
1884 if (HAS_PCH_IBX(dev_priv->dev)) {
1885 /*
1886 * make the BPC in transcoder be consistent with
1887 * that in pipeconf reg.
1888 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001889 val &= ~PIPECONF_BPC_MASK;
1890 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001891 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001892
1893 val &= ~TRANS_INTERLACE_MASK;
1894 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001895 if (HAS_PCH_IBX(dev_priv->dev) &&
1896 intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO))
1897 val |= TRANS_LEGACY_INTERLACED_ILK;
1898 else
1899 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001900 else
1901 val |= TRANS_PROGRESSIVE;
1902
Jesse Barnes040484a2011-01-03 12:14:26 -08001903 I915_WRITE(reg, val | TRANS_ENABLE);
1904 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001905 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001906}
1907
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001908static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001909 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001910{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001912
1913 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001914 BUG_ON(INTEL_INFO(dev_priv->dev)->gen < 5);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001915
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001916 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001917 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001918 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001919
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001920 /* Workaround: set timing override bit. */
1921 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001922 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001923 I915_WRITE(_TRANSA_CHICKEN2, val);
1924
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001925 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001926 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001927
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001928 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1929 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001930 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001931 else
1932 val |= TRANS_PROGRESSIVE;
1933
Daniel Vetterab9412b2013-05-03 11:49:46 +02001934 I915_WRITE(LPT_TRANSCONF, val);
1935 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001936 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001937}
1938
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001939static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1940 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001941{
Daniel Vetter23670b322012-11-01 09:15:30 +01001942 struct drm_device *dev = dev_priv->dev;
1943 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001944
1945 /* FDI relies on the transcoder */
1946 assert_fdi_tx_disabled(dev_priv, pipe);
1947 assert_fdi_rx_disabled(dev_priv, pipe);
1948
Jesse Barnes291906f2011-02-02 12:28:03 -08001949 /* Ports must be off as well */
1950 assert_pch_ports_disabled(dev_priv, pipe);
1951
Daniel Vetterab9412b2013-05-03 11:49:46 +02001952 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001953 val = I915_READ(reg);
1954 val &= ~TRANS_ENABLE;
1955 I915_WRITE(reg, val);
1956 /* wait for PCH transcoder off, transcoder state */
1957 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001958 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001959
1960 if (!HAS_PCH_IBX(dev)) {
1961 /* Workaround: Clear the timing override chicken bit again. */
1962 reg = TRANS_CHICKEN2(pipe);
1963 val = I915_READ(reg);
1964 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1965 I915_WRITE(reg, val);
1966 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001967}
1968
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001969static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001970{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001971 u32 val;
1972
Daniel Vetterab9412b2013-05-03 11:49:46 +02001973 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001974 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001975 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001976 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001977 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001978 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001979
1980 /* Workaround: clear timing override bit. */
1981 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001982 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001983 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001984}
1985
1986/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001987 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001988 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001989 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001990 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001991 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001992 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001993static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001994{
Paulo Zanoni03722642014-01-17 13:51:09 -02001995 struct drm_device *dev = crtc->base.dev;
1996 struct drm_i915_private *dev_priv = dev->dev_private;
1997 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001998 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1999 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002000 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002001 int reg;
2002 u32 val;
2003
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002004 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002005 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002006 assert_sprites_disabled(dev_priv, pipe);
2007
Paulo Zanoni681e5812012-12-06 11:12:38 -02002008 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002009 pch_transcoder = TRANSCODER_A;
2010 else
2011 pch_transcoder = pipe;
2012
Jesse Barnesb24e7172011-01-04 15:09:30 -08002013 /*
2014 * A pipe without a PLL won't actually be able to drive bits from
2015 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2016 * need the check.
2017 */
2018 if (!HAS_PCH_SPLIT(dev_priv->dev))
Paulo Zanonifbf32182014-01-17 13:51:11 -02002019 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002020 assert_dsi_pll_enabled(dev_priv);
2021 else
2022 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002023 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02002024 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002025 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002026 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002027 assert_fdi_tx_pll_enabled(dev_priv,
2028 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002029 }
2030 /* FIXME: assert CPU port conditions for SNB+ */
2031 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002032
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002033 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002034 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002035 if (val & PIPECONF_ENABLE) {
2036 WARN_ON(!(pipe == PIPE_A &&
2037 dev_priv->quirks & QUIRK_PIPEA_FORCE));
Chris Wilson00d70b12011-03-17 07:18:29 +00002038 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002039 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002040
2041 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002042 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002043}
2044
2045/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002046 * intel_disable_pipe - disable a pipe, asserting requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047 * @dev_priv: i915 private structure
2048 * @pipe: pipe to disable
2049 *
2050 * Disable @pipe, making sure that various hardware specific requirements
2051 * are met, if applicable, e.g. plane disabled, panel fitter off, etc.
2052 *
2053 * @pipe should be %PIPE_A or %PIPE_B.
2054 *
2055 * Will wait until the pipe has shut down before returning.
2056 */
2057static void intel_disable_pipe(struct drm_i915_private *dev_priv,
2058 enum pipe pipe)
2059{
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002060 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2061 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002062 int reg;
2063 u32 val;
2064
2065 /*
2066 * Make sure planes won't keep trying to pump pixels to us,
2067 * or we might hang the display.
2068 */
2069 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002070 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002071 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002072
2073 /* Don't disable pipe A or pipe A PLLs if needed */
2074 if (pipe == PIPE_A && (dev_priv->quirks & QUIRK_PIPEA_FORCE))
2075 return;
2076
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002077 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002078 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002079 if ((val & PIPECONF_ENABLE) == 0)
2080 return;
2081
2082 I915_WRITE(reg, val & ~PIPECONF_ENABLE);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002083 intel_wait_for_pipe_off(dev_priv->dev, pipe);
2084}
2085
Keith Packardd74362c2011-07-28 14:47:14 -07002086/*
2087 * Plane regs are double buffered, going from enabled->disabled needs a
2088 * trigger in order to latch. The display address reg provides this.
2089 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002090void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2091 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002092{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002093 struct drm_device *dev = dev_priv->dev;
2094 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002095
2096 I915_WRITE(reg, I915_READ(reg));
2097 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002098}
2099
Jesse Barnesb24e7172011-01-04 15:09:30 -08002100/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002101 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102 * @dev_priv: i915 private structure
2103 * @plane: plane to enable
2104 * @pipe: pipe being fed
2105 *
2106 * Enable @plane on @pipe, making sure that @pipe is running first.
2107 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002108static void intel_enable_primary_hw_plane(struct drm_i915_private *dev_priv,
2109 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002111 struct intel_crtc *intel_crtc =
2112 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002113 int reg;
2114 u32 val;
2115
2116 /* If the pipe isn't enabled, we can't pump pixels and may hang */
2117 assert_pipe_enabled(dev_priv, pipe);
2118
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002119 if (intel_crtc->primary_enabled)
2120 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002121
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002122 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002123
Jesse Barnesb24e7172011-01-04 15:09:30 -08002124 reg = DSPCNTR(plane);
2125 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002126 WARN_ON(val & DISPLAY_PLANE_ENABLE);
Chris Wilson00d70b12011-03-17 07:18:29 +00002127
2128 I915_WRITE(reg, val | DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002129 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002130}
2131
Jesse Barnesb24e7172011-01-04 15:09:30 -08002132/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002133 * intel_disable_primary_hw_plane - disable the primary hardware plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002134 * @dev_priv: i915 private structure
2135 * @plane: plane to disable
2136 * @pipe: pipe consuming the data
2137 *
2138 * Disable @plane; should be an independent operation.
2139 */
Matt Roper262ca2b2014-03-18 17:22:55 -07002140static void intel_disable_primary_hw_plane(struct drm_i915_private *dev_priv,
2141 enum plane plane, enum pipe pipe)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002142{
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002143 struct intel_crtc *intel_crtc =
2144 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145 int reg;
2146 u32 val;
2147
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002148 if (!intel_crtc->primary_enabled)
2149 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002150
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002151 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002152
Jesse Barnesb24e7172011-01-04 15:09:30 -08002153 reg = DSPCNTR(plane);
2154 val = I915_READ(reg);
Ville Syrjälä10efa932014-04-28 15:53:25 +03002155 WARN_ON((val & DISPLAY_PLANE_ENABLE) == 0);
Chris Wilson00d70b12011-03-17 07:18:29 +00002156
2157 I915_WRITE(reg, val & ~DISPLAY_PLANE_ENABLE);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002158 intel_flush_primary_plane(dev_priv, plane);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159}
2160
Chris Wilson693db182013-03-05 14:52:39 +00002161static bool need_vtd_wa(struct drm_device *dev)
2162{
2163#ifdef CONFIG_INTEL_IOMMU
2164 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2165 return true;
2166#endif
2167 return false;
2168}
2169
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002170static int intel_align_height(struct drm_device *dev, int height, bool tiled)
2171{
2172 int tile_height;
2173
2174 tile_height = tiled ? (IS_GEN2(dev) ? 16 : 8) : 1;
2175 return ALIGN(height, tile_height);
2176}
2177
Chris Wilson127bd2a2010-07-23 23:32:05 +01002178int
Chris Wilson48b956c2010-09-14 12:50:34 +01002179intel_pin_and_fence_fb_obj(struct drm_device *dev,
Chris Wilson05394f32010-11-08 19:18:58 +00002180 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002181 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002182{
Chris Wilsonce453d82011-02-21 14:43:56 +00002183 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002184 u32 alignment;
2185 int ret;
2186
Chris Wilson05394f32010-11-08 19:18:58 +00002187 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002188 case I915_TILING_NONE:
Chris Wilson534843d2010-07-05 18:01:46 +01002189 if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
2190 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002191 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002192 alignment = 4 * 1024;
2193 else
2194 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002195 break;
2196 case I915_TILING_X:
2197 /* pin() will align the object as required by fence */
2198 alignment = 0;
2199 break;
2200 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002201 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002202 return -EINVAL;
2203 default:
2204 BUG();
2205 }
2206
Chris Wilson693db182013-03-05 14:52:39 +00002207 /* Note that the w/a also requires 64 PTE of padding following the
2208 * bo. We currently fill all unused PTE with the shadow page and so
2209 * we should always have valid PTE following the scanout preventing
2210 * the VT-d warning.
2211 */
2212 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2213 alignment = 256 * 1024;
2214
Chris Wilsonce453d82011-02-21 14:43:56 +00002215 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002216 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002217 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002218 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002219
2220 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2221 * fence, whereas 965+ only requires a fence if using
2222 * framebuffer compression. For simplicity, we always install
2223 * a fence as the cost is not that onerous.
2224 */
Chris Wilson06d98132012-04-17 15:31:24 +01002225 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002226 if (ret)
2227 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002228
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002229 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002230
Chris Wilsonce453d82011-02-21 14:43:56 +00002231 dev_priv->mm.interruptible = true;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002232 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002233
2234err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002235 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002236err_interruptible:
2237 dev_priv->mm.interruptible = true;
Chris Wilson48b956c2010-09-14 12:50:34 +01002238 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002239}
2240
Chris Wilson1690e1e2011-12-14 13:57:08 +01002241void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2242{
2243 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002244 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002245}
2246
Daniel Vetterc2c75132012-07-05 12:17:30 +02002247/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2248 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002249unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2250 unsigned int tiling_mode,
2251 unsigned int cpp,
2252 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002253{
Chris Wilsonbc752862013-02-21 20:04:31 +00002254 if (tiling_mode != I915_TILING_NONE) {
2255 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002256
Chris Wilsonbc752862013-02-21 20:04:31 +00002257 tile_rows = *y / 8;
2258 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002259
Chris Wilsonbc752862013-02-21 20:04:31 +00002260 tiles = *x / (512/cpp);
2261 *x %= 512/cpp;
2262
2263 return tile_rows * pitch * 8 + tiles * 4096;
2264 } else {
2265 unsigned int offset;
2266
2267 offset = *y * pitch + *x * cpp;
2268 *y = 0;
2269 *x = (offset & 4095) / cpp;
2270 return offset & -4096;
2271 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002272}
2273
Jesse Barnes46f297f2014-03-07 08:57:48 -08002274int intel_format_to_fourcc(int format)
2275{
2276 switch (format) {
2277 case DISPPLANE_8BPP:
2278 return DRM_FORMAT_C8;
2279 case DISPPLANE_BGRX555:
2280 return DRM_FORMAT_XRGB1555;
2281 case DISPPLANE_BGRX565:
2282 return DRM_FORMAT_RGB565;
2283 default:
2284 case DISPPLANE_BGRX888:
2285 return DRM_FORMAT_XRGB8888;
2286 case DISPPLANE_RGBX888:
2287 return DRM_FORMAT_XBGR8888;
2288 case DISPPLANE_BGRX101010:
2289 return DRM_FORMAT_XRGB2101010;
2290 case DISPPLANE_RGBX101010:
2291 return DRM_FORMAT_XBGR2101010;
2292 }
2293}
2294
Jesse Barnes484b41d2014-03-07 08:57:55 -08002295static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002296 struct intel_plane_config *plane_config)
2297{
2298 struct drm_device *dev = crtc->base.dev;
2299 struct drm_i915_gem_object *obj = NULL;
2300 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2301 u32 base = plane_config->base;
2302
Chris Wilsonff2652e2014-03-10 08:07:02 +00002303 if (plane_config->size == 0)
2304 return false;
2305
Jesse Barnes46f297f2014-03-07 08:57:48 -08002306 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2307 plane_config->size);
2308 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002309 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002310
2311 if (plane_config->tiled) {
2312 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002313 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002314 }
2315
Dave Airlie66e514c2014-04-03 07:51:54 +10002316 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2317 mode_cmd.width = crtc->base.primary->fb->width;
2318 mode_cmd.height = crtc->base.primary->fb->height;
2319 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002320
2321 mutex_lock(&dev->struct_mutex);
2322
Dave Airlie66e514c2014-04-03 07:51:54 +10002323 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002324 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002325 DRM_DEBUG_KMS("intel fb init failed\n");
2326 goto out_unref_obj;
2327 }
2328
2329 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002330
2331 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2332 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002333
2334out_unref_obj:
2335 drm_gem_object_unreference(&obj->base);
2336 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002337 return false;
2338}
2339
2340static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2341 struct intel_plane_config *plane_config)
2342{
2343 struct drm_device *dev = intel_crtc->base.dev;
2344 struct drm_crtc *c;
2345 struct intel_crtc *i;
2346 struct intel_framebuffer *fb;
2347
Dave Airlie66e514c2014-04-03 07:51:54 +10002348 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002349 return;
2350
2351 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2352 return;
2353
Dave Airlie66e514c2014-04-03 07:51:54 +10002354 kfree(intel_crtc->base.primary->fb);
2355 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002356
2357 /*
2358 * Failed to alloc the obj, check to see if we should share
2359 * an fb with another CRTC instead
2360 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002361 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002362 i = to_intel_crtc(c);
2363
2364 if (c == &intel_crtc->base)
2365 continue;
2366
Dave Airlie66e514c2014-04-03 07:51:54 +10002367 if (!i->active || !c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002368 continue;
2369
Dave Airlie66e514c2014-04-03 07:51:54 +10002370 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002371 if (i915_gem_obj_ggtt_offset(fb->obj) == plane_config->base) {
Dave Airlie66e514c2014-04-03 07:51:54 +10002372 drm_framebuffer_reference(c->primary->fb);
2373 intel_crtc->base.primary->fb = c->primary->fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002374 break;
2375 }
2376 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002377}
2378
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002379static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2380 struct drm_framebuffer *fb,
2381 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002382{
2383 struct drm_device *dev = crtc->dev;
2384 struct drm_i915_private *dev_priv = dev->dev_private;
2385 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2386 struct intel_framebuffer *intel_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00002387 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002388 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002389 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002390 u32 dspcntr;
Chris Wilson5eddb702010-09-11 13:48:45 +01002391 u32 reg;
Jesse Barnes81255562010-08-02 12:07:50 -07002392
Jesse Barnes81255562010-08-02 12:07:50 -07002393 intel_fb = to_intel_framebuffer(fb);
2394 obj = intel_fb->obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002395
Chris Wilson5eddb702010-09-11 13:48:45 +01002396 reg = DSPCNTR(plane);
2397 dspcntr = I915_READ(reg);
Jesse Barnes81255562010-08-02 12:07:50 -07002398 /* Mask out pixel format bits in case we change it */
2399 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002400 switch (fb->pixel_format) {
2401 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002402 dspcntr |= DISPPLANE_8BPP;
2403 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002404 case DRM_FORMAT_XRGB1555:
2405 case DRM_FORMAT_ARGB1555:
2406 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002407 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002408 case DRM_FORMAT_RGB565:
2409 dspcntr |= DISPPLANE_BGRX565;
2410 break;
2411 case DRM_FORMAT_XRGB8888:
2412 case DRM_FORMAT_ARGB8888:
2413 dspcntr |= DISPPLANE_BGRX888;
2414 break;
2415 case DRM_FORMAT_XBGR8888:
2416 case DRM_FORMAT_ABGR8888:
2417 dspcntr |= DISPPLANE_RGBX888;
2418 break;
2419 case DRM_FORMAT_XRGB2101010:
2420 case DRM_FORMAT_ARGB2101010:
2421 dspcntr |= DISPPLANE_BGRX101010;
2422 break;
2423 case DRM_FORMAT_XBGR2101010:
2424 case DRM_FORMAT_ABGR2101010:
2425 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002426 break;
2427 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002428 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002429 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002430
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002431 if (INTEL_INFO(dev)->gen >= 4) {
Chris Wilson05394f32010-11-08 19:18:58 +00002432 if (obj->tiling_mode != I915_TILING_NONE)
Jesse Barnes81255562010-08-02 12:07:50 -07002433 dspcntr |= DISPPLANE_TILED;
2434 else
2435 dspcntr &= ~DISPPLANE_TILED;
2436 }
2437
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002438 if (IS_G4X(dev))
2439 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2440
Chris Wilson5eddb702010-09-11 13:48:45 +01002441 I915_WRITE(reg, dspcntr);
Jesse Barnes81255562010-08-02 12:07:50 -07002442
Daniel Vettere506a0c2012-07-05 12:17:29 +02002443 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Jesse Barnes81255562010-08-02 12:07:50 -07002444
Daniel Vetterc2c75132012-07-05 12:17:30 +02002445 if (INTEL_INFO(dev)->gen >= 4) {
2446 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002447 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2448 fb->bits_per_pixel / 8,
2449 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002450 linear_offset -= intel_crtc->dspaddr_offset;
2451 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002452 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002454
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002455 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2456 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2457 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002458 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002459 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002460 I915_WRITE(DSPSURF(plane),
2461 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002462 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002463 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002464 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002465 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002466 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002467}
2468
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002469static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2470 struct drm_framebuffer *fb,
2471 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002472{
2473 struct drm_device *dev = crtc->dev;
2474 struct drm_i915_private *dev_priv = dev->dev_private;
2475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2476 struct intel_framebuffer *intel_fb;
2477 struct drm_i915_gem_object *obj;
2478 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002479 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002480 u32 dspcntr;
2481 u32 reg;
2482
Jesse Barnes17638cd2011-06-24 12:19:23 -07002483 intel_fb = to_intel_framebuffer(fb);
2484 obj = intel_fb->obj;
2485
2486 reg = DSPCNTR(plane);
2487 dspcntr = I915_READ(reg);
2488 /* Mask out pixel format bits in case we change it */
2489 dspcntr &= ~DISPPLANE_PIXFORMAT_MASK;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002490 switch (fb->pixel_format) {
2491 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002492 dspcntr |= DISPPLANE_8BPP;
2493 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002494 case DRM_FORMAT_RGB565:
2495 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002496 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002497 case DRM_FORMAT_XRGB8888:
2498 case DRM_FORMAT_ARGB8888:
2499 dspcntr |= DISPPLANE_BGRX888;
2500 break;
2501 case DRM_FORMAT_XBGR8888:
2502 case DRM_FORMAT_ABGR8888:
2503 dspcntr |= DISPPLANE_RGBX888;
2504 break;
2505 case DRM_FORMAT_XRGB2101010:
2506 case DRM_FORMAT_ARGB2101010:
2507 dspcntr |= DISPPLANE_BGRX101010;
2508 break;
2509 case DRM_FORMAT_XBGR2101010:
2510 case DRM_FORMAT_ABGR2101010:
2511 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002512 break;
2513 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002514 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002515 }
2516
2517 if (obj->tiling_mode != I915_TILING_NONE)
2518 dspcntr |= DISPPLANE_TILED;
2519 else
2520 dspcntr &= ~DISPPLANE_TILED;
2521
Ville Syrjäläb42c6002013-11-03 13:47:27 +02002522 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002523 dspcntr &= ~DISPPLANE_TRICKLE_FEED_DISABLE;
2524 else
2525 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002526
2527 I915_WRITE(reg, dspcntr);
2528
Daniel Vettere506a0c2012-07-05 12:17:29 +02002529 linear_offset = y * fb->pitches[0] + x * (fb->bits_per_pixel / 8);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002530 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002531 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
2532 fb->bits_per_pixel / 8,
2533 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002534 linear_offset -= intel_crtc->dspaddr_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002535
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002536 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2537 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2538 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002539 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002540 I915_WRITE(DSPSURF(plane),
2541 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002542 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002543 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2544 } else {
2545 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2546 I915_WRITE(DSPLINOFF(plane), linear_offset);
2547 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002548 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002549}
2550
2551/* Assume fb object is pinned & idle & fenced and just update base pointers */
2552static int
2553intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2554 int x, int y, enum mode_set_atomic state)
2555{
2556 struct drm_device *dev = crtc->dev;
2557 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002558
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002559 if (dev_priv->display.disable_fbc)
2560 dev_priv->display.disable_fbc(dev);
Daniel Vetter3dec0092010-08-20 21:40:52 +02002561 intel_increase_pllclock(crtc);
Jesse Barnes81255562010-08-02 12:07:50 -07002562
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002563 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2564
2565 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002566}
2567
Ville Syrjälä96a02912013-02-18 19:08:49 +02002568void intel_display_handle_reset(struct drm_device *dev)
2569{
2570 struct drm_i915_private *dev_priv = dev->dev_private;
2571 struct drm_crtc *crtc;
2572
2573 /*
2574 * Flips in the rings have been nuked by the reset,
2575 * so complete all pending flips so that user space
2576 * will get its events and not get stuck.
2577 *
2578 * Also update the base address of all primary
2579 * planes to the the last fb to make sure we're
2580 * showing the correct fb after a reset.
2581 *
2582 * Need to make two loops over the crtcs so that we
2583 * don't try to grab a crtc mutex before the
2584 * pending_flip_queue really got woken up.
2585 */
2586
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002587 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2589 enum plane plane = intel_crtc->plane;
2590
2591 intel_prepare_page_flip(dev, plane);
2592 intel_finish_page_flip_plane(dev, plane);
2593 }
2594
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002595 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2597
Rob Clark51fd3712013-11-19 12:10:12 -05002598 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002599 /*
2600 * FIXME: Once we have proper support for primary planes (and
2601 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002602 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002603 */
Matt Roperf4510a22014-04-01 15:22:40 -07002604 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002605 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002606 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002607 crtc->x,
2608 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002609 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002610 }
2611}
2612
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002613static int
Chris Wilson14667a42012-04-03 17:58:35 +01002614intel_finish_fb(struct drm_framebuffer *old_fb)
2615{
2616 struct drm_i915_gem_object *obj = to_intel_framebuffer(old_fb)->obj;
2617 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2618 bool was_interruptible = dev_priv->mm.interruptible;
2619 int ret;
2620
Chris Wilson14667a42012-04-03 17:58:35 +01002621 /* Big Hammer, we also need to ensure that any pending
2622 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2623 * current scanout is retired before unpinning the old
2624 * framebuffer.
2625 *
2626 * This should only fail upon a hung GPU, in which case we
2627 * can safely continue.
2628 */
2629 dev_priv->mm.interruptible = false;
2630 ret = i915_gem_object_finish_gpu(obj);
2631 dev_priv->mm.interruptible = was_interruptible;
2632
2633 return ret;
2634}
2635
Chris Wilson7d5e3792014-03-04 13:15:08 +00002636static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2637{
2638 struct drm_device *dev = crtc->dev;
2639 struct drm_i915_private *dev_priv = dev->dev_private;
2640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2641 unsigned long flags;
2642 bool pending;
2643
2644 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2645 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2646 return false;
2647
2648 spin_lock_irqsave(&dev->event_lock, flags);
2649 pending = to_intel_crtc(crtc)->unpin_work != NULL;
2650 spin_unlock_irqrestore(&dev->event_lock, flags);
2651
2652 return pending;
2653}
2654
Chris Wilson14667a42012-04-03 17:58:35 +01002655static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002656intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002657 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002658{
2659 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002660 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002661 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter94352cf2012-07-05 22:51:56 +02002662 struct drm_framebuffer *old_fb;
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002663 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002664
Chris Wilson7d5e3792014-03-04 13:15:08 +00002665 if (intel_crtc_has_pending_flip(crtc)) {
2666 DRM_ERROR("pipe is still busy with an old pageflip\n");
2667 return -EBUSY;
2668 }
2669
Jesse Barnes79e53942008-11-07 14:24:08 -08002670 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002671 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002672 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002673 return 0;
2674 }
2675
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002676 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002677 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2678 plane_name(intel_crtc->plane),
2679 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002680 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002681 }
2682
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002683 mutex_lock(&dev->struct_mutex);
Chris Wilson265db952010-09-20 15:41:01 +01002684 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002685 to_intel_framebuffer(fb)->obj,
Chris Wilson919926a2010-11-12 13:42:53 +00002686 NULL);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002687 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002688 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002689 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002690 return ret;
2691 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002692
Damien Lespiaubb2043d2013-09-30 14:21:49 +01002693 /*
2694 * Update pipe size and adjust fitter if needed: the reason for this is
2695 * that in compute_mode_changes we check the native mode (not the pfit
2696 * mode) to see if we can flip rather than do a full mode set. In the
2697 * fastboot case, we'll flip, but if we don't update the pipesrc and
2698 * pfit state, we'll end up with a big fb scanned out into the wrong
2699 * sized surface.
2700 *
2701 * To fix this properly, we need to hoist the checks up into
2702 * compute_mode_changes (or above), check the actual pfit state and
2703 * whether the platform allows pfit disable with pipe active, and only
2704 * then update the pipesrc and pfit state, even on the flip path.
2705 */
Jani Nikulad330a952014-01-21 11:24:25 +02002706 if (i915.fastboot) {
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002707 const struct drm_display_mode *adjusted_mode =
2708 &intel_crtc->config.adjusted_mode;
2709
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002710 I915_WRITE(PIPESRC(intel_crtc->pipe),
Damien Lespiaud7bf63f2013-09-30 14:21:50 +01002711 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2712 (adjusted_mode->crtc_vdisplay - 1));
Chris Wilsonfd4daa92013-08-27 17:04:17 +01002713 if (!intel_crtc->config.pch_pfit.enabled &&
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002714 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2715 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
2716 I915_WRITE(PF_CTL(intel_crtc->pipe), 0);
2717 I915_WRITE(PF_WIN_POS(intel_crtc->pipe), 0);
2718 I915_WRITE(PF_WIN_SZ(intel_crtc->pipe), 0);
2719 }
Jesse Barnes0637d602013-12-19 10:48:01 -08002720 intel_crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2721 intel_crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002722 }
2723
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002724 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002725
Matt Roperf4510a22014-04-01 15:22:40 -07002726 old_fb = crtc->primary->fb;
2727 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002728 crtc->x = x;
2729 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002730
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002731 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002732 if (intel_crtc->active && old_fb != fb)
2733 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002734 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002735 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002736 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002737 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002738
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002739 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002740 intel_update_fbc(dev);
Rodrigo Vivi49065572013-07-11 18:45:05 -03002741 intel_edp_psr_update(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002742 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002743
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002744 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002745}
2746
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002747static void intel_fdi_normal_train(struct drm_crtc *crtc)
2748{
2749 struct drm_device *dev = crtc->dev;
2750 struct drm_i915_private *dev_priv = dev->dev_private;
2751 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2752 int pipe = intel_crtc->pipe;
2753 u32 reg, temp;
2754
2755 /* enable normal train */
2756 reg = FDI_TX_CTL(pipe);
2757 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002758 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002759 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2760 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002761 } else {
2762 temp &= ~FDI_LINK_TRAIN_NONE;
2763 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002764 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002765 I915_WRITE(reg, temp);
2766
2767 reg = FDI_RX_CTL(pipe);
2768 temp = I915_READ(reg);
2769 if (HAS_PCH_CPT(dev)) {
2770 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2771 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2772 } else {
2773 temp &= ~FDI_LINK_TRAIN_NONE;
2774 temp |= FDI_LINK_TRAIN_NONE;
2775 }
2776 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2777
2778 /* wait one idle pattern time */
2779 POSTING_READ(reg);
2780 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002781
2782 /* IVB wants error correction enabled */
2783 if (IS_IVYBRIDGE(dev))
2784 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2785 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002786}
2787
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002788static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002789{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002790 return crtc->base.enabled && crtc->active &&
2791 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002792}
2793
Daniel Vetter01a415f2012-10-27 15:58:40 +02002794static void ivb_modeset_global_resources(struct drm_device *dev)
2795{
2796 struct drm_i915_private *dev_priv = dev->dev_private;
2797 struct intel_crtc *pipe_B_crtc =
2798 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2799 struct intel_crtc *pipe_C_crtc =
2800 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2801 uint32_t temp;
2802
Daniel Vetter1e833f42013-02-19 22:31:57 +01002803 /*
2804 * When everything is off disable fdi C so that we could enable fdi B
2805 * with all lanes. Note that we don't care about enabled pipes without
2806 * an enabled pch encoder.
2807 */
2808 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2809 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002810 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2811 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
2812
2813 temp = I915_READ(SOUTH_CHICKEN1);
2814 temp &= ~FDI_BC_BIFURCATION_SELECT;
2815 DRM_DEBUG_KMS("disabling fdi C rx\n");
2816 I915_WRITE(SOUTH_CHICKEN1, temp);
2817 }
2818}
2819
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002820/* The FDI link training functions for ILK/Ibexpeak. */
2821static void ironlake_fdi_link_train(struct drm_crtc *crtc)
2822{
2823 struct drm_device *dev = crtc->dev;
2824 struct drm_i915_private *dev_priv = dev->dev_private;
2825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2826 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01002827 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002828
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03002829 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002830 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08002831
Adam Jacksone1a44742010-06-25 15:32:14 -04002832 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2833 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002834 reg = FDI_RX_IMR(pipe);
2835 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002836 temp &= ~FDI_RX_SYMBOL_LOCK;
2837 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002838 I915_WRITE(reg, temp);
2839 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002840 udelay(150);
2841
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002842 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002843 reg = FDI_TX_CTL(pipe);
2844 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002845 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2846 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002847 temp &= ~FDI_LINK_TRAIN_NONE;
2848 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002849 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002850
Chris Wilson5eddb702010-09-11 13:48:45 +01002851 reg = FDI_RX_CTL(pipe);
2852 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002853 temp &= ~FDI_LINK_TRAIN_NONE;
2854 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01002855 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2856
2857 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002858 udelay(150);
2859
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002860 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01002861 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
2862 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
2863 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07002864
Chris Wilson5eddb702010-09-11 13:48:45 +01002865 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002866 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002867 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002868 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2869
2870 if ((temp & FDI_RX_BIT_LOCK)) {
2871 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01002872 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002873 break;
2874 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002875 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002876 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002877 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002878
2879 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002880 reg = FDI_TX_CTL(pipe);
2881 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002882 temp &= ~FDI_LINK_TRAIN_NONE;
2883 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002884 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002885
Chris Wilson5eddb702010-09-11 13:48:45 +01002886 reg = FDI_RX_CTL(pipe);
2887 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002888 temp &= ~FDI_LINK_TRAIN_NONE;
2889 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01002890 I915_WRITE(reg, temp);
2891
2892 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002893 udelay(150);
2894
Chris Wilson5eddb702010-09-11 13:48:45 +01002895 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04002896 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002897 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002898 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2899
2900 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002901 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002902 DRM_DEBUG_KMS("FDI train 2 done.\n");
2903 break;
2904 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002905 }
Adam Jacksone1a44742010-06-25 15:32:14 -04002906 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01002907 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002908
2909 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07002910
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002911}
2912
Akshay Joshi0206e352011-08-16 15:34:10 -04002913static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002914 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
2915 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
2916 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
2917 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
2918};
2919
2920/* The FDI link training functions for SNB/Cougarpoint. */
2921static void gen6_fdi_link_train(struct drm_crtc *crtc)
2922{
2923 struct drm_device *dev = crtc->dev;
2924 struct drm_i915_private *dev_priv = dev->dev_private;
2925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2926 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05002927 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002928
Adam Jacksone1a44742010-06-25 15:32:14 -04002929 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
2930 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01002931 reg = FDI_RX_IMR(pipe);
2932 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002933 temp &= ~FDI_RX_SYMBOL_LOCK;
2934 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01002935 I915_WRITE(reg, temp);
2936
2937 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04002938 udelay(150);
2939
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002940 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01002941 reg = FDI_TX_CTL(pipe);
2942 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02002943 temp &= ~FDI_DP_PORT_WIDTH_MASK;
2944 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002945 temp &= ~FDI_LINK_TRAIN_NONE;
2946 temp |= FDI_LINK_TRAIN_PATTERN_1;
2947 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2948 /* SNB-B */
2949 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01002950 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002951
Daniel Vetterd74cf322012-10-26 10:58:13 +02002952 I915_WRITE(FDI_RX_MISC(pipe),
2953 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
2954
Chris Wilson5eddb702010-09-11 13:48:45 +01002955 reg = FDI_RX_CTL(pipe);
2956 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002957 if (HAS_PCH_CPT(dev)) {
2958 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2959 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
2960 } else {
2961 temp &= ~FDI_LINK_TRAIN_NONE;
2962 temp |= FDI_LINK_TRAIN_PATTERN_1;
2963 }
Chris Wilson5eddb702010-09-11 13:48:45 +01002964 I915_WRITE(reg, temp | FDI_RX_ENABLE);
2965
2966 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002967 udelay(150);
2968
Akshay Joshi0206e352011-08-16 15:34:10 -04002969 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01002970 reg = FDI_TX_CTL(pipe);
2971 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002972 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
2973 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01002974 I915_WRITE(reg, temp);
2975
2976 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002977 udelay(500);
2978
Sean Paulfa37d392012-03-02 12:53:39 -05002979 for (retry = 0; retry < 5; retry++) {
2980 reg = FDI_RX_IIR(pipe);
2981 temp = I915_READ(reg);
2982 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
2983 if (temp & FDI_RX_BIT_LOCK) {
2984 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
2985 DRM_DEBUG_KMS("FDI train 1 done.\n");
2986 break;
2987 }
2988 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002989 }
Sean Paulfa37d392012-03-02 12:53:39 -05002990 if (retry < 5)
2991 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002992 }
2993 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01002994 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002995
2996 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01002997 reg = FDI_TX_CTL(pipe);
2998 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08002999 temp &= ~FDI_LINK_TRAIN_NONE;
3000 temp |= FDI_LINK_TRAIN_PATTERN_2;
3001 if (IS_GEN6(dev)) {
3002 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3003 /* SNB-B */
3004 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3005 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003006 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003007
Chris Wilson5eddb702010-09-11 13:48:45 +01003008 reg = FDI_RX_CTL(pipe);
3009 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003010 if (HAS_PCH_CPT(dev)) {
3011 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3012 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3013 } else {
3014 temp &= ~FDI_LINK_TRAIN_NONE;
3015 temp |= FDI_LINK_TRAIN_PATTERN_2;
3016 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003017 I915_WRITE(reg, temp);
3018
3019 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003020 udelay(150);
3021
Akshay Joshi0206e352011-08-16 15:34:10 -04003022 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003023 reg = FDI_TX_CTL(pipe);
3024 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003025 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3026 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003027 I915_WRITE(reg, temp);
3028
3029 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003030 udelay(500);
3031
Sean Paulfa37d392012-03-02 12:53:39 -05003032 for (retry = 0; retry < 5; retry++) {
3033 reg = FDI_RX_IIR(pipe);
3034 temp = I915_READ(reg);
3035 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3036 if (temp & FDI_RX_SYMBOL_LOCK) {
3037 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3038 DRM_DEBUG_KMS("FDI train 2 done.\n");
3039 break;
3040 }
3041 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003042 }
Sean Paulfa37d392012-03-02 12:53:39 -05003043 if (retry < 5)
3044 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003045 }
3046 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003047 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003048
3049 DRM_DEBUG_KMS("FDI train done.\n");
3050}
3051
Jesse Barnes357555c2011-04-28 15:09:55 -07003052/* Manual link training for Ivy Bridge A0 parts */
3053static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3054{
3055 struct drm_device *dev = crtc->dev;
3056 struct drm_i915_private *dev_priv = dev->dev_private;
3057 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3058 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003059 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003060
3061 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3062 for train result */
3063 reg = FDI_RX_IMR(pipe);
3064 temp = I915_READ(reg);
3065 temp &= ~FDI_RX_SYMBOL_LOCK;
3066 temp &= ~FDI_RX_BIT_LOCK;
3067 I915_WRITE(reg, temp);
3068
3069 POSTING_READ(reg);
3070 udelay(150);
3071
Daniel Vetter01a415f2012-10-27 15:58:40 +02003072 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3073 I915_READ(FDI_RX_IIR(pipe)));
3074
Jesse Barnes139ccd32013-08-19 11:04:55 -07003075 /* Try each vswing and preemphasis setting twice before moving on */
3076 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3077 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003078 reg = FDI_TX_CTL(pipe);
3079 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003080 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3081 temp &= ~FDI_TX_ENABLE;
3082 I915_WRITE(reg, temp);
3083
3084 reg = FDI_RX_CTL(pipe);
3085 temp = I915_READ(reg);
3086 temp &= ~FDI_LINK_TRAIN_AUTO;
3087 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3088 temp &= ~FDI_RX_ENABLE;
3089 I915_WRITE(reg, temp);
3090
3091 /* enable CPU FDI TX and PCH FDI RX */
3092 reg = FDI_TX_CTL(pipe);
3093 temp = I915_READ(reg);
3094 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3095 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3096 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003097 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003098 temp |= snb_b_fdi_train_param[j/2];
3099 temp |= FDI_COMPOSITE_SYNC;
3100 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3101
3102 I915_WRITE(FDI_RX_MISC(pipe),
3103 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3104
3105 reg = FDI_RX_CTL(pipe);
3106 temp = I915_READ(reg);
3107 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3108 temp |= FDI_COMPOSITE_SYNC;
3109 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3110
3111 POSTING_READ(reg);
3112 udelay(1); /* should be 0.5us */
3113
3114 for (i = 0; i < 4; i++) {
3115 reg = FDI_RX_IIR(pipe);
3116 temp = I915_READ(reg);
3117 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3118
3119 if (temp & FDI_RX_BIT_LOCK ||
3120 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3121 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3122 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3123 i);
3124 break;
3125 }
3126 udelay(1); /* should be 0.5us */
3127 }
3128 if (i == 4) {
3129 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3130 continue;
3131 }
3132
3133 /* Train 2 */
3134 reg = FDI_TX_CTL(pipe);
3135 temp = I915_READ(reg);
3136 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3137 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3138 I915_WRITE(reg, temp);
3139
3140 reg = FDI_RX_CTL(pipe);
3141 temp = I915_READ(reg);
3142 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3143 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003144 I915_WRITE(reg, temp);
3145
3146 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003147 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003148
Jesse Barnes139ccd32013-08-19 11:04:55 -07003149 for (i = 0; i < 4; i++) {
3150 reg = FDI_RX_IIR(pipe);
3151 temp = I915_READ(reg);
3152 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003153
Jesse Barnes139ccd32013-08-19 11:04:55 -07003154 if (temp & FDI_RX_SYMBOL_LOCK ||
3155 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3156 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3157 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3158 i);
3159 goto train_done;
3160 }
3161 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003162 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003163 if (i == 4)
3164 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003165 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003166
Jesse Barnes139ccd32013-08-19 11:04:55 -07003167train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003168 DRM_DEBUG_KMS("FDI train done.\n");
3169}
3170
Daniel Vetter88cefb62012-08-12 19:27:14 +02003171static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003172{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003173 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003174 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003175 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003176 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003177
Jesse Barnesc64e3112010-09-10 11:27:03 -07003178
Jesse Barnes0e23b992010-09-10 11:10:00 -07003179 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003180 reg = FDI_RX_CTL(pipe);
3181 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003182 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3183 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003184 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003185 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3186
3187 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003188 udelay(200);
3189
3190 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003191 temp = I915_READ(reg);
3192 I915_WRITE(reg, temp | FDI_PCDCLK);
3193
3194 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003195 udelay(200);
3196
Paulo Zanoni20749732012-11-23 15:30:38 -02003197 /* Enable CPU FDI TX PLL, always on for Ironlake */
3198 reg = FDI_TX_CTL(pipe);
3199 temp = I915_READ(reg);
3200 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3201 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003202
Paulo Zanoni20749732012-11-23 15:30:38 -02003203 POSTING_READ(reg);
3204 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003205 }
3206}
3207
Daniel Vetter88cefb62012-08-12 19:27:14 +02003208static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3209{
3210 struct drm_device *dev = intel_crtc->base.dev;
3211 struct drm_i915_private *dev_priv = dev->dev_private;
3212 int pipe = intel_crtc->pipe;
3213 u32 reg, temp;
3214
3215 /* Switch from PCDclk to Rawclk */
3216 reg = FDI_RX_CTL(pipe);
3217 temp = I915_READ(reg);
3218 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3219
3220 /* Disable CPU FDI TX PLL */
3221 reg = FDI_TX_CTL(pipe);
3222 temp = I915_READ(reg);
3223 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3224
3225 POSTING_READ(reg);
3226 udelay(100);
3227
3228 reg = FDI_RX_CTL(pipe);
3229 temp = I915_READ(reg);
3230 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3231
3232 /* Wait for the clocks to turn off. */
3233 POSTING_READ(reg);
3234 udelay(100);
3235}
3236
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003237static void ironlake_fdi_disable(struct drm_crtc *crtc)
3238{
3239 struct drm_device *dev = crtc->dev;
3240 struct drm_i915_private *dev_priv = dev->dev_private;
3241 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3242 int pipe = intel_crtc->pipe;
3243 u32 reg, temp;
3244
3245 /* disable CPU FDI tx and PCH FDI rx */
3246 reg = FDI_TX_CTL(pipe);
3247 temp = I915_READ(reg);
3248 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3249 POSTING_READ(reg);
3250
3251 reg = FDI_RX_CTL(pipe);
3252 temp = I915_READ(reg);
3253 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003254 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003255 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3256
3257 POSTING_READ(reg);
3258 udelay(100);
3259
3260 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003261 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003262 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003263
3264 /* still set train pattern 1 */
3265 reg = FDI_TX_CTL(pipe);
3266 temp = I915_READ(reg);
3267 temp &= ~FDI_LINK_TRAIN_NONE;
3268 temp |= FDI_LINK_TRAIN_PATTERN_1;
3269 I915_WRITE(reg, temp);
3270
3271 reg = FDI_RX_CTL(pipe);
3272 temp = I915_READ(reg);
3273 if (HAS_PCH_CPT(dev)) {
3274 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3275 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3276 } else {
3277 temp &= ~FDI_LINK_TRAIN_NONE;
3278 temp |= FDI_LINK_TRAIN_PATTERN_1;
3279 }
3280 /* BPC in FDI rx is consistent with that in PIPECONF */
3281 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003282 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003283 I915_WRITE(reg, temp);
3284
3285 POSTING_READ(reg);
3286 udelay(100);
3287}
3288
Chris Wilson5dce5b932014-01-20 10:17:36 +00003289bool intel_has_pending_fb_unpin(struct drm_device *dev)
3290{
3291 struct intel_crtc *crtc;
3292
3293 /* Note that we don't need to be called with mode_config.lock here
3294 * as our list of CRTC objects is static for the lifetime of the
3295 * device and so cannot disappear as we iterate. Similarly, we can
3296 * happily treat the predicates as racy, atomic checks as userspace
3297 * cannot claim and pin a new fb without at least acquring the
3298 * struct_mutex and so serialising with us.
3299 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003300 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003301 if (atomic_read(&crtc->unpin_work_count) == 0)
3302 continue;
3303
3304 if (crtc->unpin_work)
3305 intel_wait_for_vblank(dev, crtc->pipe);
3306
3307 return true;
3308 }
3309
3310 return false;
3311}
3312
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003313void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003314{
Chris Wilson0f911282012-04-17 10:05:38 +01003315 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003316 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003317
Matt Roperf4510a22014-04-01 15:22:40 -07003318 if (crtc->primary->fb == NULL)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003319 return;
3320
Daniel Vetter2c10d572012-12-20 21:24:07 +01003321 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
3322
Daniel Vettereed6d672014-05-19 16:09:35 +02003323 WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3324 !intel_crtc_has_pending_flip(crtc),
3325 60*HZ) == 0);
Chris Wilson5bb61642012-09-27 21:25:58 +01003326
Chris Wilson0f911282012-04-17 10:05:38 +01003327 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07003328 intel_finish_fb(crtc->primary->fb);
Chris Wilson0f911282012-04-17 10:05:38 +01003329 mutex_unlock(&dev->struct_mutex);
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003330}
3331
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003332/* Program iCLKIP clock to the desired frequency */
3333static void lpt_program_iclkip(struct drm_crtc *crtc)
3334{
3335 struct drm_device *dev = crtc->dev;
3336 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003337 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003338 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3339 u32 temp;
3340
Daniel Vetter09153002012-12-12 14:06:44 +01003341 mutex_lock(&dev_priv->dpio_lock);
3342
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003343 /* It is necessary to ungate the pixclk gate prior to programming
3344 * the divisors, and gate it back when it is done.
3345 */
3346 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3347
3348 /* Disable SSCCTL */
3349 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003350 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3351 SBI_SSCCTL_DISABLE,
3352 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003353
3354 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003355 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003356 auxdiv = 1;
3357 divsel = 0x41;
3358 phaseinc = 0x20;
3359 } else {
3360 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003361 * but the adjusted_mode->crtc_clock in in KHz. To get the
3362 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003363 * convert the virtual clock precision to KHz here for higher
3364 * precision.
3365 */
3366 u32 iclk_virtual_root_freq = 172800 * 1000;
3367 u32 iclk_pi_range = 64;
3368 u32 desired_divisor, msb_divisor_value, pi_value;
3369
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003370 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003371 msb_divisor_value = desired_divisor / iclk_pi_range;
3372 pi_value = desired_divisor % iclk_pi_range;
3373
3374 auxdiv = 0;
3375 divsel = msb_divisor_value - 2;
3376 phaseinc = pi_value;
3377 }
3378
3379 /* This should not happen with any sane values */
3380 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3381 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3382 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3383 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3384
3385 DRM_DEBUG_KMS("iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003386 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003387 auxdiv,
3388 divsel,
3389 phasedir,
3390 phaseinc);
3391
3392 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003393 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003394 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3395 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3396 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3397 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3398 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3399 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003400 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003401
3402 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003403 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003404 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3405 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003406 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003407
3408 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003409 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003410 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003411 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003412
3413 /* Wait for initialization time */
3414 udelay(24);
3415
3416 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003417
3418 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003419}
3420
Daniel Vetter275f01b22013-05-03 11:49:47 +02003421static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3422 enum pipe pch_transcoder)
3423{
3424 struct drm_device *dev = crtc->base.dev;
3425 struct drm_i915_private *dev_priv = dev->dev_private;
3426 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3427
3428 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3429 I915_READ(HTOTAL(cpu_transcoder)));
3430 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3431 I915_READ(HBLANK(cpu_transcoder)));
3432 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3433 I915_READ(HSYNC(cpu_transcoder)));
3434
3435 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3436 I915_READ(VTOTAL(cpu_transcoder)));
3437 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3438 I915_READ(VBLANK(cpu_transcoder)));
3439 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3440 I915_READ(VSYNC(cpu_transcoder)));
3441 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3442 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3443}
3444
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003445static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3446{
3447 struct drm_i915_private *dev_priv = dev->dev_private;
3448 uint32_t temp;
3449
3450 temp = I915_READ(SOUTH_CHICKEN1);
3451 if (temp & FDI_BC_BIFURCATION_SELECT)
3452 return;
3453
3454 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3455 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3456
3457 temp |= FDI_BC_BIFURCATION_SELECT;
3458 DRM_DEBUG_KMS("enabling fdi C rx\n");
3459 I915_WRITE(SOUTH_CHICKEN1, temp);
3460 POSTING_READ(SOUTH_CHICKEN1);
3461}
3462
3463static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3464{
3465 struct drm_device *dev = intel_crtc->base.dev;
3466 struct drm_i915_private *dev_priv = dev->dev_private;
3467
3468 switch (intel_crtc->pipe) {
3469 case PIPE_A:
3470 break;
3471 case PIPE_B:
3472 if (intel_crtc->config.fdi_lanes > 2)
3473 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3474 else
3475 cpt_enable_fdi_bc_bifurcation(dev);
3476
3477 break;
3478 case PIPE_C:
3479 cpt_enable_fdi_bc_bifurcation(dev);
3480
3481 break;
3482 default:
3483 BUG();
3484 }
3485}
3486
Jesse Barnesf67a5592011-01-05 10:31:48 -08003487/*
3488 * Enable PCH resources required for PCH ports:
3489 * - PCH PLLs
3490 * - FDI training & RX/TX
3491 * - update transcoder timings
3492 * - DP transcoding bits
3493 * - transcoder
3494 */
3495static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003496{
3497 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003498 struct drm_i915_private *dev_priv = dev->dev_private;
3499 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3500 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003501 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003502
Daniel Vetterab9412b2013-05-03 11:49:46 +02003503 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003504
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003505 if (IS_IVYBRIDGE(dev))
3506 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3507
Daniel Vettercd986ab2012-10-26 10:58:12 +02003508 /* Write the TU size bits before fdi link training, so that error
3509 * detection works. */
3510 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3511 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3512
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003513 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003514 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003515
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003516 /* We need to program the right clock selection before writing the pixel
3517 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003518 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003519 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003520
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003521 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003522 temp |= TRANS_DPLL_ENABLE(pipe);
3523 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003524 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003525 temp |= sel;
3526 else
3527 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003528 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003529 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003530
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003531 /* XXX: pch pll's can be enabled any time before we enable the PCH
3532 * transcoder, and we actually should do this to not upset any PCH
3533 * transcoder that already use the clock when we share it.
3534 *
3535 * Note that enable_shared_dpll tries to do the right thing, but
3536 * get_shared_dpll unconditionally resets the pll - we need that to have
3537 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003538 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003539
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003540 /* set transcoder timing, panel must allow it */
3541 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003542 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003543
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003544 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003545
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003546 /* For PCH DP, enable TRANS_DP_CTL */
3547 if (HAS_PCH_CPT(dev) &&
Keith Packard417e8222011-11-01 19:54:11 -07003548 (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3549 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003550 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003551 reg = TRANS_DP_CTL(pipe);
3552 temp = I915_READ(reg);
3553 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003554 TRANS_DP_SYNC_MASK |
3555 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003556 temp |= (TRANS_DP_OUTPUT_ENABLE |
3557 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003558 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003559
3560 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003562 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003563 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003564
3565 switch (intel_trans_dp_port_sel(crtc)) {
3566 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003567 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003568 break;
3569 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003570 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003571 break;
3572 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003573 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003574 break;
3575 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003576 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003577 }
3578
Chris Wilson5eddb702010-09-11 13:48:45 +01003579 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003580 }
3581
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003582 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003583}
3584
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003585static void lpt_pch_enable(struct drm_crtc *crtc)
3586{
3587 struct drm_device *dev = crtc->dev;
3588 struct drm_i915_private *dev_priv = dev->dev_private;
3589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003590 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003591
Daniel Vetterab9412b2013-05-03 11:49:46 +02003592 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003593
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003594 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003595
Paulo Zanoni0540e482012-10-31 18:12:40 -02003596 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003597 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003598
Paulo Zanoni937bb612012-10-31 18:12:47 -02003599 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003600}
3601
Daniel Vettere2b78262013-06-07 23:10:03 +02003602static void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003603{
Daniel Vettere2b78262013-06-07 23:10:03 +02003604 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003605
3606 if (pll == NULL)
3607 return;
3608
3609 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003610 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003611 return;
3612 }
3613
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003614 if (--pll->refcount == 0) {
3615 WARN_ON(pll->on);
3616 WARN_ON(pll->active);
3617 }
3618
Daniel Vettera43f6e02013-06-07 23:10:32 +02003619 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003620}
3621
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003622static struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003623{
Daniel Vettere2b78262013-06-07 23:10:03 +02003624 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3625 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3626 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003627
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003628 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003629 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3630 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003631 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003632 }
3633
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003634 if (HAS_PCH_IBX(dev_priv->dev)) {
3635 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003636 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003637 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003638
Daniel Vetter46edb022013-06-05 13:34:12 +02003639 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3640 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003641
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003642 WARN_ON(pll->refcount);
3643
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003644 goto found;
3645 }
3646
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003647 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3648 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003649
3650 /* Only want to check enabled timings first */
3651 if (pll->refcount == 0)
3652 continue;
3653
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003654 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3655 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003656 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003657 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003658 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003659
3660 goto found;
3661 }
3662 }
3663
3664 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003665 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3666 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003667 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003668 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3669 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003670 goto found;
3671 }
3672 }
3673
3674 return NULL;
3675
3676found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003677 if (pll->refcount == 0)
3678 pll->hw_state = crtc->config.dpll_hw_state;
3679
Daniel Vettera43f6e02013-06-07 23:10:32 +02003680 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003681 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3682 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003683
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003684 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003685
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003686 return pll;
3687}
3688
Daniel Vettera1520312013-05-03 11:49:50 +02003689static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003690{
3691 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003692 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003693 u32 temp;
3694
3695 temp = I915_READ(dslreg);
3696 udelay(500);
3697 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003698 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003699 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003700 }
3701}
3702
Jesse Barnesb074cec2013-04-25 12:55:02 -07003703static void ironlake_pfit_enable(struct intel_crtc *crtc)
3704{
3705 struct drm_device *dev = crtc->base.dev;
3706 struct drm_i915_private *dev_priv = dev->dev_private;
3707 int pipe = crtc->pipe;
3708
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003709 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003710 /* Force use of hard-coded filter coefficients
3711 * as some pre-programmed values are broken,
3712 * e.g. x201.
3713 */
3714 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3715 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3716 PF_PIPE_SEL_IVB(pipe));
3717 else
3718 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3719 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3720 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003721 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003722}
3723
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003724static void intel_enable_planes(struct drm_crtc *crtc)
3725{
3726 struct drm_device *dev = crtc->dev;
3727 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003728 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003729 struct intel_plane *intel_plane;
3730
Matt Roperaf2b6532014-04-01 15:22:32 -07003731 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3732 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003733 if (intel_plane->pipe == pipe)
3734 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003735 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003736}
3737
3738static void intel_disable_planes(struct drm_crtc *crtc)
3739{
3740 struct drm_device *dev = crtc->dev;
3741 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003742 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003743 struct intel_plane *intel_plane;
3744
Matt Roperaf2b6532014-04-01 15:22:32 -07003745 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3746 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003747 if (intel_plane->pipe == pipe)
3748 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003749 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003750}
3751
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003752void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003753{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003754 struct drm_device *dev = crtc->base.dev;
3755 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003756
3757 if (!crtc->config.ips_enabled)
3758 return;
3759
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003760 /* We can only enable IPS after we enable a plane and wait for a vblank */
3761 intel_wait_for_vblank(dev, crtc->pipe);
3762
Paulo Zanonid77e4532013-09-24 13:52:55 -03003763 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003764 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003765 mutex_lock(&dev_priv->rps.hw_lock);
3766 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3767 mutex_unlock(&dev_priv->rps.hw_lock);
3768 /* Quoting Art Runyan: "its not safe to expect any particular
3769 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003770 * mailbox." Moreover, the mailbox may return a bogus state,
3771 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003772 */
3773 } else {
3774 I915_WRITE(IPS_CTL, IPS_ENABLE);
3775 /* The bit only becomes 1 in the next vblank, so this wait here
3776 * is essentially intel_wait_for_vblank. If we don't have this
3777 * and don't wait for vblanks until the end of crtc_enable, then
3778 * the HW state readout code will complain that the expected
3779 * IPS_CTL value is not the one we read. */
3780 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3781 DRM_ERROR("Timed out waiting for IPS enable\n");
3782 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003783}
3784
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003785void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003786{
3787 struct drm_device *dev = crtc->base.dev;
3788 struct drm_i915_private *dev_priv = dev->dev_private;
3789
3790 if (!crtc->config.ips_enabled)
3791 return;
3792
3793 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003794 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003795 mutex_lock(&dev_priv->rps.hw_lock);
3796 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3797 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07003798 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
3799 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
3800 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08003801 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003802 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08003803 POSTING_READ(IPS_CTL);
3804 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03003805
3806 /* We need to wait for a vblank before we can disable the plane. */
3807 intel_wait_for_vblank(dev, crtc->pipe);
3808}
3809
3810/** Loads the palette/gamma unit for the CRTC with the prepared values */
3811static void intel_crtc_load_lut(struct drm_crtc *crtc)
3812{
3813 struct drm_device *dev = crtc->dev;
3814 struct drm_i915_private *dev_priv = dev->dev_private;
3815 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3816 enum pipe pipe = intel_crtc->pipe;
3817 int palreg = PALETTE(pipe);
3818 int i;
3819 bool reenable_ips = false;
3820
3821 /* The clocks have to be on to load the palette. */
3822 if (!crtc->enabled || !intel_crtc->active)
3823 return;
3824
3825 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
3826 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
3827 assert_dsi_pll_enabled(dev_priv);
3828 else
3829 assert_pll_enabled(dev_priv, pipe);
3830 }
3831
3832 /* use legacy palette for Ironlake */
3833 if (HAS_PCH_SPLIT(dev))
3834 palreg = LGC_PALETTE(pipe);
3835
3836 /* Workaround : Do not read or write the pipe palette/gamma data while
3837 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3838 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02003839 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03003840 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
3841 GAMMA_MODE_MODE_SPLIT)) {
3842 hsw_disable_ips(intel_crtc);
3843 reenable_ips = true;
3844 }
3845
3846 for (i = 0; i < 256; i++) {
3847 I915_WRITE(palreg + 4 * i,
3848 (intel_crtc->lut_r[i] << 16) |
3849 (intel_crtc->lut_g[i] << 8) |
3850 intel_crtc->lut_b[i]);
3851 }
3852
3853 if (reenable_ips)
3854 hsw_enable_ips(intel_crtc);
3855}
3856
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003857static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
3858{
3859 if (!enable && intel_crtc->overlay) {
3860 struct drm_device *dev = intel_crtc->base.dev;
3861 struct drm_i915_private *dev_priv = dev->dev_private;
3862
3863 mutex_lock(&dev->struct_mutex);
3864 dev_priv->mm.interruptible = false;
3865 (void) intel_overlay_switch_off(intel_crtc->overlay);
3866 dev_priv->mm.interruptible = true;
3867 mutex_unlock(&dev->struct_mutex);
3868 }
3869
3870 /* Let userspace switch the overlay on again. In most cases userspace
3871 * has to recompute where to put it anyway.
3872 */
3873}
3874
3875/**
3876 * i9xx_fixup_plane - ugly workaround for G45 to fire up the hardware
3877 * cursor plane briefly if not already running after enabling the display
3878 * plane.
3879 * This workaround avoids occasional blank screens when self refresh is
3880 * enabled.
3881 */
3882static void
3883g4x_fixup_plane(struct drm_i915_private *dev_priv, enum pipe pipe)
3884{
3885 u32 cntl = I915_READ(CURCNTR(pipe));
3886
3887 if ((cntl & CURSOR_MODE) == 0) {
3888 u32 fw_bcl_self = I915_READ(FW_BLC_SELF);
3889
3890 I915_WRITE(FW_BLC_SELF, fw_bcl_self & ~FW_BLC_SELF_EN);
3891 I915_WRITE(CURCNTR(pipe), CURSOR_MODE_64_ARGB_AX);
3892 intel_wait_for_vblank(dev_priv->dev, pipe);
3893 I915_WRITE(CURCNTR(pipe), cntl);
3894 I915_WRITE(CURBASE(pipe), I915_READ(CURBASE(pipe)));
3895 I915_WRITE(FW_BLC_SELF, fw_bcl_self);
3896 }
3897}
3898
3899static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003900{
3901 struct drm_device *dev = crtc->dev;
3902 struct drm_i915_private *dev_priv = dev->dev_private;
3903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3904 int pipe = intel_crtc->pipe;
3905 int plane = intel_crtc->plane;
3906
3907 intel_enable_primary_hw_plane(dev_priv, plane, pipe);
3908 intel_enable_planes(crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003909 /* The fixup needs to happen before cursor is enabled */
3910 if (IS_G4X(dev))
3911 g4x_fixup_plane(dev_priv, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003912 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003913 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003914
3915 hsw_enable_ips(intel_crtc);
3916
3917 mutex_lock(&dev->struct_mutex);
3918 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02003919 intel_edp_psr_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003920 mutex_unlock(&dev->struct_mutex);
3921}
3922
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003923static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003924{
3925 struct drm_device *dev = crtc->dev;
3926 struct drm_i915_private *dev_priv = dev->dev_private;
3927 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3928 int pipe = intel_crtc->pipe;
3929 int plane = intel_crtc->plane;
3930
3931 intel_crtc_wait_for_pending_flips(crtc);
Daniel Vetter87b6b102014-05-15 15:33:46 +02003932 drm_crtc_vblank_off(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003933
3934 if (dev_priv->fbc.plane == plane)
3935 intel_disable_fbc(dev);
3936
3937 hsw_disable_ips(intel_crtc);
3938
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03003939 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02003940 intel_crtc_update_cursor(crtc, false);
3941 intel_disable_planes(crtc);
3942 intel_disable_primary_hw_plane(dev_priv, plane, pipe);
3943}
3944
Jesse Barnesf67a5592011-01-05 10:31:48 -08003945static void ironlake_crtc_enable(struct drm_crtc *crtc)
3946{
3947 struct drm_device *dev = crtc->dev;
3948 struct drm_i915_private *dev_priv = dev->dev_private;
3949 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02003950 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003951 int pipe = intel_crtc->pipe;
Daniel Vetter29407aa2014-04-24 23:55:08 +02003952 enum plane plane = intel_crtc->plane;
Jesse Barnesf67a5592011-01-05 10:31:48 -08003953
Daniel Vetter08a48462012-07-02 11:43:47 +02003954 WARN_ON(!crtc->enabled);
3955
Jesse Barnesf67a5592011-01-05 10:31:48 -08003956 if (intel_crtc->active)
3957 return;
3958
Daniel Vetterb14b1052014-04-24 23:55:13 +02003959 if (intel_crtc->config.has_pch_encoder)
3960 intel_prepare_shared_dpll(intel_crtc);
3961
Daniel Vetter29407aa2014-04-24 23:55:08 +02003962 if (intel_crtc->config.has_dp_encoder)
3963 intel_dp_set_m_n(intel_crtc);
3964
3965 intel_set_pipe_timings(intel_crtc);
3966
3967 if (intel_crtc->config.has_pch_encoder) {
3968 intel_cpu_transcoder_set_m_n(intel_crtc,
3969 &intel_crtc->config.fdi_m_n);
3970 }
3971
3972 ironlake_set_pipeconf(crtc);
3973
3974 /* Set up the display plane register */
3975 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE);
3976 POSTING_READ(DSPCNTR(plane));
3977
3978 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
3979 crtc->x, crtc->y);
3980
Jesse Barnesf67a5592011-01-05 10:31:48 -08003981 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03003982
3983 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
3984 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
3985
Daniel Vetterf6736a12013-06-05 13:34:30 +02003986 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02003987 if (encoder->pre_enable)
3988 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003989
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01003990 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02003991 /* Note: FDI PLL enabling _must_ be done before we enable the
3992 * cpu pipes, hence this is separate from all the other fdi/pch
3993 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02003994 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02003995 } else {
3996 assert_fdi_tx_disabled(dev_priv, pipe);
3997 assert_fdi_rx_disabled(dev_priv, pipe);
3998 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003999
Jesse Barnesb074cec2013-04-25 12:55:02 -07004000 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004001
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004002 /*
4003 * On ILK+ LUT must be loaded before the pipe is running but with
4004 * clocks enabled
4005 */
4006 intel_crtc_load_lut(crtc);
4007
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004008 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004009 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004010
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004011 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004012 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004013
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004014 for_each_encoder_on_crtc(dev, crtc, encoder)
4015 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004016
4017 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004018 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004019
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004020 intel_crtc_enable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004021
Daniel Vetter87b6b102014-05-15 15:33:46 +02004022 drm_crtc_vblank_on(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004023}
4024
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004025/* IPS only exists on ULT machines and is tied to pipe A. */
4026static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4027{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004028 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004029}
4030
Paulo Zanonie4916942013-09-20 16:21:19 -03004031/*
4032 * This implements the workaround described in the "notes" section of the mode
4033 * set sequence documentation. When going from no pipes or single pipe to
4034 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4035 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4036 */
4037static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4038{
4039 struct drm_device *dev = crtc->base.dev;
4040 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4041
4042 /* We want to get the other_active_crtc only if there's only 1 other
4043 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004044 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004045 if (!crtc_it->active || crtc_it == crtc)
4046 continue;
4047
4048 if (other_active_crtc)
4049 return;
4050
4051 other_active_crtc = crtc_it;
4052 }
4053 if (!other_active_crtc)
4054 return;
4055
4056 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4057 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4058}
4059
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004060static void haswell_crtc_enable(struct drm_crtc *crtc)
4061{
4062 struct drm_device *dev = crtc->dev;
4063 struct drm_i915_private *dev_priv = dev->dev_private;
4064 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4065 struct intel_encoder *encoder;
4066 int pipe = intel_crtc->pipe;
Daniel Vetter229fca92014-04-24 23:55:09 +02004067 enum plane plane = intel_crtc->plane;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004068
4069 WARN_ON(!crtc->enabled);
4070
4071 if (intel_crtc->active)
4072 return;
4073
Daniel Vetter229fca92014-04-24 23:55:09 +02004074 if (intel_crtc->config.has_dp_encoder)
4075 intel_dp_set_m_n(intel_crtc);
4076
4077 intel_set_pipe_timings(intel_crtc);
4078
4079 if (intel_crtc->config.has_pch_encoder) {
4080 intel_cpu_transcoder_set_m_n(intel_crtc,
4081 &intel_crtc->config.fdi_m_n);
4082 }
4083
4084 haswell_set_pipeconf(crtc);
4085
4086 intel_set_pipe_csc(crtc);
4087
4088 /* Set up the display plane register */
4089 I915_WRITE(DSPCNTR(plane), DISPPLANE_GAMMA_ENABLE | DISPPLANE_PIPE_CSC_ENABLE);
4090 POSTING_READ(DSPCNTR(plane));
4091
4092 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4093 crtc->x, crtc->y);
4094
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004095 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004096
4097 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4098 if (intel_crtc->config.has_pch_encoder)
4099 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
4100
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004101 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni04945642012-11-01 21:00:59 -02004102 dev_priv->display.fdi_link_train(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004103
4104 for_each_encoder_on_crtc(dev, crtc, encoder)
4105 if (encoder->pre_enable)
4106 encoder->pre_enable(encoder);
4107
Paulo Zanoni1f544382012-10-24 11:32:00 -02004108 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004109
Jesse Barnesb074cec2013-04-25 12:55:02 -07004110 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004111
4112 /*
4113 * On ILK+ LUT must be loaded before the pipe is running but with
4114 * clocks enabled
4115 */
4116 intel_crtc_load_lut(crtc);
4117
Paulo Zanoni1f544382012-10-24 11:32:00 -02004118 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004119 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004120
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004121 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004122 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004123
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004124 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004125 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004126
Jani Nikula8807e552013-08-30 19:40:32 +03004127 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004128 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004129 intel_opregion_notify_encoder(encoder, true);
4130 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004131
Paulo Zanonie4916942013-09-20 16:21:19 -03004132 /* If we change the relative order between pipe/planes enabling, we need
4133 * to change the workaround. */
4134 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004135 intel_crtc_enable_planes(crtc);
Ville Syrjäläf2752282014-02-19 21:29:49 +02004136
Daniel Vetter87b6b102014-05-15 15:33:46 +02004137 drm_crtc_vblank_on(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004138}
4139
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004140static void ironlake_pfit_disable(struct intel_crtc *crtc)
4141{
4142 struct drm_device *dev = crtc->base.dev;
4143 struct drm_i915_private *dev_priv = dev->dev_private;
4144 int pipe = crtc->pipe;
4145
4146 /* To avoid upsetting the power well on haswell only disable the pfit if
4147 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004148 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004149 I915_WRITE(PF_CTL(pipe), 0);
4150 I915_WRITE(PF_WIN_POS(pipe), 0);
4151 I915_WRITE(PF_WIN_SZ(pipe), 0);
4152 }
4153}
4154
Jesse Barnes6be4a602010-09-10 10:26:01 -07004155static void ironlake_crtc_disable(struct drm_crtc *crtc)
4156{
4157 struct drm_device *dev = crtc->dev;
4158 struct drm_i915_private *dev_priv = dev->dev_private;
4159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004160 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004161 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004163
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004164 if (!intel_crtc->active)
4165 return;
4166
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004167 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004168
Daniel Vetterea9d7582012-07-10 10:42:52 +02004169 for_each_encoder_on_crtc(dev, crtc, encoder)
4170 encoder->disable(encoder);
4171
Daniel Vetterd925c592013-06-05 13:34:04 +02004172 if (intel_crtc->config.has_pch_encoder)
4173 intel_set_pch_fifo_underrun_reporting(dev, pipe, false);
4174
Jesse Barnesb24e7172011-01-04 15:09:30 -08004175 intel_disable_pipe(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004176
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004177 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004178
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004179 for_each_encoder_on_crtc(dev, crtc, encoder)
4180 if (encoder->post_disable)
4181 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004182
Daniel Vetterd925c592013-06-05 13:34:04 +02004183 if (intel_crtc->config.has_pch_encoder) {
4184 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004185
Daniel Vetterd925c592013-06-05 13:34:04 +02004186 ironlake_disable_pch_transcoder(dev_priv, pipe);
4187 intel_set_pch_fifo_underrun_reporting(dev, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004188
Daniel Vetterd925c592013-06-05 13:34:04 +02004189 if (HAS_PCH_CPT(dev)) {
4190 /* disable TRANS_DP_CTL */
4191 reg = TRANS_DP_CTL(pipe);
4192 temp = I915_READ(reg);
4193 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4194 TRANS_DP_PORT_SEL_MASK);
4195 temp |= TRANS_DP_PORT_SEL_NONE;
4196 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004197
Daniel Vetterd925c592013-06-05 13:34:04 +02004198 /* disable DPLL_SEL */
4199 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004200 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004201 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004202 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004203
4204 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004205 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004206
4207 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004208 }
4209
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004210 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004211 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004212
4213 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004214 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004215 intel_edp_psr_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004216 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004217}
4218
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004219static void haswell_crtc_disable(struct drm_crtc *crtc)
4220{
4221 struct drm_device *dev = crtc->dev;
4222 struct drm_i915_private *dev_priv = dev->dev_private;
4223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4224 struct intel_encoder *encoder;
4225 int pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004226 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004227
4228 if (!intel_crtc->active)
4229 return;
4230
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004231 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004232
Jani Nikula8807e552013-08-30 19:40:32 +03004233 for_each_encoder_on_crtc(dev, crtc, encoder) {
4234 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004235 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004236 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004237
Paulo Zanoni86642812013-04-12 17:57:57 -03004238 if (intel_crtc->config.has_pch_encoder)
4239 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004240 intel_disable_pipe(dev_priv, pipe);
4241
Paulo Zanoniad80a812012-10-24 16:06:19 -02004242 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004243
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004244 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004245
Paulo Zanoni1f544382012-10-24 11:32:00 -02004246 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004247
4248 for_each_encoder_on_crtc(dev, crtc, encoder)
4249 if (encoder->post_disable)
4250 encoder->post_disable(encoder);
4251
Daniel Vetter88adfff2013-03-28 10:42:01 +01004252 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004253 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni86642812013-04-12 17:57:57 -03004254 intel_set_pch_fifo_underrun_reporting(dev, TRANSCODER_A, true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004255 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004256 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004257
4258 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004259 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004260
4261 mutex_lock(&dev->struct_mutex);
4262 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004263 intel_edp_psr_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004264 mutex_unlock(&dev->struct_mutex);
4265}
4266
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004267static void ironlake_crtc_off(struct drm_crtc *crtc)
4268{
4269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004270 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004271}
4272
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004273static void haswell_crtc_off(struct drm_crtc *crtc)
4274{
4275 intel_ddi_put_crtc_pll(crtc);
4276}
4277
Jesse Barnes2dd24552013-04-25 12:55:01 -07004278static void i9xx_pfit_enable(struct intel_crtc *crtc)
4279{
4280 struct drm_device *dev = crtc->base.dev;
4281 struct drm_i915_private *dev_priv = dev->dev_private;
4282 struct intel_crtc_config *pipe_config = &crtc->config;
4283
Daniel Vetter328d8e82013-05-08 10:36:31 +02004284 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004285 return;
4286
Daniel Vetterc0b03412013-05-28 12:05:54 +02004287 /*
4288 * The panel fitter should only be adjusted whilst the pipe is disabled,
4289 * according to register description and PRM.
4290 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004291 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4292 assert_pipe_disabled(dev_priv, crtc->pipe);
4293
Jesse Barnesb074cec2013-04-25 12:55:02 -07004294 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4295 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004296
4297 /* Border color in case we don't scale up to the full screen. Black by
4298 * default, change to something else for debugging. */
4299 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004300}
4301
Imre Deak77d22dc2014-03-05 16:20:52 +02004302#define for_each_power_domain(domain, mask) \
4303 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4304 if ((1 << (domain)) & (mask))
4305
Imre Deak319be8a2014-03-04 19:22:57 +02004306enum intel_display_power_domain
4307intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004308{
Imre Deak319be8a2014-03-04 19:22:57 +02004309 struct drm_device *dev = intel_encoder->base.dev;
4310 struct intel_digital_port *intel_dig_port;
4311
4312 switch (intel_encoder->type) {
4313 case INTEL_OUTPUT_UNKNOWN:
4314 /* Only DDI platforms should ever use this output type */
4315 WARN_ON_ONCE(!HAS_DDI(dev));
4316 case INTEL_OUTPUT_DISPLAYPORT:
4317 case INTEL_OUTPUT_HDMI:
4318 case INTEL_OUTPUT_EDP:
4319 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
4320 switch (intel_dig_port->port) {
4321 case PORT_A:
4322 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4323 case PORT_B:
4324 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4325 case PORT_C:
4326 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4327 case PORT_D:
4328 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4329 default:
4330 WARN_ON_ONCE(1);
4331 return POWER_DOMAIN_PORT_OTHER;
4332 }
4333 case INTEL_OUTPUT_ANALOG:
4334 return POWER_DOMAIN_PORT_CRT;
4335 case INTEL_OUTPUT_DSI:
4336 return POWER_DOMAIN_PORT_DSI;
4337 default:
4338 return POWER_DOMAIN_PORT_OTHER;
4339 }
4340}
4341
4342static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4343{
4344 struct drm_device *dev = crtc->dev;
4345 struct intel_encoder *intel_encoder;
4346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4347 enum pipe pipe = intel_crtc->pipe;
4348 bool pfit_enabled = intel_crtc->config.pch_pfit.enabled;
Imre Deak77d22dc2014-03-05 16:20:52 +02004349 unsigned long mask;
4350 enum transcoder transcoder;
4351
4352 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4353
4354 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4355 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
4356 if (pfit_enabled)
4357 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4358
Imre Deak319be8a2014-03-04 19:22:57 +02004359 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4360 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4361
Imre Deak77d22dc2014-03-05 16:20:52 +02004362 return mask;
4363}
4364
4365void intel_display_set_init_power(struct drm_i915_private *dev_priv,
4366 bool enable)
4367{
4368 if (dev_priv->power_domains.init_power_on == enable)
4369 return;
4370
4371 if (enable)
4372 intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
4373 else
4374 intel_display_power_put(dev_priv, POWER_DOMAIN_INIT);
4375
4376 dev_priv->power_domains.init_power_on = enable;
4377}
4378
4379static void modeset_update_crtc_power_domains(struct drm_device *dev)
4380{
4381 struct drm_i915_private *dev_priv = dev->dev_private;
4382 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4383 struct intel_crtc *crtc;
4384
4385 /*
4386 * First get all needed power domains, then put all unneeded, to avoid
4387 * any unnecessary toggling of the power wells.
4388 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004389 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004390 enum intel_display_power_domain domain;
4391
4392 if (!crtc->base.enabled)
4393 continue;
4394
Imre Deak319be8a2014-03-04 19:22:57 +02004395 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004396
4397 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4398 intel_display_power_get(dev_priv, domain);
4399 }
4400
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004401 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004402 enum intel_display_power_domain domain;
4403
4404 for_each_power_domain(domain, crtc->enabled_power_domains)
4405 intel_display_power_put(dev_priv, domain);
4406
4407 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4408 }
4409
4410 intel_display_set_init_power(dev_priv, false);
4411}
4412
Jesse Barnes586f49d2013-11-04 16:06:59 -08004413int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004414{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004415 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004416
Jesse Barnes586f49d2013-11-04 16:06:59 -08004417 /* Obtain SKU information */
4418 mutex_lock(&dev_priv->dpio_lock);
4419 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4420 CCK_FUSE_HPLL_FREQ_MASK;
4421 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004422
Jesse Barnes586f49d2013-11-04 16:06:59 -08004423 return vco_freq[hpll_freq];
Jesse Barnes30a970c2013-11-04 13:48:12 -08004424}
4425
4426/* Adjust CDclk dividers to allow high res or save power if possible */
4427static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4428{
4429 struct drm_i915_private *dev_priv = dev->dev_private;
4430 u32 val, cmd;
4431
Imre Deakd60c4472014-03-27 17:45:10 +02004432 WARN_ON(valleyview_cur_cdclk(dev_priv) != dev_priv->vlv_cdclk_freq);
4433 dev_priv->vlv_cdclk_freq = cdclk;
4434
Jesse Barnes30a970c2013-11-04 13:48:12 -08004435 if (cdclk >= 320) /* jump to highest voltage for 400MHz too */
4436 cmd = 2;
4437 else if (cdclk == 266)
4438 cmd = 1;
4439 else
4440 cmd = 0;
4441
4442 mutex_lock(&dev_priv->rps.hw_lock);
4443 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4444 val &= ~DSPFREQGUAR_MASK;
4445 val |= (cmd << DSPFREQGUAR_SHIFT);
4446 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4447 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4448 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4449 50)) {
4450 DRM_ERROR("timed out waiting for CDclk change\n");
4451 }
4452 mutex_unlock(&dev_priv->rps.hw_lock);
4453
4454 if (cdclk == 400) {
4455 u32 divider, vco;
4456
4457 vco = valleyview_get_vco(dev_priv);
4458 divider = ((vco << 1) / cdclk) - 1;
4459
4460 mutex_lock(&dev_priv->dpio_lock);
4461 /* adjust cdclk divider */
4462 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4463 val &= ~0xf;
4464 val |= divider;
4465 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
4466 mutex_unlock(&dev_priv->dpio_lock);
4467 }
4468
4469 mutex_lock(&dev_priv->dpio_lock);
4470 /* adjust self-refresh exit latency value */
4471 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4472 val &= ~0x7f;
4473
4474 /*
4475 * For high bandwidth configs, we set a higher latency in the bunit
4476 * so that the core display fetch happens in time to avoid underruns.
4477 */
4478 if (cdclk == 400)
4479 val |= 4500 / 250; /* 4.5 usec */
4480 else
4481 val |= 3000 / 250; /* 3.0 usec */
4482 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4483 mutex_unlock(&dev_priv->dpio_lock);
4484
4485 /* Since we changed the CDclk, we need to update the GMBUSFREQ too */
4486 intel_i2c_reset(dev);
4487}
4488
Imre Deakd60c4472014-03-27 17:45:10 +02004489int valleyview_cur_cdclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004490{
4491 int cur_cdclk, vco;
4492 int divider;
4493
4494 vco = valleyview_get_vco(dev_priv);
4495
4496 mutex_lock(&dev_priv->dpio_lock);
4497 divider = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
4498 mutex_unlock(&dev_priv->dpio_lock);
4499
4500 divider &= 0xf;
4501
4502 cur_cdclk = (vco << 1) / (divider + 1);
4503
4504 return cur_cdclk;
4505}
4506
4507static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4508 int max_pixclk)
4509{
Jesse Barnes30a970c2013-11-04 13:48:12 -08004510 /*
4511 * Really only a few cases to deal with, as only 4 CDclks are supported:
4512 * 200MHz
4513 * 267MHz
4514 * 320MHz
4515 * 400MHz
4516 * So we check to see whether we're above 90% of the lower bin and
4517 * adjust if needed.
4518 */
4519 if (max_pixclk > 288000) {
4520 return 400;
4521 } else if (max_pixclk > 240000) {
4522 return 320;
4523 } else
4524 return 266;
4525 /* Looks like the 200MHz CDclk freq doesn't work on some configs */
4526}
4527
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004528/* compute the max pixel clock for new configuration */
4529static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004530{
4531 struct drm_device *dev = dev_priv->dev;
4532 struct intel_crtc *intel_crtc;
4533 int max_pixclk = 0;
4534
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004535 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004536 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004537 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004538 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004539 }
4540
4541 return max_pixclk;
4542}
4543
4544static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004545 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004546{
4547 struct drm_i915_private *dev_priv = dev->dev_private;
4548 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004549 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004550
Imre Deakd60c4472014-03-27 17:45:10 +02004551 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4552 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004553 return;
4554
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004555 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004556 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004557 if (intel_crtc->base.enabled)
4558 *prepare_pipes |= (1 << intel_crtc->pipe);
4559}
4560
4561static void valleyview_modeset_global_resources(struct drm_device *dev)
4562{
4563 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004564 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004565 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4566
Imre Deakd60c4472014-03-27 17:45:10 +02004567 if (req_cdclk != dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004568 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak77961eb2014-03-05 16:20:56 +02004569 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004570}
4571
Jesse Barnes89b667f2013-04-18 14:51:36 -07004572static void valleyview_crtc_enable(struct drm_crtc *crtc)
4573{
4574 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004575 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4577 struct intel_encoder *encoder;
4578 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004579 int plane = intel_crtc->plane;
Jani Nikula23538ef2013-08-27 15:12:22 +03004580 bool is_dsi;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004581 u32 dspcntr;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004582
4583 WARN_ON(!crtc->enabled);
4584
4585 if (intel_crtc->active)
4586 return;
4587
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02004588 vlv_prepare_pll(intel_crtc);
4589
Daniel Vetter5b18e572014-04-24 23:55:06 +02004590 /* Set up the display plane register */
4591 dspcntr = DISPPLANE_GAMMA_ENABLE;
4592
4593 if (intel_crtc->config.has_dp_encoder)
4594 intel_dp_set_m_n(intel_crtc);
4595
4596 intel_set_pipe_timings(intel_crtc);
4597
4598 /* pipesrc and dspsize control the size that is scaled from,
4599 * which should always be the user's requested size.
4600 */
4601 I915_WRITE(DSPSIZE(plane),
4602 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4603 (intel_crtc->config.pipe_src_w - 1));
4604 I915_WRITE(DSPPOS(plane), 0);
4605
4606 i9xx_set_pipeconf(intel_crtc);
4607
4608 I915_WRITE(DSPCNTR(plane), dspcntr);
4609 POSTING_READ(DSPCNTR(plane));
4610
4611 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4612 crtc->x, crtc->y);
4613
Jesse Barnes89b667f2013-04-18 14:51:36 -07004614 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004615
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004616 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4617
Jesse Barnes89b667f2013-04-18 14:51:36 -07004618 for_each_encoder_on_crtc(dev, crtc, encoder)
4619 if (encoder->pre_pll_enable)
4620 encoder->pre_pll_enable(encoder);
4621
Jani Nikula23538ef2013-08-27 15:12:22 +03004622 is_dsi = intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI);
4623
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004624 if (!is_dsi) {
4625 if (IS_CHERRYVIEW(dev))
4626 chv_enable_pll(intel_crtc);
4627 else
4628 vlv_enable_pll(intel_crtc);
4629 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004630
4631 for_each_encoder_on_crtc(dev, crtc, encoder)
4632 if (encoder->pre_enable)
4633 encoder->pre_enable(encoder);
4634
Jesse Barnes2dd24552013-04-25 12:55:01 -07004635 i9xx_pfit_enable(intel_crtc);
4636
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004637 intel_crtc_load_lut(crtc);
4638
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004639 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004640 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004641
Jani Nikula50049452013-07-30 12:20:32 +03004642 for_each_encoder_on_crtc(dev, crtc, encoder)
4643 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004644
4645 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004646
Daniel Vetter87b6b102014-05-15 15:33:46 +02004647 drm_crtc_vblank_on(crtc);
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004648
4649 /* Underruns don't raise interrupts, so check manually. */
4650 i9xx_check_fifo_underruns(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004651}
4652
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004653static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4654{
4655 struct drm_device *dev = crtc->base.dev;
4656 struct drm_i915_private *dev_priv = dev->dev_private;
4657
4658 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4659 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4660}
4661
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004662static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004663{
4664 struct drm_device *dev = crtc->dev;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004665 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08004666 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004667 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004668 int pipe = intel_crtc->pipe;
Daniel Vetter5b18e572014-04-24 23:55:06 +02004669 int plane = intel_crtc->plane;
4670 u32 dspcntr;
Jesse Barnes79e53942008-11-07 14:24:08 -08004671
Daniel Vetter08a48462012-07-02 11:43:47 +02004672 WARN_ON(!crtc->enabled);
4673
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004674 if (intel_crtc->active)
4675 return;
4676
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004677 i9xx_set_pll_dividers(intel_crtc);
4678
Daniel Vetter5b18e572014-04-24 23:55:06 +02004679 /* Set up the display plane register */
4680 dspcntr = DISPPLANE_GAMMA_ENABLE;
4681
4682 if (pipe == 0)
4683 dspcntr &= ~DISPPLANE_SEL_PIPE_MASK;
4684 else
4685 dspcntr |= DISPPLANE_SEL_PIPE_B;
4686
4687 if (intel_crtc->config.has_dp_encoder)
4688 intel_dp_set_m_n(intel_crtc);
4689
4690 intel_set_pipe_timings(intel_crtc);
4691
4692 /* pipesrc and dspsize control the size that is scaled from,
4693 * which should always be the user's requested size.
4694 */
4695 I915_WRITE(DSPSIZE(plane),
4696 ((intel_crtc->config.pipe_src_h - 1) << 16) |
4697 (intel_crtc->config.pipe_src_w - 1));
4698 I915_WRITE(DSPPOS(plane), 0);
4699
4700 i9xx_set_pipeconf(intel_crtc);
4701
4702 I915_WRITE(DSPCNTR(plane), dspcntr);
4703 POSTING_READ(DSPCNTR(plane));
4704
4705 dev_priv->display.update_primary_plane(crtc, crtc->primary->fb,
4706 crtc->x, crtc->y);
4707
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004708 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004709
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004710 if (!IS_GEN2(dev))
4711 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4712
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004713 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004714 if (encoder->pre_enable)
4715 encoder->pre_enable(encoder);
4716
Daniel Vetterf6736a12013-06-05 13:34:30 +02004717 i9xx_enable_pll(intel_crtc);
4718
Jesse Barnes2dd24552013-04-25 12:55:01 -07004719 i9xx_pfit_enable(intel_crtc);
4720
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004721 intel_crtc_load_lut(crtc);
4722
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004723 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004724 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004725
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004726 for_each_encoder_on_crtc(dev, crtc, encoder)
4727 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004728
4729 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004730
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004731 /*
4732 * Gen2 reports pipe underruns whenever all planes are disabled.
4733 * So don't enable underrun reporting before at least some planes
4734 * are enabled.
4735 * FIXME: Need to fix the logic to work when we turn off all planes
4736 * but leave the pipe running.
4737 */
4738 if (IS_GEN2(dev))
4739 intel_set_cpu_fifo_underrun_reporting(dev, pipe, true);
4740
Daniel Vetter87b6b102014-05-15 15:33:46 +02004741 drm_crtc_vblank_on(crtc);
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004742
4743 /* Underruns don't raise interrupts, so check manually. */
4744 i9xx_check_fifo_underruns(dev);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004745}
4746
Daniel Vetter87476d62013-04-11 16:29:06 +02004747static void i9xx_pfit_disable(struct intel_crtc *crtc)
4748{
4749 struct drm_device *dev = crtc->base.dev;
4750 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004751
4752 if (!crtc->config.gmch_pfit.control)
4753 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004754
4755 assert_pipe_disabled(dev_priv, crtc->pipe);
4756
Daniel Vetter328d8e82013-05-08 10:36:31 +02004757 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4758 I915_READ(PFIT_CONTROL));
4759 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004760}
4761
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004762static void i9xx_crtc_disable(struct drm_crtc *crtc)
4763{
4764 struct drm_device *dev = crtc->dev;
4765 struct drm_i915_private *dev_priv = dev->dev_private;
4766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004767 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004768 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004769
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004770 if (!intel_crtc->active)
4771 return;
4772
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004773 /*
4774 * Gen2 reports pipe underruns whenever all planes are disabled.
4775 * So diasble underrun reporting before all the planes get disabled.
4776 * FIXME: Need to fix the logic to work when we turn off all planes
4777 * but leave the pipe running.
4778 */
4779 if (IS_GEN2(dev))
4780 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4781
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004782 intel_crtc_disable_planes(crtc);
4783
Daniel Vetterea9d7582012-07-10 10:42:52 +02004784 for_each_encoder_on_crtc(dev, crtc, encoder)
4785 encoder->disable(encoder);
4786
Ville Syrjälä6304cd92014-04-25 13:30:12 +03004787 /*
4788 * On gen2 planes are double buffered but the pipe isn't, so we must
4789 * wait for planes to fully turn off before disabling the pipe.
4790 */
4791 if (IS_GEN2(dev))
4792 intel_wait_for_vblank(dev, pipe);
4793
Jesse Barnesb24e7172011-01-04 15:09:30 -08004794 intel_disable_pipe(dev_priv, pipe);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004795
Daniel Vetter87476d62013-04-11 16:29:06 +02004796 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02004797
Jesse Barnes89b667f2013-04-18 14:51:36 -07004798 for_each_encoder_on_crtc(dev, crtc, encoder)
4799 if (encoder->post_disable)
4800 encoder->post_disable(encoder);
4801
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03004802 if (!intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI)) {
4803 if (IS_CHERRYVIEW(dev))
4804 chv_disable_pll(dev_priv, pipe);
4805 else if (IS_VALLEYVIEW(dev))
4806 vlv_disable_pll(dev_priv, pipe);
4807 else
4808 i9xx_disable_pll(dev_priv, pipe);
4809 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004810
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004811 if (!IS_GEN2(dev))
4812 intel_set_cpu_fifo_underrun_reporting(dev, pipe, false);
4813
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004814 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004815 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004816
Daniel Vetterefa96242014-04-24 23:55:02 +02004817 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004818 intel_update_fbc(dev);
Daniel Vetter71b1c372014-04-24 23:55:03 +02004819 intel_edp_psr_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02004820 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004821}
4822
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004823static void i9xx_crtc_off(struct drm_crtc *crtc)
4824{
4825}
4826
Daniel Vetter976f8a22012-07-08 22:34:21 +02004827static void intel_crtc_update_sarea(struct drm_crtc *crtc,
4828 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004829{
4830 struct drm_device *dev = crtc->dev;
4831 struct drm_i915_master_private *master_priv;
4832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4833 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004834
4835 if (!dev->primary->master)
4836 return;
4837
4838 master_priv = dev->primary->master->driver_priv;
4839 if (!master_priv->sarea_priv)
4840 return;
4841
Jesse Barnes79e53942008-11-07 14:24:08 -08004842 switch (pipe) {
4843 case 0:
4844 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
4845 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
4846 break;
4847 case 1:
4848 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
4849 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
4850 break;
4851 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004852 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08004853 break;
4854 }
Jesse Barnes79e53942008-11-07 14:24:08 -08004855}
4856
Daniel Vetter976f8a22012-07-08 22:34:21 +02004857/**
4858 * Sets the power management mode of the pipe and plane.
4859 */
4860void intel_crtc_update_dpms(struct drm_crtc *crtc)
Chris Wilsoncdd59982010-09-08 16:30:16 +01004861{
Chris Wilsoncdd59982010-09-08 16:30:16 +01004862 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004863 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004864 struct intel_encoder *intel_encoder;
4865 bool enable = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004866
Daniel Vetter976f8a22012-07-08 22:34:21 +02004867 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4868 enable |= intel_encoder->connectors_active;
4869
4870 if (enable)
4871 dev_priv->display.crtc_enable(crtc);
4872 else
4873 dev_priv->display.crtc_disable(crtc);
4874
4875 intel_crtc_update_sarea(crtc, enable);
4876}
4877
Daniel Vetter976f8a22012-07-08 22:34:21 +02004878static void intel_crtc_disable(struct drm_crtc *crtc)
4879{
4880 struct drm_device *dev = crtc->dev;
4881 struct drm_connector *connector;
4882 struct drm_i915_private *dev_priv = dev->dev_private;
4883
4884 /* crtc should still be enabled when we disable it. */
4885 WARN_ON(!crtc->enabled);
4886
4887 dev_priv->display.crtc_disable(crtc);
4888 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004889 dev_priv->display.off(crtc);
4890
Chris Wilson931872f2012-01-16 23:01:13 +00004891 assert_plane_disabled(dev->dev_private, to_intel_crtc(crtc)->plane);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03004892 assert_cursor_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Chris Wilson931872f2012-01-16 23:01:13 +00004893 assert_pipe_disabled(dev->dev_private, to_intel_crtc(crtc)->pipe);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004894
Matt Roperf4510a22014-04-01 15:22:40 -07004895 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01004896 mutex_lock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004897 intel_unpin_fb_obj(to_intel_framebuffer(crtc->primary->fb)->obj);
Chris Wilsoncdd59982010-09-08 16:30:16 +01004898 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07004899 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02004900 }
4901
4902 /* Update computed state. */
4903 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
4904 if (!connector->encoder || !connector->encoder->crtc)
4905 continue;
4906
4907 if (connector->encoder->crtc != crtc)
4908 continue;
4909
4910 connector->dpms = DRM_MODE_DPMS_OFF;
4911 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01004912 }
4913}
4914
Chris Wilsonea5b2132010-08-04 13:50:23 +01004915void intel_encoder_destroy(struct drm_encoder *encoder)
4916{
Chris Wilson4ef69c72010-09-09 15:14:28 +01004917 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01004918
Chris Wilsonea5b2132010-08-04 13:50:23 +01004919 drm_encoder_cleanup(encoder);
4920 kfree(intel_encoder);
4921}
4922
Damien Lespiau92373292013-08-08 22:28:57 +01004923/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004924 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
4925 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01004926static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004927{
4928 if (mode == DRM_MODE_DPMS_ON) {
4929 encoder->connectors_active = true;
4930
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004931 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004932 } else {
4933 encoder->connectors_active = false;
4934
Daniel Vetterb2cabb02012-07-01 22:42:24 +02004935 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004936 }
4937}
4938
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004939/* Cross check the actual hw state with our own modeset state tracking (and it's
4940 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02004941static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004942{
4943 if (connector->get_hw_state(connector)) {
4944 struct intel_encoder *encoder = connector->encoder;
4945 struct drm_crtc *crtc;
4946 bool encoder_enabled;
4947 enum pipe pipe;
4948
4949 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
4950 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03004951 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004952
4953 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
4954 "wrong connector dpms state\n");
4955 WARN(connector->base.encoder != &encoder->base,
4956 "active connector not linked to encoder\n");
4957 WARN(!encoder->connectors_active,
4958 "encoder->connectors_active not set\n");
4959
4960 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
4961 WARN(!encoder_enabled, "encoder not enabled\n");
4962 if (WARN_ON(!encoder->base.crtc))
4963 return;
4964
4965 crtc = encoder->base.crtc;
4966
4967 WARN(!crtc->enabled, "crtc not enabled\n");
4968 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
4969 WARN(pipe != to_intel_crtc(crtc)->pipe,
4970 "encoder active on the wrong pipe\n");
4971 }
4972}
4973
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004974/* Even simpler default implementation, if there's really no special case to
4975 * consider. */
4976void intel_connector_dpms(struct drm_connector *connector, int mode)
4977{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004978 /* All the simple cases only support two dpms states. */
4979 if (mode != DRM_MODE_DPMS_ON)
4980 mode = DRM_MODE_DPMS_OFF;
4981
4982 if (mode == connector->dpms)
4983 return;
4984
4985 connector->dpms = mode;
4986
4987 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01004988 if (connector->encoder)
4989 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02004990
Daniel Vetterb9805142012-08-31 17:37:33 +02004991 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02004992}
4993
Daniel Vetterf0947c32012-07-02 13:10:34 +02004994/* Simple connector->get_hw_state implementation for encoders that support only
4995 * one connector and no cloning and hence the encoder state determines the state
4996 * of the connector. */
4997bool intel_connector_get_hw_state(struct intel_connector *connector)
4998{
Daniel Vetter24929352012-07-02 20:28:59 +02004999 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005000 struct intel_encoder *encoder = connector->encoder;
5001
5002 return encoder->get_hw_state(encoder, &pipe);
5003}
5004
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005005static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5006 struct intel_crtc_config *pipe_config)
5007{
5008 struct drm_i915_private *dev_priv = dev->dev_private;
5009 struct intel_crtc *pipe_B_crtc =
5010 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5011
5012 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5013 pipe_name(pipe), pipe_config->fdi_lanes);
5014 if (pipe_config->fdi_lanes > 4) {
5015 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5016 pipe_name(pipe), pipe_config->fdi_lanes);
5017 return false;
5018 }
5019
Paulo Zanonibafb6552013-11-02 21:07:44 -07005020 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005021 if (pipe_config->fdi_lanes > 2) {
5022 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5023 pipe_config->fdi_lanes);
5024 return false;
5025 } else {
5026 return true;
5027 }
5028 }
5029
5030 if (INTEL_INFO(dev)->num_pipes == 2)
5031 return true;
5032
5033 /* Ivybridge 3 pipe is really complicated */
5034 switch (pipe) {
5035 case PIPE_A:
5036 return true;
5037 case PIPE_B:
5038 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5039 pipe_config->fdi_lanes > 2) {
5040 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5041 pipe_name(pipe), pipe_config->fdi_lanes);
5042 return false;
5043 }
5044 return true;
5045 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005046 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005047 pipe_B_crtc->config.fdi_lanes <= 2) {
5048 if (pipe_config->fdi_lanes > 2) {
5049 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5050 pipe_name(pipe), pipe_config->fdi_lanes);
5051 return false;
5052 }
5053 } else {
5054 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5055 return false;
5056 }
5057 return true;
5058 default:
5059 BUG();
5060 }
5061}
5062
Daniel Vettere29c22c2013-02-21 00:00:16 +01005063#define RETRY 1
5064static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5065 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005066{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005067 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005068 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005069 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005070 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005071
Daniel Vettere29c22c2013-02-21 00:00:16 +01005072retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005073 /* FDI is a binary signal running at ~2.7GHz, encoding
5074 * each output octet as 10 bits. The actual frequency
5075 * is stored as a divider into a 100MHz clock, and the
5076 * mode pixel clock is stored in units of 1KHz.
5077 * Hence the bw of each lane in terms of the mode signal
5078 * is:
5079 */
5080 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5081
Damien Lespiau241bfc32013-09-25 16:45:37 +01005082 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005083
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005084 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005085 pipe_config->pipe_bpp);
5086
5087 pipe_config->fdi_lanes = lane;
5088
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005089 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005090 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005091
Daniel Vettere29c22c2013-02-21 00:00:16 +01005092 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5093 intel_crtc->pipe, pipe_config);
5094 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5095 pipe_config->pipe_bpp -= 2*3;
5096 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5097 pipe_config->pipe_bpp);
5098 needs_recompute = true;
5099 pipe_config->bw_constrained = true;
5100
5101 goto retry;
5102 }
5103
5104 if (needs_recompute)
5105 return RETRY;
5106
5107 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005108}
5109
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005110static void hsw_compute_ips_config(struct intel_crtc *crtc,
5111 struct intel_crtc_config *pipe_config)
5112{
Jani Nikulad330a952014-01-21 11:24:25 +02005113 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005114 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005115 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005116}
5117
Daniel Vettera43f6e02013-06-07 23:10:32 +02005118static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005119 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005120{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005121 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005122 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005123
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005124 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005125 if (INTEL_INFO(dev)->gen < 4) {
5126 struct drm_i915_private *dev_priv = dev->dev_private;
5127 int clock_limit =
5128 dev_priv->display.get_display_clock_speed(dev);
5129
5130 /*
5131 * Enable pixel doubling when the dot clock
5132 * is > 90% of the (display) core speed.
5133 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005134 * GDG double wide on either pipe,
5135 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005136 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005137 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005138 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005139 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005140 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005141 }
5142
Damien Lespiau241bfc32013-09-25 16:45:37 +01005143 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005144 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005145 }
Chris Wilson89749352010-09-12 18:25:19 +01005146
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005147 /*
5148 * Pipe horizontal size must be even in:
5149 * - DVO ganged mode
5150 * - LVDS dual channel mode
5151 * - Double wide pipe
5152 */
5153 if ((intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
5154 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5155 pipe_config->pipe_src_w &= ~1;
5156
Damien Lespiau8693a822013-05-03 18:48:11 +01005157 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5158 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005159 */
5160 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5161 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005162 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005163
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005164 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005165 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005166 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005167 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5168 * for lvds. */
5169 pipe_config->pipe_bpp = 8*3;
5170 }
5171
Damien Lespiauf5adf942013-06-24 18:29:34 +01005172 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005173 hsw_compute_ips_config(crtc, pipe_config);
5174
5175 /* XXX: PCH clock sharing is done in ->mode_set, so make sure the old
5176 * clock survives for now. */
5177 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
5178 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005179
Daniel Vetter877d48d2013-04-19 11:24:43 +02005180 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005181 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005182
Daniel Vettere29c22c2013-02-21 00:00:16 +01005183 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005184}
5185
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005186static int valleyview_get_display_clock_speed(struct drm_device *dev)
5187{
5188 return 400000; /* FIXME */
5189}
5190
Jesse Barnese70236a2009-09-21 10:42:27 -07005191static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005192{
Jesse Barnese70236a2009-09-21 10:42:27 -07005193 return 400000;
5194}
Jesse Barnes79e53942008-11-07 14:24:08 -08005195
Jesse Barnese70236a2009-09-21 10:42:27 -07005196static int i915_get_display_clock_speed(struct drm_device *dev)
5197{
5198 return 333000;
5199}
Jesse Barnes79e53942008-11-07 14:24:08 -08005200
Jesse Barnese70236a2009-09-21 10:42:27 -07005201static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5202{
5203 return 200000;
5204}
Jesse Barnes79e53942008-11-07 14:24:08 -08005205
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005206static int pnv_get_display_clock_speed(struct drm_device *dev)
5207{
5208 u16 gcfgc = 0;
5209
5210 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5211
5212 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5213 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5214 return 267000;
5215 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5216 return 333000;
5217 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5218 return 444000;
5219 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5220 return 200000;
5221 default:
5222 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5223 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5224 return 133000;
5225 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5226 return 167000;
5227 }
5228}
5229
Jesse Barnese70236a2009-09-21 10:42:27 -07005230static int i915gm_get_display_clock_speed(struct drm_device *dev)
5231{
5232 u16 gcfgc = 0;
5233
5234 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5235
5236 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005237 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005238 else {
5239 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5240 case GC_DISPLAY_CLOCK_333_MHZ:
5241 return 333000;
5242 default:
5243 case GC_DISPLAY_CLOCK_190_200_MHZ:
5244 return 190000;
5245 }
5246 }
5247}
Jesse Barnes79e53942008-11-07 14:24:08 -08005248
Jesse Barnese70236a2009-09-21 10:42:27 -07005249static int i865_get_display_clock_speed(struct drm_device *dev)
5250{
5251 return 266000;
5252}
5253
5254static int i855_get_display_clock_speed(struct drm_device *dev)
5255{
5256 u16 hpllcc = 0;
5257 /* Assume that the hardware is in the high speed state. This
5258 * should be the default.
5259 */
5260 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5261 case GC_CLOCK_133_200:
5262 case GC_CLOCK_100_200:
5263 return 200000;
5264 case GC_CLOCK_166_250:
5265 return 250000;
5266 case GC_CLOCK_100_133:
5267 return 133000;
5268 }
5269
5270 /* Shouldn't happen */
5271 return 0;
5272}
5273
5274static int i830_get_display_clock_speed(struct drm_device *dev)
5275{
5276 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005277}
5278
Zhenyu Wang2c072452009-06-05 15:38:42 +08005279static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005280intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005281{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005282 while (*num > DATA_LINK_M_N_MASK ||
5283 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005284 *num >>= 1;
5285 *den >>= 1;
5286 }
5287}
5288
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005289static void compute_m_n(unsigned int m, unsigned int n,
5290 uint32_t *ret_m, uint32_t *ret_n)
5291{
5292 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5293 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5294 intel_reduce_m_n_ratio(ret_m, ret_n);
5295}
5296
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005297void
5298intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5299 int pixel_clock, int link_clock,
5300 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005301{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005302 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005303
5304 compute_m_n(bits_per_pixel * pixel_clock,
5305 link_clock * nlanes * 8,
5306 &m_n->gmch_m, &m_n->gmch_n);
5307
5308 compute_m_n(pixel_clock, link_clock,
5309 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005310}
5311
Chris Wilsona7615032011-01-12 17:04:08 +00005312static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5313{
Jani Nikulad330a952014-01-21 11:24:25 +02005314 if (i915.panel_use_ssc >= 0)
5315 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005316 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005317 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005318}
5319
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005320static int i9xx_get_refclk(struct drm_crtc *crtc, int num_connectors)
5321{
5322 struct drm_device *dev = crtc->dev;
5323 struct drm_i915_private *dev_priv = dev->dev_private;
5324 int refclk;
5325
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005326 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005327 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005328 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005329 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005330 refclk = dev_priv->vbt.lvds_ssc_freq;
5331 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005332 } else if (!IS_GEN2(dev)) {
5333 refclk = 96000;
5334 } else {
5335 refclk = 48000;
5336 }
5337
5338 return refclk;
5339}
5340
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005341static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005342{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005343 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005344}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005345
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005346static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5347{
5348 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005349}
5350
Daniel Vetterf47709a2013-03-28 10:42:02 +01005351static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005352 intel_clock_t *reduced_clock)
5353{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005354 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005355 u32 fp, fp2 = 0;
5356
5357 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005358 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005359 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005360 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005361 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005362 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005363 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005364 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005365 }
5366
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005367 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005368
Daniel Vetterf47709a2013-03-28 10:42:02 +01005369 crtc->lowfreq_avail = false;
5370 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005371 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005372 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005373 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005374 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005375 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005376 }
5377}
5378
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005379static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5380 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005381{
5382 u32 reg_val;
5383
5384 /*
5385 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5386 * and set it to a reasonable value instead.
5387 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005388 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005389 reg_val &= 0xffffff00;
5390 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005391 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005392
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005393 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005394 reg_val &= 0x8cffffff;
5395 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005396 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005397
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005398 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005399 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005400 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005401
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005402 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005403 reg_val &= 0x00ffffff;
5404 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005405 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005406}
5407
Daniel Vetterb5518422013-05-03 11:49:48 +02005408static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5409 struct intel_link_m_n *m_n)
5410{
5411 struct drm_device *dev = crtc->base.dev;
5412 struct drm_i915_private *dev_priv = dev->dev_private;
5413 int pipe = crtc->pipe;
5414
Daniel Vettere3b95f12013-05-03 11:49:49 +02005415 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5416 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5417 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5418 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005419}
5420
5421static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
5422 struct intel_link_m_n *m_n)
5423{
5424 struct drm_device *dev = crtc->base.dev;
5425 struct drm_i915_private *dev_priv = dev->dev_private;
5426 int pipe = crtc->pipe;
5427 enum transcoder transcoder = crtc->config.cpu_transcoder;
5428
5429 if (INTEL_INFO(dev)->gen >= 5) {
5430 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5431 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5432 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5433 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
5434 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005435 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5436 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5437 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5438 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005439 }
5440}
5441
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005442static void intel_dp_set_m_n(struct intel_crtc *crtc)
5443{
5444 if (crtc->config.has_pch_encoder)
5445 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5446 else
5447 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5448}
5449
Daniel Vetterf47709a2013-03-28 10:42:02 +01005450static void vlv_update_pll(struct intel_crtc *crtc)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005451{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005452 u32 dpll, dpll_md;
5453
5454 /*
5455 * Enable DPIO clock input. We should never disable the reference
5456 * clock for pipe B, since VGA hotplug / manual detection depends
5457 * on it.
5458 */
5459 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5460 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5461 /* We should never disable this, set it here for state tracking */
5462 if (crtc->pipe == PIPE_B)
5463 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5464 dpll |= DPLL_VCO_ENABLE;
5465 crtc->config.dpll_hw_state.dpll = dpll;
5466
5467 dpll_md = (crtc->config.pixel_multiplier - 1)
5468 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
5469 crtc->config.dpll_hw_state.dpll_md = dpll_md;
5470}
5471
5472static void vlv_prepare_pll(struct intel_crtc *crtc)
5473{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005474 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005475 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005476 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005477 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005478 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005479 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005480
Daniel Vetter09153002012-12-12 14:06:44 +01005481 mutex_lock(&dev_priv->dpio_lock);
5482
Daniel Vetterf47709a2013-03-28 10:42:02 +01005483 bestn = crtc->config.dpll.n;
5484 bestm1 = crtc->config.dpll.m1;
5485 bestm2 = crtc->config.dpll.m2;
5486 bestp1 = crtc->config.dpll.p1;
5487 bestp2 = crtc->config.dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005488
Jesse Barnes89b667f2013-04-18 14:51:36 -07005489 /* See eDP HDMI DPIO driver vbios notes doc */
5490
5491 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005492 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005493 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005494
5495 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005496 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005497
5498 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005499 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005500 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005501 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005502
5503 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005504 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005505
5506 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005507 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5508 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5509 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005510 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005511
5512 /*
5513 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5514 * but we don't support that).
5515 * Note: don't use the DAC post divider as it seems unstable.
5516 */
5517 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005518 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005519
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005520 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005521 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005522
Jesse Barnes89b667f2013-04-18 14:51:36 -07005523 /* Set HBR and RBR LPF coefficients */
Daniel Vetterff9a6752013-06-01 17:16:21 +02005524 if (crtc->config.port_clock == 162000 ||
Ville Syrjälä99750bd2013-06-14 14:02:52 +03005525 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_ANALOG) ||
Jesse Barnes89b667f2013-04-18 14:51:36 -07005526 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005527 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005528 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005529 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005530 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005531 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005532
Jesse Barnes89b667f2013-04-18 14:51:36 -07005533 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP) ||
5534 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT)) {
5535 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005536 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005537 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005538 0x0df40000);
5539 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005540 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005541 0x0df70000);
5542 } else { /* HDMI or VGA */
5543 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005544 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005545 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005546 0x0df70000);
5547 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005548 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005549 0x0df40000);
5550 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005551
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005552 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005553 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
5554 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT) ||
5555 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_EDP))
5556 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005557 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005558
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005559 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005560 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005561}
5562
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005563static void chv_update_pll(struct intel_crtc *crtc)
5564{
5565 struct drm_device *dev = crtc->base.dev;
5566 struct drm_i915_private *dev_priv = dev->dev_private;
5567 int pipe = crtc->pipe;
5568 int dpll_reg = DPLL(crtc->pipe);
5569 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005570 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005571 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5572 int refclk;
5573
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005574 crtc->config.dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
5575 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5576 DPLL_VCO_ENABLE;
5577 if (pipe != PIPE_A)
5578 crtc->config.dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5579
5580 crtc->config.dpll_hw_state.dpll_md =
5581 (crtc->config.pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005582
5583 bestn = crtc->config.dpll.n;
5584 bestm2_frac = crtc->config.dpll.m2 & 0x3fffff;
5585 bestm1 = crtc->config.dpll.m1;
5586 bestm2 = crtc->config.dpll.m2 >> 22;
5587 bestp1 = crtc->config.dpll.p1;
5588 bestp2 = crtc->config.dpll.p2;
5589
5590 /*
5591 * Enable Refclk and SSC
5592 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005593 I915_WRITE(dpll_reg,
5594 crtc->config.dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
5595
5596 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005597
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005598 /* p1 and p2 divider */
5599 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5600 5 << DPIO_CHV_S1_DIV_SHIFT |
5601 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5602 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5603 1 << DPIO_CHV_K_DIV_SHIFT);
5604
5605 /* Feedback post-divider - m2 */
5606 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5607
5608 /* Feedback refclk divider - n and m1 */
5609 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5610 DPIO_CHV_M1_DIV_BY_2 |
5611 1 << DPIO_CHV_N_DIV_SHIFT);
5612
5613 /* M2 fraction division */
5614 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5615
5616 /* M2 fraction division enable */
5617 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5618 DPIO_CHV_FRAC_DIV_EN |
5619 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5620
5621 /* Loop filter */
5622 refclk = i9xx_get_refclk(&crtc->base, 0);
5623 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5624 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5625 if (refclk == 100000)
5626 intcoeff = 11;
5627 else if (refclk == 38400)
5628 intcoeff = 10;
5629 else
5630 intcoeff = 9;
5631 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5632 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5633
5634 /* AFC Recal */
5635 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5636 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5637 DPIO_AFC_RECAL);
5638
5639 mutex_unlock(&dev_priv->dpio_lock);
5640}
5641
Daniel Vetterf47709a2013-03-28 10:42:02 +01005642static void i9xx_update_pll(struct intel_crtc *crtc,
5643 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005644 int num_connectors)
5645{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005646 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005647 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005648 u32 dpll;
5649 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005650 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005651
Daniel Vetterf47709a2013-03-28 10:42:02 +01005652 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305653
Daniel Vetterf47709a2013-03-28 10:42:02 +01005654 is_sdvo = intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_SDVO) ||
5655 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005656
5657 dpll = DPLL_VGA_MODE_DIS;
5658
Daniel Vetterf47709a2013-03-28 10:42:02 +01005659 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005660 dpll |= DPLLB_MODE_LVDS;
5661 else
5662 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01005663
Daniel Vetteref1b4602013-06-01 17:17:04 +02005664 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02005665 dpll |= (crtc->config.pixel_multiplier - 1)
5666 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005667 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02005668
5669 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02005670 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02005671
Daniel Vetterf47709a2013-03-28 10:42:02 +01005672 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02005673 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005674
5675 /* compute bitmask from p1 value */
5676 if (IS_PINEVIEW(dev))
5677 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
5678 else {
5679 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5680 if (IS_G4X(dev) && reduced_clock)
5681 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
5682 }
5683 switch (clock->p2) {
5684 case 5:
5685 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
5686 break;
5687 case 7:
5688 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
5689 break;
5690 case 10:
5691 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
5692 break;
5693 case 14:
5694 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
5695 break;
5696 }
5697 if (INTEL_INFO(dev)->gen >= 4)
5698 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
5699
Daniel Vetter09ede542013-04-30 14:01:45 +02005700 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005701 dpll |= PLL_REF_INPUT_TVCLKINBC;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005702 else if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005703 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5704 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5705 else
5706 dpll |= PLL_REF_INPUT_DREFCLK;
5707
5708 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005709 crtc->config.dpll_hw_state.dpll = dpll;
5710
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005711 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02005712 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
5713 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005714 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005715 }
5716}
5717
Daniel Vetterf47709a2013-03-28 10:42:02 +01005718static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01005719 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005720 int num_connectors)
5721{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005722 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005723 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005724 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005725 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005726
Daniel Vetterf47709a2013-03-28 10:42:02 +01005727 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05305728
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005729 dpll = DPLL_VGA_MODE_DIS;
5730
Daniel Vetterf47709a2013-03-28 10:42:02 +01005731 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005732 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5733 } else {
5734 if (clock->p1 == 2)
5735 dpll |= PLL_P1_DIVIDE_BY_TWO;
5736 else
5737 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
5738 if (clock->p2 == 4)
5739 dpll |= PLL_P2_DIVIDE_BY_4;
5740 }
5741
Daniel Vetter4a33e482013-07-06 12:52:05 +02005742 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_DVO))
5743 dpll |= DPLL_DVO_2X_MODE;
5744
Daniel Vetterf47709a2013-03-28 10:42:02 +01005745 if (intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005746 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
5747 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
5748 else
5749 dpll |= PLL_REF_INPUT_DREFCLK;
5750
5751 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005752 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02005753}
5754
Daniel Vetter8a654f32013-06-01 17:16:22 +02005755static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005756{
5757 struct drm_device *dev = intel_crtc->base.dev;
5758 struct drm_i915_private *dev_priv = dev->dev_private;
5759 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02005760 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02005761 struct drm_display_mode *adjusted_mode =
5762 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005763 uint32_t crtc_vtotal, crtc_vblank_end;
5764 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005765
5766 /* We need to be careful not to changed the adjusted mode, for otherwise
5767 * the hw state checker will get angry at the mismatch. */
5768 crtc_vtotal = adjusted_mode->crtc_vtotal;
5769 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005770
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005771 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005772 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005773 crtc_vtotal -= 1;
5774 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02005775
5776 if (intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5777 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5778 else
5779 vsyncshift = adjusted_mode->crtc_hsync_start -
5780 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02005781 if (vsyncshift < 0)
5782 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005783 }
5784
5785 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005786 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005787
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005788 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005789 (adjusted_mode->crtc_hdisplay - 1) |
5790 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005791 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005792 (adjusted_mode->crtc_hblank_start - 1) |
5793 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005794 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005795 (adjusted_mode->crtc_hsync_start - 1) |
5796 ((adjusted_mode->crtc_hsync_end - 1) << 16));
5797
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005798 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005799 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005800 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005801 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005802 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02005803 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02005804 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005805 (adjusted_mode->crtc_vsync_start - 1) |
5806 ((adjusted_mode->crtc_vsync_end - 1) << 16));
5807
Paulo Zanonib5e508d2012-10-24 11:34:43 -02005808 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5809 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5810 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5811 * bits. */
5812 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
5813 (pipe == PIPE_B || pipe == PIPE_C))
5814 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
5815
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005816 /* pipesrc controls the size that is scaled from, which should
5817 * always be the user's requested size.
5818 */
5819 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005820 ((intel_crtc->config.pipe_src_w - 1) << 16) |
5821 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03005822}
5823
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005824static void intel_get_pipe_timings(struct intel_crtc *crtc,
5825 struct intel_crtc_config *pipe_config)
5826{
5827 struct drm_device *dev = crtc->base.dev;
5828 struct drm_i915_private *dev_priv = dev->dev_private;
5829 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5830 uint32_t tmp;
5831
5832 tmp = I915_READ(HTOTAL(cpu_transcoder));
5833 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5834 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5835 tmp = I915_READ(HBLANK(cpu_transcoder));
5836 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
5837 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
5838 tmp = I915_READ(HSYNC(cpu_transcoder));
5839 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5840 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5841
5842 tmp = I915_READ(VTOTAL(cpu_transcoder));
5843 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5844 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5845 tmp = I915_READ(VBLANK(cpu_transcoder));
5846 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
5847 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
5848 tmp = I915_READ(VSYNC(cpu_transcoder));
5849 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5850 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5851
5852 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
5853 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5854 pipe_config->adjusted_mode.crtc_vtotal += 1;
5855 pipe_config->adjusted_mode.crtc_vblank_end += 1;
5856 }
5857
5858 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03005859 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5860 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5861
5862 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
5863 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02005864}
5865
Daniel Vetterf6a83282014-02-11 15:28:57 -08005866void intel_mode_from_pipe_config(struct drm_display_mode *mode,
5867 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03005868{
Daniel Vetterf6a83282014-02-11 15:28:57 -08005869 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
5870 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
5871 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
5872 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005873
Daniel Vetterf6a83282014-02-11 15:28:57 -08005874 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
5875 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
5876 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
5877 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03005878
Daniel Vetterf6a83282014-02-11 15:28:57 -08005879 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005880
Daniel Vetterf6a83282014-02-11 15:28:57 -08005881 mode->clock = pipe_config->adjusted_mode.crtc_clock;
5882 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03005883}
5884
Daniel Vetter84b046f2013-02-19 18:48:54 +01005885static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
5886{
5887 struct drm_device *dev = intel_crtc->base.dev;
5888 struct drm_i915_private *dev_priv = dev->dev_private;
5889 uint32_t pipeconf;
5890
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005891 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005892
Daniel Vetter67c72a12013-09-24 11:46:14 +02005893 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
5894 I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE)
5895 pipeconf |= PIPECONF_ENABLE;
5896
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005897 if (intel_crtc->config.double_wide)
5898 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01005899
Daniel Vetterff9ce462013-04-24 14:57:17 +02005900 /* only g4x and later have fancy bpc/dither controls */
5901 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02005902 /* Bspec claims that we can't use dithering for 30bpp pipes. */
5903 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
5904 pipeconf |= PIPECONF_DITHER_EN |
5905 PIPECONF_DITHER_TYPE_SP;
5906
5907 switch (intel_crtc->config.pipe_bpp) {
5908 case 18:
5909 pipeconf |= PIPECONF_6BPC;
5910 break;
5911 case 24:
5912 pipeconf |= PIPECONF_8BPC;
5913 break;
5914 case 30:
5915 pipeconf |= PIPECONF_10BPC;
5916 break;
5917 default:
5918 /* Case prevented by intel_choose_pipe_bpp_dither. */
5919 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01005920 }
5921 }
5922
5923 if (HAS_PIPE_CXSR(dev)) {
5924 if (intel_crtc->lowfreq_avail) {
5925 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
5926 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
5927 } else {
5928 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01005929 }
5930 }
5931
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02005932 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5933 if (INTEL_INFO(dev)->gen < 4 ||
5934 intel_pipe_has_type(&intel_crtc->base, INTEL_OUTPUT_SDVO))
5935 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5936 else
5937 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5938 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01005939 pipeconf |= PIPECONF_PROGRESSIVE;
5940
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02005941 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
5942 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03005943
Daniel Vetter84b046f2013-02-19 18:48:54 +01005944 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
5945 POSTING_READ(PIPECONF(intel_crtc->pipe));
5946}
5947
Eric Anholtf564048e2011-03-30 13:01:02 -07005948static int i9xx_crtc_mode_set(struct drm_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07005949 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02005950 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08005951{
5952 struct drm_device *dev = crtc->dev;
5953 struct drm_i915_private *dev_priv = dev->dev_private;
5954 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Eric Anholtc751ce42010-03-25 11:48:48 -07005955 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07005956 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02005957 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005958 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01005959 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08005960 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08005961
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02005962 for_each_encoder_on_crtc(dev, crtc, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01005963 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08005964 case INTEL_OUTPUT_LVDS:
5965 is_lvds = true;
5966 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005967 case INTEL_OUTPUT_DSI:
5968 is_dsi = true;
5969 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08005970 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05005971
Eric Anholtc751ce42010-03-25 11:48:48 -07005972 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08005973 }
5974
Jani Nikulaf2335332013-09-13 11:03:09 +03005975 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02005976 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005977
Jani Nikulaf2335332013-09-13 11:03:09 +03005978 if (!intel_crtc->config.clock_set) {
5979 refclk = i9xx_get_refclk(crtc, num_connectors);
5980
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005981 /*
5982 * Returns a set of divisors for the desired target clock with
5983 * the given refclk, or FALSE. The returned values represent
5984 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
5985 * 2) / p1 / p2.
5986 */
5987 limit = intel_limit(crtc, refclk);
5988 ok = dev_priv->display.find_dpll(limit, crtc,
5989 intel_crtc->config.port_clock,
5990 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03005991 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03005992 DRM_ERROR("Couldn't find PLL settings for mode!\n");
5993 return -EINVAL;
5994 }
Eric Anholtf564048e2011-03-30 13:01:02 -07005995
Jani Nikulaf2335332013-09-13 11:03:09 +03005996 if (is_lvds && dev_priv->lvds_downclock_avail) {
5997 /*
5998 * Ensure we match the reduced clock's P to the target
5999 * clock. If the clocks don't match, we can't switch
6000 * the display clock by using the FP0/FP1. In such case
6001 * we will disable the LVDS downclock feature.
6002 */
6003 has_reduced_clock =
6004 dev_priv->display.find_dpll(limit, crtc,
6005 dev_priv->lvds_downclock,
6006 refclk, &clock,
6007 &reduced_clock);
6008 }
6009 /* Compat-code for transition, will disappear. */
Daniel Vetterf47709a2013-03-28 10:42:02 +01006010 intel_crtc->config.dpll.n = clock.n;
6011 intel_crtc->config.dpll.m1 = clock.m1;
6012 intel_crtc->config.dpll.m2 = clock.m2;
6013 intel_crtc->config.dpll.p1 = clock.p1;
6014 intel_crtc->config.dpll.p2 = clock.p2;
6015 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006016
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006017 if (IS_GEN2(dev)) {
Daniel Vetter8a654f32013-06-01 17:16:22 +02006018 i8xx_update_pll(intel_crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306019 has_reduced_clock ? &reduced_clock : NULL,
6020 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006021 } else if (IS_CHERRYVIEW(dev)) {
6022 chv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006023 } else if (IS_VALLEYVIEW(dev)) {
Jani Nikulaf2335332013-09-13 11:03:09 +03006024 vlv_update_pll(intel_crtc);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006025 } else {
Daniel Vetterf47709a2013-03-28 10:42:02 +01006026 i9xx_update_pll(intel_crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006027 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006028 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006029 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006030
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006031 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006032}
6033
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006034static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6035 struct intel_crtc_config *pipe_config)
6036{
6037 struct drm_device *dev = crtc->base.dev;
6038 struct drm_i915_private *dev_priv = dev->dev_private;
6039 uint32_t tmp;
6040
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006041 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6042 return;
6043
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006044 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006045 if (!(tmp & PFIT_ENABLE))
6046 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006047
Daniel Vetter06922822013-07-11 13:35:40 +02006048 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006049 if (INTEL_INFO(dev)->gen < 4) {
6050 if (crtc->pipe != PIPE_B)
6051 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006052 } else {
6053 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6054 return;
6055 }
6056
Daniel Vetter06922822013-07-11 13:35:40 +02006057 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006058 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6059 if (INTEL_INFO(dev)->gen < 5)
6060 pipe_config->gmch_pfit.lvds_border_bits =
6061 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6062}
6063
Jesse Barnesacbec812013-09-20 11:29:32 -07006064static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6065 struct intel_crtc_config *pipe_config)
6066{
6067 struct drm_device *dev = crtc->base.dev;
6068 struct drm_i915_private *dev_priv = dev->dev_private;
6069 int pipe = pipe_config->cpu_transcoder;
6070 intel_clock_t clock;
6071 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006072 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006073
6074 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006075 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006076 mutex_unlock(&dev_priv->dpio_lock);
6077
6078 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6079 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6080 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6081 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6082 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6083
Ville Syrjäläf6466282013-10-14 14:50:31 +03006084 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006085
Ville Syrjäläf6466282013-10-14 14:50:31 +03006086 /* clock.dot is the fast clock */
6087 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006088}
6089
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006090static void i9xx_get_plane_config(struct intel_crtc *crtc,
6091 struct intel_plane_config *plane_config)
6092{
6093 struct drm_device *dev = crtc->base.dev;
6094 struct drm_i915_private *dev_priv = dev->dev_private;
6095 u32 val, base, offset;
6096 int pipe = crtc->pipe, plane = crtc->plane;
6097 int fourcc, pixel_format;
6098 int aligned_height;
6099
Dave Airlie66e514c2014-04-03 07:51:54 +10006100 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6101 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006102 DRM_DEBUG_KMS("failed to alloc fb\n");
6103 return;
6104 }
6105
6106 val = I915_READ(DSPCNTR(plane));
6107
6108 if (INTEL_INFO(dev)->gen >= 4)
6109 if (val & DISPPLANE_TILED)
6110 plane_config->tiled = true;
6111
6112 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6113 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006114 crtc->base.primary->fb->pixel_format = fourcc;
6115 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006116 drm_format_plane_cpp(fourcc, 0) * 8;
6117
6118 if (INTEL_INFO(dev)->gen >= 4) {
6119 if (plane_config->tiled)
6120 offset = I915_READ(DSPTILEOFF(plane));
6121 else
6122 offset = I915_READ(DSPLINOFF(plane));
6123 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6124 } else {
6125 base = I915_READ(DSPADDR(plane));
6126 }
6127 plane_config->base = base;
6128
6129 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006130 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6131 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006132
6133 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006134 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006135
Dave Airlie66e514c2014-04-03 07:51:54 +10006136 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006137 plane_config->tiled);
6138
Dave Airlie66e514c2014-04-03 07:51:54 +10006139 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006140 aligned_height, PAGE_SIZE);
6141
6142 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10006143 pipe, plane, crtc->base.primary->fb->width,
6144 crtc->base.primary->fb->height,
6145 crtc->base.primary->fb->bits_per_pixel, base,
6146 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006147 plane_config->size);
6148
6149}
6150
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006151static void chv_crtc_clock_get(struct intel_crtc *crtc,
6152 struct intel_crtc_config *pipe_config)
6153{
6154 struct drm_device *dev = crtc->base.dev;
6155 struct drm_i915_private *dev_priv = dev->dev_private;
6156 int pipe = pipe_config->cpu_transcoder;
6157 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6158 intel_clock_t clock;
6159 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6160 int refclk = 100000;
6161
6162 mutex_lock(&dev_priv->dpio_lock);
6163 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6164 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6165 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6166 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6167 mutex_unlock(&dev_priv->dpio_lock);
6168
6169 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6170 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6171 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6172 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6173 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6174
6175 chv_clock(refclk, &clock);
6176
6177 /* clock.dot is the fast clock */
6178 pipe_config->port_clock = clock.dot / 5;
6179}
6180
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006181static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6182 struct intel_crtc_config *pipe_config)
6183{
6184 struct drm_device *dev = crtc->base.dev;
6185 struct drm_i915_private *dev_priv = dev->dev_private;
6186 uint32_t tmp;
6187
Imre Deakb5482bd2014-03-05 16:20:55 +02006188 if (!intel_display_power_enabled(dev_priv,
6189 POWER_DOMAIN_PIPE(crtc->pipe)))
6190 return false;
6191
Daniel Vettere143a212013-07-04 12:01:15 +02006192 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006193 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006194
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006195 tmp = I915_READ(PIPECONF(crtc->pipe));
6196 if (!(tmp & PIPECONF_ENABLE))
6197 return false;
6198
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006199 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6200 switch (tmp & PIPECONF_BPC_MASK) {
6201 case PIPECONF_6BPC:
6202 pipe_config->pipe_bpp = 18;
6203 break;
6204 case PIPECONF_8BPC:
6205 pipe_config->pipe_bpp = 24;
6206 break;
6207 case PIPECONF_10BPC:
6208 pipe_config->pipe_bpp = 30;
6209 break;
6210 default:
6211 break;
6212 }
6213 }
6214
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006215 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6216 pipe_config->limited_color_range = true;
6217
Ville Syrjälä282740f2013-09-04 18:30:03 +03006218 if (INTEL_INFO(dev)->gen < 4)
6219 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6220
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006221 intel_get_pipe_timings(crtc, pipe_config);
6222
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006223 i9xx_get_pfit_config(crtc, pipe_config);
6224
Daniel Vetter6c49f242013-06-06 12:45:25 +02006225 if (INTEL_INFO(dev)->gen >= 4) {
6226 tmp = I915_READ(DPLL_MD(crtc->pipe));
6227 pipe_config->pixel_multiplier =
6228 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6229 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006230 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006231 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6232 tmp = I915_READ(DPLL(crtc->pipe));
6233 pipe_config->pixel_multiplier =
6234 ((tmp & SDVO_MULTIPLIER_MASK)
6235 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6236 } else {
6237 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6238 * port and will be fixed up in the encoder->get_config
6239 * function. */
6240 pipe_config->pixel_multiplier = 1;
6241 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006242 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6243 if (!IS_VALLEYVIEW(dev)) {
6244 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6245 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006246 } else {
6247 /* Mask out read-only status bits. */
6248 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6249 DPLL_PORTC_READY_MASK |
6250 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006251 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006252
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006253 if (IS_CHERRYVIEW(dev))
6254 chv_crtc_clock_get(crtc, pipe_config);
6255 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006256 vlv_crtc_clock_get(crtc, pipe_config);
6257 else
6258 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006259
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006260 return true;
6261}
6262
Paulo Zanonidde86e22012-12-01 12:04:25 -02006263static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006264{
6265 struct drm_i915_private *dev_priv = dev->dev_private;
6266 struct drm_mode_config *mode_config = &dev->mode_config;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006267 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006268 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006269 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006270 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006271 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006272 bool has_ck505 = false;
6273 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006274
6275 /* We need to take the global config into account */
Keith Packard199e5d72011-09-22 12:01:57 -07006276 list_for_each_entry(encoder, &mode_config->encoder_list,
6277 base.head) {
6278 switch (encoder->type) {
6279 case INTEL_OUTPUT_LVDS:
6280 has_panel = true;
6281 has_lvds = true;
6282 break;
6283 case INTEL_OUTPUT_EDP:
6284 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006285 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006286 has_cpu_edp = true;
6287 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006288 }
6289 }
6290
Keith Packard99eb6a02011-09-26 14:29:12 -07006291 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006292 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006293 can_ssc = has_ck505;
6294 } else {
6295 has_ck505 = false;
6296 can_ssc = true;
6297 }
6298
Imre Deak2de69052013-05-08 13:14:04 +03006299 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6300 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006301
6302 /* Ironlake: try to setup display ref clock before DPLL
6303 * enabling. This is only under driver's control after
6304 * PCH B stepping, previous chipset stepping should be
6305 * ignoring this setting.
6306 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006307 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006308
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006309 /* As we must carefully and slowly disable/enable each source in turn,
6310 * compute the final state we want first and check if we need to
6311 * make any changes at all.
6312 */
6313 final = val;
6314 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006315 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006316 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006317 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006318 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6319
6320 final &= ~DREF_SSC_SOURCE_MASK;
6321 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6322 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006323
Keith Packard199e5d72011-09-22 12:01:57 -07006324 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006325 final |= DREF_SSC_SOURCE_ENABLE;
6326
6327 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6328 final |= DREF_SSC1_ENABLE;
6329
6330 if (has_cpu_edp) {
6331 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6332 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6333 else
6334 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6335 } else
6336 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6337 } else {
6338 final |= DREF_SSC_SOURCE_DISABLE;
6339 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6340 }
6341
6342 if (final == val)
6343 return;
6344
6345 /* Always enable nonspread source */
6346 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6347
6348 if (has_ck505)
6349 val |= DREF_NONSPREAD_CK505_ENABLE;
6350 else
6351 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6352
6353 if (has_panel) {
6354 val &= ~DREF_SSC_SOURCE_MASK;
6355 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006356
Keith Packard199e5d72011-09-22 12:01:57 -07006357 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006358 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006359 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006360 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006361 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006362 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006363
6364 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006365 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006366 POSTING_READ(PCH_DREF_CONTROL);
6367 udelay(200);
6368
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006369 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006370
6371 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006372 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006373 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006374 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006375 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006376 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006377 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006378 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006379 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006380
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006381 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006382 POSTING_READ(PCH_DREF_CONTROL);
6383 udelay(200);
6384 } else {
6385 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6386
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006387 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006388
6389 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006390 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006391
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006392 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006393 POSTING_READ(PCH_DREF_CONTROL);
6394 udelay(200);
6395
6396 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006397 val &= ~DREF_SSC_SOURCE_MASK;
6398 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006399
6400 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006401 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006402
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006403 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006404 POSTING_READ(PCH_DREF_CONTROL);
6405 udelay(200);
6406 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006407
6408 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006409}
6410
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006411static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006412{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006413 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006414
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006415 tmp = I915_READ(SOUTH_CHICKEN2);
6416 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6417 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006418
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006419 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6420 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6421 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006422
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006423 tmp = I915_READ(SOUTH_CHICKEN2);
6424 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6425 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006426
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006427 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6428 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6429 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006430}
6431
6432/* WaMPhyProgramming:hsw */
6433static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6434{
6435 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006436
6437 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6438 tmp &= ~(0xFF << 24);
6439 tmp |= (0x12 << 24);
6440 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6441
Paulo Zanonidde86e22012-12-01 12:04:25 -02006442 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6443 tmp |= (1 << 11);
6444 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6445
6446 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6447 tmp |= (1 << 11);
6448 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6449
Paulo Zanonidde86e22012-12-01 12:04:25 -02006450 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6451 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6452 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6453
6454 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6455 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6456 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6457
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006458 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6459 tmp &= ~(7 << 13);
6460 tmp |= (5 << 13);
6461 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006462
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006463 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6464 tmp &= ~(7 << 13);
6465 tmp |= (5 << 13);
6466 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006467
6468 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6469 tmp &= ~0xFF;
6470 tmp |= 0x1C;
6471 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6472
6473 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6474 tmp &= ~0xFF;
6475 tmp |= 0x1C;
6476 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6477
6478 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6479 tmp &= ~(0xFF << 16);
6480 tmp |= (0x1C << 16);
6481 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6482
6483 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6484 tmp &= ~(0xFF << 16);
6485 tmp |= (0x1C << 16);
6486 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6487
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006488 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6489 tmp |= (1 << 27);
6490 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006491
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006492 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6493 tmp |= (1 << 27);
6494 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006495
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006496 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6497 tmp &= ~(0xF << 28);
6498 tmp |= (4 << 28);
6499 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006500
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006501 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6502 tmp &= ~(0xF << 28);
6503 tmp |= (4 << 28);
6504 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006505}
6506
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006507/* Implements 3 different sequences from BSpec chapter "Display iCLK
6508 * Programming" based on the parameters passed:
6509 * - Sequence to enable CLKOUT_DP
6510 * - Sequence to enable CLKOUT_DP without spread
6511 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6512 */
6513static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6514 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006515{
6516 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006517 uint32_t reg, tmp;
6518
6519 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6520 with_spread = true;
6521 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6522 with_fdi, "LP PCH doesn't have FDI\n"))
6523 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006524
6525 mutex_lock(&dev_priv->dpio_lock);
6526
6527 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6528 tmp &= ~SBI_SSCCTL_DISABLE;
6529 tmp |= SBI_SSCCTL_PATHALT;
6530 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6531
6532 udelay(24);
6533
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006534 if (with_spread) {
6535 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6536 tmp &= ~SBI_SSCCTL_PATHALT;
6537 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006538
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006539 if (with_fdi) {
6540 lpt_reset_fdi_mphy(dev_priv);
6541 lpt_program_fdi_mphy(dev_priv);
6542 }
6543 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006544
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006545 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6546 SBI_GEN0 : SBI_DBUFF0;
6547 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6548 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6549 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006550
6551 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006552}
6553
Paulo Zanoni47701c32013-07-23 11:19:25 -03006554/* Sequence to disable CLKOUT_DP */
6555static void lpt_disable_clkout_dp(struct drm_device *dev)
6556{
6557 struct drm_i915_private *dev_priv = dev->dev_private;
6558 uint32_t reg, tmp;
6559
6560 mutex_lock(&dev_priv->dpio_lock);
6561
6562 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6563 SBI_GEN0 : SBI_DBUFF0;
6564 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6565 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6566 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6567
6568 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6569 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6570 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6571 tmp |= SBI_SSCCTL_PATHALT;
6572 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6573 udelay(32);
6574 }
6575 tmp |= SBI_SSCCTL_DISABLE;
6576 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6577 }
6578
6579 mutex_unlock(&dev_priv->dpio_lock);
6580}
6581
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006582static void lpt_init_pch_refclk(struct drm_device *dev)
6583{
6584 struct drm_mode_config *mode_config = &dev->mode_config;
6585 struct intel_encoder *encoder;
6586 bool has_vga = false;
6587
6588 list_for_each_entry(encoder, &mode_config->encoder_list, base.head) {
6589 switch (encoder->type) {
6590 case INTEL_OUTPUT_ANALOG:
6591 has_vga = true;
6592 break;
6593 }
6594 }
6595
Paulo Zanoni47701c32013-07-23 11:19:25 -03006596 if (has_vga)
6597 lpt_enable_clkout_dp(dev, true, true);
6598 else
6599 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006600}
6601
Paulo Zanonidde86e22012-12-01 12:04:25 -02006602/*
6603 * Initialize reference clocks when the driver loads
6604 */
6605void intel_init_pch_refclk(struct drm_device *dev)
6606{
6607 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6608 ironlake_init_pch_refclk(dev);
6609 else if (HAS_PCH_LPT(dev))
6610 lpt_init_pch_refclk(dev);
6611}
6612
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006613static int ironlake_get_refclk(struct drm_crtc *crtc)
6614{
6615 struct drm_device *dev = crtc->dev;
6616 struct drm_i915_private *dev_priv = dev->dev_private;
6617 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006618 int num_connectors = 0;
6619 bool is_lvds = false;
6620
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006621 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006622 switch (encoder->type) {
6623 case INTEL_OUTPUT_LVDS:
6624 is_lvds = true;
6625 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006626 }
6627 num_connectors++;
6628 }
6629
6630 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006631 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006632 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006633 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006634 }
6635
6636 return 120000;
6637}
6638
Daniel Vetter6ff93602013-04-19 11:24:36 +02006639static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03006640{
6641 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
6642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6643 int pipe = intel_crtc->pipe;
6644 uint32_t val;
6645
Daniel Vetter78114072013-06-13 00:54:57 +02006646 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03006647
Daniel Vetter965e0c42013-03-27 00:44:57 +01006648 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03006649 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006650 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006651 break;
6652 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006653 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006654 break;
6655 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006656 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006657 break;
6658 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01006659 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03006660 break;
6661 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03006662 /* Case prevented by intel_choose_pipe_bpp_dither. */
6663 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03006664 }
6665
Daniel Vetterd8b32242013-04-25 17:54:44 +02006666 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03006667 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6668
Daniel Vetter6ff93602013-04-19 11:24:36 +02006669 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03006670 val |= PIPECONF_INTERLACED_ILK;
6671 else
6672 val |= PIPECONF_PROGRESSIVE;
6673
Daniel Vetter50f3b012013-03-27 00:44:56 +01006674 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006675 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02006676
Paulo Zanonic8203562012-09-12 10:06:29 -03006677 I915_WRITE(PIPECONF(pipe), val);
6678 POSTING_READ(PIPECONF(pipe));
6679}
6680
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006681/*
6682 * Set up the pipe CSC unit.
6683 *
6684 * Currently only full range RGB to limited range RGB conversion
6685 * is supported, but eventually this should handle various
6686 * RGB<->YCbCr scenarios as well.
6687 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01006688static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006689{
6690 struct drm_device *dev = crtc->dev;
6691 struct drm_i915_private *dev_priv = dev->dev_private;
6692 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6693 int pipe = intel_crtc->pipe;
6694 uint16_t coeff = 0x7800; /* 1.0 */
6695
6696 /*
6697 * TODO: Check what kind of values actually come out of the pipe
6698 * with these coeff/postoff values and adjust to get the best
6699 * accuracy. Perhaps we even need to take the bpc value into
6700 * consideration.
6701 */
6702
Daniel Vetter50f3b012013-03-27 00:44:56 +01006703 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006704 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
6705
6706 /*
6707 * GY/GU and RY/RU should be the other way around according
6708 * to BSpec, but reality doesn't agree. Just set them up in
6709 * a way that results in the correct picture.
6710 */
6711 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
6712 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
6713
6714 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
6715 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
6716
6717 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
6718 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
6719
6720 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
6721 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
6722 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
6723
6724 if (INTEL_INFO(dev)->gen > 6) {
6725 uint16_t postoff = 0;
6726
Daniel Vetter50f3b012013-03-27 00:44:56 +01006727 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02006728 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006729
6730 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
6731 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
6732 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
6733
6734 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
6735 } else {
6736 uint32_t mode = CSC_MODE_YUV_TO_RGB;
6737
Daniel Vetter50f3b012013-03-27 00:44:56 +01006738 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02006739 mode |= CSC_BLACK_SCREEN_OFFSET;
6740
6741 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
6742 }
6743}
6744
Daniel Vetter6ff93602013-04-19 11:24:36 +02006745static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006746{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006747 struct drm_device *dev = crtc->dev;
6748 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006749 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006750 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006751 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006752 uint32_t val;
6753
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006754 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006755
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006756 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006757 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6758
Daniel Vetter6ff93602013-04-19 11:24:36 +02006759 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006760 val |= PIPECONF_INTERLACED_ILK;
6761 else
6762 val |= PIPECONF_PROGRESSIVE;
6763
Paulo Zanoni702e7a52012-10-23 18:29:59 -02006764 I915_WRITE(PIPECONF(cpu_transcoder), val);
6765 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02006766
6767 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
6768 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07006769
6770 if (IS_BROADWELL(dev)) {
6771 val = 0;
6772
6773 switch (intel_crtc->config.pipe_bpp) {
6774 case 18:
6775 val |= PIPEMISC_DITHER_6_BPC;
6776 break;
6777 case 24:
6778 val |= PIPEMISC_DITHER_8_BPC;
6779 break;
6780 case 30:
6781 val |= PIPEMISC_DITHER_10_BPC;
6782 break;
6783 case 36:
6784 val |= PIPEMISC_DITHER_12_BPC;
6785 break;
6786 default:
6787 /* Case prevented by pipe_config_set_bpp. */
6788 BUG();
6789 }
6790
6791 if (intel_crtc->config.dither)
6792 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6793
6794 I915_WRITE(PIPEMISC(pipe), val);
6795 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03006796}
6797
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006798static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006799 intel_clock_t *clock,
6800 bool *has_reduced_clock,
6801 intel_clock_t *reduced_clock)
6802{
6803 struct drm_device *dev = crtc->dev;
6804 struct drm_i915_private *dev_priv = dev->dev_private;
6805 struct intel_encoder *intel_encoder;
6806 int refclk;
6807 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02006808 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006809
6810 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6811 switch (intel_encoder->type) {
6812 case INTEL_OUTPUT_LVDS:
6813 is_lvds = true;
6814 break;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006815 }
6816 }
6817
6818 refclk = ironlake_get_refclk(crtc);
6819
6820 /*
6821 * Returns a set of divisors for the desired target clock with the given
6822 * refclk, or FALSE. The returned values represent the clock equation:
6823 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
6824 */
6825 limit = intel_limit(crtc, refclk);
Daniel Vetterff9a6752013-06-01 17:16:21 +02006826 ret = dev_priv->display.find_dpll(limit, crtc,
6827 to_intel_crtc(crtc)->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02006828 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006829 if (!ret)
6830 return false;
6831
6832 if (is_lvds && dev_priv->lvds_downclock_avail) {
6833 /*
6834 * Ensure we match the reduced clock's P to the target clock.
6835 * If the clocks don't match, we can't switch the display clock
6836 * by using the FP0/FP1. In such case we will disable the LVDS
6837 * downclock feature.
6838 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02006839 *has_reduced_clock =
6840 dev_priv->display.find_dpll(limit, crtc,
6841 dev_priv->lvds_downclock,
6842 refclk, clock,
6843 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006844 }
6845
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006846 return true;
6847}
6848
Paulo Zanonid4b19312012-11-29 11:29:32 -02006849int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
6850{
6851 /*
6852 * Account for spread spectrum to avoid
6853 * oversubscribing the link. Max center spread
6854 * is 2.5%; use 5% for safety's sake.
6855 */
6856 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02006857 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02006858}
6859
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006860static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02006861{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006862 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006863}
6864
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006865static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006866 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006867 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006868{
6869 struct drm_crtc *crtc = &intel_crtc->base;
6870 struct drm_device *dev = crtc->dev;
6871 struct drm_i915_private *dev_priv = dev->dev_private;
6872 struct intel_encoder *intel_encoder;
6873 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006874 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02006875 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006876
6877 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
6878 switch (intel_encoder->type) {
6879 case INTEL_OUTPUT_LVDS:
6880 is_lvds = true;
6881 break;
6882 case INTEL_OUTPUT_SDVO:
6883 case INTEL_OUTPUT_HDMI:
6884 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006885 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006886 }
6887
6888 num_connectors++;
6889 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006890
Chris Wilsonc1858122010-12-03 21:35:48 +00006891 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07006892 factor = 21;
6893 if (is_lvds) {
6894 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006895 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02006896 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07006897 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02006898 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07006899 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00006900
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006901 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02006902 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00006903
Daniel Vetter9a7c7892013-04-04 22:20:34 +02006904 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
6905 *fp2 |= FP_CB_TUNE;
6906
Chris Wilson5eddb702010-09-11 13:48:45 +01006907 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006908
Eric Anholta07d6782011-03-30 13:01:08 -07006909 if (is_lvds)
6910 dpll |= DPLLB_MODE_LVDS;
6911 else
6912 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006913
Daniel Vetteref1b4602013-06-01 17:17:04 +02006914 dpll |= (intel_crtc->config.pixel_multiplier - 1)
6915 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006916
6917 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006918 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02006919 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006920 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08006921
Eric Anholta07d6782011-03-30 13:01:08 -07006922 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006923 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006924 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006925 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07006926
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006927 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07006928 case 5:
6929 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6930 break;
6931 case 7:
6932 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6933 break;
6934 case 10:
6935 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6936 break;
6937 case 14:
6938 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6939 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006940 }
6941
Daniel Vetterb4c09f32013-04-30 14:01:42 +02006942 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006943 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08006944 else
6945 dpll |= PLL_REF_INPUT_DREFCLK;
6946
Daniel Vetter959e16d2013-06-05 13:34:21 +02006947 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03006948}
6949
Jesse Barnes79e53942008-11-07 14:24:08 -08006950static int ironlake_crtc_mode_set(struct drm_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08006951 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006952 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006953{
6954 struct drm_device *dev = crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006955 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006956 int num_connectors = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006957 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006958 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03006959 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01006960 bool is_lvds = false;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03006961 struct intel_encoder *encoder;
Daniel Vettere2b78262013-06-07 23:10:03 +02006962 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08006963
6964 for_each_encoder_on_crtc(dev, crtc, encoder) {
6965 switch (encoder->type) {
6966 case INTEL_OUTPUT_LVDS:
6967 is_lvds = true;
6968 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006969 }
6970
6971 num_connectors++;
6972 }
6973
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006974 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
6975 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
6976
Daniel Vetterff9a6752013-06-01 17:16:21 +02006977 ok = ironlake_compute_clocks(crtc, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03006978 &has_reduced_clock, &reduced_clock);
Daniel Vetteree9300b2013-06-03 22:40:22 +02006979 if (!ok && !intel_crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006980 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6981 return -EINVAL;
6982 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01006983 /* Compat-code for transition, will disappear. */
6984 if (!intel_crtc->config.clock_set) {
6985 intel_crtc->config.dpll.n = clock.n;
6986 intel_crtc->config.dpll.m1 = clock.m1;
6987 intel_crtc->config.dpll.m2 = clock.m2;
6988 intel_crtc->config.dpll.p1 = clock.p1;
6989 intel_crtc->config.dpll.p2 = clock.p2;
6990 }
Jesse Barnes79e53942008-11-07 14:24:08 -08006991
Paulo Zanoni5dc52982012-10-05 12:05:56 -03006992 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Daniel Vetter8b470472013-03-28 10:41:59 +01006993 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006994 fp = i9xx_dpll_compute_fp(&intel_crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006995 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006996 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006997
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006998 dpll = ironlake_compute_dpll(intel_crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02006999 &fp, &reduced_clock,
7000 has_reduced_clock ? &fp2 : NULL);
7001
Daniel Vetter959e16d2013-06-05 13:34:21 +02007002 intel_crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007003 intel_crtc->config.dpll_hw_state.fp0 = fp;
7004 if (has_reduced_clock)
7005 intel_crtc->config.dpll_hw_state.fp1 = fp2;
7006 else
7007 intel_crtc->config.dpll_hw_state.fp1 = fp;
7008
Daniel Vetterb89a1d32013-06-05 13:34:24 +02007009 pll = intel_get_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007010 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007011 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Daniel Vetter29407aa2014-04-24 23:55:08 +02007012 pipe_name(intel_crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007013 return -EINVAL;
7014 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007015 } else
Daniel Vettere72f9fb2013-06-05 13:34:06 +02007016 intel_put_shared_dpll(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007017
Jani Nikulad330a952014-01-21 11:24:25 +02007018 if (is_lvds && has_reduced_clock && i915.powersave)
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007019 intel_crtc->lowfreq_avail = true;
7020 else
7021 intel_crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007022
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007023 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007024}
7025
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007026static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7027 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007028{
7029 struct drm_device *dev = crtc->base.dev;
7030 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007031 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007032
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007033 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7034 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7035 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7036 & ~TU_SIZE_MASK;
7037 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7038 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7039 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7040}
7041
7042static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7043 enum transcoder transcoder,
7044 struct intel_link_m_n *m_n)
7045{
7046 struct drm_device *dev = crtc->base.dev;
7047 struct drm_i915_private *dev_priv = dev->dev_private;
7048 enum pipe pipe = crtc->pipe;
7049
7050 if (INTEL_INFO(dev)->gen >= 5) {
7051 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7052 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7053 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7054 & ~TU_SIZE_MASK;
7055 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7056 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7057 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7058 } else {
7059 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7060 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7061 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7062 & ~TU_SIZE_MASK;
7063 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7064 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7065 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7066 }
7067}
7068
7069void intel_dp_get_m_n(struct intel_crtc *crtc,
7070 struct intel_crtc_config *pipe_config)
7071{
7072 if (crtc->config.has_pch_encoder)
7073 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7074 else
7075 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7076 &pipe_config->dp_m_n);
7077}
7078
Daniel Vetter72419202013-04-04 13:28:53 +02007079static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7080 struct intel_crtc_config *pipe_config)
7081{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007082 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
7083 &pipe_config->fdi_m_n);
Daniel Vetter72419202013-04-04 13:28:53 +02007084}
7085
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007086static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7087 struct intel_crtc_config *pipe_config)
7088{
7089 struct drm_device *dev = crtc->base.dev;
7090 struct drm_i915_private *dev_priv = dev->dev_private;
7091 uint32_t tmp;
7092
7093 tmp = I915_READ(PF_CTL(crtc->pipe));
7094
7095 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007096 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007097 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7098 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007099
7100 /* We currently do not free assignements of panel fitters on
7101 * ivb/hsw (since we don't use the higher upscaling modes which
7102 * differentiates them) so just WARN about this case for now. */
7103 if (IS_GEN7(dev)) {
7104 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7105 PF_PIPE_SEL_IVB(crtc->pipe));
7106 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007107 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007108}
7109
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007110static void ironlake_get_plane_config(struct intel_crtc *crtc,
7111 struct intel_plane_config *plane_config)
7112{
7113 struct drm_device *dev = crtc->base.dev;
7114 struct drm_i915_private *dev_priv = dev->dev_private;
7115 u32 val, base, offset;
7116 int pipe = crtc->pipe, plane = crtc->plane;
7117 int fourcc, pixel_format;
7118 int aligned_height;
7119
Dave Airlie66e514c2014-04-03 07:51:54 +10007120 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7121 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007122 DRM_DEBUG_KMS("failed to alloc fb\n");
7123 return;
7124 }
7125
7126 val = I915_READ(DSPCNTR(plane));
7127
7128 if (INTEL_INFO(dev)->gen >= 4)
7129 if (val & DISPPLANE_TILED)
7130 plane_config->tiled = true;
7131
7132 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7133 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007134 crtc->base.primary->fb->pixel_format = fourcc;
7135 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007136 drm_format_plane_cpp(fourcc, 0) * 8;
7137
7138 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7139 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7140 offset = I915_READ(DSPOFFSET(plane));
7141 } else {
7142 if (plane_config->tiled)
7143 offset = I915_READ(DSPTILEOFF(plane));
7144 else
7145 offset = I915_READ(DSPLINOFF(plane));
7146 }
7147 plane_config->base = base;
7148
7149 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007150 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7151 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007152
7153 val = I915_READ(DSPSTRIDE(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007154 crtc->base.primary->fb->pitches[0] = val & 0xffffff80;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007155
Dave Airlie66e514c2014-04-03 07:51:54 +10007156 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007157 plane_config->tiled);
7158
Dave Airlie66e514c2014-04-03 07:51:54 +10007159 plane_config->size = ALIGN(crtc->base.primary->fb->pitches[0] *
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007160 aligned_height, PAGE_SIZE);
7161
7162 DRM_DEBUG_KMS("pipe/plane %d/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
Dave Airlie66e514c2014-04-03 07:51:54 +10007163 pipe, plane, crtc->base.primary->fb->width,
7164 crtc->base.primary->fb->height,
7165 crtc->base.primary->fb->bits_per_pixel, base,
7166 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007167 plane_config->size);
7168}
7169
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007170static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7171 struct intel_crtc_config *pipe_config)
7172{
7173 struct drm_device *dev = crtc->base.dev;
7174 struct drm_i915_private *dev_priv = dev->dev_private;
7175 uint32_t tmp;
7176
Daniel Vettere143a212013-07-04 12:01:15 +02007177 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007178 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007179
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007180 tmp = I915_READ(PIPECONF(crtc->pipe));
7181 if (!(tmp & PIPECONF_ENABLE))
7182 return false;
7183
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007184 switch (tmp & PIPECONF_BPC_MASK) {
7185 case PIPECONF_6BPC:
7186 pipe_config->pipe_bpp = 18;
7187 break;
7188 case PIPECONF_8BPC:
7189 pipe_config->pipe_bpp = 24;
7190 break;
7191 case PIPECONF_10BPC:
7192 pipe_config->pipe_bpp = 30;
7193 break;
7194 case PIPECONF_12BPC:
7195 pipe_config->pipe_bpp = 36;
7196 break;
7197 default:
7198 break;
7199 }
7200
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007201 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7202 pipe_config->limited_color_range = true;
7203
Daniel Vetterab9412b2013-05-03 11:49:46 +02007204 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007205 struct intel_shared_dpll *pll;
7206
Daniel Vetter88adfff2013-03-28 10:42:01 +01007207 pipe_config->has_pch_encoder = true;
7208
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007209 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7210 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7211 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007212
7213 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007214
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007215 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007216 pipe_config->shared_dpll =
7217 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007218 } else {
7219 tmp = I915_READ(PCH_DPLL_SEL);
7220 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7221 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7222 else
7223 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7224 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007225
7226 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7227
7228 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7229 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007230
7231 tmp = pipe_config->dpll_hw_state.dpll;
7232 pipe_config->pixel_multiplier =
7233 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7234 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007235
7236 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007237 } else {
7238 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007239 }
7240
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007241 intel_get_pipe_timings(crtc, pipe_config);
7242
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007243 ironlake_get_pfit_config(crtc, pipe_config);
7244
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007245 return true;
7246}
7247
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007248static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7249{
7250 struct drm_device *dev = dev_priv->dev;
7251 struct intel_ddi_plls *plls = &dev_priv->ddi_plls;
7252 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007253
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007254 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007255 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007256 pipe_name(crtc->pipe));
7257
7258 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
7259 WARN(plls->spll_refcount, "SPLL enabled\n");
7260 WARN(plls->wrpll1_refcount, "WRPLL1 enabled\n");
7261 WARN(plls->wrpll2_refcount, "WRPLL2 enabled\n");
7262 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7263 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7264 "CPU PWM1 enabled\n");
7265 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7266 "CPU PWM2 enabled\n");
7267 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7268 "PCH PWM1 enabled\n");
7269 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7270 "Utility pin enabled\n");
7271 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7272
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007273 /*
7274 * In theory we can still leave IRQs enabled, as long as only the HPD
7275 * interrupts remain enabled. We used to check for that, but since it's
7276 * gen-specific and since we only disable LCPLL after we fully disable
7277 * the interrupts, the check below should be enough.
7278 */
7279 WARN(!dev_priv->pm.irqs_disabled, "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007280}
7281
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007282static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7283{
7284 struct drm_device *dev = dev_priv->dev;
7285
7286 if (IS_HASWELL(dev)) {
7287 mutex_lock(&dev_priv->rps.hw_lock);
7288 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7289 val))
7290 DRM_ERROR("Failed to disable D_COMP\n");
7291 mutex_unlock(&dev_priv->rps.hw_lock);
7292 } else {
7293 I915_WRITE(D_COMP, val);
7294 }
7295 POSTING_READ(D_COMP);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007296}
7297
7298/*
7299 * This function implements pieces of two sequences from BSpec:
7300 * - Sequence for display software to disable LCPLL
7301 * - Sequence for display software to allow package C8+
7302 * The steps implemented here are just the steps that actually touch the LCPLL
7303 * register. Callers should take care of disabling all the display engine
7304 * functions, doing the mode unset, fixing interrupts, etc.
7305 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007306static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7307 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007308{
7309 uint32_t val;
7310
7311 assert_can_disable_lcpll(dev_priv);
7312
7313 val = I915_READ(LCPLL_CTL);
7314
7315 if (switch_to_fclk) {
7316 val |= LCPLL_CD_SOURCE_FCLK;
7317 I915_WRITE(LCPLL_CTL, val);
7318
7319 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7320 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7321 DRM_ERROR("Switching to FCLK failed\n");
7322
7323 val = I915_READ(LCPLL_CTL);
7324 }
7325
7326 val |= LCPLL_PLL_DISABLE;
7327 I915_WRITE(LCPLL_CTL, val);
7328 POSTING_READ(LCPLL_CTL);
7329
7330 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7331 DRM_ERROR("LCPLL still locked\n");
7332
7333 val = I915_READ(D_COMP);
7334 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007335 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007336 ndelay(100);
7337
7338 if (wait_for((I915_READ(D_COMP) & D_COMP_RCOMP_IN_PROGRESS) == 0, 1))
7339 DRM_ERROR("D_COMP RCOMP still in progress\n");
7340
7341 if (allow_power_down) {
7342 val = I915_READ(LCPLL_CTL);
7343 val |= LCPLL_POWER_DOWN_ALLOW;
7344 I915_WRITE(LCPLL_CTL, val);
7345 POSTING_READ(LCPLL_CTL);
7346 }
7347}
7348
7349/*
7350 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7351 * source.
7352 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007353static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007354{
7355 uint32_t val;
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007356 unsigned long irqflags;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007357
7358 val = I915_READ(LCPLL_CTL);
7359
7360 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7361 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7362 return;
7363
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007364 /*
7365 * Make sure we're not on PC8 state before disabling PC8, otherwise
7366 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7367 *
7368 * The other problem is that hsw_restore_lcpll() is called as part of
7369 * the runtime PM resume sequence, so we can't just call
7370 * gen6_gt_force_wake_get() because that function calls
7371 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7372 * while we are on the resume sequence. So to solve this problem we have
7373 * to call special forcewake code that doesn't touch runtime PM and
7374 * doesn't enable the forcewake delayed work.
7375 */
7376 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7377 if (dev_priv->uncore.forcewake_count++ == 0)
7378 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
7379 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007380
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007381 if (val & LCPLL_POWER_DOWN_ALLOW) {
7382 val &= ~LCPLL_POWER_DOWN_ALLOW;
7383 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007384 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007385 }
7386
7387 val = I915_READ(D_COMP);
7388 val |= D_COMP_COMP_FORCE;
7389 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007390 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007391
7392 val = I915_READ(LCPLL_CTL);
7393 val &= ~LCPLL_PLL_DISABLE;
7394 I915_WRITE(LCPLL_CTL, val);
7395
7396 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7397 DRM_ERROR("LCPLL not locked yet\n");
7398
7399 if (val & LCPLL_CD_SOURCE_FCLK) {
7400 val = I915_READ(LCPLL_CTL);
7401 val &= ~LCPLL_CD_SOURCE_FCLK;
7402 I915_WRITE(LCPLL_CTL, val);
7403
7404 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7405 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7406 DRM_ERROR("Switching back to LCPLL failed\n");
7407 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007408
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007409 /* See the big comment above. */
7410 spin_lock_irqsave(&dev_priv->uncore.lock, irqflags);
7411 if (--dev_priv->uncore.forcewake_count == 0)
7412 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
7413 spin_unlock_irqrestore(&dev_priv->uncore.lock, irqflags);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007414}
7415
Paulo Zanoni765dab672014-03-07 20:08:18 -03007416/*
7417 * Package states C8 and deeper are really deep PC states that can only be
7418 * reached when all the devices on the system allow it, so even if the graphics
7419 * device allows PC8+, it doesn't mean the system will actually get to these
7420 * states. Our driver only allows PC8+ when going into runtime PM.
7421 *
7422 * The requirements for PC8+ are that all the outputs are disabled, the power
7423 * well is disabled and most interrupts are disabled, and these are also
7424 * requirements for runtime PM. When these conditions are met, we manually do
7425 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7426 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7427 * hang the machine.
7428 *
7429 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7430 * the state of some registers, so when we come back from PC8+ we need to
7431 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7432 * need to take care of the registers kept by RC6. Notice that this happens even
7433 * if we don't put the device in PCI D3 state (which is what currently happens
7434 * because of the runtime PM support).
7435 *
7436 * For more, read "Display Sequences for Package C8" on the hardware
7437 * documentation.
7438 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007439void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007440{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007441 struct drm_device *dev = dev_priv->dev;
7442 uint32_t val;
7443
Paulo Zanonic67a4702013-08-19 13:18:09 -03007444 DRM_DEBUG_KMS("Enabling package C8+\n");
7445
Paulo Zanonic67a4702013-08-19 13:18:09 -03007446 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7447 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7448 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7449 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7450 }
7451
7452 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007453 hsw_disable_lcpll(dev_priv, true, true);
7454}
7455
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007456void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007457{
7458 struct drm_device *dev = dev_priv->dev;
7459 uint32_t val;
7460
Paulo Zanonic67a4702013-08-19 13:18:09 -03007461 DRM_DEBUG_KMS("Disabling package C8+\n");
7462
7463 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007464 lpt_init_pch_refclk(dev);
7465
7466 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7467 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7468 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7469 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7470 }
7471
7472 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007473}
7474
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007475static void snb_modeset_global_resources(struct drm_device *dev)
7476{
7477 modeset_update_crtc_power_domains(dev);
7478}
7479
Imre Deak4f074122013-10-16 17:25:51 +03007480static void haswell_modeset_global_resources(struct drm_device *dev)
7481{
Paulo Zanonida723562013-12-19 11:54:51 -02007482 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007483}
7484
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007485static int haswell_crtc_mode_set(struct drm_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007486 int x, int y,
7487 struct drm_framebuffer *fb)
7488{
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007489 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007490
Paulo Zanoni566b7342013-11-25 15:27:08 -02007491 if (!intel_ddi_pll_select(intel_crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007492 return -EINVAL;
Paulo Zanoni566b7342013-11-25 15:27:08 -02007493 intel_ddi_pll_enable(intel_crtc);
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007494
Daniel Vetter644cef32014-04-24 23:55:07 +02007495 intel_crtc->lowfreq_avail = false;
7496
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007497 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007498}
7499
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007500static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7501 struct intel_crtc_config *pipe_config)
7502{
7503 struct drm_device *dev = crtc->base.dev;
7504 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007505 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007506 uint32_t tmp;
7507
Imre Deakb5482bd2014-03-05 16:20:55 +02007508 if (!intel_display_power_enabled(dev_priv,
7509 POWER_DOMAIN_PIPE(crtc->pipe)))
7510 return false;
7511
Daniel Vettere143a212013-07-04 12:01:15 +02007512 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007513 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7514
Daniel Vettereccb1402013-05-22 00:50:22 +02007515 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7516 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7517 enum pipe trans_edp_pipe;
7518 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7519 default:
7520 WARN(1, "unknown pipe linked to edp transcoder\n");
7521 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7522 case TRANS_DDI_EDP_INPUT_A_ON:
7523 trans_edp_pipe = PIPE_A;
7524 break;
7525 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7526 trans_edp_pipe = PIPE_B;
7527 break;
7528 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7529 trans_edp_pipe = PIPE_C;
7530 break;
7531 }
7532
7533 if (trans_edp_pipe == crtc->pipe)
7534 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7535 }
7536
Imre Deakda7e29b2014-02-18 00:02:02 +02007537 if (!intel_display_power_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007538 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007539 return false;
7540
Daniel Vettereccb1402013-05-22 00:50:22 +02007541 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007542 if (!(tmp & PIPECONF_ENABLE))
7543 return false;
7544
Daniel Vetter88adfff2013-03-28 10:42:01 +01007545 /*
Paulo Zanonif196e6b2013-04-18 16:35:41 -03007546 * Haswell has only FDI/PCH transcoder A. It is which is connected to
Daniel Vetter88adfff2013-03-28 10:42:01 +01007547 * DDI E. So just check whether this pipe is wired to DDI E and whether
7548 * the PCH transcoder is on.
7549 */
Daniel Vettereccb1402013-05-22 00:50:22 +02007550 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
Daniel Vetter88adfff2013-03-28 10:42:01 +01007551 if ((tmp & TRANS_DDI_PORT_MASK) == TRANS_DDI_SELECT_PORT(PORT_E) &&
Daniel Vetterab9412b2013-05-03 11:49:46 +02007552 I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter88adfff2013-03-28 10:42:01 +01007553 pipe_config->has_pch_encoder = true;
7554
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007555 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7556 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7557 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007558
7559 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007560 }
7561
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007562 intel_get_pipe_timings(crtc, pipe_config);
7563
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007564 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Imre Deakda7e29b2014-02-18 00:02:02 +02007565 if (intel_display_power_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007566 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007567
Jesse Barnese59150d2014-01-07 13:30:45 -08007568 if (IS_HASWELL(dev))
7569 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7570 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007571
Daniel Vetter6c49f242013-06-06 12:45:25 +02007572 pipe_config->pixel_multiplier = 1;
7573
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007574 return true;
7575}
7576
Jani Nikula1a915102013-10-16 12:34:48 +03007577static struct {
7578 int clock;
7579 u32 config;
7580} hdmi_audio_clock[] = {
7581 { DIV_ROUND_UP(25200 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_25175 },
7582 { 25200, AUD_CONFIG_PIXEL_CLOCK_HDMI_25200 }, /* default per bspec */
7583 { 27000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27000 },
7584 { 27000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_27027 },
7585 { 54000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54000 },
7586 { 54000 * 1001 / 1000, AUD_CONFIG_PIXEL_CLOCK_HDMI_54054 },
7587 { DIV_ROUND_UP(74250 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_74176 },
7588 { 74250, AUD_CONFIG_PIXEL_CLOCK_HDMI_74250 },
7589 { DIV_ROUND_UP(148500 * 1000, 1001), AUD_CONFIG_PIXEL_CLOCK_HDMI_148352 },
7590 { 148500, AUD_CONFIG_PIXEL_CLOCK_HDMI_148500 },
7591};
7592
7593/* get AUD_CONFIG_PIXEL_CLOCK_HDMI_* value for mode */
7594static u32 audio_config_hdmi_pixel_clock(struct drm_display_mode *mode)
7595{
7596 int i;
7597
7598 for (i = 0; i < ARRAY_SIZE(hdmi_audio_clock); i++) {
7599 if (mode->clock == hdmi_audio_clock[i].clock)
7600 break;
7601 }
7602
7603 if (i == ARRAY_SIZE(hdmi_audio_clock)) {
7604 DRM_DEBUG_KMS("HDMI audio pixel clock setting for %d not found, falling back to defaults\n", mode->clock);
7605 i = 1;
7606 }
7607
7608 DRM_DEBUG_KMS("Configuring HDMI audio for pixel clock %d (0x%08x)\n",
7609 hdmi_audio_clock[i].clock,
7610 hdmi_audio_clock[i].config);
7611
7612 return hdmi_audio_clock[i].config;
7613}
7614
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007615static bool intel_eld_uptodate(struct drm_connector *connector,
7616 int reg_eldv, uint32_t bits_eldv,
7617 int reg_elda, uint32_t bits_elda,
7618 int reg_edid)
7619{
7620 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7621 uint8_t *eld = connector->eld;
7622 uint32_t i;
7623
7624 i = I915_READ(reg_eldv);
7625 i &= bits_eldv;
7626
7627 if (!eld[0])
7628 return !i;
7629
7630 if (!i)
7631 return false;
7632
7633 i = I915_READ(reg_elda);
7634 i &= ~bits_elda;
7635 I915_WRITE(reg_elda, i);
7636
7637 for (i = 0; i < eld[2]; i++)
7638 if (I915_READ(reg_edid) != *((uint32_t *)eld + i))
7639 return false;
7640
7641 return true;
7642}
7643
Wu Fengguange0dac652011-09-05 14:25:34 +08007644static void g4x_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007645 struct drm_crtc *crtc,
7646 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007647{
7648 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7649 uint8_t *eld = connector->eld;
7650 uint32_t eldv;
7651 uint32_t len;
7652 uint32_t i;
7653
7654 i = I915_READ(G4X_AUD_VID_DID);
7655
7656 if (i == INTEL_AUDIO_DEVBLC || i == INTEL_AUDIO_DEVCL)
7657 eldv = G4X_ELDV_DEVCL_DEVBLC;
7658 else
7659 eldv = G4X_ELDV_DEVCTG;
7660
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007661 if (intel_eld_uptodate(connector,
7662 G4X_AUD_CNTL_ST, eldv,
7663 G4X_AUD_CNTL_ST, G4X_ELD_ADDR,
7664 G4X_HDMIW_HDMIEDID))
7665 return;
7666
Wu Fengguange0dac652011-09-05 14:25:34 +08007667 i = I915_READ(G4X_AUD_CNTL_ST);
7668 i &= ~(eldv | G4X_ELD_ADDR);
7669 len = (i >> 9) & 0x1f; /* ELD buffer size */
7670 I915_WRITE(G4X_AUD_CNTL_ST, i);
7671
7672 if (!eld[0])
7673 return;
7674
7675 len = min_t(uint8_t, eld[2], len);
7676 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7677 for (i = 0; i < len; i++)
7678 I915_WRITE(G4X_HDMIW_HDMIEDID, *((uint32_t *)eld + i));
7679
7680 i = I915_READ(G4X_AUD_CNTL_ST);
7681 i |= eldv;
7682 I915_WRITE(G4X_AUD_CNTL_ST, i);
7683}
7684
Wang Xingchao83358c852012-08-16 22:43:37 +08007685static void haswell_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007686 struct drm_crtc *crtc,
7687 struct drm_display_mode *mode)
Wang Xingchao83358c852012-08-16 22:43:37 +08007688{
7689 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7690 uint8_t *eld = connector->eld;
Wang Xingchao83358c852012-08-16 22:43:37 +08007691 uint32_t eldv;
7692 uint32_t i;
7693 int len;
7694 int pipe = to_intel_crtc(crtc)->pipe;
7695 int tmp;
7696
7697 int hdmiw_hdmiedid = HSW_AUD_EDID_DATA(pipe);
7698 int aud_cntl_st = HSW_AUD_DIP_ELD_CTRL(pipe);
7699 int aud_config = HSW_AUD_CFG(pipe);
7700 int aud_cntrl_st2 = HSW_AUD_PIN_ELD_CP_VLD;
7701
Wang Xingchao83358c852012-08-16 22:43:37 +08007702 /* Audio output enable */
7703 DRM_DEBUG_DRIVER("HDMI audio: enable codec\n");
7704 tmp = I915_READ(aud_cntrl_st2);
7705 tmp |= (AUDIO_OUTPUT_ENABLE_A << (pipe * 4));
7706 I915_WRITE(aud_cntrl_st2, tmp);
Daniel Vetterc7905792014-04-16 16:56:09 +02007707 POSTING_READ(aud_cntrl_st2);
Wang Xingchao83358c852012-08-16 22:43:37 +08007708
Daniel Vetterc7905792014-04-16 16:56:09 +02007709 assert_pipe_disabled(dev_priv, to_intel_crtc(crtc)->pipe);
Wang Xingchao83358c852012-08-16 22:43:37 +08007710
7711 /* Set ELD valid state */
7712 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007713 DRM_DEBUG_DRIVER("HDMI audio: pin eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007714 tmp |= (AUDIO_ELD_VALID_A << (pipe * 4));
7715 I915_WRITE(aud_cntrl_st2, tmp);
7716 tmp = I915_READ(aud_cntrl_st2);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007717 DRM_DEBUG_DRIVER("HDMI audio: eld vld status=0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007718
7719 /* Enable HDMI mode */
7720 tmp = I915_READ(aud_config);
Takashi Iwai7e7cb342013-09-10 07:30:36 +02007721 DRM_DEBUG_DRIVER("HDMI audio: audio conf: 0x%08x\n", tmp);
Wang Xingchao83358c852012-08-16 22:43:37 +08007722 /* clear N_programing_enable and N_value_index */
7723 tmp &= ~(AUD_CONFIG_N_VALUE_INDEX | AUD_CONFIG_N_PROG_ENABLE);
7724 I915_WRITE(aud_config, tmp);
7725
7726 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
7727
7728 eldv = AUDIO_ELD_VALID_A << (pipe * 4);
7729
7730 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7731 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7732 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
7733 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007734 } else {
7735 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7736 }
Wang Xingchao83358c852012-08-16 22:43:37 +08007737
7738 if (intel_eld_uptodate(connector,
7739 aud_cntrl_st2, eldv,
7740 aud_cntl_st, IBX_ELD_ADDRESS,
7741 hdmiw_hdmiedid))
7742 return;
7743
7744 i = I915_READ(aud_cntrl_st2);
7745 i &= ~eldv;
7746 I915_WRITE(aud_cntrl_st2, i);
7747
7748 if (!eld[0])
7749 return;
7750
7751 i = I915_READ(aud_cntl_st);
7752 i &= ~IBX_ELD_ADDRESS;
7753 I915_WRITE(aud_cntl_st, i);
7754 i = (i >> 29) & DIP_PORT_SEL_MASK; /* DIP_Port_Select, 0x1 = PortB */
7755 DRM_DEBUG_DRIVER("port num:%d\n", i);
7756
7757 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7758 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7759 for (i = 0; i < len; i++)
7760 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7761
7762 i = I915_READ(aud_cntrl_st2);
7763 i |= eldv;
7764 I915_WRITE(aud_cntrl_st2, i);
7765
7766}
7767
Wu Fengguange0dac652011-09-05 14:25:34 +08007768static void ironlake_write_eld(struct drm_connector *connector,
Jani Nikula34427052013-10-16 12:34:47 +03007769 struct drm_crtc *crtc,
7770 struct drm_display_mode *mode)
Wu Fengguange0dac652011-09-05 14:25:34 +08007771{
7772 struct drm_i915_private *dev_priv = connector->dev->dev_private;
7773 uint8_t *eld = connector->eld;
7774 uint32_t eldv;
7775 uint32_t i;
7776 int len;
7777 int hdmiw_hdmiedid;
Wu Fengguangb6daa022012-01-06 14:41:31 -06007778 int aud_config;
Wu Fengguange0dac652011-09-05 14:25:34 +08007779 int aud_cntl_st;
7780 int aud_cntrl_st2;
Wang Xingchao9b138a82012-08-09 16:52:18 +08007781 int pipe = to_intel_crtc(crtc)->pipe;
Wu Fengguange0dac652011-09-05 14:25:34 +08007782
Wu Fengguangb3f33cb2011-12-09 20:42:17 +08007783 if (HAS_PCH_IBX(connector->dev)) {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007784 hdmiw_hdmiedid = IBX_HDMIW_HDMIEDID(pipe);
7785 aud_config = IBX_AUD_CFG(pipe);
7786 aud_cntl_st = IBX_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007787 aud_cntrl_st2 = IBX_AUD_CNTL_ST2;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007788 } else if (IS_VALLEYVIEW(connector->dev)) {
7789 hdmiw_hdmiedid = VLV_HDMIW_HDMIEDID(pipe);
7790 aud_config = VLV_AUD_CFG(pipe);
7791 aud_cntl_st = VLV_AUD_CNTL_ST(pipe);
7792 aud_cntrl_st2 = VLV_AUD_CNTL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007793 } else {
Wang Xingchao9b138a82012-08-09 16:52:18 +08007794 hdmiw_hdmiedid = CPT_HDMIW_HDMIEDID(pipe);
7795 aud_config = CPT_AUD_CFG(pipe);
7796 aud_cntl_st = CPT_AUD_CNTL_ST(pipe);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007797 aud_cntrl_st2 = CPT_AUD_CNTRL_ST2;
Wu Fengguange0dac652011-09-05 14:25:34 +08007798 }
7799
Wang Xingchao9b138a82012-08-09 16:52:18 +08007800 DRM_DEBUG_DRIVER("ELD on pipe %c\n", pipe_name(pipe));
Wu Fengguange0dac652011-09-05 14:25:34 +08007801
Mengdong Lin9ca2fe72013-11-01 00:17:03 -04007802 if (IS_VALLEYVIEW(connector->dev)) {
7803 struct intel_encoder *intel_encoder;
7804 struct intel_digital_port *intel_dig_port;
7805
7806 intel_encoder = intel_attached_encoder(connector);
7807 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
7808 i = intel_dig_port->port;
7809 } else {
7810 i = I915_READ(aud_cntl_st);
7811 i = (i >> 29) & DIP_PORT_SEL_MASK;
7812 /* DIP_Port_Select, 0x1 = PortB */
7813 }
7814
Wu Fengguange0dac652011-09-05 14:25:34 +08007815 if (!i) {
7816 DRM_DEBUG_DRIVER("Audio directed to unknown port\n");
7817 /* operate blindly on all ports */
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007818 eldv = IBX_ELD_VALIDB;
7819 eldv |= IBX_ELD_VALIDB << 4;
7820 eldv |= IBX_ELD_VALIDB << 8;
Wu Fengguange0dac652011-09-05 14:25:34 +08007821 } else {
Ville Syrjälä2582a852013-04-17 17:48:47 +03007822 DRM_DEBUG_DRIVER("ELD on port %c\n", port_name(i));
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007823 eldv = IBX_ELD_VALIDB << ((i - 1) * 4);
Wu Fengguange0dac652011-09-05 14:25:34 +08007824 }
7825
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007826 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
7827 DRM_DEBUG_DRIVER("ELD: DisplayPort detected\n");
7828 eld[5] |= (1 << 2); /* Conn_Type, 0x1 = DisplayPort */
Wu Fengguangb6daa022012-01-06 14:41:31 -06007829 I915_WRITE(aud_config, AUD_CONFIG_N_VALUE_INDEX); /* 0x1 = DP */
Jani Nikula1a915102013-10-16 12:34:48 +03007830 } else {
7831 I915_WRITE(aud_config, audio_config_hdmi_pixel_clock(mode));
7832 }
Wu Fengguang3a9627f2011-12-09 20:42:19 +08007833
7834 if (intel_eld_uptodate(connector,
7835 aud_cntrl_st2, eldv,
7836 aud_cntl_st, IBX_ELD_ADDRESS,
7837 hdmiw_hdmiedid))
7838 return;
7839
Wu Fengguange0dac652011-09-05 14:25:34 +08007840 i = I915_READ(aud_cntrl_st2);
7841 i &= ~eldv;
7842 I915_WRITE(aud_cntrl_st2, i);
7843
7844 if (!eld[0])
7845 return;
7846
Wu Fengguange0dac652011-09-05 14:25:34 +08007847 i = I915_READ(aud_cntl_st);
Wu Fengguang1202b4c62011-12-09 20:42:18 +08007848 i &= ~IBX_ELD_ADDRESS;
Wu Fengguange0dac652011-09-05 14:25:34 +08007849 I915_WRITE(aud_cntl_st, i);
7850
7851 len = min_t(uint8_t, eld[2], 21); /* 84 bytes of hw ELD buffer */
7852 DRM_DEBUG_DRIVER("ELD size %d\n", len);
7853 for (i = 0; i < len; i++)
7854 I915_WRITE(hdmiw_hdmiedid, *((uint32_t *)eld + i));
7855
7856 i = I915_READ(aud_cntrl_st2);
7857 i |= eldv;
7858 I915_WRITE(aud_cntrl_st2, i);
7859}
7860
7861void intel_write_eld(struct drm_encoder *encoder,
7862 struct drm_display_mode *mode)
7863{
7864 struct drm_crtc *crtc = encoder->crtc;
7865 struct drm_connector *connector;
7866 struct drm_device *dev = encoder->dev;
7867 struct drm_i915_private *dev_priv = dev->dev_private;
7868
7869 connector = drm_select_eld(encoder, mode);
7870 if (!connector)
7871 return;
7872
7873 DRM_DEBUG_DRIVER("ELD on [CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7874 connector->base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03007875 connector->name,
Wu Fengguange0dac652011-09-05 14:25:34 +08007876 connector->encoder->base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +03007877 connector->encoder->name);
Wu Fengguange0dac652011-09-05 14:25:34 +08007878
7879 connector->eld[6] = drm_av_sync_delay(connector, mode) / 2;
7880
7881 if (dev_priv->display.write_eld)
Jani Nikula34427052013-10-16 12:34:47 +03007882 dev_priv->display.write_eld(connector, crtc, mode);
Wu Fengguange0dac652011-09-05 14:25:34 +08007883}
7884
Chris Wilson560b85b2010-08-07 11:01:38 +01007885static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
7886{
7887 struct drm_device *dev = crtc->dev;
7888 struct drm_i915_private *dev_priv = dev->dev_private;
7889 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7890 bool visible = base != 0;
7891 u32 cntl;
7892
7893 if (intel_crtc->cursor_visible == visible)
7894 return;
7895
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007896 cntl = I915_READ(_CURACNTR);
Chris Wilson560b85b2010-08-07 11:01:38 +01007897 if (visible) {
7898 /* On these chipsets we can only modify the base whilst
7899 * the cursor is disabled.
7900 */
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007901 I915_WRITE(_CURABASE, base);
Chris Wilson560b85b2010-08-07 11:01:38 +01007902
7903 cntl &= ~(CURSOR_FORMAT_MASK);
7904 /* XXX width must be 64, stride 256 => 0x00 << 28 */
7905 cntl |= CURSOR_ENABLE |
7906 CURSOR_GAMMA_ENABLE |
7907 CURSOR_FORMAT_ARGB;
7908 } else
7909 cntl &= ~(CURSOR_ENABLE | CURSOR_GAMMA_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007910 I915_WRITE(_CURACNTR, cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01007911
7912 intel_crtc->cursor_visible = visible;
7913}
7914
7915static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
7916{
7917 struct drm_device *dev = crtc->dev;
7918 struct drm_i915_private *dev_priv = dev->dev_private;
7919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7920 int pipe = intel_crtc->pipe;
7921 bool visible = base != 0;
7922
7923 if (intel_crtc->cursor_visible != visible) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307924 int16_t width = intel_crtc->cursor_width;
Jesse Barnes548f2452011-02-17 10:40:53 -08007925 uint32_t cntl = I915_READ(CURCNTR(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01007926 if (base) {
7927 cntl &= ~(CURSOR_MODE | MCURSOR_PIPE_SELECT);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307928 cntl |= MCURSOR_GAMMA_ENABLE;
7929
7930 switch (width) {
7931 case 64:
7932 cntl |= CURSOR_MODE_64_ARGB_AX;
7933 break;
7934 case 128:
7935 cntl |= CURSOR_MODE_128_ARGB_AX;
7936 break;
7937 case 256:
7938 cntl |= CURSOR_MODE_256_ARGB_AX;
7939 break;
7940 default:
7941 WARN_ON(1);
7942 return;
7943 }
Chris Wilson560b85b2010-08-07 11:01:38 +01007944 cntl |= pipe << 28; /* Connect to correct pipe */
7945 } else {
7946 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7947 cntl |= CURSOR_MODE_DISABLE;
7948 }
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007949 I915_WRITE(CURCNTR(pipe), cntl);
Chris Wilson560b85b2010-08-07 11:01:38 +01007950
7951 intel_crtc->cursor_visible = visible;
7952 }
7953 /* and commit changes on next vblank */
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007954 POSTING_READ(CURCNTR(pipe));
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08007955 I915_WRITE(CURBASE(pipe), base);
Daniel Vetterb2ea8ef2013-11-04 08:13:45 +01007956 POSTING_READ(CURBASE(pipe));
Chris Wilson560b85b2010-08-07 11:01:38 +01007957}
7958
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007959static void ivb_update_cursor(struct drm_crtc *crtc, u32 base)
7960{
7961 struct drm_device *dev = crtc->dev;
7962 struct drm_i915_private *dev_priv = dev->dev_private;
7963 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7964 int pipe = intel_crtc->pipe;
7965 bool visible = base != 0;
7966
7967 if (intel_crtc->cursor_visible != visible) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307968 int16_t width = intel_crtc->cursor_width;
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03007969 uint32_t cntl = I915_READ(CURCNTR(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007970 if (base) {
7971 cntl &= ~CURSOR_MODE;
Sagar Kamble4726e0b2014-03-10 17:06:23 +05307972 cntl |= MCURSOR_GAMMA_ENABLE;
7973 switch (width) {
7974 case 64:
7975 cntl |= CURSOR_MODE_64_ARGB_AX;
7976 break;
7977 case 128:
7978 cntl |= CURSOR_MODE_128_ARGB_AX;
7979 break;
7980 case 256:
7981 cntl |= CURSOR_MODE_256_ARGB_AX;
7982 break;
7983 default:
7984 WARN_ON(1);
7985 return;
7986 }
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007987 } else {
7988 cntl &= ~(CURSOR_MODE | MCURSOR_GAMMA_ENABLE);
7989 cntl |= CURSOR_MODE_DISABLE;
7990 }
Ville Syrjälä6bbfa1c2013-11-02 21:07:39 -07007991 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007992 cntl |= CURSOR_PIPE_CSC_ENABLE;
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03007993 cntl &= ~CURSOR_TRICKLE_FEED_DISABLE;
7994 }
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03007995 I915_WRITE(CURCNTR(pipe), cntl);
Jesse Barnes65a21cd2011-10-12 11:10:21 -07007996
7997 intel_crtc->cursor_visible = visible;
7998 }
7999 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008000 POSTING_READ(CURCNTR(pipe));
8001 I915_WRITE(CURBASE(pipe), base);
8002 POSTING_READ(CURBASE(pipe));
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008003}
8004
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008005/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008006static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8007 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008008{
8009 struct drm_device *dev = crtc->dev;
8010 struct drm_i915_private *dev_priv = dev->dev_private;
8011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8012 int pipe = intel_crtc->pipe;
8013 int x = intel_crtc->cursor_x;
8014 int y = intel_crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008015 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008016 bool visible;
8017
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008018 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008019 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008020
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008021 if (x >= intel_crtc->config.pipe_src_w)
8022 base = 0;
8023
8024 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008025 base = 0;
8026
8027 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008028 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008029 base = 0;
8030
8031 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8032 x = -x;
8033 }
8034 pos |= x << CURSOR_X_SHIFT;
8035
8036 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008037 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008038 base = 0;
8039
8040 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8041 y = -y;
8042 }
8043 pos |= y << CURSOR_Y_SHIFT;
8044
8045 visible = base != 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008046 if (!visible && !intel_crtc->cursor_visible)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008047 return;
8048
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008049 I915_WRITE(CURPOS(pipe), pos);
8050
8051 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev) || IS_BROADWELL(dev))
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008052 ivb_update_cursor(crtc, base);
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008053 else if (IS_845G(dev) || IS_I865G(dev))
8054 i845_update_cursor(crtc, base);
8055 else
8056 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008057}
8058
Jesse Barnes79e53942008-11-07 14:24:08 -08008059static int intel_crtc_cursor_set(struct drm_crtc *crtc,
Chris Wilson05394f32010-11-08 19:18:58 +00008060 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -08008061 uint32_t handle,
8062 uint32_t width, uint32_t height)
8063{
8064 struct drm_device *dev = crtc->dev;
8065 struct drm_i915_private *dev_priv = dev->dev_private;
8066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson05394f32010-11-08 19:18:58 +00008067 struct drm_i915_gem_object *obj;
Chris Wilson64f962e2014-03-26 12:38:15 +00008068 unsigned old_width;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008069 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008070 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008071
Jesse Barnes79e53942008-11-07 14:24:08 -08008072 /* if we want to turn off the cursor ignore width and height */
8073 if (!handle) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008074 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008075 addr = 0;
Chris Wilson05394f32010-11-08 19:18:58 +00008076 obj = NULL;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008077 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008078 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008079 }
8080
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308081 /* Check for which cursor types we support */
8082 if (!((width == 64 && height == 64) ||
8083 (width == 128 && height == 128 && !IS_GEN2(dev)) ||
8084 (width == 256 && height == 256 && !IS_GEN2(dev)))) {
8085 DRM_DEBUG("Cursor dimension not supported\n");
Jesse Barnes79e53942008-11-07 14:24:08 -08008086 return -EINVAL;
8087 }
8088
Chris Wilson05394f32010-11-08 19:18:58 +00008089 obj = to_intel_bo(drm_gem_object_lookup(dev, file, handle));
Chris Wilsonc8725222011-02-19 11:31:06 +00008090 if (&obj->base == NULL)
Jesse Barnes79e53942008-11-07 14:24:08 -08008091 return -ENOENT;
8092
Chris Wilson05394f32010-11-08 19:18:58 +00008093 if (obj->base.size < width * height * 4) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008094 DRM_DEBUG_KMS("buffer is to small\n");
Dave Airlie34b8686e2009-01-15 14:03:07 +10008095 ret = -ENOMEM;
8096 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008097 }
8098
Dave Airlie71acb5e2008-12-30 20:31:46 +10008099 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008100 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008101 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008102 unsigned alignment;
8103
Chris Wilsond9e86c02010-11-10 16:40:20 +00008104 if (obj->tiling_mode) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008105 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008106 ret = -EINVAL;
8107 goto fail_locked;
8108 }
8109
Chris Wilson693db182013-03-05 14:52:39 +00008110 /* Note that the w/a also requires 2 PTE of padding following
8111 * the bo. We currently fill all unused PTE with the shadow
8112 * page and so we should always have valid PTE following the
8113 * cursor preventing the VT-d warning.
8114 */
8115 alignment = 0;
8116 if (need_vtd_wa(dev))
8117 alignment = 64*1024;
8118
8119 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008120 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008121 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008122 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008123 }
8124
Chris Wilsond9e86c02010-11-10 16:40:20 +00008125 ret = i915_gem_object_put_fence(obj);
8126 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008127 DRM_DEBUG_KMS("failed to release fence for cursor");
Chris Wilsond9e86c02010-11-10 16:40:20 +00008128 goto fail_unpin;
8129 }
8130
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008131 addr = i915_gem_obj_ggtt_offset(obj);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008132 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008133 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson05394f32010-11-08 19:18:58 +00008134 ret = i915_gem_attach_phys_object(dev, obj,
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008135 (intel_crtc->pipe == 0) ? I915_GEM_PHYS_CURSOR_0 : I915_GEM_PHYS_CURSOR_1,
8136 align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008137 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008138 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008139 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008140 }
Chris Wilson05394f32010-11-08 19:18:58 +00008141 addr = obj->phys_obj->handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008142 }
8143
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008144 if (IS_GEN2(dev))
Jesse Barnes14b603912009-05-20 16:47:08 -04008145 I915_WRITE(CURSIZE, (height << 12) | width);
8146
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008147 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008148 if (intel_crtc->cursor_bo) {
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008149 if (INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson05394f32010-11-08 19:18:58 +00008150 if (intel_crtc->cursor_bo != obj)
Dave Airlie71acb5e2008-12-30 20:31:46 +10008151 i915_gem_detach_phys_object(dev, intel_crtc->cursor_bo);
8152 } else
Chris Wilsoncc98b412013-08-09 12:25:09 +01008153 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Chris Wilson05394f32010-11-08 19:18:58 +00008154 drm_gem_object_unreference(&intel_crtc->cursor_bo->base);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008155 }
Jesse Barnes80824002009-09-10 15:28:06 -07008156
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008157 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008158
Chris Wilson64f962e2014-03-26 12:38:15 +00008159 old_width = intel_crtc->cursor_width;
8160
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008161 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008162 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008163 intel_crtc->cursor_width = width;
8164 intel_crtc->cursor_height = height;
8165
Chris Wilson64f962e2014-03-26 12:38:15 +00008166 if (intel_crtc->active) {
8167 if (old_width != width)
8168 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03008169 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Chris Wilson64f962e2014-03-26 12:38:15 +00008170 }
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008171
Jesse Barnes79e53942008-11-07 14:24:08 -08008172 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008173fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008174 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008175fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008176 mutex_unlock(&dev->struct_mutex);
Luca Barbieribc9025b2010-02-09 05:49:12 +00008177fail:
Chris Wilson05394f32010-11-08 19:18:58 +00008178 drm_gem_object_unreference_unlocked(&obj->base);
Dave Airlie34b8686e2009-01-15 14:03:07 +10008179 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008180}
8181
8182static int intel_crtc_cursor_move(struct drm_crtc *crtc, int x, int y)
8183{
Jesse Barnes79e53942008-11-07 14:24:08 -08008184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008185
Ville Syrjälä92e76c82013-10-21 19:01:58 +03008186 intel_crtc->cursor_x = clamp_t(int, x, SHRT_MIN, SHRT_MAX);
8187 intel_crtc->cursor_y = clamp_t(int, y, SHRT_MIN, SHRT_MAX);
Jesse Barnes652c3932009-08-17 13:31:43 -07008188
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03008189 if (intel_crtc->active)
8190 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -08008191
8192 return 0;
8193}
8194
Jesse Barnes79e53942008-11-07 14:24:08 -08008195static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008196 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008197{
James Simmons72034252010-08-03 01:33:19 +01008198 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008199 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008200
James Simmons72034252010-08-03 01:33:19 +01008201 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008202 intel_crtc->lut_r[i] = red[i] >> 8;
8203 intel_crtc->lut_g[i] = green[i] >> 8;
8204 intel_crtc->lut_b[i] = blue[i] >> 8;
8205 }
8206
8207 intel_crtc_load_lut(crtc);
8208}
8209
Jesse Barnes79e53942008-11-07 14:24:08 -08008210/* VESA 640x480x72Hz mode to set on the pipe */
8211static struct drm_display_mode load_detect_mode = {
8212 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8213 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8214};
8215
Daniel Vettera8bb6812014-02-10 18:00:39 +01008216struct drm_framebuffer *
8217__intel_framebuffer_create(struct drm_device *dev,
8218 struct drm_mode_fb_cmd2 *mode_cmd,
8219 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008220{
8221 struct intel_framebuffer *intel_fb;
8222 int ret;
8223
8224 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8225 if (!intel_fb) {
8226 drm_gem_object_unreference_unlocked(&obj->base);
8227 return ERR_PTR(-ENOMEM);
8228 }
8229
8230 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008231 if (ret)
8232 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008233
8234 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008235err:
8236 drm_gem_object_unreference_unlocked(&obj->base);
8237 kfree(intel_fb);
8238
8239 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008240}
8241
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008242static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008243intel_framebuffer_create(struct drm_device *dev,
8244 struct drm_mode_fb_cmd2 *mode_cmd,
8245 struct drm_i915_gem_object *obj)
8246{
8247 struct drm_framebuffer *fb;
8248 int ret;
8249
8250 ret = i915_mutex_lock_interruptible(dev);
8251 if (ret)
8252 return ERR_PTR(ret);
8253 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8254 mutex_unlock(&dev->struct_mutex);
8255
8256 return fb;
8257}
8258
Chris Wilsond2dff872011-04-19 08:36:26 +01008259static u32
8260intel_framebuffer_pitch_for_width(int width, int bpp)
8261{
8262 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8263 return ALIGN(pitch, 64);
8264}
8265
8266static u32
8267intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8268{
8269 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
8270 return ALIGN(pitch * mode->vdisplay, PAGE_SIZE);
8271}
8272
8273static struct drm_framebuffer *
8274intel_framebuffer_create_for_mode(struct drm_device *dev,
8275 struct drm_display_mode *mode,
8276 int depth, int bpp)
8277{
8278 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008279 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008280
8281 obj = i915_gem_alloc_object(dev,
8282 intel_framebuffer_size_for_mode(mode, bpp));
8283 if (obj == NULL)
8284 return ERR_PTR(-ENOMEM);
8285
8286 mode_cmd.width = mode->hdisplay;
8287 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008288 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8289 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008290 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008291
8292 return intel_framebuffer_create(dev, &mode_cmd, obj);
8293}
8294
8295static struct drm_framebuffer *
8296mode_fits_in_fbdev(struct drm_device *dev,
8297 struct drm_display_mode *mode)
8298{
Daniel Vetter4520f532013-10-09 09:18:51 +02008299#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008300 struct drm_i915_private *dev_priv = dev->dev_private;
8301 struct drm_i915_gem_object *obj;
8302 struct drm_framebuffer *fb;
8303
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008304 if (!dev_priv->fbdev)
8305 return NULL;
8306
8307 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008308 return NULL;
8309
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008310 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008311 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008312
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008313 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008314 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8315 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008316 return NULL;
8317
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008318 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008319 return NULL;
8320
8321 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008322#else
8323 return NULL;
8324#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008325}
8326
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008327bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008328 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008329 struct intel_load_detect_pipe *old,
8330 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008331{
8332 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008333 struct intel_encoder *intel_encoder =
8334 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008335 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008336 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008337 struct drm_crtc *crtc = NULL;
8338 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008339 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008340 struct drm_mode_config *config = &dev->mode_config;
8341 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008342
Chris Wilsond2dff872011-04-19 08:36:26 +01008343 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008344 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008345 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008346
Rob Clark51fd3712013-11-19 12:10:12 -05008347 drm_modeset_acquire_init(ctx, 0);
8348
8349retry:
8350 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8351 if (ret)
8352 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008353
Jesse Barnes79e53942008-11-07 14:24:08 -08008354 /*
8355 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008356 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008357 * - if the connector already has an assigned crtc, use it (but make
8358 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008359 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008360 * - try to find the first unused crtc that can drive this connector,
8361 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008362 */
8363
8364 /* See if we already have a CRTC for this connector */
8365 if (encoder->crtc) {
8366 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008367
Rob Clark51fd3712013-11-19 12:10:12 -05008368 ret = drm_modeset_lock(&crtc->mutex, ctx);
8369 if (ret)
8370 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008371
Daniel Vetter24218aa2012-08-12 19:27:11 +02008372 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008373 old->load_detect_temp = false;
8374
8375 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008376 if (connector->dpms != DRM_MODE_DPMS_ON)
8377 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008378
Chris Wilson71731882011-04-19 23:10:58 +01008379 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008380 }
8381
8382 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008383 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008384 i++;
8385 if (!(encoder->possible_crtcs & (1 << i)))
8386 continue;
8387 if (!possible_crtc->enabled) {
8388 crtc = possible_crtc;
8389 break;
8390 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008391 }
8392
8393 /*
8394 * If we didn't find an unused CRTC, don't use any.
8395 */
8396 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008397 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008398 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008399 }
8400
Rob Clark51fd3712013-11-19 12:10:12 -05008401 ret = drm_modeset_lock(&crtc->mutex, ctx);
8402 if (ret)
8403 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008404 intel_encoder->new_crtc = to_intel_crtc(crtc);
8405 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008406
8407 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008408 intel_crtc->new_enabled = true;
8409 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008410 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008411 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008412 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008413
Chris Wilson64927112011-04-20 07:25:26 +01008414 if (!mode)
8415 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008416
Chris Wilsond2dff872011-04-19 08:36:26 +01008417 /* We need a framebuffer large enough to accommodate all accesses
8418 * that the plane may generate whilst we perform load detection.
8419 * We can not rely on the fbcon either being present (we get called
8420 * during its initialisation to detect all boot displays, or it may
8421 * not even exist) or that it is large enough to satisfy the
8422 * requested mode.
8423 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008424 fb = mode_fits_in_fbdev(dev, mode);
8425 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008426 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008427 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8428 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008429 } else
8430 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008431 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008432 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008433 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008434 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008435
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008436 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008437 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008438 if (old->release_fb)
8439 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008440 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008441 }
Chris Wilson71731882011-04-19 23:10:58 +01008442
Jesse Barnes79e53942008-11-07 14:24:08 -08008443 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008444 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008445 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008446
8447 fail:
8448 intel_crtc->new_enabled = crtc->enabled;
8449 if (intel_crtc->new_enabled)
8450 intel_crtc->new_config = &intel_crtc->config;
8451 else
8452 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008453fail_unlock:
8454 if (ret == -EDEADLK) {
8455 drm_modeset_backoff(ctx);
8456 goto retry;
8457 }
8458
8459 drm_modeset_drop_locks(ctx);
8460 drm_modeset_acquire_fini(ctx);
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008461
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008462 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008463}
8464
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008465void intel_release_load_detect_pipe(struct drm_connector *connector,
Rob Clark51fd3712013-11-19 12:10:12 -05008466 struct intel_load_detect_pipe *old,
8467 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008468{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008469 struct intel_encoder *intel_encoder =
8470 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008471 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008472 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008473 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008474
Chris Wilsond2dff872011-04-19 08:36:26 +01008475 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008476 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008477 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008478
Chris Wilson8261b192011-04-19 23:18:09 +01008479 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008480 to_intel_connector(connector)->new_encoder = NULL;
8481 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008482 intel_crtc->new_enabled = false;
8483 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008484 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008485
Daniel Vetter36206362012-12-10 20:42:17 +01008486 if (old->release_fb) {
8487 drm_framebuffer_unregister_private(old->release_fb);
8488 drm_framebuffer_unreference(old->release_fb);
8489 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008490
Rob Clark51fd3712013-11-19 12:10:12 -05008491 goto unlock;
Chris Wilson0622a532011-04-21 09:32:11 +01008492 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008493 }
8494
Eric Anholtc751ce42010-03-25 11:48:48 -07008495 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008496 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8497 connector->funcs->dpms(connector, old->dpms_mode);
Daniel Vetter7b240562012-12-12 00:35:33 +01008498
Rob Clark51fd3712013-11-19 12:10:12 -05008499unlock:
8500 drm_modeset_drop_locks(ctx);
8501 drm_modeset_acquire_fini(ctx);
Jesse Barnes79e53942008-11-07 14:24:08 -08008502}
8503
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008504static int i9xx_pll_refclk(struct drm_device *dev,
8505 const struct intel_crtc_config *pipe_config)
8506{
8507 struct drm_i915_private *dev_priv = dev->dev_private;
8508 u32 dpll = pipe_config->dpll_hw_state.dpll;
8509
8510 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008511 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008512 else if (HAS_PCH_SPLIT(dev))
8513 return 120000;
8514 else if (!IS_GEN2(dev))
8515 return 96000;
8516 else
8517 return 48000;
8518}
8519
Jesse Barnes79e53942008-11-07 14:24:08 -08008520/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008521static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8522 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008523{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008524 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008525 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008526 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008527 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008528 u32 fp;
8529 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008530 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008531
8532 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008533 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008534 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008535 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008536
8537 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008538 if (IS_PINEVIEW(dev)) {
8539 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8540 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008541 } else {
8542 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8543 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8544 }
8545
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008546 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008547 if (IS_PINEVIEW(dev))
8548 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8549 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008550 else
8551 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008552 DPLL_FPA01_P1_POST_DIV_SHIFT);
8553
8554 switch (dpll & DPLL_MODE_MASK) {
8555 case DPLLB_MODE_DAC_SERIAL:
8556 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8557 5 : 10;
8558 break;
8559 case DPLLB_MODE_LVDS:
8560 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8561 7 : 14;
8562 break;
8563 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008564 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008565 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008566 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008567 }
8568
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008569 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008570 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008571 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008572 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008573 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008574 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008575 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008576
8577 if (is_lvds) {
8578 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8579 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008580
8581 if (lvds & LVDS_CLKB_POWER_UP)
8582 clock.p2 = 7;
8583 else
8584 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008585 } else {
8586 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8587 clock.p1 = 2;
8588 else {
8589 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8590 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8591 }
8592 if (dpll & PLL_P2_DIVIDE_BY_4)
8593 clock.p2 = 4;
8594 else
8595 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008596 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008597
8598 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008599 }
8600
Ville Syrjälä18442d02013-09-13 16:00:08 +03008601 /*
8602 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008603 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008604 * encoder's get_config() function.
8605 */
8606 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008607}
8608
Ville Syrjälä6878da02013-09-13 15:59:11 +03008609int intel_dotclock_calculate(int link_freq,
8610 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008611{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008612 /*
8613 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008614 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008615 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008616 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008617 *
8618 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008619 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008620 */
8621
Ville Syrjälä6878da02013-09-13 15:59:11 +03008622 if (!m_n->link_n)
8623 return 0;
8624
8625 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8626}
8627
Ville Syrjälä18442d02013-09-13 16:00:08 +03008628static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8629 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008630{
8631 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008632
8633 /* read out port_clock from the DPLL */
8634 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008635
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008636 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008637 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008638 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008639 * agree once we know their relationship in the encoder's
8640 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008641 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008642 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008643 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8644 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008645}
8646
8647/** Returns the currently programmed mode of the given pipe. */
8648struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8649 struct drm_crtc *crtc)
8650{
Jesse Barnes548f2452011-02-17 10:40:53 -08008651 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008653 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008654 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008655 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008656 int htot = I915_READ(HTOTAL(cpu_transcoder));
8657 int hsync = I915_READ(HSYNC(cpu_transcoder));
8658 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8659 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008660 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008661
8662 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8663 if (!mode)
8664 return NULL;
8665
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008666 /*
8667 * Construct a pipe_config sufficient for getting the clock info
8668 * back out of crtc_clock_get.
8669 *
8670 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8671 * to use a real value here instead.
8672 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008673 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008674 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008675 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8676 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8677 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008678 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8679
Ville Syrjälä773ae032013-09-23 17:48:20 +03008680 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008681 mode->hdisplay = (htot & 0xffff) + 1;
8682 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8683 mode->hsync_start = (hsync & 0xffff) + 1;
8684 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8685 mode->vdisplay = (vtot & 0xffff) + 1;
8686 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8687 mode->vsync_start = (vsync & 0xffff) + 1;
8688 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8689
8690 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008691
8692 return mode;
8693}
8694
Daniel Vetter3dec0092010-08-20 21:40:52 +02008695static void intel_increase_pllclock(struct drm_crtc *crtc)
Jesse Barnes652c3932009-08-17 13:31:43 -07008696{
8697 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008698 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8700 int pipe = intel_crtc->pipe;
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008701 int dpll_reg = DPLL(pipe);
8702 int dpll;
Jesse Barnes652c3932009-08-17 13:31:43 -07008703
Eric Anholtbad720f2009-10-22 16:11:14 -07008704 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008705 return;
8706
8707 if (!dev_priv->lvds_downclock_avail)
8708 return;
8709
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008710 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008711 if (!HAS_PIPE_CXSR(dev) && (dpll & DISPLAY_RATE_SELECT_FPA1)) {
Zhao Yakui44d98a62009-10-09 11:39:40 +08008712 DRM_DEBUG_DRIVER("upclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008713
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008714 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008715
8716 dpll &= ~DISPLAY_RATE_SELECT_FPA1;
8717 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008718 intel_wait_for_vblank(dev, pipe);
Jesse Barnesdbdc6472010-12-30 09:36:39 -08008719
Jesse Barnes652c3932009-08-17 13:31:43 -07008720 dpll = I915_READ(dpll_reg);
8721 if (dpll & DISPLAY_RATE_SELECT_FPA1)
Zhao Yakui44d98a62009-10-09 11:39:40 +08008722 DRM_DEBUG_DRIVER("failed to upclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008723 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008724}
8725
8726static void intel_decrease_pllclock(struct drm_crtc *crtc)
8727{
8728 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008729 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008730 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008731
Eric Anholtbad720f2009-10-22 16:11:14 -07008732 if (HAS_PCH_SPLIT(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008733 return;
8734
8735 if (!dev_priv->lvds_downclock_avail)
8736 return;
8737
8738 /*
8739 * Since this is called by a timer, we should never get here in
8740 * the manual case.
8741 */
8742 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008743 int pipe = intel_crtc->pipe;
8744 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008745 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008746
Zhao Yakui44d98a62009-10-09 11:39:40 +08008747 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008748
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008749 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008750
Chris Wilson074b5e12012-05-02 12:07:06 +01008751 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008752 dpll |= DISPLAY_RATE_SELECT_FPA1;
8753 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008754 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008755 dpll = I915_READ(dpll_reg);
8756 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008757 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008758 }
8759
8760}
8761
Chris Wilsonf047e392012-07-21 12:31:41 +01008762void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008763{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008764 struct drm_i915_private *dev_priv = dev->dev_private;
8765
Chris Wilsonf62a0072014-02-21 17:55:39 +00008766 if (dev_priv->mm.busy)
8767 return;
8768
Paulo Zanoni43694d62014-03-07 20:08:08 -03008769 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008770 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00008771 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01008772}
8773
8774void intel_mark_idle(struct drm_device *dev)
8775{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008776 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008777 struct drm_crtc *crtc;
8778
Chris Wilsonf62a0072014-02-21 17:55:39 +00008779 if (!dev_priv->mm.busy)
8780 return;
8781
8782 dev_priv->mm.busy = false;
8783
Jani Nikulad330a952014-01-21 11:24:25 +02008784 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008785 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00008786
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008787 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008788 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00008789 continue;
8790
8791 intel_decrease_pllclock(crtc);
8792 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008793
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008794 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008795 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008796
8797out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03008798 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008799}
8800
Chris Wilsonc65355b2013-06-06 16:53:41 -03008801void intel_mark_fb_busy(struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01008802 struct intel_engine_cs *ring)
Chris Wilsonf047e392012-07-21 12:31:41 +01008803{
8804 struct drm_device *dev = obj->base.dev;
Jesse Barnes652c3932009-08-17 13:31:43 -07008805 struct drm_crtc *crtc;
Jesse Barnes652c3932009-08-17 13:31:43 -07008806
Jani Nikulad330a952014-01-21 11:24:25 +02008807 if (!i915.powersave)
Jesse Barnes652c3932009-08-17 13:31:43 -07008808 return;
8809
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008810 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008811 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -07008812 continue;
8813
Matt Roperf4510a22014-04-01 15:22:40 -07008814 if (to_intel_framebuffer(crtc->primary->fb)->obj != obj)
Chris Wilsonc65355b2013-06-06 16:53:41 -03008815 continue;
8816
8817 intel_increase_pllclock(crtc);
8818 if (ring && intel_fbc_enabled(dev))
8819 ring->fbc_dirty = true;
Jesse Barnes652c3932009-08-17 13:31:43 -07008820 }
Jesse Barnes652c3932009-08-17 13:31:43 -07008821}
8822
Jesse Barnes79e53942008-11-07 14:24:08 -08008823static void intel_crtc_destroy(struct drm_crtc *crtc)
8824{
8825 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008826 struct drm_device *dev = crtc->dev;
8827 struct intel_unpin_work *work;
8828 unsigned long flags;
8829
8830 spin_lock_irqsave(&dev->event_lock, flags);
8831 work = intel_crtc->unpin_work;
8832 intel_crtc->unpin_work = NULL;
8833 spin_unlock_irqrestore(&dev->event_lock, flags);
8834
8835 if (work) {
8836 cancel_work_sync(&work->work);
8837 kfree(work);
8838 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008839
Mika Kuoppala40ccc722013-04-23 17:27:08 +03008840 intel_crtc_cursor_set(crtc, NULL, 0, 0, 0);
8841
Jesse Barnes79e53942008-11-07 14:24:08 -08008842 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008843
Jesse Barnes79e53942008-11-07 14:24:08 -08008844 kfree(intel_crtc);
8845}
8846
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008847static void intel_unpin_work_fn(struct work_struct *__work)
8848{
8849 struct intel_unpin_work *work =
8850 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008851 struct drm_device *dev = work->crtc->dev;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008852
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008853 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008854 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008855 drm_gem_object_unreference(&work->pending_flip_obj->base);
8856 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008857
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008858 intel_update_fbc(dev);
8859 mutex_unlock(&dev->struct_mutex);
8860
8861 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8862 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8863
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008864 kfree(work);
8865}
8866
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008867static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008868 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008869{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008870 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8872 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008873 unsigned long flags;
8874
8875 /* Ignore early vblank irqs */
8876 if (intel_crtc == NULL)
8877 return;
8878
8879 spin_lock_irqsave(&dev->event_lock, flags);
8880 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008881
8882 /* Ensure we don't miss a work->pending update ... */
8883 smp_rmb();
8884
8885 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008886 spin_unlock_irqrestore(&dev->event_lock, flags);
8887 return;
8888 }
8889
Chris Wilsone7d841c2012-12-03 11:36:30 +00008890 /* and that the unpin work is consistent wrt ->pending. */
8891 smp_rmb();
8892
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008893 intel_crtc->unpin_work = NULL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008894
Rob Clark45a066e2012-10-08 14:50:40 -05008895 if (work->event)
8896 drm_send_vblank_event(dev, intel_crtc->pipe, work->event);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008897
Daniel Vetter87b6b102014-05-15 15:33:46 +02008898 drm_crtc_vblank_put(crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008899
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008900 spin_unlock_irqrestore(&dev->event_lock, flags);
8901
Daniel Vetter2c10d572012-12-20 21:24:07 +01008902 wake_up_all(&dev_priv->pending_flip_queue);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008903
8904 queue_work(dev_priv->wq, &work->work);
Jesse Barnese5510fa2010-07-01 16:48:37 -07008905
8906 trace_i915_flip_complete(intel_crtc->plane, work->pending_flip_obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008907}
8908
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008909void intel_finish_page_flip(struct drm_device *dev, int pipe)
8910{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008911 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008912 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8913
Mario Kleiner49b14a52010-12-09 07:00:07 +01008914 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008915}
8916
8917void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8918{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008919 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008920 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8921
Mario Kleiner49b14a52010-12-09 07:00:07 +01008922 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008923}
8924
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03008925/* Is 'a' after or equal to 'b'? */
8926static bool g4x_flip_count_after_eq(u32 a, u32 b)
8927{
8928 return !((a - b) & 0x80000000);
8929}
8930
8931static bool page_flip_finished(struct intel_crtc *crtc)
8932{
8933 struct drm_device *dev = crtc->base.dev;
8934 struct drm_i915_private *dev_priv = dev->dev_private;
8935
8936 /*
8937 * The relevant registers doen't exist on pre-ctg.
8938 * As the flip done interrupt doesn't trigger for mmio
8939 * flips on gmch platforms, a flip count check isn't
8940 * really needed there. But since ctg has the registers,
8941 * include it in the check anyway.
8942 */
8943 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8944 return true;
8945
8946 /*
8947 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8948 * used the same base address. In that case the mmio flip might
8949 * have completed, but the CS hasn't even executed the flip yet.
8950 *
8951 * A flip count check isn't enough as the CS might have updated
8952 * the base address just after start of vblank, but before we
8953 * managed to process the interrupt. This means we'd complete the
8954 * CS flip too soon.
8955 *
8956 * Combining both checks should get us a good enough result. It may
8957 * still happen that the CS flip has been executed, but has not
8958 * yet actually completed. But in case the base address is the same
8959 * anyway, we don't really care.
8960 */
8961 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
8962 crtc->unpin_work->gtt_offset &&
8963 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
8964 crtc->unpin_work->flip_count);
8965}
8966
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008967void intel_prepare_page_flip(struct drm_device *dev, int plane)
8968{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008969 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008970 struct intel_crtc *intel_crtc =
8971 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
8972 unsigned long flags;
8973
Chris Wilsone7d841c2012-12-03 11:36:30 +00008974 /* NB: An MMIO update of the plane base pointer will also
8975 * generate a page-flip completion irq, i.e. every modeset
8976 * is also accompanied by a spurious intel_prepare_page_flip().
8977 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008978 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03008979 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00008980 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008981 spin_unlock_irqrestore(&dev->event_lock, flags);
8982}
8983
Robin Schroereba905b2014-05-18 02:24:50 +02008984static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00008985{
8986 /* Ensure that the work item is consistent when activating it ... */
8987 smp_wmb();
8988 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
8989 /* and that it is marked active as soon as the irq could fire. */
8990 smp_wmb();
8991}
8992
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008993static int intel_gen2_queue_flip(struct drm_device *dev,
8994 struct drm_crtc *crtc,
8995 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07008996 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01008997 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07008998 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07008999{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009000 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009001 u32 flip_mask;
9002 int ret;
9003
Daniel Vetter6d90c952012-04-26 23:28:05 +02009004 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009005 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009006 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009007
9008 /* Can't queue multiple flips, so wait for the previous
9009 * one to finish before executing the next.
9010 */
9011 if (intel_crtc->plane)
9012 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9013 else
9014 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009015 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9016 intel_ring_emit(ring, MI_NOOP);
9017 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9018 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9019 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009020 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009021 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009022
9023 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009024 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009025 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009026}
9027
9028static int intel_gen3_queue_flip(struct drm_device *dev,
9029 struct drm_crtc *crtc,
9030 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009031 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009032 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009033 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009034{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009036 u32 flip_mask;
9037 int ret;
9038
Daniel Vetter6d90c952012-04-26 23:28:05 +02009039 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009040 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009041 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009042
9043 if (intel_crtc->plane)
9044 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9045 else
9046 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009047 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9048 intel_ring_emit(ring, MI_NOOP);
9049 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9050 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9051 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009052 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009053 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009054
Chris Wilsone7d841c2012-12-03 11:36:30 +00009055 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009056 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009057 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009058}
9059
9060static int intel_gen4_queue_flip(struct drm_device *dev,
9061 struct drm_crtc *crtc,
9062 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009063 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009064 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009065 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009066{
9067 struct drm_i915_private *dev_priv = dev->dev_private;
9068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9069 uint32_t pf, pipesrc;
9070 int ret;
9071
Daniel Vetter6d90c952012-04-26 23:28:05 +02009072 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009073 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009074 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009075
9076 /* i965+ uses the linear or tiled offsets from the
9077 * Display Registers (which do not change across a page-flip)
9078 * so we need only reprogram the base address.
9079 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009080 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9081 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9082 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009083 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009084 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009085
9086 /* XXX Enabling the panel-fitter across page-flip is so far
9087 * untested on non-native modes, so ignore it for now.
9088 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9089 */
9090 pf = 0;
9091 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009092 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009093
9094 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009095 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009096 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009097}
9098
9099static int intel_gen6_queue_flip(struct drm_device *dev,
9100 struct drm_crtc *crtc,
9101 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009102 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009103 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009104 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009105{
9106 struct drm_i915_private *dev_priv = dev->dev_private;
9107 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9108 uint32_t pf, pipesrc;
9109 int ret;
9110
Daniel Vetter6d90c952012-04-26 23:28:05 +02009111 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009112 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009113 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009114
Daniel Vetter6d90c952012-04-26 23:28:05 +02009115 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9116 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9117 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009118 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009119
Chris Wilson99d9acd2012-04-17 20:37:00 +01009120 /* Contrary to the suggestions in the documentation,
9121 * "Enable Panel Fitter" does not seem to be required when page
9122 * flipping with a non-native mode, and worse causes a normal
9123 * modeset to fail.
9124 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9125 */
9126 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009127 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009128 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009129
9130 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009131 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009132 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009133}
9134
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009135static int intel_gen7_queue_flip(struct drm_device *dev,
9136 struct drm_crtc *crtc,
9137 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009138 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009139 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009140 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009141{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009143 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009144 int len, ret;
9145
Robin Schroereba905b2014-05-18 02:24:50 +02009146 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009147 case PLANE_A:
9148 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9149 break;
9150 case PLANE_B:
9151 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9152 break;
9153 case PLANE_C:
9154 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9155 break;
9156 default:
9157 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009158 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009159 }
9160
Chris Wilsonffe74d72013-08-26 20:58:12 +01009161 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009162 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009163 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009164 /*
9165 * On Gen 8, SRM is now taking an extra dword to accommodate
9166 * 48bits addresses, and we need a NOOP for the batch size to
9167 * stay even.
9168 */
9169 if (IS_GEN8(dev))
9170 len += 2;
9171 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009172
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009173 /*
9174 * BSpec MI_DISPLAY_FLIP for IVB:
9175 * "The full packet must be contained within the same cache line."
9176 *
9177 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9178 * cacheline, if we ever start emitting more commands before
9179 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9180 * then do the cacheline alignment, and finally emit the
9181 * MI_DISPLAY_FLIP.
9182 */
9183 ret = intel_ring_cacheline_align(ring);
9184 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009185 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009186
Chris Wilsonffe74d72013-08-26 20:58:12 +01009187 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009188 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009189 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009190
Chris Wilsonffe74d72013-08-26 20:58:12 +01009191 /* Unmask the flip-done completion message. Note that the bspec says that
9192 * we should do this for both the BCS and RCS, and that we must not unmask
9193 * more than one flip event at any time (or ensure that one flip message
9194 * can be sent by waiting for flip-done prior to queueing new flips).
9195 * Experimentation says that BCS works despite DERRMR masking all
9196 * flip-done completion events and that unmasking all planes at once
9197 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9198 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9199 */
9200 if (ring->id == RCS) {
9201 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9202 intel_ring_emit(ring, DERRMR);
9203 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9204 DERRMR_PIPEB_PRI_FLIP_DONE |
9205 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009206 if (IS_GEN8(dev))
9207 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9208 MI_SRM_LRM_GLOBAL_GTT);
9209 else
9210 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9211 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009212 intel_ring_emit(ring, DERRMR);
9213 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009214 if (IS_GEN8(dev)) {
9215 intel_ring_emit(ring, 0);
9216 intel_ring_emit(ring, MI_NOOP);
9217 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009218 }
9219
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009220 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009221 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009222 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009223 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009224
9225 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009226 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009227 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009228}
9229
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009230static int intel_default_queue_flip(struct drm_device *dev,
9231 struct drm_crtc *crtc,
9232 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009233 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009234 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009235 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009236{
9237 return -ENODEV;
9238}
9239
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009240static int intel_crtc_page_flip(struct drm_crtc *crtc,
9241 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009242 struct drm_pending_vblank_event *event,
9243 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009244{
9245 struct drm_device *dev = crtc->dev;
9246 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009247 struct drm_framebuffer *old_fb = crtc->primary->fb;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009248 struct drm_i915_gem_object *obj = to_intel_framebuffer(fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009249 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9250 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009251 struct intel_engine_cs *ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009252 unsigned long flags;
Chris Wilson52e68632010-08-08 10:15:59 +01009253 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009254
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009255 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009256 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009257 return -EINVAL;
9258
9259 /*
9260 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9261 * Note that pitch changes could also affect these register.
9262 */
9263 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009264 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9265 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009266 return -EINVAL;
9267
Chris Wilsonf900db42014-02-20 09:26:13 +00009268 if (i915_terminally_wedged(&dev_priv->gpu_error))
9269 goto out_hang;
9270
Daniel Vetterb14c5672013-09-19 12:18:32 +02009271 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009272 if (work == NULL)
9273 return -ENOMEM;
9274
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009275 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009276 work->crtc = crtc;
Ville Syrjälä4a35f832013-02-22 16:53:38 +02009277 work->old_fb_obj = to_intel_framebuffer(old_fb)->obj;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009278 INIT_WORK(&work->work, intel_unpin_work_fn);
9279
Daniel Vetter87b6b102014-05-15 15:33:46 +02009280 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009281 if (ret)
9282 goto free_work;
9283
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009284 /* We borrow the event spin lock for protecting unpin_work */
9285 spin_lock_irqsave(&dev->event_lock, flags);
9286 if (intel_crtc->unpin_work) {
9287 spin_unlock_irqrestore(&dev->event_lock, flags);
9288 kfree(work);
Daniel Vetter87b6b102014-05-15 15:33:46 +02009289 drm_crtc_vblank_put(crtc);
Chris Wilson468f0b42010-05-27 13:18:13 +01009290
9291 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009292 return -EBUSY;
9293 }
9294 intel_crtc->unpin_work = work;
9295 spin_unlock_irqrestore(&dev->event_lock, flags);
9296
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009297 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9298 flush_workqueue(dev_priv->wq);
9299
Chris Wilson79158102012-05-23 11:13:58 +01009300 ret = i915_mutex_lock_interruptible(dev);
9301 if (ret)
9302 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009303
Jesse Barnes75dfca82010-02-10 15:09:44 -08009304 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009305 drm_gem_object_reference(&work->old_fb_obj->base);
9306 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009307
Matt Roperf4510a22014-04-01 15:22:40 -07009308 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009309
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009310 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009311
Simon Farnsworth4e5359c2010-09-01 17:47:52 +01009312 work->enable_stall_check = true;
9313
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009314 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009315 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009316
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009317 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
9318 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(intel_crtc->pipe)) + 1;
9319
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009320 if (IS_VALLEYVIEW(dev)) {
9321 ring = &dev_priv->ring[BCS];
9322 } else if (INTEL_INFO(dev)->gen >= 7) {
9323 ring = obj->ring;
9324 if (ring == NULL || ring->id != RCS)
9325 ring = &dev_priv->ring[BCS];
9326 } else {
9327 ring = &dev_priv->ring[RCS];
9328 }
9329
9330 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009331 if (ret)
9332 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009333
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009334 work->gtt_offset =
9335 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9336
9337 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring, page_flip_flags);
9338 if (ret)
9339 goto cleanup_unpin;
9340
Chris Wilson7782de32011-07-08 12:22:41 +01009341 intel_disable_fbc(dev);
Chris Wilsonc65355b2013-06-06 16:53:41 -03009342 intel_mark_fb_busy(obj, NULL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009343 mutex_unlock(&dev->struct_mutex);
9344
Jesse Barnese5510fa2010-07-01 16:48:37 -07009345 trace_i915_flip_request(intel_crtc->plane, obj);
9346
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009347 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009348
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009349cleanup_unpin:
9350 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009351cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009352 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009353 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00009354 drm_gem_object_unreference(&work->old_fb_obj->base);
9355 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009356 mutex_unlock(&dev->struct_mutex);
9357
Chris Wilson79158102012-05-23 11:13:58 +01009358cleanup:
Chris Wilson96b099f2010-06-07 14:03:04 +01009359 spin_lock_irqsave(&dev->event_lock, flags);
9360 intel_crtc->unpin_work = NULL;
9361 spin_unlock_irqrestore(&dev->event_lock, flags);
9362
Daniel Vetter87b6b102014-05-15 15:33:46 +02009363 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009364free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009365 kfree(work);
9366
Chris Wilsonf900db42014-02-20 09:26:13 +00009367 if (ret == -EIO) {
9368out_hang:
9369 intel_crtc_wait_for_pending_flips(crtc);
9370 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
9371 if (ret == 0 && event)
9372 drm_send_vblank_event(dev, intel_crtc->pipe, event);
9373 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009374 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009375}
9376
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009377static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009378 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9379 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009380};
9381
Daniel Vetter9a935852012-07-05 22:34:27 +02009382/**
9383 * intel_modeset_update_staged_output_state
9384 *
9385 * Updates the staged output configuration state, e.g. after we've read out the
9386 * current hw state.
9387 */
9388static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9389{
Ville Syrjälä76688512014-01-10 11:28:06 +02009390 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009391 struct intel_encoder *encoder;
9392 struct intel_connector *connector;
9393
9394 list_for_each_entry(connector, &dev->mode_config.connector_list,
9395 base.head) {
9396 connector->new_encoder =
9397 to_intel_encoder(connector->base.encoder);
9398 }
9399
9400 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9401 base.head) {
9402 encoder->new_crtc =
9403 to_intel_crtc(encoder->base.crtc);
9404 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009405
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009406 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009407 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009408
9409 if (crtc->new_enabled)
9410 crtc->new_config = &crtc->config;
9411 else
9412 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009413 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009414}
9415
9416/**
9417 * intel_modeset_commit_output_state
9418 *
9419 * This function copies the stage display pipe configuration to the real one.
9420 */
9421static void intel_modeset_commit_output_state(struct drm_device *dev)
9422{
Ville Syrjälä76688512014-01-10 11:28:06 +02009423 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009424 struct intel_encoder *encoder;
9425 struct intel_connector *connector;
9426
9427 list_for_each_entry(connector, &dev->mode_config.connector_list,
9428 base.head) {
9429 connector->base.encoder = &connector->new_encoder->base;
9430 }
9431
9432 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9433 base.head) {
9434 encoder->base.crtc = &encoder->new_crtc->base;
9435 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009436
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009437 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009438 crtc->base.enabled = crtc->new_enabled;
9439 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009440}
9441
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009442static void
Robin Schroereba905b2014-05-18 02:24:50 +02009443connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009444 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009445{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009446 int bpp = pipe_config->pipe_bpp;
9447
9448 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9449 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03009450 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009451
9452 /* Don't use an invalid EDID bpc value */
9453 if (connector->base.display_info.bpc &&
9454 connector->base.display_info.bpc * 3 < bpp) {
9455 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9456 bpp, connector->base.display_info.bpc*3);
9457 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9458 }
9459
9460 /* Clamp bpp to 8 on screens without EDID 1.4 */
9461 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9462 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9463 bpp);
9464 pipe_config->pipe_bpp = 24;
9465 }
9466}
9467
9468static int
9469compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9470 struct drm_framebuffer *fb,
9471 struct intel_crtc_config *pipe_config)
9472{
9473 struct drm_device *dev = crtc->base.dev;
9474 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009475 int bpp;
9476
Daniel Vetterd42264b2013-03-28 16:38:08 +01009477 switch (fb->pixel_format) {
9478 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009479 bpp = 8*3; /* since we go through a colormap */
9480 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009481 case DRM_FORMAT_XRGB1555:
9482 case DRM_FORMAT_ARGB1555:
9483 /* checked in intel_framebuffer_init already */
9484 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9485 return -EINVAL;
9486 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009487 bpp = 6*3; /* min is 18bpp */
9488 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009489 case DRM_FORMAT_XBGR8888:
9490 case DRM_FORMAT_ABGR8888:
9491 /* checked in intel_framebuffer_init already */
9492 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9493 return -EINVAL;
9494 case DRM_FORMAT_XRGB8888:
9495 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009496 bpp = 8*3;
9497 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009498 case DRM_FORMAT_XRGB2101010:
9499 case DRM_FORMAT_ARGB2101010:
9500 case DRM_FORMAT_XBGR2101010:
9501 case DRM_FORMAT_ABGR2101010:
9502 /* checked in intel_framebuffer_init already */
9503 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01009504 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009505 bpp = 10*3;
9506 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01009507 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009508 default:
9509 DRM_DEBUG_KMS("unsupported depth\n");
9510 return -EINVAL;
9511 }
9512
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009513 pipe_config->pipe_bpp = bpp;
9514
9515 /* Clamp display bpp to EDID value */
9516 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009517 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02009518 if (!connector->new_encoder ||
9519 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009520 continue;
9521
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009522 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009523 }
9524
9525 return bpp;
9526}
9527
Daniel Vetter644db712013-09-19 14:53:58 +02009528static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9529{
9530 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9531 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01009532 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02009533 mode->crtc_hdisplay, mode->crtc_hsync_start,
9534 mode->crtc_hsync_end, mode->crtc_htotal,
9535 mode->crtc_vdisplay, mode->crtc_vsync_start,
9536 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9537}
9538
Daniel Vetterc0b03412013-05-28 12:05:54 +02009539static void intel_dump_pipe_config(struct intel_crtc *crtc,
9540 struct intel_crtc_config *pipe_config,
9541 const char *context)
9542{
9543 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9544 context, pipe_name(crtc->pipe));
9545
9546 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9547 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9548 pipe_config->pipe_bpp, pipe_config->dither);
9549 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9550 pipe_config->has_pch_encoder,
9551 pipe_config->fdi_lanes,
9552 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9553 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9554 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009555 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9556 pipe_config->has_dp_encoder,
9557 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9558 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9559 pipe_config->dp_m_n.tu);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009560 DRM_DEBUG_KMS("requested mode:\n");
9561 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9562 DRM_DEBUG_KMS("adjusted mode:\n");
9563 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02009564 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009565 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009566 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9567 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009568 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9569 pipe_config->gmch_pfit.control,
9570 pipe_config->gmch_pfit.pgm_ratios,
9571 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009572 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02009573 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009574 pipe_config->pch_pfit.size,
9575 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009576 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03009577 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009578}
9579
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009580static bool encoders_cloneable(const struct intel_encoder *a,
9581 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009582{
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009583 /* masks could be asymmetric, so check both ways */
9584 return a == b || (a->cloneable & (1 << b->type) &&
9585 b->cloneable & (1 << a->type));
9586}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009587
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009588static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9589 struct intel_encoder *encoder)
9590{
9591 struct drm_device *dev = crtc->base.dev;
9592 struct intel_encoder *source_encoder;
9593
9594 list_for_each_entry(source_encoder,
9595 &dev->mode_config.encoder_list, base.head) {
9596 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009597 continue;
9598
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009599 if (!encoders_cloneable(encoder, source_encoder))
9600 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009601 }
9602
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009603 return true;
9604}
9605
9606static bool check_encoder_cloning(struct intel_crtc *crtc)
9607{
9608 struct drm_device *dev = crtc->base.dev;
9609 struct intel_encoder *encoder;
9610
9611 list_for_each_entry(encoder,
9612 &dev->mode_config.encoder_list, base.head) {
9613 if (encoder->new_crtc != crtc)
9614 continue;
9615
9616 if (!check_single_encoder_cloning(crtc, encoder))
9617 return false;
9618 }
9619
9620 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009621}
9622
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009623static struct intel_crtc_config *
9624intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009625 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009626 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009627{
9628 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009629 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009630 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009631 int plane_bpp, ret = -EINVAL;
9632 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009633
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009634 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009635 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9636 return ERR_PTR(-EINVAL);
9637 }
9638
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009639 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9640 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009641 return ERR_PTR(-ENOMEM);
9642
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009643 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9644 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009645
Daniel Vettere143a212013-07-04 12:01:15 +02009646 pipe_config->cpu_transcoder =
9647 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009648 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009649
Imre Deak2960bc92013-07-30 13:36:32 +03009650 /*
9651 * Sanitize sync polarity flags based on requested ones. If neither
9652 * positive or negative polarity is requested, treat this as meaning
9653 * negative polarity.
9654 */
9655 if (!(pipe_config->adjusted_mode.flags &
9656 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9657 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9658
9659 if (!(pipe_config->adjusted_mode.flags &
9660 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9661 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9662
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009663 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9664 * plane pixel format and any sink constraints into account. Returns the
9665 * source plane bpp so that dithering can be selected on mismatches
9666 * after encoders and crtc also have had their say. */
9667 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9668 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009669 if (plane_bpp < 0)
9670 goto fail;
9671
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009672 /*
9673 * Determine the real pipe dimensions. Note that stereo modes can
9674 * increase the actual pipe size due to the frame doubling and
9675 * insertion of additional space for blanks between the frame. This
9676 * is stored in the crtc timings. We use the requested mode to do this
9677 * computation to clearly distinguish it from the adjusted mode, which
9678 * can be changed by the connectors in the below retry loop.
9679 */
9680 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9681 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9682 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9683
Daniel Vettere29c22c2013-02-21 00:00:16 +01009684encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02009685 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02009686 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02009687 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009688
Daniel Vetter135c81b2013-07-21 21:37:09 +02009689 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009690 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009691
Daniel Vetter7758a112012-07-08 19:40:39 +02009692 /* Pass our mode to the connectors and the CRTC to give them a chance to
9693 * adjust it according to limitations or connector properties, and also
9694 * a chance to reject the mode entirely.
9695 */
9696 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9697 base.head) {
9698
9699 if (&encoder->new_crtc->base != crtc)
9700 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +01009701
Daniel Vetterefea6e82013-07-21 21:36:59 +02009702 if (!(encoder->compute_config(encoder, pipe_config))) {
9703 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +02009704 goto fail;
9705 }
9706 }
9707
Daniel Vetterff9a6752013-06-01 17:16:21 +02009708 /* Set default port clock if not overwritten by the encoder. Needs to be
9709 * done afterwards in case the encoder adjusts the mode. */
9710 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +01009711 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
9712 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009713
Daniel Vettera43f6e02013-06-07 23:10:32 +02009714 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009715 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +02009716 DRM_DEBUG_KMS("CRTC fixup failed\n");
9717 goto fail;
9718 }
Daniel Vettere29c22c2013-02-21 00:00:16 +01009719
9720 if (ret == RETRY) {
9721 if (WARN(!retry, "loop in pipe configuration computation\n")) {
9722 ret = -EINVAL;
9723 goto fail;
9724 }
9725
9726 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
9727 retry = false;
9728 goto encoder_retry;
9729 }
9730
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009731 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
9732 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
9733 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
9734
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009735 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +02009736fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009737 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +01009738 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +02009739}
9740
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009741/* Computes which crtcs are affected and sets the relevant bits in the mask. For
9742 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
9743static void
9744intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
9745 unsigned *prepare_pipes, unsigned *disable_pipes)
9746{
9747 struct intel_crtc *intel_crtc;
9748 struct drm_device *dev = crtc->dev;
9749 struct intel_encoder *encoder;
9750 struct intel_connector *connector;
9751 struct drm_crtc *tmp_crtc;
9752
9753 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
9754
9755 /* Check which crtcs have changed outputs connected to them, these need
9756 * to be part of the prepare_pipes mask. We don't (yet) support global
9757 * modeset across multiple crtcs, so modeset_pipes will only have one
9758 * bit set at most. */
9759 list_for_each_entry(connector, &dev->mode_config.connector_list,
9760 base.head) {
9761 if (connector->base.encoder == &connector->new_encoder->base)
9762 continue;
9763
9764 if (connector->base.encoder) {
9765 tmp_crtc = connector->base.encoder->crtc;
9766
9767 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9768 }
9769
9770 if (connector->new_encoder)
9771 *prepare_pipes |=
9772 1 << connector->new_encoder->new_crtc->pipe;
9773 }
9774
9775 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
9776 base.head) {
9777 if (encoder->base.crtc == &encoder->new_crtc->base)
9778 continue;
9779
9780 if (encoder->base.crtc) {
9781 tmp_crtc = encoder->base.crtc;
9782
9783 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
9784 }
9785
9786 if (encoder->new_crtc)
9787 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
9788 }
9789
Ville Syrjälä76688512014-01-10 11:28:06 +02009790 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009791 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009792 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009793 continue;
9794
Ville Syrjälä76688512014-01-10 11:28:06 +02009795 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009796 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +02009797 else
9798 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009799 }
9800
9801
9802 /* set_mode is also used to update properties on life display pipes. */
9803 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +02009804 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009805 *prepare_pipes |= 1 << intel_crtc->pipe;
9806
Daniel Vetterb6c51642013-04-12 18:48:43 +02009807 /*
9808 * For simplicity do a full modeset on any pipe where the output routing
9809 * changed. We could be more clever, but that would require us to be
9810 * more careful with calling the relevant encoder->mode_set functions.
9811 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009812 if (*prepare_pipes)
9813 *modeset_pipes = *prepare_pipes;
9814
9815 /* ... and mask these out. */
9816 *modeset_pipes &= ~(*disable_pipes);
9817 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +02009818
9819 /*
9820 * HACK: We don't (yet) fully support global modesets. intel_set_config
9821 * obies this rule, but the modeset restore mode of
9822 * intel_modeset_setup_hw_state does not.
9823 */
9824 *modeset_pipes &= 1 << intel_crtc->pipe;
9825 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +02009826
9827 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
9828 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +02009829}
9830
Daniel Vetterea9d7582012-07-10 10:42:52 +02009831static bool intel_crtc_in_use(struct drm_crtc *crtc)
9832{
9833 struct drm_encoder *encoder;
9834 struct drm_device *dev = crtc->dev;
9835
9836 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
9837 if (encoder->crtc == crtc)
9838 return true;
9839
9840 return false;
9841}
9842
9843static void
9844intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
9845{
9846 struct intel_encoder *intel_encoder;
9847 struct intel_crtc *intel_crtc;
9848 struct drm_connector *connector;
9849
9850 list_for_each_entry(intel_encoder, &dev->mode_config.encoder_list,
9851 base.head) {
9852 if (!intel_encoder->base.crtc)
9853 continue;
9854
9855 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
9856
9857 if (prepare_pipes & (1 << intel_crtc->pipe))
9858 intel_encoder->connectors_active = false;
9859 }
9860
9861 intel_modeset_commit_output_state(dev);
9862
Ville Syrjälä76688512014-01-10 11:28:06 +02009863 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009864 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009865 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009866 WARN_ON(intel_crtc->new_config &&
9867 intel_crtc->new_config != &intel_crtc->config);
9868 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009869 }
9870
9871 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
9872 if (!connector->encoder || !connector->encoder->crtc)
9873 continue;
9874
9875 intel_crtc = to_intel_crtc(connector->encoder->crtc);
9876
9877 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +02009878 struct drm_property *dpms_property =
9879 dev->mode_config.dpms_property;
9880
Daniel Vetterea9d7582012-07-10 10:42:52 +02009881 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -05009882 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +02009883 dpms_property,
9884 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +02009885
9886 intel_encoder = to_intel_encoder(connector->encoder);
9887 intel_encoder->connectors_active = true;
9888 }
9889 }
9890
9891}
9892
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009893static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009894{
Ville Syrjälä3bd26262013-09-06 23:29:02 +03009895 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009896
9897 if (clock1 == clock2)
9898 return true;
9899
9900 if (!clock1 || !clock2)
9901 return false;
9902
9903 diff = abs(clock1 - clock2);
9904
9905 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
9906 return true;
9907
9908 return false;
9909}
9910
Daniel Vetter25c5b262012-07-08 22:08:04 +02009911#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
9912 list_for_each_entry((intel_crtc), \
9913 &(dev)->mode_config.crtc_list, \
9914 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +02009915 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +02009916
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009917static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009918intel_pipe_config_compare(struct drm_device *dev,
9919 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009920 struct intel_crtc_config *pipe_config)
9921{
Daniel Vetter66e985c2013-06-05 13:34:20 +02009922#define PIPE_CONF_CHECK_X(name) \
9923 if (current_config->name != pipe_config->name) { \
9924 DRM_ERROR("mismatch in " #name " " \
9925 "(expected 0x%08x, found 0x%08x)\n", \
9926 current_config->name, \
9927 pipe_config->name); \
9928 return false; \
9929 }
9930
Daniel Vetter08a24032013-04-19 11:25:34 +02009931#define PIPE_CONF_CHECK_I(name) \
9932 if (current_config->name != pipe_config->name) { \
9933 DRM_ERROR("mismatch in " #name " " \
9934 "(expected %i, found %i)\n", \
9935 current_config->name, \
9936 pipe_config->name); \
9937 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +01009938 }
9939
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009940#define PIPE_CONF_CHECK_FLAGS(name, mask) \
9941 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -07009942 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009943 "(expected %i, found %i)\n", \
9944 current_config->name & (mask), \
9945 pipe_config->name & (mask)); \
9946 return false; \
9947 }
9948
Ville Syrjälä5e550652013-09-06 23:29:07 +03009949#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
9950 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
9951 DRM_ERROR("mismatch in " #name " " \
9952 "(expected %i, found %i)\n", \
9953 current_config->name, \
9954 pipe_config->name); \
9955 return false; \
9956 }
9957
Daniel Vetterbb760062013-06-06 14:55:52 +02009958#define PIPE_CONF_QUIRK(quirk) \
9959 ((current_config->quirks | pipe_config->quirks) & (quirk))
9960
Daniel Vettereccb1402013-05-22 00:50:22 +02009961 PIPE_CONF_CHECK_I(cpu_transcoder);
9962
Daniel Vetter08a24032013-04-19 11:25:34 +02009963 PIPE_CONF_CHECK_I(has_pch_encoder);
9964 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +02009965 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
9966 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
9967 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
9968 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
9969 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +02009970
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009971 PIPE_CONF_CHECK_I(has_dp_encoder);
9972 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
9973 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
9974 PIPE_CONF_CHECK_I(dp_m_n.link_m);
9975 PIPE_CONF_CHECK_I(dp_m_n.link_n);
9976 PIPE_CONF_CHECK_I(dp_m_n.tu);
9977
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009978 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
9979 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
9980 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
9981 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
9982 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
9983 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
9984
9985 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
9986 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
9987 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
9988 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
9989 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
9990 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
9991
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009992 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +02009993 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009994 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
9995 IS_VALLEYVIEW(dev))
9996 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009997
Daniel Vetter9ed109a2014-04-24 23:54:52 +02009998 PIPE_CONF_CHECK_I(has_audio);
9999
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010000 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10001 DRM_MODE_FLAG_INTERLACE);
10002
Daniel Vetterbb760062013-06-06 14:55:52 +020010003 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10004 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10005 DRM_MODE_FLAG_PHSYNC);
10006 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10007 DRM_MODE_FLAG_NHSYNC);
10008 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10009 DRM_MODE_FLAG_PVSYNC);
10010 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10011 DRM_MODE_FLAG_NVSYNC);
10012 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010013
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010014 PIPE_CONF_CHECK_I(pipe_src_w);
10015 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010016
Daniel Vetter99535992014-04-13 12:00:33 +020010017 /*
10018 * FIXME: BIOS likes to set up a cloned config with lvds+external
10019 * screen. Since we don't yet re-compute the pipe config when moving
10020 * just the lvds port away to another pipe the sw tracking won't match.
10021 *
10022 * Proper atomic modesets with recomputed global state will fix this.
10023 * Until then just don't check gmch state for inherited modes.
10024 */
10025 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10026 PIPE_CONF_CHECK_I(gmch_pfit.control);
10027 /* pfit ratios are autocomputed by the hw on gen4+ */
10028 if (INTEL_INFO(dev)->gen < 4)
10029 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10030 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10031 }
10032
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010033 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10034 if (current_config->pch_pfit.enabled) {
10035 PIPE_CONF_CHECK_I(pch_pfit.pos);
10036 PIPE_CONF_CHECK_I(pch_pfit.size);
10037 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010038
Jesse Barnese59150d2014-01-07 13:30:45 -080010039 /* BDW+ don't expose a synchronous way to read the state */
10040 if (IS_HASWELL(dev))
10041 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010042
Ville Syrjälä282740f2013-09-04 18:30:03 +030010043 PIPE_CONF_CHECK_I(double_wide);
10044
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010045 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010046 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010047 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010048 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10049 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010050
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010051 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10052 PIPE_CONF_CHECK_I(pipe_bpp);
10053
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010054 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10055 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010056
Daniel Vetter66e985c2013-06-05 13:34:20 +020010057#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010058#undef PIPE_CONF_CHECK_I
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010059#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010060#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010061#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010062
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010063 return true;
10064}
10065
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010066static void
10067check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010068{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010069 struct intel_connector *connector;
10070
10071 list_for_each_entry(connector, &dev->mode_config.connector_list,
10072 base.head) {
10073 /* This also checks the encoder/connector hw state with the
10074 * ->get_hw_state callbacks. */
10075 intel_connector_check_state(connector);
10076
10077 WARN(&connector->new_encoder->base != connector->base.encoder,
10078 "connector's staged encoder doesn't match current encoder\n");
10079 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010080}
10081
10082static void
10083check_encoder_state(struct drm_device *dev)
10084{
10085 struct intel_encoder *encoder;
10086 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010087
10088 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10089 base.head) {
10090 bool enabled = false;
10091 bool active = false;
10092 enum pipe pipe, tracked_pipe;
10093
10094 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10095 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010096 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010097
10098 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10099 "encoder's stage crtc doesn't match current crtc\n");
10100 WARN(encoder->connectors_active && !encoder->base.crtc,
10101 "encoder's active_connectors set, but no crtc\n");
10102
10103 list_for_each_entry(connector, &dev->mode_config.connector_list,
10104 base.head) {
10105 if (connector->base.encoder != &encoder->base)
10106 continue;
10107 enabled = true;
10108 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10109 active = true;
10110 }
10111 WARN(!!encoder->base.crtc != enabled,
10112 "encoder's enabled state mismatch "
10113 "(expected %i, found %i)\n",
10114 !!encoder->base.crtc, enabled);
10115 WARN(active && !encoder->base.crtc,
10116 "active encoder with no crtc\n");
10117
10118 WARN(encoder->connectors_active != active,
10119 "encoder's computed active state doesn't match tracked active state "
10120 "(expected %i, found %i)\n", active, encoder->connectors_active);
10121
10122 active = encoder->get_hw_state(encoder, &pipe);
10123 WARN(active != encoder->connectors_active,
10124 "encoder's hw state doesn't match sw tracking "
10125 "(expected %i, found %i)\n",
10126 encoder->connectors_active, active);
10127
10128 if (!encoder->base.crtc)
10129 continue;
10130
10131 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10132 WARN(active && pipe != tracked_pipe,
10133 "active encoder's pipe doesn't match"
10134 "(expected %i, found %i)\n",
10135 tracked_pipe, pipe);
10136
10137 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010138}
10139
10140static void
10141check_crtc_state(struct drm_device *dev)
10142{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010143 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010144 struct intel_crtc *crtc;
10145 struct intel_encoder *encoder;
10146 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010147
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010148 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010149 bool enabled = false;
10150 bool active = false;
10151
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010152 memset(&pipe_config, 0, sizeof(pipe_config));
10153
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010154 DRM_DEBUG_KMS("[CRTC:%d]\n",
10155 crtc->base.base.id);
10156
10157 WARN(crtc->active && !crtc->base.enabled,
10158 "active crtc, but not enabled in sw tracking\n");
10159
10160 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10161 base.head) {
10162 if (encoder->base.crtc != &crtc->base)
10163 continue;
10164 enabled = true;
10165 if (encoder->connectors_active)
10166 active = true;
10167 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010168
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010169 WARN(active != crtc->active,
10170 "crtc's computed active state doesn't match tracked active state "
10171 "(expected %i, found %i)\n", active, crtc->active);
10172 WARN(enabled != crtc->base.enabled,
10173 "crtc's computed enabled state doesn't match tracked enabled state "
10174 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10175
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010176 active = dev_priv->display.get_pipe_config(crtc,
10177 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010178
10179 /* hw state is inconsistent with the pipe A quirk */
10180 if (crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE)
10181 active = crtc->active;
10182
Daniel Vetter6c49f242013-06-06 12:45:25 +020010183 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10184 base.head) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010185 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010186 if (encoder->base.crtc != &crtc->base)
10187 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010188 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010189 encoder->get_config(encoder, &pipe_config);
10190 }
10191
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010192 WARN(crtc->active != active,
10193 "crtc active state doesn't match with hw state "
10194 "(expected %i, found %i)\n", crtc->active, active);
10195
Daniel Vetterc0b03412013-05-28 12:05:54 +020010196 if (active &&
10197 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10198 WARN(1, "pipe state doesn't match!\n");
10199 intel_dump_pipe_config(crtc, &pipe_config,
10200 "[hw state]");
10201 intel_dump_pipe_config(crtc, &crtc->config,
10202 "[sw state]");
10203 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010204 }
10205}
10206
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010207static void
10208check_shared_dpll_state(struct drm_device *dev)
10209{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010210 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010211 struct intel_crtc *crtc;
10212 struct intel_dpll_hw_state dpll_hw_state;
10213 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010214
10215 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10216 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10217 int enabled_crtcs = 0, active_crtcs = 0;
10218 bool active;
10219
10220 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10221
10222 DRM_DEBUG_KMS("%s\n", pll->name);
10223
10224 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10225
10226 WARN(pll->active > pll->refcount,
10227 "more active pll users than references: %i vs %i\n",
10228 pll->active, pll->refcount);
10229 WARN(pll->active && !pll->on,
10230 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010231 WARN(pll->on && !pll->active,
10232 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010233 WARN(pll->on != active,
10234 "pll on state mismatch (expected %i, found %i)\n",
10235 pll->on, active);
10236
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010237 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010238 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10239 enabled_crtcs++;
10240 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10241 active_crtcs++;
10242 }
10243 WARN(pll->active != active_crtcs,
10244 "pll active crtcs mismatch (expected %i, found %i)\n",
10245 pll->active, active_crtcs);
10246 WARN(pll->refcount != enabled_crtcs,
10247 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10248 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010249
10250 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10251 sizeof(dpll_hw_state)),
10252 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010253 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010254}
10255
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010256void
10257intel_modeset_check_state(struct drm_device *dev)
10258{
10259 check_connector_state(dev);
10260 check_encoder_state(dev);
10261 check_crtc_state(dev);
10262 check_shared_dpll_state(dev);
10263}
10264
Ville Syrjälä18442d02013-09-13 16:00:08 +030010265void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10266 int dotclock)
10267{
10268 /*
10269 * FDI already provided one idea for the dotclock.
10270 * Yell if the encoder disagrees.
10271 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010272 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010273 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010274 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010275}
10276
Ville Syrjälä80715b22014-05-15 20:23:23 +030010277static void update_scanline_offset(struct intel_crtc *crtc)
10278{
10279 struct drm_device *dev = crtc->base.dev;
10280
10281 /*
10282 * The scanline counter increments at the leading edge of hsync.
10283 *
10284 * On most platforms it starts counting from vtotal-1 on the
10285 * first active line. That means the scanline counter value is
10286 * always one less than what we would expect. Ie. just after
10287 * start of vblank, which also occurs at start of hsync (on the
10288 * last active line), the scanline counter will read vblank_start-1.
10289 *
10290 * On gen2 the scanline counter starts counting from 1 instead
10291 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10292 * to keep the value positive), instead of adding one.
10293 *
10294 * On HSW+ the behaviour of the scanline counter depends on the output
10295 * type. For DP ports it behaves like most other platforms, but on HDMI
10296 * there's an extra 1 line difference. So we need to add two instead of
10297 * one to the value.
10298 */
10299 if (IS_GEN2(dev)) {
10300 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10301 int vtotal;
10302
10303 vtotal = mode->crtc_vtotal;
10304 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10305 vtotal /= 2;
10306
10307 crtc->scanline_offset = vtotal - 1;
10308 } else if (HAS_DDI(dev) &&
10309 intel_pipe_has_type(&crtc->base, INTEL_OUTPUT_HDMI)) {
10310 crtc->scanline_offset = 2;
10311 } else
10312 crtc->scanline_offset = 1;
10313}
10314
Daniel Vetterf30da182013-04-11 20:22:50 +020010315static int __intel_set_mode(struct drm_crtc *crtc,
10316 struct drm_display_mode *mode,
10317 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020010318{
10319 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010320 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010321 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010322 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010323 struct intel_crtc *intel_crtc;
10324 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010325 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020010326
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010327 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010328 if (!saved_mode)
10329 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020010330
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010331 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020010332 &prepare_pipes, &disable_pipes);
10333
Tim Gardner3ac18232012-12-07 07:54:26 -070010334 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010335
Daniel Vetter25c5b262012-07-08 22:08:04 +020010336 /* Hack: Because we don't (yet) support global modeset on multiple
10337 * crtcs, we don't keep track of the new mode for more than one crtc.
10338 * Hence simply check whether any bit is set in modeset_pipes in all the
10339 * pieces of code that are not yet converted to deal with mutliple crtcs
10340 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010341 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010342 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010343 if (IS_ERR(pipe_config)) {
10344 ret = PTR_ERR(pipe_config);
10345 pipe_config = NULL;
10346
Tim Gardner3ac18232012-12-07 07:54:26 -070010347 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010348 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010349 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10350 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010351 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020010352 }
10353
Jesse Barnes30a970c2013-11-04 13:48:12 -080010354 /*
10355 * See if the config requires any additional preparation, e.g.
10356 * to adjust global state with pipes off. We need to do this
10357 * here so we can get the modeset_pipe updated config for the new
10358 * mode set on this crtc. For other crtcs we need to use the
10359 * adjusted_mode bits in the crtc directly.
10360 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020010361 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020010362 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080010363
Ville Syrjäläc164f832013-11-05 22:34:12 +020010364 /* may have added more to prepare_pipes than we should */
10365 prepare_pipes &= ~disable_pipes;
10366 }
10367
Daniel Vetter460da9162013-03-27 00:44:51 +010010368 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10369 intel_crtc_disable(&intel_crtc->base);
10370
Daniel Vetterea9d7582012-07-10 10:42:52 +020010371 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10372 if (intel_crtc->base.enabled)
10373 dev_priv->display.crtc_disable(&intel_crtc->base);
10374 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010375
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020010376 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10377 * to set it here already despite that we pass it down the callchain.
10378 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010379 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020010380 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010381 /* mode_set/enable/disable functions rely on a correct pipe
10382 * config. */
10383 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010384 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020010385
10386 /*
10387 * Calculate and store various constants which
10388 * are later needed by vblank and swap-completion
10389 * timestamping. They are derived from true hwmode.
10390 */
10391 drm_calc_timestamping_constants(crtc,
10392 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010393 }
Daniel Vetter7758a112012-07-08 19:40:39 +020010394
Daniel Vetterea9d7582012-07-10 10:42:52 +020010395 /* Only after disabling all output pipelines that will be changed can we
10396 * update the the output configuration. */
10397 intel_modeset_update_state(dev, prepare_pipes);
10398
Daniel Vetter47fab732012-10-26 10:58:18 +020010399 if (dev_priv->display.modeset_global_resources)
10400 dev_priv->display.modeset_global_resources(dev);
10401
Daniel Vettera6778b32012-07-02 09:56:42 +020010402 /* Set up the DPLL and any encoders state that needs to adjust or depend
10403 * on the DPLL.
10404 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010405 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Daniel Vetter4c107942014-04-24 23:55:05 +020010406 struct drm_framebuffer *old_fb;
10407
10408 mutex_lock(&dev->struct_mutex);
10409 ret = intel_pin_and_fence_fb_obj(dev,
10410 to_intel_framebuffer(fb)->obj,
10411 NULL);
10412 if (ret != 0) {
10413 DRM_ERROR("pin & fence failed\n");
10414 mutex_unlock(&dev->struct_mutex);
10415 goto done;
10416 }
10417 old_fb = crtc->primary->fb;
10418 if (old_fb)
10419 intel_unpin_fb_obj(to_intel_framebuffer(old_fb)->obj);
10420 mutex_unlock(&dev->struct_mutex);
10421
10422 crtc->primary->fb = fb;
10423 crtc->x = x;
10424 crtc->y = y;
10425
Daniel Vetter4271b752014-04-24 23:55:00 +020010426 ret = dev_priv->display.crtc_mode_set(&intel_crtc->base,
10427 x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010428 if (ret)
10429 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020010430 }
10431
10432 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030010433 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10434 update_scanline_offset(intel_crtc);
10435
Daniel Vetter25c5b262012-07-08 22:08:04 +020010436 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030010437 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010438
Daniel Vettera6778b32012-07-02 09:56:42 +020010439 /* FIXME: add subpixel order */
10440done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010441 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070010442 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010443
Tim Gardner3ac18232012-12-07 07:54:26 -070010444out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010445 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070010446 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020010447 return ret;
10448}
10449
Damien Lespiaue7457a92013-08-08 22:28:59 +010010450static int intel_set_mode(struct drm_crtc *crtc,
10451 struct drm_display_mode *mode,
10452 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020010453{
10454 int ret;
10455
10456 ret = __intel_set_mode(crtc, mode, x, y, fb);
10457
10458 if (ret == 0)
10459 intel_modeset_check_state(crtc->dev);
10460
10461 return ret;
10462}
10463
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010464void intel_crtc_restore_mode(struct drm_crtc *crtc)
10465{
Matt Roperf4510a22014-04-01 15:22:40 -070010466 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010467}
10468
Daniel Vetter25c5b262012-07-08 22:08:04 +020010469#undef for_each_intel_crtc_masked
10470
Daniel Vetterd9e55602012-07-04 22:16:09 +020010471static void intel_set_config_free(struct intel_set_config *config)
10472{
10473 if (!config)
10474 return;
10475
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010476 kfree(config->save_connector_encoders);
10477 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020010478 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020010479 kfree(config);
10480}
10481
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010482static int intel_set_config_save_state(struct drm_device *dev,
10483 struct intel_set_config *config)
10484{
Ville Syrjälä76688512014-01-10 11:28:06 +020010485 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010486 struct drm_encoder *encoder;
10487 struct drm_connector *connector;
10488 int count;
10489
Ville Syrjälä76688512014-01-10 11:28:06 +020010490 config->save_crtc_enabled =
10491 kcalloc(dev->mode_config.num_crtc,
10492 sizeof(bool), GFP_KERNEL);
10493 if (!config->save_crtc_enabled)
10494 return -ENOMEM;
10495
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010496 config->save_encoder_crtcs =
10497 kcalloc(dev->mode_config.num_encoder,
10498 sizeof(struct drm_crtc *), GFP_KERNEL);
10499 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010500 return -ENOMEM;
10501
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010502 config->save_connector_encoders =
10503 kcalloc(dev->mode_config.num_connector,
10504 sizeof(struct drm_encoder *), GFP_KERNEL);
10505 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010506 return -ENOMEM;
10507
10508 /* Copy data. Note that driver private data is not affected.
10509 * Should anything bad happen only the expected state is
10510 * restored, not the drivers personal bookkeeping.
10511 */
10512 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010513 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010514 config->save_crtc_enabled[count++] = crtc->enabled;
10515 }
10516
10517 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010518 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010519 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010520 }
10521
10522 count = 0;
10523 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010524 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010525 }
10526
10527 return 0;
10528}
10529
10530static void intel_set_config_restore_state(struct drm_device *dev,
10531 struct intel_set_config *config)
10532{
Ville Syrjälä76688512014-01-10 11:28:06 +020010533 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010534 struct intel_encoder *encoder;
10535 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010536 int count;
10537
10538 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010539 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010540 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010541
10542 if (crtc->new_enabled)
10543 crtc->new_config = &crtc->config;
10544 else
10545 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010546 }
10547
10548 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010549 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10550 encoder->new_crtc =
10551 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010552 }
10553
10554 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010555 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10556 connector->new_encoder =
10557 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010558 }
10559}
10560
Imre Deake3de42b2013-05-03 19:44:07 +020010561static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010010562is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020010563{
10564 int i;
10565
Chris Wilson2e57f472013-07-17 12:14:40 +010010566 if (set->num_connectors == 0)
10567 return false;
10568
10569 if (WARN_ON(set->connectors == NULL))
10570 return false;
10571
10572 for (i = 0; i < set->num_connectors; i++)
10573 if (set->connectors[i]->encoder &&
10574 set->connectors[i]->encoder->crtc == set->crtc &&
10575 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020010576 return true;
10577
10578 return false;
10579}
10580
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010581static void
10582intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10583 struct intel_set_config *config)
10584{
10585
10586 /* We should be able to check here if the fb has the same properties
10587 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010010588 if (is_crtc_connector_off(set)) {
10589 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070010590 } else if (set->crtc->primary->fb != set->fb) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010591 /* If we have no fb then treat it as a full mode set */
Matt Roperf4510a22014-04-01 15:22:40 -070010592 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010593 struct intel_crtc *intel_crtc =
10594 to_intel_crtc(set->crtc);
10595
Jani Nikulad330a952014-01-21 11:24:25 +020010596 if (intel_crtc->active && i915.fastboot) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010597 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10598 config->fb_changed = true;
10599 } else {
10600 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10601 config->mode_changed = true;
10602 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010603 } else if (set->fb == NULL) {
10604 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010010605 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070010606 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010607 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010608 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010609 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010610 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010611 }
10612
Daniel Vetter835c5872012-07-10 18:11:08 +020010613 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010614 config->fb_changed = true;
10615
10616 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10617 DRM_DEBUG_KMS("modes are different, full mode set\n");
10618 drm_mode_debug_printmodeline(&set->crtc->mode);
10619 drm_mode_debug_printmodeline(set->mode);
10620 config->mode_changed = true;
10621 }
Chris Wilsona1d95702013-08-13 18:48:47 +010010622
10623 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10624 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010625}
10626
Daniel Vetter2e431052012-07-04 22:42:15 +020010627static int
Daniel Vetter9a935852012-07-05 22:34:27 +020010628intel_modeset_stage_output_state(struct drm_device *dev,
10629 struct drm_mode_set *set,
10630 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020010631{
Daniel Vetter9a935852012-07-05 22:34:27 +020010632 struct intel_connector *connector;
10633 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020010634 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030010635 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020010636
Damien Lespiau9abdda72013-02-13 13:29:23 +000010637 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020010638 * of connectors. For paranoia, double-check this. */
10639 WARN_ON(!set->fb && (set->num_connectors != 0));
10640 WARN_ON(set->fb && (set->num_connectors == 0));
10641
Daniel Vetter9a935852012-07-05 22:34:27 +020010642 list_for_each_entry(connector, &dev->mode_config.connector_list,
10643 base.head) {
10644 /* Otherwise traverse passed in connector list and get encoders
10645 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010646 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010647 if (set->connectors[ro] == &connector->base) {
10648 connector->new_encoder = connector->encoder;
Daniel Vetter50f56112012-07-02 09:35:43 +020010649 break;
10650 }
10651 }
10652
Daniel Vetter9a935852012-07-05 22:34:27 +020010653 /* If we disable the crtc, disable all its connectors. Also, if
10654 * the connector is on the changing crtc but not on the new
10655 * connector list, disable it. */
10656 if ((!set->fb || ro == set->num_connectors) &&
10657 connector->base.encoder &&
10658 connector->base.encoder->crtc == set->crtc) {
10659 connector->new_encoder = NULL;
10660
10661 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
10662 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010663 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020010664 }
10665
10666
10667 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010668 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010669 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010670 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010671 }
10672 /* connector->new_encoder is now updated for all connectors. */
10673
10674 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020010675 list_for_each_entry(connector, &dev->mode_config.connector_list,
10676 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010677 struct drm_crtc *new_crtc;
10678
Daniel Vetter9a935852012-07-05 22:34:27 +020010679 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020010680 continue;
10681
Daniel Vetter9a935852012-07-05 22:34:27 +020010682 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020010683
10684 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010685 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020010686 new_crtc = set->crtc;
10687 }
10688
10689 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010010690 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
10691 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010692 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020010693 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010694 connector->encoder->new_crtc = to_intel_crtc(new_crtc);
10695
10696 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
10697 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010698 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020010699 new_crtc->base.id);
10700 }
10701
10702 /* Check for any encoders that needs to be disabled. */
10703 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
10704 base.head) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010705 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010706 list_for_each_entry(connector,
10707 &dev->mode_config.connector_list,
10708 base.head) {
10709 if (connector->new_encoder == encoder) {
10710 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010711 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020010712 }
10713 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020010714
10715 if (num_connectors == 0)
10716 encoder->new_crtc = NULL;
10717 else if (num_connectors > 1)
10718 return -EINVAL;
10719
Daniel Vetter9a935852012-07-05 22:34:27 +020010720 /* Only now check for crtc changes so we don't miss encoders
10721 * that will be disabled. */
10722 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020010723 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010724 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020010725 }
10726 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010727 /* Now we've also updated encoder->new_crtc for all encoders. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010728
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010729 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010730 crtc->new_enabled = false;
10731
10732 list_for_each_entry(encoder,
10733 &dev->mode_config.encoder_list,
10734 base.head) {
10735 if (encoder->new_crtc == crtc) {
10736 crtc->new_enabled = true;
10737 break;
10738 }
10739 }
10740
10741 if (crtc->new_enabled != crtc->base.enabled) {
10742 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
10743 crtc->new_enabled ? "en" : "dis");
10744 config->mode_changed = true;
10745 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010746
10747 if (crtc->new_enabled)
10748 crtc->new_config = &crtc->config;
10749 else
10750 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010751 }
10752
Daniel Vetter2e431052012-07-04 22:42:15 +020010753 return 0;
10754}
10755
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010756static void disable_crtc_nofb(struct intel_crtc *crtc)
10757{
10758 struct drm_device *dev = crtc->base.dev;
10759 struct intel_encoder *encoder;
10760 struct intel_connector *connector;
10761
10762 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
10763 pipe_name(crtc->pipe));
10764
10765 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10766 if (connector->new_encoder &&
10767 connector->new_encoder->new_crtc == crtc)
10768 connector->new_encoder = NULL;
10769 }
10770
10771 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
10772 if (encoder->new_crtc == crtc)
10773 encoder->new_crtc = NULL;
10774 }
10775
10776 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010777 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010778}
10779
Daniel Vetter2e431052012-07-04 22:42:15 +020010780static int intel_crtc_set_config(struct drm_mode_set *set)
10781{
10782 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020010783 struct drm_mode_set save_set;
10784 struct intel_set_config *config;
10785 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020010786
Daniel Vetter8d3e3752012-07-05 16:09:09 +020010787 BUG_ON(!set);
10788 BUG_ON(!set->crtc);
10789 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020010790
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010010791 /* Enforce sane interface api - has been abused by the fb helper. */
10792 BUG_ON(!set->mode && set->fb);
10793 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020010794
Daniel Vetter2e431052012-07-04 22:42:15 +020010795 if (set->fb) {
10796 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
10797 set->crtc->base.id, set->fb->base.id,
10798 (int)set->num_connectors, set->x, set->y);
10799 } else {
10800 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020010801 }
10802
10803 dev = set->crtc->dev;
10804
10805 ret = -ENOMEM;
10806 config = kzalloc(sizeof(*config), GFP_KERNEL);
10807 if (!config)
10808 goto out_config;
10809
10810 ret = intel_set_config_save_state(dev, config);
10811 if (ret)
10812 goto out_config;
10813
10814 save_set.crtc = set->crtc;
10815 save_set.mode = &set->crtc->mode;
10816 save_set.x = set->crtc->x;
10817 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070010818 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020010819
10820 /* Compute whether we need a full modeset, only an fb base update or no
10821 * change at all. In the future we might also check whether only the
10822 * mode changed, e.g. for LVDS where we only change the panel fitter in
10823 * such cases. */
10824 intel_set_config_compute_mode_changes(set, config);
10825
Daniel Vetter9a935852012-07-05 22:34:27 +020010826 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020010827 if (ret)
10828 goto fail;
10829
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010830 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010831 ret = intel_set_mode(set->crtc, set->mode,
10832 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010833 } else if (config->fb_changed) {
Ville Syrjälä4878cae2013-02-18 19:08:48 +020010834 intel_crtc_wait_for_pending_flips(set->crtc);
10835
Daniel Vetter4f660f42012-07-02 09:47:37 +020010836 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020010837 set->x, set->y, set->fb);
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010838 /*
10839 * In the fastboot case this may be our only check of the
10840 * state after boot. It would be better to only do it on
10841 * the first update, but we don't have a nice way of doing that
10842 * (and really, set_config isn't used much for high freq page
10843 * flipping, so increasing its cost here shouldn't be a big
10844 * deal).
10845 */
Jani Nikulad330a952014-01-21 11:24:25 +020010846 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080010847 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020010848 }
10849
Chris Wilson2d05eae2013-05-03 17:36:25 +010010850 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020010851 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
10852 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020010853fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010010854 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010855
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020010856 /*
10857 * HACK: if the pipe was on, but we didn't have a framebuffer,
10858 * force the pipe off to avoid oopsing in the modeset code
10859 * due to fb==NULL. This should only happen during boot since
10860 * we don't yet reconstruct the FB from the hardware state.
10861 */
10862 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
10863 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
10864
Chris Wilson2d05eae2013-05-03 17:36:25 +010010865 /* Try to restore the config */
10866 if (config->mode_changed &&
10867 intel_set_mode(save_set.crtc, save_set.mode,
10868 save_set.x, save_set.y, save_set.fb))
10869 DRM_ERROR("failed to restore config after modeset failure\n");
10870 }
Daniel Vetter50f56112012-07-02 09:35:43 +020010871
Daniel Vetterd9e55602012-07-04 22:16:09 +020010872out_config:
10873 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020010874 return ret;
10875}
10876
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010877static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010878 .cursor_set = intel_crtc_cursor_set,
10879 .cursor_move = intel_crtc_cursor_move,
10880 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020010881 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010882 .destroy = intel_crtc_destroy,
10883 .page_flip = intel_crtc_page_flip,
10884};
10885
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010886static void intel_cpu_pll_init(struct drm_device *dev)
10887{
Paulo Zanoniaffa9352012-11-23 15:30:39 -020010888 if (HAS_DDI(dev))
Paulo Zanoni79f689a2012-10-05 12:05:52 -030010889 intel_ddi_pll_init(dev);
10890}
10891
Daniel Vetter53589012013-06-05 13:34:16 +020010892static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
10893 struct intel_shared_dpll *pll,
10894 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010895{
Daniel Vetter53589012013-06-05 13:34:16 +020010896 uint32_t val;
10897
10898 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020010899 hw_state->dpll = val;
10900 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
10901 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020010902
10903 return val & DPLL_VCO_ENABLE;
10904}
10905
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010906static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
10907 struct intel_shared_dpll *pll)
10908{
10909 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
10910 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
10911}
10912
Daniel Vettere7b903d2013-06-05 13:34:14 +020010913static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
10914 struct intel_shared_dpll *pll)
10915{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010916 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020010917 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020010918
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010919 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10920
10921 /* Wait for the clocks to stabilize. */
10922 POSTING_READ(PCH_DPLL(pll->id));
10923 udelay(150);
10924
10925 /* The pixel multiplier can only be updated once the
10926 * DPLL is enabled and the clocks are stable.
10927 *
10928 * So write it again.
10929 */
10930 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
10931 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010932 udelay(200);
10933}
10934
10935static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
10936 struct intel_shared_dpll *pll)
10937{
10938 struct drm_device *dev = dev_priv->dev;
10939 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010940
10941 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010942 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020010943 if (intel_crtc_to_shared_dpll(crtc) == pll)
10944 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
10945 }
10946
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010947 I915_WRITE(PCH_DPLL(pll->id), 0);
10948 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020010949 udelay(200);
10950}
10951
Daniel Vetter46edb022013-06-05 13:34:12 +020010952static char *ibx_pch_dpll_names[] = {
10953 "PCH DPLL A",
10954 "PCH DPLL B",
10955};
10956
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010957static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010958{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010959 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010960 int i;
10961
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010962 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010963
Daniel Vettere72f9fb2013-06-05 13:34:06 +020010964 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020010965 dev_priv->shared_dplls[i].id = i;
10966 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020010967 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020010968 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
10969 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020010970 dev_priv->shared_dplls[i].get_hw_state =
10971 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010010972 }
10973}
10974
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010975static void intel_shared_dpll_init(struct drm_device *dev)
10976{
Daniel Vettere7b903d2013-06-05 13:34:14 +020010977 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010978
10979 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
10980 ibx_pch_dpll_init(dev);
10981 else
10982 dev_priv->num_shared_dpll = 0;
10983
10984 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020010985}
10986
Hannes Ederb358d0a2008-12-18 21:18:47 +010010987static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080010988{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010989 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010990 struct intel_crtc *intel_crtc;
10991 int i;
10992
Daniel Vetter955382f2013-09-19 14:05:45 +020010993 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010994 if (intel_crtc == NULL)
10995 return;
10996
10997 drm_crtc_init(dev, &intel_crtc->base, &intel_crtc_funcs);
10998
10999 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080011000 for (i = 0; i < 256; i++) {
11001 intel_crtc->lut_r[i] = i;
11002 intel_crtc->lut_g[i] = i;
11003 intel_crtc->lut_b[i] = i;
11004 }
11005
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011006 /*
11007 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
11008 * is hooked to plane B. Hence we want plane A feeding pipe B.
11009 */
Jesse Barnes80824002009-09-10 15:28:06 -070011010 intel_crtc->pipe = pipe;
11011 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010011012 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080011013 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010011014 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070011015 }
11016
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030011017 init_waitqueue_head(&intel_crtc->vbl_wait);
11018
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080011019 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11020 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11021 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11022 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11023
Jesse Barnes79e53942008-11-07 14:24:08 -080011024 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020011025
11026 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Jesse Barnes79e53942008-11-07 14:24:08 -080011027}
11028
Jesse Barnes752aa882013-10-31 18:55:49 +020011029enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11030{
11031 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011032 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020011033
Rob Clark51fd3712013-11-19 12:10:12 -050011034 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020011035
11036 if (!encoder)
11037 return INVALID_PIPE;
11038
11039 return to_intel_crtc(encoder->crtc)->pipe;
11040}
11041
Carl Worth08d7b3d2009-04-29 14:43:54 -070011042int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000011043 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070011044{
Carl Worth08d7b3d2009-04-29 14:43:54 -070011045 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Daniel Vetterc05422d2009-08-11 16:05:30 +020011046 struct drm_mode_object *drmmode_obj;
11047 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011048
Daniel Vetter1cff8f62012-04-24 09:55:08 +020011049 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11050 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011051
Daniel Vetterc05422d2009-08-11 16:05:30 +020011052 drmmode_obj = drm_mode_object_find(dev, pipe_from_crtc_id->crtc_id,
11053 DRM_MODE_OBJECT_CRTC);
Carl Worth08d7b3d2009-04-29 14:43:54 -070011054
Daniel Vetterc05422d2009-08-11 16:05:30 +020011055 if (!drmmode_obj) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070011056 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030011057 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011058 }
11059
Daniel Vetterc05422d2009-08-11 16:05:30 +020011060 crtc = to_intel_crtc(obj_to_crtc(drmmode_obj));
11061 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011062
Daniel Vetterc05422d2009-08-11 16:05:30 +020011063 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011064}
11065
Daniel Vetter66a92782012-07-12 20:08:18 +020011066static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080011067{
Daniel Vetter66a92782012-07-12 20:08:18 +020011068 struct drm_device *dev = encoder->base.dev;
11069 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011070 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011071 int entry = 0;
11072
Daniel Vetter66a92782012-07-12 20:08:18 +020011073 list_for_each_entry(source_encoder,
11074 &dev->mode_config.encoder_list, base.head) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011075 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020011076 index_mask |= (1 << entry);
11077
Jesse Barnes79e53942008-11-07 14:24:08 -080011078 entry++;
11079 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010011080
Jesse Barnes79e53942008-11-07 14:24:08 -080011081 return index_mask;
11082}
11083
Chris Wilson4d302442010-12-14 19:21:29 +000011084static bool has_edp_a(struct drm_device *dev)
11085{
11086 struct drm_i915_private *dev_priv = dev->dev_private;
11087
11088 if (!IS_MOBILE(dev))
11089 return false;
11090
11091 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11092 return false;
11093
Damien Lespiaue3589902014-02-07 19:12:50 +000011094 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000011095 return false;
11096
11097 return true;
11098}
11099
Damien Lespiauba0fbca2014-01-08 14:18:23 +000011100const char *intel_output_name(int output)
11101{
11102 static const char *names[] = {
11103 [INTEL_OUTPUT_UNUSED] = "Unused",
11104 [INTEL_OUTPUT_ANALOG] = "Analog",
11105 [INTEL_OUTPUT_DVO] = "DVO",
11106 [INTEL_OUTPUT_SDVO] = "SDVO",
11107 [INTEL_OUTPUT_LVDS] = "LVDS",
11108 [INTEL_OUTPUT_TVOUT] = "TV",
11109 [INTEL_OUTPUT_HDMI] = "HDMI",
11110 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11111 [INTEL_OUTPUT_EDP] = "eDP",
11112 [INTEL_OUTPUT_DSI] = "DSI",
11113 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11114 };
11115
11116 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11117 return "Invalid";
11118
11119 return names[output];
11120}
11121
Jesse Barnes79e53942008-11-07 14:24:08 -080011122static void intel_setup_outputs(struct drm_device *dev)
11123{
Eric Anholt725e30a2009-01-22 13:01:02 -080011124 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011125 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011126 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011127
Daniel Vetterc9093352013-06-06 22:22:47 +020011128 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011129
Ville Syrjälä7895a812014-04-09 13:28:23 +030011130 if (!IS_ULT(dev) && !IS_CHERRYVIEW(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020011131 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011132
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011133 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030011134 int found;
11135
11136 /* Haswell uses DDI functions to detect digital outputs */
11137 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
11138 /* DDI A only supports eDP */
11139 if (found)
11140 intel_ddi_init(dev, PORT_A);
11141
11142 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
11143 * register */
11144 found = I915_READ(SFUSE_STRAP);
11145
11146 if (found & SFUSE_STRAP_DDIB_DETECTED)
11147 intel_ddi_init(dev, PORT_B);
11148 if (found & SFUSE_STRAP_DDIC_DETECTED)
11149 intel_ddi_init(dev, PORT_C);
11150 if (found & SFUSE_STRAP_DDID_DETECTED)
11151 intel_ddi_init(dev, PORT_D);
11152 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011153 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011154 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020011155
11156 if (has_edp_a(dev))
11157 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011158
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011159 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080011160 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010011161 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011162 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011163 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011164 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011165 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011166 }
11167
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011168 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011169 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011170
Paulo Zanonidc0fa712013-02-19 16:21:46 -030011171 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030011172 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080011173
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011174 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011175 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080011176
Daniel Vetter270b3042012-10-27 15:52:05 +020011177 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011178 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070011179 } else if (IS_VALLEYVIEW(dev)) {
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030011180 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED) {
11181 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
11182 PORT_B);
11183 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED)
11184 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
11185 }
11186
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011187 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED) {
11188 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
11189 PORT_C);
11190 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED)
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020011191 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Jesse Barnes6f6005a2013-08-09 09:34:35 -070011192 }
Gajanan Bhat19c03922012-09-27 19:13:07 +053011193
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030011194 if (IS_CHERRYVIEW(dev)) {
11195 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED) {
11196 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
11197 PORT_D);
11198 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
11199 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
11200 }
11201 }
11202
Jani Nikula3cfca972013-08-27 15:12:26 +030011203 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080011204 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011205 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080011206
Paulo Zanonie2debe92013-02-18 19:00:27 -030011207 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011208 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011209 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011210 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
11211 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011212 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011213 }
Ma Ling27185ae2009-08-24 13:50:23 +080011214
Imre Deake7281ea2013-05-08 13:14:08 +030011215 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011216 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080011217 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011218
11219 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040011220
Paulo Zanonie2debe92013-02-18 19:00:27 -030011221 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011222 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011223 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011224 }
Ma Ling27185ae2009-08-24 13:50:23 +080011225
Paulo Zanonie2debe92013-02-18 19:00:27 -030011226 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080011227
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011228 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
11229 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030011230 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011231 }
Imre Deake7281ea2013-05-08 13:14:08 +030011232 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011233 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080011234 }
Ma Ling27185ae2009-08-24 13:50:23 +080011235
Jesse Barnesb01f2c32009-12-11 11:07:17 -080011236 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030011237 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030011238 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070011239 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011240 intel_dvo_init(dev);
11241
Zhenyu Wang103a1962009-11-27 11:44:36 +080011242 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080011243 intel_tv_init(dev);
11244
Chris Wilson4ef69c72010-09-09 15:14:28 +010011245 list_for_each_entry(encoder, &dev->mode_config.encoder_list, base.head) {
11246 encoder->base.possible_crtcs = encoder->crtc_mask;
11247 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020011248 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080011249 }
Chris Wilson47356eb2011-01-11 17:06:04 +000011250
Paulo Zanonidde86e22012-12-01 12:04:25 -020011251 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020011252
11253 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011254}
11255
11256static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11257{
11258 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080011259
Daniel Vetteref2d6332014-02-10 18:00:38 +010011260 drm_framebuffer_cleanup(fb);
11261 WARN_ON(!intel_fb->obj->framebuffer_references--);
11262 drm_gem_object_unreference_unlocked(&intel_fb->obj->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080011263 kfree(intel_fb);
11264}
11265
11266static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000011267 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080011268 unsigned int *handle)
11269{
11270 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011271 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011272
Chris Wilson05394f32010-11-08 19:18:58 +000011273 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080011274}
11275
11276static const struct drm_framebuffer_funcs intel_fb_funcs = {
11277 .destroy = intel_user_framebuffer_destroy,
11278 .create_handle = intel_user_framebuffer_create_handle,
11279};
11280
Daniel Vetterb5ea6422014-03-02 21:18:00 +010011281static int intel_framebuffer_init(struct drm_device *dev,
11282 struct intel_framebuffer *intel_fb,
11283 struct drm_mode_fb_cmd2 *mode_cmd,
11284 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080011285{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011286 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011287 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080011288 int ret;
11289
Daniel Vetterdd4916c2013-10-09 21:23:51 +020011290 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
11291
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011292 if (obj->tiling_mode == I915_TILING_Y) {
11293 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010011294 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011295 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011296
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011297 if (mode_cmd->pitches[0] & 63) {
11298 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
11299 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010011300 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011301 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011302
Chris Wilsona35cdaa2013-06-25 17:26:45 +010011303 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
11304 pitch_limit = 32*1024;
11305 } else if (INTEL_INFO(dev)->gen >= 4) {
11306 if (obj->tiling_mode)
11307 pitch_limit = 16*1024;
11308 else
11309 pitch_limit = 32*1024;
11310 } else if (INTEL_INFO(dev)->gen >= 3) {
11311 if (obj->tiling_mode)
11312 pitch_limit = 8*1024;
11313 else
11314 pitch_limit = 16*1024;
11315 } else
11316 /* XXX DSPC is limited to 4k tiled */
11317 pitch_limit = 8*1024;
11318
11319 if (mode_cmd->pitches[0] > pitch_limit) {
11320 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
11321 obj->tiling_mode ? "tiled" : "linear",
11322 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011323 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011324 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011325
11326 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011327 mode_cmd->pitches[0] != obj->stride) {
11328 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
11329 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011330 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011331 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020011332
Ville Syrjälä57779d02012-10-31 17:50:14 +020011333 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011334 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020011335 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011336 case DRM_FORMAT_RGB565:
11337 case DRM_FORMAT_XRGB8888:
11338 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011339 break;
11340 case DRM_FORMAT_XRGB1555:
11341 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011342 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011343 DRM_DEBUG("unsupported pixel format: %s\n",
11344 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011345 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011346 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020011347 break;
11348 case DRM_FORMAT_XBGR8888:
11349 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020011350 case DRM_FORMAT_XRGB2101010:
11351 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020011352 case DRM_FORMAT_XBGR2101010:
11353 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011354 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011355 DRM_DEBUG("unsupported pixel format: %s\n",
11356 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011357 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011358 }
Jesse Barnesb5626742011-06-24 12:19:27 -070011359 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020011360 case DRM_FORMAT_YUYV:
11361 case DRM_FORMAT_UYVY:
11362 case DRM_FORMAT_YVYU:
11363 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011364 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011365 DRM_DEBUG("unsupported pixel format: %s\n",
11366 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020011367 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000011368 }
Chris Wilson57cd6502010-08-08 12:34:44 +010011369 break;
11370 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000011371 DRM_DEBUG("unsupported pixel format: %s\n",
11372 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010011373 return -EINVAL;
11374 }
11375
Ville Syrjälä90f9a332012-10-31 17:50:19 +020011376 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
11377 if (mode_cmd->offsets[0] != 0)
11378 return -EINVAL;
11379
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080011380 aligned_height = intel_align_height(dev, mode_cmd->height,
11381 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020011382 /* FIXME drm helper for size checks (especially planar formats)? */
11383 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
11384 return -EINVAL;
11385
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011386 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
11387 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020011388 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010011389
Jesse Barnes79e53942008-11-07 14:24:08 -080011390 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
11391 if (ret) {
11392 DRM_ERROR("framebuffer init failed %d\n", ret);
11393 return ret;
11394 }
11395
Jesse Barnes79e53942008-11-07 14:24:08 -080011396 return 0;
11397}
11398
Jesse Barnes79e53942008-11-07 14:24:08 -080011399static struct drm_framebuffer *
11400intel_user_framebuffer_create(struct drm_device *dev,
11401 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011402 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080011403{
Chris Wilson05394f32010-11-08 19:18:58 +000011404 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080011405
Jesse Barnes308e5bc2011-11-14 14:51:28 -080011406 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
11407 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000011408 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010011409 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080011410
Chris Wilsond2dff872011-04-19 08:36:26 +010011411 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080011412}
11413
Daniel Vetter4520f532013-10-09 09:18:51 +020011414#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020011415static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020011416{
11417}
11418#endif
11419
Jesse Barnes79e53942008-11-07 14:24:08 -080011420static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080011421 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020011422 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080011423};
11424
Jesse Barnese70236a2009-09-21 10:42:27 -070011425/* Set up chip specific display functions */
11426static void intel_init_display(struct drm_device *dev)
11427{
11428 struct drm_i915_private *dev_priv = dev->dev_private;
11429
Daniel Vetteree9300b2013-06-03 22:40:22 +020011430 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
11431 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030011432 else if (IS_CHERRYVIEW(dev))
11433 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020011434 else if (IS_VALLEYVIEW(dev))
11435 dev_priv->display.find_dpll = vlv_find_best_dpll;
11436 else if (IS_PINEVIEW(dev))
11437 dev_priv->display.find_dpll = pnv_find_best_dpll;
11438 else
11439 dev_priv->display.find_dpll = i9xx_find_best_dpll;
11440
Paulo Zanoniaffa9352012-11-23 15:30:39 -020011441 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011442 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011443 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011444 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020011445 dev_priv->display.crtc_enable = haswell_crtc_enable;
11446 dev_priv->display.crtc_disable = haswell_crtc_disable;
Paulo Zanoni6441ab52012-10-05 12:05:58 -030011447 dev_priv->display.off = haswell_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011448 dev_priv->display.update_primary_plane =
11449 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030011450 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011451 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080011452 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011453 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011454 dev_priv->display.crtc_enable = ironlake_crtc_enable;
11455 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011456 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011457 dev_priv->display.update_primary_plane =
11458 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011459 } else if (IS_VALLEYVIEW(dev)) {
11460 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011461 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070011462 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
11463 dev_priv->display.crtc_enable = valleyview_crtc_enable;
11464 dev_priv->display.crtc_disable = i9xx_crtc_disable;
11465 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011466 dev_priv->display.update_primary_plane =
11467 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011468 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011469 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080011470 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070011471 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020011472 dev_priv->display.crtc_enable = i9xx_crtc_enable;
11473 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011474 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070011475 dev_priv->display.update_primary_plane =
11476 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070011477 }
Jesse Barnese70236a2009-09-21 10:42:27 -070011478
Jesse Barnese70236a2009-09-21 10:42:27 -070011479 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070011480 if (IS_VALLEYVIEW(dev))
11481 dev_priv->display.get_display_clock_speed =
11482 valleyview_get_display_clock_speed;
11483 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070011484 dev_priv->display.get_display_clock_speed =
11485 i945_get_display_clock_speed;
11486 else if (IS_I915G(dev))
11487 dev_priv->display.get_display_clock_speed =
11488 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011489 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011490 dev_priv->display.get_display_clock_speed =
11491 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020011492 else if (IS_PINEVIEW(dev))
11493 dev_priv->display.get_display_clock_speed =
11494 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070011495 else if (IS_I915GM(dev))
11496 dev_priv->display.get_display_clock_speed =
11497 i915gm_get_display_clock_speed;
11498 else if (IS_I865G(dev))
11499 dev_priv->display.get_display_clock_speed =
11500 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020011501 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070011502 dev_priv->display.get_display_clock_speed =
11503 i855_get_display_clock_speed;
11504 else /* 852, 830 */
11505 dev_priv->display.get_display_clock_speed =
11506 i830_get_display_clock_speed;
11507
Zhenyu Wang7f8a8562010-04-01 13:07:53 +080011508 if (HAS_PCH_SPLIT(dev)) {
Chris Wilsonf00a3dd2010-10-21 14:57:17 +010011509 if (IS_GEN5(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011510 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011511 dev_priv->display.write_eld = ironlake_write_eld;
Yuanhan Liu13982612010-12-15 15:42:31 +080011512 } else if (IS_GEN6(dev)) {
Jesse Barnes674cf962011-04-28 14:27:04 -070011513 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011514 dev_priv->display.write_eld = ironlake_write_eld;
Paulo Zanoni9a952a02014-03-07 20:12:34 -030011515 dev_priv->display.modeset_global_resources =
11516 snb_modeset_global_resources;
Jesse Barnes357555c2011-04-28 15:09:55 -070011517 } else if (IS_IVYBRIDGE(dev)) {
11518 /* FIXME: detect B0+ stepping and use auto training */
11519 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Wu Fengguange0dac652011-09-05 14:25:34 +080011520 dev_priv->display.write_eld = ironlake_write_eld;
Daniel Vetter01a415f2012-10-27 15:58:40 +020011521 dev_priv->display.modeset_global_resources =
11522 ivb_modeset_global_resources;
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011523 } else if (IS_HASWELL(dev) || IS_GEN8(dev)) {
Eugeni Dodonovc82e4d22012-05-09 15:37:21 -030011524 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Wang Xingchao83358c852012-08-16 22:43:37 +080011525 dev_priv->display.write_eld = haswell_write_eld;
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -020011526 dev_priv->display.modeset_global_resources =
11527 haswell_modeset_global_resources;
Paulo Zanonia0e63c22012-12-06 11:12:39 -020011528 }
Jesse Barnes6067aae2011-04-28 15:04:31 -070011529 } else if (IS_G4X(dev)) {
Wu Fengguange0dac652011-09-05 14:25:34 +080011530 dev_priv->display.write_eld = g4x_write_eld;
Jesse Barnes30a970c2013-11-04 13:48:12 -080011531 } else if (IS_VALLEYVIEW(dev)) {
11532 dev_priv->display.modeset_global_resources =
11533 valleyview_modeset_global_resources;
Mengdong Lin9ca2fe72013-11-01 00:17:03 -040011534 dev_priv->display.write_eld = ironlake_write_eld;
Jesse Barnese70236a2009-09-21 10:42:27 -070011535 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011536
11537 /* Default just returns -ENODEV to indicate unsupported */
11538 dev_priv->display.queue_flip = intel_default_queue_flip;
11539
11540 switch (INTEL_INFO(dev)->gen) {
11541 case 2:
11542 dev_priv->display.queue_flip = intel_gen2_queue_flip;
11543 break;
11544
11545 case 3:
11546 dev_priv->display.queue_flip = intel_gen3_queue_flip;
11547 break;
11548
11549 case 4:
11550 case 5:
11551 dev_priv->display.queue_flip = intel_gen4_queue_flip;
11552 break;
11553
11554 case 6:
11555 dev_priv->display.queue_flip = intel_gen6_queue_flip;
11556 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011557 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070011558 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011559 dev_priv->display.queue_flip = intel_gen7_queue_flip;
11560 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011561 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020011562
11563 intel_panel_init_backlight_funcs(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070011564}
11565
Jesse Barnesb690e962010-07-19 13:53:12 -070011566/*
11567 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
11568 * resume, or other times. This quirk makes sure that's the case for
11569 * affected systems.
11570 */
Akshay Joshi0206e352011-08-16 15:34:10 -040011571static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070011572{
11573 struct drm_i915_private *dev_priv = dev->dev_private;
11574
11575 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011576 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011577}
11578
Keith Packard435793d2011-07-12 14:56:22 -070011579/*
11580 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
11581 */
11582static void quirk_ssc_force_disable(struct drm_device *dev)
11583{
11584 struct drm_i915_private *dev_priv = dev->dev_private;
11585 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011586 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070011587}
11588
Carsten Emde4dca20e2012-03-15 15:56:26 +010011589/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010011590 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
11591 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010011592 */
11593static void quirk_invert_brightness(struct drm_device *dev)
11594{
11595 struct drm_i915_private *dev_priv = dev->dev_private;
11596 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020011597 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070011598}
11599
11600struct intel_quirk {
11601 int device;
11602 int subsystem_vendor;
11603 int subsystem_device;
11604 void (*hook)(struct drm_device *dev);
11605};
11606
Egbert Eich5f85f172012-10-14 15:46:38 +020011607/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
11608struct intel_dmi_quirk {
11609 void (*hook)(struct drm_device *dev);
11610 const struct dmi_system_id (*dmi_id_list)[];
11611};
11612
11613static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
11614{
11615 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
11616 return 1;
11617}
11618
11619static const struct intel_dmi_quirk intel_dmi_quirks[] = {
11620 {
11621 .dmi_id_list = &(const struct dmi_system_id[]) {
11622 {
11623 .callback = intel_dmi_reverse_brightness,
11624 .ident = "NCR Corporation",
11625 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
11626 DMI_MATCH(DMI_PRODUCT_NAME, ""),
11627 },
11628 },
11629 { } /* terminating entry */
11630 },
11631 .hook = quirk_invert_brightness,
11632 },
11633};
11634
Ben Widawskyc43b5632012-04-16 14:07:40 -070011635static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070011636 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040011637 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070011638
Jesse Barnesb690e962010-07-19 13:53:12 -070011639 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
11640 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
11641
Jesse Barnesb690e962010-07-19 13:53:12 -070011642 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
11643 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
11644
Chris Wilsona4945f92013-10-08 11:16:59 +010011645 /* 830 needs to leave pipe A & dpll A up */
Daniel Vetterdcdaed62012-08-12 21:19:34 +020011646 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
Keith Packard435793d2011-07-12 14:56:22 -070011647
11648 /* Lenovo U160 cannot use SSC on LVDS */
11649 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020011650
11651 /* Sony Vaio Y cannot use SSC on LVDS */
11652 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010011653
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010011654 /* Acer Aspire 5734Z must invert backlight brightness */
11655 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
11656
11657 /* Acer/eMachines G725 */
11658 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
11659
11660 /* Acer/eMachines e725 */
11661 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
11662
11663 /* Acer/Packard Bell NCL20 */
11664 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
11665
11666 /* Acer Aspire 4736Z */
11667 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020011668
11669 /* Acer Aspire 5336 */
11670 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Jesse Barnesb690e962010-07-19 13:53:12 -070011671};
11672
11673static void intel_init_quirks(struct drm_device *dev)
11674{
11675 struct pci_dev *d = dev->pdev;
11676 int i;
11677
11678 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
11679 struct intel_quirk *q = &intel_quirks[i];
11680
11681 if (d->device == q->device &&
11682 (d->subsystem_vendor == q->subsystem_vendor ||
11683 q->subsystem_vendor == PCI_ANY_ID) &&
11684 (d->subsystem_device == q->subsystem_device ||
11685 q->subsystem_device == PCI_ANY_ID))
11686 q->hook(dev);
11687 }
Egbert Eich5f85f172012-10-14 15:46:38 +020011688 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
11689 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
11690 intel_dmi_quirks[i].hook(dev);
11691 }
Jesse Barnesb690e962010-07-19 13:53:12 -070011692}
11693
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011694/* Disable the VGA plane that we never use */
11695static void i915_disable_vga(struct drm_device *dev)
11696{
11697 struct drm_i915_private *dev_priv = dev->dev_private;
11698 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020011699 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011700
Ville Syrjälä2b37c612014-01-22 21:32:38 +020011701 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011702 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070011703 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011704 sr1 = inb(VGA_SR_DATA);
11705 outb(sr1 | 1<<5, VGA_SR_DATA);
11706 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
11707 udelay(300);
11708
11709 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
11710 POSTING_READ(vga_reg);
11711}
11712
Daniel Vetterf8175862012-04-10 15:50:11 +020011713void intel_modeset_init_hw(struct drm_device *dev)
11714{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030011715 intel_prepare_ddi(dev);
11716
Daniel Vetterf8175862012-04-10 15:50:11 +020011717 intel_init_clock_gating(dev);
11718
Jesse Barnes5382f5f352013-12-16 16:34:24 -080011719 intel_reset_dpio(dev);
Jesse Barnes40e9cf62013-10-03 11:35:46 -070011720
Daniel Vetter8090c6b2012-06-24 16:42:32 +020011721 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020011722}
11723
Imre Deak7d708ee2013-04-17 14:04:50 +030011724void intel_modeset_suspend_hw(struct drm_device *dev)
11725{
11726 intel_suspend_hw(dev);
11727}
11728
Jesse Barnes79e53942008-11-07 14:24:08 -080011729void intel_modeset_init(struct drm_device *dev)
11730{
Jesse Barnes652c3932009-08-17 13:31:43 -070011731 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000011732 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000011733 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080011734 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080011735
11736 drm_mode_config_init(dev);
11737
11738 dev->mode_config.min_width = 0;
11739 dev->mode_config.min_height = 0;
11740
Dave Airlie019d96c2011-09-29 16:20:42 +010011741 dev->mode_config.preferred_depth = 24;
11742 dev->mode_config.prefer_shadow = 1;
11743
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020011744 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080011745
Jesse Barnesb690e962010-07-19 13:53:12 -070011746 intel_init_quirks(dev);
11747
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030011748 intel_init_pm(dev);
11749
Ben Widawskye3c74752013-04-05 13:12:39 -070011750 if (INTEL_INFO(dev)->num_pipes == 0)
11751 return;
11752
Jesse Barnese70236a2009-09-21 10:42:27 -070011753 intel_init_display(dev);
11754
Chris Wilsona6c45cf2010-09-17 00:32:17 +010011755 if (IS_GEN2(dev)) {
11756 dev->mode_config.max_width = 2048;
11757 dev->mode_config.max_height = 2048;
11758 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070011759 dev->mode_config.max_width = 4096;
11760 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080011761 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010011762 dev->mode_config.max_width = 8192;
11763 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080011764 }
Damien Lespiau068be562014-03-28 14:17:49 +000011765
11766 if (IS_GEN2(dev)) {
11767 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
11768 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
11769 } else {
11770 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
11771 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
11772 }
11773
Ben Widawsky5d4545a2013-01-17 12:45:15 -080011774 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080011775
Zhao Yakui28c97732009-10-09 11:39:41 +080011776 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011777 INTEL_INFO(dev)->num_pipes,
11778 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080011779
Damien Lespiau8cc87b72014-03-03 17:31:44 +000011780 for_each_pipe(pipe) {
11781 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000011782 for_each_sprite(pipe, sprite) {
11783 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070011784 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030011785 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000011786 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070011787 }
Jesse Barnes79e53942008-11-07 14:24:08 -080011788 }
11789
Jesse Barnesf42bb702013-12-16 16:34:23 -080011790 intel_init_dpio(dev);
Jesse Barnes5382f5f352013-12-16 16:34:24 -080011791 intel_reset_dpio(dev);
Jesse Barnesf42bb702013-12-16 16:34:23 -080011792
Paulo Zanoni79f689a2012-10-05 12:05:52 -030011793 intel_cpu_pll_init(dev);
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011794 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011795
Jesse Barnes9cce37f2010-08-13 15:11:26 -070011796 /* Just disable it once at startup */
11797 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011798 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000011799
11800 /* Just in case the BIOS is doing something questionable. */
11801 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080011802
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011803 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080011804 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011805 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080011806
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011807 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080011808 if (!crtc->active)
11809 continue;
11810
Jesse Barnes46f297f2014-03-07 08:57:48 -080011811 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080011812 * Note that reserving the BIOS fb up front prevents us
11813 * from stuffing other stolen allocations like the ring
11814 * on top. This prevents some ugliness at boot time, and
11815 * can even allow for smooth boot transitions if the BIOS
11816 * fb is large enough for the active pipe configuration.
11817 */
11818 if (dev_priv->display.get_plane_config) {
11819 dev_priv->display.get_plane_config(crtc,
11820 &crtc->plane_config);
11821 /*
11822 * If the fb is shared between multiple heads, we'll
11823 * just get the first one.
11824 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080011825 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080011826 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080011827 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010011828}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080011829
Daniel Vetter24929352012-07-02 20:28:59 +020011830static void
11831intel_connector_break_all_links(struct intel_connector *connector)
11832{
11833 connector->base.dpms = DRM_MODE_DPMS_OFF;
11834 connector->base.encoder = NULL;
11835 connector->encoder->connectors_active = false;
11836 connector->encoder->base.crtc = NULL;
11837}
11838
Daniel Vetter7fad7982012-07-04 17:51:47 +020011839static void intel_enable_pipe_a(struct drm_device *dev)
11840{
11841 struct intel_connector *connector;
11842 struct drm_connector *crt = NULL;
11843 struct intel_load_detect_pipe load_detect_temp;
Rob Clark51fd3712013-11-19 12:10:12 -050011844 struct drm_modeset_acquire_ctx ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020011845
11846 /* We can't just switch on the pipe A, we need to set things up with a
11847 * proper mode and output configuration. As a gross hack, enable pipe A
11848 * by enabling the load detect pipe once. */
11849 list_for_each_entry(connector,
11850 &dev->mode_config.connector_list,
11851 base.head) {
11852 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
11853 crt = &connector->base;
11854 break;
11855 }
11856 }
11857
11858 if (!crt)
11859 return;
11860
Rob Clark51fd3712013-11-19 12:10:12 -050011861 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, &ctx))
11862 intel_release_load_detect_pipe(crt, &load_detect_temp, &ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020011863
11864
11865}
11866
Daniel Vetterfa555832012-10-10 23:14:00 +020011867static bool
11868intel_check_plane_mapping(struct intel_crtc *crtc)
11869{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011870 struct drm_device *dev = crtc->base.dev;
11871 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020011872 u32 reg, val;
11873
Ben Widawsky7eb552a2013-03-13 14:05:41 -070011874 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020011875 return true;
11876
11877 reg = DSPCNTR(!crtc->plane);
11878 val = I915_READ(reg);
11879
11880 if ((val & DISPLAY_PLANE_ENABLE) &&
11881 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
11882 return false;
11883
11884 return true;
11885}
11886
Daniel Vetter24929352012-07-02 20:28:59 +020011887static void intel_sanitize_crtc(struct intel_crtc *crtc)
11888{
11889 struct drm_device *dev = crtc->base.dev;
11890 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020011891 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020011892
Daniel Vetter24929352012-07-02 20:28:59 +020011893 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020011894 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020011895 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
11896
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030011897 /* restore vblank interrupts to correct state */
11898 if (crtc->active)
11899 drm_vblank_on(dev, crtc->pipe);
11900 else
11901 drm_vblank_off(dev, crtc->pipe);
11902
Daniel Vetter24929352012-07-02 20:28:59 +020011903 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020011904 * disable the crtc (and hence change the state) if it is wrong. Note
11905 * that gen4+ has a fixed plane -> pipe mapping. */
11906 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020011907 struct intel_connector *connector;
11908 bool plane;
11909
Daniel Vetter24929352012-07-02 20:28:59 +020011910 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
11911 crtc->base.base.id);
11912
11913 /* Pipe has the wrong plane attached and the plane is active.
11914 * Temporarily change the plane mapping and disable everything
11915 * ... */
11916 plane = crtc->plane;
11917 crtc->plane = !plane;
11918 dev_priv->display.crtc_disable(&crtc->base);
11919 crtc->plane = plane;
11920
11921 /* ... and break all links. */
11922 list_for_each_entry(connector, &dev->mode_config.connector_list,
11923 base.head) {
11924 if (connector->encoder->base.crtc != &crtc->base)
11925 continue;
11926
11927 intel_connector_break_all_links(connector);
11928 }
11929
11930 WARN_ON(crtc->active);
11931 crtc->base.enabled = false;
11932 }
Daniel Vetter24929352012-07-02 20:28:59 +020011933
Daniel Vetter7fad7982012-07-04 17:51:47 +020011934 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
11935 crtc->pipe == PIPE_A && !crtc->active) {
11936 /* BIOS forgot to enable pipe A, this mostly happens after
11937 * resume. Force-enable the pipe to fix this, the update_dpms
11938 * call below we restore the pipe to the right state, but leave
11939 * the required bits on. */
11940 intel_enable_pipe_a(dev);
11941 }
11942
Daniel Vetter24929352012-07-02 20:28:59 +020011943 /* Adjust the state of the output pipe according to whether we
11944 * have active connectors/encoders. */
11945 intel_crtc_update_dpms(&crtc->base);
11946
11947 if (crtc->active != crtc->base.enabled) {
11948 struct intel_encoder *encoder;
11949
11950 /* This can happen either due to bugs in the get_hw_state
11951 * functions or because the pipe is force-enabled due to the
11952 * pipe A quirk. */
11953 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
11954 crtc->base.base.id,
11955 crtc->base.enabled ? "enabled" : "disabled",
11956 crtc->active ? "enabled" : "disabled");
11957
11958 crtc->base.enabled = crtc->active;
11959
11960 /* Because we only establish the connector -> encoder ->
11961 * crtc links if something is active, this means the
11962 * crtc is now deactivated. Break the links. connector
11963 * -> encoder links are only establish when things are
11964 * actually up, hence no need to break them. */
11965 WARN_ON(crtc->active);
11966
11967 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
11968 WARN_ON(encoder->connectors_active);
11969 encoder->base.crtc = NULL;
11970 }
11971 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020011972
11973 if (crtc->active || IS_VALLEYVIEW(dev) || INTEL_INFO(dev)->gen < 5) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010011974 /*
11975 * We start out with underrun reporting disabled to avoid races.
11976 * For correct bookkeeping mark this on active crtcs.
11977 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020011978 * Also on gmch platforms we dont have any hardware bits to
11979 * disable the underrun reporting. Which means we need to start
11980 * out with underrun reporting disabled also on inactive pipes,
11981 * since otherwise we'll complain about the garbage we read when
11982 * e.g. coming up after runtime pm.
11983 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010011984 * No protection against concurrent access is required - at
11985 * worst a fifo underrun happens which also sets this to false.
11986 */
11987 crtc->cpu_fifo_underrun_disabled = true;
11988 crtc->pch_fifo_underrun_disabled = true;
Ville Syrjälä80715b22014-05-15 20:23:23 +030011989
11990 update_scanline_offset(crtc);
Daniel Vetter4cc31482014-03-24 00:01:41 +010011991 }
Daniel Vetter24929352012-07-02 20:28:59 +020011992}
11993
11994static void intel_sanitize_encoder(struct intel_encoder *encoder)
11995{
11996 struct intel_connector *connector;
11997 struct drm_device *dev = encoder->base.dev;
11998
11999 /* We need to check both for a crtc link (meaning that the
12000 * encoder is active and trying to read from a pipe) and the
12001 * pipe itself being active. */
12002 bool has_active_crtc = encoder->base.crtc &&
12003 to_intel_crtc(encoder->base.crtc)->active;
12004
12005 if (encoder->connectors_active && !has_active_crtc) {
12006 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12007 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012008 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012009
12010 /* Connector is active, but has no active pipe. This is
12011 * fallout from our resume register restoring. Disable
12012 * the encoder manually again. */
12013 if (encoder->base.crtc) {
12014 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12015 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012016 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012017 encoder->disable(encoder);
12018 }
12019
12020 /* Inconsistent output/port/pipe state happens presumably due to
12021 * a bug in one of the get_hw_state functions. Or someplace else
12022 * in our code, like the register restore mess on resume. Clamp
12023 * things to off as a safer default. */
12024 list_for_each_entry(connector,
12025 &dev->mode_config.connector_list,
12026 base.head) {
12027 if (connector->encoder != encoder)
12028 continue;
12029
12030 intel_connector_break_all_links(connector);
12031 }
12032 }
12033 /* Enabled encoders without active connectors will be fixed in
12034 * the crtc fixup. */
12035}
12036
Imre Deak04098752014-02-18 00:02:16 +020012037void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012038{
12039 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012040 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012041
Imre Deak04098752014-02-18 00:02:16 +020012042 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12043 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12044 i915_disable_vga(dev);
12045 }
12046}
12047
12048void i915_redisable_vga(struct drm_device *dev)
12049{
12050 struct drm_i915_private *dev_priv = dev->dev_private;
12051
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012052 /* This function can be called both from intel_modeset_setup_hw_state or
12053 * at a very early point in our resume sequence, where the power well
12054 * structures are not yet restored. Since this function is at a very
12055 * paranoid "someone might have enabled VGA while we were not looking"
12056 * level, just check if the power well is enabled instead of trying to
12057 * follow the "don't touch the power well if we don't need it" policy
12058 * the rest of the driver uses. */
Imre Deak04098752014-02-18 00:02:16 +020012059 if (!intel_display_power_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012060 return;
12061
Imre Deak04098752014-02-18 00:02:16 +020012062 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012063}
12064
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012065static bool primary_get_hw_state(struct intel_crtc *crtc)
12066{
12067 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12068
12069 if (!crtc->active)
12070 return false;
12071
12072 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12073}
12074
Daniel Vetter30e984d2013-06-05 13:34:17 +020012075static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020012076{
12077 struct drm_i915_private *dev_priv = dev->dev_private;
12078 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020012079 struct intel_crtc *crtc;
12080 struct intel_encoder *encoder;
12081 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020012082 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020012083
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012084 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010012085 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020012086
Daniel Vetter99535992014-04-13 12:00:33 +020012087 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
12088
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012089 crtc->active = dev_priv->display.get_pipe_config(crtc,
12090 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012091
12092 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012093 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020012094
12095 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
12096 crtc->base.base.id,
12097 crtc->active ? "enabled" : "disabled");
12098 }
12099
Daniel Vetter53589012013-06-05 13:34:16 +020012100 /* FIXME: Smash this into the new shared dpll infrastructure. */
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012101 if (HAS_DDI(dev))
Paulo Zanoni6441ab52012-10-05 12:05:58 -030012102 intel_ddi_setup_hw_pll_state(dev);
12103
Daniel Vetter53589012013-06-05 13:34:16 +020012104 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12105 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12106
12107 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
12108 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012109 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020012110 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12111 pll->active++;
12112 }
12113 pll->refcount = pll->active;
12114
Daniel Vetter35c95372013-07-17 06:55:04 +020012115 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
12116 pll->name, pll->refcount, pll->on);
Daniel Vetter53589012013-06-05 13:34:16 +020012117 }
12118
Daniel Vetter24929352012-07-02 20:28:59 +020012119 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12120 base.head) {
12121 pipe = 0;
12122
12123 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012124 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12125 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012126 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020012127 } else {
12128 encoder->base.crtc = NULL;
12129 }
12130
12131 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012132 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020012133 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012134 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012135 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010012136 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020012137 }
12138
12139 list_for_each_entry(connector, &dev->mode_config.connector_list,
12140 base.head) {
12141 if (connector->get_hw_state(connector)) {
12142 connector->base.dpms = DRM_MODE_DPMS_ON;
12143 connector->encoder->connectors_active = true;
12144 connector->base.encoder = &connector->encoder->base;
12145 } else {
12146 connector->base.dpms = DRM_MODE_DPMS_OFF;
12147 connector->base.encoder = NULL;
12148 }
12149 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
12150 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012151 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020012152 connector->base.encoder ? "enabled" : "disabled");
12153 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020012154}
12155
12156/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
12157 * and i915 state tracking structures. */
12158void intel_modeset_setup_hw_state(struct drm_device *dev,
12159 bool force_restore)
12160{
12161 struct drm_i915_private *dev_priv = dev->dev_private;
12162 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012163 struct intel_crtc *crtc;
12164 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020012165 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020012166
12167 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020012168
Jesse Barnesbabea612013-06-26 18:57:38 +030012169 /*
12170 * Now that we have the config, copy it to each CRTC struct
12171 * Note that this could go away if we move to using crtc_config
12172 * checking everywhere.
12173 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012174 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020012175 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080012176 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030012177 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
12178 crtc->base.base.id);
12179 drm_mode_debug_printmodeline(&crtc->base.mode);
12180 }
12181 }
12182
Daniel Vetter24929352012-07-02 20:28:59 +020012183 /* HW state is read out, now we need to sanitize this mess. */
12184 list_for_each_entry(encoder, &dev->mode_config.encoder_list,
12185 base.head) {
12186 intel_sanitize_encoder(encoder);
12187 }
12188
12189 for_each_pipe(pipe) {
12190 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
12191 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012192 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020012193 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012194
Daniel Vetter35c95372013-07-17 06:55:04 +020012195 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12196 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12197
12198 if (!pll->on || pll->active)
12199 continue;
12200
12201 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
12202
12203 pll->disable(dev_priv, pll);
12204 pll->on = false;
12205 }
12206
Ville Syrjälä96f90c52013-12-05 15:51:38 +020012207 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030012208 ilk_wm_get_hw_state(dev);
12209
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012210 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030012211 i915_redisable_vga(dev);
12212
Daniel Vetterf30da182013-04-11 20:22:50 +020012213 /*
12214 * We need to use raw interfaces for restoring state to avoid
12215 * checking (bogus) intermediate states.
12216 */
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012217 for_each_pipe(pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070012218 struct drm_crtc *crtc =
12219 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020012220
12221 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070012222 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010012223 }
12224 } else {
12225 intel_modeset_update_staged_output_state(dev);
12226 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012227
12228 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010012229}
12230
12231void intel_modeset_gem_init(struct drm_device *dev)
12232{
Jesse Barnes484b41d2014-03-07 08:57:55 -080012233 struct drm_crtc *c;
12234 struct intel_framebuffer *fb;
12235
Imre Deakae484342014-03-31 15:10:44 +030012236 mutex_lock(&dev->struct_mutex);
12237 intel_init_gt_powersave(dev);
12238 mutex_unlock(&dev->struct_mutex);
12239
Chris Wilson1833b132012-05-09 11:56:28 +010012240 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020012241
12242 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012243
12244 /*
12245 * Make sure any fbs we allocated at startup are properly
12246 * pinned & fenced. When we do the allocation it's too early
12247 * for this.
12248 */
12249 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012250 for_each_crtc(dev, c) {
Dave Airlie66e514c2014-04-03 07:51:54 +100012251 if (!c->primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -080012252 continue;
12253
Dave Airlie66e514c2014-04-03 07:51:54 +100012254 fb = to_intel_framebuffer(c->primary->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -080012255 if (intel_pin_and_fence_fb_obj(dev, fb->obj, NULL)) {
12256 DRM_ERROR("failed to pin boot fb on pipe %d\n",
12257 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100012258 drm_framebuffer_unreference(c->primary->fb);
12259 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080012260 }
12261 }
12262 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012263}
12264
Imre Deak4932e2c2014-02-11 17:12:48 +020012265void intel_connector_unregister(struct intel_connector *intel_connector)
12266{
12267 struct drm_connector *connector = &intel_connector->base;
12268
12269 intel_panel_destroy_backlight(connector);
12270 drm_sysfs_connector_remove(connector);
12271}
12272
Jesse Barnes79e53942008-11-07 14:24:08 -080012273void intel_modeset_cleanup(struct drm_device *dev)
12274{
Jesse Barnes652c3932009-08-17 13:31:43 -070012275 struct drm_i915_private *dev_priv = dev->dev_private;
12276 struct drm_crtc *crtc;
Paulo Zanonid9255d52013-09-26 20:05:59 -030012277 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070012278
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012279 /*
12280 * Interrupts and polling as the first thing to avoid creating havoc.
12281 * Too much stuff here (turning of rps, connectors, ...) would
12282 * experience fancy races otherwise.
12283 */
12284 drm_irq_uninstall(dev);
12285 cancel_work_sync(&dev_priv->hotplug_work);
12286 /*
12287 * Due to the hpd irq storm handling the hotplug work can re-arm the
12288 * poll handlers. Hence disable polling after hpd handling is shut down.
12289 */
Keith Packardf87ea762010-10-03 19:36:26 -070012290 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020012291
Jesse Barnes652c3932009-08-17 13:31:43 -070012292 mutex_lock(&dev->struct_mutex);
12293
Jesse Barnes723bfd72010-10-07 16:01:13 -070012294 intel_unregister_dsm_handler();
12295
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012296 for_each_crtc(dev, crtc) {
Jesse Barnes652c3932009-08-17 13:31:43 -070012297 /* Skip inactive CRTCs */
Matt Roperf4510a22014-04-01 15:22:40 -070012298 if (!crtc->primary->fb)
Jesse Barnes652c3932009-08-17 13:31:43 -070012299 continue;
12300
Daniel Vetter3dec0092010-08-20 21:40:52 +020012301 intel_increase_pllclock(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070012302 }
12303
Chris Wilson973d04f2011-07-08 12:22:37 +010012304 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070012305
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012306 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000012307
Daniel Vetter930ebb42012-06-29 23:32:16 +020012308 ironlake_teardown_rc6(dev);
12309
Kristian Høgsberg69341a52009-11-11 12:19:17 -050012310 mutex_unlock(&dev->struct_mutex);
12311
Chris Wilson1630fe72011-07-08 12:22:42 +010012312 /* flush any delayed tasks or pending work */
12313 flush_scheduled_work();
12314
Jani Nikuladb31af1d2013-11-08 16:48:53 +020012315 /* destroy the backlight and sysfs files before encoders/connectors */
12316 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020012317 struct intel_connector *intel_connector;
12318
12319 intel_connector = to_intel_connector(connector);
12320 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020012321 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030012322
Jesse Barnes79e53942008-11-07 14:24:08 -080012323 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010012324
12325 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030012326
12327 mutex_lock(&dev->struct_mutex);
12328 intel_cleanup_gt_powersave(dev);
12329 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012330}
12331
Dave Airlie28d52042009-09-21 14:33:58 +100012332/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080012333 * Return which encoder is currently attached for connector.
12334 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010012335struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080012336{
Chris Wilsondf0e9242010-09-09 16:20:55 +010012337 return &intel_attached_encoder(connector)->base;
12338}
Jesse Barnes79e53942008-11-07 14:24:08 -080012339
Chris Wilsondf0e9242010-09-09 16:20:55 +010012340void intel_connector_attach_encoder(struct intel_connector *connector,
12341 struct intel_encoder *encoder)
12342{
12343 connector->encoder = encoder;
12344 drm_mode_connector_attach_encoder(&connector->base,
12345 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080012346}
Dave Airlie28d52042009-09-21 14:33:58 +100012347
12348/*
12349 * set vga decode state - true == enable VGA decode
12350 */
12351int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
12352{
12353 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000012354 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100012355 u16 gmch_ctrl;
12356
Chris Wilson75fa0412014-02-07 18:37:02 -020012357 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
12358 DRM_ERROR("failed to read control word\n");
12359 return -EIO;
12360 }
12361
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020012362 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
12363 return 0;
12364
Dave Airlie28d52042009-09-21 14:33:58 +100012365 if (state)
12366 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
12367 else
12368 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020012369
12370 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
12371 DRM_ERROR("failed to write control word\n");
12372 return -EIO;
12373 }
12374
Dave Airlie28d52042009-09-21 14:33:58 +100012375 return 0;
12376}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012377
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012378struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012379
12380 u32 power_well_driver;
12381
Chris Wilson63b66e52013-08-08 15:12:06 +020012382 int num_transcoders;
12383
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012384 struct intel_cursor_error_state {
12385 u32 control;
12386 u32 position;
12387 u32 base;
12388 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010012389 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012390
12391 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012392 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012393 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030012394 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010012395 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012396
12397 struct intel_plane_error_state {
12398 u32 control;
12399 u32 stride;
12400 u32 size;
12401 u32 pos;
12402 u32 addr;
12403 u32 surface;
12404 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010012405 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020012406
12407 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020012408 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020012409 enum transcoder cpu_transcoder;
12410
12411 u32 conf;
12412
12413 u32 htotal;
12414 u32 hblank;
12415 u32 hsync;
12416 u32 vtotal;
12417 u32 vblank;
12418 u32 vsync;
12419 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012420};
12421
12422struct intel_display_error_state *
12423intel_display_capture_error_state(struct drm_device *dev)
12424{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012425 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012426 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020012427 int transcoders[] = {
12428 TRANSCODER_A,
12429 TRANSCODER_B,
12430 TRANSCODER_C,
12431 TRANSCODER_EDP,
12432 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012433 int i;
12434
Chris Wilson63b66e52013-08-08 15:12:06 +020012435 if (INTEL_INFO(dev)->num_pipes == 0)
12436 return NULL;
12437
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012438 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012439 if (error == NULL)
12440 return NULL;
12441
Imre Deak190be112013-11-25 17:15:31 +020012442 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012443 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
12444
Damien Lespiau52331302012-08-15 19:23:25 +010012445 for_each_pipe(i) {
Imre Deakddf9c532013-11-27 22:02:02 +020012446 error->pipe[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012447 intel_display_power_enabled_sw(dev_priv,
12448 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020012449 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012450 continue;
12451
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030012452 error->cursor[i].control = I915_READ(CURCNTR(i));
12453 error->cursor[i].position = I915_READ(CURPOS(i));
12454 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012455
12456 error->plane[i].control = I915_READ(DSPCNTR(i));
12457 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012458 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030012459 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012460 error->plane[i].pos = I915_READ(DSPPOS(i));
12461 }
Paulo Zanonica291362013-03-06 20:03:14 -030012462 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
12463 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012464 if (INTEL_INFO(dev)->gen >= 4) {
12465 error->plane[i].surface = I915_READ(DSPSURF(i));
12466 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
12467 }
12468
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012469 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030012470
12471 if (!HAS_PCH_SPLIT(dev))
12472 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020012473 }
12474
12475 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
12476 if (HAS_DDI(dev_priv->dev))
12477 error->num_transcoders++; /* Account for eDP. */
12478
12479 for (i = 0; i < error->num_transcoders; i++) {
12480 enum transcoder cpu_transcoder = transcoders[i];
12481
Imre Deakddf9c532013-11-27 22:02:02 +020012482 error->transcoder[i].power_domain_on =
Imre Deakda7e29b2014-02-18 00:02:02 +020012483 intel_display_power_enabled_sw(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020012484 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012485 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020012486 continue;
12487
Chris Wilson63b66e52013-08-08 15:12:06 +020012488 error->transcoder[i].cpu_transcoder = cpu_transcoder;
12489
12490 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
12491 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
12492 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
12493 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
12494 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
12495 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
12496 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012497 }
12498
12499 return error;
12500}
12501
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012502#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
12503
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012504void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012505intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012506 struct drm_device *dev,
12507 struct intel_display_error_state *error)
12508{
12509 int i;
12510
Chris Wilson63b66e52013-08-08 15:12:06 +020012511 if (!error)
12512 return;
12513
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012514 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020012515 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012516 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030012517 error->power_well_driver);
Damien Lespiau52331302012-08-15 19:23:25 +010012518 for_each_pipe(i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012519 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020012520 err_printf(m, " Power: %s\n",
12521 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012522 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030012523 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012524
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012525 err_printf(m, "Plane [%d]:\n", i);
12526 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
12527 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012528 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012529 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
12530 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030012531 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030012532 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012533 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012534 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012535 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
12536 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012537 }
12538
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030012539 err_printf(m, "Cursor [%d]:\n", i);
12540 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
12541 err_printf(m, " POS: %08x\n", error->cursor[i].position);
12542 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012543 }
Chris Wilson63b66e52013-08-08 15:12:06 +020012544
12545 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010012546 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020012547 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020012548 err_printf(m, " Power: %s\n",
12549 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020012550 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
12551 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
12552 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
12553 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
12554 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
12555 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
12556 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
12557 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000012558}