blob: 848f330a931f5dadc41f1fa160e4322e7f338443 [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>
Matt Roper465c1202014-05-29 08:06:54 -070042#include <drm/drm_plane_helper.h>
43#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080044#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080045
Matt Roper465c1202014-05-29 08:06:54 -070046/* Primary plane formats supported by all gen */
47#define COMMON_PRIMARY_FORMATS \
48 DRM_FORMAT_C8, \
49 DRM_FORMAT_RGB565, \
50 DRM_FORMAT_XRGB8888, \
51 DRM_FORMAT_ARGB8888
52
53/* Primary plane formats for gen <= 3 */
54static const uint32_t intel_primary_formats_gen2[] = {
55 COMMON_PRIMARY_FORMATS,
56 DRM_FORMAT_XRGB1555,
57 DRM_FORMAT_ARGB1555,
58};
59
60/* Primary plane formats for gen >= 4 */
61static const uint32_t intel_primary_formats_gen4[] = {
62 COMMON_PRIMARY_FORMATS, \
63 DRM_FORMAT_XBGR8888,
64 DRM_FORMAT_ABGR8888,
65 DRM_FORMAT_XRGB2101010,
66 DRM_FORMAT_ARGB2101010,
67 DRM_FORMAT_XBGR2101010,
68 DRM_FORMAT_ABGR2101010,
69};
70
Matt Roper3d7d6512014-06-10 08:28:13 -070071/* Cursor formats */
72static const uint32_t intel_cursor_formats[] = {
73 DRM_FORMAT_ARGB8888,
74};
75
Chris Wilson6b383a72010-09-13 13:54:26 +010076static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080077
Jesse Barnesf1f644d2013-06-27 00:39:25 +030078static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
79 struct intel_crtc_config *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030080static void ironlake_pch_clock_get(struct intel_crtc *crtc,
81 struct intel_crtc_config *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030082
Damien Lespiaue7457a92013-08-08 22:28:59 +010083static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
84 int x, int y, struct drm_framebuffer *old_fb);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080085static int intel_framebuffer_init(struct drm_device *dev,
86 struct intel_framebuffer *ifb,
87 struct drm_mode_fb_cmd2 *mode_cmd,
88 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020089static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
90static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020091static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070092 struct intel_link_m_n *m_n,
93 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020094static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020095static void haswell_set_pipeconf(struct drm_crtc *crtc);
96static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +020097static void vlv_prepare_pll(struct intel_crtc *crtc,
98 const struct intel_crtc_config *pipe_config);
99static void chv_prepare_pll(struct intel_crtc *crtc,
100 const struct intel_crtc_config *pipe_config);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100101
Dave Airlie0e32b392014-05-02 14:02:48 +1000102static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
103{
104 if (!connector->mst_port)
105 return connector->encoder;
106 else
107 return &connector->mst_port->mst_encoders[pipe]->base;
108}
109
Jesse Barnes79e53942008-11-07 14:24:08 -0800110typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400111 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800112} intel_range_t;
113
114typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400115 int dot_limit;
116 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800117} intel_p2_t;
118
Ma Lingd4906092009-03-18 20:13:27 +0800119typedef struct intel_limit intel_limit_t;
120struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400121 intel_range_t dot, vco, n, m, m1, m2, p, p1;
122 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800123};
Jesse Barnes79e53942008-11-07 14:24:08 -0800124
Daniel Vetterd2acd212012-10-20 20:57:43 +0200125int
126intel_pch_rawclk(struct drm_device *dev)
127{
128 struct drm_i915_private *dev_priv = dev->dev_private;
129
130 WARN_ON(!HAS_PCH_SPLIT(dev));
131
132 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
133}
134
Chris Wilson021357a2010-09-07 20:54:59 +0100135static inline u32 /* units of 100MHz */
136intel_fdi_link_freq(struct drm_device *dev)
137{
Chris Wilson8b99e682010-10-13 09:59:17 +0100138 if (IS_GEN5(dev)) {
139 struct drm_i915_private *dev_priv = dev->dev_private;
140 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
141 } else
142 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100143}
144
Daniel Vetter5d536e22013-07-06 12:52:06 +0200145static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400146 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200147 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200148 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400149 .m = { .min = 96, .max = 140 },
150 .m1 = { .min = 18, .max = 26 },
151 .m2 = { .min = 6, .max = 16 },
152 .p = { .min = 4, .max = 128 },
153 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700154 .p2 = { .dot_limit = 165000,
155 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700156};
157
Daniel Vetter5d536e22013-07-06 12:52:06 +0200158static const intel_limit_t intel_limits_i8xx_dvo = {
159 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200160 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200161 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200162 .m = { .min = 96, .max = 140 },
163 .m1 = { .min = 18, .max = 26 },
164 .m2 = { .min = 6, .max = 16 },
165 .p = { .min = 4, .max = 128 },
166 .p1 = { .min = 2, .max = 33 },
167 .p2 = { .dot_limit = 165000,
168 .p2_slow = 4, .p2_fast = 4 },
169};
170
Keith Packarde4b36692009-06-05 19:22:17 -0700171static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400172 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200173 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200174 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400175 .m = { .min = 96, .max = 140 },
176 .m1 = { .min = 18, .max = 26 },
177 .m2 = { .min = 6, .max = 16 },
178 .p = { .min = 4, .max = 128 },
179 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700180 .p2 = { .dot_limit = 165000,
181 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700182};
Eric Anholt273e27c2011-03-30 13:01:10 -0700183
Keith Packarde4b36692009-06-05 19:22:17 -0700184static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400185 .dot = { .min = 20000, .max = 400000 },
186 .vco = { .min = 1400000, .max = 2800000 },
187 .n = { .min = 1, .max = 6 },
188 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100189 .m1 = { .min = 8, .max = 18 },
190 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400191 .p = { .min = 5, .max = 80 },
192 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700193 .p2 = { .dot_limit = 200000,
194 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700195};
196
197static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400198 .dot = { .min = 20000, .max = 400000 },
199 .vco = { .min = 1400000, .max = 2800000 },
200 .n = { .min = 1, .max = 6 },
201 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100202 .m1 = { .min = 8, .max = 18 },
203 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400204 .p = { .min = 7, .max = 98 },
205 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700206 .p2 = { .dot_limit = 112000,
207 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700208};
209
Eric Anholt273e27c2011-03-30 13:01:10 -0700210
Keith Packarde4b36692009-06-05 19:22:17 -0700211static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700212 .dot = { .min = 25000, .max = 270000 },
213 .vco = { .min = 1750000, .max = 3500000},
214 .n = { .min = 1, .max = 4 },
215 .m = { .min = 104, .max = 138 },
216 .m1 = { .min = 17, .max = 23 },
217 .m2 = { .min = 5, .max = 11 },
218 .p = { .min = 10, .max = 30 },
219 .p1 = { .min = 1, .max = 3},
220 .p2 = { .dot_limit = 270000,
221 .p2_slow = 10,
222 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800223 },
Keith Packarde4b36692009-06-05 19:22:17 -0700224};
225
226static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700227 .dot = { .min = 22000, .max = 400000 },
228 .vco = { .min = 1750000, .max = 3500000},
229 .n = { .min = 1, .max = 4 },
230 .m = { .min = 104, .max = 138 },
231 .m1 = { .min = 16, .max = 23 },
232 .m2 = { .min = 5, .max = 11 },
233 .p = { .min = 5, .max = 80 },
234 .p1 = { .min = 1, .max = 8},
235 .p2 = { .dot_limit = 165000,
236 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700237};
238
239static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700240 .dot = { .min = 20000, .max = 115000 },
241 .vco = { .min = 1750000, .max = 3500000 },
242 .n = { .min = 1, .max = 3 },
243 .m = { .min = 104, .max = 138 },
244 .m1 = { .min = 17, .max = 23 },
245 .m2 = { .min = 5, .max = 11 },
246 .p = { .min = 28, .max = 112 },
247 .p1 = { .min = 2, .max = 8 },
248 .p2 = { .dot_limit = 0,
249 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800250 },
Keith Packarde4b36692009-06-05 19:22:17 -0700251};
252
253static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700254 .dot = { .min = 80000, .max = 224000 },
255 .vco = { .min = 1750000, .max = 3500000 },
256 .n = { .min = 1, .max = 3 },
257 .m = { .min = 104, .max = 138 },
258 .m1 = { .min = 17, .max = 23 },
259 .m2 = { .min = 5, .max = 11 },
260 .p = { .min = 14, .max = 42 },
261 .p1 = { .min = 2, .max = 6 },
262 .p2 = { .dot_limit = 0,
263 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800264 },
Keith Packarde4b36692009-06-05 19:22:17 -0700265};
266
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500267static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400268 .dot = { .min = 20000, .max = 400000},
269 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700270 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400271 .n = { .min = 3, .max = 6 },
272 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700273 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400274 .m1 = { .min = 0, .max = 0 },
275 .m2 = { .min = 0, .max = 254 },
276 .p = { .min = 5, .max = 80 },
277 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700278 .p2 = { .dot_limit = 200000,
279 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700280};
281
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500282static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .dot = { .min = 20000, .max = 400000 },
284 .vco = { .min = 1700000, .max = 3500000 },
285 .n = { .min = 3, .max = 6 },
286 .m = { .min = 2, .max = 256 },
287 .m1 = { .min = 0, .max = 0 },
288 .m2 = { .min = 0, .max = 254 },
289 .p = { .min = 7, .max = 112 },
290 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700291 .p2 = { .dot_limit = 112000,
292 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700293};
294
Eric Anholt273e27c2011-03-30 13:01:10 -0700295/* Ironlake / Sandybridge
296 *
297 * We calculate clock using (register_value + 2) for N/M1/M2, so here
298 * the range value for them is (actual_value - 2).
299 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800300static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700301 .dot = { .min = 25000, .max = 350000 },
302 .vco = { .min = 1760000, .max = 3510000 },
303 .n = { .min = 1, .max = 5 },
304 .m = { .min = 79, .max = 127 },
305 .m1 = { .min = 12, .max = 22 },
306 .m2 = { .min = 5, .max = 9 },
307 .p = { .min = 5, .max = 80 },
308 .p1 = { .min = 1, .max = 8 },
309 .p2 = { .dot_limit = 225000,
310 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700311};
312
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800313static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700314 .dot = { .min = 25000, .max = 350000 },
315 .vco = { .min = 1760000, .max = 3510000 },
316 .n = { .min = 1, .max = 3 },
317 .m = { .min = 79, .max = 118 },
318 .m1 = { .min = 12, .max = 22 },
319 .m2 = { .min = 5, .max = 9 },
320 .p = { .min = 28, .max = 112 },
321 .p1 = { .min = 2, .max = 8 },
322 .p2 = { .dot_limit = 225000,
323 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800324};
325
326static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700327 .dot = { .min = 25000, .max = 350000 },
328 .vco = { .min = 1760000, .max = 3510000 },
329 .n = { .min = 1, .max = 3 },
330 .m = { .min = 79, .max = 127 },
331 .m1 = { .min = 12, .max = 22 },
332 .m2 = { .min = 5, .max = 9 },
333 .p = { .min = 14, .max = 56 },
334 .p1 = { .min = 2, .max = 8 },
335 .p2 = { .dot_limit = 225000,
336 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800337};
338
Eric Anholt273e27c2011-03-30 13:01:10 -0700339/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800340static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700341 .dot = { .min = 25000, .max = 350000 },
342 .vco = { .min = 1760000, .max = 3510000 },
343 .n = { .min = 1, .max = 2 },
344 .m = { .min = 79, .max = 126 },
345 .m1 = { .min = 12, .max = 22 },
346 .m2 = { .min = 5, .max = 9 },
347 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400348 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700349 .p2 = { .dot_limit = 225000,
350 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351};
352
353static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700354 .dot = { .min = 25000, .max = 350000 },
355 .vco = { .min = 1760000, .max = 3510000 },
356 .n = { .min = 1, .max = 3 },
357 .m = { .min = 79, .max = 126 },
358 .m1 = { .min = 12, .max = 22 },
359 .m2 = { .min = 5, .max = 9 },
360 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400361 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700362 .p2 = { .dot_limit = 225000,
363 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800364};
365
Ville Syrjälädc730512013-09-24 21:26:30 +0300366static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300367 /*
368 * These are the data rate limits (measured in fast clocks)
369 * since those are the strictest limits we have. The fast
370 * clock and actual rate limits are more relaxed, so checking
371 * them would make no difference.
372 */
373 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200374 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700375 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700376 .m1 = { .min = 2, .max = 3 },
377 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300378 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300379 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700380};
381
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300382static const intel_limit_t intel_limits_chv = {
383 /*
384 * These are the data rate limits (measured in fast clocks)
385 * since those are the strictest limits we have. The fast
386 * clock and actual rate limits are more relaxed, so checking
387 * them would make no difference.
388 */
389 .dot = { .min = 25000 * 5, .max = 540000 * 5},
390 .vco = { .min = 4860000, .max = 6700000 },
391 .n = { .min = 1, .max = 1 },
392 .m1 = { .min = 2, .max = 2 },
393 .m2 = { .min = 24 << 22, .max = 175 << 22 },
394 .p1 = { .min = 2, .max = 4 },
395 .p2 = { .p2_slow = 1, .p2_fast = 14 },
396};
397
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300398static void vlv_clock(int refclk, intel_clock_t *clock)
399{
400 clock->m = clock->m1 * clock->m2;
401 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200402 if (WARN_ON(clock->n == 0 || clock->p == 0))
403 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300404 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
405 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300406}
407
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300408/**
409 * Returns whether any output on the specified pipe is of the specified type
410 */
Jani Nikula7c10a2b2014-10-27 16:26:43 +0200411bool intel_pipe_has_type(struct intel_crtc *crtc, int type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300412{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300413 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300414 struct intel_encoder *encoder;
415
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300416 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300417 if (encoder->type == type)
418 return true;
419
420 return false;
421}
422
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300423static const intel_limit_t *intel_ironlake_limit(struct intel_crtc *crtc,
Chris Wilson1b894b52010-12-14 20:04:54 +0000424 int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800425{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300426 struct drm_device *dev = crtc->base.dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800427 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800428
429 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100430 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000431 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800432 limit = &intel_limits_ironlake_dual_lvds_100m;
433 else
434 limit = &intel_limits_ironlake_dual_lvds;
435 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000436 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800437 limit = &intel_limits_ironlake_single_lvds_100m;
438 else
439 limit = &intel_limits_ironlake_single_lvds;
440 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200441 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800442 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800443
444 return limit;
445}
446
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300447static const intel_limit_t *intel_g4x_limit(struct intel_crtc *crtc)
Ma Ling044c7c42009-03-18 20:13:23 +0800448{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300449 struct drm_device *dev = crtc->base.dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800450 const intel_limit_t *limit;
451
452 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100453 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700454 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800455 else
Keith Packarde4b36692009-06-05 19:22:17 -0700456 limit = &intel_limits_g4x_single_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800457 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI) ||
458 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700459 limit = &intel_limits_g4x_hdmi;
Ma Ling044c7c42009-03-18 20:13:23 +0800460 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700461 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800462 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700463 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800464
465 return limit;
466}
467
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300468static const intel_limit_t *intel_limit(struct intel_crtc *crtc, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800469{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300470 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800471 const intel_limit_t *limit;
472
Eric Anholtbad720f2009-10-22 16:11:14 -0700473 if (HAS_PCH_SPLIT(dev))
Chris Wilson1b894b52010-12-14 20:04:54 +0000474 limit = intel_ironlake_limit(crtc, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800475 else if (IS_G4X(dev)) {
Ma Ling044c7c42009-03-18 20:13:23 +0800476 limit = intel_g4x_limit(crtc);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500477 } else if (IS_PINEVIEW(dev)) {
Shaohua Li21778322009-02-23 15:19:16 +0800478 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500479 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800480 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500481 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300482 } else if (IS_CHERRYVIEW(dev)) {
483 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700484 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300485 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100486 } else if (!IS_GEN2(dev)) {
487 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
488 limit = &intel_limits_i9xx_lvds;
489 else
490 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800491 } else {
492 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700493 limit = &intel_limits_i8xx_lvds;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200494 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700495 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200496 else
497 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800498 }
499 return limit;
500}
501
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500502/* m1 is reserved as 0 in Pineview, n is a ring counter */
503static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800504{
Shaohua Li21778322009-02-23 15:19:16 +0800505 clock->m = clock->m2 + 2;
506 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200507 if (WARN_ON(clock->n == 0 || clock->p == 0))
508 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300509 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
510 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800511}
512
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200513static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
514{
515 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
516}
517
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200518static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800519{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200520 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800521 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200522 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
523 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300524 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
525 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800526}
527
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300528static void chv_clock(int refclk, intel_clock_t *clock)
529{
530 clock->m = clock->m1 * clock->m2;
531 clock->p = clock->p1 * clock->p2;
532 if (WARN_ON(clock->n == 0 || clock->p == 0))
533 return;
534 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
535 clock->n << 22);
536 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
537}
538
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800539#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800540/**
541 * Returns whether the given set of divisors are valid for a given refclk with
542 * the given connectors.
543 */
544
Chris Wilson1b894b52010-12-14 20:04:54 +0000545static bool intel_PLL_is_valid(struct drm_device *dev,
546 const intel_limit_t *limit,
547 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800548{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300549 if (clock->n < limit->n.min || limit->n.max < clock->n)
550 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800551 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400552 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800553 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400554 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800555 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400556 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300557
558 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev))
559 if (clock->m1 <= clock->m2)
560 INTELPllInvalid("m1 <= m2\n");
561
562 if (!IS_VALLEYVIEW(dev)) {
563 if (clock->p < limit->p.min || limit->p.max < clock->p)
564 INTELPllInvalid("p out of range\n");
565 if (clock->m < limit->m.min || limit->m.max < clock->m)
566 INTELPllInvalid("m out of range\n");
567 }
568
Jesse Barnes79e53942008-11-07 14:24:08 -0800569 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400570 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800571 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
572 * connector, etc., rather than just a single range.
573 */
574 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400575 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800576
577 return true;
578}
579
Ma Lingd4906092009-03-18 20:13:27 +0800580static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300581i9xx_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Sean Paulcec2f352012-01-10 15:09:36 -0800582 int target, int refclk, intel_clock_t *match_clock,
583 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800584{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300585 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800586 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800587 int err = target;
588
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300589 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800590 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100591 * For LVDS just rely on its current settings for dual-channel.
592 * We haven't figured out how to reliably set up different
593 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800594 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100595 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800596 clock.p2 = limit->p2.p2_fast;
597 else
598 clock.p2 = limit->p2.p2_slow;
599 } else {
600 if (target < limit->p2.dot_limit)
601 clock.p2 = limit->p2.p2_slow;
602 else
603 clock.p2 = limit->p2.p2_fast;
604 }
605
Akshay Joshi0206e352011-08-16 15:34:10 -0400606 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800607
Zhao Yakui42158662009-11-20 11:24:18 +0800608 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
609 clock.m1++) {
610 for (clock.m2 = limit->m2.min;
611 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200612 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800613 break;
614 for (clock.n = limit->n.min;
615 clock.n <= limit->n.max; clock.n++) {
616 for (clock.p1 = limit->p1.min;
617 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800618 int this_err;
619
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200620 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000621 if (!intel_PLL_is_valid(dev, limit,
622 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800623 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800624 if (match_clock &&
625 clock.p != match_clock->p)
626 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800627
628 this_err = abs(clock.dot - target);
629 if (this_err < err) {
630 *best_clock = clock;
631 err = this_err;
632 }
633 }
634 }
635 }
636 }
637
638 return (err != target);
639}
640
Ma Lingd4906092009-03-18 20:13:27 +0800641static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300642pnv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200643 int target, int refclk, intel_clock_t *match_clock,
644 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200645{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300646 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200647 intel_clock_t clock;
648 int err = target;
649
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300650 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200651 /*
652 * For LVDS just rely on its current settings for dual-channel.
653 * We haven't figured out how to reliably set up different
654 * single/dual channel state, if we even can.
655 */
656 if (intel_is_dual_link_lvds(dev))
657 clock.p2 = limit->p2.p2_fast;
658 else
659 clock.p2 = limit->p2.p2_slow;
660 } else {
661 if (target < limit->p2.dot_limit)
662 clock.p2 = limit->p2.p2_slow;
663 else
664 clock.p2 = limit->p2.p2_fast;
665 }
666
667 memset(best_clock, 0, sizeof(*best_clock));
668
669 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
670 clock.m1++) {
671 for (clock.m2 = limit->m2.min;
672 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200673 for (clock.n = limit->n.min;
674 clock.n <= limit->n.max; clock.n++) {
675 for (clock.p1 = limit->p1.min;
676 clock.p1 <= limit->p1.max; clock.p1++) {
677 int this_err;
678
679 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800680 if (!intel_PLL_is_valid(dev, limit,
681 &clock))
682 continue;
683 if (match_clock &&
684 clock.p != match_clock->p)
685 continue;
686
687 this_err = abs(clock.dot - target);
688 if (this_err < err) {
689 *best_clock = clock;
690 err = this_err;
691 }
692 }
693 }
694 }
695 }
696
697 return (err != target);
698}
699
Ma Lingd4906092009-03-18 20:13:27 +0800700static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300701g4x_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200702 int target, int refclk, intel_clock_t *match_clock,
703 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800704{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300705 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800706 intel_clock_t clock;
707 int max_n;
708 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400709 /* approximately equals target * 0.00585 */
710 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800711 found = false;
712
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300713 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100714 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800715 clock.p2 = limit->p2.p2_fast;
716 else
717 clock.p2 = limit->p2.p2_slow;
718 } else {
719 if (target < limit->p2.dot_limit)
720 clock.p2 = limit->p2.p2_slow;
721 else
722 clock.p2 = limit->p2.p2_fast;
723 }
724
725 memset(best_clock, 0, sizeof(*best_clock));
726 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200727 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800728 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200729 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800730 for (clock.m1 = limit->m1.max;
731 clock.m1 >= limit->m1.min; clock.m1--) {
732 for (clock.m2 = limit->m2.max;
733 clock.m2 >= limit->m2.min; clock.m2--) {
734 for (clock.p1 = limit->p1.max;
735 clock.p1 >= limit->p1.min; clock.p1--) {
736 int this_err;
737
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000739 if (!intel_PLL_is_valid(dev, limit,
740 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800741 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000742
743 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800744 if (this_err < err_most) {
745 *best_clock = clock;
746 err_most = this_err;
747 max_n = clock.n;
748 found = true;
749 }
750 }
751 }
752 }
753 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800754 return found;
755}
Ma Lingd4906092009-03-18 20:13:27 +0800756
Zhenyu Wang2c072452009-06-05 15:38:42 +0800757static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300758vlv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200759 int target, int refclk, intel_clock_t *match_clock,
760 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700761{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300762 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300763 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300764 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300765 /* min update 19.2 MHz */
766 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300767 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700768
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300769 target *= 5; /* fast clock */
770
771 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700772
773 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300774 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300775 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300776 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300777 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300778 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700779 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300780 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300781 unsigned int ppm, diff;
782
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300783 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
784 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300785
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300786 vlv_clock(refclk, &clock);
787
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300788 if (!intel_PLL_is_valid(dev, limit,
789 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300790 continue;
791
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300792 diff = abs(clock.dot - target);
793 ppm = div_u64(1000000ULL * diff, target);
794
795 if (ppm < 100 && clock.p > best_clock->p) {
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300796 bestppm = 0;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300797 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300798 found = true;
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300799 }
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300800
Ville Syrjäläc6861222013-09-24 21:26:21 +0300801 if (bestppm >= 10 && ppm < bestppm - 10) {
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300802 bestppm = ppm;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300803 *best_clock = clock;
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300804 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700805 }
806 }
807 }
808 }
809 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700810
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300811 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700812}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700813
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300814static bool
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300815chv_find_best_dpll(const intel_limit_t *limit, struct intel_crtc *crtc,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300816 int target, int refclk, intel_clock_t *match_clock,
817 intel_clock_t *best_clock)
818{
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300819 struct drm_device *dev = crtc->base.dev;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300820 intel_clock_t clock;
821 uint64_t m2;
822 int found = false;
823
824 memset(best_clock, 0, sizeof(*best_clock));
825
826 /*
827 * Based on hardware doc, the n always set to 1, and m1 always
828 * set to 2. If requires to support 200Mhz refclk, we need to
829 * revisit this because n may not 1 anymore.
830 */
831 clock.n = 1, clock.m1 = 2;
832 target *= 5; /* fast clock */
833
834 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
835 for (clock.p2 = limit->p2.p2_fast;
836 clock.p2 >= limit->p2.p2_slow;
837 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
838
839 clock.p = clock.p1 * clock.p2;
840
841 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
842 clock.n) << 22, refclk * clock.m1);
843
844 if (m2 > INT_MAX/clock.m1)
845 continue;
846
847 clock.m2 = m2;
848
849 chv_clock(refclk, &clock);
850
851 if (!intel_PLL_is_valid(dev, limit, &clock))
852 continue;
853
854 /* based on hardware requirement, prefer bigger p
855 */
856 if (clock.p > best_clock->p) {
857 *best_clock = clock;
858 found = true;
859 }
860 }
861 }
862
863 return found;
864}
865
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300866bool intel_crtc_active(struct drm_crtc *crtc)
867{
868 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
869
870 /* Be paranoid as we can arrive here with only partial
871 * state retrieved from the hardware during setup.
872 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100873 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300874 * as Haswell has gained clock readout/fastboot support.
875 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000876 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300877 * properly reconstruct framebuffers.
878 */
Matt Roperf4510a22014-04-01 15:22:40 -0700879 return intel_crtc->active && crtc->primary->fb &&
Damien Lespiau241bfc32013-09-25 16:45:37 +0100880 intel_crtc->config.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300881}
882
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200883enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
884 enum pipe pipe)
885{
886 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
887 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
888
Daniel Vetter3b117c82013-04-17 20:15:07 +0200889 return intel_crtc->config.cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -0200890}
891
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300892static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
893{
894 struct drm_i915_private *dev_priv = dev->dev_private;
895 u32 reg = PIPEDSL(pipe);
896 u32 line1, line2;
897 u32 line_mask;
898
899 if (IS_GEN2(dev))
900 line_mask = DSL_LINEMASK_GEN2;
901 else
902 line_mask = DSL_LINEMASK_GEN3;
903
904 line1 = I915_READ(reg) & line_mask;
905 mdelay(5);
906 line2 = I915_READ(reg) & line_mask;
907
908 return line1 == line2;
909}
910
Keith Packardab7ad7f2010-10-03 00:33:06 -0700911/*
912 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300913 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700914 *
915 * After disabling a pipe, we can't wait for vblank in the usual way,
916 * spinning on the vblank interrupt status bit, since we won't actually
917 * see an interrupt when the pipe is disabled.
918 *
Keith Packardab7ad7f2010-10-03 00:33:06 -0700919 * On Gen4 and above:
920 * wait for the pipe register state bit to turn off
921 *
922 * Otherwise:
923 * wait for the display line value to settle (it usually
924 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +0100925 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700926 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300927static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700928{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300929 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700930 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +0300931 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
932 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700933
Keith Packardab7ad7f2010-10-03 00:33:06 -0700934 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -0200935 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -0700936
Keith Packardab7ad7f2010-10-03 00:33:06 -0700937 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +0100938 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
939 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200940 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700941 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -0700942 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +0300943 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +0200944 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -0700945 }
Jesse Barnes79e53942008-11-07 14:24:08 -0800946}
947
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000948/*
949 * ibx_digital_port_connected - is the specified port connected?
950 * @dev_priv: i915 private structure
951 * @port: the port to test
952 *
953 * Returns true if @port is connected, false otherwise.
954 */
955bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
956 struct intel_digital_port *port)
957{
958 u32 bit;
959
Damien Lespiauc36346e2012-12-13 16:09:03 +0000960 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +0200961 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000962 case PORT_B:
963 bit = SDE_PORTB_HOTPLUG;
964 break;
965 case PORT_C:
966 bit = SDE_PORTC_HOTPLUG;
967 break;
968 case PORT_D:
969 bit = SDE_PORTD_HOTPLUG;
970 break;
971 default:
972 return true;
973 }
974 } else {
Robin Schroereba905b2014-05-18 02:24:50 +0200975 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +0000976 case PORT_B:
977 bit = SDE_PORTB_HOTPLUG_CPT;
978 break;
979 case PORT_C:
980 bit = SDE_PORTC_HOTPLUG_CPT;
981 break;
982 case PORT_D:
983 bit = SDE_PORTD_HOTPLUG_CPT;
984 break;
985 default:
986 return true;
987 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +0000988 }
989
990 return I915_READ(SDEISR) & bit;
991}
992
Jesse Barnesb24e7172011-01-04 15:09:30 -0800993static const char *state_string(bool enabled)
994{
995 return enabled ? "on" : "off";
996}
997
998/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +0200999void assert_pll(struct drm_i915_private *dev_priv,
1000 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001001{
1002 int reg;
1003 u32 val;
1004 bool cur_state;
1005
1006 reg = DPLL(pipe);
1007 val = I915_READ(reg);
1008 cur_state = !!(val & DPLL_VCO_ENABLE);
1009 WARN(cur_state != state,
1010 "PLL state assertion failure (expected %s, current %s)\n",
1011 state_string(state), state_string(cur_state));
1012}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001013
Jani Nikula23538ef2013-08-27 15:12:22 +03001014/* XXX: the dsi pll is shared between MIPI DSI ports */
1015static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1016{
1017 u32 val;
1018 bool cur_state;
1019
1020 mutex_lock(&dev_priv->dpio_lock);
1021 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1022 mutex_unlock(&dev_priv->dpio_lock);
1023
1024 cur_state = val & DSI_PLL_VCO_EN;
1025 WARN(cur_state != state,
1026 "DSI PLL state assertion failure (expected %s, current %s)\n",
1027 state_string(state), state_string(cur_state));
1028}
1029#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1030#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1031
Daniel Vetter55607e82013-06-16 21:42:39 +02001032struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001033intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001034{
Daniel Vettere2b78262013-06-07 23:10:03 +02001035 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1036
Daniel Vettera43f6e02013-06-07 23:10:32 +02001037 if (crtc->config.shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001038 return NULL;
1039
Daniel Vettera43f6e02013-06-07 23:10:32 +02001040 return &dev_priv->shared_dplls[crtc->config.shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001041}
1042
Jesse Barnesb24e7172011-01-04 15:09:30 -08001043/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001044void assert_shared_dpll(struct drm_i915_private *dev_priv,
1045 struct intel_shared_dpll *pll,
1046 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001047{
Jesse Barnes040484a2011-01-03 12:14:26 -08001048 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001049 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001050
Chris Wilson92b27b02012-05-20 18:10:50 +01001051 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001052 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001053 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001054
Daniel Vetter53589012013-06-05 13:34:16 +02001055 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Chris Wilson92b27b02012-05-20 18:10:50 +01001056 WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001057 "%s assertion failure (expected %s, current %s)\n",
1058 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001059}
Jesse Barnes040484a2011-01-03 12:14:26 -08001060
1061static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1062 enum pipe pipe, bool state)
1063{
1064 int reg;
1065 u32 val;
1066 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001067 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1068 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001069
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001070 if (HAS_DDI(dev_priv->dev)) {
1071 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001072 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001073 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001074 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001075 } else {
1076 reg = FDI_TX_CTL(pipe);
1077 val = I915_READ(reg);
1078 cur_state = !!(val & FDI_TX_ENABLE);
1079 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001080 WARN(cur_state != state,
1081 "FDI TX state assertion failure (expected %s, current %s)\n",
1082 state_string(state), state_string(cur_state));
1083}
1084#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1085#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1086
1087static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1088 enum pipe pipe, bool state)
1089{
1090 int reg;
1091 u32 val;
1092 bool cur_state;
1093
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001094 reg = FDI_RX_CTL(pipe);
1095 val = I915_READ(reg);
1096 cur_state = !!(val & FDI_RX_ENABLE);
Jesse Barnes040484a2011-01-03 12:14:26 -08001097 WARN(cur_state != state,
1098 "FDI RX state assertion failure (expected %s, current %s)\n",
1099 state_string(state), state_string(cur_state));
1100}
1101#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1102#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1103
1104static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1105 enum pipe pipe)
1106{
1107 int reg;
1108 u32 val;
1109
1110 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001111 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001112 return;
1113
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001114 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001115 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001116 return;
1117
Jesse Barnes040484a2011-01-03 12:14:26 -08001118 reg = FDI_TX_CTL(pipe);
1119 val = I915_READ(reg);
1120 WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
1121}
1122
Daniel Vetter55607e82013-06-16 21:42:39 +02001123void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001125{
1126 int reg;
1127 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001128 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001129
1130 reg = FDI_RX_CTL(pipe);
1131 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001132 cur_state = !!(val & FDI_RX_PLL_ENABLE);
1133 WARN(cur_state != state,
1134 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001136}
1137
Daniel Vetterb680c372014-09-19 18:27:27 +02001138void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1139 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001140{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001141 struct drm_device *dev = dev_priv->dev;
1142 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001143 u32 val;
1144 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001145 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001146
Jani Nikulabedd4db2014-08-22 15:04:13 +03001147 if (WARN_ON(HAS_DDI(dev)))
1148 return;
1149
1150 if (HAS_PCH_SPLIT(dev)) {
1151 u32 port_sel;
1152
Jesse Barnesea0760c2011-01-04 15:09:32 -08001153 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001154 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1155
1156 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1157 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1158 panel_pipe = PIPE_B;
1159 /* XXX: else fix for eDP */
1160 } else if (IS_VALLEYVIEW(dev)) {
1161 /* presumably write lock depends on pipe, not port select */
1162 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1163 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001164 } else {
1165 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001166 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1167 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001168 }
1169
1170 val = I915_READ(pp_reg);
1171 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001172 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001173 locked = false;
1174
Jesse Barnesea0760c2011-01-04 15:09:32 -08001175 WARN(panel_pipe == pipe && locked,
1176 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001177 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001178}
1179
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001180static void assert_cursor(struct drm_i915_private *dev_priv,
1181 enum pipe pipe, bool state)
1182{
1183 struct drm_device *dev = dev_priv->dev;
1184 bool cur_state;
1185
Paulo Zanonid9d82082014-02-27 16:30:56 -03001186 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001187 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001188 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001189 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001190
1191 WARN(cur_state != state,
1192 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1193 pipe_name(pipe), state_string(state), state_string(cur_state));
1194}
1195#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1196#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1197
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001198void assert_pipe(struct drm_i915_private *dev_priv,
1199 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001200{
1201 int reg;
1202 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001203 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001204 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1205 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001206
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001207 /* if we need the pipe quirk it must be always on */
1208 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1209 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001210 state = true;
1211
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001212 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001213 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001214 cur_state = false;
1215 } else {
1216 reg = PIPECONF(cpu_transcoder);
1217 val = I915_READ(reg);
1218 cur_state = !!(val & PIPECONF_ENABLE);
1219 }
1220
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001221 WARN(cur_state != state,
1222 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001223 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001224}
1225
Chris Wilson931872f2012-01-16 23:01:13 +00001226static void assert_plane(struct drm_i915_private *dev_priv,
1227 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001228{
1229 int reg;
1230 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001231 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001232
1233 reg = DSPCNTR(plane);
1234 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001235 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
1236 WARN(cur_state != state,
1237 "plane %c assertion failure (expected %s, current %s)\n",
1238 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001239}
1240
Chris Wilson931872f2012-01-16 23:01:13 +00001241#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1242#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1243
Jesse Barnesb24e7172011-01-04 15:09:30 -08001244static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1245 enum pipe pipe)
1246{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001247 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001248 int reg, i;
1249 u32 val;
1250 int cur_pipe;
1251
Ville Syrjälä653e1022013-06-04 13:49:05 +03001252 /* Primary planes are fixed to pipes on gen4+ */
1253 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001254 reg = DSPCNTR(pipe);
1255 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001256 WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001257 "plane %c assertion failure, should be disabled but not\n",
1258 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001259 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001260 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001261
Jesse Barnesb24e7172011-01-04 15:09:30 -08001262 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001263 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001264 reg = DSPCNTR(i);
1265 val = I915_READ(reg);
1266 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1267 DISPPLANE_SEL_PIPE_SHIFT;
1268 WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001269 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1270 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001271 }
1272}
1273
Jesse Barnes19332d72013-03-28 09:55:38 -07001274static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1275 enum pipe pipe)
1276{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001277 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001278 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001279 u32 val;
1280
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001281 if (INTEL_INFO(dev)->gen >= 9) {
1282 for_each_sprite(pipe, sprite) {
1283 val = I915_READ(PLANE_CTL(pipe, sprite));
1284 WARN(val & PLANE_CTL_ENABLE,
1285 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1286 sprite, pipe_name(pipe));
1287 }
1288 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001289 for_each_sprite(pipe, sprite) {
1290 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001291 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001292 WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001293 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001294 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001295 }
1296 } else if (INTEL_INFO(dev)->gen >= 7) {
1297 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001298 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001299 WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001300 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001301 plane_name(pipe), pipe_name(pipe));
1302 } else if (INTEL_INFO(dev)->gen >= 5) {
1303 reg = DVSCNTR(pipe);
1304 val = I915_READ(reg);
Damien Lespiau83f26f12014-03-17 17:59:48 +00001305 WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001306 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1307 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001308 }
1309}
1310
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001311static void assert_vblank_disabled(struct drm_crtc *crtc)
1312{
1313 if (WARN_ON(drm_crtc_vblank_get(crtc) == 0))
1314 drm_crtc_vblank_put(crtc);
1315}
1316
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001317static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001318{
1319 u32 val;
1320 bool enabled;
1321
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001322 WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001323
Jesse Barnes92f25842011-01-04 15:09:34 -08001324 val = I915_READ(PCH_DREF_CONTROL);
1325 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1326 DREF_SUPERSPREAD_SOURCE_MASK));
1327 WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
1328}
1329
Daniel Vetterab9412b2013-05-03 11:49:46 +02001330static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1331 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001332{
1333 int reg;
1334 u32 val;
1335 bool enabled;
1336
Daniel Vetterab9412b2013-05-03 11:49:46 +02001337 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001338 val = I915_READ(reg);
1339 enabled = !!(val & TRANS_ENABLE);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001340 WARN(enabled,
1341 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1342 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001343}
1344
Keith Packard4e634382011-08-06 10:39:45 -07001345static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1346 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001347{
1348 if ((val & DP_PORT_EN) == 0)
1349 return false;
1350
1351 if (HAS_PCH_CPT(dev_priv->dev)) {
1352 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1353 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1354 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1355 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001356 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1357 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1358 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001359 } else {
1360 if ((val & DP_PIPE_MASK) != (pipe << 30))
1361 return false;
1362 }
1363 return true;
1364}
1365
Keith Packard1519b992011-08-06 10:35:34 -07001366static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1367 enum pipe pipe, u32 val)
1368{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001369 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001370 return false;
1371
1372 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001373 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001374 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001375 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1376 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1377 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001378 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001379 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001380 return false;
1381 }
1382 return true;
1383}
1384
1385static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1386 enum pipe pipe, u32 val)
1387{
1388 if ((val & LVDS_PORT_EN) == 0)
1389 return false;
1390
1391 if (HAS_PCH_CPT(dev_priv->dev)) {
1392 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1393 return false;
1394 } else {
1395 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1396 return false;
1397 }
1398 return true;
1399}
1400
1401static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1402 enum pipe pipe, u32 val)
1403{
1404 if ((val & ADPA_DAC_ENABLE) == 0)
1405 return false;
1406 if (HAS_PCH_CPT(dev_priv->dev)) {
1407 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1408 return false;
1409 } else {
1410 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1411 return false;
1412 }
1413 return true;
1414}
1415
Jesse Barnes291906f2011-02-02 12:28:03 -08001416static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001417 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001418{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001419 u32 val = I915_READ(reg);
Keith Packard4e634382011-08-06 10:39:45 -07001420 WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001421 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001422 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001423
Daniel Vetter75c5da22012-09-10 21:58:29 +02001424 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
1425 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001426 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001427}
1428
1429static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1430 enum pipe pipe, int reg)
1431{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001432 u32 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001433 WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001434 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001435 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001436
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001437 WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001438 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001439 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001440}
1441
1442static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1443 enum pipe pipe)
1444{
1445 int reg;
1446 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001447
Keith Packardf0575e92011-07-25 22:12:43 -07001448 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1449 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1450 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001451
1452 reg = PCH_ADPA;
1453 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001454 WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001455 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001456 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001457
1458 reg = PCH_LVDS;
1459 val = I915_READ(reg);
Xu, Anhuab70ad582012-08-13 03:08:33 +00001460 WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001461 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001462 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001463
Paulo Zanonie2debe92013-02-18 19:00:27 -03001464 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1465 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1466 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001467}
1468
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001469static void intel_init_dpio(struct drm_device *dev)
1470{
1471 struct drm_i915_private *dev_priv = dev->dev_private;
1472
1473 if (!IS_VALLEYVIEW(dev))
1474 return;
1475
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001476 /*
1477 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1478 * CHV x1 PHY (DP/HDMI D)
1479 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1480 */
1481 if (IS_CHERRYVIEW(dev)) {
1482 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1483 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1484 } else {
1485 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1486 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001487}
1488
Ville Syrjäläd288f652014-10-28 13:20:22 +02001489static void vlv_enable_pll(struct intel_crtc *crtc,
1490 const struct intel_crtc_config *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001491{
Daniel Vetter426115c2013-07-11 22:13:42 +02001492 struct drm_device *dev = crtc->base.dev;
1493 struct drm_i915_private *dev_priv = dev->dev_private;
1494 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001495 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001496
Daniel Vetter426115c2013-07-11 22:13:42 +02001497 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001498
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001499 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001500 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1501
1502 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001503 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001504 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001505
Daniel Vetter426115c2013-07-11 22:13:42 +02001506 I915_WRITE(reg, dpll);
1507 POSTING_READ(reg);
1508 udelay(150);
1509
1510 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1511 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1512
Ville Syrjäläd288f652014-10-28 13:20:22 +02001513 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001514 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001515
1516 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001517 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001518 POSTING_READ(reg);
1519 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001520 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001521 POSTING_READ(reg);
1522 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001523 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001524 POSTING_READ(reg);
1525 udelay(150); /* wait for warmup */
1526}
1527
Ville Syrjäläd288f652014-10-28 13:20:22 +02001528static void chv_enable_pll(struct intel_crtc *crtc,
1529 const struct intel_crtc_config *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001530{
1531 struct drm_device *dev = crtc->base.dev;
1532 struct drm_i915_private *dev_priv = dev->dev_private;
1533 int pipe = crtc->pipe;
1534 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001535 u32 tmp;
1536
1537 assert_pipe_disabled(dev_priv, crtc->pipe);
1538
1539 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1540
1541 mutex_lock(&dev_priv->dpio_lock);
1542
1543 /* Enable back the 10bit clock to display controller */
1544 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1545 tmp |= DPIO_DCLKP_EN;
1546 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1547
1548 /*
1549 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1550 */
1551 udelay(1);
1552
1553 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001554 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001555
1556 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001557 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001558 DRM_ERROR("PLL %d failed to lock\n", pipe);
1559
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001560 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001561 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001562 POSTING_READ(DPLL_MD(pipe));
1563
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001564 mutex_unlock(&dev_priv->dpio_lock);
1565}
1566
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001567static int intel_num_dvo_pipes(struct drm_device *dev)
1568{
1569 struct intel_crtc *crtc;
1570 int count = 0;
1571
1572 for_each_intel_crtc(dev, crtc)
1573 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001574 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001575
1576 return count;
1577}
1578
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001579static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001580{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001581 struct drm_device *dev = crtc->base.dev;
1582 struct drm_i915_private *dev_priv = dev->dev_private;
1583 int reg = DPLL(crtc->pipe);
1584 u32 dpll = crtc->config.dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001585
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001586 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001587
1588 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001589 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001590
1591 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001592 if (IS_MOBILE(dev) && !IS_I830(dev))
1593 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001594
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001595 /* Enable DVO 2x clock on both PLLs if necessary */
1596 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1597 /*
1598 * It appears to be important that we don't enable this
1599 * for the current pipe before otherwise configuring the
1600 * PLL. No idea how this should be handled if multiple
1601 * DVO outputs are enabled simultaneosly.
1602 */
1603 dpll |= DPLL_DVO_2X_MODE;
1604 I915_WRITE(DPLL(!crtc->pipe),
1605 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1606 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001607
1608 /* Wait for the clocks to stabilize. */
1609 POSTING_READ(reg);
1610 udelay(150);
1611
1612 if (INTEL_INFO(dev)->gen >= 4) {
1613 I915_WRITE(DPLL_MD(crtc->pipe),
1614 crtc->config.dpll_hw_state.dpll_md);
1615 } else {
1616 /* The pixel multiplier can only be updated once the
1617 * DPLL is enabled and the clocks are stable.
1618 *
1619 * So write it again.
1620 */
1621 I915_WRITE(reg, dpll);
1622 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623
1624 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001625 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001626 POSTING_READ(reg);
1627 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001628 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001629 POSTING_READ(reg);
1630 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001631 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001632 POSTING_READ(reg);
1633 udelay(150); /* wait for warmup */
1634}
1635
1636/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001637 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001638 * @dev_priv: i915 private structure
1639 * @pipe: pipe PLL to disable
1640 *
1641 * Disable the PLL for @pipe, making sure the pipe is off first.
1642 *
1643 * Note! This is for pre-ILK only.
1644 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001645static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001646{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001647 struct drm_device *dev = crtc->base.dev;
1648 struct drm_i915_private *dev_priv = dev->dev_private;
1649 enum pipe pipe = crtc->pipe;
1650
1651 /* Disable DVO 2x clock on both PLLs if necessary */
1652 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001653 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001654 intel_num_dvo_pipes(dev) == 1) {
1655 I915_WRITE(DPLL(PIPE_B),
1656 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1657 I915_WRITE(DPLL(PIPE_A),
1658 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1659 }
1660
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001661 /* Don't disable pipe or pipe PLLs if needed */
1662 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1663 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001664 return;
1665
1666 /* Make sure the pipe isn't still relying on us */
1667 assert_pipe_disabled(dev_priv, pipe);
1668
Daniel Vetter50b44a42013-06-05 13:34:33 +02001669 I915_WRITE(DPLL(pipe), 0);
1670 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001671}
1672
Jesse Barnesf6071162013-10-01 10:41:38 -07001673static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1674{
1675 u32 val = 0;
1676
1677 /* Make sure the pipe isn't still relying on us */
1678 assert_pipe_disabled(dev_priv, pipe);
1679
Imre Deake5cbfbf2014-01-09 17:08:16 +02001680 /*
1681 * Leave integrated clock source and reference clock enabled for pipe B.
1682 * The latter is needed for VGA hotplug / manual detection.
1683 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001684 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001685 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001686 I915_WRITE(DPLL(pipe), val);
1687 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001688
1689}
1690
1691static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1692{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001693 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001694 u32 val;
1695
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001696 /* Make sure the pipe isn't still relying on us */
1697 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001698
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001699 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001700 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001701 if (pipe != PIPE_A)
1702 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1703 I915_WRITE(DPLL(pipe), val);
1704 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001705
1706 mutex_lock(&dev_priv->dpio_lock);
1707
1708 /* Disable 10bit clock to display controller */
1709 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1710 val &= ~DPIO_DCLKP_EN;
1711 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1712
Ville Syrjälä61407f62014-05-27 16:32:55 +03001713 /* disable left/right clock distribution */
1714 if (pipe != PIPE_B) {
1715 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1716 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1717 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1718 } else {
1719 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1720 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1721 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1722 }
1723
Ville Syrjäläd7520482014-04-09 13:28:59 +03001724 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001725}
1726
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001727void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1728 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001729{
1730 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001731 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001732
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001733 switch (dport->port) {
1734 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001735 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001736 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001737 break;
1738 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001739 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001740 dpll_reg = DPLL(0);
1741 break;
1742 case PORT_D:
1743 port_mask = DPLL_PORTD_READY_MASK;
1744 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001745 break;
1746 default:
1747 BUG();
1748 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001749
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001750 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001751 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001752 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001753}
1754
Daniel Vetterb14b1052014-04-24 23:55:13 +02001755static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1756{
1757 struct drm_device *dev = crtc->base.dev;
1758 struct drm_i915_private *dev_priv = dev->dev_private;
1759 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1760
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001761 if (WARN_ON(pll == NULL))
1762 return;
1763
Daniel Vetterb14b1052014-04-24 23:55:13 +02001764 WARN_ON(!pll->refcount);
1765 if (pll->active == 0) {
1766 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1767 WARN_ON(pll->on);
1768 assert_shared_dpll_disabled(dev_priv, pll);
1769
1770 pll->mode_set(dev_priv, pll);
1771 }
1772}
1773
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001774/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001775 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001776 * @dev_priv: i915 private structure
1777 * @pipe: pipe PLL to enable
1778 *
1779 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1780 * drives the transcoder clock.
1781 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001782static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001783{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001784 struct drm_device *dev = crtc->base.dev;
1785 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001786 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001787
Daniel Vetter87a875b2013-06-05 13:34:19 +02001788 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001789 return;
1790
1791 if (WARN_ON(pll->refcount == 0))
1792 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001793
Damien Lespiau74dd6922014-07-29 18:06:17 +01001794 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001795 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001796 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001797
Daniel Vettercdbd2312013-06-05 13:34:03 +02001798 if (pll->active++) {
1799 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001800 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001801 return;
1802 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001803 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001804
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001805 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1806
Daniel Vetter46edb022013-06-05 13:34:12 +02001807 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001808 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001809 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001810}
1811
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001812static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001813{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001814 struct drm_device *dev = crtc->base.dev;
1815 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001816 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001817
Jesse Barnes92f25842011-01-04 15:09:34 -08001818 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001819 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001820 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001821 return;
1822
Chris Wilson48da64a2012-05-13 20:16:12 +01001823 if (WARN_ON(pll->refcount == 0))
1824 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001825
Daniel Vetter46edb022013-06-05 13:34:12 +02001826 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1827 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001828 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001829
Chris Wilson48da64a2012-05-13 20:16:12 +01001830 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001831 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001832 return;
1833 }
1834
Daniel Vettere9d69442013-06-05 13:34:15 +02001835 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001836 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001837 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001838 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001839
Daniel Vetter46edb022013-06-05 13:34:12 +02001840 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001841 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001842 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001843
1844 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001845}
1846
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001847static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1848 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001849{
Daniel Vetter23670b322012-11-01 09:15:30 +01001850 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001851 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001853 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001854
1855 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001856 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001857
1858 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001859 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001860 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001861
1862 /* FDI must be feeding us bits for PCH ports */
1863 assert_fdi_tx_enabled(dev_priv, pipe);
1864 assert_fdi_rx_enabled(dev_priv, pipe);
1865
Daniel Vetter23670b322012-11-01 09:15:30 +01001866 if (HAS_PCH_CPT(dev)) {
1867 /* Workaround: Set the timing override bit before enabling the
1868 * pch transcoder. */
1869 reg = TRANS_CHICKEN2(pipe);
1870 val = I915_READ(reg);
1871 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1872 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001873 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001874
Daniel Vetterab9412b2013-05-03 11:49:46 +02001875 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001876 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001877 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001878
1879 if (HAS_PCH_IBX(dev_priv->dev)) {
1880 /*
1881 * make the BPC in transcoder be consistent with
1882 * that in pipeconf reg.
1883 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01001884 val &= ~PIPECONF_BPC_MASK;
1885 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07001886 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001887
1888 val &= ~TRANS_INTERLACE_MASK;
1889 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001890 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001891 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001892 val |= TRANS_LEGACY_INTERLACED_ILK;
1893 else
1894 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001895 else
1896 val |= TRANS_PROGRESSIVE;
1897
Jesse Barnes040484a2011-01-03 12:14:26 -08001898 I915_WRITE(reg, val | TRANS_ENABLE);
1899 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001900 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08001901}
1902
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001903static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02001904 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08001905{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001906 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001907
1908 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001909 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001910
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001911 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01001912 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02001913 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001914
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001915 /* Workaround: set timing override bit. */
1916 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001917 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001918 I915_WRITE(_TRANSA_CHICKEN2, val);
1919
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02001920 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02001921 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001922
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02001923 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
1924 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02001925 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001926 else
1927 val |= TRANS_PROGRESSIVE;
1928
Daniel Vetterab9412b2013-05-03 11:49:46 +02001929 I915_WRITE(LPT_TRANSCONF, val);
1930 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02001931 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001932}
1933
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001934static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
1935 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001936{
Daniel Vetter23670b322012-11-01 09:15:30 +01001937 struct drm_device *dev = dev_priv->dev;
1938 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001939
1940 /* FDI relies on the transcoder */
1941 assert_fdi_tx_disabled(dev_priv, pipe);
1942 assert_fdi_rx_disabled(dev_priv, pipe);
1943
Jesse Barnes291906f2011-02-02 12:28:03 -08001944 /* Ports must be off as well */
1945 assert_pch_ports_disabled(dev_priv, pipe);
1946
Daniel Vetterab9412b2013-05-03 11:49:46 +02001947 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001948 val = I915_READ(reg);
1949 val &= ~TRANS_ENABLE;
1950 I915_WRITE(reg, val);
1951 /* wait for PCH transcoder off, transcoder state */
1952 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03001953 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01001954
1955 if (!HAS_PCH_IBX(dev)) {
1956 /* Workaround: Clear the timing override chicken bit again. */
1957 reg = TRANS_CHICKEN2(pipe);
1958 val = I915_READ(reg);
1959 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
1960 I915_WRITE(reg, val);
1961 }
Jesse Barnes040484a2011-01-03 12:14:26 -08001962}
1963
Paulo Zanoniab4d9662012-10-31 18:12:55 -02001964static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001965{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001966 u32 val;
1967
Daniel Vetterab9412b2013-05-03 11:49:46 +02001968 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001969 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02001970 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02001971 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02001972 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02001973 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001974
1975 /* Workaround: clear timing override bit. */
1976 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01001977 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02001978 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08001979}
1980
1981/**
Chris Wilson309cfea2011-01-28 13:54:53 +00001982 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02001983 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08001984 *
Paulo Zanoni03722642014-01-17 13:51:09 -02001985 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08001986 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08001987 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02001988static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001989{
Paulo Zanoni03722642014-01-17 13:51:09 -02001990 struct drm_device *dev = crtc->base.dev;
1991 struct drm_i915_private *dev_priv = dev->dev_private;
1992 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001993 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1994 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01001995 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001996 int reg;
1997 u32 val;
1998
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001999 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002000 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002001 assert_sprites_disabled(dev_priv, pipe);
2002
Paulo Zanoni681e5812012-12-06 11:12:38 -02002003 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002004 pch_transcoder = TRANSCODER_A;
2005 else
2006 pch_transcoder = pipe;
2007
Jesse Barnesb24e7172011-01-04 15:09:30 -08002008 /*
2009 * A pipe without a PLL won't actually be able to drive bits from
2010 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2011 * need the check.
2012 */
2013 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002014 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002015 assert_dsi_pll_enabled(dev_priv);
2016 else
2017 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002018 else {
Paulo Zanoni30421c42014-01-17 13:51:10 -02002019 if (crtc->config.has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002020 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002021 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002022 assert_fdi_tx_pll_enabled(dev_priv,
2023 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002024 }
2025 /* FIXME: assert CPU port conditions for SNB+ */
2026 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002027
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002028 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002029 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002030 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002031 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2032 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002033 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002034 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002035
2036 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002037 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002038}
2039
2040/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002041 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002042 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002043 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002044 * Disable the pipe of @crtc, making sure that various hardware
2045 * specific requirements are met, if applicable, e.g. plane
2046 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002047 *
2048 * Will wait until the pipe has shut down before returning.
2049 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002050static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002051{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002052 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
2053 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
2054 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002055 int reg;
2056 u32 val;
2057
2058 /*
2059 * Make sure planes won't keep trying to pump pixels to us,
2060 * or we might hang the display.
2061 */
2062 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002063 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002064 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002065
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002066 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002067 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002068 if ((val & PIPECONF_ENABLE) == 0)
2069 return;
2070
Ville Syrjälä67adc642014-08-15 01:21:57 +03002071 /*
2072 * Double wide has implications for planes
2073 * so best keep it disabled when not needed.
2074 */
2075 if (crtc->config.double_wide)
2076 val &= ~PIPECONF_DOUBLE_WIDE;
2077
2078 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002079 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2080 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002081 val &= ~PIPECONF_ENABLE;
2082
2083 I915_WRITE(reg, val);
2084 if ((val & PIPECONF_ENABLE) == 0)
2085 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002086}
2087
Keith Packardd74362c2011-07-28 14:47:14 -07002088/*
2089 * Plane regs are double buffered, going from enabled->disabled needs a
2090 * trigger in order to latch. The display address reg provides this.
2091 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002092void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2093 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002094{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002095 struct drm_device *dev = dev_priv->dev;
2096 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002097
2098 I915_WRITE(reg, I915_READ(reg));
2099 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002100}
2101
Jesse Barnesb24e7172011-01-04 15:09:30 -08002102/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002103 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002104 * @plane: plane to be enabled
2105 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002106 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002107 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002109static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2110 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002111{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002112 struct drm_device *dev = plane->dev;
2113 struct drm_i915_private *dev_priv = dev->dev_private;
2114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002115
2116 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002117 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118
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
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002124 dev_priv->display.update_primary_plane(crtc, plane->fb,
2125 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002126
2127 /*
2128 * BDW signals flip done immediately if the plane
2129 * is disabled, even if the plane enable is already
2130 * armed to occur at the next vblank :(
2131 */
2132 if (IS_BROADWELL(dev))
2133 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002134}
2135
Jesse Barnesb24e7172011-01-04 15:09:30 -08002136/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002137 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002138 * @plane: plane to be disabled
2139 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002140 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002141 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002142 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002143static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2144 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002145{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002146 struct drm_device *dev = plane->dev;
2147 struct drm_i915_private *dev_priv = dev->dev_private;
2148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2149
2150 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002151
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002152 if (!intel_crtc->primary_enabled)
2153 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002154
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002155 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002156
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002157 dev_priv->display.update_primary_plane(crtc, plane->fb,
2158 crtc->x, crtc->y);
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
Matt Roperebcdd392014-07-09 16:22:11 -07002187 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2188
Chris Wilson05394f32010-11-08 19:18:58 +00002189 switch (obj->tiling_mode) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002190 case I915_TILING_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002191 if (INTEL_INFO(dev)->gen >= 9)
2192 alignment = 256 * 1024;
2193 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002194 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002195 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002196 alignment = 4 * 1024;
2197 else
2198 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002199 break;
2200 case I915_TILING_X:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002201 if (INTEL_INFO(dev)->gen >= 9)
2202 alignment = 256 * 1024;
2203 else {
2204 /* pin() will align the object as required by fence */
2205 alignment = 0;
2206 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002207 break;
2208 case I915_TILING_Y:
Daniel Vetter80075d42013-10-09 21:23:52 +02002209 WARN(1, "Y tiled bo slipped through, driver bug!\n");
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002210 return -EINVAL;
2211 default:
2212 BUG();
2213 }
2214
Chris Wilson693db182013-03-05 14:52:39 +00002215 /* Note that the w/a also requires 64 PTE of padding following the
2216 * bo. We currently fill all unused PTE with the shadow page and so
2217 * we should always have valid PTE following the scanout preventing
2218 * the VT-d warning.
2219 */
2220 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2221 alignment = 256 * 1024;
2222
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002223 /*
2224 * Global gtt pte registers are special registers which actually forward
2225 * writes to a chunk of system memory. Which means that there is no risk
2226 * that the register values disappear as soon as we call
2227 * intel_runtime_pm_put(), so it is correct to wrap only the
2228 * pin/unpin/fence and not more.
2229 */
2230 intel_runtime_pm_get(dev_priv);
2231
Chris Wilsonce453d82011-02-21 14:43:56 +00002232 dev_priv->mm.interruptible = false;
Chris Wilson2da3b9b2011-04-14 09:41:17 +01002233 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined);
Chris Wilson48b956c2010-09-14 12:50:34 +01002234 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002235 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002236
2237 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2238 * fence, whereas 965+ only requires a fence if using
2239 * framebuffer compression. For simplicity, we always install
2240 * a fence as the cost is not that onerous.
2241 */
Chris Wilson06d98132012-04-17 15:31:24 +01002242 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002243 if (ret)
2244 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002245
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002246 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002247
Chris Wilsonce453d82011-02-21 14:43:56 +00002248 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002249 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002250 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002251
2252err_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01002253 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilsonce453d82011-02-21 14:43:56 +00002254err_interruptible:
2255 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002256 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002257 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002258}
2259
Chris Wilson1690e1e2011-12-14 13:57:08 +01002260void intel_unpin_fb_obj(struct drm_i915_gem_object *obj)
2261{
Matt Roperebcdd392014-07-09 16:22:11 -07002262 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2263
Chris Wilson1690e1e2011-12-14 13:57:08 +01002264 i915_gem_object_unpin_fence(obj);
Chris Wilsoncc98b412013-08-09 12:25:09 +01002265 i915_gem_object_unpin_from_display_plane(obj);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002266}
2267
Daniel Vetterc2c75132012-07-05 12:17:30 +02002268/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2269 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002270unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2271 unsigned int tiling_mode,
2272 unsigned int cpp,
2273 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002274{
Chris Wilsonbc752862013-02-21 20:04:31 +00002275 if (tiling_mode != I915_TILING_NONE) {
2276 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002277
Chris Wilsonbc752862013-02-21 20:04:31 +00002278 tile_rows = *y / 8;
2279 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002280
Chris Wilsonbc752862013-02-21 20:04:31 +00002281 tiles = *x / (512/cpp);
2282 *x %= 512/cpp;
2283
2284 return tile_rows * pitch * 8 + tiles * 4096;
2285 } else {
2286 unsigned int offset;
2287
2288 offset = *y * pitch + *x * cpp;
2289 *y = 0;
2290 *x = (offset & 4095) / cpp;
2291 return offset & -4096;
2292 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002293}
2294
Jesse Barnes46f297f2014-03-07 08:57:48 -08002295int intel_format_to_fourcc(int format)
2296{
2297 switch (format) {
2298 case DISPPLANE_8BPP:
2299 return DRM_FORMAT_C8;
2300 case DISPPLANE_BGRX555:
2301 return DRM_FORMAT_XRGB1555;
2302 case DISPPLANE_BGRX565:
2303 return DRM_FORMAT_RGB565;
2304 default:
2305 case DISPPLANE_BGRX888:
2306 return DRM_FORMAT_XRGB8888;
2307 case DISPPLANE_RGBX888:
2308 return DRM_FORMAT_XBGR8888;
2309 case DISPPLANE_BGRX101010:
2310 return DRM_FORMAT_XRGB2101010;
2311 case DISPPLANE_RGBX101010:
2312 return DRM_FORMAT_XBGR2101010;
2313 }
2314}
2315
Jesse Barnes484b41d2014-03-07 08:57:55 -08002316static bool intel_alloc_plane_obj(struct intel_crtc *crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -08002317 struct intel_plane_config *plane_config)
2318{
2319 struct drm_device *dev = crtc->base.dev;
2320 struct drm_i915_gem_object *obj = NULL;
2321 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2322 u32 base = plane_config->base;
2323
Chris Wilsonff2652e2014-03-10 08:07:02 +00002324 if (plane_config->size == 0)
2325 return false;
2326
Jesse Barnes46f297f2014-03-07 08:57:48 -08002327 obj = i915_gem_object_create_stolen_for_preallocated(dev, base, base,
2328 plane_config->size);
2329 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002330 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002331
2332 if (plane_config->tiled) {
2333 obj->tiling_mode = I915_TILING_X;
Dave Airlie66e514c2014-04-03 07:51:54 +10002334 obj->stride = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002335 }
2336
Dave Airlie66e514c2014-04-03 07:51:54 +10002337 mode_cmd.pixel_format = crtc->base.primary->fb->pixel_format;
2338 mode_cmd.width = crtc->base.primary->fb->width;
2339 mode_cmd.height = crtc->base.primary->fb->height;
2340 mode_cmd.pitches[0] = crtc->base.primary->fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002341
2342 mutex_lock(&dev->struct_mutex);
2343
Dave Airlie66e514c2014-04-03 07:51:54 +10002344 if (intel_framebuffer_init(dev, to_intel_framebuffer(crtc->base.primary->fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002345 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002346 DRM_DEBUG_KMS("intel fb init failed\n");
2347 goto out_unref_obj;
2348 }
2349
Daniel Vettera071fa02014-06-18 23:28:09 +02002350 obj->frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002351 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002352
2353 DRM_DEBUG_KMS("plane fb obj %p\n", obj);
2354 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002355
2356out_unref_obj:
2357 drm_gem_object_unreference(&obj->base);
2358 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002359 return false;
2360}
2361
2362static void intel_find_plane_obj(struct intel_crtc *intel_crtc,
2363 struct intel_plane_config *plane_config)
2364{
2365 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002366 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002367 struct drm_crtc *c;
2368 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002369 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002370
Dave Airlie66e514c2014-04-03 07:51:54 +10002371 if (!intel_crtc->base.primary->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002372 return;
2373
2374 if (intel_alloc_plane_obj(intel_crtc, plane_config))
2375 return;
2376
Dave Airlie66e514c2014-04-03 07:51:54 +10002377 kfree(intel_crtc->base.primary->fb);
2378 intel_crtc->base.primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002379
2380 /*
2381 * Failed to alloc the obj, check to see if we should share
2382 * an fb with another CRTC instead
2383 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002384 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002385 i = to_intel_crtc(c);
2386
2387 if (c == &intel_crtc->base)
2388 continue;
2389
Matt Roper2ff8fde2014-07-08 07:50:07 -07002390 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002391 continue;
2392
Matt Roper2ff8fde2014-07-08 07:50:07 -07002393 obj = intel_fb_obj(c->primary->fb);
2394 if (obj == NULL)
2395 continue;
2396
2397 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002398 if (obj->tiling_mode != I915_TILING_NONE)
2399 dev_priv->preserve_bios_swizzle = true;
2400
Dave Airlie66e514c2014-04-03 07:51:54 +10002401 drm_framebuffer_reference(c->primary->fb);
2402 intel_crtc->base.primary->fb = c->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002403 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002404 break;
2405 }
2406 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002407}
2408
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002409static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2410 struct drm_framebuffer *fb,
2411 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002412{
2413 struct drm_device *dev = crtc->dev;
2414 struct drm_i915_private *dev_priv = dev->dev_private;
2415 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002416 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002417 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002418 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002419 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002420 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302421 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002422
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002423 if (!intel_crtc->primary_enabled) {
2424 I915_WRITE(reg, 0);
2425 if (INTEL_INFO(dev)->gen >= 4)
2426 I915_WRITE(DSPSURF(plane), 0);
2427 else
2428 I915_WRITE(DSPADDR(plane), 0);
2429 POSTING_READ(reg);
2430 return;
2431 }
2432
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002433 obj = intel_fb_obj(fb);
2434 if (WARN_ON(obj == NULL))
2435 return;
2436
2437 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2438
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002439 dspcntr = DISPPLANE_GAMMA_ENABLE;
2440
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002441 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002442
2443 if (INTEL_INFO(dev)->gen < 4) {
2444 if (intel_crtc->pipe == PIPE_B)
2445 dspcntr |= DISPPLANE_SEL_PIPE_B;
2446
2447 /* pipesrc and dspsize control the size that is scaled from,
2448 * which should always be the user's requested size.
2449 */
2450 I915_WRITE(DSPSIZE(plane),
2451 ((intel_crtc->config.pipe_src_h - 1) << 16) |
2452 (intel_crtc->config.pipe_src_w - 1));
2453 I915_WRITE(DSPPOS(plane), 0);
2454 }
2455
Ville Syrjälä57779d02012-10-31 17:50:14 +02002456 switch (fb->pixel_format) {
2457 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002458 dspcntr |= DISPPLANE_8BPP;
2459 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002460 case DRM_FORMAT_XRGB1555:
2461 case DRM_FORMAT_ARGB1555:
2462 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002463 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002464 case DRM_FORMAT_RGB565:
2465 dspcntr |= DISPPLANE_BGRX565;
2466 break;
2467 case DRM_FORMAT_XRGB8888:
2468 case DRM_FORMAT_ARGB8888:
2469 dspcntr |= DISPPLANE_BGRX888;
2470 break;
2471 case DRM_FORMAT_XBGR8888:
2472 case DRM_FORMAT_ABGR8888:
2473 dspcntr |= DISPPLANE_RGBX888;
2474 break;
2475 case DRM_FORMAT_XRGB2101010:
2476 case DRM_FORMAT_ARGB2101010:
2477 dspcntr |= DISPPLANE_BGRX101010;
2478 break;
2479 case DRM_FORMAT_XBGR2101010:
2480 case DRM_FORMAT_ABGR2101010:
2481 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002482 break;
2483 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002484 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002485 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002486
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002487 if (INTEL_INFO(dev)->gen >= 4 &&
2488 obj->tiling_mode != I915_TILING_NONE)
2489 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002490
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002491 if (IS_G4X(dev))
2492 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2493
Ville Syrjäläb98971272014-08-27 16:51:22 +03002494 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002495
Daniel Vetterc2c75132012-07-05 12:17:30 +02002496 if (INTEL_INFO(dev)->gen >= 4) {
2497 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002498 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002499 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002500 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002501 linear_offset -= intel_crtc->dspaddr_offset;
2502 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002503 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002504 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002505
Sonika Jindal48404c12014-08-22 14:06:04 +05302506 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2507 dspcntr |= DISPPLANE_ROTATE_180;
2508
2509 x += (intel_crtc->config.pipe_src_w - 1);
2510 y += (intel_crtc->config.pipe_src_h - 1);
2511
2512 /* Finding the last pixel of the last line of the display
2513 data and adding to linear_offset*/
2514 linear_offset +=
2515 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2516 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2517 }
2518
2519 I915_WRITE(reg, dspcntr);
2520
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002521 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2522 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2523 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002524 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002525 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002526 I915_WRITE(DSPSURF(plane),
2527 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002528 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002529 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002530 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002531 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002532 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002533}
2534
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002535static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2536 struct drm_framebuffer *fb,
2537 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002538{
2539 struct drm_device *dev = crtc->dev;
2540 struct drm_i915_private *dev_priv = dev->dev_private;
2541 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002542 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002543 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002544 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002545 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002546 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302547 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002548
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002549 if (!intel_crtc->primary_enabled) {
2550 I915_WRITE(reg, 0);
2551 I915_WRITE(DSPSURF(plane), 0);
2552 POSTING_READ(reg);
2553 return;
2554 }
2555
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002556 obj = intel_fb_obj(fb);
2557 if (WARN_ON(obj == NULL))
2558 return;
2559
2560 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2561
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002562 dspcntr = DISPPLANE_GAMMA_ENABLE;
2563
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002564 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002565
2566 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2567 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2568
Ville Syrjälä57779d02012-10-31 17:50:14 +02002569 switch (fb->pixel_format) {
2570 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002571 dspcntr |= DISPPLANE_8BPP;
2572 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002573 case DRM_FORMAT_RGB565:
2574 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002575 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002576 case DRM_FORMAT_XRGB8888:
2577 case DRM_FORMAT_ARGB8888:
2578 dspcntr |= DISPPLANE_BGRX888;
2579 break;
2580 case DRM_FORMAT_XBGR8888:
2581 case DRM_FORMAT_ABGR8888:
2582 dspcntr |= DISPPLANE_RGBX888;
2583 break;
2584 case DRM_FORMAT_XRGB2101010:
2585 case DRM_FORMAT_ARGB2101010:
2586 dspcntr |= DISPPLANE_BGRX101010;
2587 break;
2588 case DRM_FORMAT_XBGR2101010:
2589 case DRM_FORMAT_ABGR2101010:
2590 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002591 break;
2592 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002593 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002594 }
2595
2596 if (obj->tiling_mode != I915_TILING_NONE)
2597 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002598
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002599 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002600 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002601
Ville Syrjäläb98971272014-08-27 16:51:22 +03002602 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002603 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002604 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002605 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002606 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002607 linear_offset -= intel_crtc->dspaddr_offset;
Sonika Jindal48404c12014-08-22 14:06:04 +05302608 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180)) {
2609 dspcntr |= DISPPLANE_ROTATE_180;
2610
2611 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
2612 x += (intel_crtc->config.pipe_src_w - 1);
2613 y += (intel_crtc->config.pipe_src_h - 1);
2614
2615 /* Finding the last pixel of the last line of the display
2616 data and adding to linear_offset*/
2617 linear_offset +=
2618 (intel_crtc->config.pipe_src_h - 1) * fb->pitches[0] +
2619 (intel_crtc->config.pipe_src_w - 1) * pixel_size;
2620 }
2621 }
2622
2623 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002624
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002625 DRM_DEBUG_KMS("Writing base %08lX %08lX %d %d %d\n",
2626 i915_gem_obj_ggtt_offset(obj), linear_offset, x, y,
2627 fb->pitches[0]);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002628 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002629 I915_WRITE(DSPSURF(plane),
2630 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002631 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002632 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2633 } else {
2634 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2635 I915_WRITE(DSPLINOFF(plane), linear_offset);
2636 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002637 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002638}
2639
Damien Lespiau70d21f02013-07-03 21:06:04 +01002640static void skylake_update_primary_plane(struct drm_crtc *crtc,
2641 struct drm_framebuffer *fb,
2642 int x, int y)
2643{
2644 struct drm_device *dev = crtc->dev;
2645 struct drm_i915_private *dev_priv = dev->dev_private;
2646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2647 struct intel_framebuffer *intel_fb;
2648 struct drm_i915_gem_object *obj;
2649 int pipe = intel_crtc->pipe;
2650 u32 plane_ctl, stride;
2651
2652 if (!intel_crtc->primary_enabled) {
2653 I915_WRITE(PLANE_CTL(pipe, 0), 0);
2654 I915_WRITE(PLANE_SURF(pipe, 0), 0);
2655 POSTING_READ(PLANE_CTL(pipe, 0));
2656 return;
2657 }
2658
2659 plane_ctl = PLANE_CTL_ENABLE |
2660 PLANE_CTL_PIPE_GAMMA_ENABLE |
2661 PLANE_CTL_PIPE_CSC_ENABLE;
2662
2663 switch (fb->pixel_format) {
2664 case DRM_FORMAT_RGB565:
2665 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
2666 break;
2667 case DRM_FORMAT_XRGB8888:
2668 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2669 break;
2670 case DRM_FORMAT_XBGR8888:
2671 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2672 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
2673 break;
2674 case DRM_FORMAT_XRGB2101010:
2675 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2676 break;
2677 case DRM_FORMAT_XBGR2101010:
2678 plane_ctl |= PLANE_CTL_ORDER_RGBX;
2679 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
2680 break;
2681 default:
2682 BUG();
2683 }
2684
2685 intel_fb = to_intel_framebuffer(fb);
2686 obj = intel_fb->obj;
2687
2688 /*
2689 * The stride is either expressed as a multiple of 64 bytes chunks for
2690 * linear buffers or in number of tiles for tiled buffers.
2691 */
2692 switch (obj->tiling_mode) {
2693 case I915_TILING_NONE:
2694 stride = fb->pitches[0] >> 6;
2695 break;
2696 case I915_TILING_X:
2697 plane_ctl |= PLANE_CTL_TILED_X;
2698 stride = fb->pitches[0] >> 9;
2699 break;
2700 default:
2701 BUG();
2702 }
2703
2704 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal1447dde2014-10-04 10:53:31 +01002705 if (to_intel_plane(crtc->primary)->rotation == BIT(DRM_ROTATE_180))
2706 plane_ctl |= PLANE_CTL_ROTATE_180;
Damien Lespiau70d21f02013-07-03 21:06:04 +01002707
2708 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
2709
2710 DRM_DEBUG_KMS("Writing base %08lX %d,%d,%d,%d pitch=%d\n",
2711 i915_gem_obj_ggtt_offset(obj),
2712 x, y, fb->width, fb->height,
2713 fb->pitches[0]);
2714
2715 I915_WRITE(PLANE_POS(pipe, 0), 0);
2716 I915_WRITE(PLANE_OFFSET(pipe, 0), (y << 16) | x);
2717 I915_WRITE(PLANE_SIZE(pipe, 0),
2718 (intel_crtc->config.pipe_src_h - 1) << 16 |
2719 (intel_crtc->config.pipe_src_w - 1));
2720 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
2721 I915_WRITE(PLANE_SURF(pipe, 0), i915_gem_obj_ggtt_offset(obj));
2722
2723 POSTING_READ(PLANE_SURF(pipe, 0));
2724}
2725
Jesse Barnes17638cd2011-06-24 12:19:23 -07002726/* Assume fb object is pinned & idle & fenced and just update base pointers */
2727static int
2728intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
2729 int x, int y, enum mode_set_atomic state)
2730{
2731 struct drm_device *dev = crtc->dev;
2732 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002733
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002734 if (dev_priv->display.disable_fbc)
2735 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07002736
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002737 dev_priv->display.update_primary_plane(crtc, fb, x, y);
2738
2739 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07002740}
2741
Ville Syrjälä96a02912013-02-18 19:08:49 +02002742void intel_display_handle_reset(struct drm_device *dev)
2743{
2744 struct drm_i915_private *dev_priv = dev->dev_private;
2745 struct drm_crtc *crtc;
2746
2747 /*
2748 * Flips in the rings have been nuked by the reset,
2749 * so complete all pending flips so that user space
2750 * will get its events and not get stuck.
2751 *
2752 * Also update the base address of all primary
2753 * planes to the the last fb to make sure we're
2754 * showing the correct fb after a reset.
2755 *
2756 * Need to make two loops over the crtcs so that we
2757 * don't try to grab a crtc mutex before the
2758 * pending_flip_queue really got woken up.
2759 */
2760
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002761 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002762 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2763 enum plane plane = intel_crtc->plane;
2764
2765 intel_prepare_page_flip(dev, plane);
2766 intel_finish_page_flip_plane(dev, plane);
2767 }
2768
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002769 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02002770 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2771
Rob Clark51fd3712013-11-19 12:10:12 -05002772 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002773 /*
2774 * FIXME: Once we have proper support for primary planes (and
2775 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10002776 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00002777 */
Matt Roperf4510a22014-04-01 15:22:40 -07002778 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07002779 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10002780 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07002781 crtc->x,
2782 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05002783 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02002784 }
2785}
2786
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002787static int
Chris Wilson14667a42012-04-03 17:58:35 +01002788intel_finish_fb(struct drm_framebuffer *old_fb)
2789{
Matt Roper2ff8fde2014-07-08 07:50:07 -07002790 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01002791 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
2792 bool was_interruptible = dev_priv->mm.interruptible;
2793 int ret;
2794
Chris Wilson14667a42012-04-03 17:58:35 +01002795 /* Big Hammer, we also need to ensure that any pending
2796 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
2797 * current scanout is retired before unpinning the old
2798 * framebuffer.
2799 *
2800 * This should only fail upon a hung GPU, in which case we
2801 * can safely continue.
2802 */
2803 dev_priv->mm.interruptible = false;
2804 ret = i915_gem_object_finish_gpu(obj);
2805 dev_priv->mm.interruptible = was_interruptible;
2806
2807 return ret;
2808}
2809
Chris Wilson7d5e3792014-03-04 13:15:08 +00002810static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
2811{
2812 struct drm_device *dev = crtc->dev;
2813 struct drm_i915_private *dev_priv = dev->dev_private;
2814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002815 bool pending;
2816
2817 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
2818 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
2819 return false;
2820
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002821 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002822 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02002823 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00002824
2825 return pending;
2826}
2827
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002828static void intel_update_pipe_size(struct intel_crtc *crtc)
2829{
2830 struct drm_device *dev = crtc->base.dev;
2831 struct drm_i915_private *dev_priv = dev->dev_private;
2832 const struct drm_display_mode *adjusted_mode;
2833
2834 if (!i915.fastboot)
2835 return;
2836
2837 /*
2838 * Update pipe size and adjust fitter if needed: the reason for this is
2839 * that in compute_mode_changes we check the native mode (not the pfit
2840 * mode) to see if we can flip rather than do a full mode set. In the
2841 * fastboot case, we'll flip, but if we don't update the pipesrc and
2842 * pfit state, we'll end up with a big fb scanned out into the wrong
2843 * sized surface.
2844 *
2845 * To fix this properly, we need to hoist the checks up into
2846 * compute_mode_changes (or above), check the actual pfit state and
2847 * whether the platform allows pfit disable with pipe active, and only
2848 * then update the pipesrc and pfit state, even on the flip path.
2849 */
2850
2851 adjusted_mode = &crtc->config.adjusted_mode;
2852
2853 I915_WRITE(PIPESRC(crtc->pipe),
2854 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
2855 (adjusted_mode->crtc_vdisplay - 1));
2856 if (!crtc->config.pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002857 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
2858 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002859 I915_WRITE(PF_CTL(crtc->pipe), 0);
2860 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
2861 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
2862 }
2863 crtc->config.pipe_src_w = adjusted_mode->crtc_hdisplay;
2864 crtc->config.pipe_src_h = adjusted_mode->crtc_vdisplay;
2865}
2866
Chris Wilson14667a42012-04-03 17:58:35 +01002867static int
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002868intel_pipe_set_base(struct drm_crtc *crtc, int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02002869 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08002870{
2871 struct drm_device *dev = crtc->dev;
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002872 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08002873 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02002874 enum pipe pipe = intel_crtc->pipe;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002875 struct drm_framebuffer *old_fb = crtc->primary->fb;
2876 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
2877 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002878 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08002879
Chris Wilson7d5e3792014-03-04 13:15:08 +00002880 if (intel_crtc_has_pending_flip(crtc)) {
2881 DRM_ERROR("pipe is still busy with an old pageflip\n");
2882 return -EBUSY;
2883 }
2884
Jesse Barnes79e53942008-11-07 14:24:08 -08002885 /* no fb bound */
Daniel Vetter94352cf2012-07-05 22:51:56 +02002886 if (!fb) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002887 DRM_ERROR("No FB bound\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002888 return 0;
2889 }
2890
Ben Widawsky7eb552a2013-03-13 14:05:41 -07002891 if (intel_crtc->plane > INTEL_INFO(dev)->num_pipes) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03002892 DRM_ERROR("no plane for crtc: plane %c, num_pipes %d\n",
2893 plane_name(intel_crtc->plane),
2894 INTEL_INFO(dev)->num_pipes);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002895 return -EINVAL;
Jesse Barnes79e53942008-11-07 14:24:08 -08002896 }
2897
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002898 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02002899 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
2900 if (ret == 0)
Matt Roper91565c852014-06-24 17:05:02 -07002901 i915_gem_track_fb(old_obj, obj,
Daniel Vettera071fa02014-06-18 23:28:09 +02002902 INTEL_FRONTBUFFER_PRIMARY(pipe));
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002903 mutex_unlock(&dev->struct_mutex);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002904 if (ret != 0) {
Jesse Barnesa5071c22011-07-19 15:38:56 -07002905 DRM_ERROR("pin & fence failed\n");
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002906 return ret;
2907 }
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002908
Gustavo Padovane30e8f72014-09-10 12:04:17 -03002909 intel_update_pipe_size(intel_crtc);
Jesse Barnes4d6a3e62013-06-26 01:38:18 +03002910
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002911 dev_priv->display.update_primary_plane(crtc, fb, x, y);
Kristian Høgsberg3c4fdcf2008-12-17 22:14:46 -05002912
Daniel Vetterf99d7062014-06-19 16:01:59 +02002913 if (intel_crtc->active)
2914 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
2915
Matt Roperf4510a22014-04-01 15:22:40 -07002916 crtc->primary->fb = fb;
Daniel Vetter6c4c86f2012-09-10 21:58:30 +02002917 crtc->x = x;
2918 crtc->y = y;
Daniel Vetter94352cf2012-07-05 22:51:56 +02002919
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002920 if (old_fb) {
Daniel Vetterd7697ee2013-06-02 17:23:01 +02002921 if (intel_crtc->active && old_fb != fb)
2922 intel_wait_for_vblank(dev, intel_crtc->pipe);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002923 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002924 intel_unpin_fb_obj(old_obj);
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002925 mutex_unlock(&dev->struct_mutex);
Chris Wilsonb7f1de22010-12-14 16:09:31 +00002926 }
Jesse Barnes652c3932009-08-17 13:31:43 -07002927
Ville Syrjälä8ac36ec2014-03-11 19:37:33 +02002928 mutex_lock(&dev->struct_mutex);
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01002929 intel_update_fbc(dev);
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002930 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -08002931
Chris Wilson5c3b82e2009-02-11 13:25:09 +00002932 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08002933}
2934
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002935static void intel_fdi_normal_train(struct drm_crtc *crtc)
2936{
2937 struct drm_device *dev = crtc->dev;
2938 struct drm_i915_private *dev_priv = dev->dev_private;
2939 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2940 int pipe = intel_crtc->pipe;
2941 u32 reg, temp;
2942
2943 /* enable normal train */
2944 reg = FDI_TX_CTL(pipe);
2945 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07002946 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07002947 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
2948 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07002949 } else {
2950 temp &= ~FDI_LINK_TRAIN_NONE;
2951 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07002952 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002953 I915_WRITE(reg, temp);
2954
2955 reg = FDI_RX_CTL(pipe);
2956 temp = I915_READ(reg);
2957 if (HAS_PCH_CPT(dev)) {
2958 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
2959 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
2960 } else {
2961 temp &= ~FDI_LINK_TRAIN_NONE;
2962 temp |= FDI_LINK_TRAIN_NONE;
2963 }
2964 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
2965
2966 /* wait one idle pattern time */
2967 POSTING_READ(reg);
2968 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07002969
2970 /* IVB wants error correction enabled */
2971 if (IS_IVYBRIDGE(dev))
2972 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
2973 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08002974}
2975
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002976static bool pipe_has_enabled_pch(struct intel_crtc *crtc)
Daniel Vetter1e833f42013-02-19 22:31:57 +01002977{
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01002978 return crtc->base.enabled && crtc->active &&
2979 crtc->config.has_pch_encoder;
Daniel Vetter1e833f42013-02-19 22:31:57 +01002980}
2981
Daniel Vetter01a415f2012-10-27 15:58:40 +02002982static void ivb_modeset_global_resources(struct drm_device *dev)
2983{
2984 struct drm_i915_private *dev_priv = dev->dev_private;
2985 struct intel_crtc *pipe_B_crtc =
2986 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
2987 struct intel_crtc *pipe_C_crtc =
2988 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_C]);
2989 uint32_t temp;
2990
Daniel Vetter1e833f42013-02-19 22:31:57 +01002991 /*
2992 * When everything is off disable fdi C so that we could enable fdi B
2993 * with all lanes. Note that we don't care about enabled pipes without
2994 * an enabled pch encoder.
2995 */
2996 if (!pipe_has_enabled_pch(pipe_B_crtc) &&
2997 !pipe_has_enabled_pch(pipe_C_crtc)) {
Daniel Vetter01a415f2012-10-27 15:58:40 +02002998 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
2999 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3000
3001 temp = I915_READ(SOUTH_CHICKEN1);
3002 temp &= ~FDI_BC_BIFURCATION_SELECT;
3003 DRM_DEBUG_KMS("disabling fdi C rx\n");
3004 I915_WRITE(SOUTH_CHICKEN1, temp);
3005 }
3006}
3007
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003008/* The FDI link training functions for ILK/Ibexpeak. */
3009static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3010{
3011 struct drm_device *dev = crtc->dev;
3012 struct drm_i915_private *dev_priv = dev->dev_private;
3013 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3014 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003015 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003016
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003017 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003018 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003019
Adam Jacksone1a44742010-06-25 15:32:14 -04003020 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3021 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003022 reg = FDI_RX_IMR(pipe);
3023 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003024 temp &= ~FDI_RX_SYMBOL_LOCK;
3025 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003026 I915_WRITE(reg, temp);
3027 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003028 udelay(150);
3029
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003030 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003031 reg = FDI_TX_CTL(pipe);
3032 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003033 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3034 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003035 temp &= ~FDI_LINK_TRAIN_NONE;
3036 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003037 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003038
Chris Wilson5eddb702010-09-11 13:48:45 +01003039 reg = FDI_RX_CTL(pipe);
3040 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003041 temp &= ~FDI_LINK_TRAIN_NONE;
3042 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003043 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3044
3045 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003046 udelay(150);
3047
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003048 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003049 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3050 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3051 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003052
Chris Wilson5eddb702010-09-11 13:48:45 +01003053 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003054 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003055 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003056 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3057
3058 if ((temp & FDI_RX_BIT_LOCK)) {
3059 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003060 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003061 break;
3062 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003063 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003064 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003065 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003066
3067 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003068 reg = FDI_TX_CTL(pipe);
3069 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003070 temp &= ~FDI_LINK_TRAIN_NONE;
3071 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003072 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003073
Chris Wilson5eddb702010-09-11 13:48:45 +01003074 reg = FDI_RX_CTL(pipe);
3075 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003076 temp &= ~FDI_LINK_TRAIN_NONE;
3077 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003078 I915_WRITE(reg, temp);
3079
3080 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003081 udelay(150);
3082
Chris Wilson5eddb702010-09-11 13:48:45 +01003083 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003084 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003085 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003086 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3087
3088 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003089 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003090 DRM_DEBUG_KMS("FDI train 2 done.\n");
3091 break;
3092 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003093 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003094 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003095 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003096
3097 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003098
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003099}
3100
Akshay Joshi0206e352011-08-16 15:34:10 -04003101static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003102 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3103 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3104 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3105 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3106};
3107
3108/* The FDI link training functions for SNB/Cougarpoint. */
3109static void gen6_fdi_link_train(struct drm_crtc *crtc)
3110{
3111 struct drm_device *dev = crtc->dev;
3112 struct drm_i915_private *dev_priv = dev->dev_private;
3113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3114 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003115 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003116
Adam Jacksone1a44742010-06-25 15:32:14 -04003117 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3118 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003119 reg = FDI_RX_IMR(pipe);
3120 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003121 temp &= ~FDI_RX_SYMBOL_LOCK;
3122 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003123 I915_WRITE(reg, temp);
3124
3125 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003126 udelay(150);
3127
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003128 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003129 reg = FDI_TX_CTL(pipe);
3130 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003131 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3132 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003133 temp &= ~FDI_LINK_TRAIN_NONE;
3134 temp |= FDI_LINK_TRAIN_PATTERN_1;
3135 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3136 /* SNB-B */
3137 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003138 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003139
Daniel Vetterd74cf322012-10-26 10:58:13 +02003140 I915_WRITE(FDI_RX_MISC(pipe),
3141 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3142
Chris Wilson5eddb702010-09-11 13:48:45 +01003143 reg = FDI_RX_CTL(pipe);
3144 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003145 if (HAS_PCH_CPT(dev)) {
3146 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3147 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3148 } else {
3149 temp &= ~FDI_LINK_TRAIN_NONE;
3150 temp |= FDI_LINK_TRAIN_PATTERN_1;
3151 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003152 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3153
3154 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003155 udelay(150);
3156
Akshay Joshi0206e352011-08-16 15:34:10 -04003157 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003158 reg = FDI_TX_CTL(pipe);
3159 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003160 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3161 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003162 I915_WRITE(reg, temp);
3163
3164 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003165 udelay(500);
3166
Sean Paulfa37d392012-03-02 12:53:39 -05003167 for (retry = 0; retry < 5; retry++) {
3168 reg = FDI_RX_IIR(pipe);
3169 temp = I915_READ(reg);
3170 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3171 if (temp & FDI_RX_BIT_LOCK) {
3172 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3173 DRM_DEBUG_KMS("FDI train 1 done.\n");
3174 break;
3175 }
3176 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003177 }
Sean Paulfa37d392012-03-02 12:53:39 -05003178 if (retry < 5)
3179 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003180 }
3181 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003182 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003183
3184 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003185 reg = FDI_TX_CTL(pipe);
3186 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003187 temp &= ~FDI_LINK_TRAIN_NONE;
3188 temp |= FDI_LINK_TRAIN_PATTERN_2;
3189 if (IS_GEN6(dev)) {
3190 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3191 /* SNB-B */
3192 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3193 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003194 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003195
Chris Wilson5eddb702010-09-11 13:48:45 +01003196 reg = FDI_RX_CTL(pipe);
3197 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003198 if (HAS_PCH_CPT(dev)) {
3199 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3200 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3201 } else {
3202 temp &= ~FDI_LINK_TRAIN_NONE;
3203 temp |= FDI_LINK_TRAIN_PATTERN_2;
3204 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003205 I915_WRITE(reg, temp);
3206
3207 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003208 udelay(150);
3209
Akshay Joshi0206e352011-08-16 15:34:10 -04003210 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003211 reg = FDI_TX_CTL(pipe);
3212 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003213 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3214 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003215 I915_WRITE(reg, temp);
3216
3217 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003218 udelay(500);
3219
Sean Paulfa37d392012-03-02 12:53:39 -05003220 for (retry = 0; retry < 5; retry++) {
3221 reg = FDI_RX_IIR(pipe);
3222 temp = I915_READ(reg);
3223 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3224 if (temp & FDI_RX_SYMBOL_LOCK) {
3225 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3226 DRM_DEBUG_KMS("FDI train 2 done.\n");
3227 break;
3228 }
3229 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003230 }
Sean Paulfa37d392012-03-02 12:53:39 -05003231 if (retry < 5)
3232 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003233 }
3234 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003235 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003236
3237 DRM_DEBUG_KMS("FDI train done.\n");
3238}
3239
Jesse Barnes357555c2011-04-28 15:09:55 -07003240/* Manual link training for Ivy Bridge A0 parts */
3241static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3242{
3243 struct drm_device *dev = crtc->dev;
3244 struct drm_i915_private *dev_priv = dev->dev_private;
3245 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3246 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003247 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003248
3249 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3250 for train result */
3251 reg = FDI_RX_IMR(pipe);
3252 temp = I915_READ(reg);
3253 temp &= ~FDI_RX_SYMBOL_LOCK;
3254 temp &= ~FDI_RX_BIT_LOCK;
3255 I915_WRITE(reg, temp);
3256
3257 POSTING_READ(reg);
3258 udelay(150);
3259
Daniel Vetter01a415f2012-10-27 15:58:40 +02003260 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3261 I915_READ(FDI_RX_IIR(pipe)));
3262
Jesse Barnes139ccd32013-08-19 11:04:55 -07003263 /* Try each vswing and preemphasis setting twice before moving on */
3264 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3265 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003266 reg = FDI_TX_CTL(pipe);
3267 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003268 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3269 temp &= ~FDI_TX_ENABLE;
3270 I915_WRITE(reg, temp);
3271
3272 reg = FDI_RX_CTL(pipe);
3273 temp = I915_READ(reg);
3274 temp &= ~FDI_LINK_TRAIN_AUTO;
3275 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3276 temp &= ~FDI_RX_ENABLE;
3277 I915_WRITE(reg, temp);
3278
3279 /* enable CPU FDI TX and PCH FDI RX */
3280 reg = FDI_TX_CTL(pipe);
3281 temp = I915_READ(reg);
3282 temp &= ~FDI_DP_PORT_WIDTH_MASK;
3283 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
3284 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003285 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003286 temp |= snb_b_fdi_train_param[j/2];
3287 temp |= FDI_COMPOSITE_SYNC;
3288 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3289
3290 I915_WRITE(FDI_RX_MISC(pipe),
3291 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3292
3293 reg = FDI_RX_CTL(pipe);
3294 temp = I915_READ(reg);
3295 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3296 temp |= FDI_COMPOSITE_SYNC;
3297 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3298
3299 POSTING_READ(reg);
3300 udelay(1); /* should be 0.5us */
3301
3302 for (i = 0; i < 4; i++) {
3303 reg = FDI_RX_IIR(pipe);
3304 temp = I915_READ(reg);
3305 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3306
3307 if (temp & FDI_RX_BIT_LOCK ||
3308 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3309 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3310 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3311 i);
3312 break;
3313 }
3314 udelay(1); /* should be 0.5us */
3315 }
3316 if (i == 4) {
3317 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3318 continue;
3319 }
3320
3321 /* Train 2 */
3322 reg = FDI_TX_CTL(pipe);
3323 temp = I915_READ(reg);
3324 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3325 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3326 I915_WRITE(reg, temp);
3327
3328 reg = FDI_RX_CTL(pipe);
3329 temp = I915_READ(reg);
3330 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3331 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003332 I915_WRITE(reg, temp);
3333
3334 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003335 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003336
Jesse Barnes139ccd32013-08-19 11:04:55 -07003337 for (i = 0; i < 4; i++) {
3338 reg = FDI_RX_IIR(pipe);
3339 temp = I915_READ(reg);
3340 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003341
Jesse Barnes139ccd32013-08-19 11:04:55 -07003342 if (temp & FDI_RX_SYMBOL_LOCK ||
3343 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3344 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3345 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3346 i);
3347 goto train_done;
3348 }
3349 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003350 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003351 if (i == 4)
3352 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003353 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003354
Jesse Barnes139ccd32013-08-19 11:04:55 -07003355train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003356 DRM_DEBUG_KMS("FDI train done.\n");
3357}
3358
Daniel Vetter88cefb62012-08-12 19:27:14 +02003359static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003360{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003361 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003362 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003363 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003364 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003365
Jesse Barnesc64e3112010-09-10 11:27:03 -07003366
Jesse Barnes0e23b992010-09-10 11:10:00 -07003367 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003368 reg = FDI_RX_CTL(pipe);
3369 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003370 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
3371 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config.fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003372 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3374
3375 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003376 udelay(200);
3377
3378 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003379 temp = I915_READ(reg);
3380 I915_WRITE(reg, temp | FDI_PCDCLK);
3381
3382 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003383 udelay(200);
3384
Paulo Zanoni20749732012-11-23 15:30:38 -02003385 /* Enable CPU FDI TX PLL, always on for Ironlake */
3386 reg = FDI_TX_CTL(pipe);
3387 temp = I915_READ(reg);
3388 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3389 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003390
Paulo Zanoni20749732012-11-23 15:30:38 -02003391 POSTING_READ(reg);
3392 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003393 }
3394}
3395
Daniel Vetter88cefb62012-08-12 19:27:14 +02003396static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3397{
3398 struct drm_device *dev = intel_crtc->base.dev;
3399 struct drm_i915_private *dev_priv = dev->dev_private;
3400 int pipe = intel_crtc->pipe;
3401 u32 reg, temp;
3402
3403 /* Switch from PCDclk to Rawclk */
3404 reg = FDI_RX_CTL(pipe);
3405 temp = I915_READ(reg);
3406 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3407
3408 /* Disable CPU FDI TX PLL */
3409 reg = FDI_TX_CTL(pipe);
3410 temp = I915_READ(reg);
3411 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3412
3413 POSTING_READ(reg);
3414 udelay(100);
3415
3416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
3418 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3419
3420 /* Wait for the clocks to turn off. */
3421 POSTING_READ(reg);
3422 udelay(100);
3423}
3424
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003425static void ironlake_fdi_disable(struct drm_crtc *crtc)
3426{
3427 struct drm_device *dev = crtc->dev;
3428 struct drm_i915_private *dev_priv = dev->dev_private;
3429 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3430 int pipe = intel_crtc->pipe;
3431 u32 reg, temp;
3432
3433 /* disable CPU FDI tx and PCH FDI rx */
3434 reg = FDI_TX_CTL(pipe);
3435 temp = I915_READ(reg);
3436 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3437 POSTING_READ(reg);
3438
3439 reg = FDI_RX_CTL(pipe);
3440 temp = I915_READ(reg);
3441 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003442 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003443 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3444
3445 POSTING_READ(reg);
3446 udelay(100);
3447
3448 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003449 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003450 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003451
3452 /* still set train pattern 1 */
3453 reg = FDI_TX_CTL(pipe);
3454 temp = I915_READ(reg);
3455 temp &= ~FDI_LINK_TRAIN_NONE;
3456 temp |= FDI_LINK_TRAIN_PATTERN_1;
3457 I915_WRITE(reg, temp);
3458
3459 reg = FDI_RX_CTL(pipe);
3460 temp = I915_READ(reg);
3461 if (HAS_PCH_CPT(dev)) {
3462 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3463 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3464 } else {
3465 temp &= ~FDI_LINK_TRAIN_NONE;
3466 temp |= FDI_LINK_TRAIN_PATTERN_1;
3467 }
3468 /* BPC in FDI rx is consistent with that in PIPECONF */
3469 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003470 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003471 I915_WRITE(reg, temp);
3472
3473 POSTING_READ(reg);
3474 udelay(100);
3475}
3476
Chris Wilson5dce5b932014-01-20 10:17:36 +00003477bool intel_has_pending_fb_unpin(struct drm_device *dev)
3478{
3479 struct intel_crtc *crtc;
3480
3481 /* Note that we don't need to be called with mode_config.lock here
3482 * as our list of CRTC objects is static for the lifetime of the
3483 * device and so cannot disappear as we iterate. Similarly, we can
3484 * happily treat the predicates as racy, atomic checks as userspace
3485 * cannot claim and pin a new fb without at least acquring the
3486 * struct_mutex and so serialising with us.
3487 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003488 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003489 if (atomic_read(&crtc->unpin_work_count) == 0)
3490 continue;
3491
3492 if (crtc->unpin_work)
3493 intel_wait_for_vblank(dev, crtc->pipe);
3494
3495 return true;
3496 }
3497
3498 return false;
3499}
3500
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003501static void page_flip_completed(struct intel_crtc *intel_crtc)
3502{
3503 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3504 struct intel_unpin_work *work = intel_crtc->unpin_work;
3505
3506 /* ensure that the unpin work is consistent wrt ->pending. */
3507 smp_rmb();
3508 intel_crtc->unpin_work = NULL;
3509
3510 if (work->event)
3511 drm_send_vblank_event(intel_crtc->base.dev,
3512 intel_crtc->pipe,
3513 work->event);
3514
3515 drm_crtc_vblank_put(&intel_crtc->base);
3516
3517 wake_up_all(&dev_priv->pending_flip_queue);
3518 queue_work(dev_priv->wq, &work->work);
3519
3520 trace_i915_flip_complete(intel_crtc->plane,
3521 work->pending_flip_obj);
3522}
3523
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003524void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003525{
Chris Wilson0f911282012-04-17 10:05:38 +01003526 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003527 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003528
Daniel Vetter2c10d572012-12-20 21:24:07 +01003529 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003530 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3531 !intel_crtc_has_pending_flip(crtc),
3532 60*HZ) == 0)) {
3533 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003534
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003535 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003536 if (intel_crtc->unpin_work) {
3537 WARN_ONCE(1, "Removing stuck page flip\n");
3538 page_flip_completed(intel_crtc);
3539 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003540 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003541 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003542
Chris Wilson975d5682014-08-20 13:13:34 +01003543 if (crtc->primary->fb) {
3544 mutex_lock(&dev->struct_mutex);
3545 intel_finish_fb(crtc->primary->fb);
3546 mutex_unlock(&dev->struct_mutex);
3547 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003548}
3549
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003550/* Program iCLKIP clock to the desired frequency */
3551static void lpt_program_iclkip(struct drm_crtc *crtc)
3552{
3553 struct drm_device *dev = crtc->dev;
3554 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau241bfc32013-09-25 16:45:37 +01003555 int clock = to_intel_crtc(crtc)->config.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003556 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3557 u32 temp;
3558
Daniel Vetter09153002012-12-12 14:06:44 +01003559 mutex_lock(&dev_priv->dpio_lock);
3560
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003561 /* It is necessary to ungate the pixclk gate prior to programming
3562 * the divisors, and gate it back when it is done.
3563 */
3564 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3565
3566 /* Disable SSCCTL */
3567 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003568 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3569 SBI_SSCCTL_DISABLE,
3570 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003571
3572 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003573 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003574 auxdiv = 1;
3575 divsel = 0x41;
3576 phaseinc = 0x20;
3577 } else {
3578 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003579 * but the adjusted_mode->crtc_clock in in KHz. To get the
3580 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003581 * convert the virtual clock precision to KHz here for higher
3582 * precision.
3583 */
3584 u32 iclk_virtual_root_freq = 172800 * 1000;
3585 u32 iclk_pi_range = 64;
3586 u32 desired_divisor, msb_divisor_value, pi_value;
3587
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003588 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003589 msb_divisor_value = desired_divisor / iclk_pi_range;
3590 pi_value = desired_divisor % iclk_pi_range;
3591
3592 auxdiv = 0;
3593 divsel = msb_divisor_value - 2;
3594 phaseinc = pi_value;
3595 }
3596
3597 /* This should not happen with any sane values */
3598 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3599 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3600 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3601 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3602
3603 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 +03003604 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003605 auxdiv,
3606 divsel,
3607 phasedir,
3608 phaseinc);
3609
3610 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003611 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003612 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3613 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3614 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3615 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3616 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3617 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003618 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003619
3620 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003621 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003622 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3623 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003624 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003625
3626 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003627 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003628 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003629 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003630
3631 /* Wait for initialization time */
3632 udelay(24);
3633
3634 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003635
3636 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003637}
3638
Daniel Vetter275f01b22013-05-03 11:49:47 +02003639static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3640 enum pipe pch_transcoder)
3641{
3642 struct drm_device *dev = crtc->base.dev;
3643 struct drm_i915_private *dev_priv = dev->dev_private;
3644 enum transcoder cpu_transcoder = crtc->config.cpu_transcoder;
3645
3646 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3647 I915_READ(HTOTAL(cpu_transcoder)));
3648 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
3649 I915_READ(HBLANK(cpu_transcoder)));
3650 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
3651 I915_READ(HSYNC(cpu_transcoder)));
3652
3653 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
3654 I915_READ(VTOTAL(cpu_transcoder)));
3655 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
3656 I915_READ(VBLANK(cpu_transcoder)));
3657 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
3658 I915_READ(VSYNC(cpu_transcoder)));
3659 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
3660 I915_READ(VSYNCSHIFT(cpu_transcoder)));
3661}
3662
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003663static void cpt_enable_fdi_bc_bifurcation(struct drm_device *dev)
3664{
3665 struct drm_i915_private *dev_priv = dev->dev_private;
3666 uint32_t temp;
3667
3668 temp = I915_READ(SOUTH_CHICKEN1);
3669 if (temp & FDI_BC_BIFURCATION_SELECT)
3670 return;
3671
3672 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
3673 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
3674
3675 temp |= FDI_BC_BIFURCATION_SELECT;
3676 DRM_DEBUG_KMS("enabling fdi C rx\n");
3677 I915_WRITE(SOUTH_CHICKEN1, temp);
3678 POSTING_READ(SOUTH_CHICKEN1);
3679}
3680
3681static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
3682{
3683 struct drm_device *dev = intel_crtc->base.dev;
3684 struct drm_i915_private *dev_priv = dev->dev_private;
3685
3686 switch (intel_crtc->pipe) {
3687 case PIPE_A:
3688 break;
3689 case PIPE_B:
3690 if (intel_crtc->config.fdi_lanes > 2)
3691 WARN_ON(I915_READ(SOUTH_CHICKEN1) & FDI_BC_BIFURCATION_SELECT);
3692 else
3693 cpt_enable_fdi_bc_bifurcation(dev);
3694
3695 break;
3696 case PIPE_C:
3697 cpt_enable_fdi_bc_bifurcation(dev);
3698
3699 break;
3700 default:
3701 BUG();
3702 }
3703}
3704
Jesse Barnesf67a5592011-01-05 10:31:48 -08003705/*
3706 * Enable PCH resources required for PCH ports:
3707 * - PCH PLLs
3708 * - FDI training & RX/TX
3709 * - update transcoder timings
3710 * - DP transcoding bits
3711 * - transcoder
3712 */
3713static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08003714{
3715 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08003716 struct drm_i915_private *dev_priv = dev->dev_private;
3717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3718 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003719 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07003720
Daniel Vetterab9412b2013-05-03 11:49:46 +02003721 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01003722
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01003723 if (IS_IVYBRIDGE(dev))
3724 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
3725
Daniel Vettercd986ab2012-10-26 10:58:12 +02003726 /* Write the TU size bits before fdi link training, so that error
3727 * detection works. */
3728 I915_WRITE(FDI_RX_TUSIZE1(pipe),
3729 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
3730
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003731 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07003732 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07003733
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003734 /* We need to program the right clock selection before writing the pixel
3735 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003736 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003737 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07003738
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003739 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02003740 temp |= TRANS_DPLL_ENABLE(pipe);
3741 sel = TRANS_DPLLB_SEL(pipe);
Daniel Vettera43f6e02013-06-07 23:10:32 +02003742 if (intel_crtc->config.shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003743 temp |= sel;
3744 else
3745 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003746 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003747 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003748
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003749 /* XXX: pch pll's can be enabled any time before we enable the PCH
3750 * transcoder, and we actually should do this to not upset any PCH
3751 * transcoder that already use the clock when we share it.
3752 *
3753 * Note that enable_shared_dpll tries to do the right thing, but
3754 * get_shared_dpll unconditionally resets the pll - we need that to have
3755 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02003756 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02003757
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08003758 /* set transcoder timing, panel must allow it */
3759 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02003760 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003761
Paulo Zanoni303b81e2012-10-31 18:12:23 -02003762 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003763
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003764 /* For PCH DP, enable TRANS_DP_CTL */
3765 if (HAS_PCH_CPT(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003766 (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DISPLAYPORT) ||
3767 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_EDP))) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003768 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01003769 reg = TRANS_DP_CTL(pipe);
3770 temp = I915_READ(reg);
3771 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08003772 TRANS_DP_SYNC_MASK |
3773 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01003774 temp |= (TRANS_DP_OUTPUT_ENABLE |
3775 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07003776 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003777
3778 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003779 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003780 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01003781 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003782
3783 switch (intel_trans_dp_port_sel(crtc)) {
3784 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01003785 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003786 break;
3787 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01003788 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003789 break;
3790 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01003791 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003792 break;
3793 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02003794 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003795 }
3796
Chris Wilson5eddb702010-09-11 13:48:45 +01003797 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07003798 }
3799
Paulo Zanonib8a4f402012-10-31 18:12:42 -02003800 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003801}
3802
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003803static void lpt_pch_enable(struct drm_crtc *crtc)
3804{
3805 struct drm_device *dev = crtc->dev;
3806 struct drm_i915_private *dev_priv = dev->dev_private;
3807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02003808 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003809
Daniel Vetterab9412b2013-05-03 11:49:46 +02003810 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003811
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02003812 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003813
Paulo Zanoni0540e482012-10-31 18:12:40 -02003814 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02003815 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02003816
Paulo Zanoni937bb612012-10-31 18:12:47 -02003817 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08003818}
3819
Daniel Vetter716c2e52014-06-25 22:02:02 +03003820void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003821{
Daniel Vettere2b78262013-06-07 23:10:03 +02003822 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003823
3824 if (pll == NULL)
3825 return;
3826
3827 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003828 WARN(1, "bad %s refcount\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003829 return;
3830 }
3831
Daniel Vetterf4a091c2013-06-10 17:28:22 +02003832 if (--pll->refcount == 0) {
3833 WARN_ON(pll->on);
3834 WARN_ON(pll->active);
3835 }
3836
Daniel Vettera43f6e02013-06-07 23:10:32 +02003837 crtc->config.shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003838}
3839
Daniel Vetter716c2e52014-06-25 22:02:02 +03003840struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003841{
Daniel Vettere2b78262013-06-07 23:10:03 +02003842 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
3843 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
3844 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003845
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003846 if (pll) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003847 DRM_DEBUG_KMS("CRTC:%d dropping existing %s\n",
3848 crtc->base.base.id, pll->name);
Daniel Vettere2b78262013-06-07 23:10:03 +02003849 intel_put_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003850 }
3851
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003852 if (HAS_PCH_IBX(dev_priv->dev)) {
3853 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02003854 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003855 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003856
Daniel Vetter46edb022013-06-05 13:34:12 +02003857 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
3858 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003859
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003860 WARN_ON(pll->refcount);
3861
Daniel Vetter98b6bd92012-05-20 20:00:25 +02003862 goto found;
3863 }
3864
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003865 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3866 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003867
3868 /* Only want to check enabled timings first */
3869 if (pll->refcount == 0)
3870 continue;
3871
Daniel Vetterb89a1d32013-06-05 13:34:24 +02003872 if (memcmp(&crtc->config.dpll_hw_state, &pll->hw_state,
3873 sizeof(pll->hw_state)) == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003874 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (refcount %d, ative %d)\n",
Daniel Vettere2b78262013-06-07 23:10:03 +02003875 crtc->base.base.id,
Daniel Vetter46edb022013-06-05 13:34:12 +02003876 pll->name, pll->refcount, pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003877
3878 goto found;
3879 }
3880 }
3881
3882 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02003883 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
3884 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003885 if (pll->refcount == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02003886 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
3887 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003888 goto found;
3889 }
3890 }
3891
3892 return NULL;
3893
3894found:
Daniel Vetterf2a69f42014-05-20 15:19:19 +02003895 if (pll->refcount == 0)
3896 pll->hw_state = crtc->config.dpll_hw_state;
3897
Daniel Vettera43f6e02013-06-07 23:10:32 +02003898 crtc->config.shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02003899 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
3900 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02003901
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003902 pll->refcount++;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003903
Jesse Barnesee7b9f92012-04-20 17:11:53 +01003904 return pll;
3905}
3906
Daniel Vettera1520312013-05-03 11:49:50 +02003907static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07003908{
3909 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01003910 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07003911 u32 temp;
3912
3913 temp = I915_READ(dslreg);
3914 udelay(500);
3915 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07003916 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03003917 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07003918 }
3919}
3920
Jesse Barnesb074cec2013-04-25 12:55:02 -07003921static void ironlake_pfit_enable(struct intel_crtc *crtc)
3922{
3923 struct drm_device *dev = crtc->base.dev;
3924 struct drm_i915_private *dev_priv = dev->dev_private;
3925 int pipe = crtc->pipe;
3926
Chris Wilsonfd4daa92013-08-27 17:04:17 +01003927 if (crtc->config.pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07003928 /* Force use of hard-coded filter coefficients
3929 * as some pre-programmed values are broken,
3930 * e.g. x201.
3931 */
3932 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
3933 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
3934 PF_PIPE_SEL_IVB(pipe));
3935 else
3936 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
3937 I915_WRITE(PF_WIN_POS(pipe), crtc->config.pch_pfit.pos);
3938 I915_WRITE(PF_WIN_SZ(pipe), crtc->config.pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08003939 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08003940}
3941
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003942static void intel_enable_planes(struct drm_crtc *crtc)
3943{
3944 struct drm_device *dev = crtc->dev;
3945 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003946 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003947 struct intel_plane *intel_plane;
3948
Matt Roperaf2b6532014-04-01 15:22:32 -07003949 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3950 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003951 if (intel_plane->pipe == pipe)
3952 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003953 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003954}
3955
3956static void intel_disable_planes(struct drm_crtc *crtc)
3957{
3958 struct drm_device *dev = crtc->dev;
3959 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07003960 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003961 struct intel_plane *intel_plane;
3962
Matt Roperaf2b6532014-04-01 15:22:32 -07003963 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
3964 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003965 if (intel_plane->pipe == pipe)
3966 intel_plane_disable(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07003967 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03003968}
3969
Ville Syrjälä20bc86732013-10-01 18:02:17 +03003970void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03003971{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003972 struct drm_device *dev = crtc->base.dev;
3973 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03003974
3975 if (!crtc->config.ips_enabled)
3976 return;
3977
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003978 /* We can only enable IPS after we enable a plane and wait for a vblank */
3979 intel_wait_for_vblank(dev, crtc->pipe);
3980
Paulo Zanonid77e4532013-09-24 13:52:55 -03003981 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03003982 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003983 mutex_lock(&dev_priv->rps.hw_lock);
3984 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
3985 mutex_unlock(&dev_priv->rps.hw_lock);
3986 /* Quoting Art Runyan: "its not safe to expect any particular
3987 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08003988 * mailbox." Moreover, the mailbox may return a bogus state,
3989 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07003990 */
3991 } else {
3992 I915_WRITE(IPS_CTL, IPS_ENABLE);
3993 /* The bit only becomes 1 in the next vblank, so this wait here
3994 * is essentially intel_wait_for_vblank. If we don't have this
3995 * and don't wait for vblanks until the end of crtc_enable, then
3996 * the HW state readout code will complain that the expected
3997 * IPS_CTL value is not the one we read. */
3998 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
3999 DRM_ERROR("Timed out waiting for IPS enable\n");
4000 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004001}
4002
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004003void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004004{
4005 struct drm_device *dev = crtc->base.dev;
4006 struct drm_i915_private *dev_priv = dev->dev_private;
4007
4008 if (!crtc->config.ips_enabled)
4009 return;
4010
4011 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004012 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004013 mutex_lock(&dev_priv->rps.hw_lock);
4014 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4015 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004016 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4017 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4018 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004019 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004020 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004021 POSTING_READ(IPS_CTL);
4022 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004023
4024 /* We need to wait for a vblank before we can disable the plane. */
4025 intel_wait_for_vblank(dev, crtc->pipe);
4026}
4027
4028/** Loads the palette/gamma unit for the CRTC with the prepared values */
4029static void intel_crtc_load_lut(struct drm_crtc *crtc)
4030{
4031 struct drm_device *dev = crtc->dev;
4032 struct drm_i915_private *dev_priv = dev->dev_private;
4033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4034 enum pipe pipe = intel_crtc->pipe;
4035 int palreg = PALETTE(pipe);
4036 int i;
4037 bool reenable_ips = false;
4038
4039 /* The clocks have to be on to load the palette. */
4040 if (!crtc->enabled || !intel_crtc->active)
4041 return;
4042
4043 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004044 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004045 assert_dsi_pll_enabled(dev_priv);
4046 else
4047 assert_pll_enabled(dev_priv, pipe);
4048 }
4049
4050 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304051 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004052 palreg = LGC_PALETTE(pipe);
4053
4054 /* Workaround : Do not read or write the pipe palette/gamma data while
4055 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4056 */
Paulo Zanoni41e6fc42014-01-08 17:26:31 -02004057 if (IS_HASWELL(dev) && intel_crtc->config.ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004058 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4059 GAMMA_MODE_MODE_SPLIT)) {
4060 hsw_disable_ips(intel_crtc);
4061 reenable_ips = true;
4062 }
4063
4064 for (i = 0; i < 256; i++) {
4065 I915_WRITE(palreg + 4 * i,
4066 (intel_crtc->lut_r[i] << 16) |
4067 (intel_crtc->lut_g[i] << 8) |
4068 intel_crtc->lut_b[i]);
4069 }
4070
4071 if (reenable_ips)
4072 hsw_enable_ips(intel_crtc);
4073}
4074
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004075static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4076{
4077 if (!enable && intel_crtc->overlay) {
4078 struct drm_device *dev = intel_crtc->base.dev;
4079 struct drm_i915_private *dev_priv = dev->dev_private;
4080
4081 mutex_lock(&dev->struct_mutex);
4082 dev_priv->mm.interruptible = false;
4083 (void) intel_overlay_switch_off(intel_crtc->overlay);
4084 dev_priv->mm.interruptible = true;
4085 mutex_unlock(&dev->struct_mutex);
4086 }
4087
4088 /* Let userspace switch the overlay on again. In most cases userspace
4089 * has to recompute where to put it anyway.
4090 */
4091}
4092
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004093static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004094{
4095 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4097 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004098
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004099 intel_enable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004100 intel_enable_planes(crtc);
4101 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004102 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004103
4104 hsw_enable_ips(intel_crtc);
4105
4106 mutex_lock(&dev->struct_mutex);
4107 intel_update_fbc(dev);
4108 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004109
4110 /*
4111 * FIXME: Once we grow proper nuclear flip support out of this we need
4112 * to compute the mask of flip planes precisely. For the time being
4113 * consider this a flip from a NULL plane.
4114 */
4115 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004116}
4117
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004118static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004119{
4120 struct drm_device *dev = crtc->dev;
4121 struct drm_i915_private *dev_priv = dev->dev_private;
4122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4123 int pipe = intel_crtc->pipe;
4124 int plane = intel_crtc->plane;
4125
4126 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004127
4128 if (dev_priv->fbc.plane == plane)
4129 intel_disable_fbc(dev);
4130
4131 hsw_disable_ips(intel_crtc);
4132
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004133 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004134 intel_crtc_update_cursor(crtc, false);
4135 intel_disable_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004136 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004137
Daniel Vetterf99d7062014-06-19 16:01:59 +02004138 /*
4139 * FIXME: Once we grow proper nuclear flip support out of this we need
4140 * to compute the mask of flip planes precisely. For the time being
4141 * consider this a flip to a NULL plane.
4142 */
4143 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004144}
4145
Jesse Barnesf67a5592011-01-05 10:31:48 -08004146static void ironlake_crtc_enable(struct drm_crtc *crtc)
4147{
4148 struct drm_device *dev = crtc->dev;
4149 struct drm_i915_private *dev_priv = dev->dev_private;
4150 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004151 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004152 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004153
Daniel Vetter08a48462012-07-02 11:43:47 +02004154 WARN_ON(!crtc->enabled);
4155
Jesse Barnesf67a5592011-01-05 10:31:48 -08004156 if (intel_crtc->active)
4157 return;
4158
Daniel Vetterb14b1052014-04-24 23:55:13 +02004159 if (intel_crtc->config.has_pch_encoder)
4160 intel_prepare_shared_dpll(intel_crtc);
4161
Daniel Vetter29407aa2014-04-24 23:55:08 +02004162 if (intel_crtc->config.has_dp_encoder)
4163 intel_dp_set_m_n(intel_crtc);
4164
4165 intel_set_pipe_timings(intel_crtc);
4166
4167 if (intel_crtc->config.has_pch_encoder) {
4168 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004169 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004170 }
4171
4172 ironlake_set_pipeconf(crtc);
4173
Jesse Barnesf67a5592011-01-05 10:31:48 -08004174 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004175
Daniel Vettera72e4c92014-09-30 10:56:47 +02004176 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4177 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004178
Daniel Vetterf6736a12013-06-05 13:34:30 +02004179 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004180 if (encoder->pre_enable)
4181 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004182
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004183 if (intel_crtc->config.has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004184 /* Note: FDI PLL enabling _must_ be done before we enable the
4185 * cpu pipes, hence this is separate from all the other fdi/pch
4186 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004187 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004188 } else {
4189 assert_fdi_tx_disabled(dev_priv, pipe);
4190 assert_fdi_rx_disabled(dev_priv, pipe);
4191 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004192
Jesse Barnesb074cec2013-04-25 12:55:02 -07004193 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004194
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004195 /*
4196 * On ILK+ LUT must be loaded before the pipe is running but with
4197 * clocks enabled
4198 */
4199 intel_crtc_load_lut(crtc);
4200
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004201 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004202 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004203
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004204 if (intel_crtc->config.has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004205 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004206
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004207 for_each_encoder_on_crtc(dev, crtc, encoder)
4208 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004209
4210 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004211 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004212
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004213 assert_vblank_disabled(crtc);
4214 drm_crtc_vblank_on(crtc);
4215
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004216 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004217}
4218
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004219/* IPS only exists on ULT machines and is tied to pipe A. */
4220static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4221{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004222 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004223}
4224
Paulo Zanonie4916942013-09-20 16:21:19 -03004225/*
4226 * This implements the workaround described in the "notes" section of the mode
4227 * set sequence documentation. When going from no pipes or single pipe to
4228 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4229 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4230 */
4231static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4232{
4233 struct drm_device *dev = crtc->base.dev;
4234 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4235
4236 /* We want to get the other_active_crtc only if there's only 1 other
4237 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004238 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004239 if (!crtc_it->active || crtc_it == crtc)
4240 continue;
4241
4242 if (other_active_crtc)
4243 return;
4244
4245 other_active_crtc = crtc_it;
4246 }
4247 if (!other_active_crtc)
4248 return;
4249
4250 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4251 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4252}
4253
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004254static void haswell_crtc_enable(struct drm_crtc *crtc)
4255{
4256 struct drm_device *dev = crtc->dev;
4257 struct drm_i915_private *dev_priv = dev->dev_private;
4258 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4259 struct intel_encoder *encoder;
4260 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004261
4262 WARN_ON(!crtc->enabled);
4263
4264 if (intel_crtc->active)
4265 return;
4266
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004267 if (intel_crtc_to_shared_dpll(intel_crtc))
4268 intel_enable_shared_dpll(intel_crtc);
4269
Daniel Vetter229fca92014-04-24 23:55:09 +02004270 if (intel_crtc->config.has_dp_encoder)
4271 intel_dp_set_m_n(intel_crtc);
4272
4273 intel_set_pipe_timings(intel_crtc);
4274
Clint Taylorebb69c92014-09-30 10:30:22 -07004275 if (intel_crtc->config.cpu_transcoder != TRANSCODER_EDP) {
4276 I915_WRITE(PIPE_MULT(intel_crtc->config.cpu_transcoder),
4277 intel_crtc->config.pixel_multiplier - 1);
4278 }
4279
Daniel Vetter229fca92014-04-24 23:55:09 +02004280 if (intel_crtc->config.has_pch_encoder) {
4281 intel_cpu_transcoder_set_m_n(intel_crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07004282 &intel_crtc->config.fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004283 }
4284
4285 haswell_set_pipeconf(crtc);
4286
4287 intel_set_pipe_csc(crtc);
4288
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004289 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004290
Daniel Vettera72e4c92014-09-30 10:56:47 +02004291 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004292 for_each_encoder_on_crtc(dev, crtc, encoder)
4293 if (encoder->pre_enable)
4294 encoder->pre_enable(encoder);
4295
Imre Deak4fe94672014-06-25 22:01:49 +03004296 if (intel_crtc->config.has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004297 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4298 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004299 dev_priv->display.fdi_link_train(crtc);
4300 }
4301
Paulo Zanoni1f544382012-10-24 11:32:00 -02004302 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004303
Jesse Barnesb074cec2013-04-25 12:55:02 -07004304 ironlake_pfit_enable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004305
4306 /*
4307 * On ILK+ LUT must be loaded before the pipe is running but with
4308 * clocks enabled
4309 */
4310 intel_crtc_load_lut(crtc);
4311
Paulo Zanoni1f544382012-10-24 11:32:00 -02004312 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004313 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004314
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004315 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004316 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004317
Daniel Vetter5bfe2ac2013-03-27 00:44:55 +01004318 if (intel_crtc->config.has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004319 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004320
Dave Airlie0e32b392014-05-02 14:02:48 +10004321 if (intel_crtc->config.dp_encoder_is_mst)
4322 intel_ddi_set_vc_payload_alloc(crtc, true);
4323
Jani Nikula8807e552013-08-30 19:40:32 +03004324 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004325 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004326 intel_opregion_notify_encoder(encoder, true);
4327 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004328
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004329 assert_vblank_disabled(crtc);
4330 drm_crtc_vblank_on(crtc);
4331
Paulo Zanonie4916942013-09-20 16:21:19 -03004332 /* If we change the relative order between pipe/planes enabling, we need
4333 * to change the workaround. */
4334 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004335 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004336}
4337
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004338static void ironlake_pfit_disable(struct intel_crtc *crtc)
4339{
4340 struct drm_device *dev = crtc->base.dev;
4341 struct drm_i915_private *dev_priv = dev->dev_private;
4342 int pipe = crtc->pipe;
4343
4344 /* To avoid upsetting the power well on haswell only disable the pfit if
4345 * it's in use. The hw state code will make sure we get this right. */
Chris Wilsonfd4daa92013-08-27 17:04:17 +01004346 if (crtc->config.pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004347 I915_WRITE(PF_CTL(pipe), 0);
4348 I915_WRITE(PF_WIN_POS(pipe), 0);
4349 I915_WRITE(PF_WIN_SZ(pipe), 0);
4350 }
4351}
4352
Jesse Barnes6be4a602010-09-10 10:26:01 -07004353static void ironlake_crtc_disable(struct drm_crtc *crtc)
4354{
4355 struct drm_device *dev = crtc->dev;
4356 struct drm_i915_private *dev_priv = dev->dev_private;
4357 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004358 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004359 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004360 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004361
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004362 if (!intel_crtc->active)
4363 return;
4364
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004365 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004366
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004367 drm_crtc_vblank_off(crtc);
4368 assert_vblank_disabled(crtc);
4369
Daniel Vetterea9d7582012-07-10 10:42:52 +02004370 for_each_encoder_on_crtc(dev, crtc, encoder)
4371 encoder->disable(encoder);
4372
Daniel Vetterd925c592013-06-05 13:34:04 +02004373 if (intel_crtc->config.has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004374 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004375
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004376 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004377
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004378 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004379
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004380 for_each_encoder_on_crtc(dev, crtc, encoder)
4381 if (encoder->post_disable)
4382 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004383
Daniel Vetterd925c592013-06-05 13:34:04 +02004384 if (intel_crtc->config.has_pch_encoder) {
4385 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004386
Daniel Vetterd925c592013-06-05 13:34:04 +02004387 ironlake_disable_pch_transcoder(dev_priv, pipe);
Daniel Vettera72e4c92014-09-30 10:56:47 +02004388 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004389
Daniel Vetterd925c592013-06-05 13:34:04 +02004390 if (HAS_PCH_CPT(dev)) {
4391 /* disable TRANS_DP_CTL */
4392 reg = TRANS_DP_CTL(pipe);
4393 temp = I915_READ(reg);
4394 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4395 TRANS_DP_PORT_SEL_MASK);
4396 temp |= TRANS_DP_PORT_SEL_NONE;
4397 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004398
Daniel Vetterd925c592013-06-05 13:34:04 +02004399 /* disable DPLL_SEL */
4400 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004401 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02004402 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08004403 }
Daniel Vetterd925c592013-06-05 13:34:04 +02004404
4405 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004406 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02004407
4408 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004409 }
4410
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004411 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004412 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004413
4414 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01004415 intel_update_fbc(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01004416 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004417}
4418
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004419static void haswell_crtc_disable(struct drm_crtc *crtc)
4420{
4421 struct drm_device *dev = crtc->dev;
4422 struct drm_i915_private *dev_priv = dev->dev_private;
4423 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4424 struct intel_encoder *encoder;
Daniel Vetter3b117c82013-04-17 20:15:07 +02004425 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004426
4427 if (!intel_crtc->active)
4428 return;
4429
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004430 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03004431
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004432 drm_crtc_vblank_off(crtc);
4433 assert_vblank_disabled(crtc);
4434
Jani Nikula8807e552013-08-30 19:40:32 +03004435 for_each_encoder_on_crtc(dev, crtc, encoder) {
4436 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004437 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004438 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004439
Paulo Zanoni86642812013-04-12 17:57:57 -03004440 if (intel_crtc->config.has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004441 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4442 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004443 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004444
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03004445 if (intel_crtc->config.dp_encoder_is_mst)
4446 intel_ddi_set_vc_payload_alloc(crtc, false);
4447
Paulo Zanoniad80a812012-10-24 16:06:19 -02004448 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004449
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004450 ironlake_pfit_disable(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004451
Paulo Zanoni1f544382012-10-24 11:32:00 -02004452 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004453
Daniel Vetter88adfff2013-03-28 10:42:01 +01004454 if (intel_crtc->config.has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02004455 lpt_disable_pch_transcoder(dev_priv);
Daniel Vettera72e4c92014-09-30 10:56:47 +02004456 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4457 true);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02004458 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02004459 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004460
Imre Deak97b040a2014-06-25 22:01:50 +03004461 for_each_encoder_on_crtc(dev, crtc, encoder)
4462 if (encoder->post_disable)
4463 encoder->post_disable(encoder);
4464
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004465 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03004466 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004467
4468 mutex_lock(&dev->struct_mutex);
4469 intel_update_fbc(dev);
4470 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004471
4472 if (intel_crtc_to_shared_dpll(intel_crtc))
4473 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004474}
4475
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004476static void ironlake_crtc_off(struct drm_crtc *crtc)
4477{
4478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004479 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004480}
4481
Paulo Zanoni6441ab52012-10-05 12:05:58 -03004482
Jesse Barnes2dd24552013-04-25 12:55:01 -07004483static void i9xx_pfit_enable(struct intel_crtc *crtc)
4484{
4485 struct drm_device *dev = crtc->base.dev;
4486 struct drm_i915_private *dev_priv = dev->dev_private;
4487 struct intel_crtc_config *pipe_config = &crtc->config;
4488
Daniel Vetter328d8e82013-05-08 10:36:31 +02004489 if (!crtc->config.gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07004490 return;
4491
Daniel Vetterc0b03412013-05-28 12:05:54 +02004492 /*
4493 * The panel fitter should only be adjusted whilst the pipe is disabled,
4494 * according to register description and PRM.
4495 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07004496 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
4497 assert_pipe_disabled(dev_priv, crtc->pipe);
4498
Jesse Barnesb074cec2013-04-25 12:55:02 -07004499 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
4500 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02004501
4502 /* Border color in case we don't scale up to the full screen. Black by
4503 * default, change to something else for debugging. */
4504 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07004505}
4506
Dave Airlied05410f2014-06-05 13:22:59 +10004507static enum intel_display_power_domain port_to_power_domain(enum port port)
4508{
4509 switch (port) {
4510 case PORT_A:
4511 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
4512 case PORT_B:
4513 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
4514 case PORT_C:
4515 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
4516 case PORT_D:
4517 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
4518 default:
4519 WARN_ON_ONCE(1);
4520 return POWER_DOMAIN_PORT_OTHER;
4521 }
4522}
4523
Imre Deak77d22dc2014-03-05 16:20:52 +02004524#define for_each_power_domain(domain, mask) \
4525 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
4526 if ((1 << (domain)) & (mask))
4527
Imre Deak319be8a2014-03-04 19:22:57 +02004528enum intel_display_power_domain
4529intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02004530{
Imre Deak319be8a2014-03-04 19:22:57 +02004531 struct drm_device *dev = intel_encoder->base.dev;
4532 struct intel_digital_port *intel_dig_port;
4533
4534 switch (intel_encoder->type) {
4535 case INTEL_OUTPUT_UNKNOWN:
4536 /* Only DDI platforms should ever use this output type */
4537 WARN_ON_ONCE(!HAS_DDI(dev));
4538 case INTEL_OUTPUT_DISPLAYPORT:
4539 case INTEL_OUTPUT_HDMI:
4540 case INTEL_OUTPUT_EDP:
4541 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10004542 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10004543 case INTEL_OUTPUT_DP_MST:
4544 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
4545 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02004546 case INTEL_OUTPUT_ANALOG:
4547 return POWER_DOMAIN_PORT_CRT;
4548 case INTEL_OUTPUT_DSI:
4549 return POWER_DOMAIN_PORT_DSI;
4550 default:
4551 return POWER_DOMAIN_PORT_OTHER;
4552 }
4553}
4554
4555static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
4556{
4557 struct drm_device *dev = crtc->dev;
4558 struct intel_encoder *intel_encoder;
4559 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4560 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02004561 unsigned long mask;
4562 enum transcoder transcoder;
4563
4564 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
4565
4566 mask = BIT(POWER_DOMAIN_PIPE(pipe));
4567 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Daniel Vetterfabf6e52014-05-29 14:10:22 +02004568 if (intel_crtc->config.pch_pfit.enabled ||
4569 intel_crtc->config.pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02004570 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4571
Imre Deak319be8a2014-03-04 19:22:57 +02004572 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
4573 mask |= BIT(intel_display_port_power_domain(intel_encoder));
4574
Imre Deak77d22dc2014-03-05 16:20:52 +02004575 return mask;
4576}
4577
Imre Deak77d22dc2014-03-05 16:20:52 +02004578static void modeset_update_crtc_power_domains(struct drm_device *dev)
4579{
4580 struct drm_i915_private *dev_priv = dev->dev_private;
4581 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
4582 struct intel_crtc *crtc;
4583
4584 /*
4585 * First get all needed power domains, then put all unneeded, to avoid
4586 * any unnecessary toggling of the power wells.
4587 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004588 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004589 enum intel_display_power_domain domain;
4590
4591 if (!crtc->base.enabled)
4592 continue;
4593
Imre Deak319be8a2014-03-04 19:22:57 +02004594 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02004595
4596 for_each_power_domain(domain, pipe_domains[crtc->pipe])
4597 intel_display_power_get(dev_priv, domain);
4598 }
4599
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004600 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02004601 enum intel_display_power_domain domain;
4602
4603 for_each_power_domain(domain, crtc->enabled_power_domains)
4604 intel_display_power_put(dev_priv, domain);
4605
4606 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
4607 }
4608
4609 intel_display_set_init_power(dev_priv, false);
4610}
4611
Ville Syrjälädfcab172014-06-13 13:37:47 +03004612/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004613static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004614{
Jesse Barnes586f49d2013-11-04 16:06:59 -08004615 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08004616
Jesse Barnes586f49d2013-11-04 16:06:59 -08004617 /* Obtain SKU information */
4618 mutex_lock(&dev_priv->dpio_lock);
4619 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
4620 CCK_FUSE_HPLL_FREQ_MASK;
4621 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004622
Ville Syrjälädfcab172014-06-13 13:37:47 +03004623 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004624}
4625
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004626static void vlv_update_cdclk(struct drm_device *dev)
4627{
4628 struct drm_i915_private *dev_priv = dev->dev_private;
4629
4630 dev_priv->vlv_cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03004631 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004632 dev_priv->vlv_cdclk_freq);
4633
4634 /*
4635 * Program the gmbus_freq based on the cdclk frequency.
4636 * BSpec erroneously claims we should aim for 4MHz, but
4637 * in fact 1MHz is the correct frequency.
4638 */
4639 I915_WRITE(GMBUSFREQ_VLV, dev_priv->vlv_cdclk_freq);
4640}
4641
Jesse Barnes30a970c2013-11-04 13:48:12 -08004642/* Adjust CDclk dividers to allow high res or save power if possible */
4643static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
4644{
4645 struct drm_i915_private *dev_priv = dev->dev_private;
4646 u32 val, cmd;
4647
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03004648 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02004649
Ville Syrjälädfcab172014-06-13 13:37:47 +03004650 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08004651 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03004652 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004653 cmd = 1;
4654 else
4655 cmd = 0;
4656
4657 mutex_lock(&dev_priv->rps.hw_lock);
4658 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4659 val &= ~DSPFREQGUAR_MASK;
4660 val |= (cmd << DSPFREQGUAR_SHIFT);
4661 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4662 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4663 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
4664 50)) {
4665 DRM_ERROR("timed out waiting for CDclk change\n");
4666 }
4667 mutex_unlock(&dev_priv->rps.hw_lock);
4668
Ville Syrjälädfcab172014-06-13 13:37:47 +03004669 if (cdclk == 400000) {
Jesse Barnes30a970c2013-11-04 13:48:12 -08004670 u32 divider, vco;
4671
4672 vco = valleyview_get_vco(dev_priv);
Ville Syrjälädfcab172014-06-13 13:37:47 +03004673 divider = DIV_ROUND_CLOSEST(vco << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004674
4675 mutex_lock(&dev_priv->dpio_lock);
4676 /* adjust cdclk divider */
4677 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03004678 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004679 val |= divider;
4680 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03004681
4682 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
4683 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
4684 50))
4685 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08004686 mutex_unlock(&dev_priv->dpio_lock);
4687 }
4688
4689 mutex_lock(&dev_priv->dpio_lock);
4690 /* adjust self-refresh exit latency value */
4691 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
4692 val &= ~0x7f;
4693
4694 /*
4695 * For high bandwidth configs, we set a higher latency in the bunit
4696 * so that the core display fetch happens in time to avoid underruns.
4697 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03004698 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004699 val |= 4500 / 250; /* 4.5 usec */
4700 else
4701 val |= 3000 / 250; /* 3.0 usec */
4702 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
4703 mutex_unlock(&dev_priv->dpio_lock);
4704
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03004705 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004706}
4707
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004708static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
4709{
4710 struct drm_i915_private *dev_priv = dev->dev_private;
4711 u32 val, cmd;
4712
4713 WARN_ON(dev_priv->display.get_display_clock_speed(dev) != dev_priv->vlv_cdclk_freq);
4714
4715 switch (cdclk) {
4716 case 400000:
4717 cmd = 3;
4718 break;
4719 case 333333:
4720 case 320000:
4721 cmd = 2;
4722 break;
4723 case 266667:
4724 cmd = 1;
4725 break;
4726 case 200000:
4727 cmd = 0;
4728 break;
4729 default:
4730 WARN_ON(1);
4731 return;
4732 }
4733
4734 mutex_lock(&dev_priv->rps.hw_lock);
4735 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
4736 val &= ~DSPFREQGUAR_MASK_CHV;
4737 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
4738 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
4739 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
4740 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
4741 50)) {
4742 DRM_ERROR("timed out waiting for CDclk change\n");
4743 }
4744 mutex_unlock(&dev_priv->rps.hw_lock);
4745
4746 vlv_update_cdclk(dev);
4747}
4748
Jesse Barnes30a970c2013-11-04 13:48:12 -08004749static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
4750 int max_pixclk)
4751{
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004752 int vco = valleyview_get_vco(dev_priv);
4753 int freq_320 = (vco << 1) % 320000 != 0 ? 333333 : 320000;
4754
Ville Syrjäläd49a3402014-06-28 02:03:58 +03004755 /* FIXME: Punit isn't quite ready yet */
4756 if (IS_CHERRYVIEW(dev_priv->dev))
4757 return 400000;
4758
Jesse Barnes30a970c2013-11-04 13:48:12 -08004759 /*
4760 * Really only a few cases to deal with, as only 4 CDclks are supported:
4761 * 200MHz
4762 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004763 * 320/333MHz (depends on HPLL freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004764 * 400MHz
4765 * So we check to see whether we're above 90% of the lower bin and
4766 * adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004767 *
4768 * We seem to get an unstable or solid color picture at 200MHz.
4769 * Not sure what's wrong. For now use 200MHz only when all pipes
4770 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08004771 */
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004772 if (max_pixclk > freq_320*9/10)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004773 return 400000;
4774 else if (max_pixclk > 266667*9/10)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03004775 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004776 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03004777 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03004778 else
4779 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08004780}
4781
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004782/* compute the max pixel clock for new configuration */
4783static int intel_mode_max_pixclk(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004784{
4785 struct drm_device *dev = dev_priv->dev;
4786 struct intel_crtc *intel_crtc;
4787 int max_pixclk = 0;
4788
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004789 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004790 if (intel_crtc->new_enabled)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004791 max_pixclk = max(max_pixclk,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004792 intel_crtc->new_config->adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004793 }
4794
4795 return max_pixclk;
4796}
4797
4798static void valleyview_modeset_global_pipes(struct drm_device *dev,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004799 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004800{
4801 struct drm_i915_private *dev_priv = dev->dev_private;
4802 struct intel_crtc *intel_crtc;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004803 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004804
Imre Deakd60c4472014-03-27 17:45:10 +02004805 if (valleyview_calc_cdclk(dev_priv, max_pixclk) ==
4806 dev_priv->vlv_cdclk_freq)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004807 return;
4808
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004809 /* disable/enable all currently active pipes while we change cdclk */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004810 for_each_intel_crtc(dev, intel_crtc)
Jesse Barnes30a970c2013-11-04 13:48:12 -08004811 if (intel_crtc->base.enabled)
4812 *prepare_pipes |= (1 << intel_crtc->pipe);
4813}
4814
4815static void valleyview_modeset_global_resources(struct drm_device *dev)
4816{
4817 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02004818 int max_pixclk = intel_mode_max_pixclk(dev_priv);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004819 int req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
4820
Ville Syrjälä383c5a62014-06-28 02:03:57 +03004821 if (req_cdclk != dev_priv->vlv_cdclk_freq) {
4822 if (IS_CHERRYVIEW(dev))
4823 cherryview_set_cdclk(dev, req_cdclk);
4824 else
4825 valleyview_set_cdclk(dev, req_cdclk);
4826 }
4827
Imre Deak77961eb2014-03-05 16:20:56 +02004828 modeset_update_crtc_power_domains(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08004829}
4830
Jesse Barnes89b667f2013-04-18 14:51:36 -07004831static void valleyview_crtc_enable(struct drm_crtc *crtc)
4832{
4833 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02004834 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004835 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4836 struct intel_encoder *encoder;
4837 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03004838 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004839
4840 WARN_ON(!crtc->enabled);
4841
4842 if (intel_crtc->active)
4843 return;
4844
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004845 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05304846
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004847 if (!is_dsi) {
4848 if (IS_CHERRYVIEW(dev))
Ville Syrjäläd288f652014-10-28 13:20:22 +02004849 chv_prepare_pll(intel_crtc, &intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004850 else
Ville Syrjäläd288f652014-10-28 13:20:22 +02004851 vlv_prepare_pll(intel_crtc, &intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03004852 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02004853
4854 if (intel_crtc->config.has_dp_encoder)
4855 intel_dp_set_m_n(intel_crtc);
4856
4857 intel_set_pipe_timings(intel_crtc);
4858
Daniel Vetter5b18e572014-04-24 23:55:06 +02004859 i9xx_set_pipeconf(intel_crtc);
4860
Jesse Barnes89b667f2013-04-18 14:51:36 -07004861 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07004862
Daniel Vettera72e4c92014-09-30 10:56:47 +02004863 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004864
Jesse Barnes89b667f2013-04-18 14:51:36 -07004865 for_each_encoder_on_crtc(dev, crtc, encoder)
4866 if (encoder->pre_pll_enable)
4867 encoder->pre_pll_enable(encoder);
4868
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004869 if (!is_dsi) {
4870 if (IS_CHERRYVIEW(dev))
Ville Syrjäläd288f652014-10-28 13:20:22 +02004871 chv_enable_pll(intel_crtc, &intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004872 else
Ville Syrjäläd288f652014-10-28 13:20:22 +02004873 vlv_enable_pll(intel_crtc, &intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03004874 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07004875
4876 for_each_encoder_on_crtc(dev, crtc, encoder)
4877 if (encoder->pre_enable)
4878 encoder->pre_enable(encoder);
4879
Jesse Barnes2dd24552013-04-25 12:55:01 -07004880 i9xx_pfit_enable(intel_crtc);
4881
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004882 intel_crtc_load_lut(crtc);
4883
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004884 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004885 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004886
Jani Nikula50049452013-07-30 12:20:32 +03004887 for_each_encoder_on_crtc(dev, crtc, encoder)
4888 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004889
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004890 assert_vblank_disabled(crtc);
4891 drm_crtc_vblank_on(crtc);
4892
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004893 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004894
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004895 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02004896 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07004897}
4898
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004899static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
4900{
4901 struct drm_device *dev = crtc->base.dev;
4902 struct drm_i915_private *dev_priv = dev->dev_private;
4903
4904 I915_WRITE(FP0(crtc->pipe), crtc->config.dpll_hw_state.fp0);
4905 I915_WRITE(FP1(crtc->pipe), crtc->config.dpll_hw_state.fp1);
4906}
4907
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004908static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08004909{
4910 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02004911 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08004912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004913 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08004914 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08004915
Daniel Vetter08a48462012-07-02 11:43:47 +02004916 WARN_ON(!crtc->enabled);
4917
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004918 if (intel_crtc->active)
4919 return;
4920
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02004921 i9xx_set_pll_dividers(intel_crtc);
4922
Daniel Vetter5b18e572014-04-24 23:55:06 +02004923 if (intel_crtc->config.has_dp_encoder)
4924 intel_dp_set_m_n(intel_crtc);
4925
4926 intel_set_pipe_timings(intel_crtc);
4927
Daniel Vetter5b18e572014-04-24 23:55:06 +02004928 i9xx_set_pipeconf(intel_crtc);
4929
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004930 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01004931
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004932 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02004933 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004934
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02004935 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02004936 if (encoder->pre_enable)
4937 encoder->pre_enable(encoder);
4938
Daniel Vetterf6736a12013-06-05 13:34:30 +02004939 i9xx_enable_pll(intel_crtc);
4940
Jesse Barnes2dd24552013-04-25 12:55:01 -07004941 i9xx_pfit_enable(intel_crtc);
4942
Ville Syrjälä63cbb072013-06-04 13:48:59 +03004943 intel_crtc_load_lut(crtc);
4944
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004945 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004946 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02004947
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004948 for_each_encoder_on_crtc(dev, crtc, encoder)
4949 encoder->enable(encoder);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004950
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03004951 assert_vblank_disabled(crtc);
4952 drm_crtc_vblank_on(crtc);
4953
Ville Syrjälä9ab04602014-05-08 19:23:14 +03004954 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02004955
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004956 /*
4957 * Gen2 reports pipe underruns whenever all planes are disabled.
4958 * So don't enable underrun reporting before at least some planes
4959 * are enabled.
4960 * FIXME: Need to fix the logic to work when we turn off all planes
4961 * but leave the pipe running.
4962 */
4963 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02004964 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004965
Ville Syrjälä56b80e12014-05-16 19:40:22 +03004966 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02004967 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004968}
4969
Daniel Vetter87476d62013-04-11 16:29:06 +02004970static void i9xx_pfit_disable(struct intel_crtc *crtc)
4971{
4972 struct drm_device *dev = crtc->base.dev;
4973 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02004974
4975 if (!crtc->config.gmch_pfit.control)
4976 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02004977
4978 assert_pipe_disabled(dev_priv, crtc->pipe);
4979
Daniel Vetter328d8e82013-05-08 10:36:31 +02004980 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
4981 I915_READ(PFIT_CONTROL));
4982 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02004983}
4984
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004985static void i9xx_crtc_disable(struct drm_crtc *crtc)
4986{
4987 struct drm_device *dev = crtc->dev;
4988 struct drm_i915_private *dev_priv = dev->dev_private;
4989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004990 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07004991 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004992
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004993 if (!intel_crtc->active)
4994 return;
4995
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03004996 /*
4997 * Gen2 reports pipe underruns whenever all planes are disabled.
4998 * So diasble underrun reporting before all the planes get disabled.
4999 * FIXME: Need to fix the logic to work when we turn off all planes
5000 * but leave the pipe running.
5001 */
5002 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005003 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005004
Imre Deak564ed192014-06-13 14:54:21 +03005005 /*
5006 * Vblank time updates from the shadow to live plane control register
5007 * are blocked if the memory self-refresh mode is active at that
5008 * moment. So to make sure the plane gets truly disabled, disable
5009 * first the self-refresh mode. The self-refresh enable bit in turn
5010 * will be checked/applied by the HW only at the next frame start
5011 * event which is after the vblank start event, so we need to have a
5012 * wait-for-vblank between disabling the plane and the pipe.
5013 */
5014 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005015 intel_crtc_disable_planes(crtc);
5016
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005017 /*
5018 * On gen2 planes are double buffered but the pipe isn't, so we must
5019 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005020 * We also need to wait on all gmch platforms because of the
5021 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005022 */
Imre Deak564ed192014-06-13 14:54:21 +03005023 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005024
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005025 drm_crtc_vblank_off(crtc);
5026 assert_vblank_disabled(crtc);
5027
5028 for_each_encoder_on_crtc(dev, crtc, encoder)
5029 encoder->disable(encoder);
5030
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005031 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005032
Daniel Vetter87476d62013-04-11 16:29:06 +02005033 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005034
Jesse Barnes89b667f2013-04-18 14:51:36 -07005035 for_each_encoder_on_crtc(dev, crtc, encoder)
5036 if (encoder->post_disable)
5037 encoder->post_disable(encoder);
5038
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005039 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005040 if (IS_CHERRYVIEW(dev))
5041 chv_disable_pll(dev_priv, pipe);
5042 else if (IS_VALLEYVIEW(dev))
5043 vlv_disable_pll(dev_priv, pipe);
5044 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005045 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005046 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005047
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005048 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005049 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005050
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005051 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005052 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005053
Daniel Vetterefa96242014-04-24 23:55:02 +02005054 mutex_lock(&dev->struct_mutex);
Chris Wilson6b383a72010-09-13 13:54:26 +01005055 intel_update_fbc(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005056 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005057}
5058
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005059static void i9xx_crtc_off(struct drm_crtc *crtc)
5060{
5061}
5062
Daniel Vetter976f8a22012-07-08 22:34:21 +02005063static void intel_crtc_update_sarea(struct drm_crtc *crtc,
5064 bool enabled)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005065{
5066 struct drm_device *dev = crtc->dev;
5067 struct drm_i915_master_private *master_priv;
5068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5069 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005070
5071 if (!dev->primary->master)
5072 return;
5073
5074 master_priv = dev->primary->master->driver_priv;
5075 if (!master_priv->sarea_priv)
5076 return;
5077
Jesse Barnes79e53942008-11-07 14:24:08 -08005078 switch (pipe) {
5079 case 0:
5080 master_priv->sarea_priv->pipeA_w = enabled ? crtc->mode.hdisplay : 0;
5081 master_priv->sarea_priv->pipeA_h = enabled ? crtc->mode.vdisplay : 0;
5082 break;
5083 case 1:
5084 master_priv->sarea_priv->pipeB_w = enabled ? crtc->mode.hdisplay : 0;
5085 master_priv->sarea_priv->pipeB_h = enabled ? crtc->mode.vdisplay : 0;
5086 break;
5087 default:
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005088 DRM_ERROR("Can't update pipe %c in SAREA\n", pipe_name(pipe));
Jesse Barnes79e53942008-11-07 14:24:08 -08005089 break;
5090 }
Jesse Barnes79e53942008-11-07 14:24:08 -08005091}
5092
Borun Fub04c5bd2014-07-12 10:02:27 +05305093/* Master function to enable/disable CRTC and corresponding power wells */
5094void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005095{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005096 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005097 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005099 enum intel_display_power_domain domain;
5100 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005101
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005102 if (enable) {
5103 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005104 domains = get_crtc_power_domains(crtc);
5105 for_each_power_domain(domain, domains)
5106 intel_display_power_get(dev_priv, domain);
5107 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005108
5109 dev_priv->display.crtc_enable(crtc);
5110 }
5111 } else {
5112 if (intel_crtc->active) {
5113 dev_priv->display.crtc_disable(crtc);
5114
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005115 domains = intel_crtc->enabled_power_domains;
5116 for_each_power_domain(domain, domains)
5117 intel_display_power_put(dev_priv, domain);
5118 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005119 }
5120 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305121}
5122
5123/**
5124 * Sets the power management mode of the pipe and plane.
5125 */
5126void intel_crtc_update_dpms(struct drm_crtc *crtc)
5127{
5128 struct drm_device *dev = crtc->dev;
5129 struct intel_encoder *intel_encoder;
5130 bool enable = false;
5131
5132 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5133 enable |= intel_encoder->connectors_active;
5134
5135 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005136
5137 intel_crtc_update_sarea(crtc, enable);
5138}
5139
Daniel Vetter976f8a22012-07-08 22:34:21 +02005140static void intel_crtc_disable(struct drm_crtc *crtc)
5141{
5142 struct drm_device *dev = crtc->dev;
5143 struct drm_connector *connector;
5144 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2ff8fde2014-07-08 07:50:07 -07005145 struct drm_i915_gem_object *old_obj = intel_fb_obj(crtc->primary->fb);
Daniel Vettera071fa02014-06-18 23:28:09 +02005146 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005147
5148 /* crtc should still be enabled when we disable it. */
5149 WARN_ON(!crtc->enabled);
5150
5151 dev_priv->display.crtc_disable(crtc);
5152 intel_crtc_update_sarea(crtc, false);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005153 dev_priv->display.off(crtc);
5154
Matt Roperf4510a22014-04-01 15:22:40 -07005155 if (crtc->primary->fb) {
Chris Wilsoncdd59982010-09-08 16:30:16 +01005156 mutex_lock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +02005157 intel_unpin_fb_obj(old_obj);
5158 i915_gem_track_fb(old_obj, NULL,
5159 INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilsoncdd59982010-09-08 16:30:16 +01005160 mutex_unlock(&dev->struct_mutex);
Matt Roperf4510a22014-04-01 15:22:40 -07005161 crtc->primary->fb = NULL;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005162 }
5163
5164 /* Update computed state. */
5165 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
5166 if (!connector->encoder || !connector->encoder->crtc)
5167 continue;
5168
5169 if (connector->encoder->crtc != crtc)
5170 continue;
5171
5172 connector->dpms = DRM_MODE_DPMS_OFF;
5173 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01005174 }
5175}
5176
Chris Wilsonea5b2132010-08-04 13:50:23 +01005177void intel_encoder_destroy(struct drm_encoder *encoder)
5178{
Chris Wilson4ef69c72010-09-09 15:14:28 +01005179 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01005180
Chris Wilsonea5b2132010-08-04 13:50:23 +01005181 drm_encoder_cleanup(encoder);
5182 kfree(intel_encoder);
5183}
5184
Damien Lespiau92373292013-08-08 22:28:57 +01005185/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005186 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
5187 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01005188static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005189{
5190 if (mode == DRM_MODE_DPMS_ON) {
5191 encoder->connectors_active = true;
5192
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005193 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005194 } else {
5195 encoder->connectors_active = false;
5196
Daniel Vetterb2cabb02012-07-01 22:42:24 +02005197 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005198 }
5199}
5200
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005201/* Cross check the actual hw state with our own modeset state tracking (and it's
5202 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02005203static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005204{
5205 if (connector->get_hw_state(connector)) {
5206 struct intel_encoder *encoder = connector->encoder;
5207 struct drm_crtc *crtc;
5208 bool encoder_enabled;
5209 enum pipe pipe;
5210
5211 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
5212 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03005213 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005214
Dave Airlie0e32b392014-05-02 14:02:48 +10005215 /* there is no real hw state for MST connectors */
5216 if (connector->mst_port)
5217 return;
5218
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005219 WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
5220 "wrong connector dpms state\n");
5221 WARN(connector->base.encoder != &encoder->base,
5222 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005223
Dave Airlie36cd7442014-05-02 13:44:18 +10005224 if (encoder) {
5225 WARN(!encoder->connectors_active,
5226 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005227
Dave Airlie36cd7442014-05-02 13:44:18 +10005228 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
5229 WARN(!encoder_enabled, "encoder not enabled\n");
5230 if (WARN_ON(!encoder->base.crtc))
5231 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005232
Dave Airlie36cd7442014-05-02 13:44:18 +10005233 crtc = encoder->base.crtc;
5234
5235 WARN(!crtc->enabled, "crtc not enabled\n");
5236 WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
5237 WARN(pipe != to_intel_crtc(crtc)->pipe,
5238 "encoder active on the wrong pipe\n");
5239 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005240 }
5241}
5242
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005243/* Even simpler default implementation, if there's really no special case to
5244 * consider. */
5245void intel_connector_dpms(struct drm_connector *connector, int mode)
5246{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005247 /* All the simple cases only support two dpms states. */
5248 if (mode != DRM_MODE_DPMS_ON)
5249 mode = DRM_MODE_DPMS_OFF;
5250
5251 if (mode == connector->dpms)
5252 return;
5253
5254 connector->dpms = mode;
5255
5256 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01005257 if (connector->encoder)
5258 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02005259
Daniel Vetterb9805142012-08-31 17:37:33 +02005260 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02005261}
5262
Daniel Vetterf0947c32012-07-02 13:10:34 +02005263/* Simple connector->get_hw_state implementation for encoders that support only
5264 * one connector and no cloning and hence the encoder state determines the state
5265 * of the connector. */
5266bool intel_connector_get_hw_state(struct intel_connector *connector)
5267{
Daniel Vetter24929352012-07-02 20:28:59 +02005268 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02005269 struct intel_encoder *encoder = connector->encoder;
5270
5271 return encoder->get_hw_state(encoder, &pipe);
5272}
5273
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005274static bool ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
5275 struct intel_crtc_config *pipe_config)
5276{
5277 struct drm_i915_private *dev_priv = dev->dev_private;
5278 struct intel_crtc *pipe_B_crtc =
5279 to_intel_crtc(dev_priv->pipe_to_crtc_mapping[PIPE_B]);
5280
5281 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
5282 pipe_name(pipe), pipe_config->fdi_lanes);
5283 if (pipe_config->fdi_lanes > 4) {
5284 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
5285 pipe_name(pipe), pipe_config->fdi_lanes);
5286 return false;
5287 }
5288
Paulo Zanonibafb6552013-11-02 21:07:44 -07005289 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005290 if (pipe_config->fdi_lanes > 2) {
5291 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
5292 pipe_config->fdi_lanes);
5293 return false;
5294 } else {
5295 return true;
5296 }
5297 }
5298
5299 if (INTEL_INFO(dev)->num_pipes == 2)
5300 return true;
5301
5302 /* Ivybridge 3 pipe is really complicated */
5303 switch (pipe) {
5304 case PIPE_A:
5305 return true;
5306 case PIPE_B:
5307 if (dev_priv->pipe_to_crtc_mapping[PIPE_C]->enabled &&
5308 pipe_config->fdi_lanes > 2) {
5309 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5310 pipe_name(pipe), pipe_config->fdi_lanes);
5311 return false;
5312 }
5313 return true;
5314 case PIPE_C:
Daniel Vetter1e833f42013-02-19 22:31:57 +01005315 if (!pipe_has_enabled_pch(pipe_B_crtc) ||
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005316 pipe_B_crtc->config.fdi_lanes <= 2) {
5317 if (pipe_config->fdi_lanes > 2) {
5318 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
5319 pipe_name(pipe), pipe_config->fdi_lanes);
5320 return false;
5321 }
5322 } else {
5323 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
5324 return false;
5325 }
5326 return true;
5327 default:
5328 BUG();
5329 }
5330}
5331
Daniel Vettere29c22c2013-02-21 00:00:16 +01005332#define RETRY 1
5333static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
5334 struct intel_crtc_config *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02005335{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005336 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005337 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Daniel Vetterff9a6752013-06-01 17:16:21 +02005338 int lane, link_bw, fdi_dotclock;
Daniel Vettere29c22c2013-02-21 00:00:16 +01005339 bool setup_ok, needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005340
Daniel Vettere29c22c2013-02-21 00:00:16 +01005341retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02005342 /* FDI is a binary signal running at ~2.7GHz, encoding
5343 * each output octet as 10 bits. The actual frequency
5344 * is stored as a divider into a 100MHz clock, and the
5345 * mode pixel clock is stored in units of 1KHz.
5346 * Hence the bw of each lane in terms of the mode signal
5347 * is:
5348 */
5349 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
5350
Damien Lespiau241bfc32013-09-25 16:45:37 +01005351 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005352
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005353 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005354 pipe_config->pipe_bpp);
5355
5356 pipe_config->fdi_lanes = lane;
5357
Daniel Vetter2bd89a02013-06-01 17:16:19 +02005358 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02005359 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02005360
Daniel Vettere29c22c2013-02-21 00:00:16 +01005361 setup_ok = ironlake_check_fdi_lanes(intel_crtc->base.dev,
5362 intel_crtc->pipe, pipe_config);
5363 if (!setup_ok && pipe_config->pipe_bpp > 6*3) {
5364 pipe_config->pipe_bpp -= 2*3;
5365 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
5366 pipe_config->pipe_bpp);
5367 needs_recompute = true;
5368 pipe_config->bw_constrained = true;
5369
5370 goto retry;
5371 }
5372
5373 if (needs_recompute)
5374 return RETRY;
5375
5376 return setup_ok ? 0 : -EINVAL;
Daniel Vetter877d48d2013-04-19 11:24:43 +02005377}
5378
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005379static void hsw_compute_ips_config(struct intel_crtc *crtc,
5380 struct intel_crtc_config *pipe_config)
5381{
Jani Nikulad330a952014-01-21 11:24:25 +02005382 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03005383 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07005384 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005385}
5386
Daniel Vettera43f6e02013-06-07 23:10:32 +02005387static int intel_crtc_compute_config(struct intel_crtc *crtc,
Daniel Vettere29c22c2013-02-21 00:00:16 +01005388 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08005389{
Daniel Vettera43f6e02013-06-07 23:10:32 +02005390 struct drm_device *dev = crtc->base.dev;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01005391 struct drm_display_mode *adjusted_mode = &pipe_config->adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01005392
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005393 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005394 if (INTEL_INFO(dev)->gen < 4) {
5395 struct drm_i915_private *dev_priv = dev->dev_private;
5396 int clock_limit =
5397 dev_priv->display.get_display_clock_speed(dev);
5398
5399 /*
5400 * Enable pixel doubling when the dot clock
5401 * is > 90% of the (display) core speed.
5402 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03005403 * GDG double wide on either pipe,
5404 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005405 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03005406 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01005407 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005408 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03005409 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03005410 }
5411
Damien Lespiau241bfc32013-09-25 16:45:37 +01005412 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005413 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08005414 }
Chris Wilson89749352010-09-12 18:25:19 +01005415
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005416 /*
5417 * Pipe horizontal size must be even in:
5418 * - DVO ganged mode
5419 * - LVDS dual channel mode
5420 * - Double wide pipe
5421 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005422 if ((intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03005423 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
5424 pipe_config->pipe_src_w &= ~1;
5425
Damien Lespiau8693a822013-05-03 18:48:11 +01005426 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
5427 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03005428 */
5429 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
5430 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01005431 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03005432
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005433 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)) && pipe_config->pipe_bpp > 10*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005434 pipe_config->pipe_bpp = 10*3; /* 12bpc is gen5+ */
Daniel Vetterbd080ee2013-04-17 20:01:39 +02005435 } else if (INTEL_INFO(dev)->gen <= 4 && pipe_config->pipe_bpp > 8*3) {
Daniel Vetter5d2d38d2013-03-27 00:45:01 +01005436 /* only a 8bpc pipe, with 6bpc dither through the panel fitter
5437 * for lvds. */
5438 pipe_config->pipe_bpp = 8*3;
5439 }
5440
Damien Lespiauf5adf942013-06-24 18:29:34 +01005441 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005442 hsw_compute_ips_config(crtc, pipe_config);
5443
Daniel Vetter12030432014-06-25 22:02:00 +03005444 /*
5445 * XXX: PCH/WRPLL clock sharing is done in ->mode_set, so make sure the
5446 * old clock survives for now.
5447 */
5448 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev) || HAS_DDI(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02005449 pipe_config->shared_dpll = crtc->config.shared_dpll;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005450
Daniel Vetter877d48d2013-04-19 11:24:43 +02005451 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02005452 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02005453
Daniel Vettere29c22c2013-02-21 00:00:16 +01005454 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08005455}
5456
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005457static int valleyview_get_display_clock_speed(struct drm_device *dev)
5458{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005459 struct drm_i915_private *dev_priv = dev->dev_private;
5460 int vco = valleyview_get_vco(dev_priv);
5461 u32 val;
5462 int divider;
5463
Ville Syrjäläd49a3402014-06-28 02:03:58 +03005464 /* FIXME: Punit isn't quite ready yet */
5465 if (IS_CHERRYVIEW(dev))
5466 return 400000;
5467
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005468 mutex_lock(&dev_priv->dpio_lock);
5469 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
5470 mutex_unlock(&dev_priv->dpio_lock);
5471
5472 divider = val & DISPLAY_FREQUENCY_VALUES;
5473
Ville Syrjälä7d007f42014-06-13 13:37:53 +03005474 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
5475 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5476 "cdclk change in progress\n");
5477
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03005478 return DIV_ROUND_CLOSEST(vco << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07005479}
5480
Jesse Barnese70236a2009-09-21 10:42:27 -07005481static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08005482{
Jesse Barnese70236a2009-09-21 10:42:27 -07005483 return 400000;
5484}
Jesse Barnes79e53942008-11-07 14:24:08 -08005485
Jesse Barnese70236a2009-09-21 10:42:27 -07005486static int i915_get_display_clock_speed(struct drm_device *dev)
5487{
5488 return 333000;
5489}
Jesse Barnes79e53942008-11-07 14:24:08 -08005490
Jesse Barnese70236a2009-09-21 10:42:27 -07005491static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
5492{
5493 return 200000;
5494}
Jesse Barnes79e53942008-11-07 14:24:08 -08005495
Daniel Vetter257a7ff2013-07-26 08:35:42 +02005496static int pnv_get_display_clock_speed(struct drm_device *dev)
5497{
5498 u16 gcfgc = 0;
5499
5500 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5501
5502 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5503 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
5504 return 267000;
5505 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
5506 return 333000;
5507 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
5508 return 444000;
5509 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
5510 return 200000;
5511 default:
5512 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
5513 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
5514 return 133000;
5515 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
5516 return 167000;
5517 }
5518}
5519
Jesse Barnese70236a2009-09-21 10:42:27 -07005520static int i915gm_get_display_clock_speed(struct drm_device *dev)
5521{
5522 u16 gcfgc = 0;
5523
5524 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
5525
5526 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Jesse Barnes79e53942008-11-07 14:24:08 -08005527 return 133000;
Jesse Barnese70236a2009-09-21 10:42:27 -07005528 else {
5529 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
5530 case GC_DISPLAY_CLOCK_333_MHZ:
5531 return 333000;
5532 default:
5533 case GC_DISPLAY_CLOCK_190_200_MHZ:
5534 return 190000;
5535 }
5536 }
5537}
Jesse Barnes79e53942008-11-07 14:24:08 -08005538
Jesse Barnese70236a2009-09-21 10:42:27 -07005539static int i865_get_display_clock_speed(struct drm_device *dev)
5540{
5541 return 266000;
5542}
5543
5544static int i855_get_display_clock_speed(struct drm_device *dev)
5545{
5546 u16 hpllcc = 0;
5547 /* Assume that the hardware is in the high speed state. This
5548 * should be the default.
5549 */
5550 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
5551 case GC_CLOCK_133_200:
5552 case GC_CLOCK_100_200:
5553 return 200000;
5554 case GC_CLOCK_166_250:
5555 return 250000;
5556 case GC_CLOCK_100_133:
5557 return 133000;
5558 }
5559
5560 /* Shouldn't happen */
5561 return 0;
5562}
5563
5564static int i830_get_display_clock_speed(struct drm_device *dev)
5565{
5566 return 133000;
Jesse Barnes79e53942008-11-07 14:24:08 -08005567}
5568
Zhenyu Wang2c072452009-06-05 15:38:42 +08005569static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005570intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005571{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005572 while (*num > DATA_LINK_M_N_MASK ||
5573 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08005574 *num >>= 1;
5575 *den >>= 1;
5576 }
5577}
5578
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005579static void compute_m_n(unsigned int m, unsigned int n,
5580 uint32_t *ret_m, uint32_t *ret_n)
5581{
5582 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5583 *ret_m = div_u64((uint64_t) m * *ret_n, n);
5584 intel_reduce_m_n_ratio(ret_m, ret_n);
5585}
5586
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005587void
5588intel_link_compute_m_n(int bits_per_pixel, int nlanes,
5589 int pixel_clock, int link_clock,
5590 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005591{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01005592 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03005593
5594 compute_m_n(bits_per_pixel * pixel_clock,
5595 link_clock * nlanes * 8,
5596 &m_n->gmch_m, &m_n->gmch_n);
5597
5598 compute_m_n(pixel_clock, link_clock,
5599 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08005600}
5601
Chris Wilsona7615032011-01-12 17:04:08 +00005602static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
5603{
Jani Nikulad330a952014-01-21 11:24:25 +02005604 if (i915.panel_use_ssc >= 0)
5605 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03005606 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07005607 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00005608}
5609
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005610static int i9xx_get_refclk(struct intel_crtc *crtc, int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005611{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005612 struct drm_device *dev = crtc->base.dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005613 struct drm_i915_private *dev_priv = dev->dev_private;
5614 int refclk;
5615
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005616 if (IS_VALLEYVIEW(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02005617 refclk = 100000;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005618 } else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005619 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02005620 refclk = dev_priv->vbt.lvds_ssc_freq;
5621 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005622 } else if (!IS_GEN2(dev)) {
5623 refclk = 96000;
5624 } else {
5625 refclk = 48000;
5626 }
5627
5628 return refclk;
5629}
5630
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005631static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005632{
Daniel Vetter7df00d72013-05-21 21:54:55 +02005633 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005634}
Daniel Vetterf47709a2013-03-28 10:42:02 +01005635
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005636static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
5637{
5638 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08005639}
5640
Daniel Vetterf47709a2013-03-28 10:42:02 +01005641static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Jesse Barnesa7516a02011-12-15 12:30:37 -08005642 intel_clock_t *reduced_clock)
5643{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005644 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005645 u32 fp, fp2 = 0;
5646
5647 if (IS_PINEVIEW(dev)) {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005648 fp = pnv_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005649 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005650 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005651 } else {
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005652 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005653 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02005654 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08005655 }
5656
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005657 crtc->config.dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005658
Daniel Vetterf47709a2013-03-28 10:42:02 +01005659 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005660 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Jani Nikulad330a952014-01-21 11:24:25 +02005661 reduced_clock && i915.powersave) {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005662 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005663 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005664 } else {
Daniel Vetter8bcc2792013-06-05 13:34:28 +02005665 crtc->config.dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08005666 }
5667}
5668
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005669static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
5670 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07005671{
5672 u32 reg_val;
5673
5674 /*
5675 * PLLB opamp always calibrates to max value of 0x3f, force enable it
5676 * and set it to a reasonable value instead.
5677 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005678 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005679 reg_val &= 0xffffff00;
5680 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005681 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005682
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005683 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005684 reg_val &= 0x8cffffff;
5685 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005686 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005687
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005688 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005689 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005690 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005691
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005692 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005693 reg_val &= 0x00ffffff;
5694 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005695 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005696}
5697
Daniel Vetterb5518422013-05-03 11:49:48 +02005698static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
5699 struct intel_link_m_n *m_n)
5700{
5701 struct drm_device *dev = crtc->base.dev;
5702 struct drm_i915_private *dev_priv = dev->dev_private;
5703 int pipe = crtc->pipe;
5704
Daniel Vettere3b95f12013-05-03 11:49:49 +02005705 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5706 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5707 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5708 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005709}
5710
5711static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07005712 struct intel_link_m_n *m_n,
5713 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02005714{
5715 struct drm_device *dev = crtc->base.dev;
5716 struct drm_i915_private *dev_priv = dev->dev_private;
5717 int pipe = crtc->pipe;
5718 enum transcoder transcoder = crtc->config.cpu_transcoder;
5719
5720 if (INTEL_INFO(dev)->gen >= 5) {
5721 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
5722 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
5723 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
5724 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07005725 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
5726 * for gen < 8) and if DRRS is supported (to make sure the
5727 * registers are not unnecessarily accessed).
5728 */
5729 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
5730 crtc->config.has_drrs) {
5731 I915_WRITE(PIPE_DATA_M2(transcoder),
5732 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5733 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
5734 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
5735 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
5736 }
Daniel Vetterb5518422013-05-03 11:49:48 +02005737 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02005738 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
5739 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5740 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
5741 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02005742 }
5743}
5744
Vandana Kannanf769cd22014-08-05 07:51:22 -07005745void intel_dp_set_m_n(struct intel_crtc *crtc)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005746{
5747 if (crtc->config.has_pch_encoder)
5748 intel_pch_transcoder_set_m_n(crtc, &crtc->config.dp_m_n);
5749 else
Vandana Kannanf769cd22014-08-05 07:51:22 -07005750 intel_cpu_transcoder_set_m_n(crtc, &crtc->config.dp_m_n,
5751 &crtc->config.dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02005752}
5753
Ville Syrjäläd288f652014-10-28 13:20:22 +02005754static void vlv_update_pll(struct intel_crtc *crtc,
5755 struct intel_crtc_config *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005756{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005757 u32 dpll, dpll_md;
5758
5759 /*
5760 * Enable DPIO clock input. We should never disable the reference
5761 * clock for pipe B, since VGA hotplug / manual detection depends
5762 * on it.
5763 */
5764 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
5765 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
5766 /* We should never disable this, set it here for state tracking */
5767 if (crtc->pipe == PIPE_B)
5768 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
5769 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02005770 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005771
Ville Syrjäläd288f652014-10-28 13:20:22 +02005772 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005773 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02005774 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005775}
5776
Ville Syrjäläd288f652014-10-28 13:20:22 +02005777static void vlv_prepare_pll(struct intel_crtc *crtc,
5778 const struct intel_crtc_config *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005779{
Daniel Vetterf47709a2013-03-28 10:42:02 +01005780 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005781 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01005782 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005783 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005784 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005785 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005786
Daniel Vetter09153002012-12-12 14:06:44 +01005787 mutex_lock(&dev_priv->dpio_lock);
5788
Ville Syrjäläd288f652014-10-28 13:20:22 +02005789 bestn = pipe_config->dpll.n;
5790 bestm1 = pipe_config->dpll.m1;
5791 bestm2 = pipe_config->dpll.m2;
5792 bestp1 = pipe_config->dpll.p1;
5793 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005794
Jesse Barnes89b667f2013-04-18 14:51:36 -07005795 /* See eDP HDMI DPIO driver vbios notes doc */
5796
5797 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005798 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08005799 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005800
5801 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005802 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005803
5804 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005805 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005806 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005807 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005808
5809 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005810 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005811
5812 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005813 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
5814 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
5815 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005816 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07005817
5818 /*
5819 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
5820 * but we don't support that).
5821 * Note: don't use the DAC post divider as it seems unstable.
5822 */
5823 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005824 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005825
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005826 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005827 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005828
Jesse Barnes89b667f2013-04-18 14:51:36 -07005829 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02005830 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005831 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
5832 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005833 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03005834 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005835 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005836 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005837 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005838
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005839 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP) ||
5840 intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT)) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07005841 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005842 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005843 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005844 0x0df40000);
5845 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005846 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005847 0x0df70000);
5848 } else { /* HDMI or VGA */
5849 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02005850 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005851 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005852 0x0df70000);
5853 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005854 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07005855 0x0df40000);
5856 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005857
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005858 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07005859 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005860 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
5861 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07005862 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005863 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005864
Chon Ming Leeab3c7592013-11-07 10:43:30 +08005865 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01005866 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07005867}
5868
Ville Syrjäläd288f652014-10-28 13:20:22 +02005869static void chv_update_pll(struct intel_crtc *crtc,
5870 struct intel_crtc_config *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005871{
Ville Syrjäläd288f652014-10-28 13:20:22 +02005872 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005873 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
5874 DPLL_VCO_ENABLE;
5875 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02005876 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005877
Ville Syrjäläd288f652014-10-28 13:20:22 +02005878 pipe_config->dpll_hw_state.dpll_md =
5879 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005880}
5881
Ville Syrjäläd288f652014-10-28 13:20:22 +02005882static void chv_prepare_pll(struct intel_crtc *crtc,
5883 const struct intel_crtc_config *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005884{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005885 struct drm_device *dev = crtc->base.dev;
5886 struct drm_i915_private *dev_priv = dev->dev_private;
5887 int pipe = crtc->pipe;
5888 int dpll_reg = DPLL(crtc->pipe);
5889 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Ville Syrjälä580d3812014-04-09 13:29:00 +03005890 u32 loopfilter, intcoeff;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005891 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
5892 int refclk;
5893
Ville Syrjäläd288f652014-10-28 13:20:22 +02005894 bestn = pipe_config->dpll.n;
5895 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
5896 bestm1 = pipe_config->dpll.m1;
5897 bestm2 = pipe_config->dpll.m2 >> 22;
5898 bestp1 = pipe_config->dpll.p1;
5899 bestp2 = pipe_config->dpll.p2;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005900
5901 /*
5902 * Enable Refclk and SSC
5903 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005904 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02005905 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03005906
5907 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005908
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005909 /* p1 and p2 divider */
5910 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
5911 5 << DPIO_CHV_S1_DIV_SHIFT |
5912 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
5913 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
5914 1 << DPIO_CHV_K_DIV_SHIFT);
5915
5916 /* Feedback post-divider - m2 */
5917 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
5918
5919 /* Feedback refclk divider - n and m1 */
5920 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
5921 DPIO_CHV_M1_DIV_BY_2 |
5922 1 << DPIO_CHV_N_DIV_SHIFT);
5923
5924 /* M2 fraction division */
5925 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
5926
5927 /* M2 fraction division enable */
5928 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port),
5929 DPIO_CHV_FRAC_DIV_EN |
5930 (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT));
5931
5932 /* Loop filter */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005933 refclk = i9xx_get_refclk(crtc, 0);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005934 loopfilter = 5 << DPIO_CHV_PROP_COEFF_SHIFT |
5935 2 << DPIO_CHV_GAIN_CTRL_SHIFT;
5936 if (refclk == 100000)
5937 intcoeff = 11;
5938 else if (refclk == 38400)
5939 intcoeff = 10;
5940 else
5941 intcoeff = 9;
5942 loopfilter |= intcoeff << DPIO_CHV_INT_COEFF_SHIFT;
5943 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
5944
5945 /* AFC Recal */
5946 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
5947 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
5948 DPIO_AFC_RECAL);
5949
5950 mutex_unlock(&dev_priv->dpio_lock);
5951}
5952
Ville Syrjäläd288f652014-10-28 13:20:22 +02005953/**
5954 * vlv_force_pll_on - forcibly enable just the PLL
5955 * @dev_priv: i915 private structure
5956 * @pipe: pipe PLL to enable
5957 * @dpll: PLL configuration
5958 *
5959 * Enable the PLL for @pipe using the supplied @dpll config. To be used
5960 * in cases where we need the PLL enabled even when @pipe is not going to
5961 * be enabled.
5962 */
5963void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
5964 const struct dpll *dpll)
5965{
5966 struct intel_crtc *crtc =
5967 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
5968 struct intel_crtc_config pipe_config = {
5969 .pixel_multiplier = 1,
5970 .dpll = *dpll,
5971 };
5972
5973 if (IS_CHERRYVIEW(dev)) {
5974 chv_update_pll(crtc, &pipe_config);
5975 chv_prepare_pll(crtc, &pipe_config);
5976 chv_enable_pll(crtc, &pipe_config);
5977 } else {
5978 vlv_update_pll(crtc, &pipe_config);
5979 vlv_prepare_pll(crtc, &pipe_config);
5980 vlv_enable_pll(crtc, &pipe_config);
5981 }
5982}
5983
5984/**
5985 * vlv_force_pll_off - forcibly disable just the PLL
5986 * @dev_priv: i915 private structure
5987 * @pipe: pipe PLL to disable
5988 *
5989 * Disable the PLL for @pipe. To be used in cases where we need
5990 * the PLL enabled even when @pipe is not going to be enabled.
5991 */
5992void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
5993{
5994 if (IS_CHERRYVIEW(dev))
5995 chv_disable_pll(to_i915(dev), pipe);
5996 else
5997 vlv_disable_pll(to_i915(dev), pipe);
5998}
5999
Daniel Vetterf47709a2013-03-28 10:42:02 +01006000static void i9xx_update_pll(struct intel_crtc *crtc,
6001 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006002 int num_connectors)
6003{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006004 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006005 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006006 u32 dpll;
6007 bool is_sdvo;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006008 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006009
Daniel Vetterf47709a2013-03-28 10:42:02 +01006010 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306011
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006012 is_sdvo = intel_pipe_has_type(crtc, INTEL_OUTPUT_SDVO) ||
6013 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006014
6015 dpll = DPLL_VGA_MODE_DIS;
6016
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006017 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006018 dpll |= DPLLB_MODE_LVDS;
6019 else
6020 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01006021
Daniel Vetteref1b4602013-06-01 17:17:04 +02006022 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Daniel Vetter198a037f2013-04-19 11:14:37 +02006023 dpll |= (crtc->config.pixel_multiplier - 1)
6024 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006025 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02006026
6027 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02006028 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02006029
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006030 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006031 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006032
6033 /* compute bitmask from p1 value */
6034 if (IS_PINEVIEW(dev))
6035 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
6036 else {
6037 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6038 if (IS_G4X(dev) && reduced_clock)
6039 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
6040 }
6041 switch (clock->p2) {
6042 case 5:
6043 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
6044 break;
6045 case 7:
6046 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
6047 break;
6048 case 10:
6049 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
6050 break;
6051 case 14:
6052 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
6053 break;
6054 }
6055 if (INTEL_INFO(dev)->gen >= 4)
6056 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
6057
Daniel Vetter09ede542013-04-30 14:01:45 +02006058 if (crtc->config.sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006059 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006060 else if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006061 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6062 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6063 else
6064 dpll |= PLL_REF_INPUT_DREFCLK;
6065
6066 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006067 crtc->config.dpll_hw_state.dpll = dpll;
6068
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006069 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetteref1b4602013-06-01 17:17:04 +02006070 u32 dpll_md = (crtc->config.pixel_multiplier - 1)
6071 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006072 crtc->config.dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006073 }
6074}
6075
Daniel Vetterf47709a2013-03-28 10:42:02 +01006076static void i8xx_update_pll(struct intel_crtc *crtc,
Daniel Vetterf47709a2013-03-28 10:42:02 +01006077 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006078 int num_connectors)
6079{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006080 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006081 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006082 u32 dpll;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006083 struct dpll *clock = &crtc->config.dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006084
Daniel Vetterf47709a2013-03-28 10:42:02 +01006085 i9xx_update_pll_dividers(crtc, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306086
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006087 dpll = DPLL_VGA_MODE_DIS;
6088
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006089 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006090 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6091 } else {
6092 if (clock->p1 == 2)
6093 dpll |= PLL_P1_DIVIDE_BY_TWO;
6094 else
6095 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
6096 if (clock->p2 == 4)
6097 dpll |= PLL_P2_DIVIDE_BY_4;
6098 }
6099
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006100 if (!IS_I830(dev) && intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02006101 dpll |= DPLL_DVO_2X_MODE;
6102
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006103 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006104 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
6105 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
6106 else
6107 dpll |= PLL_REF_INPUT_DREFCLK;
6108
6109 dpll |= DPLL_VCO_ENABLE;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006110 crtc->config.dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006111}
6112
Daniel Vetter8a654f32013-06-01 17:16:22 +02006113static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006114{
6115 struct drm_device *dev = intel_crtc->base.dev;
6116 struct drm_i915_private *dev_priv = dev->dev_private;
6117 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02006118 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02006119 struct drm_display_mode *adjusted_mode =
6120 &intel_crtc->config.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006121 uint32_t crtc_vtotal, crtc_vblank_end;
6122 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006123
6124 /* We need to be careful not to changed the adjusted mode, for otherwise
6125 * the hw state checker will get angry at the mismatch. */
6126 crtc_vtotal = adjusted_mode->crtc_vtotal;
6127 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006128
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006129 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006130 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006131 crtc_vtotal -= 1;
6132 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006133
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006134 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02006135 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
6136 else
6137 vsyncshift = adjusted_mode->crtc_hsync_start -
6138 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02006139 if (vsyncshift < 0)
6140 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006141 }
6142
6143 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006144 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006145
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006146 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006147 (adjusted_mode->crtc_hdisplay - 1) |
6148 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006149 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006150 (adjusted_mode->crtc_hblank_start - 1) |
6151 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006152 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006153 (adjusted_mode->crtc_hsync_start - 1) |
6154 ((adjusted_mode->crtc_hsync_end - 1) << 16));
6155
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006156 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006157 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006158 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006159 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006160 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02006161 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02006162 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006163 (adjusted_mode->crtc_vsync_start - 1) |
6164 ((adjusted_mode->crtc_vsync_end - 1) << 16));
6165
Paulo Zanonib5e508d2012-10-24 11:34:43 -02006166 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
6167 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
6168 * documented on the DDI_FUNC_CTL register description, EDP Input Select
6169 * bits. */
6170 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
6171 (pipe == PIPE_B || pipe == PIPE_C))
6172 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
6173
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006174 /* pipesrc controls the size that is scaled from, which should
6175 * always be the user's requested size.
6176 */
6177 I915_WRITE(PIPESRC(pipe),
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006178 ((intel_crtc->config.pipe_src_w - 1) << 16) |
6179 (intel_crtc->config.pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03006180}
6181
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006182static void intel_get_pipe_timings(struct intel_crtc *crtc,
6183 struct intel_crtc_config *pipe_config)
6184{
6185 struct drm_device *dev = crtc->base.dev;
6186 struct drm_i915_private *dev_priv = dev->dev_private;
6187 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6188 uint32_t tmp;
6189
6190 tmp = I915_READ(HTOTAL(cpu_transcoder));
6191 pipe_config->adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
6192 pipe_config->adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
6193 tmp = I915_READ(HBLANK(cpu_transcoder));
6194 pipe_config->adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
6195 pipe_config->adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
6196 tmp = I915_READ(HSYNC(cpu_transcoder));
6197 pipe_config->adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
6198 pipe_config->adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
6199
6200 tmp = I915_READ(VTOTAL(cpu_transcoder));
6201 pipe_config->adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
6202 pipe_config->adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
6203 tmp = I915_READ(VBLANK(cpu_transcoder));
6204 pipe_config->adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
6205 pipe_config->adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
6206 tmp = I915_READ(VSYNC(cpu_transcoder));
6207 pipe_config->adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
6208 pipe_config->adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
6209
6210 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
6211 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
6212 pipe_config->adjusted_mode.crtc_vtotal += 1;
6213 pipe_config->adjusted_mode.crtc_vblank_end += 1;
6214 }
6215
6216 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03006217 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
6218 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
6219
6220 pipe_config->requested_mode.vdisplay = pipe_config->pipe_src_h;
6221 pipe_config->requested_mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006222}
6223
Daniel Vetterf6a83282014-02-11 15:28:57 -08006224void intel_mode_from_pipe_config(struct drm_display_mode *mode,
6225 struct intel_crtc_config *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03006226{
Daniel Vetterf6a83282014-02-11 15:28:57 -08006227 mode->hdisplay = pipe_config->adjusted_mode.crtc_hdisplay;
6228 mode->htotal = pipe_config->adjusted_mode.crtc_htotal;
6229 mode->hsync_start = pipe_config->adjusted_mode.crtc_hsync_start;
6230 mode->hsync_end = pipe_config->adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006231
Daniel Vetterf6a83282014-02-11 15:28:57 -08006232 mode->vdisplay = pipe_config->adjusted_mode.crtc_vdisplay;
6233 mode->vtotal = pipe_config->adjusted_mode.crtc_vtotal;
6234 mode->vsync_start = pipe_config->adjusted_mode.crtc_vsync_start;
6235 mode->vsync_end = pipe_config->adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03006236
Daniel Vetterf6a83282014-02-11 15:28:57 -08006237 mode->flags = pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006238
Daniel Vetterf6a83282014-02-11 15:28:57 -08006239 mode->clock = pipe_config->adjusted_mode.crtc_clock;
6240 mode->flags |= pipe_config->adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03006241}
6242
Daniel Vetter84b046f2013-02-19 18:48:54 +01006243static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
6244{
6245 struct drm_device *dev = intel_crtc->base.dev;
6246 struct drm_i915_private *dev_priv = dev->dev_private;
6247 uint32_t pipeconf;
6248
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006249 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006250
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03006251 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
6252 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
6253 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02006254
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006255 if (intel_crtc->config.double_wide)
6256 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01006257
Daniel Vetterff9ce462013-04-24 14:57:17 +02006258 /* only g4x and later have fancy bpc/dither controls */
6259 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02006260 /* Bspec claims that we can't use dithering for 30bpp pipes. */
6261 if (intel_crtc->config.dither && intel_crtc->config.pipe_bpp != 30)
6262 pipeconf |= PIPECONF_DITHER_EN |
6263 PIPECONF_DITHER_TYPE_SP;
6264
6265 switch (intel_crtc->config.pipe_bpp) {
6266 case 18:
6267 pipeconf |= PIPECONF_6BPC;
6268 break;
6269 case 24:
6270 pipeconf |= PIPECONF_8BPC;
6271 break;
6272 case 30:
6273 pipeconf |= PIPECONF_10BPC;
6274 break;
6275 default:
6276 /* Case prevented by intel_choose_pipe_bpp_dither. */
6277 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01006278 }
6279 }
6280
6281 if (HAS_PIPE_CXSR(dev)) {
6282 if (intel_crtc->lowfreq_avail) {
6283 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
6284 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
6285 } else {
6286 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01006287 }
6288 }
6289
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006290 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
6291 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006292 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02006293 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
6294 else
6295 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
6296 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01006297 pipeconf |= PIPECONF_PROGRESSIVE;
6298
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02006299 if (IS_VALLEYVIEW(dev) && intel_crtc->config.limited_color_range)
6300 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03006301
Daniel Vetter84b046f2013-02-19 18:48:54 +01006302 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
6303 POSTING_READ(PIPECONF(intel_crtc->pipe));
6304}
6305
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006306static int i9xx_crtc_mode_set(struct intel_crtc *crtc,
Eric Anholtf564048e2011-03-30 13:01:02 -07006307 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02006308 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08006309{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006310 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08006311 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07006312 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07006313 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02006314 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006315 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01006316 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08006317 const intel_limit_t *limit;
Jesse Barnes79e53942008-11-07 14:24:08 -08006318
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006319 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
Chris Wilson5eddb702010-09-11 13:48:45 +01006320 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08006321 case INTEL_OUTPUT_LVDS:
6322 is_lvds = true;
6323 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006324 case INTEL_OUTPUT_DSI:
6325 is_dsi = true;
6326 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006327 default:
6328 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08006329 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05006330
Eric Anholtc751ce42010-03-25 11:48:48 -07006331 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08006332 }
6333
Jani Nikulaf2335332013-09-13 11:03:09 +03006334 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02006335 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006336
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006337 if (!crtc->config.clock_set) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006338 refclk = i9xx_get_refclk(crtc, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03006339
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006340 /*
6341 * Returns a set of divisors for the desired target clock with
6342 * the given refclk, or FALSE. The returned values represent
6343 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
6344 * 2) / p1 / p2.
6345 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006346 limit = intel_limit(crtc, refclk);
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006347 ok = dev_priv->display.find_dpll(limit, crtc,
6348 crtc->config.port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006349 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03006350 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006351 DRM_ERROR("Couldn't find PLL settings for mode!\n");
6352 return -EINVAL;
6353 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006354
Jani Nikulaf2335332013-09-13 11:03:09 +03006355 if (is_lvds && dev_priv->lvds_downclock_avail) {
6356 /*
6357 * Ensure we match the reduced clock's P to the target
6358 * clock. If the clocks don't match, we can't switch
6359 * the display clock by using the FP0/FP1. In such case
6360 * we will disable the LVDS downclock feature.
6361 */
6362 has_reduced_clock =
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006363 dev_priv->display.find_dpll(limit, crtc,
Jani Nikulaf2335332013-09-13 11:03:09 +03006364 dev_priv->lvds_downclock,
6365 refclk, &clock,
6366 &reduced_clock);
6367 }
6368 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006369 crtc->config.dpll.n = clock.n;
6370 crtc->config.dpll.m1 = clock.m1;
6371 crtc->config.dpll.m2 = clock.m2;
6372 crtc->config.dpll.p1 = clock.p1;
6373 crtc->config.dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006374 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006375
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006376 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006377 i8xx_update_pll(crtc,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05306378 has_reduced_clock ? &reduced_clock : NULL,
6379 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006380 } else if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläd288f652014-10-28 13:20:22 +02006381 chv_update_pll(crtc, &crtc->config);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006382 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläd288f652014-10-28 13:20:22 +02006383 vlv_update_pll(crtc, &crtc->config);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006384 } else {
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03006385 i9xx_update_pll(crtc,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02006386 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02006387 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03006388 }
Eric Anholtf564048e2011-03-30 13:01:02 -07006389
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02006390 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07006391}
6392
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006393static void i9xx_get_pfit_config(struct intel_crtc *crtc,
6394 struct intel_crtc_config *pipe_config)
6395{
6396 struct drm_device *dev = crtc->base.dev;
6397 struct drm_i915_private *dev_priv = dev->dev_private;
6398 uint32_t tmp;
6399
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02006400 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
6401 return;
6402
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006403 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02006404 if (!(tmp & PFIT_ENABLE))
6405 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006406
Daniel Vetter06922822013-07-11 13:35:40 +02006407 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006408 if (INTEL_INFO(dev)->gen < 4) {
6409 if (crtc->pipe != PIPE_B)
6410 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006411 } else {
6412 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
6413 return;
6414 }
6415
Daniel Vetter06922822013-07-11 13:35:40 +02006416 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006417 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
6418 if (INTEL_INFO(dev)->gen < 5)
6419 pipe_config->gmch_pfit.lvds_border_bits =
6420 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
6421}
6422
Jesse Barnesacbec812013-09-20 11:29:32 -07006423static void vlv_crtc_clock_get(struct intel_crtc *crtc,
6424 struct intel_crtc_config *pipe_config)
6425{
6426 struct drm_device *dev = crtc->base.dev;
6427 struct drm_i915_private *dev_priv = dev->dev_private;
6428 int pipe = pipe_config->cpu_transcoder;
6429 intel_clock_t clock;
6430 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07006431 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07006432
Shobhit Kumarf573de52014-07-30 20:32:37 +05306433 /* In case of MIPI DPLL will not even be used */
6434 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
6435 return;
6436
Jesse Barnesacbec812013-09-20 11:29:32 -07006437 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006438 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07006439 mutex_unlock(&dev_priv->dpio_lock);
6440
6441 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
6442 clock.m2 = mdiv & DPIO_M2DIV_MASK;
6443 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
6444 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
6445 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
6446
Ville Syrjäläf6466282013-10-14 14:50:31 +03006447 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07006448
Ville Syrjäläf6466282013-10-14 14:50:31 +03006449 /* clock.dot is the fast clock */
6450 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07006451}
6452
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006453static void i9xx_get_plane_config(struct intel_crtc *crtc,
6454 struct intel_plane_config *plane_config)
6455{
6456 struct drm_device *dev = crtc->base.dev;
6457 struct drm_i915_private *dev_priv = dev->dev_private;
6458 u32 val, base, offset;
6459 int pipe = crtc->pipe, plane = crtc->plane;
6460 int fourcc, pixel_format;
6461 int aligned_height;
6462
Dave Airlie66e514c2014-04-03 07:51:54 +10006463 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
6464 if (!crtc->base.primary->fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006465 DRM_DEBUG_KMS("failed to alloc fb\n");
6466 return;
6467 }
6468
6469 val = I915_READ(DSPCNTR(plane));
6470
6471 if (INTEL_INFO(dev)->gen >= 4)
6472 if (val & DISPPLANE_TILED)
6473 plane_config->tiled = true;
6474
6475 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
6476 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10006477 crtc->base.primary->fb->pixel_format = fourcc;
6478 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006479 drm_format_plane_cpp(fourcc, 0) * 8;
6480
6481 if (INTEL_INFO(dev)->gen >= 4) {
6482 if (plane_config->tiled)
6483 offset = I915_READ(DSPTILEOFF(plane));
6484 else
6485 offset = I915_READ(DSPLINOFF(plane));
6486 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
6487 } else {
6488 base = I915_READ(DSPADDR(plane));
6489 }
6490 plane_config->base = base;
6491
6492 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10006493 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
6494 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006495
6496 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01006497 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006498
Dave Airlie66e514c2014-04-03 07:51:54 +10006499 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006500 plane_config->tiled);
6501
Fabian Frederick1267a262014-07-01 20:39:41 +02006502 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
6503 aligned_height);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006504
6505 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 +10006506 pipe, plane, crtc->base.primary->fb->width,
6507 crtc->base.primary->fb->height,
6508 crtc->base.primary->fb->bits_per_pixel, base,
6509 crtc->base.primary->fb->pitches[0],
Jesse Barnes1ad292b2014-03-07 08:57:49 -08006510 plane_config->size);
6511
6512}
6513
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006514static void chv_crtc_clock_get(struct intel_crtc *crtc,
6515 struct intel_crtc_config *pipe_config)
6516{
6517 struct drm_device *dev = crtc->base.dev;
6518 struct drm_i915_private *dev_priv = dev->dev_private;
6519 int pipe = pipe_config->cpu_transcoder;
6520 enum dpio_channel port = vlv_pipe_to_channel(pipe);
6521 intel_clock_t clock;
6522 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
6523 int refclk = 100000;
6524
6525 mutex_lock(&dev_priv->dpio_lock);
6526 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
6527 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
6528 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
6529 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
6530 mutex_unlock(&dev_priv->dpio_lock);
6531
6532 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
6533 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
6534 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
6535 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
6536 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
6537
6538 chv_clock(refclk, &clock);
6539
6540 /* clock.dot is the fast clock */
6541 pipe_config->port_clock = clock.dot / 5;
6542}
6543
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006544static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
6545 struct intel_crtc_config *pipe_config)
6546{
6547 struct drm_device *dev = crtc->base.dev;
6548 struct drm_i915_private *dev_priv = dev->dev_private;
6549 uint32_t tmp;
6550
Daniel Vetterf458ebb2014-09-30 10:56:39 +02006551 if (!intel_display_power_is_enabled(dev_priv,
6552 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02006553 return false;
6554
Daniel Vettere143a212013-07-04 12:01:15 +02006555 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02006556 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02006557
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006558 tmp = I915_READ(PIPECONF(crtc->pipe));
6559 if (!(tmp & PIPECONF_ENABLE))
6560 return false;
6561
Ville Syrjälä42571ae2013-09-06 23:29:00 +03006562 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
6563 switch (tmp & PIPECONF_BPC_MASK) {
6564 case PIPECONF_6BPC:
6565 pipe_config->pipe_bpp = 18;
6566 break;
6567 case PIPECONF_8BPC:
6568 pipe_config->pipe_bpp = 24;
6569 break;
6570 case PIPECONF_10BPC:
6571 pipe_config->pipe_bpp = 30;
6572 break;
6573 default:
6574 break;
6575 }
6576 }
6577
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02006578 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
6579 pipe_config->limited_color_range = true;
6580
Ville Syrjälä282740f2013-09-04 18:30:03 +03006581 if (INTEL_INFO(dev)->gen < 4)
6582 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
6583
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02006584 intel_get_pipe_timings(crtc, pipe_config);
6585
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02006586 i9xx_get_pfit_config(crtc, pipe_config);
6587
Daniel Vetter6c49f242013-06-06 12:45:25 +02006588 if (INTEL_INFO(dev)->gen >= 4) {
6589 tmp = I915_READ(DPLL_MD(crtc->pipe));
6590 pipe_config->pixel_multiplier =
6591 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
6592 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006593 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02006594 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
6595 tmp = I915_READ(DPLL(crtc->pipe));
6596 pipe_config->pixel_multiplier =
6597 ((tmp & SDVO_MULTIPLIER_MASK)
6598 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
6599 } else {
6600 /* Note that on i915G/GM the pixel multiplier is in the sdvo
6601 * port and will be fixed up in the encoder->get_config
6602 * function. */
6603 pipe_config->pixel_multiplier = 1;
6604 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006605 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
6606 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006607 /*
6608 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
6609 * on 830. Filter it out here so that we don't
6610 * report errors due to that.
6611 */
6612 if (IS_I830(dev))
6613 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
6614
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006615 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
6616 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03006617 } else {
6618 /* Mask out read-only status bits. */
6619 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
6620 DPLL_PORTC_READY_MASK |
6621 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02006622 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02006623
Ville Syrjälä70b23a92014-04-09 13:28:22 +03006624 if (IS_CHERRYVIEW(dev))
6625 chv_crtc_clock_get(crtc, pipe_config);
6626 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07006627 vlv_crtc_clock_get(crtc, pipe_config);
6628 else
6629 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03006630
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01006631 return true;
6632}
6633
Paulo Zanonidde86e22012-12-01 12:04:25 -02006634static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07006635{
6636 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006637 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006638 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006639 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006640 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07006641 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07006642 bool has_ck505 = false;
6643 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006644
6645 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01006646 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07006647 switch (encoder->type) {
6648 case INTEL_OUTPUT_LVDS:
6649 has_panel = true;
6650 has_lvds = true;
6651 break;
6652 case INTEL_OUTPUT_EDP:
6653 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03006654 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07006655 has_cpu_edp = true;
6656 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006657 default:
6658 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006659 }
6660 }
6661
Keith Packard99eb6a02011-09-26 14:29:12 -07006662 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006663 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07006664 can_ssc = has_ck505;
6665 } else {
6666 has_ck505 = false;
6667 can_ssc = true;
6668 }
6669
Imre Deak2de69052013-05-08 13:14:04 +03006670 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
6671 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006672
6673 /* Ironlake: try to setup display ref clock before DPLL
6674 * enabling. This is only under driver's control after
6675 * PCH B stepping, previous chipset stepping should be
6676 * ignoring this setting.
6677 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006678 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006679
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006680 /* As we must carefully and slowly disable/enable each source in turn,
6681 * compute the final state we want first and check if we need to
6682 * make any changes at all.
6683 */
6684 final = val;
6685 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07006686 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006687 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07006688 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006689 final |= DREF_NONSPREAD_SOURCE_ENABLE;
6690
6691 final &= ~DREF_SSC_SOURCE_MASK;
6692 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
6693 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006694
Keith Packard199e5d72011-09-22 12:01:57 -07006695 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006696 final |= DREF_SSC_SOURCE_ENABLE;
6697
6698 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6699 final |= DREF_SSC1_ENABLE;
6700
6701 if (has_cpu_edp) {
6702 if (intel_panel_use_ssc(dev_priv) && can_ssc)
6703 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
6704 else
6705 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
6706 } else
6707 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6708 } else {
6709 final |= DREF_SSC_SOURCE_DISABLE;
6710 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
6711 }
6712
6713 if (final == val)
6714 return;
6715
6716 /* Always enable nonspread source */
6717 val &= ~DREF_NONSPREAD_SOURCE_MASK;
6718
6719 if (has_ck505)
6720 val |= DREF_NONSPREAD_CK505_ENABLE;
6721 else
6722 val |= DREF_NONSPREAD_SOURCE_ENABLE;
6723
6724 if (has_panel) {
6725 val &= ~DREF_SSC_SOURCE_MASK;
6726 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006727
Keith Packard199e5d72011-09-22 12:01:57 -07006728 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07006729 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006730 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006731 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02006732 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006733 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006734
6735 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006736 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006737 POSTING_READ(PCH_DREF_CONTROL);
6738 udelay(200);
6739
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006740 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07006741
6742 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07006743 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07006744 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07006745 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006746 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02006747 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006748 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07006749 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006750 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006751
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006752 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006753 POSTING_READ(PCH_DREF_CONTROL);
6754 udelay(200);
6755 } else {
6756 DRM_DEBUG_KMS("Disabling SSC entirely\n");
6757
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006758 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07006759
6760 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006761 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006762
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006763 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07006764 POSTING_READ(PCH_DREF_CONTROL);
6765 udelay(200);
6766
6767 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006768 val &= ~DREF_SSC_SOURCE_MASK;
6769 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006770
6771 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006772 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07006773
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006774 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006775 POSTING_READ(PCH_DREF_CONTROL);
6776 udelay(200);
6777 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07006778
6779 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07006780}
6781
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006782static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02006783{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006784 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006785
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006786 tmp = I915_READ(SOUTH_CHICKEN2);
6787 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
6788 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006789
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006790 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
6791 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
6792 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02006793
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006794 tmp = I915_READ(SOUTH_CHICKEN2);
6795 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
6796 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006797
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006798 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
6799 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
6800 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006801}
6802
6803/* WaMPhyProgramming:hsw */
6804static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
6805{
6806 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02006807
6808 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
6809 tmp &= ~(0xFF << 24);
6810 tmp |= (0x12 << 24);
6811 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
6812
Paulo Zanonidde86e22012-12-01 12:04:25 -02006813 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
6814 tmp |= (1 << 11);
6815 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
6816
6817 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
6818 tmp |= (1 << 11);
6819 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
6820
Paulo Zanonidde86e22012-12-01 12:04:25 -02006821 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
6822 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6823 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
6824
6825 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
6826 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
6827 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
6828
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006829 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
6830 tmp &= ~(7 << 13);
6831 tmp |= (5 << 13);
6832 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006833
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006834 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
6835 tmp &= ~(7 << 13);
6836 tmp |= (5 << 13);
6837 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006838
6839 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
6840 tmp &= ~0xFF;
6841 tmp |= 0x1C;
6842 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
6843
6844 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
6845 tmp &= ~0xFF;
6846 tmp |= 0x1C;
6847 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
6848
6849 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
6850 tmp &= ~(0xFF << 16);
6851 tmp |= (0x1C << 16);
6852 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
6853
6854 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
6855 tmp &= ~(0xFF << 16);
6856 tmp |= (0x1C << 16);
6857 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
6858
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006859 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
6860 tmp |= (1 << 27);
6861 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006862
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006863 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
6864 tmp |= (1 << 27);
6865 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006866
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006867 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
6868 tmp &= ~(0xF << 28);
6869 tmp |= (4 << 28);
6870 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006871
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03006872 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
6873 tmp &= ~(0xF << 28);
6874 tmp |= (4 << 28);
6875 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006876}
6877
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006878/* Implements 3 different sequences from BSpec chapter "Display iCLK
6879 * Programming" based on the parameters passed:
6880 * - Sequence to enable CLKOUT_DP
6881 * - Sequence to enable CLKOUT_DP without spread
6882 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
6883 */
6884static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
6885 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006886{
6887 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006888 uint32_t reg, tmp;
6889
6890 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
6891 with_spread = true;
6892 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
6893 with_fdi, "LP PCH doesn't have FDI\n"))
6894 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006895
6896 mutex_lock(&dev_priv->dpio_lock);
6897
6898 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6899 tmp &= ~SBI_SSCCTL_DISABLE;
6900 tmp |= SBI_SSCCTL_PATHALT;
6901 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6902
6903 udelay(24);
6904
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006905 if (with_spread) {
6906 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6907 tmp &= ~SBI_SSCCTL_PATHALT;
6908 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03006909
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006910 if (with_fdi) {
6911 lpt_reset_fdi_mphy(dev_priv);
6912 lpt_program_fdi_mphy(dev_priv);
6913 }
6914 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02006915
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03006916 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6917 SBI_GEN0 : SBI_DBUFF0;
6918 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6919 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6920 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01006921
6922 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02006923}
6924
Paulo Zanoni47701c32013-07-23 11:19:25 -03006925/* Sequence to disable CLKOUT_DP */
6926static void lpt_disable_clkout_dp(struct drm_device *dev)
6927{
6928 struct drm_i915_private *dev_priv = dev->dev_private;
6929 uint32_t reg, tmp;
6930
6931 mutex_lock(&dev_priv->dpio_lock);
6932
6933 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
6934 SBI_GEN0 : SBI_DBUFF0;
6935 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
6936 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
6937 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
6938
6939 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
6940 if (!(tmp & SBI_SSCCTL_DISABLE)) {
6941 if (!(tmp & SBI_SSCCTL_PATHALT)) {
6942 tmp |= SBI_SSCCTL_PATHALT;
6943 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6944 udelay(32);
6945 }
6946 tmp |= SBI_SSCCTL_DISABLE;
6947 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
6948 }
6949
6950 mutex_unlock(&dev_priv->dpio_lock);
6951}
6952
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006953static void lpt_init_pch_refclk(struct drm_device *dev)
6954{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006955 struct intel_encoder *encoder;
6956 bool has_vga = false;
6957
Damien Lespiaub2784e12014-08-05 11:29:37 +01006958 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006959 switch (encoder->type) {
6960 case INTEL_OUTPUT_ANALOG:
6961 has_vga = true;
6962 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006963 default:
6964 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006965 }
6966 }
6967
Paulo Zanoni47701c32013-07-23 11:19:25 -03006968 if (has_vga)
6969 lpt_enable_clkout_dp(dev, true, true);
6970 else
6971 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03006972}
6973
Paulo Zanonidde86e22012-12-01 12:04:25 -02006974/*
6975 * Initialize reference clocks when the driver loads
6976 */
6977void intel_init_pch_refclk(struct drm_device *dev)
6978{
6979 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
6980 ironlake_init_pch_refclk(dev);
6981 else if (HAS_PCH_LPT(dev))
6982 lpt_init_pch_refclk(dev);
6983}
6984
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006985static int ironlake_get_refclk(struct drm_crtc *crtc)
6986{
6987 struct drm_device *dev = crtc->dev;
6988 struct drm_i915_private *dev_priv = dev->dev_private;
6989 struct intel_encoder *encoder;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006990 int num_connectors = 0;
6991 bool is_lvds = false;
6992
Daniel Vetter6c2b7c12012-07-05 09:50:24 +02006993 for_each_encoder_on_crtc(dev, crtc, encoder) {
Jesse Barnesd9d444c2011-09-02 13:03:05 -07006994 switch (encoder->type) {
6995 case INTEL_OUTPUT_LVDS:
6996 is_lvds = true;
6997 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02006998 default:
6999 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007000 }
7001 num_connectors++;
7002 }
7003
7004 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007005 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007006 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007007 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07007008 }
7009
7010 return 120000;
7011}
7012
Daniel Vetter6ff93602013-04-19 11:24:36 +02007013static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03007014{
7015 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
7016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7017 int pipe = intel_crtc->pipe;
7018 uint32_t val;
7019
Daniel Vetter78114072013-06-13 00:54:57 +02007020 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03007021
Daniel Vetter965e0c42013-03-27 00:44:57 +01007022 switch (intel_crtc->config.pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03007023 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007024 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007025 break;
7026 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007027 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007028 break;
7029 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007030 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007031 break;
7032 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01007033 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03007034 break;
7035 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03007036 /* Case prevented by intel_choose_pipe_bpp_dither. */
7037 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03007038 }
7039
Daniel Vetterd8b32242013-04-25 17:54:44 +02007040 if (intel_crtc->config.dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03007041 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7042
Daniel Vetter6ff93602013-04-19 11:24:36 +02007043 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03007044 val |= PIPECONF_INTERLACED_ILK;
7045 else
7046 val |= PIPECONF_PROGRESSIVE;
7047
Daniel Vetter50f3b012013-03-27 00:44:56 +01007048 if (intel_crtc->config.limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007049 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02007050
Paulo Zanonic8203562012-09-12 10:06:29 -03007051 I915_WRITE(PIPECONF(pipe), val);
7052 POSTING_READ(PIPECONF(pipe));
7053}
7054
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007055/*
7056 * Set up the pipe CSC unit.
7057 *
7058 * Currently only full range RGB to limited range RGB conversion
7059 * is supported, but eventually this should handle various
7060 * RGB<->YCbCr scenarios as well.
7061 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01007062static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007063{
7064 struct drm_device *dev = crtc->dev;
7065 struct drm_i915_private *dev_priv = dev->dev_private;
7066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
7067 int pipe = intel_crtc->pipe;
7068 uint16_t coeff = 0x7800; /* 1.0 */
7069
7070 /*
7071 * TODO: Check what kind of values actually come out of the pipe
7072 * with these coeff/postoff values and adjust to get the best
7073 * accuracy. Perhaps we even need to take the bpc value into
7074 * consideration.
7075 */
7076
Daniel Vetter50f3b012013-03-27 00:44:56 +01007077 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007078 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
7079
7080 /*
7081 * GY/GU and RY/RU should be the other way around according
7082 * to BSpec, but reality doesn't agree. Just set them up in
7083 * a way that results in the correct picture.
7084 */
7085 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
7086 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
7087
7088 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
7089 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
7090
7091 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
7092 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
7093
7094 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
7095 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
7096 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
7097
7098 if (INTEL_INFO(dev)->gen > 6) {
7099 uint16_t postoff = 0;
7100
Daniel Vetter50f3b012013-03-27 00:44:56 +01007101 if (intel_crtc->config.limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02007102 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007103
7104 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
7105 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
7106 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
7107
7108 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
7109 } else {
7110 uint32_t mode = CSC_MODE_YUV_TO_RGB;
7111
Daniel Vetter50f3b012013-03-27 00:44:56 +01007112 if (intel_crtc->config.limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02007113 mode |= CSC_BLACK_SCREEN_OFFSET;
7114
7115 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
7116 }
7117}
7118
Daniel Vetter6ff93602013-04-19 11:24:36 +02007119static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007120{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007121 struct drm_device *dev = crtc->dev;
7122 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007124 enum pipe pipe = intel_crtc->pipe;
Daniel Vetter3b117c82013-04-17 20:15:07 +02007125 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007126 uint32_t val;
7127
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007128 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007129
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007130 if (IS_HASWELL(dev) && intel_crtc->config.dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007131 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
7132
Daniel Vetter6ff93602013-04-19 11:24:36 +02007133 if (intel_crtc->config.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007134 val |= PIPECONF_INTERLACED_ILK;
7135 else
7136 val |= PIPECONF_PROGRESSIVE;
7137
Paulo Zanoni702e7a52012-10-23 18:29:59 -02007138 I915_WRITE(PIPECONF(cpu_transcoder), val);
7139 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02007140
7141 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
7142 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007143
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05307144 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07007145 val = 0;
7146
7147 switch (intel_crtc->config.pipe_bpp) {
7148 case 18:
7149 val |= PIPEMISC_DITHER_6_BPC;
7150 break;
7151 case 24:
7152 val |= PIPEMISC_DITHER_8_BPC;
7153 break;
7154 case 30:
7155 val |= PIPEMISC_DITHER_10_BPC;
7156 break;
7157 case 36:
7158 val |= PIPEMISC_DITHER_12_BPC;
7159 break;
7160 default:
7161 /* Case prevented by pipe_config_set_bpp. */
7162 BUG();
7163 }
7164
7165 if (intel_crtc->config.dither)
7166 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
7167
7168 I915_WRITE(PIPEMISC(pipe), val);
7169 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03007170}
7171
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007172static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007173 intel_clock_t *clock,
7174 bool *has_reduced_clock,
7175 intel_clock_t *reduced_clock)
7176{
7177 struct drm_device *dev = crtc->dev;
7178 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007180 int refclk;
7181 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02007182 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007183
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007184 is_lvds = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007185
7186 refclk = ironlake_get_refclk(crtc);
7187
7188 /*
7189 * Returns a set of divisors for the desired target clock with the given
7190 * refclk, or FALSE. The returned values represent the clock equation:
7191 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
7192 */
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007193 limit = intel_limit(intel_crtc, refclk);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007194 ret = dev_priv->display.find_dpll(limit, intel_crtc,
7195 intel_crtc->config.port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007196 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007197 if (!ret)
7198 return false;
7199
7200 if (is_lvds && dev_priv->lvds_downclock_avail) {
7201 /*
7202 * Ensure we match the reduced clock's P to the target clock.
7203 * If the clocks don't match, we can't switch the display clock
7204 * by using the FP0/FP1. In such case we will disable the LVDS
7205 * downclock feature.
7206 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02007207 *has_reduced_clock =
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +03007208 dev_priv->display.find_dpll(limit, intel_crtc,
Daniel Vetteree9300b2013-06-03 22:40:22 +02007209 dev_priv->lvds_downclock,
7210 refclk, clock,
7211 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007212 }
7213
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007214 return true;
7215}
7216
Paulo Zanonid4b19312012-11-29 11:29:32 -02007217int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
7218{
7219 /*
7220 * Account for spread spectrum to avoid
7221 * oversubscribing the link. Max center spread
7222 * is 2.5%; use 5% for safety's sake.
7223 */
7224 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02007225 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02007226}
7227
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007228static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02007229{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007230 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03007231}
7232
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007233static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007234 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007235 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007236{
7237 struct drm_crtc *crtc = &intel_crtc->base;
7238 struct drm_device *dev = crtc->dev;
7239 struct drm_i915_private *dev_priv = dev->dev_private;
7240 struct intel_encoder *intel_encoder;
7241 uint32_t dpll;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007242 int factor, num_connectors = 0;
Daniel Vetter09ede542013-04-30 14:01:45 +02007243 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007244
7245 for_each_encoder_on_crtc(dev, crtc, intel_encoder) {
7246 switch (intel_encoder->type) {
7247 case INTEL_OUTPUT_LVDS:
7248 is_lvds = true;
7249 break;
7250 case INTEL_OUTPUT_SDVO:
7251 case INTEL_OUTPUT_HDMI:
7252 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007253 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007254 default:
7255 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007256 }
7257
7258 num_connectors++;
7259 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007260
Chris Wilsonc1858122010-12-03 21:35:48 +00007261 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07007262 factor = 21;
7263 if (is_lvds) {
7264 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007265 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02007266 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07007267 factor = 25;
Daniel Vetter09ede542013-04-30 14:01:45 +02007268 } else if (intel_crtc->config.sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07007269 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00007270
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007271 if (ironlake_needs_fb_cb_tune(&intel_crtc->config.dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02007272 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00007273
Daniel Vetter9a7c7892013-04-04 22:20:34 +02007274 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
7275 *fp2 |= FP_CB_TUNE;
7276
Chris Wilson5eddb702010-09-11 13:48:45 +01007277 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08007278
Eric Anholta07d6782011-03-30 13:01:08 -07007279 if (is_lvds)
7280 dpll |= DPLLB_MODE_LVDS;
7281 else
7282 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007283
Daniel Vetteref1b4602013-06-01 17:17:04 +02007284 dpll |= (intel_crtc->config.pixel_multiplier - 1)
7285 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007286
7287 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007288 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter9566e9a2013-04-19 11:14:36 +02007289 if (intel_crtc->config.has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007290 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08007291
Eric Anholta07d6782011-03-30 13:01:08 -07007292 /* compute bitmask from p1 value */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007293 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007294 /* also FPA1 */
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007295 dpll |= (1 << (intel_crtc->config.dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07007296
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007297 switch (intel_crtc->config.dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07007298 case 5:
7299 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7300 break;
7301 case 7:
7302 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7303 break;
7304 case 10:
7305 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7306 break;
7307 case 14:
7308 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7309 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007310 }
7311
Daniel Vetterb4c09f32013-04-30 14:01:42 +02007312 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007313 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08007314 else
7315 dpll |= PLL_REF_INPUT_DREFCLK;
7316
Daniel Vetter959e16d2013-06-05 13:34:21 +02007317 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03007318}
7319
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007320static int ironlake_crtc_mode_set(struct intel_crtc *crtc,
Jesse Barnes79e53942008-11-07 14:24:08 -08007321 int x, int y,
Daniel Vetter94352cf2012-07-05 22:51:56 +02007322 struct drm_framebuffer *fb)
Jesse Barnes79e53942008-11-07 14:24:08 -08007323{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007324 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007325 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007326 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03007327 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01007328 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007329 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08007330
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007331 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08007332
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007333 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
7334 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
7335
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007336 ok = ironlake_compute_clocks(&crtc->base, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03007337 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007338 if (!ok && !crtc->config.clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007339 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7340 return -EINVAL;
7341 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01007342 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007343 if (!crtc->config.clock_set) {
7344 crtc->config.dpll.n = clock.n;
7345 crtc->config.dpll.m1 = clock.m1;
7346 crtc->config.dpll.m2 = clock.m2;
7347 crtc->config.dpll.p1 = clock.p1;
7348 crtc->config.dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007349 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007350
Paulo Zanoni5dc52982012-10-05 12:05:56 -03007351 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007352 if (crtc->config.has_pch_encoder) {
7353 fp = i9xx_dpll_compute_fp(&crtc->config.dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007354 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007355 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007356
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007357 dpll = ironlake_compute_dpll(crtc,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02007358 &fp, &reduced_clock,
7359 has_reduced_clock ? &fp2 : NULL);
7360
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007361 crtc->config.dpll_hw_state.dpll = dpll;
7362 crtc->config.dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007363 if (has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007364 crtc->config.dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007365 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007366 crtc->config.dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02007367
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007368 pll = intel_get_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007369 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03007370 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007371 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07007372 return -EINVAL;
7373 }
Jesse Barnesee7b9f92012-04-20 17:11:53 +01007374 } else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007375 intel_put_shared_dpll(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08007376
Jani Nikulad330a952014-01-21 11:24:25 +02007377 if (is_lvds && has_reduced_clock && i915.powersave)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007378 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02007379 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007380 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02007381
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007382 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007383}
7384
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007385static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
7386 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02007387{
7388 struct drm_device *dev = crtc->base.dev;
7389 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007390 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02007391
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007392 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
7393 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
7394 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
7395 & ~TU_SIZE_MASK;
7396 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
7397 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
7398 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7399}
7400
7401static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
7402 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007403 struct intel_link_m_n *m_n,
7404 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007405{
7406 struct drm_device *dev = crtc->base.dev;
7407 struct drm_i915_private *dev_priv = dev->dev_private;
7408 enum pipe pipe = crtc->pipe;
7409
7410 if (INTEL_INFO(dev)->gen >= 5) {
7411 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
7412 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
7413 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
7414 & ~TU_SIZE_MASK;
7415 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
7416 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
7417 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007418 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
7419 * gen < 8) and if DRRS is supported (to make sure the
7420 * registers are not unnecessarily read).
7421 */
7422 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
7423 crtc->config.has_drrs) {
7424 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
7425 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
7426 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
7427 & ~TU_SIZE_MASK;
7428 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
7429 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
7430 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7431 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007432 } else {
7433 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
7434 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
7435 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
7436 & ~TU_SIZE_MASK;
7437 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
7438 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
7439 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
7440 }
7441}
7442
7443void intel_dp_get_m_n(struct intel_crtc *crtc,
7444 struct intel_crtc_config *pipe_config)
7445{
7446 if (crtc->config.has_pch_encoder)
7447 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
7448 else
7449 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007450 &pipe_config->dp_m_n,
7451 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007452}
7453
Daniel Vetter72419202013-04-04 13:28:53 +02007454static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
7455 struct intel_crtc_config *pipe_config)
7456{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03007457 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07007458 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02007459}
7460
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007461static void ironlake_get_pfit_config(struct intel_crtc *crtc,
7462 struct intel_crtc_config *pipe_config)
7463{
7464 struct drm_device *dev = crtc->base.dev;
7465 struct drm_i915_private *dev_priv = dev->dev_private;
7466 uint32_t tmp;
7467
7468 tmp = I915_READ(PF_CTL(crtc->pipe));
7469
7470 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01007471 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007472 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
7473 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02007474
7475 /* We currently do not free assignements of panel fitters on
7476 * ivb/hsw (since we don't use the higher upscaling modes which
7477 * differentiates them) so just WARN about this case for now. */
7478 if (IS_GEN7(dev)) {
7479 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
7480 PF_PIPE_SEL_IVB(crtc->pipe));
7481 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007482 }
Jesse Barnes79e53942008-11-07 14:24:08 -08007483}
7484
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007485static void ironlake_get_plane_config(struct intel_crtc *crtc,
7486 struct intel_plane_config *plane_config)
7487{
7488 struct drm_device *dev = crtc->base.dev;
7489 struct drm_i915_private *dev_priv = dev->dev_private;
7490 u32 val, base, offset;
7491 int pipe = crtc->pipe, plane = crtc->plane;
7492 int fourcc, pixel_format;
7493 int aligned_height;
7494
Dave Airlie66e514c2014-04-03 07:51:54 +10007495 crtc->base.primary->fb = kzalloc(sizeof(struct intel_framebuffer), GFP_KERNEL);
7496 if (!crtc->base.primary->fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007497 DRM_DEBUG_KMS("failed to alloc fb\n");
7498 return;
7499 }
7500
7501 val = I915_READ(DSPCNTR(plane));
7502
7503 if (INTEL_INFO(dev)->gen >= 4)
7504 if (val & DISPPLANE_TILED)
7505 plane_config->tiled = true;
7506
7507 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
7508 fourcc = intel_format_to_fourcc(pixel_format);
Dave Airlie66e514c2014-04-03 07:51:54 +10007509 crtc->base.primary->fb->pixel_format = fourcc;
7510 crtc->base.primary->fb->bits_per_pixel =
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007511 drm_format_plane_cpp(fourcc, 0) * 8;
7512
7513 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7514 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
7515 offset = I915_READ(DSPOFFSET(plane));
7516 } else {
7517 if (plane_config->tiled)
7518 offset = I915_READ(DSPTILEOFF(plane));
7519 else
7520 offset = I915_READ(DSPLINOFF(plane));
7521 }
7522 plane_config->base = base;
7523
7524 val = I915_READ(PIPESRC(pipe));
Dave Airlie66e514c2014-04-03 07:51:54 +10007525 crtc->base.primary->fb->width = ((val >> 16) & 0xfff) + 1;
7526 crtc->base.primary->fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007527
7528 val = I915_READ(DSPSTRIDE(pipe));
Rafael Barbalho026b96e2014-07-28 19:56:27 +01007529 crtc->base.primary->fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007530
Dave Airlie66e514c2014-04-03 07:51:54 +10007531 aligned_height = intel_align_height(dev, crtc->base.primary->fb->height,
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007532 plane_config->tiled);
7533
Fabian Frederick1267a262014-07-01 20:39:41 +02007534 plane_config->size = PAGE_ALIGN(crtc->base.primary->fb->pitches[0] *
7535 aligned_height);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007536
7537 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 +10007538 pipe, plane, crtc->base.primary->fb->width,
7539 crtc->base.primary->fb->height,
7540 crtc->base.primary->fb->bits_per_pixel, base,
7541 crtc->base.primary->fb->pitches[0],
Jesse Barnes4c6baa52014-03-07 08:57:50 -08007542 plane_config->size);
7543}
7544
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007545static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
7546 struct intel_crtc_config *pipe_config)
7547{
7548 struct drm_device *dev = crtc->base.dev;
7549 struct drm_i915_private *dev_priv = dev->dev_private;
7550 uint32_t tmp;
7551
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007552 if (!intel_display_power_is_enabled(dev_priv,
7553 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03007554 return false;
7555
Daniel Vettere143a212013-07-04 12:01:15 +02007556 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007557 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007558
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007559 tmp = I915_READ(PIPECONF(crtc->pipe));
7560 if (!(tmp & PIPECONF_ENABLE))
7561 return false;
7562
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007563 switch (tmp & PIPECONF_BPC_MASK) {
7564 case PIPECONF_6BPC:
7565 pipe_config->pipe_bpp = 18;
7566 break;
7567 case PIPECONF_8BPC:
7568 pipe_config->pipe_bpp = 24;
7569 break;
7570 case PIPECONF_10BPC:
7571 pipe_config->pipe_bpp = 30;
7572 break;
7573 case PIPECONF_12BPC:
7574 pipe_config->pipe_bpp = 36;
7575 break;
7576 default:
7577 break;
7578 }
7579
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007580 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
7581 pipe_config->limited_color_range = true;
7582
Daniel Vetterab9412b2013-05-03 11:49:46 +02007583 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02007584 struct intel_shared_dpll *pll;
7585
Daniel Vetter88adfff2013-03-28 10:42:01 +01007586 pipe_config->has_pch_encoder = true;
7587
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007588 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
7589 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7590 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02007591
7592 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007593
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007594 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02007595 pipe_config->shared_dpll =
7596 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007597 } else {
7598 tmp = I915_READ(PCH_DPLL_SEL);
7599 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
7600 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
7601 else
7602 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
7603 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02007604
7605 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7606
7607 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7608 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02007609
7610 tmp = pipe_config->dpll_hw_state.dpll;
7611 pipe_config->pixel_multiplier =
7612 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
7613 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03007614
7615 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02007616 } else {
7617 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007618 }
7619
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007620 intel_get_pipe_timings(crtc, pipe_config);
7621
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007622 ironlake_get_pfit_config(crtc, pipe_config);
7623
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007624 return true;
7625}
7626
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007627static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
7628{
7629 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007630 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007631
Damien Lespiaud3fcc802014-05-13 23:32:22 +01007632 for_each_intel_crtc(dev, crtc)
Paulo Zanoni798183c2013-12-06 20:29:01 -02007633 WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007634 pipe_name(crtc->pipe));
7635
7636 WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
Daniel Vetter8cc3e162014-06-25 22:01:46 +03007637 WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
7638 WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
7639 WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007640 WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
7641 WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
7642 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03007643 if (IS_HASWELL(dev))
7644 WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
7645 "CPU PWM2 enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007646 WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
7647 "PCH PWM1 enabled\n");
7648 WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
7649 "Utility pin enabled\n");
7650 WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
7651
Paulo Zanoni9926ada2014-04-01 19:39:47 -03007652 /*
7653 * In theory we can still leave IRQs enabled, as long as only the HPD
7654 * interrupts remain enabled. We used to check for that, but since it's
7655 * gen-specific and since we only disable LCPLL after we fully disable
7656 * the interrupts, the check below should be enough.
7657 */
Jesse Barnes9df7575f2014-06-20 09:29:20 -07007658 WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007659}
7660
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007661static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
7662{
7663 struct drm_device *dev = dev_priv->dev;
7664
7665 if (IS_HASWELL(dev))
7666 return I915_READ(D_COMP_HSW);
7667 else
7668 return I915_READ(D_COMP_BDW);
7669}
7670
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007671static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
7672{
7673 struct drm_device *dev = dev_priv->dev;
7674
7675 if (IS_HASWELL(dev)) {
7676 mutex_lock(&dev_priv->rps.hw_lock);
7677 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
7678 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03007679 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007680 mutex_unlock(&dev_priv->rps.hw_lock);
7681 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007682 I915_WRITE(D_COMP_BDW, val);
7683 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007684 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007685}
7686
7687/*
7688 * This function implements pieces of two sequences from BSpec:
7689 * - Sequence for display software to disable LCPLL
7690 * - Sequence for display software to allow package C8+
7691 * The steps implemented here are just the steps that actually touch the LCPLL
7692 * register. Callers should take care of disabling all the display engine
7693 * functions, doing the mode unset, fixing interrupts, etc.
7694 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007695static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
7696 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007697{
7698 uint32_t val;
7699
7700 assert_can_disable_lcpll(dev_priv);
7701
7702 val = I915_READ(LCPLL_CTL);
7703
7704 if (switch_to_fclk) {
7705 val |= LCPLL_CD_SOURCE_FCLK;
7706 I915_WRITE(LCPLL_CTL, val);
7707
7708 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
7709 LCPLL_CD_SOURCE_FCLK_DONE, 1))
7710 DRM_ERROR("Switching to FCLK failed\n");
7711
7712 val = I915_READ(LCPLL_CTL);
7713 }
7714
7715 val |= LCPLL_PLL_DISABLE;
7716 I915_WRITE(LCPLL_CTL, val);
7717 POSTING_READ(LCPLL_CTL);
7718
7719 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
7720 DRM_ERROR("LCPLL still locked\n");
7721
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007722 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007723 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007724 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007725 ndelay(100);
7726
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007727 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
7728 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007729 DRM_ERROR("D_COMP RCOMP still in progress\n");
7730
7731 if (allow_power_down) {
7732 val = I915_READ(LCPLL_CTL);
7733 val |= LCPLL_POWER_DOWN_ALLOW;
7734 I915_WRITE(LCPLL_CTL, val);
7735 POSTING_READ(LCPLL_CTL);
7736 }
7737}
7738
7739/*
7740 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
7741 * source.
7742 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03007743static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007744{
7745 uint32_t val;
7746
7747 val = I915_READ(LCPLL_CTL);
7748
7749 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
7750 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
7751 return;
7752
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007753 /*
7754 * Make sure we're not on PC8 state before disabling PC8, otherwise
7755 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
7756 *
7757 * The other problem is that hsw_restore_lcpll() is called as part of
7758 * the runtime PM resume sequence, so we can't just call
7759 * gen6_gt_force_wake_get() because that function calls
7760 * intel_runtime_pm_get(), and we can't change the runtime PM refcount
7761 * while we are on the resume sequence. So to solve this problem we have
7762 * to call special forcewake code that doesn't touch runtime PM and
7763 * doesn't enable the forcewake delayed work.
7764 */
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007765 spin_lock_irq(&dev_priv->uncore.lock);
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007766 if (dev_priv->uncore.forcewake_count++ == 0)
7767 dev_priv->uncore.funcs.force_wake_get(dev_priv, FORCEWAKE_ALL);
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007768 spin_unlock_irq(&dev_priv->uncore.lock);
Paulo Zanoni215733f2013-08-19 13:18:07 -03007769
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007770 if (val & LCPLL_POWER_DOWN_ALLOW) {
7771 val &= ~LCPLL_POWER_DOWN_ALLOW;
7772 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02007773 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007774 }
7775
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03007776 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007777 val |= D_COMP_COMP_FORCE;
7778 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03007779 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007780
7781 val = I915_READ(LCPLL_CTL);
7782 val &= ~LCPLL_PLL_DISABLE;
7783 I915_WRITE(LCPLL_CTL, val);
7784
7785 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
7786 DRM_ERROR("LCPLL not locked yet\n");
7787
7788 if (val & LCPLL_CD_SOURCE_FCLK) {
7789 val = I915_READ(LCPLL_CTL);
7790 val &= ~LCPLL_CD_SOURCE_FCLK;
7791 I915_WRITE(LCPLL_CTL, val);
7792
7793 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
7794 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
7795 DRM_ERROR("Switching back to LCPLL failed\n");
7796 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03007797
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007798 /* See the big comment above. */
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007799 spin_lock_irq(&dev_priv->uncore.lock);
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03007800 if (--dev_priv->uncore.forcewake_count == 0)
7801 dev_priv->uncore.funcs.force_wake_put(dev_priv, FORCEWAKE_ALL);
Daniel Vetterd2e40e22014-09-15 14:55:31 +02007802 spin_unlock_irq(&dev_priv->uncore.lock);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03007803}
7804
Paulo Zanoni765dab672014-03-07 20:08:18 -03007805/*
7806 * Package states C8 and deeper are really deep PC states that can only be
7807 * reached when all the devices on the system allow it, so even if the graphics
7808 * device allows PC8+, it doesn't mean the system will actually get to these
7809 * states. Our driver only allows PC8+ when going into runtime PM.
7810 *
7811 * The requirements for PC8+ are that all the outputs are disabled, the power
7812 * well is disabled and most interrupts are disabled, and these are also
7813 * requirements for runtime PM. When these conditions are met, we manually do
7814 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
7815 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
7816 * hang the machine.
7817 *
7818 * When we really reach PC8 or deeper states (not just when we allow it) we lose
7819 * the state of some registers, so when we come back from PC8+ we need to
7820 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
7821 * need to take care of the registers kept by RC6. Notice that this happens even
7822 * if we don't put the device in PCI D3 state (which is what currently happens
7823 * because of the runtime PM support).
7824 *
7825 * For more, read "Display Sequences for Package C8" on the hardware
7826 * documentation.
7827 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007828void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007829{
Paulo Zanonic67a4702013-08-19 13:18:09 -03007830 struct drm_device *dev = dev_priv->dev;
7831 uint32_t val;
7832
Paulo Zanonic67a4702013-08-19 13:18:09 -03007833 DRM_DEBUG_KMS("Enabling package C8+\n");
7834
Paulo Zanonic67a4702013-08-19 13:18:09 -03007835 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7836 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7837 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
7838 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7839 }
7840
7841 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007842 hsw_disable_lcpll(dev_priv, true, true);
7843}
7844
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03007845void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03007846{
7847 struct drm_device *dev = dev_priv->dev;
7848 uint32_t val;
7849
Paulo Zanonic67a4702013-08-19 13:18:09 -03007850 DRM_DEBUG_KMS("Disabling package C8+\n");
7851
7852 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007853 lpt_init_pch_refclk(dev);
7854
7855 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
7856 val = I915_READ(SOUTH_DSPCLK_GATE_D);
7857 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
7858 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
7859 }
7860
7861 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03007862}
7863
Paulo Zanoni9a952a02014-03-07 20:12:34 -03007864static void snb_modeset_global_resources(struct drm_device *dev)
7865{
7866 modeset_update_crtc_power_domains(dev);
7867}
7868
Imre Deak4f074122013-10-16 17:25:51 +03007869static void haswell_modeset_global_resources(struct drm_device *dev)
7870{
Paulo Zanonida723562013-12-19 11:54:51 -02007871 modeset_update_crtc_power_domains(dev);
Daniel Vetterd6dd9eb2013-01-29 16:35:20 -02007872}
7873
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007874static int haswell_crtc_mode_set(struct intel_crtc *crtc,
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03007875 int x, int y,
7876 struct drm_framebuffer *fb)
7877{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007878 if (!intel_ddi_pll_select(crtc))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03007879 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03007880
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007881 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02007882
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007883 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007884}
7885
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007886static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
7887 enum port port,
7888 struct intel_crtc_config *pipe_config)
7889{
7890 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
7891
7892 switch (pipe_config->ddi_pll_sel) {
7893 case PORT_CLK_SEL_WRPLL1:
7894 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
7895 break;
7896 case PORT_CLK_SEL_WRPLL2:
7897 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
7898 break;
7899 }
7900}
7901
Daniel Vetter26804af2014-06-25 22:01:55 +03007902static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
7903 struct intel_crtc_config *pipe_config)
7904{
7905 struct drm_device *dev = crtc->base.dev;
7906 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007907 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03007908 enum port port;
7909 uint32_t tmp;
7910
7911 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
7912
7913 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
7914
Damien Lespiau7d2c8172014-07-29 18:06:18 +01007915 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03007916
Daniel Vetterd452c5b2014-07-04 11:27:39 -03007917 if (pipe_config->shared_dpll >= 0) {
7918 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
7919
7920 WARN_ON(!pll->get_hw_state(dev_priv, pll,
7921 &pipe_config->dpll_hw_state));
7922 }
7923
Daniel Vetter26804af2014-06-25 22:01:55 +03007924 /*
7925 * Haswell has only FDI/PCH transcoder A. It is which is connected to
7926 * DDI E. So just check whether this pipe is wired to DDI E and whether
7927 * the PCH transcoder is on.
7928 */
Damien Lespiauca370452013-12-03 13:56:24 +00007929 if (INTEL_INFO(dev)->gen < 9 &&
7930 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03007931 pipe_config->has_pch_encoder = true;
7932
7933 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
7934 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
7935 FDI_DP_PORT_WIDTH_SHIFT) + 1;
7936
7937 ironlake_get_fdi_m_n_config(crtc, pipe_config);
7938 }
7939}
7940
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007941static bool haswell_get_pipe_config(struct intel_crtc *crtc,
7942 struct intel_crtc_config *pipe_config)
7943{
7944 struct drm_device *dev = crtc->base.dev;
7945 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007946 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007947 uint32_t tmp;
7948
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007949 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02007950 POWER_DOMAIN_PIPE(crtc->pipe)))
7951 return false;
7952
Daniel Vettere143a212013-07-04 12:01:15 +02007953 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007954 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
7955
Daniel Vettereccb1402013-05-22 00:50:22 +02007956 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
7957 if (tmp & TRANS_DDI_FUNC_ENABLE) {
7958 enum pipe trans_edp_pipe;
7959 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
7960 default:
7961 WARN(1, "unknown pipe linked to edp transcoder\n");
7962 case TRANS_DDI_EDP_INPUT_A_ONOFF:
7963 case TRANS_DDI_EDP_INPUT_A_ON:
7964 trans_edp_pipe = PIPE_A;
7965 break;
7966 case TRANS_DDI_EDP_INPUT_B_ONOFF:
7967 trans_edp_pipe = PIPE_B;
7968 break;
7969 case TRANS_DDI_EDP_INPUT_C_ONOFF:
7970 trans_edp_pipe = PIPE_C;
7971 break;
7972 }
7973
7974 if (trans_edp_pipe == crtc->pipe)
7975 pipe_config->cpu_transcoder = TRANSCODER_EDP;
7976 }
7977
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007978 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02007979 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03007980 return false;
7981
Daniel Vettereccb1402013-05-22 00:50:22 +02007982 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007983 if (!(tmp & PIPECONF_ENABLE))
7984 return false;
7985
Daniel Vetter26804af2014-06-25 22:01:55 +03007986 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02007987
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007988 intel_get_pipe_timings(crtc, pipe_config);
7989
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007990 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007991 if (intel_display_power_is_enabled(dev_priv, pfit_domain))
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007992 ironlake_get_pfit_config(crtc, pipe_config);
Daniel Vetter88adfff2013-03-28 10:42:01 +01007993
Jesse Barnese59150d2014-01-07 13:30:45 -08007994 if (IS_HASWELL(dev))
7995 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
7996 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03007997
Clint Taylorebb69c92014-09-30 10:30:22 -07007998 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
7999 pipe_config->pixel_multiplier =
8000 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
8001 } else {
8002 pipe_config->pixel_multiplier = 1;
8003 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008004
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008005 return true;
8006}
8007
Chris Wilson560b85b2010-08-07 11:01:38 +01008008static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
8009{
8010 struct drm_device *dev = crtc->dev;
8011 struct drm_i915_private *dev_priv = dev->dev_private;
8012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03008013 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01008014
Ville Syrjälädc41c152014-08-13 11:57:05 +03008015 if (base) {
8016 unsigned int width = intel_crtc->cursor_width;
8017 unsigned int height = intel_crtc->cursor_height;
8018 unsigned int stride = roundup_pow_of_two(width) * 4;
8019
8020 switch (stride) {
8021 default:
8022 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
8023 width, stride);
8024 stride = 256;
8025 /* fallthrough */
8026 case 256:
8027 case 512:
8028 case 1024:
8029 case 2048:
8030 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008031 }
8032
Ville Syrjälädc41c152014-08-13 11:57:05 +03008033 cntl |= CURSOR_ENABLE |
8034 CURSOR_GAMMA_ENABLE |
8035 CURSOR_FORMAT_ARGB |
8036 CURSOR_STRIDE(stride);
8037
8038 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008039 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008040
Ville Syrjälädc41c152014-08-13 11:57:05 +03008041 if (intel_crtc->cursor_cntl != 0 &&
8042 (intel_crtc->cursor_base != base ||
8043 intel_crtc->cursor_size != size ||
8044 intel_crtc->cursor_cntl != cntl)) {
8045 /* On these chipsets we can only modify the base/size/stride
8046 * whilst the cursor is disabled.
8047 */
8048 I915_WRITE(_CURACNTR, 0);
8049 POSTING_READ(_CURACNTR);
8050 intel_crtc->cursor_cntl = 0;
8051 }
8052
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008053 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03008054 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008055 intel_crtc->cursor_base = base;
8056 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03008057
8058 if (intel_crtc->cursor_size != size) {
8059 I915_WRITE(CURSIZE, size);
8060 intel_crtc->cursor_size = size;
8061 }
8062
Chris Wilson4b0e3332014-05-30 16:35:26 +03008063 if (intel_crtc->cursor_cntl != cntl) {
8064 I915_WRITE(_CURACNTR, cntl);
8065 POSTING_READ(_CURACNTR);
8066 intel_crtc->cursor_cntl = cntl;
8067 }
Chris Wilson560b85b2010-08-07 11:01:38 +01008068}
8069
8070static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
8071{
8072 struct drm_device *dev = crtc->dev;
8073 struct drm_i915_private *dev_priv = dev->dev_private;
8074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8075 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03008076 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01008077
Chris Wilson4b0e3332014-05-30 16:35:26 +03008078 cntl = 0;
8079 if (base) {
8080 cntl = MCURSOR_GAMMA_ENABLE;
8081 switch (intel_crtc->cursor_width) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05308082 case 64:
8083 cntl |= CURSOR_MODE_64_ARGB_AX;
8084 break;
8085 case 128:
8086 cntl |= CURSOR_MODE_128_ARGB_AX;
8087 break;
8088 case 256:
8089 cntl |= CURSOR_MODE_256_ARGB_AX;
8090 break;
8091 default:
8092 WARN_ON(1);
8093 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01008094 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008095 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03008096
8097 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
8098 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01008099 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03008100
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008101 if (to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180))
8102 cntl |= CURSOR_ROTATE_180;
8103
Chris Wilson4b0e3332014-05-30 16:35:26 +03008104 if (intel_crtc->cursor_cntl != cntl) {
8105 I915_WRITE(CURCNTR(pipe), cntl);
8106 POSTING_READ(CURCNTR(pipe));
8107 intel_crtc->cursor_cntl = cntl;
8108 }
8109
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008110 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008111 I915_WRITE(CURBASE(pipe), base);
8112 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03008113
8114 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07008115}
8116
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008117/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01008118static void intel_crtc_update_cursor(struct drm_crtc *crtc,
8119 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008120{
8121 struct drm_device *dev = crtc->dev;
8122 struct drm_i915_private *dev_priv = dev->dev_private;
8123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8124 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07008125 int x = crtc->cursor_x;
8126 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008127 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008128
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008129 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008130 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008131
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03008132 if (x >= intel_crtc->config.pipe_src_w)
8133 base = 0;
8134
8135 if (y >= intel_crtc->config.pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008136 base = 0;
8137
8138 if (x < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008139 if (x + intel_crtc->cursor_width <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008140 base = 0;
8141
8142 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
8143 x = -x;
8144 }
8145 pos |= x << CURSOR_X_SHIFT;
8146
8147 if (y < 0) {
Ville Syrjäläefc90642013-09-04 18:25:30 +03008148 if (y + intel_crtc->cursor_height <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008149 base = 0;
8150
8151 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
8152 y = -y;
8153 }
8154 pos |= y << CURSOR_Y_SHIFT;
8155
Chris Wilson4b0e3332014-05-30 16:35:26 +03008156 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008157 return;
8158
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008159 I915_WRITE(CURPOS(pipe), pos);
8160
Ville Syrjälä4398ad42014-10-23 07:41:34 -07008161 /* ILK+ do this automagically */
8162 if (HAS_GMCH_DISPLAY(dev) &&
8163 to_intel_plane(crtc->cursor)->rotation == BIT(DRM_ROTATE_180)) {
8164 base += (intel_crtc->cursor_height *
8165 intel_crtc->cursor_width - 1) * 4;
8166 }
8167
Ville Syrjälä8ac54662014-08-12 19:39:54 +03008168 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03008169 i845_update_cursor(crtc, base);
8170 else
8171 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008172}
8173
Ville Syrjälädc41c152014-08-13 11:57:05 +03008174static bool cursor_size_ok(struct drm_device *dev,
8175 uint32_t width, uint32_t height)
8176{
8177 if (width == 0 || height == 0)
8178 return false;
8179
8180 /*
8181 * 845g/865g are special in that they are only limited by
8182 * the width of their cursors, the height is arbitrary up to
8183 * the precision of the register. Everything else requires
8184 * square cursors, limited to a few power-of-two sizes.
8185 */
8186 if (IS_845G(dev) || IS_I865G(dev)) {
8187 if ((width & 63) != 0)
8188 return false;
8189
8190 if (width > (IS_845G(dev) ? 64 : 512))
8191 return false;
8192
8193 if (height > 1023)
8194 return false;
8195 } else {
8196 switch (width | height) {
8197 case 256:
8198 case 128:
8199 if (IS_GEN2(dev))
8200 return false;
8201 case 64:
8202 break;
8203 default:
8204 return false;
8205 }
8206 }
8207
8208 return true;
8209}
8210
Matt Ropere3287952014-06-10 08:28:12 -07008211static int intel_crtc_cursor_set_obj(struct drm_crtc *crtc,
8212 struct drm_i915_gem_object *obj,
8213 uint32_t width, uint32_t height)
Jesse Barnes79e53942008-11-07 14:24:08 -08008214{
8215 struct drm_device *dev = crtc->dev;
8216 struct drm_i915_private *dev_priv = dev->dev_private;
8217 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02008218 enum pipe pipe = intel_crtc->pipe;
Gustavo Padovan757f9a32014-09-24 14:20:24 -03008219 unsigned old_width;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008220 uint32_t addr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008221 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008222
Jesse Barnes79e53942008-11-07 14:24:08 -08008223 /* if we want to turn off the cursor ignore width and height */
Matt Ropere3287952014-06-10 08:28:12 -07008224 if (!obj) {
Zhao Yakui28c97732009-10-09 11:39:41 +08008225 DRM_DEBUG_KMS("cursor off\n");
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008226 addr = 0;
Pierre Willenbrock50044172009-02-23 10:12:15 +10008227 mutex_lock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008228 goto finish;
Jesse Barnes79e53942008-11-07 14:24:08 -08008229 }
8230
Dave Airlie71acb5e2008-12-30 20:31:46 +10008231 /* we only need to pin inside GTT if cursor is non-phy */
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008232 mutex_lock(&dev->struct_mutex);
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008233 if (!INTEL_INFO(dev)->cursor_needs_physical) {
Chris Wilson693db182013-03-05 14:52:39 +00008234 unsigned alignment;
8235
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008236 /*
8237 * Global gtt pte registers are special registers which actually
8238 * forward writes to a chunk of system memory. Which means that
8239 * there is no risk that the register values disappear as soon
8240 * as we call intel_runtime_pm_put(), so it is correct to wrap
8241 * only the pin/unpin/fence and not more.
8242 */
8243 intel_runtime_pm_get(dev_priv);
8244
Chris Wilson693db182013-03-05 14:52:39 +00008245 /* Note that the w/a also requires 2 PTE of padding following
8246 * the bo. We currently fill all unused PTE with the shadow
8247 * page and so we should always have valid PTE following the
8248 * cursor preventing the VT-d warning.
8249 */
8250 alignment = 0;
8251 if (need_vtd_wa(dev))
8252 alignment = 64*1024;
8253
8254 ret = i915_gem_object_pin_to_display_plane(obj, alignment, NULL);
Chris Wilsone7b526b2010-06-02 08:30:48 +01008255 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008256 DRM_DEBUG_KMS("failed to move cursor bo into the GTT\n");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008257 intel_runtime_pm_put(dev_priv);
Chris Wilson2da3b9b2011-04-14 09:41:17 +01008258 goto fail_locked;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008259 }
8260
Chris Wilsond9e86c02010-11-10 16:40:20 +00008261 ret = i915_gem_object_put_fence(obj);
8262 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008263 DRM_DEBUG_KMS("failed to release fence for cursor");
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008264 intel_runtime_pm_put(dev_priv);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008265 goto fail_unpin;
8266 }
8267
Ben Widawskyf343c5f2013-07-05 14:41:04 -07008268 addr = i915_gem_obj_ggtt_offset(obj);
Paulo Zanonid6dd6842014-08-15 15:59:32 -03008269
8270 intel_runtime_pm_put(dev_priv);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008271 } else {
Chris Wilson6eeefaf2010-08-07 11:01:39 +01008272 int align = IS_I830(dev) ? 16 * 1024 : 256;
Chris Wilson00731152014-05-21 12:42:56 +01008273 ret = i915_gem_object_attach_phys(obj, align);
Dave Airlie71acb5e2008-12-30 20:31:46 +10008274 if (ret) {
Daniel Vetter3b25b312014-02-14 14:06:06 +01008275 DRM_DEBUG_KMS("failed to attach phys object\n");
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008276 goto fail_locked;
Dave Airlie71acb5e2008-12-30 20:31:46 +10008277 }
Chris Wilson00731152014-05-21 12:42:56 +01008278 addr = obj->phys_handle->busaddr;
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008279 }
8280
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008281 finish:
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008282 if (intel_crtc->cursor_bo) {
Chris Wilson00731152014-05-21 12:42:56 +01008283 if (!INTEL_INFO(dev)->cursor_needs_physical)
Chris Wilsoncc98b412013-08-09 12:25:09 +01008284 i915_gem_object_unpin_from_display_plane(intel_crtc->cursor_bo);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008285 }
Jesse Barnes80824002009-09-10 15:28:06 -07008286
Daniel Vettera071fa02014-06-18 23:28:09 +02008287 i915_gem_track_fb(intel_crtc->cursor_bo, obj,
8288 INTEL_FRONTBUFFER_CURSOR(pipe));
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008289 mutex_unlock(&dev->struct_mutex);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008290
Chris Wilson64f962e2014-03-26 12:38:15 +00008291 old_width = intel_crtc->cursor_width;
8292
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008293 intel_crtc->cursor_addr = addr;
Chris Wilson05394f32010-11-08 19:18:58 +00008294 intel_crtc->cursor_bo = obj;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01008295 intel_crtc->cursor_width = width;
8296 intel_crtc->cursor_height = height;
8297
Chris Wilson64f962e2014-03-26 12:38:15 +00008298 if (intel_crtc->active) {
8299 if (old_width != width)
8300 intel_update_watermarks(crtc);
Ville Syrjäläf2f5f7712013-09-17 18:33:44 +03008301 intel_crtc_update_cursor(crtc, intel_crtc->cursor_bo != NULL);
Kristian Høgsberg3f8bc372008-12-17 22:14:59 -05008302
Gustavo Padovan3f20df92014-10-24 14:51:34 +01008303 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_CURSOR(pipe));
8304 }
Daniel Vetterf99d7062014-06-19 16:01:59 +02008305
Jesse Barnes79e53942008-11-07 14:24:08 -08008306 return 0;
Chris Wilsone7b526b2010-06-02 08:30:48 +01008307fail_unpin:
Chris Wilsoncc98b412013-08-09 12:25:09 +01008308 i915_gem_object_unpin_from_display_plane(obj);
Kristian Høgsberg7f9872e2009-02-13 20:56:49 -05008309fail_locked:
Dave Airlie34b8686e2009-01-15 14:03:07 +10008310 mutex_unlock(&dev->struct_mutex);
8311 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08008312}
8313
Jesse Barnes79e53942008-11-07 14:24:08 -08008314static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01008315 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08008316{
James Simmons72034252010-08-03 01:33:19 +01008317 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08008318 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008319
James Simmons72034252010-08-03 01:33:19 +01008320 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008321 intel_crtc->lut_r[i] = red[i] >> 8;
8322 intel_crtc->lut_g[i] = green[i] >> 8;
8323 intel_crtc->lut_b[i] = blue[i] >> 8;
8324 }
8325
8326 intel_crtc_load_lut(crtc);
8327}
8328
Jesse Barnes79e53942008-11-07 14:24:08 -08008329/* VESA 640x480x72Hz mode to set on the pipe */
8330static struct drm_display_mode load_detect_mode = {
8331 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
8332 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
8333};
8334
Daniel Vettera8bb6812014-02-10 18:00:39 +01008335struct drm_framebuffer *
8336__intel_framebuffer_create(struct drm_device *dev,
8337 struct drm_mode_fb_cmd2 *mode_cmd,
8338 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01008339{
8340 struct intel_framebuffer *intel_fb;
8341 int ret;
8342
8343 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
8344 if (!intel_fb) {
8345 drm_gem_object_unreference_unlocked(&obj->base);
8346 return ERR_PTR(-ENOMEM);
8347 }
8348
8349 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008350 if (ret)
8351 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01008352
8353 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02008354err:
8355 drm_gem_object_unreference_unlocked(&obj->base);
8356 kfree(intel_fb);
8357
8358 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01008359}
8360
Daniel Vetterb5ea6422014-03-02 21:18:00 +01008361static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01008362intel_framebuffer_create(struct drm_device *dev,
8363 struct drm_mode_fb_cmd2 *mode_cmd,
8364 struct drm_i915_gem_object *obj)
8365{
8366 struct drm_framebuffer *fb;
8367 int ret;
8368
8369 ret = i915_mutex_lock_interruptible(dev);
8370 if (ret)
8371 return ERR_PTR(ret);
8372 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
8373 mutex_unlock(&dev->struct_mutex);
8374
8375 return fb;
8376}
8377
Chris Wilsond2dff872011-04-19 08:36:26 +01008378static u32
8379intel_framebuffer_pitch_for_width(int width, int bpp)
8380{
8381 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
8382 return ALIGN(pitch, 64);
8383}
8384
8385static u32
8386intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
8387{
8388 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02008389 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01008390}
8391
8392static struct drm_framebuffer *
8393intel_framebuffer_create_for_mode(struct drm_device *dev,
8394 struct drm_display_mode *mode,
8395 int depth, int bpp)
8396{
8397 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00008398 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01008399
8400 obj = i915_gem_alloc_object(dev,
8401 intel_framebuffer_size_for_mode(mode, bpp));
8402 if (obj == NULL)
8403 return ERR_PTR(-ENOMEM);
8404
8405 mode_cmd.width = mode->hdisplay;
8406 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08008407 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
8408 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00008409 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01008410
8411 return intel_framebuffer_create(dev, &mode_cmd, obj);
8412}
8413
8414static struct drm_framebuffer *
8415mode_fits_in_fbdev(struct drm_device *dev,
8416 struct drm_display_mode *mode)
8417{
Daniel Vetter4520f532013-10-09 09:18:51 +02008418#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01008419 struct drm_i915_private *dev_priv = dev->dev_private;
8420 struct drm_i915_gem_object *obj;
8421 struct drm_framebuffer *fb;
8422
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008423 if (!dev_priv->fbdev)
8424 return NULL;
8425
8426 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01008427 return NULL;
8428
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008429 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01008430 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01008431
Jesse Barnes8bcd4552014-02-07 12:10:38 -08008432 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008433 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
8434 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01008435 return NULL;
8436
Ville Syrjälä01f2c772011-12-20 00:06:49 +02008437 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01008438 return NULL;
8439
8440 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02008441#else
8442 return NULL;
8443#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01008444}
8445
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008446bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01008447 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05008448 struct intel_load_detect_pipe *old,
8449 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08008450{
8451 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008452 struct intel_encoder *intel_encoder =
8453 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08008454 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01008455 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08008456 struct drm_crtc *crtc = NULL;
8457 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02008458 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05008459 struct drm_mode_config *config = &dev->mode_config;
8460 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008461
Chris Wilsond2dff872011-04-19 08:36:26 +01008462 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008463 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008464 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008465
Rob Clark51fd3712013-11-19 12:10:12 -05008466retry:
8467 ret = drm_modeset_lock(&config->connection_mutex, ctx);
8468 if (ret)
8469 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02008470
Jesse Barnes79e53942008-11-07 14:24:08 -08008471 /*
8472 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01008473 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008474 * - if the connector already has an assigned crtc, use it (but make
8475 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01008476 *
Jesse Barnes79e53942008-11-07 14:24:08 -08008477 * - try to find the first unused crtc that can drive this connector,
8478 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08008479 */
8480
8481 /* See if we already have a CRTC for this connector */
8482 if (encoder->crtc) {
8483 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01008484
Rob Clark51fd3712013-11-19 12:10:12 -05008485 ret = drm_modeset_lock(&crtc->mutex, ctx);
8486 if (ret)
8487 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01008488
Daniel Vetter24218aa2012-08-12 19:27:11 +02008489 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008490 old->load_detect_temp = false;
8491
8492 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008493 if (connector->dpms != DRM_MODE_DPMS_ON)
8494 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01008495
Chris Wilson71731882011-04-19 23:10:58 +01008496 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08008497 }
8498
8499 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008500 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008501 i++;
8502 if (!(encoder->possible_crtcs & (1 << i)))
8503 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +03008504 if (possible_crtc->enabled)
8505 continue;
8506 /* This can occur when applying the pipe A quirk on resume. */
8507 if (to_intel_crtc(possible_crtc)->new_enabled)
8508 continue;
8509
8510 crtc = possible_crtc;
8511 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008512 }
8513
8514 /*
8515 * If we didn't find an unused CRTC, don't use any.
8516 */
8517 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01008518 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05008519 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08008520 }
8521
Rob Clark51fd3712013-11-19 12:10:12 -05008522 ret = drm_modeset_lock(&crtc->mutex, ctx);
8523 if (ret)
8524 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02008525 intel_encoder->new_crtc = to_intel_crtc(crtc);
8526 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008527
8528 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008529 intel_crtc->new_enabled = true;
8530 intel_crtc->new_config = &intel_crtc->config;
Daniel Vetter24218aa2012-08-12 19:27:11 +02008531 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01008532 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01008533 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08008534
Chris Wilson64927112011-04-20 07:25:26 +01008535 if (!mode)
8536 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08008537
Chris Wilsond2dff872011-04-19 08:36:26 +01008538 /* We need a framebuffer large enough to accommodate all accesses
8539 * that the plane may generate whilst we perform load detection.
8540 * We can not rely on the fbcon either being present (we get called
8541 * during its initialisation to detect all boot displays, or it may
8542 * not even exist) or that it is large enough to satisfy the
8543 * requested mode.
8544 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02008545 fb = mode_fits_in_fbdev(dev, mode);
8546 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008547 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008548 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
8549 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01008550 } else
8551 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02008552 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01008553 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008554 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008555 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008556
Chris Wilsonc0c36b942012-12-19 16:08:43 +00008557 if (intel_set_mode(crtc, mode, 0, 0, fb)) {
Chris Wilson64927112011-04-20 07:25:26 +01008558 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01008559 if (old->release_fb)
8560 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008561 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08008562 }
Chris Wilson71731882011-04-19 23:10:58 +01008563
Jesse Barnes79e53942008-11-07 14:24:08 -08008564 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008565 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01008566 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008567
8568 fail:
8569 intel_crtc->new_enabled = crtc->enabled;
8570 if (intel_crtc->new_enabled)
8571 intel_crtc->new_config = &intel_crtc->config;
8572 else
8573 intel_crtc->new_config = NULL;
Rob Clark51fd3712013-11-19 12:10:12 -05008574fail_unlock:
8575 if (ret == -EDEADLK) {
8576 drm_modeset_backoff(ctx);
8577 goto retry;
8578 }
8579
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008580 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08008581}
8582
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008583void intel_release_load_detect_pipe(struct drm_connector *connector,
Ville Syrjälä208bf9f2014-08-11 13:15:35 +03008584 struct intel_load_detect_pipe *old)
Jesse Barnes79e53942008-11-07 14:24:08 -08008585{
Daniel Vetterd2434ab2012-08-12 21:20:10 +02008586 struct intel_encoder *intel_encoder =
8587 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01008588 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01008589 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08008591
Chris Wilsond2dff872011-04-19 08:36:26 +01008592 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03008593 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03008594 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01008595
Chris Wilson8261b192011-04-19 23:18:09 +01008596 if (old->load_detect_temp) {
Daniel Vetterfc303102012-07-09 10:40:58 +02008597 to_intel_connector(connector)->new_encoder = NULL;
8598 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02008599 intel_crtc->new_enabled = false;
8600 intel_crtc->new_config = NULL;
Daniel Vetterfc303102012-07-09 10:40:58 +02008601 intel_set_mode(crtc, NULL, 0, 0, NULL);
Chris Wilsond2dff872011-04-19 08:36:26 +01008602
Daniel Vetter36206362012-12-10 20:42:17 +01008603 if (old->release_fb) {
8604 drm_framebuffer_unregister_private(old->release_fb);
8605 drm_framebuffer_unreference(old->release_fb);
8606 }
Chris Wilsond2dff872011-04-19 08:36:26 +01008607
Chris Wilson0622a532011-04-21 09:32:11 +01008608 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008609 }
8610
Eric Anholtc751ce42010-03-25 11:48:48 -07008611 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02008612 if (old->dpms_mode != DRM_MODE_DPMS_ON)
8613 connector->funcs->dpms(connector, old->dpms_mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008614}
8615
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008616static int i9xx_pll_refclk(struct drm_device *dev,
8617 const struct intel_crtc_config *pipe_config)
8618{
8619 struct drm_i915_private *dev_priv = dev->dev_private;
8620 u32 dpll = pipe_config->dpll_hw_state.dpll;
8621
8622 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008623 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008624 else if (HAS_PCH_SPLIT(dev))
8625 return 120000;
8626 else if (!IS_GEN2(dev))
8627 return 96000;
8628 else
8629 return 48000;
8630}
8631
Jesse Barnes79e53942008-11-07 14:24:08 -08008632/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008633static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
8634 struct intel_crtc_config *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08008635{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008636 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008637 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008638 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008639 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008640 u32 fp;
8641 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008642 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08008643
8644 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03008645 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008646 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03008647 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08008648
8649 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008650 if (IS_PINEVIEW(dev)) {
8651 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
8652 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08008653 } else {
8654 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
8655 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
8656 }
8657
Chris Wilsona6c45cf2010-09-17 00:32:17 +01008658 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05008659 if (IS_PINEVIEW(dev))
8660 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
8661 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08008662 else
8663 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08008664 DPLL_FPA01_P1_POST_DIV_SHIFT);
8665
8666 switch (dpll & DPLL_MODE_MASK) {
8667 case DPLLB_MODE_DAC_SERIAL:
8668 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
8669 5 : 10;
8670 break;
8671 case DPLLB_MODE_LVDS:
8672 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
8673 7 : 14;
8674 break;
8675 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08008676 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08008677 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008678 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08008679 }
8680
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008681 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008682 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02008683 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008684 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008685 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02008686 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008687 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08008688
8689 if (is_lvds) {
8690 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
8691 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02008692
8693 if (lvds & LVDS_CLKB_POWER_UP)
8694 clock.p2 = 7;
8695 else
8696 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08008697 } else {
8698 if (dpll & PLL_P1_DIVIDE_BY_TWO)
8699 clock.p1 = 2;
8700 else {
8701 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
8702 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
8703 }
8704 if (dpll & PLL_P2_DIVIDE_BY_4)
8705 clock.p2 = 4;
8706 else
8707 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08008708 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03008709
8710 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08008711 }
8712
Ville Syrjälä18442d02013-09-13 16:00:08 +03008713 /*
8714 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01008715 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03008716 * encoder's get_config() function.
8717 */
8718 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008719}
8720
Ville Syrjälä6878da02013-09-13 15:59:11 +03008721int intel_dotclock_calculate(int link_freq,
8722 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008723{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008724 /*
8725 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008726 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008727 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008728 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008729 *
8730 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03008731 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08008732 */
8733
Ville Syrjälä6878da02013-09-13 15:59:11 +03008734 if (!m_n->link_n)
8735 return 0;
8736
8737 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
8738}
8739
Ville Syrjälä18442d02013-09-13 16:00:08 +03008740static void ironlake_pch_clock_get(struct intel_crtc *crtc,
8741 struct intel_crtc_config *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03008742{
8743 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008744
8745 /* read out port_clock from the DPLL */
8746 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03008747
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008748 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03008749 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01008750 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03008751 * agree once we know their relationship in the encoder's
8752 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008753 */
Damien Lespiau241bfc32013-09-25 16:45:37 +01008754 pipe_config->adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03008755 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
8756 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08008757}
8758
8759/** Returns the currently programmed mode of the given pipe. */
8760struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
8761 struct drm_crtc *crtc)
8762{
Jesse Barnes548f2452011-02-17 10:40:53 -08008763 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08008764 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter3b117c82013-04-17 20:15:07 +02008765 enum transcoder cpu_transcoder = intel_crtc->config.cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08008766 struct drm_display_mode *mode;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008767 struct intel_crtc_config pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02008768 int htot = I915_READ(HTOTAL(cpu_transcoder));
8769 int hsync = I915_READ(HSYNC(cpu_transcoder));
8770 int vtot = I915_READ(VTOTAL(cpu_transcoder));
8771 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03008772 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08008773
8774 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
8775 if (!mode)
8776 return NULL;
8777
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008778 /*
8779 * Construct a pipe_config sufficient for getting the clock info
8780 * back out of crtc_clock_get.
8781 *
8782 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
8783 * to use a real value here instead.
8784 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03008785 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008786 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03008787 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
8788 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
8789 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03008790 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
8791
Ville Syrjälä773ae032013-09-23 17:48:20 +03008792 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08008793 mode->hdisplay = (htot & 0xffff) + 1;
8794 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
8795 mode->hsync_start = (hsync & 0xffff) + 1;
8796 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
8797 mode->vdisplay = (vtot & 0xffff) + 1;
8798 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
8799 mode->vsync_start = (vsync & 0xffff) + 1;
8800 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
8801
8802 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08008803
8804 return mode;
8805}
8806
Jesse Barnes652c3932009-08-17 13:31:43 -07008807static void intel_decrease_pllclock(struct drm_crtc *crtc)
8808{
8809 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03008810 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07008811 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07008812
Sonika Jindalbaff2962014-07-22 11:16:35 +05308813 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -07008814 return;
8815
8816 if (!dev_priv->lvds_downclock_avail)
8817 return;
8818
8819 /*
8820 * Since this is called by a timer, we should never get here in
8821 * the manual case.
8822 */
8823 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +01008824 int pipe = intel_crtc->pipe;
8825 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +02008826 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +01008827
Zhao Yakui44d98a62009-10-09 11:39:40 +08008828 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008829
Sean Paul8ac5a6d2012-02-13 13:14:51 -05008830 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008831
Chris Wilson074b5e12012-05-02 12:07:06 +01008832 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -07008833 dpll |= DISPLAY_RATE_SELECT_FPA1;
8834 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07008835 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -07008836 dpll = I915_READ(dpll_reg);
8837 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +08008838 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -07008839 }
8840
8841}
8842
Chris Wilsonf047e392012-07-21 12:31:41 +01008843void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -07008844{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008845 struct drm_i915_private *dev_priv = dev->dev_private;
8846
Chris Wilsonf62a0072014-02-21 17:55:39 +00008847 if (dev_priv->mm.busy)
8848 return;
8849
Paulo Zanoni43694d62014-03-07 20:08:08 -03008850 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03008851 i915_update_gfx_val(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +00008852 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +01008853}
8854
8855void intel_mark_idle(struct drm_device *dev)
8856{
Paulo Zanonic67a4702013-08-19 13:18:09 -03008857 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +00008858 struct drm_crtc *crtc;
8859
Chris Wilsonf62a0072014-02-21 17:55:39 +00008860 if (!dev_priv->mm.busy)
8861 return;
8862
8863 dev_priv->mm.busy = false;
8864
Jani Nikulad330a952014-01-21 11:24:25 +02008865 if (!i915.powersave)
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008866 goto out;
Chris Wilson725a5b52013-01-08 11:02:57 +00008867
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01008868 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -07008869 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +00008870 continue;
8871
8872 intel_decrease_pllclock(crtc);
8873 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008874
Damien Lespiau3d13ef22014-02-07 19:12:47 +00008875 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +01008876 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -03008877
8878out:
Paulo Zanoni43694d62014-03-07 20:08:08 -03008879 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +01008880}
8881
Jesse Barnes79e53942008-11-07 14:24:08 -08008882static void intel_crtc_destroy(struct drm_crtc *crtc)
8883{
8884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008885 struct drm_device *dev = crtc->dev;
8886 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +02008887
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02008888 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008889 work = intel_crtc->unpin_work;
8890 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02008891 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008892
8893 if (work) {
8894 cancel_work_sync(&work->work);
8895 kfree(work);
8896 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008897
8898 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +02008899
Jesse Barnes79e53942008-11-07 14:24:08 -08008900 kfree(intel_crtc);
8901}
8902
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008903static void intel_unpin_work_fn(struct work_struct *__work)
8904{
8905 struct intel_unpin_work *work =
8906 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008907 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +02008908 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008909
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008910 mutex_lock(&dev->struct_mutex);
Chris Wilson1690e1e2011-12-14 13:57:08 +01008911 intel_unpin_fb_obj(work->old_fb_obj);
Chris Wilson05394f32010-11-08 19:18:58 +00008912 drm_gem_object_unreference(&work->pending_flip_obj->base);
8913 drm_gem_object_unreference(&work->old_fb_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +00008914
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008915 intel_update_fbc(dev);
8916 mutex_unlock(&dev->struct_mutex);
8917
Daniel Vetterf99d7062014-06-19 16:01:59 +02008918 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
8919
Chris Wilsonb4a98e52012-11-01 09:26:26 +00008920 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
8921 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
8922
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008923 kfree(work);
8924}
8925
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008926static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +01008927 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008928{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008929 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8930 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008931 unsigned long flags;
8932
8933 /* Ignore early vblank irqs */
8934 if (intel_crtc == NULL)
8935 return;
8936
Daniel Vetterf3260382014-09-15 14:55:23 +02008937 /*
8938 * This is called both by irq handlers and the reset code (to complete
8939 * lost pageflips) so needs the full irqsave spinlocks.
8940 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008941 spin_lock_irqsave(&dev->event_lock, flags);
8942 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +00008943
8944 /* Ensure we don't miss a work->pending update ... */
8945 smp_rmb();
8946
8947 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008948 spin_unlock_irqrestore(&dev->event_lock, flags);
8949 return;
8950 }
8951
Chris Wilsond6bbafa2014-09-05 07:13:24 +01008952 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +01008953
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008954 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05008955}
8956
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008957void intel_finish_page_flip(struct drm_device *dev, int pipe)
8958{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008959 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008960 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
8961
Mario Kleiner49b14a52010-12-09 07:00:07 +01008962 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008963}
8964
8965void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
8966{
Jani Nikulafbee40d2014-03-31 14:27:18 +03008967 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008968 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
8969
Mario Kleiner49b14a52010-12-09 07:00:07 +01008970 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -07008971}
8972
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03008973/* Is 'a' after or equal to 'b'? */
8974static bool g4x_flip_count_after_eq(u32 a, u32 b)
8975{
8976 return !((a - b) & 0x80000000);
8977}
8978
8979static bool page_flip_finished(struct intel_crtc *crtc)
8980{
8981 struct drm_device *dev = crtc->base.dev;
8982 struct drm_i915_private *dev_priv = dev->dev_private;
8983
8984 /*
8985 * The relevant registers doen't exist on pre-ctg.
8986 * As the flip done interrupt doesn't trigger for mmio
8987 * flips on gmch platforms, a flip count check isn't
8988 * really needed there. But since ctg has the registers,
8989 * include it in the check anyway.
8990 */
8991 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
8992 return true;
8993
8994 /*
8995 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
8996 * used the same base address. In that case the mmio flip might
8997 * have completed, but the CS hasn't even executed the flip yet.
8998 *
8999 * A flip count check isn't enough as the CS might have updated
9000 * the base address just after start of vblank, but before we
9001 * managed to process the interrupt. This means we'd complete the
9002 * CS flip too soon.
9003 *
9004 * Combining both checks should get us a good enough result. It may
9005 * still happen that the CS flip has been executed, but has not
9006 * yet actually completed. But in case the base address is the same
9007 * anyway, we don't really care.
9008 */
9009 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
9010 crtc->unpin_work->gtt_offset &&
9011 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
9012 crtc->unpin_work->flip_count);
9013}
9014
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009015void intel_prepare_page_flip(struct drm_device *dev, int plane)
9016{
Jani Nikulafbee40d2014-03-31 14:27:18 +03009017 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009018 struct intel_crtc *intel_crtc =
9019 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
9020 unsigned long flags;
9021
Daniel Vetterf3260382014-09-15 14:55:23 +02009022
9023 /*
9024 * This is called both by irq handlers and the reset code (to complete
9025 * lost pageflips) so needs the full irqsave spinlocks.
9026 *
9027 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +00009028 * generate a page-flip completion irq, i.e. every modeset
9029 * is also accompanied by a spurious intel_prepare_page_flip().
9030 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009031 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009032 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +00009033 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009034 spin_unlock_irqrestore(&dev->event_lock, flags);
9035}
9036
Robin Schroereba905b2014-05-18 02:24:50 +02009037static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +00009038{
9039 /* Ensure that the work item is consistent when activating it ... */
9040 smp_wmb();
9041 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
9042 /* and that it is marked active as soon as the irq could fire. */
9043 smp_wmb();
9044}
9045
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009046static int intel_gen2_queue_flip(struct drm_device *dev,
9047 struct drm_crtc *crtc,
9048 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009049 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009050 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009051 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009052{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009054 u32 flip_mask;
9055 int ret;
9056
Daniel Vetter6d90c952012-04-26 23:28:05 +02009057 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009058 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009059 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009060
9061 /* Can't queue multiple flips, so wait for the previous
9062 * one to finish before executing the next.
9063 */
9064 if (intel_crtc->plane)
9065 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9066 else
9067 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009068 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9069 intel_ring_emit(ring, MI_NOOP);
9070 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9071 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9072 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009073 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009074 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +00009075
9076 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009077 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009078 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009079}
9080
9081static int intel_gen3_queue_flip(struct drm_device *dev,
9082 struct drm_crtc *crtc,
9083 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009084 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009085 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009086 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009087{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009089 u32 flip_mask;
9090 int ret;
9091
Daniel Vetter6d90c952012-04-26 23:28:05 +02009092 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009093 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009094 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009095
9096 if (intel_crtc->plane)
9097 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
9098 else
9099 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009100 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
9101 intel_ring_emit(ring, MI_NOOP);
9102 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
9103 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9104 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009105 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +02009106 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009107
Chris Wilsone7d841c2012-12-03 11:36:30 +00009108 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009109 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009110 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009111}
9112
9113static int intel_gen4_queue_flip(struct drm_device *dev,
9114 struct drm_crtc *crtc,
9115 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009116 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009117 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009118 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009119{
9120 struct drm_i915_private *dev_priv = dev->dev_private;
9121 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9122 uint32_t pf, pipesrc;
9123 int ret;
9124
Daniel Vetter6d90c952012-04-26 23:28:05 +02009125 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009126 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009127 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009128
9129 /* i965+ uses the linear or tiled offsets from the
9130 * Display Registers (which do not change across a page-flip)
9131 * so we need only reprogram the base address.
9132 */
Daniel Vetter6d90c952012-04-26 23:28:05 +02009133 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9134 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9135 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009136 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +02009137 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009138
9139 /* XXX Enabling the panel-fitter across page-flip is so far
9140 * untested on non-native modes, so ignore it for now.
9141 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
9142 */
9143 pf = 0;
9144 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009145 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009146
9147 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009148 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009149 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009150}
9151
9152static int intel_gen6_queue_flip(struct drm_device *dev,
9153 struct drm_crtc *crtc,
9154 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009155 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009156 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009157 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009158{
9159 struct drm_i915_private *dev_priv = dev->dev_private;
9160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9161 uint32_t pf, pipesrc;
9162 int ret;
9163
Daniel Vetter6d90c952012-04-26 23:28:05 +02009164 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009165 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009166 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009167
Daniel Vetter6d90c952012-04-26 23:28:05 +02009168 intel_ring_emit(ring, MI_DISPLAY_FLIP |
9169 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
9170 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009171 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009172
Chris Wilson99d9acd2012-04-17 20:37:00 +01009173 /* Contrary to the suggestions in the documentation,
9174 * "Enable Panel Fitter" does not seem to be required when page
9175 * flipping with a non-native mode, and worse causes a normal
9176 * modeset to fail.
9177 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
9178 */
9179 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009180 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +02009181 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +00009182
9183 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009184 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009185 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009186}
9187
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009188static int intel_gen7_queue_flip(struct drm_device *dev,
9189 struct drm_crtc *crtc,
9190 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009191 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009192 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009193 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009194{
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009196 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +01009197 int len, ret;
9198
Robin Schroereba905b2014-05-18 02:24:50 +02009199 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009200 case PLANE_A:
9201 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
9202 break;
9203 case PLANE_B:
9204 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
9205 break;
9206 case PLANE_C:
9207 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
9208 break;
9209 default:
9210 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009211 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009212 }
9213
Chris Wilsonffe74d72013-08-26 20:58:12 +01009214 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +01009215 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +01009216 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +01009217 /*
9218 * On Gen 8, SRM is now taking an extra dword to accommodate
9219 * 48bits addresses, and we need a NOOP for the batch size to
9220 * stay even.
9221 */
9222 if (IS_GEN8(dev))
9223 len += 2;
9224 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009225
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009226 /*
9227 * BSpec MI_DISPLAY_FLIP for IVB:
9228 * "The full packet must be contained within the same cache line."
9229 *
9230 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
9231 * cacheline, if we ever start emitting more commands before
9232 * the MI_DISPLAY_FLIP we may need to first emit everything else,
9233 * then do the cacheline alignment, and finally emit the
9234 * MI_DISPLAY_FLIP.
9235 */
9236 ret = intel_ring_cacheline_align(ring);
9237 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009238 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +02009239
Chris Wilsonffe74d72013-08-26 20:58:12 +01009240 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009241 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009242 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009243
Chris Wilsonffe74d72013-08-26 20:58:12 +01009244 /* Unmask the flip-done completion message. Note that the bspec says that
9245 * we should do this for both the BCS and RCS, and that we must not unmask
9246 * more than one flip event at any time (or ensure that one flip message
9247 * can be sent by waiting for flip-done prior to queueing new flips).
9248 * Experimentation says that BCS works despite DERRMR masking all
9249 * flip-done completion events and that unmasking all planes at once
9250 * for the RCS also doesn't appear to drop events. Setting the DERRMR
9251 * to zero does lead to lockups within MI_DISPLAY_FLIP.
9252 */
9253 if (ring->id == RCS) {
9254 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
9255 intel_ring_emit(ring, DERRMR);
9256 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
9257 DERRMR_PIPEB_PRI_FLIP_DONE |
9258 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +01009259 if (IS_GEN8(dev))
9260 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
9261 MI_SRM_LRM_GLOBAL_GTT);
9262 else
9263 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
9264 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +01009265 intel_ring_emit(ring, DERRMR);
9266 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +01009267 if (IS_GEN8(dev)) {
9268 intel_ring_emit(ring, 0);
9269 intel_ring_emit(ring, MI_NOOP);
9270 }
Chris Wilsonffe74d72013-08-26 20:58:12 +01009271 }
9272
Daniel Vettercb05d8d2012-05-23 14:02:00 +02009273 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009274 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009275 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009276 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +00009277
9278 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +01009279 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +01009280 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -07009281}
9282
Sourab Gupta84c33a62014-06-02 16:47:17 +05309283static bool use_mmio_flip(struct intel_engine_cs *ring,
9284 struct drm_i915_gem_object *obj)
9285{
9286 /*
9287 * This is not being used for older platforms, because
9288 * non-availability of flip done interrupt forces us to use
9289 * CS flips. Older platforms derive flip done using some clever
9290 * tricks involving the flip_pending status bits and vblank irqs.
9291 * So using MMIO flips there would disrupt this mechanism.
9292 */
9293
Chris Wilson8e09bf82014-07-08 10:40:30 +01009294 if (ring == NULL)
9295 return true;
9296
Sourab Gupta84c33a62014-06-02 16:47:17 +05309297 if (INTEL_INFO(ring->dev)->gen < 5)
9298 return false;
9299
9300 if (i915.use_mmio_flip < 0)
9301 return false;
9302 else if (i915.use_mmio_flip > 0)
9303 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +01009304 else if (i915.enable_execlists)
9305 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +05309306 else
9307 return ring != obj->ring;
9308}
9309
9310static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
9311{
9312 struct drm_device *dev = intel_crtc->base.dev;
9313 struct drm_i915_private *dev_priv = dev->dev_private;
9314 struct intel_framebuffer *intel_fb =
9315 to_intel_framebuffer(intel_crtc->base.primary->fb);
9316 struct drm_i915_gem_object *obj = intel_fb->obj;
9317 u32 dspcntr;
9318 u32 reg;
9319
9320 intel_mark_page_flip_active(intel_crtc);
9321
9322 reg = DSPCNTR(intel_crtc->plane);
9323 dspcntr = I915_READ(reg);
9324
Damien Lespiauc5d97472014-10-25 00:11:11 +01009325 if (obj->tiling_mode != I915_TILING_NONE)
9326 dspcntr |= DISPPLANE_TILED;
9327 else
9328 dspcntr &= ~DISPPLANE_TILED;
9329
Sourab Gupta84c33a62014-06-02 16:47:17 +05309330 I915_WRITE(reg, dspcntr);
9331
9332 I915_WRITE(DSPSURF(intel_crtc->plane),
9333 intel_crtc->unpin_work->gtt_offset);
9334 POSTING_READ(DSPSURF(intel_crtc->plane));
9335}
9336
9337static int intel_postpone_flip(struct drm_i915_gem_object *obj)
9338{
9339 struct intel_engine_cs *ring;
9340 int ret;
9341
9342 lockdep_assert_held(&obj->base.dev->struct_mutex);
9343
9344 if (!obj->last_write_seqno)
9345 return 0;
9346
9347 ring = obj->ring;
9348
9349 if (i915_seqno_passed(ring->get_seqno(ring, true),
9350 obj->last_write_seqno))
9351 return 0;
9352
9353 ret = i915_gem_check_olr(ring, obj->last_write_seqno);
9354 if (ret)
9355 return ret;
9356
9357 if (WARN_ON(!ring->irq_get(ring)))
9358 return 0;
9359
9360 return 1;
9361}
9362
9363void intel_notify_mmio_flip(struct intel_engine_cs *ring)
9364{
9365 struct drm_i915_private *dev_priv = to_i915(ring->dev);
9366 struct intel_crtc *intel_crtc;
9367 unsigned long irq_flags;
9368 u32 seqno;
9369
9370 seqno = ring->get_seqno(ring, false);
9371
9372 spin_lock_irqsave(&dev_priv->mmio_flip_lock, irq_flags);
9373 for_each_intel_crtc(ring->dev, intel_crtc) {
9374 struct intel_mmio_flip *mmio_flip;
9375
9376 mmio_flip = &intel_crtc->mmio_flip;
9377 if (mmio_flip->seqno == 0)
9378 continue;
9379
9380 if (ring->id != mmio_flip->ring_id)
9381 continue;
9382
9383 if (i915_seqno_passed(seqno, mmio_flip->seqno)) {
9384 intel_do_mmio_flip(intel_crtc);
9385 mmio_flip->seqno = 0;
9386 ring->irq_put(ring);
9387 }
9388 }
9389 spin_unlock_irqrestore(&dev_priv->mmio_flip_lock, irq_flags);
9390}
9391
9392static int intel_queue_mmio_flip(struct drm_device *dev,
9393 struct drm_crtc *crtc,
9394 struct drm_framebuffer *fb,
9395 struct drm_i915_gem_object *obj,
9396 struct intel_engine_cs *ring,
9397 uint32_t flags)
9398{
9399 struct drm_i915_private *dev_priv = dev->dev_private;
9400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309401 int ret;
9402
9403 if (WARN_ON(intel_crtc->mmio_flip.seqno))
9404 return -EBUSY;
9405
9406 ret = intel_postpone_flip(obj);
9407 if (ret < 0)
9408 return ret;
9409 if (ret == 0) {
9410 intel_do_mmio_flip(intel_crtc);
9411 return 0;
9412 }
9413
Daniel Vetter24955f22014-09-15 14:55:32 +02009414 spin_lock_irq(&dev_priv->mmio_flip_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309415 intel_crtc->mmio_flip.seqno = obj->last_write_seqno;
9416 intel_crtc->mmio_flip.ring_id = obj->ring->id;
Daniel Vetter24955f22014-09-15 14:55:32 +02009417 spin_unlock_irq(&dev_priv->mmio_flip_lock);
Sourab Gupta84c33a62014-06-02 16:47:17 +05309418
9419 /*
9420 * Double check to catch cases where irq fired before
9421 * mmio flip data was ready
9422 */
9423 intel_notify_mmio_flip(obj->ring);
9424 return 0;
9425}
9426
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009427static int intel_default_queue_flip(struct drm_device *dev,
9428 struct drm_crtc *crtc,
9429 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009430 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009431 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -07009432 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009433{
9434 return -ENODEV;
9435}
9436
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009437static bool __intel_pageflip_stall_check(struct drm_device *dev,
9438 struct drm_crtc *crtc)
9439{
9440 struct drm_i915_private *dev_priv = dev->dev_private;
9441 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9442 struct intel_unpin_work *work = intel_crtc->unpin_work;
9443 u32 addr;
9444
9445 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
9446 return true;
9447
9448 if (!work->enable_stall_check)
9449 return false;
9450
9451 if (work->flip_ready_vblank == 0) {
9452 if (work->flip_queued_ring &&
9453 !i915_seqno_passed(work->flip_queued_ring->get_seqno(work->flip_queued_ring, true),
9454 work->flip_queued_seqno))
9455 return false;
9456
9457 work->flip_ready_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9458 }
9459
9460 if (drm_vblank_count(dev, intel_crtc->pipe) - work->flip_ready_vblank < 3)
9461 return false;
9462
9463 /* Potential stall - if we see that the flip has happened,
9464 * assume a missed interrupt. */
9465 if (INTEL_INFO(dev)->gen >= 4)
9466 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
9467 else
9468 addr = I915_READ(DSPADDR(intel_crtc->plane));
9469
9470 /* There is a potential issue here with a false positive after a flip
9471 * to the same address. We could address this by checking for a
9472 * non-incrementing frame counter.
9473 */
9474 return addr == work->gtt_offset;
9475}
9476
9477void intel_check_page_flip(struct drm_device *dev, int pipe)
9478{
9479 struct drm_i915_private *dev_priv = dev->dev_private;
9480 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
9481 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetterf3260382014-09-15 14:55:23 +02009482
9483 WARN_ON(!in_irq());
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009484
9485 if (crtc == NULL)
9486 return;
9487
Daniel Vetterf3260382014-09-15 14:55:23 +02009488 spin_lock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009489 if (intel_crtc->unpin_work && __intel_pageflip_stall_check(dev, crtc)) {
9490 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
9491 intel_crtc->unpin_work->flip_queued_vblank, drm_vblank_count(dev, pipe));
9492 page_flip_completed(intel_crtc);
9493 }
Daniel Vetterf3260382014-09-15 14:55:23 +02009494 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009495}
9496
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009497static int intel_crtc_page_flip(struct drm_crtc *crtc,
9498 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -07009499 struct drm_pending_vblank_event *event,
9500 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009501{
9502 struct drm_device *dev = crtc->dev;
9503 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -07009504 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009505 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009506 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettera071fa02014-06-18 23:28:09 +02009507 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009508 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +01009509 struct intel_engine_cs *ring;
Chris Wilson52e68632010-08-08 10:15:59 +01009510 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009511
Matt Roper2ff8fde2014-07-08 07:50:07 -07009512 /*
9513 * drm_mode_page_flip_ioctl() should already catch this, but double
9514 * check to be safe. In the future we may enable pageflipping from
9515 * a disabled primary plane.
9516 */
9517 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
9518 return -EBUSY;
9519
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009520 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -07009521 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009522 return -EINVAL;
9523
9524 /*
9525 * TILEOFF/LINOFF registers can't be changed via MI display flips.
9526 * Note that pitch changes could also affect these register.
9527 */
9528 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -07009529 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
9530 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +03009531 return -EINVAL;
9532
Chris Wilsonf900db42014-02-20 09:26:13 +00009533 if (i915_terminally_wedged(&dev_priv->gpu_error))
9534 goto out_hang;
9535
Daniel Vetterb14c5672013-09-19 12:18:32 +02009536 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009537 if (work == NULL)
9538 return -ENOMEM;
9539
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009540 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009541 work->crtc = crtc;
Matt Roper2ff8fde2014-07-08 07:50:07 -07009542 work->old_fb_obj = intel_fb_obj(old_fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009543 INIT_WORK(&work->work, intel_unpin_work_fn);
9544
Daniel Vetter87b6b102014-05-15 15:33:46 +02009545 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009546 if (ret)
9547 goto free_work;
9548
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009549 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009550 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009551 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009552 /* Before declaring the flip queue wedged, check if
9553 * the hardware completed the operation behind our backs.
9554 */
9555 if (__intel_pageflip_stall_check(dev, crtc)) {
9556 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
9557 page_flip_completed(intel_crtc);
9558 } else {
9559 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009560 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +01009561
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009562 drm_crtc_vblank_put(crtc);
9563 kfree(work);
9564 return -EBUSY;
9565 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009566 }
9567 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009568 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009569
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009570 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
9571 flush_workqueue(dev_priv->wq);
9572
Chris Wilson79158102012-05-23 11:13:58 +01009573 ret = i915_mutex_lock_interruptible(dev);
9574 if (ret)
9575 goto cleanup;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009576
Jesse Barnes75dfca82010-02-10 15:09:44 -08009577 /* Reference the objects for the scheduled work. */
Chris Wilson05394f32010-11-08 19:18:58 +00009578 drm_gem_object_reference(&work->old_fb_obj->base);
9579 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009580
Matt Roperf4510a22014-04-01 15:22:40 -07009581 crtc->primary->fb = fb;
Chris Wilson96b099f2010-06-07 14:03:04 +01009582
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009583 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009584
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009585 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +02009586 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +01009587
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009588 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +02009589 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +03009590
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009591 if (IS_VALLEYVIEW(dev)) {
9592 ring = &dev_priv->ring[BCS];
Chris Wilson8e09bf82014-07-08 10:40:30 +01009593 if (obj->tiling_mode != work->old_fb_obj->tiling_mode)
9594 /* vlv: DISPLAY_FLIP fails to change tiling */
9595 ring = NULL;
Chris Wilson2a92d5b2014-07-08 10:40:29 +01009596 } else if (IS_IVYBRIDGE(dev)) {
9597 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009598 } else if (INTEL_INFO(dev)->gen >= 7) {
9599 ring = obj->ring;
9600 if (ring == NULL || ring->id != RCS)
9601 ring = &dev_priv->ring[BCS];
9602 } else {
9603 ring = &dev_priv->ring[RCS];
9604 }
9605
9606 ret = intel_pin_and_fence_fb_obj(dev, obj, ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009607 if (ret)
9608 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009609
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009610 work->gtt_offset =
9611 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset;
9612
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009613 if (use_mmio_flip(ring, obj)) {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309614 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
9615 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009616 if (ret)
9617 goto cleanup_unpin;
9618
9619 work->flip_queued_seqno = obj->last_write_seqno;
9620 work->flip_queued_ring = obj->ring;
9621 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +05309622 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +01009623 page_flip_flags);
9624 if (ret)
9625 goto cleanup_unpin;
9626
9627 work->flip_queued_seqno = intel_ring_get_seqno(ring);
9628 work->flip_queued_ring = ring;
9629 }
9630
9631 work->flip_queued_vblank = drm_vblank_count(dev, intel_crtc->pipe);
9632 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009633
Daniel Vettera071fa02014-06-18 23:28:09 +02009634 i915_gem_track_fb(work->old_fb_obj, obj,
9635 INTEL_FRONTBUFFER_PRIMARY(pipe));
9636
Chris Wilson7782de32011-07-08 12:22:41 +01009637 intel_disable_fbc(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +02009638 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009639 mutex_unlock(&dev->struct_mutex);
9640
Jesse Barnese5510fa2010-07-01 16:48:37 -07009641 trace_i915_flip_request(intel_crtc->plane, obj);
9642
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009643 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +01009644
Ville Syrjälä4fa62c82014-04-15 21:41:38 +03009645cleanup_unpin:
9646 intel_unpin_fb_obj(obj);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -07009647cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +00009648 atomic_dec(&intel_crtc->unpin_work_count);
Matt Roperf4510a22014-04-01 15:22:40 -07009649 crtc->primary->fb = old_fb;
Chris Wilson05394f32010-11-08 19:18:58 +00009650 drm_gem_object_unreference(&work->old_fb_obj->base);
9651 drm_gem_object_unreference(&obj->base);
Chris Wilson96b099f2010-06-07 14:03:04 +01009652 mutex_unlock(&dev->struct_mutex);
9653
Chris Wilson79158102012-05-23 11:13:58 +01009654cleanup:
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009655 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009656 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009657 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +01009658
Daniel Vetter87b6b102014-05-15 15:33:46 +02009659 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -07009660free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +01009661 kfree(work);
9662
Chris Wilsonf900db42014-02-20 09:26:13 +00009663 if (ret == -EIO) {
9664out_hang:
9665 intel_crtc_wait_for_pending_flips(crtc);
9666 ret = intel_pipe_set_base(crtc, crtc->x, crtc->y, fb);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009667 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009668 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +02009669 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02009670 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +01009671 }
Chris Wilsonf900db42014-02-20 09:26:13 +00009672 }
Chris Wilson96b099f2010-06-07 14:03:04 +01009673 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05009674}
9675
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009676static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009677 .mode_set_base_atomic = intel_pipe_set_base_atomic,
9678 .load_lut = intel_crtc_load_lut,
Chris Wilsonf6e5b162011-04-12 18:06:51 +01009679};
9680
Daniel Vetter9a935852012-07-05 22:34:27 +02009681/**
9682 * intel_modeset_update_staged_output_state
9683 *
9684 * Updates the staged output configuration state, e.g. after we've read out the
9685 * current hw state.
9686 */
9687static void intel_modeset_update_staged_output_state(struct drm_device *dev)
9688{
Ville Syrjälä76688512014-01-10 11:28:06 +02009689 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009690 struct intel_encoder *encoder;
9691 struct intel_connector *connector;
9692
9693 list_for_each_entry(connector, &dev->mode_config.connector_list,
9694 base.head) {
9695 connector->new_encoder =
9696 to_intel_encoder(connector->base.encoder);
9697 }
9698
Damien Lespiaub2784e12014-08-05 11:29:37 +01009699 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009700 encoder->new_crtc =
9701 to_intel_crtc(encoder->base.crtc);
9702 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009703
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009704 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009705 crtc->new_enabled = crtc->base.enabled;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +02009706
9707 if (crtc->new_enabled)
9708 crtc->new_config = &crtc->config;
9709 else
9710 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +02009711 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009712}
9713
9714/**
9715 * intel_modeset_commit_output_state
9716 *
9717 * This function copies the stage display pipe configuration to the real one.
9718 */
9719static void intel_modeset_commit_output_state(struct drm_device *dev)
9720{
Ville Syrjälä76688512014-01-10 11:28:06 +02009721 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +02009722 struct intel_encoder *encoder;
9723 struct intel_connector *connector;
9724
9725 list_for_each_entry(connector, &dev->mode_config.connector_list,
9726 base.head) {
9727 connector->base.encoder = &connector->new_encoder->base;
9728 }
9729
Damien Lespiaub2784e12014-08-05 11:29:37 +01009730 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +02009731 encoder->base.crtc = &encoder->new_crtc->base;
9732 }
Ville Syrjälä76688512014-01-10 11:28:06 +02009733
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009734 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +02009735 crtc->base.enabled = crtc->new_enabled;
9736 }
Daniel Vetter9a935852012-07-05 22:34:27 +02009737}
9738
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009739static void
Robin Schroereba905b2014-05-18 02:24:50 +02009740connected_sink_compute_bpp(struct intel_connector *connector,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009741 struct intel_crtc_config *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009742{
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009743 int bpp = pipe_config->pipe_bpp;
9744
9745 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
9746 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03009747 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009748
9749 /* Don't use an invalid EDID bpc value */
9750 if (connector->base.display_info.bpc &&
9751 connector->base.display_info.bpc * 3 < bpp) {
9752 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
9753 bpp, connector->base.display_info.bpc*3);
9754 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
9755 }
9756
9757 /* Clamp bpp to 8 on screens without EDID 1.4 */
9758 if (connector->base.display_info.bpc == 0 && bpp > 24) {
9759 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
9760 bpp);
9761 pipe_config->pipe_bpp = 24;
9762 }
9763}
9764
9765static int
9766compute_baseline_pipe_bpp(struct intel_crtc *crtc,
9767 struct drm_framebuffer *fb,
9768 struct intel_crtc_config *pipe_config)
9769{
9770 struct drm_device *dev = crtc->base.dev;
9771 struct intel_connector *connector;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009772 int bpp;
9773
Daniel Vetterd42264b2013-03-28 16:38:08 +01009774 switch (fb->pixel_format) {
9775 case DRM_FORMAT_C8:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009776 bpp = 8*3; /* since we go through a colormap */
9777 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009778 case DRM_FORMAT_XRGB1555:
9779 case DRM_FORMAT_ARGB1555:
9780 /* checked in intel_framebuffer_init already */
9781 if (WARN_ON(INTEL_INFO(dev)->gen > 3))
9782 return -EINVAL;
9783 case DRM_FORMAT_RGB565:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009784 bpp = 6*3; /* min is 18bpp */
9785 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009786 case DRM_FORMAT_XBGR8888:
9787 case DRM_FORMAT_ABGR8888:
9788 /* checked in intel_framebuffer_init already */
9789 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
9790 return -EINVAL;
9791 case DRM_FORMAT_XRGB8888:
9792 case DRM_FORMAT_ARGB8888:
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009793 bpp = 8*3;
9794 break;
Daniel Vetterd42264b2013-03-28 16:38:08 +01009795 case DRM_FORMAT_XRGB2101010:
9796 case DRM_FORMAT_ARGB2101010:
9797 case DRM_FORMAT_XBGR2101010:
9798 case DRM_FORMAT_ABGR2101010:
9799 /* checked in intel_framebuffer_init already */
9800 if (WARN_ON(INTEL_INFO(dev)->gen < 4))
Daniel Vetterbaba1332013-03-27 00:45:00 +01009801 return -EINVAL;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009802 bpp = 10*3;
9803 break;
Daniel Vetterbaba1332013-03-27 00:45:00 +01009804 /* TODO: gen4+ supports 16 bpc floating point, too. */
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009805 default:
9806 DRM_DEBUG_KMS("unsupported depth\n");
9807 return -EINVAL;
9808 }
9809
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009810 pipe_config->pipe_bpp = bpp;
9811
9812 /* Clamp display bpp to EDID value */
9813 list_for_each_entry(connector, &dev->mode_config.connector_list,
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009814 base.head) {
Daniel Vetter1b829e02013-06-02 13:26:24 +02009815 if (!connector->new_encoder ||
9816 connector->new_encoder->new_crtc != crtc)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009817 continue;
9818
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009819 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009820 }
9821
9822 return bpp;
9823}
9824
Daniel Vetter644db712013-09-19 14:53:58 +02009825static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
9826{
9827 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
9828 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +01009829 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +02009830 mode->crtc_hdisplay, mode->crtc_hsync_start,
9831 mode->crtc_hsync_end, mode->crtc_htotal,
9832 mode->crtc_vdisplay, mode->crtc_vsync_start,
9833 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
9834}
9835
Daniel Vetterc0b03412013-05-28 12:05:54 +02009836static void intel_dump_pipe_config(struct intel_crtc *crtc,
9837 struct intel_crtc_config *pipe_config,
9838 const char *context)
9839{
9840 DRM_DEBUG_KMS("[CRTC:%d]%s config for pipe %c\n", crtc->base.base.id,
9841 context, pipe_name(crtc->pipe));
9842
9843 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
9844 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
9845 pipe_config->pipe_bpp, pipe_config->dither);
9846 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9847 pipe_config->has_pch_encoder,
9848 pipe_config->fdi_lanes,
9849 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
9850 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
9851 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009852 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
9853 pipe_config->has_dp_encoder,
9854 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
9855 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
9856 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009857
9858 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
9859 pipe_config->has_dp_encoder,
9860 pipe_config->dp_m2_n2.gmch_m,
9861 pipe_config->dp_m2_n2.gmch_n,
9862 pipe_config->dp_m2_n2.link_m,
9863 pipe_config->dp_m2_n2.link_n,
9864 pipe_config->dp_m2_n2.tu);
9865
Daniel Vetterc0b03412013-05-28 12:05:54 +02009866 DRM_DEBUG_KMS("requested mode:\n");
9867 drm_mode_debug_printmodeline(&pipe_config->requested_mode);
9868 DRM_DEBUG_KMS("adjusted mode:\n");
9869 drm_mode_debug_printmodeline(&pipe_config->adjusted_mode);
Daniel Vetter644db712013-09-19 14:53:58 +02009870 intel_dump_crtc_timings(&pipe_config->adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +03009871 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009872 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
9873 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009874 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
9875 pipe_config->gmch_pfit.control,
9876 pipe_config->gmch_pfit.pgm_ratios,
9877 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009878 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +02009879 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009880 pipe_config->pch_pfit.size,
9881 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009882 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03009883 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Daniel Vetterc0b03412013-05-28 12:05:54 +02009884}
9885
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009886static bool encoders_cloneable(const struct intel_encoder *a,
9887 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009888{
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009889 /* masks could be asymmetric, so check both ways */
9890 return a == b || (a->cloneable & (1 << b->type) &&
9891 b->cloneable & (1 << a->type));
9892}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009893
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009894static bool check_single_encoder_cloning(struct intel_crtc *crtc,
9895 struct intel_encoder *encoder)
9896{
9897 struct drm_device *dev = crtc->base.dev;
9898 struct intel_encoder *source_encoder;
9899
Damien Lespiaub2784e12014-08-05 11:29:37 +01009900 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009901 if (source_encoder->new_crtc != crtc)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009902 continue;
9903
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009904 if (!encoders_cloneable(encoder, source_encoder))
9905 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009906 }
9907
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009908 return true;
9909}
9910
9911static bool check_encoder_cloning(struct intel_crtc *crtc)
9912{
9913 struct drm_device *dev = crtc->base.dev;
9914 struct intel_encoder *encoder;
9915
Damien Lespiaub2784e12014-08-05 11:29:37 +01009916 for_each_intel_encoder(dev, encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009917 if (encoder->new_crtc != crtc)
9918 continue;
9919
9920 if (!check_single_encoder_cloning(crtc, encoder))
9921 return false;
9922 }
9923
9924 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009925}
9926
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009927static struct intel_crtc_config *
9928intel_modeset_pipe_config(struct drm_crtc *crtc,
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009929 struct drm_framebuffer *fb,
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009930 struct drm_display_mode *mode)
Daniel Vetter7758a112012-07-08 19:40:39 +02009931{
9932 struct drm_device *dev = crtc->dev;
Daniel Vetter7758a112012-07-08 19:40:39 +02009933 struct intel_encoder *encoder;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009934 struct intel_crtc_config *pipe_config;
Daniel Vettere29c22c2013-02-21 00:00:16 +01009935 int plane_bpp, ret = -EINVAL;
9936 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +02009937
Ville Syrjäläbc079e82014-03-03 16:15:28 +02009938 if (!check_encoder_cloning(to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +02009939 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
9940 return ERR_PTR(-EINVAL);
9941 }
9942
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009943 pipe_config = kzalloc(sizeof(*pipe_config), GFP_KERNEL);
9944 if (!pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +02009945 return ERR_PTR(-ENOMEM);
9946
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009947 drm_mode_copy(&pipe_config->adjusted_mode, mode);
9948 drm_mode_copy(&pipe_config->requested_mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +03009949
Daniel Vettere143a212013-07-04 12:01:15 +02009950 pipe_config->cpu_transcoder =
9951 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009952 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +01009953
Imre Deak2960bc92013-07-30 13:36:32 +03009954 /*
9955 * Sanitize sync polarity flags based on requested ones. If neither
9956 * positive or negative polarity is requested, treat this as meaning
9957 * negative polarity.
9958 */
9959 if (!(pipe_config->adjusted_mode.flags &
9960 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
9961 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
9962
9963 if (!(pipe_config->adjusted_mode.flags &
9964 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
9965 pipe_config->adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
9966
Daniel Vetter050f7ae2013-06-02 13:26:23 +02009967 /* Compute a starting value for pipe_config->pipe_bpp taking the source
9968 * plane pixel format and any sink constraints into account. Returns the
9969 * source plane bpp so that dithering can be selected on mismatches
9970 * after encoders and crtc also have had their say. */
9971 plane_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
9972 fb, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +01009973 if (plane_bpp < 0)
9974 goto fail;
9975
Ville Syrjäläe41a56b2013-10-01 22:52:14 +03009976 /*
9977 * Determine the real pipe dimensions. Note that stereo modes can
9978 * increase the actual pipe size due to the frame doubling and
9979 * insertion of additional space for blanks between the frame. This
9980 * is stored in the crtc timings. We use the requested mode to do this
9981 * computation to clearly distinguish it from the adjusted mode, which
9982 * can be changed by the connectors in the below retry loop.
9983 */
9984 drm_mode_set_crtcinfo(&pipe_config->requested_mode, CRTC_STEREO_DOUBLE);
9985 pipe_config->pipe_src_w = pipe_config->requested_mode.crtc_hdisplay;
9986 pipe_config->pipe_src_h = pipe_config->requested_mode.crtc_vdisplay;
9987
Daniel Vettere29c22c2013-02-21 00:00:16 +01009988encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +02009989 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +02009990 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +02009991 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +02009992
Daniel Vetter135c81b2013-07-21 21:37:09 +02009993 /* Fill in default crtc timings, allow encoders to overwrite them. */
Damien Lespiau6ce70f52013-09-25 16:45:38 +01009994 drm_mode_set_crtcinfo(&pipe_config->adjusted_mode, CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +02009995
Daniel Vetter7758a112012-07-08 19:40:39 +02009996 /* Pass our mode to the connectors and the CRTC to give them a chance to
9997 * adjust it according to limitations or connector properties, and also
9998 * a chance to reject the mode entirely.
9999 */
Damien Lespiaub2784e12014-08-05 11:29:37 +010010000 for_each_intel_encoder(dev, encoder) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010001
10002 if (&encoder->new_crtc->base != crtc)
10003 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010010004
Daniel Vetterefea6e82013-07-21 21:36:59 +020010005 if (!(encoder->compute_config(encoder, pipe_config))) {
10006 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020010007 goto fail;
10008 }
10009 }
10010
Daniel Vetterff9a6752013-06-01 17:16:21 +020010011 /* Set default port clock if not overwritten by the encoder. Needs to be
10012 * done afterwards in case the encoder adjusts the mode. */
10013 if (!pipe_config->port_clock)
Damien Lespiau241bfc32013-09-25 16:45:37 +010010014 pipe_config->port_clock = pipe_config->adjusted_mode.crtc_clock
10015 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020010016
Daniel Vettera43f6e02013-06-07 23:10:32 +020010017 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010018 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020010019 DRM_DEBUG_KMS("CRTC fixup failed\n");
10020 goto fail;
10021 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010010022
10023 if (ret == RETRY) {
10024 if (WARN(!retry, "loop in pipe configuration computation\n")) {
10025 ret = -EINVAL;
10026 goto fail;
10027 }
10028
10029 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
10030 retry = false;
10031 goto encoder_retry;
10032 }
10033
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010034 pipe_config->dither = pipe_config->pipe_bpp != plane_bpp;
10035 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
10036 plane_bpp, pipe_config->pipe_bpp, pipe_config->dither);
10037
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010038 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020010039fail:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010040 kfree(pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010010041 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020010042}
10043
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010044/* Computes which crtcs are affected and sets the relevant bits in the mask. For
10045 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
10046static void
10047intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
10048 unsigned *prepare_pipes, unsigned *disable_pipes)
10049{
10050 struct intel_crtc *intel_crtc;
10051 struct drm_device *dev = crtc->dev;
10052 struct intel_encoder *encoder;
10053 struct intel_connector *connector;
10054 struct drm_crtc *tmp_crtc;
10055
10056 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
10057
10058 /* Check which crtcs have changed outputs connected to them, these need
10059 * to be part of the prepare_pipes mask. We don't (yet) support global
10060 * modeset across multiple crtcs, so modeset_pipes will only have one
10061 * bit set at most. */
10062 list_for_each_entry(connector, &dev->mode_config.connector_list,
10063 base.head) {
10064 if (connector->base.encoder == &connector->new_encoder->base)
10065 continue;
10066
10067 if (connector->base.encoder) {
10068 tmp_crtc = connector->base.encoder->crtc;
10069
10070 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10071 }
10072
10073 if (connector->new_encoder)
10074 *prepare_pipes |=
10075 1 << connector->new_encoder->new_crtc->pipe;
10076 }
10077
Damien Lespiaub2784e12014-08-05 11:29:37 +010010078 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010079 if (encoder->base.crtc == &encoder->new_crtc->base)
10080 continue;
10081
10082 if (encoder->base.crtc) {
10083 tmp_crtc = encoder->base.crtc;
10084
10085 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
10086 }
10087
10088 if (encoder->new_crtc)
10089 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
10090 }
10091
Ville Syrjälä76688512014-01-10 11:28:06 +020010092 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010093 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010094 if (intel_crtc->base.enabled == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010095 continue;
10096
Ville Syrjälä76688512014-01-10 11:28:06 +020010097 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010098 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020010099 else
10100 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010101 }
10102
10103
10104 /* set_mode is also used to update properties on life display pipes. */
10105 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020010106 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010107 *prepare_pipes |= 1 << intel_crtc->pipe;
10108
Daniel Vetterb6c51642013-04-12 18:48:43 +020010109 /*
10110 * For simplicity do a full modeset on any pipe where the output routing
10111 * changed. We could be more clever, but that would require us to be
10112 * more careful with calling the relevant encoder->mode_set functions.
10113 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010114 if (*prepare_pipes)
10115 *modeset_pipes = *prepare_pipes;
10116
10117 /* ... and mask these out. */
10118 *modeset_pipes &= ~(*disable_pipes);
10119 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020010120
10121 /*
10122 * HACK: We don't (yet) fully support global modesets. intel_set_config
10123 * obies this rule, but the modeset restore mode of
10124 * intel_modeset_setup_hw_state does not.
10125 */
10126 *modeset_pipes &= 1 << intel_crtc->pipe;
10127 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020010128
10129 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
10130 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010131}
10132
Daniel Vetterea9d7582012-07-10 10:42:52 +020010133static bool intel_crtc_in_use(struct drm_crtc *crtc)
10134{
10135 struct drm_encoder *encoder;
10136 struct drm_device *dev = crtc->dev;
10137
10138 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
10139 if (encoder->crtc == crtc)
10140 return true;
10141
10142 return false;
10143}
10144
10145static void
10146intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
10147{
10148 struct intel_encoder *intel_encoder;
10149 struct intel_crtc *intel_crtc;
10150 struct drm_connector *connector;
10151
Damien Lespiaub2784e12014-08-05 11:29:37 +010010152 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020010153 if (!intel_encoder->base.crtc)
10154 continue;
10155
10156 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
10157
10158 if (prepare_pipes & (1 << intel_crtc->pipe))
10159 intel_encoder->connectors_active = false;
10160 }
10161
10162 intel_modeset_commit_output_state(dev);
10163
Ville Syrjälä76688512014-01-10 11:28:06 +020010164 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010165 for_each_intel_crtc(dev, intel_crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010166 WARN_ON(intel_crtc->base.enabled != intel_crtc_in_use(&intel_crtc->base));
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010167 WARN_ON(intel_crtc->new_config &&
10168 intel_crtc->new_config != &intel_crtc->config);
10169 WARN_ON(intel_crtc->base.enabled != !!intel_crtc->new_config);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010170 }
10171
10172 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
10173 if (!connector->encoder || !connector->encoder->crtc)
10174 continue;
10175
10176 intel_crtc = to_intel_crtc(connector->encoder->crtc);
10177
10178 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020010179 struct drm_property *dpms_property =
10180 dev->mode_config.dpms_property;
10181
Daniel Vetterea9d7582012-07-10 10:42:52 +020010182 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050010183 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020010184 dpms_property,
10185 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020010186
10187 intel_encoder = to_intel_encoder(connector->encoder);
10188 intel_encoder->connectors_active = true;
10189 }
10190 }
10191
10192}
10193
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010194static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010195{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030010196 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010197
10198 if (clock1 == clock2)
10199 return true;
10200
10201 if (!clock1 || !clock2)
10202 return false;
10203
10204 diff = abs(clock1 - clock2);
10205
10206 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
10207 return true;
10208
10209 return false;
10210}
10211
Daniel Vetter25c5b262012-07-08 22:08:04 +020010212#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
10213 list_for_each_entry((intel_crtc), \
10214 &(dev)->mode_config.crtc_list, \
10215 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020010216 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020010217
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010218static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010219intel_pipe_config_compare(struct drm_device *dev,
10220 struct intel_crtc_config *current_config,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010221 struct intel_crtc_config *pipe_config)
10222{
Daniel Vetter66e985c2013-06-05 13:34:20 +020010223#define PIPE_CONF_CHECK_X(name) \
10224 if (current_config->name != pipe_config->name) { \
10225 DRM_ERROR("mismatch in " #name " " \
10226 "(expected 0x%08x, found 0x%08x)\n", \
10227 current_config->name, \
10228 pipe_config->name); \
10229 return false; \
10230 }
10231
Daniel Vetter08a24032013-04-19 11:25:34 +020010232#define PIPE_CONF_CHECK_I(name) \
10233 if (current_config->name != pipe_config->name) { \
10234 DRM_ERROR("mismatch in " #name " " \
10235 "(expected %i, found %i)\n", \
10236 current_config->name, \
10237 pipe_config->name); \
10238 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010010239 }
10240
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010241/* This is required for BDW+ where there is only one set of registers for
10242 * switching between high and low RR.
10243 * This macro can be used whenever a comparison has to be made between one
10244 * hw state and multiple sw state variables.
10245 */
10246#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
10247 if ((current_config->name != pipe_config->name) && \
10248 (current_config->alt_name != pipe_config->name)) { \
10249 DRM_ERROR("mismatch in " #name " " \
10250 "(expected %i or %i, found %i)\n", \
10251 current_config->name, \
10252 current_config->alt_name, \
10253 pipe_config->name); \
10254 return false; \
10255 }
10256
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010257#define PIPE_CONF_CHECK_FLAGS(name, mask) \
10258 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070010259 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010260 "(expected %i, found %i)\n", \
10261 current_config->name & (mask), \
10262 pipe_config->name & (mask)); \
10263 return false; \
10264 }
10265
Ville Syrjälä5e550652013-09-06 23:29:07 +030010266#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
10267 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
10268 DRM_ERROR("mismatch in " #name " " \
10269 "(expected %i, found %i)\n", \
10270 current_config->name, \
10271 pipe_config->name); \
10272 return false; \
10273 }
10274
Daniel Vetterbb760062013-06-06 14:55:52 +020010275#define PIPE_CONF_QUIRK(quirk) \
10276 ((current_config->quirks | pipe_config->quirks) & (quirk))
10277
Daniel Vettereccb1402013-05-22 00:50:22 +020010278 PIPE_CONF_CHECK_I(cpu_transcoder);
10279
Daniel Vetter08a24032013-04-19 11:25:34 +020010280 PIPE_CONF_CHECK_I(has_pch_encoder);
10281 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020010282 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
10283 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
10284 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
10285 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
10286 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020010287
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010288 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010289
10290 if (INTEL_INFO(dev)->gen < 8) {
10291 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
10292 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
10293 PIPE_CONF_CHECK_I(dp_m_n.link_m);
10294 PIPE_CONF_CHECK_I(dp_m_n.link_n);
10295 PIPE_CONF_CHECK_I(dp_m_n.tu);
10296
10297 if (current_config->has_drrs) {
10298 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
10299 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
10300 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
10301 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
10302 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
10303 }
10304 } else {
10305 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
10306 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
10307 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
10308 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
10309 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
10310 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030010311
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010312 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hdisplay);
10313 PIPE_CONF_CHECK_I(adjusted_mode.crtc_htotal);
10314 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_start);
10315 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hblank_end);
10316 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_start);
10317 PIPE_CONF_CHECK_I(adjusted_mode.crtc_hsync_end);
10318
10319 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vdisplay);
10320 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vtotal);
10321 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_start);
10322 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vblank_end);
10323 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_start);
10324 PIPE_CONF_CHECK_I(adjusted_mode.crtc_vsync_end);
10325
Daniel Vetterc93f54c2013-06-27 19:47:19 +020010326 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020010327 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020010328 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
10329 IS_VALLEYVIEW(dev))
10330 PIPE_CONF_CHECK_I(limited_color_range);
Daniel Vetter6c49f242013-06-06 12:45:25 +020010331
Daniel Vetter9ed109a2014-04-24 23:54:52 +020010332 PIPE_CONF_CHECK_I(has_audio);
10333
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010334 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10335 DRM_MODE_FLAG_INTERLACE);
10336
Daniel Vetterbb760062013-06-06 14:55:52 +020010337 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
10338 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10339 DRM_MODE_FLAG_PHSYNC);
10340 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10341 DRM_MODE_FLAG_NHSYNC);
10342 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10343 DRM_MODE_FLAG_PVSYNC);
10344 PIPE_CONF_CHECK_FLAGS(adjusted_mode.flags,
10345 DRM_MODE_FLAG_NVSYNC);
10346 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010347
Ville Syrjälä37327ab2013-09-04 18:25:28 +030010348 PIPE_CONF_CHECK_I(pipe_src_w);
10349 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010350
Daniel Vetter99535992014-04-13 12:00:33 +020010351 /*
10352 * FIXME: BIOS likes to set up a cloned config with lvds+external
10353 * screen. Since we don't yet re-compute the pipe config when moving
10354 * just the lvds port away to another pipe the sw tracking won't match.
10355 *
10356 * Proper atomic modesets with recomputed global state will fix this.
10357 * Until then just don't check gmch state for inherited modes.
10358 */
10359 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
10360 PIPE_CONF_CHECK_I(gmch_pfit.control);
10361 /* pfit ratios are autocomputed by the hw on gen4+ */
10362 if (INTEL_INFO(dev)->gen < 4)
10363 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
10364 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
10365 }
10366
Chris Wilsonfd4daa92013-08-27 17:04:17 +010010367 PIPE_CONF_CHECK_I(pch_pfit.enabled);
10368 if (current_config->pch_pfit.enabled) {
10369 PIPE_CONF_CHECK_I(pch_pfit.pos);
10370 PIPE_CONF_CHECK_I(pch_pfit.size);
10371 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020010372
Jesse Barnese59150d2014-01-07 13:30:45 -080010373 /* BDW+ don't expose a synchronous way to read the state */
10374 if (IS_HASWELL(dev))
10375 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010376
Ville Syrjälä282740f2013-09-04 18:30:03 +030010377 PIPE_CONF_CHECK_I(double_wide);
10378
Daniel Vetter26804af2014-06-25 22:01:55 +030010379 PIPE_CONF_CHECK_X(ddi_pll_sel);
10380
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010381 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010382 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020010383 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010384 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
10385 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030010386 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020010387
Ville Syrjälä42571ae2013-09-06 23:29:00 +030010388 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
10389 PIPE_CONF_CHECK_I(pipe_bpp);
10390
Jesse Barnesa9a7e982014-01-20 14:18:04 -080010391 PIPE_CONF_CHECK_CLOCK_FUZZY(adjusted_mode.crtc_clock);
10392 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030010393
Daniel Vetter66e985c2013-06-05 13:34:20 +020010394#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020010395#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070010396#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020010397#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030010398#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020010399#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020010400
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010401 return true;
10402}
10403
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010404static void
10405check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010406{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010407 struct intel_connector *connector;
10408
10409 list_for_each_entry(connector, &dev->mode_config.connector_list,
10410 base.head) {
10411 /* This also checks the encoder/connector hw state with the
10412 * ->get_hw_state callbacks. */
10413 intel_connector_check_state(connector);
10414
10415 WARN(&connector->new_encoder->base != connector->base.encoder,
10416 "connector's staged encoder doesn't match current encoder\n");
10417 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010418}
10419
10420static void
10421check_encoder_state(struct drm_device *dev)
10422{
10423 struct intel_encoder *encoder;
10424 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010425
Damien Lespiaub2784e12014-08-05 11:29:37 +010010426 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010427 bool enabled = false;
10428 bool active = false;
10429 enum pipe pipe, tracked_pipe;
10430
10431 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
10432 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030010433 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010434
10435 WARN(&encoder->new_crtc->base != encoder->base.crtc,
10436 "encoder's stage crtc doesn't match current crtc\n");
10437 WARN(encoder->connectors_active && !encoder->base.crtc,
10438 "encoder's active_connectors set, but no crtc\n");
10439
10440 list_for_each_entry(connector, &dev->mode_config.connector_list,
10441 base.head) {
10442 if (connector->base.encoder != &encoder->base)
10443 continue;
10444 enabled = true;
10445 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
10446 active = true;
10447 }
Dave Airlie0e32b392014-05-02 14:02:48 +100010448 /*
10449 * for MST connectors if we unplug the connector is gone
10450 * away but the encoder is still connected to a crtc
10451 * until a modeset happens in response to the hotplug.
10452 */
10453 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
10454 continue;
10455
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010456 WARN(!!encoder->base.crtc != enabled,
10457 "encoder's enabled state mismatch "
10458 "(expected %i, found %i)\n",
10459 !!encoder->base.crtc, enabled);
10460 WARN(active && !encoder->base.crtc,
10461 "active encoder with no crtc\n");
10462
10463 WARN(encoder->connectors_active != active,
10464 "encoder's computed active state doesn't match tracked active state "
10465 "(expected %i, found %i)\n", active, encoder->connectors_active);
10466
10467 active = encoder->get_hw_state(encoder, &pipe);
10468 WARN(active != encoder->connectors_active,
10469 "encoder's hw state doesn't match sw tracking "
10470 "(expected %i, found %i)\n",
10471 encoder->connectors_active, active);
10472
10473 if (!encoder->base.crtc)
10474 continue;
10475
10476 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
10477 WARN(active && pipe != tracked_pipe,
10478 "active encoder's pipe doesn't match"
10479 "(expected %i, found %i)\n",
10480 tracked_pipe, pipe);
10481
10482 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010483}
10484
10485static void
10486check_crtc_state(struct drm_device *dev)
10487{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010488 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010489 struct intel_crtc *crtc;
10490 struct intel_encoder *encoder;
10491 struct intel_crtc_config pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010492
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010493 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010494 bool enabled = false;
10495 bool active = false;
10496
Jesse Barnes045ac3b2013-05-14 17:08:26 -070010497 memset(&pipe_config, 0, sizeof(pipe_config));
10498
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010499 DRM_DEBUG_KMS("[CRTC:%d]\n",
10500 crtc->base.base.id);
10501
10502 WARN(crtc->active && !crtc->base.enabled,
10503 "active crtc, but not enabled in sw tracking\n");
10504
Damien Lespiaub2784e12014-08-05 11:29:37 +010010505 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010506 if (encoder->base.crtc != &crtc->base)
10507 continue;
10508 enabled = true;
10509 if (encoder->connectors_active)
10510 active = true;
10511 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010512
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010513 WARN(active != crtc->active,
10514 "crtc's computed active state doesn't match tracked active state "
10515 "(expected %i, found %i)\n", active, crtc->active);
10516 WARN(enabled != crtc->base.enabled,
10517 "crtc's computed enabled state doesn't match tracked enabled state "
10518 "(expected %i, found %i)\n", enabled, crtc->base.enabled);
10519
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010520 active = dev_priv->display.get_pipe_config(crtc,
10521 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020010522
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030010523 /* hw state is inconsistent with the pipe quirk */
10524 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
10525 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020010526 active = crtc->active;
10527
Damien Lespiaub2784e12014-08-05 11:29:37 +010010528 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030010529 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020010530 if (encoder->base.crtc != &crtc->base)
10531 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010010532 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020010533 encoder->get_config(encoder, &pipe_config);
10534 }
10535
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010536 WARN(crtc->active != active,
10537 "crtc active state doesn't match with hw state "
10538 "(expected %i, found %i)\n", crtc->active, active);
10539
Daniel Vetterc0b03412013-05-28 12:05:54 +020010540 if (active &&
10541 !intel_pipe_config_compare(dev, &crtc->config, &pipe_config)) {
10542 WARN(1, "pipe state doesn't match!\n");
10543 intel_dump_pipe_config(crtc, &pipe_config,
10544 "[hw state]");
10545 intel_dump_pipe_config(crtc, &crtc->config,
10546 "[sw state]");
10547 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020010548 }
10549}
10550
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010551static void
10552check_shared_dpll_state(struct drm_device *dev)
10553{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010554 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010555 struct intel_crtc *crtc;
10556 struct intel_dpll_hw_state dpll_hw_state;
10557 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020010558
10559 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
10560 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
10561 int enabled_crtcs = 0, active_crtcs = 0;
10562 bool active;
10563
10564 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
10565
10566 DRM_DEBUG_KMS("%s\n", pll->name);
10567
10568 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
10569
10570 WARN(pll->active > pll->refcount,
10571 "more active pll users than references: %i vs %i\n",
10572 pll->active, pll->refcount);
10573 WARN(pll->active && !pll->on,
10574 "pll in active use but not on in sw tracking\n");
Daniel Vetter35c95372013-07-17 06:55:04 +020010575 WARN(pll->on && !pll->active,
10576 "pll in on but not on in use in sw tracking\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010577 WARN(pll->on != active,
10578 "pll on state mismatch (expected %i, found %i)\n",
10579 pll->on, active);
10580
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010581 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020010582 if (crtc->base.enabled && intel_crtc_to_shared_dpll(crtc) == pll)
10583 enabled_crtcs++;
10584 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
10585 active_crtcs++;
10586 }
10587 WARN(pll->active != active_crtcs,
10588 "pll active crtcs mismatch (expected %i, found %i)\n",
10589 pll->active, active_crtcs);
10590 WARN(pll->refcount != enabled_crtcs,
10591 "pll enabled crtcs mismatch (expected %i, found %i)\n",
10592 pll->refcount, enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020010593
10594 WARN(pll->on && memcmp(&pll->hw_state, &dpll_hw_state,
10595 sizeof(dpll_hw_state)),
10596 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020010597 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010598}
10599
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020010600void
10601intel_modeset_check_state(struct drm_device *dev)
10602{
10603 check_connector_state(dev);
10604 check_encoder_state(dev);
10605 check_crtc_state(dev);
10606 check_shared_dpll_state(dev);
10607}
10608
Ville Syrjälä18442d02013-09-13 16:00:08 +030010609void ironlake_check_encoder_dotclock(const struct intel_crtc_config *pipe_config,
10610 int dotclock)
10611{
10612 /*
10613 * FDI already provided one idea for the dotclock.
10614 * Yell if the encoder disagrees.
10615 */
Damien Lespiau241bfc32013-09-25 16:45:37 +010010616 WARN(!intel_fuzzy_clock_check(pipe_config->adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030010617 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Damien Lespiau241bfc32013-09-25 16:45:37 +010010618 pipe_config->adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030010619}
10620
Ville Syrjälä80715b22014-05-15 20:23:23 +030010621static void update_scanline_offset(struct intel_crtc *crtc)
10622{
10623 struct drm_device *dev = crtc->base.dev;
10624
10625 /*
10626 * The scanline counter increments at the leading edge of hsync.
10627 *
10628 * On most platforms it starts counting from vtotal-1 on the
10629 * first active line. That means the scanline counter value is
10630 * always one less than what we would expect. Ie. just after
10631 * start of vblank, which also occurs at start of hsync (on the
10632 * last active line), the scanline counter will read vblank_start-1.
10633 *
10634 * On gen2 the scanline counter starts counting from 1 instead
10635 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
10636 * to keep the value positive), instead of adding one.
10637 *
10638 * On HSW+ the behaviour of the scanline counter depends on the output
10639 * type. For DP ports it behaves like most other platforms, but on HDMI
10640 * there's an extra 1 line difference. So we need to add two instead of
10641 * one to the value.
10642 */
10643 if (IS_GEN2(dev)) {
10644 const struct drm_display_mode *mode = &crtc->config.adjusted_mode;
10645 int vtotal;
10646
10647 vtotal = mode->crtc_vtotal;
10648 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
10649 vtotal /= 2;
10650
10651 crtc->scanline_offset = vtotal - 1;
10652 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030010653 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030010654 crtc->scanline_offset = 2;
10655 } else
10656 crtc->scanline_offset = 1;
10657}
10658
Daniel Vetterf30da182013-04-11 20:22:50 +020010659static int __intel_set_mode(struct drm_crtc *crtc,
10660 struct drm_display_mode *mode,
10661 int x, int y, struct drm_framebuffer *fb)
Daniel Vettera6778b32012-07-02 09:56:42 +020010662{
10663 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010664 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010665 struct drm_display_mode *saved_mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010666 struct intel_crtc_config *pipe_config = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010667 struct intel_crtc *intel_crtc;
10668 unsigned disable_pipes, prepare_pipes, modeset_pipes;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010669 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020010670
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010671 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010672 if (!saved_mode)
10673 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020010674
Daniel Vettere2e1ed42012-07-08 21:14:38 +020010675 intel_modeset_affected_pipes(crtc, &modeset_pipes,
Daniel Vetter25c5b262012-07-08 22:08:04 +020010676 &prepare_pipes, &disable_pipes);
10677
Tim Gardner3ac18232012-12-07 07:54:26 -070010678 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010679
Daniel Vetter25c5b262012-07-08 22:08:04 +020010680 /* Hack: Because we don't (yet) support global modeset on multiple
10681 * crtcs, we don't keep track of the new mode for more than one crtc.
10682 * Hence simply check whether any bit is set in modeset_pipes in all the
10683 * pieces of code that are not yet converted to deal with mutliple crtcs
10684 * changing their mode at the same time. */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010685 if (modeset_pipes) {
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010686 pipe_config = intel_modeset_pipe_config(crtc, fb, mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010687 if (IS_ERR(pipe_config)) {
10688 ret = PTR_ERR(pipe_config);
10689 pipe_config = NULL;
10690
Tim Gardner3ac18232012-12-07 07:54:26 -070010691 goto out;
Daniel Vetter25c5b262012-07-08 22:08:04 +020010692 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020010693 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
10694 "[modeset]");
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010695 to_intel_crtc(crtc)->new_config = pipe_config;
Daniel Vettera6778b32012-07-02 09:56:42 +020010696 }
10697
Jesse Barnes30a970c2013-11-04 13:48:12 -080010698 /*
10699 * See if the config requires any additional preparation, e.g.
10700 * to adjust global state with pipes off. We need to do this
10701 * here so we can get the modeset_pipe updated config for the new
10702 * mode set on this crtc. For other crtcs we need to use the
10703 * adjusted_mode bits in the crtc directly.
10704 */
Ville Syrjäläc164f832013-11-05 22:34:12 +020010705 if (IS_VALLEYVIEW(dev)) {
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +020010706 valleyview_modeset_global_pipes(dev, &prepare_pipes);
Jesse Barnes30a970c2013-11-04 13:48:12 -080010707
Ville Syrjäläc164f832013-11-05 22:34:12 +020010708 /* may have added more to prepare_pipes than we should */
10709 prepare_pipes &= ~disable_pipes;
10710 }
10711
Daniel Vetter460da9162013-03-27 00:44:51 +010010712 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
10713 intel_crtc_disable(&intel_crtc->base);
10714
Daniel Vetterea9d7582012-07-10 10:42:52 +020010715 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10716 if (intel_crtc->base.enabled)
10717 dev_priv->display.crtc_disable(&intel_crtc->base);
10718 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010719
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020010720 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
10721 * to set it here already despite that we pass it down the callchain.
10722 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010723 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020010724 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010725 /* mode_set/enable/disable functions rely on a correct pipe
10726 * config. */
10727 to_intel_crtc(crtc)->config = *pipe_config;
Ville Syrjälä50741ab2014-01-10 11:28:07 +020010728 to_intel_crtc(crtc)->new_config = &to_intel_crtc(crtc)->config;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020010729
10730 /*
10731 * Calculate and store various constants which
10732 * are later needed by vblank and swap-completion
10733 * timestamping. They are derived from true hwmode.
10734 */
10735 drm_calc_timestamping_constants(crtc,
10736 &pipe_config->adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010737 }
Daniel Vetter7758a112012-07-08 19:40:39 +020010738
Daniel Vetterea9d7582012-07-10 10:42:52 +020010739 /* Only after disabling all output pipelines that will be changed can we
10740 * update the the output configuration. */
10741 intel_modeset_update_state(dev, prepare_pipes);
10742
Daniel Vetter47fab732012-10-26 10:58:18 +020010743 if (dev_priv->display.modeset_global_resources)
10744 dev_priv->display.modeset_global_resources(dev);
10745
Daniel Vettera6778b32012-07-02 09:56:42 +020010746 /* Set up the DPLL and any encoders state that needs to adjust or depend
10747 * on the DPLL.
10748 */
Daniel Vetter25c5b262012-07-08 22:08:04 +020010749 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070010750 struct drm_framebuffer *old_fb = crtc->primary->fb;
10751 struct drm_i915_gem_object *old_obj = intel_fb_obj(old_fb);
10752 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Daniel Vetter4c107942014-04-24 23:55:05 +020010753
10754 mutex_lock(&dev->struct_mutex);
10755 ret = intel_pin_and_fence_fb_obj(dev,
Daniel Vettera071fa02014-06-18 23:28:09 +020010756 obj,
Daniel Vetter4c107942014-04-24 23:55:05 +020010757 NULL);
10758 if (ret != 0) {
10759 DRM_ERROR("pin & fence failed\n");
10760 mutex_unlock(&dev->struct_mutex);
10761 goto done;
10762 }
Matt Roper2ff8fde2014-07-08 07:50:07 -070010763 if (old_fb)
Daniel Vettera071fa02014-06-18 23:28:09 +020010764 intel_unpin_fb_obj(old_obj);
Daniel Vettera071fa02014-06-18 23:28:09 +020010765 i915_gem_track_fb(old_obj, obj,
10766 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Daniel Vetter4c107942014-04-24 23:55:05 +020010767 mutex_unlock(&dev->struct_mutex);
10768
10769 crtc->primary->fb = fb;
10770 crtc->x = x;
10771 crtc->y = y;
10772
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +030010773 ret = dev_priv->display.crtc_mode_set(intel_crtc, x, y, fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010774 if (ret)
10775 goto done;
Daniel Vettera6778b32012-07-02 09:56:42 +020010776 }
10777
10778 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030010779 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
10780 update_scanline_offset(intel_crtc);
10781
Daniel Vetter25c5b262012-07-08 22:08:04 +020010782 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030010783 }
Daniel Vettera6778b32012-07-02 09:56:42 +020010784
Daniel Vettera6778b32012-07-02 09:56:42 +020010785 /* FIXME: add subpixel order */
10786done:
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030010787 if (ret && crtc->enabled)
Tim Gardner3ac18232012-12-07 07:54:26 -070010788 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020010789
Tim Gardner3ac18232012-12-07 07:54:26 -070010790out:
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010010791 kfree(pipe_config);
Tim Gardner3ac18232012-12-07 07:54:26 -070010792 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020010793 return ret;
10794}
10795
Damien Lespiaue7457a92013-08-08 22:28:59 +010010796static int intel_set_mode(struct drm_crtc *crtc,
10797 struct drm_display_mode *mode,
10798 int x, int y, struct drm_framebuffer *fb)
Daniel Vetterf30da182013-04-11 20:22:50 +020010799{
10800 int ret;
10801
10802 ret = __intel_set_mode(crtc, mode, x, y, fb);
10803
10804 if (ret == 0)
10805 intel_modeset_check_state(crtc->dev);
10806
10807 return ret;
10808}
10809
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010810void intel_crtc_restore_mode(struct drm_crtc *crtc)
10811{
Matt Roperf4510a22014-04-01 15:22:40 -070010812 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000010813}
10814
Daniel Vetter25c5b262012-07-08 22:08:04 +020010815#undef for_each_intel_crtc_masked
10816
Daniel Vetterd9e55602012-07-04 22:16:09 +020010817static void intel_set_config_free(struct intel_set_config *config)
10818{
10819 if (!config)
10820 return;
10821
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010822 kfree(config->save_connector_encoders);
10823 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020010824 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020010825 kfree(config);
10826}
10827
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010828static int intel_set_config_save_state(struct drm_device *dev,
10829 struct intel_set_config *config)
10830{
Ville Syrjälä76688512014-01-10 11:28:06 +020010831 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010832 struct drm_encoder *encoder;
10833 struct drm_connector *connector;
10834 int count;
10835
Ville Syrjälä76688512014-01-10 11:28:06 +020010836 config->save_crtc_enabled =
10837 kcalloc(dev->mode_config.num_crtc,
10838 sizeof(bool), GFP_KERNEL);
10839 if (!config->save_crtc_enabled)
10840 return -ENOMEM;
10841
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010842 config->save_encoder_crtcs =
10843 kcalloc(dev->mode_config.num_encoder,
10844 sizeof(struct drm_crtc *), GFP_KERNEL);
10845 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010846 return -ENOMEM;
10847
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010848 config->save_connector_encoders =
10849 kcalloc(dev->mode_config.num_connector,
10850 sizeof(struct drm_encoder *), GFP_KERNEL);
10851 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010852 return -ENOMEM;
10853
10854 /* Copy data. Note that driver private data is not affected.
10855 * Should anything bad happen only the expected state is
10856 * restored, not the drivers personal bookkeeping.
10857 */
10858 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010859 for_each_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010860 config->save_crtc_enabled[count++] = crtc->enabled;
10861 }
10862
10863 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010864 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010865 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010866 }
10867
10868 count = 0;
10869 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020010870 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010871 }
10872
10873 return 0;
10874}
10875
10876static void intel_set_config_restore_state(struct drm_device *dev,
10877 struct intel_set_config *config)
10878{
Ville Syrjälä76688512014-01-10 11:28:06 +020010879 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010880 struct intel_encoder *encoder;
10881 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010882 int count;
10883
10884 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010885 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020010886 crtc->new_enabled = config->save_crtc_enabled[count++];
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020010887
10888 if (crtc->new_enabled)
10889 crtc->new_config = &crtc->config;
10890 else
10891 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020010892 }
10893
10894 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010010895 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010896 encoder->new_crtc =
10897 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010898 }
10899
10900 count = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020010901 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
10902 connector->new_encoder =
10903 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020010904 }
10905}
10906
Imre Deake3de42b2013-05-03 19:44:07 +020010907static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010010908is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020010909{
10910 int i;
10911
Chris Wilson2e57f472013-07-17 12:14:40 +010010912 if (set->num_connectors == 0)
10913 return false;
10914
10915 if (WARN_ON(set->connectors == NULL))
10916 return false;
10917
10918 for (i = 0; i < set->num_connectors; i++)
10919 if (set->connectors[i]->encoder &&
10920 set->connectors[i]->encoder->crtc == set->crtc &&
10921 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020010922 return true;
10923
10924 return false;
10925}
10926
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010927static void
10928intel_set_config_compute_mode_changes(struct drm_mode_set *set,
10929 struct intel_set_config *config)
10930{
10931
10932 /* We should be able to check here if the fb has the same properties
10933 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010010934 if (is_crtc_connector_off(set)) {
10935 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070010936 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070010937 /*
10938 * If we have no fb, we can only flip as long as the crtc is
10939 * active, otherwise we need a full mode set. The crtc may
10940 * be active if we've only disabled the primary plane, or
10941 * in fastboot situations.
10942 */
Matt Roperf4510a22014-04-01 15:22:40 -070010943 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010944 struct intel_crtc *intel_crtc =
10945 to_intel_crtc(set->crtc);
10946
Matt Roper3b150f02014-05-29 08:06:53 -070010947 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030010948 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
10949 config->fb_changed = true;
10950 } else {
10951 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
10952 config->mode_changed = true;
10953 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010954 } else if (set->fb == NULL) {
10955 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010010956 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070010957 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010958 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010959 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010960 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020010961 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010962 }
10963
Daniel Vetter835c5872012-07-10 18:11:08 +020010964 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010965 config->fb_changed = true;
10966
10967 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
10968 DRM_DEBUG_KMS("modes are different, full mode set\n");
10969 drm_mode_debug_printmodeline(&set->crtc->mode);
10970 drm_mode_debug_printmodeline(set->mode);
10971 config->mode_changed = true;
10972 }
Chris Wilsona1d95702013-08-13 18:48:47 +010010973
10974 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
10975 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020010976}
10977
Daniel Vetter2e431052012-07-04 22:42:15 +020010978static int
Daniel Vetter9a935852012-07-05 22:34:27 +020010979intel_modeset_stage_output_state(struct drm_device *dev,
10980 struct drm_mode_set *set,
10981 struct intel_set_config *config)
Daniel Vetter50f56112012-07-02 09:35:43 +020010982{
Daniel Vetter9a935852012-07-05 22:34:27 +020010983 struct intel_connector *connector;
10984 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020010985 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030010986 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020010987
Damien Lespiau9abdda72013-02-13 13:29:23 +000010988 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020010989 * of connectors. For paranoia, double-check this. */
10990 WARN_ON(!set->fb && (set->num_connectors != 0));
10991 WARN_ON(set->fb && (set->num_connectors == 0));
10992
Daniel Vetter9a935852012-07-05 22:34:27 +020010993 list_for_each_entry(connector, &dev->mode_config.connector_list,
10994 base.head) {
10995 /* Otherwise traverse passed in connector list and get encoders
10996 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020010997 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010998 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100010999 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020011000 break;
11001 }
11002 }
11003
Daniel Vetter9a935852012-07-05 22:34:27 +020011004 /* If we disable the crtc, disable all its connectors. Also, if
11005 * the connector is on the changing crtc but not on the new
11006 * connector list, disable it. */
11007 if ((!set->fb || ro == set->num_connectors) &&
11008 connector->base.encoder &&
11009 connector->base.encoder->crtc == set->crtc) {
11010 connector->new_encoder = NULL;
11011
11012 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
11013 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011014 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020011015 }
11016
11017
11018 if (&connector->new_encoder->base != connector->base.encoder) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011019 DRM_DEBUG_KMS("encoder changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011020 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011021 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011022 }
11023 /* connector->new_encoder is now updated for all connectors. */
11024
11025 /* Update crtc of enabled connectors. */
Daniel Vetter9a935852012-07-05 22:34:27 +020011026 list_for_each_entry(connector, &dev->mode_config.connector_list,
11027 base.head) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011028 struct drm_crtc *new_crtc;
11029
Daniel Vetter9a935852012-07-05 22:34:27 +020011030 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020011031 continue;
11032
Daniel Vetter9a935852012-07-05 22:34:27 +020011033 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020011034
11035 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011036 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020011037 new_crtc = set->crtc;
11038 }
11039
11040 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010011041 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
11042 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011043 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020011044 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011045 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020011046
11047 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
11048 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011049 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020011050 new_crtc->base.id);
11051 }
11052
11053 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010011054 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011055 int num_connectors = 0;
Daniel Vetter9a935852012-07-05 22:34:27 +020011056 list_for_each_entry(connector,
11057 &dev->mode_config.connector_list,
11058 base.head) {
11059 if (connector->new_encoder == encoder) {
11060 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011061 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020011062 }
11063 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020011064
11065 if (num_connectors == 0)
11066 encoder->new_crtc = NULL;
11067 else if (num_connectors > 1)
11068 return -EINVAL;
11069
Daniel Vetter9a935852012-07-05 22:34:27 +020011070 /* Only now check for crtc changes so we don't miss encoders
11071 * that will be disabled. */
11072 if (&encoder->new_crtc->base != encoder->base.crtc) {
Daniel Vetter50f56112012-07-02 09:35:43 +020011073 DRM_DEBUG_KMS("crtc changed, full mode switch\n");
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011074 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020011075 }
11076 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011077 /* Now we've also updated encoder->new_crtc for all encoders. */
Dave Airlie0e32b392014-05-02 14:02:48 +100011078 list_for_each_entry(connector, &dev->mode_config.connector_list,
11079 base.head) {
11080 if (connector->new_encoder)
11081 if (connector->new_encoder != connector->encoder)
11082 connector->encoder = connector->new_encoder;
11083 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011084 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011085 crtc->new_enabled = false;
11086
Damien Lespiaub2784e12014-08-05 11:29:37 +010011087 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020011088 if (encoder->new_crtc == crtc) {
11089 crtc->new_enabled = true;
11090 break;
11091 }
11092 }
11093
11094 if (crtc->new_enabled != crtc->base.enabled) {
11095 DRM_DEBUG_KMS("crtc %sabled, full mode switch\n",
11096 crtc->new_enabled ? "en" : "dis");
11097 config->mode_changed = true;
11098 }
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011099
11100 if (crtc->new_enabled)
11101 crtc->new_config = &crtc->config;
11102 else
11103 crtc->new_config = NULL;
Ville Syrjälä76688512014-01-10 11:28:06 +020011104 }
11105
Daniel Vetter2e431052012-07-04 22:42:15 +020011106 return 0;
11107}
11108
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011109static void disable_crtc_nofb(struct intel_crtc *crtc)
11110{
11111 struct drm_device *dev = crtc->base.dev;
11112 struct intel_encoder *encoder;
11113 struct intel_connector *connector;
11114
11115 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
11116 pipe_name(crtc->pipe));
11117
11118 list_for_each_entry(connector, &dev->mode_config.connector_list, base.head) {
11119 if (connector->new_encoder &&
11120 connector->new_encoder->new_crtc == crtc)
11121 connector->new_encoder = NULL;
11122 }
11123
Damien Lespiaub2784e12014-08-05 11:29:37 +010011124 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011125 if (encoder->new_crtc == crtc)
11126 encoder->new_crtc = NULL;
11127 }
11128
11129 crtc->new_enabled = false;
Ville Syrjälä7bd0a8e2014-01-14 14:31:38 +020011130 crtc->new_config = NULL;
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011131}
11132
Daniel Vetter2e431052012-07-04 22:42:15 +020011133static int intel_crtc_set_config(struct drm_mode_set *set)
11134{
11135 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020011136 struct drm_mode_set save_set;
11137 struct intel_set_config *config;
11138 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020011139
Daniel Vetter8d3e3752012-07-05 16:09:09 +020011140 BUG_ON(!set);
11141 BUG_ON(!set->crtc);
11142 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020011143
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010011144 /* Enforce sane interface api - has been abused by the fb helper. */
11145 BUG_ON(!set->mode && set->fb);
11146 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020011147
Daniel Vetter2e431052012-07-04 22:42:15 +020011148 if (set->fb) {
11149 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
11150 set->crtc->base.id, set->fb->base.id,
11151 (int)set->num_connectors, set->x, set->y);
11152 } else {
11153 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020011154 }
11155
11156 dev = set->crtc->dev;
11157
11158 ret = -ENOMEM;
11159 config = kzalloc(sizeof(*config), GFP_KERNEL);
11160 if (!config)
11161 goto out_config;
11162
11163 ret = intel_set_config_save_state(dev, config);
11164 if (ret)
11165 goto out_config;
11166
11167 save_set.crtc = set->crtc;
11168 save_set.mode = &set->crtc->mode;
11169 save_set.x = set->crtc->x;
11170 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070011171 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020011172
11173 /* Compute whether we need a full modeset, only an fb base update or no
11174 * change at all. In the future we might also check whether only the
11175 * mode changed, e.g. for LVDS where we only change the panel fitter in
11176 * such cases. */
11177 intel_set_config_compute_mode_changes(set, config);
11178
Daniel Vetter9a935852012-07-05 22:34:27 +020011179 ret = intel_modeset_stage_output_state(dev, set, config);
Daniel Vetter2e431052012-07-04 22:42:15 +020011180 if (ret)
11181 goto fail;
11182
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011183 if (config->mode_changed) {
Chris Wilsonc0c36b942012-12-19 16:08:43 +000011184 ret = intel_set_mode(set->crtc, set->mode,
11185 set->x, set->y, set->fb);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020011186 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070011187 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
11188
Ville Syrjälä4878cae2013-02-18 19:08:48 +020011189 intel_crtc_wait_for_pending_flips(set->crtc);
11190
Daniel Vetter4f660f42012-07-02 09:47:37 +020011191 ret = intel_pipe_set_base(set->crtc,
Daniel Vetter94352cf2012-07-05 22:51:56 +020011192 set->x, set->y, set->fb);
Matt Roper3b150f02014-05-29 08:06:53 -070011193
11194 /*
11195 * We need to make sure the primary plane is re-enabled if it
11196 * has previously been turned off.
11197 */
11198 if (!intel_crtc->primary_enabled && ret == 0) {
11199 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011200 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070011201 }
11202
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011203 /*
11204 * In the fastboot case this may be our only check of the
11205 * state after boot. It would be better to only do it on
11206 * the first update, but we don't have a nice way of doing that
11207 * (and really, set_config isn't used much for high freq page
11208 * flipping, so increasing its cost here shouldn't be a big
11209 * deal).
11210 */
Jani Nikulad330a952014-01-21 11:24:25 +020011211 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080011212 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020011213 }
11214
Chris Wilson2d05eae2013-05-03 17:36:25 +010011215 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020011216 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
11217 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020011218fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010011219 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011220
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020011221 /*
11222 * HACK: if the pipe was on, but we didn't have a framebuffer,
11223 * force the pipe off to avoid oopsing in the modeset code
11224 * due to fb==NULL. This should only happen during boot since
11225 * we don't yet reconstruct the FB from the hardware state.
11226 */
11227 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
11228 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
11229
Chris Wilson2d05eae2013-05-03 17:36:25 +010011230 /* Try to restore the config */
11231 if (config->mode_changed &&
11232 intel_set_mode(save_set.crtc, save_set.mode,
11233 save_set.x, save_set.y, save_set.fb))
11234 DRM_ERROR("failed to restore config after modeset failure\n");
11235 }
Daniel Vetter50f56112012-07-02 09:35:43 +020011236
Daniel Vetterd9e55602012-07-04 22:16:09 +020011237out_config:
11238 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020011239 return ret;
11240}
11241
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011242static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011243 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020011244 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011245 .destroy = intel_crtc_destroy,
11246 .page_flip = intel_crtc_page_flip,
11247};
11248
Daniel Vetter53589012013-06-05 13:34:16 +020011249static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
11250 struct intel_shared_dpll *pll,
11251 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011252{
Daniel Vetter53589012013-06-05 13:34:16 +020011253 uint32_t val;
11254
Daniel Vetterf458ebb2014-09-30 10:56:39 +020011255 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030011256 return false;
11257
Daniel Vetter53589012013-06-05 13:34:16 +020011258 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020011259 hw_state->dpll = val;
11260 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
11261 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020011262
11263 return val & DPLL_VCO_ENABLE;
11264}
11265
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011266static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
11267 struct intel_shared_dpll *pll)
11268{
11269 I915_WRITE(PCH_FP0(pll->id), pll->hw_state.fp0);
11270 I915_WRITE(PCH_FP1(pll->id), pll->hw_state.fp1);
11271}
11272
Daniel Vettere7b903d2013-06-05 13:34:14 +020011273static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
11274 struct intel_shared_dpll *pll)
11275{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011276 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020011277 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020011278
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011279 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11280
11281 /* Wait for the clocks to stabilize. */
11282 POSTING_READ(PCH_DPLL(pll->id));
11283 udelay(150);
11284
11285 /* The pixel multiplier can only be updated once the
11286 * DPLL is enabled and the clocks are stable.
11287 *
11288 * So write it again.
11289 */
11290 I915_WRITE(PCH_DPLL(pll->id), pll->hw_state.dpll);
11291 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011292 udelay(200);
11293}
11294
11295static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
11296 struct intel_shared_dpll *pll)
11297{
11298 struct drm_device *dev = dev_priv->dev;
11299 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011300
11301 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011302 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020011303 if (intel_crtc_to_shared_dpll(crtc) == pll)
11304 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
11305 }
11306
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011307 I915_WRITE(PCH_DPLL(pll->id), 0);
11308 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020011309 udelay(200);
11310}
11311
Daniel Vetter46edb022013-06-05 13:34:12 +020011312static char *ibx_pch_dpll_names[] = {
11313 "PCH DPLL A",
11314 "PCH DPLL B",
11315};
11316
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011317static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011318{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011319 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011320 int i;
11321
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011322 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011323
Daniel Vettere72f9fb2013-06-05 13:34:06 +020011324 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020011325 dev_priv->shared_dplls[i].id = i;
11326 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020011327 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020011328 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
11329 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020011330 dev_priv->shared_dplls[i].get_hw_state =
11331 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010011332 }
11333}
11334
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011335static void intel_shared_dpll_init(struct drm_device *dev)
11336{
Daniel Vettere7b903d2013-06-05 13:34:14 +020011337 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011338
Daniel Vetter9cd86932014-06-25 22:01:57 +030011339 if (HAS_DDI(dev))
11340 intel_ddi_pll_init(dev);
11341 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011342 ibx_pch_dpll_init(dev);
11343 else
11344 dev_priv->num_shared_dpll = 0;
11345
11346 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020011347}
11348
Matt Roper465c1202014-05-29 08:06:54 -070011349static int
11350intel_primary_plane_disable(struct drm_plane *plane)
11351{
11352 struct drm_device *dev = plane->dev;
Matt Roper465c1202014-05-29 08:06:54 -070011353 struct intel_crtc *intel_crtc;
11354
11355 if (!plane->fb)
11356 return 0;
11357
11358 BUG_ON(!plane->crtc);
11359
11360 intel_crtc = to_intel_crtc(plane->crtc);
11361
11362 /*
11363 * Even though we checked plane->fb above, it's still possible that
11364 * the primary plane has been implicitly disabled because the crtc
11365 * coordinates given weren't visible, or because we detected
11366 * that it was 100% covered by a sprite plane. Or, the CRTC may be
11367 * off and we've set a fb, but haven't actually turned on the CRTC yet.
11368 * In either case, we need to unpin the FB and let the fb pointer get
11369 * updated, but otherwise we don't need to touch the hardware.
11370 */
11371 if (!intel_crtc->primary_enabled)
11372 goto disable_unpin;
11373
11374 intel_crtc_wait_for_pending_flips(plane->crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030011375 intel_disable_primary_hw_plane(plane, plane->crtc);
11376
Matt Roper465c1202014-05-29 08:06:54 -070011377disable_unpin:
Matt Roper4c345742014-07-09 16:22:10 -070011378 mutex_lock(&dev->struct_mutex);
Matt Roper2ff8fde2014-07-08 07:50:07 -070011379 i915_gem_track_fb(intel_fb_obj(plane->fb), NULL,
Daniel Vettera071fa02014-06-18 23:28:09 +020011380 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe));
Matt Roper2ff8fde2014-07-08 07:50:07 -070011381 intel_unpin_fb_obj(intel_fb_obj(plane->fb));
Matt Roper4c345742014-07-09 16:22:10 -070011382 mutex_unlock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070011383 plane->fb = NULL;
11384
11385 return 0;
11386}
11387
11388static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011389intel_check_primary_plane(struct drm_plane *plane,
11390 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070011391{
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011392 struct drm_crtc *crtc = state->crtc;
11393 struct drm_framebuffer *fb = state->fb;
11394 struct drm_rect *dest = &state->dst;
11395 struct drm_rect *src = &state->src;
11396 const struct drm_rect *clip = &state->clip;
11397
Gustavo Padovan3ead8bb2014-10-24 19:00:18 +010011398 return drm_plane_helper_check_update(plane, crtc, fb,
11399 src, dest, clip,
11400 DRM_PLANE_HELPER_NO_SCALING,
11401 DRM_PLANE_HELPER_NO_SCALING,
11402 false, true, &state->visible);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011403}
11404
11405static int
11406intel_commit_primary_plane(struct drm_plane *plane,
11407 struct intel_plane_state *state)
11408{
11409 struct drm_crtc *crtc = state->crtc;
11410 struct drm_framebuffer *fb = state->fb;
Matt Roper465c1202014-05-29 08:06:54 -070011411 struct drm_device *dev = crtc->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053011412 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper465c1202014-05-29 08:06:54 -070011413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011414 enum pipe pipe = intel_crtc->pipe;
11415 struct drm_framebuffer *old_fb = plane->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011416 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11417 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Sonika Jindalce54d852014-08-21 11:44:39 +053011418 struct intel_plane *intel_plane = to_intel_plane(plane);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011419 struct drm_rect *src = &state->src;
Matt Roper465c1202014-05-29 08:06:54 -070011420 int ret;
11421
Matt Roper465c1202014-05-29 08:06:54 -070011422 intel_crtc_wait_for_pending_flips(crtc);
11423
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011424 if (intel_crtc_has_pending_flip(crtc)) {
11425 DRM_ERROR("pipe is still busy with an old pageflip\n");
11426 return -EBUSY;
Matt Roper465c1202014-05-29 08:06:54 -070011427 }
11428
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011429 if (plane->fb != fb) {
11430 mutex_lock(&dev->struct_mutex);
11431 ret = intel_pin_and_fence_fb_obj(dev, obj, NULL);
11432 if (ret == 0)
11433 i915_gem_track_fb(old_obj, obj,
11434 INTEL_FRONTBUFFER_PRIMARY(pipe));
11435 mutex_unlock(&dev->struct_mutex);
11436 if (ret != 0) {
11437 DRM_DEBUG_KMS("pin & fence failed\n");
11438 return ret;
11439 }
11440 }
11441
11442 crtc->primary->fb = fb;
11443 crtc->x = src->x1;
11444 crtc->y = src->y1;
11445
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011446 intel_plane->crtc_x = state->orig_dst.x1;
11447 intel_plane->crtc_y = state->orig_dst.y1;
11448 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11449 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11450 intel_plane->src_x = state->orig_src.x1;
11451 intel_plane->src_y = state->orig_src.y1;
11452 intel_plane->src_w = drm_rect_width(&state->orig_src);
11453 intel_plane->src_h = drm_rect_height(&state->orig_src);
Sonika Jindalce54d852014-08-21 11:44:39 +053011454 intel_plane->obj = obj;
Matt Roper465c1202014-05-29 08:06:54 -070011455
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030011456 if (intel_crtc->active) {
11457 /*
11458 * FBC does not work on some platforms for rotated
11459 * planes, so disable it when rotation is not 0 and
11460 * update it when rotation is set back to 0.
11461 *
11462 * FIXME: This is redundant with the fbc update done in
11463 * the primary plane enable function except that that
11464 * one is done too late. We eventually need to unify
11465 * this.
11466 */
11467 if (intel_crtc->primary_enabled &&
11468 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11469 dev_priv->fbc.plane == intel_crtc->plane &&
11470 intel_plane->rotation != BIT(DRM_ROTATE_0)) {
11471 intel_disable_fbc(dev);
11472 }
11473
11474 if (state->visible) {
11475 bool was_enabled = intel_crtc->primary_enabled;
11476
11477 /* FIXME: kill this fastboot hack */
11478 intel_update_pipe_size(intel_crtc);
11479
11480 intel_crtc->primary_enabled = true;
11481
11482 dev_priv->display.update_primary_plane(crtc, plane->fb,
11483 crtc->x, crtc->y);
11484
11485 /*
11486 * BDW signals flip done immediately if the plane
11487 * is disabled, even if the plane enable is already
11488 * armed to occur at the next vblank :(
11489 */
11490 if (IS_BROADWELL(dev) && !was_enabled)
11491 intel_wait_for_vblank(dev, intel_crtc->pipe);
11492 } else {
11493 /*
11494 * If clipping results in a non-visible primary plane,
11495 * we'll disable the primary plane. Note that this is
11496 * a bit different than what happens if userspace
11497 * explicitly disables the plane by passing fb=0
11498 * because plane->fb still gets set and pinned.
11499 */
11500 intel_disable_primary_hw_plane(plane, crtc);
11501 }
11502
11503 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
11504
11505 mutex_lock(&dev->struct_mutex);
11506 intel_update_fbc(dev);
11507 mutex_unlock(&dev->struct_mutex);
11508 }
11509
11510 if (old_fb && old_fb != fb) {
11511 if (intel_crtc->active)
11512 intel_wait_for_vblank(dev, intel_crtc->pipe);
11513
11514 mutex_lock(&dev->struct_mutex);
11515 intel_unpin_fb_obj(old_obj);
11516 mutex_unlock(&dev->struct_mutex);
11517 }
11518
Matt Roper465c1202014-05-29 08:06:54 -070011519 return 0;
11520}
11521
Gustavo Padovan3c692a42014-09-05 17:04:49 -030011522static int
11523intel_primary_plane_setplane(struct drm_plane *plane, struct drm_crtc *crtc,
11524 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11525 unsigned int crtc_w, unsigned int crtc_h,
11526 uint32_t src_x, uint32_t src_y,
11527 uint32_t src_w, uint32_t src_h)
11528{
11529 struct intel_plane_state state;
11530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11531 int ret;
11532
11533 state.crtc = crtc;
11534 state.fb = fb;
11535
11536 /* sample coordinates in 16.16 fixed point */
11537 state.src.x1 = src_x;
11538 state.src.x2 = src_x + src_w;
11539 state.src.y1 = src_y;
11540 state.src.y2 = src_y + src_h;
11541
11542 /* integer pixels */
11543 state.dst.x1 = crtc_x;
11544 state.dst.x2 = crtc_x + crtc_w;
11545 state.dst.y1 = crtc_y;
11546 state.dst.y2 = crtc_y + crtc_h;
11547
11548 state.clip.x1 = 0;
11549 state.clip.y1 = 0;
11550 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11551 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11552
11553 state.orig_src = state.src;
11554 state.orig_dst = state.dst;
11555
11556 ret = intel_check_primary_plane(plane, &state);
11557 if (ret)
11558 return ret;
11559
11560 intel_commit_primary_plane(plane, &state);
11561
11562 return 0;
11563}
11564
Matt Roper3d7d6512014-06-10 08:28:13 -070011565/* Common destruction function for both primary and cursor planes */
11566static void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070011567{
11568 struct intel_plane *intel_plane = to_intel_plane(plane);
11569 drm_plane_cleanup(plane);
11570 kfree(intel_plane);
11571}
11572
11573static const struct drm_plane_funcs intel_primary_plane_funcs = {
11574 .update_plane = intel_primary_plane_setplane,
11575 .disable_plane = intel_primary_plane_disable,
Matt Roper3d7d6512014-06-10 08:28:13 -070011576 .destroy = intel_plane_destroy,
Sonika Jindal48404c12014-08-22 14:06:04 +053011577 .set_property = intel_plane_set_property
Matt Roper465c1202014-05-29 08:06:54 -070011578};
11579
11580static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
11581 int pipe)
11582{
11583 struct intel_plane *primary;
11584 const uint32_t *intel_primary_formats;
11585 int num_formats;
11586
11587 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
11588 if (primary == NULL)
11589 return NULL;
11590
11591 primary->can_scale = false;
11592 primary->max_downscale = 1;
11593 primary->pipe = pipe;
11594 primary->plane = pipe;
Sonika Jindal48404c12014-08-22 14:06:04 +053011595 primary->rotation = BIT(DRM_ROTATE_0);
Matt Roper465c1202014-05-29 08:06:54 -070011596 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
11597 primary->plane = !pipe;
11598
11599 if (INTEL_INFO(dev)->gen <= 3) {
11600 intel_primary_formats = intel_primary_formats_gen2;
11601 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
11602 } else {
11603 intel_primary_formats = intel_primary_formats_gen4;
11604 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
11605 }
11606
11607 drm_universal_plane_init(dev, &primary->base, 0,
11608 &intel_primary_plane_funcs,
11609 intel_primary_formats, num_formats,
11610 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053011611
11612 if (INTEL_INFO(dev)->gen >= 4) {
11613 if (!dev->mode_config.rotation_property)
11614 dev->mode_config.rotation_property =
11615 drm_mode_create_rotation_property(dev,
11616 BIT(DRM_ROTATE_0) |
11617 BIT(DRM_ROTATE_180));
11618 if (dev->mode_config.rotation_property)
11619 drm_object_attach_property(&primary->base.base,
11620 dev->mode_config.rotation_property,
11621 primary->rotation);
11622 }
11623
Matt Roper465c1202014-05-29 08:06:54 -070011624 return &primary->base;
11625}
11626
Matt Roper3d7d6512014-06-10 08:28:13 -070011627static int
11628intel_cursor_plane_disable(struct drm_plane *plane)
11629{
11630 if (!plane->fb)
11631 return 0;
11632
11633 BUG_ON(!plane->crtc);
11634
11635 return intel_crtc_cursor_set_obj(plane->crtc, NULL, 0, 0);
11636}
11637
11638static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030011639intel_check_cursor_plane(struct drm_plane *plane,
11640 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070011641{
Gustavo Padovan852e7872014-09-05 17:22:31 -030011642 struct drm_crtc *crtc = state->crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011643 struct drm_device *dev = crtc->dev;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011644 struct drm_framebuffer *fb = state->fb;
11645 struct drm_rect *dest = &state->dst;
11646 struct drm_rect *src = &state->src;
11647 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011648 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11649 int crtc_w, crtc_h;
11650 unsigned stride;
11651 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011652
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011653 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030011654 src, dest, clip,
11655 DRM_PLANE_HELPER_NO_SCALING,
11656 DRM_PLANE_HELPER_NO_SCALING,
11657 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011658 if (ret)
11659 return ret;
11660
11661
11662 /* if we want to turn off the cursor ignore width and height */
11663 if (!obj)
11664 return 0;
11665
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011666 /* Check for which cursor types we support */
11667 crtc_w = drm_rect_width(&state->orig_dst);
11668 crtc_h = drm_rect_height(&state->orig_dst);
11669 if (!cursor_size_ok(dev, crtc_w, crtc_h)) {
11670 DRM_DEBUG("Cursor dimension not supported\n");
11671 return -EINVAL;
11672 }
11673
11674 stride = roundup_pow_of_two(crtc_w) * 4;
11675 if (obj->base.size < stride * crtc_h) {
11676 DRM_DEBUG_KMS("buffer is too small\n");
11677 return -ENOMEM;
11678 }
11679
Gustavo Padovane391ea82014-09-24 14:20:25 -030011680 if (fb == crtc->cursor->fb)
11681 return 0;
11682
Gustavo Padovan757f9a32014-09-24 14:20:24 -030011683 /* we only need to pin inside GTT if cursor is non-phy */
11684 mutex_lock(&dev->struct_mutex);
11685 if (!INTEL_INFO(dev)->cursor_needs_physical && obj->tiling_mode) {
11686 DRM_DEBUG_KMS("cursor cannot be tiled\n");
11687 ret = -EINVAL;
11688 }
11689 mutex_unlock(&dev->struct_mutex);
11690
11691 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011692}
11693
11694static int
11695intel_commit_cursor_plane(struct drm_plane *plane,
11696 struct intel_plane_state *state)
11697{
11698 struct drm_crtc *crtc = state->crtc;
11699 struct drm_framebuffer *fb = state->fb;
Matt Roper3d7d6512014-06-10 08:28:13 -070011700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070011701 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper3d7d6512014-06-10 08:28:13 -070011702 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11703 struct drm_i915_gem_object *obj = intel_fb->obj;
Gustavo Padovan852e7872014-09-05 17:22:31 -030011704 int crtc_w, crtc_h;
Matt Roper3d7d6512014-06-10 08:28:13 -070011705
Gustavo Padovan852e7872014-09-05 17:22:31 -030011706 crtc->cursor_x = state->orig_dst.x1;
11707 crtc->cursor_y = state->orig_dst.y1;
Sonika Jindala919db92014-10-23 07:41:33 -070011708
11709 intel_plane->crtc_x = state->orig_dst.x1;
11710 intel_plane->crtc_y = state->orig_dst.y1;
11711 intel_plane->crtc_w = drm_rect_width(&state->orig_dst);
11712 intel_plane->crtc_h = drm_rect_height(&state->orig_dst);
11713 intel_plane->src_x = state->orig_src.x1;
11714 intel_plane->src_y = state->orig_src.y1;
11715 intel_plane->src_w = drm_rect_width(&state->orig_src);
11716 intel_plane->src_h = drm_rect_height(&state->orig_src);
11717 intel_plane->obj = obj;
11718
Matt Roper3d7d6512014-06-10 08:28:13 -070011719 if (fb != crtc->cursor->fb) {
Gustavo Padovan852e7872014-09-05 17:22:31 -030011720 crtc_w = drm_rect_width(&state->orig_dst);
11721 crtc_h = drm_rect_height(&state->orig_dst);
Matt Roper3d7d6512014-06-10 08:28:13 -070011722 return intel_crtc_cursor_set_obj(crtc, obj, crtc_w, crtc_h);
11723 } else {
Gustavo Padovan852e7872014-09-05 17:22:31 -030011724 intel_crtc_update_cursor(crtc, state->visible);
Daniel Vetter4ed91092014-08-08 20:27:01 +020011725
11726 intel_frontbuffer_flip(crtc->dev,
11727 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe));
11728
Matt Roper3d7d6512014-06-10 08:28:13 -070011729 return 0;
11730 }
11731}
Gustavo Padovan852e7872014-09-05 17:22:31 -030011732
11733static int
11734intel_cursor_plane_update(struct drm_plane *plane, struct drm_crtc *crtc,
11735 struct drm_framebuffer *fb, int crtc_x, int crtc_y,
11736 unsigned int crtc_w, unsigned int crtc_h,
11737 uint32_t src_x, uint32_t src_y,
11738 uint32_t src_w, uint32_t src_h)
11739{
11740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11741 struct intel_plane_state state;
11742 int ret;
11743
11744 state.crtc = crtc;
11745 state.fb = fb;
11746
11747 /* sample coordinates in 16.16 fixed point */
11748 state.src.x1 = src_x;
11749 state.src.x2 = src_x + src_w;
11750 state.src.y1 = src_y;
11751 state.src.y2 = src_y + src_h;
11752
11753 /* integer pixels */
11754 state.dst.x1 = crtc_x;
11755 state.dst.x2 = crtc_x + crtc_w;
11756 state.dst.y1 = crtc_y;
11757 state.dst.y2 = crtc_y + crtc_h;
11758
11759 state.clip.x1 = 0;
11760 state.clip.y1 = 0;
11761 state.clip.x2 = intel_crtc->active ? intel_crtc->config.pipe_src_w : 0;
11762 state.clip.y2 = intel_crtc->active ? intel_crtc->config.pipe_src_h : 0;
11763
11764 state.orig_src = state.src;
11765 state.orig_dst = state.dst;
11766
11767 ret = intel_check_cursor_plane(plane, &state);
11768 if (ret)
11769 return ret;
11770
11771 return intel_commit_cursor_plane(plane, &state);
11772}
11773
Matt Roper3d7d6512014-06-10 08:28:13 -070011774static const struct drm_plane_funcs intel_cursor_plane_funcs = {
11775 .update_plane = intel_cursor_plane_update,
11776 .disable_plane = intel_cursor_plane_disable,
11777 .destroy = intel_plane_destroy,
Ville Syrjälä4398ad42014-10-23 07:41:34 -070011778 .set_property = intel_plane_set_property,
Matt Roper3d7d6512014-06-10 08:28:13 -070011779};
11780
11781static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
11782 int pipe)
11783{
11784 struct intel_plane *cursor;
11785
11786 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
11787 if (cursor == NULL)
11788 return NULL;
11789
11790 cursor->can_scale = false;
11791 cursor->max_downscale = 1;
11792 cursor->pipe = pipe;
11793 cursor->plane = pipe;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070011794 cursor->rotation = BIT(DRM_ROTATE_0);
Matt Roper3d7d6512014-06-10 08:28:13 -070011795
11796 drm_universal_plane_init(dev, &cursor->base, 0,
11797 &intel_cursor_plane_funcs,
11798 intel_cursor_formats,
11799 ARRAY_SIZE(intel_cursor_formats),
11800 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070011801
11802 if (INTEL_INFO(dev)->gen >= 4) {
11803 if (!dev->mode_config.rotation_property)
11804 dev->mode_config.rotation_property =
11805 drm_mode_create_rotation_property(dev,
11806 BIT(DRM_ROTATE_0) |
11807 BIT(DRM_ROTATE_180));
11808 if (dev->mode_config.rotation_property)
11809 drm_object_attach_property(&cursor->base.base,
11810 dev->mode_config.rotation_property,
11811 cursor->rotation);
11812 }
11813
Matt Roper3d7d6512014-06-10 08:28:13 -070011814 return &cursor->base;
11815}
11816
Hannes Ederb358d0a2008-12-18 21:18:47 +010011817static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080011818{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011819 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080011820 struct intel_crtc *intel_crtc;
Matt Roper3d7d6512014-06-10 08:28:13 -070011821 struct drm_plane *primary = NULL;
11822 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070011823 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080011824
Daniel Vetter955382f2013-09-19 14:05:45 +020011825 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080011826 if (intel_crtc == NULL)
11827 return;
11828
Matt Roper465c1202014-05-29 08:06:54 -070011829 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011830 if (!primary)
11831 goto fail;
11832
11833 cursor = intel_cursor_plane_create(dev, pipe);
11834 if (!cursor)
11835 goto fail;
11836
Matt Roper465c1202014-05-29 08:06:54 -070011837 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070011838 cursor, &intel_crtc_funcs);
11839 if (ret)
11840 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080011841
11842 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080011843 for (i = 0; i < 256; i++) {
11844 intel_crtc->lut_r[i] = i;
11845 intel_crtc->lut_g[i] = i;
11846 intel_crtc->lut_b[i] = i;
11847 }
11848
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011849 /*
11850 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020011851 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020011852 */
Jesse Barnes80824002009-09-10 15:28:06 -070011853 intel_crtc->pipe = pipe;
11854 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010011855 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080011856 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010011857 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070011858 }
11859
Chris Wilson4b0e3332014-05-30 16:35:26 +030011860 intel_crtc->cursor_base = ~0;
11861 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030011862 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030011863
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080011864 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
11865 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
11866 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
11867 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
11868
Jesse Barnes79e53942008-11-07 14:24:08 -080011869 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020011870
11871 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070011872 return;
11873
11874fail:
11875 if (primary)
11876 drm_plane_cleanup(primary);
11877 if (cursor)
11878 drm_plane_cleanup(cursor);
11879 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080011880}
11881
Jesse Barnes752aa882013-10-31 18:55:49 +020011882enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
11883{
11884 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020011885 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020011886
Rob Clark51fd3712013-11-19 12:10:12 -050011887 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020011888
11889 if (!encoder)
11890 return INVALID_PIPE;
11891
11892 return to_intel_crtc(encoder->crtc)->pipe;
11893}
11894
Carl Worth08d7b3d2009-04-29 14:43:54 -070011895int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000011896 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070011897{
Carl Worth08d7b3d2009-04-29 14:43:54 -070011898 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040011899 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020011900 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011901
Daniel Vetter1cff8f62012-04-24 09:55:08 +020011902 if (!drm_core_check_feature(dev, DRIVER_MODESET))
11903 return -ENODEV;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011904
Rob Clark7707e652014-07-17 23:30:04 -040011905 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070011906
Rob Clark7707e652014-07-17 23:30:04 -040011907 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070011908 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030011909 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011910 }
11911
Rob Clark7707e652014-07-17 23:30:04 -040011912 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020011913 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011914
Daniel Vetterc05422d2009-08-11 16:05:30 +020011915 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070011916}
11917
Daniel Vetter66a92782012-07-12 20:08:18 +020011918static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080011919{
Daniel Vetter66a92782012-07-12 20:08:18 +020011920 struct drm_device *dev = encoder->base.dev;
11921 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080011922 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080011923 int entry = 0;
11924
Damien Lespiaub2784e12014-08-05 11:29:37 +010011925 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011926 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020011927 index_mask |= (1 << entry);
11928
Jesse Barnes79e53942008-11-07 14:24:08 -080011929 entry++;
11930 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010011931
Jesse Barnes79e53942008-11-07 14:24:08 -080011932 return index_mask;
11933}
11934
Chris Wilson4d302442010-12-14 19:21:29 +000011935static bool has_edp_a(struct drm_device *dev)
11936{
11937 struct drm_i915_private *dev_priv = dev->dev_private;
11938
11939 if (!IS_MOBILE(dev))
11940 return false;
11941
11942 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
11943 return false;
11944
Damien Lespiaue3589902014-02-07 19:12:50 +000011945 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000011946 return false;
11947
11948 return true;
11949}
11950
Damien Lespiauba0fbca2014-01-08 14:18:23 +000011951const char *intel_output_name(int output)
11952{
11953 static const char *names[] = {
11954 [INTEL_OUTPUT_UNUSED] = "Unused",
11955 [INTEL_OUTPUT_ANALOG] = "Analog",
11956 [INTEL_OUTPUT_DVO] = "DVO",
11957 [INTEL_OUTPUT_SDVO] = "SDVO",
11958 [INTEL_OUTPUT_LVDS] = "LVDS",
11959 [INTEL_OUTPUT_TVOUT] = "TV",
11960 [INTEL_OUTPUT_HDMI] = "HDMI",
11961 [INTEL_OUTPUT_DISPLAYPORT] = "DisplayPort",
11962 [INTEL_OUTPUT_EDP] = "eDP",
11963 [INTEL_OUTPUT_DSI] = "DSI",
11964 [INTEL_OUTPUT_UNKNOWN] = "Unknown",
11965 };
11966
11967 if (output < 0 || output >= ARRAY_SIZE(names) || !names[output])
11968 return "Invalid";
11969
11970 return names[output];
11971}
11972
Jesse Barnes84b4e042014-06-25 08:24:29 -070011973static bool intel_crt_present(struct drm_device *dev)
11974{
11975 struct drm_i915_private *dev_priv = dev->dev_private;
11976
Damien Lespiau884497e2013-12-03 13:56:23 +000011977 if (INTEL_INFO(dev)->gen >= 9)
11978 return false;
11979
Damien Lespiaucf404ce2014-10-01 20:04:15 +010011980 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070011981 return false;
11982
11983 if (IS_CHERRYVIEW(dev))
11984 return false;
11985
11986 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
11987 return false;
11988
11989 return true;
11990}
11991
Jesse Barnes79e53942008-11-07 14:24:08 -080011992static void intel_setup_outputs(struct drm_device *dev)
11993{
Eric Anholt725e30a2009-01-22 13:01:02 -080011994 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010011995 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040011996 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080011997
Daniel Vetterc9093352013-06-06 22:22:47 +020011998 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080011999
Jesse Barnes84b4e042014-06-25 08:24:29 -070012000 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020012001 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012002
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012003 if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030012004 int found;
12005
12006 /* Haswell uses DDI functions to detect digital outputs */
12007 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
12008 /* DDI A only supports eDP */
12009 if (found)
12010 intel_ddi_init(dev, PORT_A);
12011
12012 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
12013 * register */
12014 found = I915_READ(SFUSE_STRAP);
12015
12016 if (found & SFUSE_STRAP_DDIB_DETECTED)
12017 intel_ddi_init(dev, PORT_B);
12018 if (found & SFUSE_STRAP_DDIC_DETECTED)
12019 intel_ddi_init(dev, PORT_C);
12020 if (found & SFUSE_STRAP_DDID_DETECTED)
12021 intel_ddi_init(dev, PORT_D);
12022 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012023 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020012024 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020012025
12026 if (has_edp_a(dev))
12027 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040012028
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012029 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080012030 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010012031 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012032 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012033 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012034 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012035 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012036 }
12037
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012038 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012039 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012040
Paulo Zanonidc0fa712013-02-19 16:21:46 -030012041 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030012042 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080012043
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012044 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012045 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080012046
Daniel Vetter270b3042012-10-27 15:52:05 +020012047 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012048 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070012049 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012050 /*
12051 * The DP_DETECTED bit is the latched state of the DDC
12052 * SDA pin at boot. However since eDP doesn't require DDC
12053 * (no way to plug in a DP->HDMI dongle) the DDC pins for
12054 * eDP ports may have been muxed to an alternate function.
12055 * Thus we can't rely on the DP_DETECTED bit alone to detect
12056 * eDP ports. Consult the VBT as well as DP_DETECTED to
12057 * detect eDP ports.
12058 */
12059 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED)
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012060 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
12061 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012062 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
12063 intel_dp_is_edp(dev, PORT_B))
12064 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030012065
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012066 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED)
Jesse Barnes6f6005a2013-08-09 09:34:35 -070012067 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
12068 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012069 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
12070 intel_dp_is_edp(dev, PORT_C))
12071 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053012072
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012073 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012074 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012075 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
12076 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030012077 /* eDP not supported on port D, so don't check VBT */
12078 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
12079 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030012080 }
12081
Jani Nikula3cfca972013-08-27 15:12:26 +030012082 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080012083 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012084 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080012085
Paulo Zanonie2debe92013-02-18 19:00:27 -030012086 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012087 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012088 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012089 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
12090 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012091 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012092 }
Ma Ling27185ae2009-08-24 13:50:23 +080012093
Imre Deake7281ea2013-05-08 13:14:08 +030012094 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012095 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080012096 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012097
12098 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040012099
Paulo Zanonie2debe92013-02-18 19:00:27 -030012100 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012101 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012102 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012103 }
Ma Ling27185ae2009-08-24 13:50:23 +080012104
Paulo Zanonie2debe92013-02-18 19:00:27 -030012105 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080012106
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012107 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
12108 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030012109 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012110 }
Imre Deake7281ea2013-05-08 13:14:08 +030012111 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012112 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080012113 }
Ma Ling27185ae2009-08-24 13:50:23 +080012114
Jesse Barnesb01f2c32009-12-11 11:07:17 -080012115 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030012116 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030012117 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070012118 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012119 intel_dvo_init(dev);
12120
Zhenyu Wang103a1962009-11-27 11:44:36 +080012121 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080012122 intel_tv_init(dev);
12123
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070012124 intel_edp_psr_init(dev);
12125
Damien Lespiaub2784e12014-08-05 11:29:37 +010012126 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010012127 encoder->base.possible_crtcs = encoder->crtc_mask;
12128 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020012129 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080012130 }
Chris Wilson47356eb2011-01-11 17:06:04 +000012131
Paulo Zanonidde86e22012-12-01 12:04:25 -020012132 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020012133
12134 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012135}
12136
12137static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
12138{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012139 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080012140 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080012141
Daniel Vetteref2d6332014-02-10 18:00:38 +010012142 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012143 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010012144 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030012145 drm_gem_object_unreference(&intel_fb->obj->base);
12146 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080012147 kfree(intel_fb);
12148}
12149
12150static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000012151 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080012152 unsigned int *handle)
12153{
12154 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000012155 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012156
Chris Wilson05394f32010-11-08 19:18:58 +000012157 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080012158}
12159
12160static const struct drm_framebuffer_funcs intel_fb_funcs = {
12161 .destroy = intel_user_framebuffer_destroy,
12162 .create_handle = intel_user_framebuffer_create_handle,
12163};
12164
Daniel Vetterb5ea6422014-03-02 21:18:00 +010012165static int intel_framebuffer_init(struct drm_device *dev,
12166 struct intel_framebuffer *intel_fb,
12167 struct drm_mode_fb_cmd2 *mode_cmd,
12168 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080012169{
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012170 int aligned_height;
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012171 int pitch_limit;
Jesse Barnes79e53942008-11-07 14:24:08 -080012172 int ret;
12173
Daniel Vetterdd4916c2013-10-09 21:23:51 +020012174 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
12175
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012176 if (obj->tiling_mode == I915_TILING_Y) {
12177 DRM_DEBUG("hardware does not support tiling Y\n");
Chris Wilson57cd6502010-08-08 12:34:44 +010012178 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012179 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012180
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012181 if (mode_cmd->pitches[0] & 63) {
12182 DRM_DEBUG("pitch (%d) must be at least 64 byte aligned\n",
12183 mode_cmd->pitches[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010012184 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012185 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012186
Chris Wilsona35cdaa2013-06-25 17:26:45 +010012187 if (INTEL_INFO(dev)->gen >= 5 && !IS_VALLEYVIEW(dev)) {
12188 pitch_limit = 32*1024;
12189 } else if (INTEL_INFO(dev)->gen >= 4) {
12190 if (obj->tiling_mode)
12191 pitch_limit = 16*1024;
12192 else
12193 pitch_limit = 32*1024;
12194 } else if (INTEL_INFO(dev)->gen >= 3) {
12195 if (obj->tiling_mode)
12196 pitch_limit = 8*1024;
12197 else
12198 pitch_limit = 16*1024;
12199 } else
12200 /* XXX DSPC is limited to 4k tiled */
12201 pitch_limit = 8*1024;
12202
12203 if (mode_cmd->pitches[0] > pitch_limit) {
12204 DRM_DEBUG("%s pitch (%d) must be at less than %d\n",
12205 obj->tiling_mode ? "tiled" : "linear",
12206 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012207 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012208 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012209
12210 if (obj->tiling_mode != I915_TILING_NONE &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012211 mode_cmd->pitches[0] != obj->stride) {
12212 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
12213 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012214 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012215 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020012216
Ville Syrjälä57779d02012-10-31 17:50:14 +020012217 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012218 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020012219 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012220 case DRM_FORMAT_RGB565:
12221 case DRM_FORMAT_XRGB8888:
12222 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012223 break;
12224 case DRM_FORMAT_XRGB1555:
12225 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012226 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012227 DRM_DEBUG("unsupported pixel format: %s\n",
12228 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012229 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012230 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020012231 break;
12232 case DRM_FORMAT_XBGR8888:
12233 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020012234 case DRM_FORMAT_XRGB2101010:
12235 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020012236 case DRM_FORMAT_XBGR2101010:
12237 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012238 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012239 DRM_DEBUG("unsupported pixel format: %s\n",
12240 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012241 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012242 }
Jesse Barnesb5626742011-06-24 12:19:27 -070012243 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020012244 case DRM_FORMAT_YUYV:
12245 case DRM_FORMAT_UYVY:
12246 case DRM_FORMAT_YVYU:
12247 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012248 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012249 DRM_DEBUG("unsupported pixel format: %s\n",
12250 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020012251 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000012252 }
Chris Wilson57cd6502010-08-08 12:34:44 +010012253 break;
12254 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000012255 DRM_DEBUG("unsupported pixel format: %s\n",
12256 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010012257 return -EINVAL;
12258 }
12259
Ville Syrjälä90f9a332012-10-31 17:50:19 +020012260 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12261 if (mode_cmd->offsets[0] != 0)
12262 return -EINVAL;
12263
Jesse Barnesa57ce0b2014-02-07 12:10:35 -080012264 aligned_height = intel_align_height(dev, mode_cmd->height,
12265 obj->tiling_mode);
Daniel Vetter53155c02013-10-09 21:55:33 +020012266 /* FIXME drm helper for size checks (especially planar formats)? */
12267 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
12268 return -EINVAL;
12269
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012270 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
12271 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020012272 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010012273
Jesse Barnes79e53942008-11-07 14:24:08 -080012274 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
12275 if (ret) {
12276 DRM_ERROR("framebuffer init failed %d\n", ret);
12277 return ret;
12278 }
12279
Jesse Barnes79e53942008-11-07 14:24:08 -080012280 return 0;
12281}
12282
Jesse Barnes79e53942008-11-07 14:24:08 -080012283static struct drm_framebuffer *
12284intel_user_framebuffer_create(struct drm_device *dev,
12285 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012286 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080012287{
Chris Wilson05394f32010-11-08 19:18:58 +000012288 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080012289
Jesse Barnes308e5bc2011-11-14 14:51:28 -080012290 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
12291 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000012292 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010012293 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080012294
Chris Wilsond2dff872011-04-19 08:36:26 +010012295 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080012296}
12297
Daniel Vetter4520f532013-10-09 09:18:51 +020012298#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020012299static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020012300{
12301}
12302#endif
12303
Jesse Barnes79e53942008-11-07 14:24:08 -080012304static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080012305 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020012306 .output_poll_changed = intel_fbdev_output_poll_changed,
Jesse Barnes79e53942008-11-07 14:24:08 -080012307};
12308
Jesse Barnese70236a2009-09-21 10:42:27 -070012309/* Set up chip specific display functions */
12310static void intel_init_display(struct drm_device *dev)
12311{
12312 struct drm_i915_private *dev_priv = dev->dev_private;
12313
Daniel Vetteree9300b2013-06-03 22:40:22 +020012314 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
12315 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030012316 else if (IS_CHERRYVIEW(dev))
12317 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020012318 else if (IS_VALLEYVIEW(dev))
12319 dev_priv->display.find_dpll = vlv_find_best_dpll;
12320 else if (IS_PINEVIEW(dev))
12321 dev_priv->display.find_dpll = pnv_find_best_dpll;
12322 else
12323 dev_priv->display.find_dpll = i9xx_find_best_dpll;
12324
Paulo Zanoniaffa9352012-11-23 15:30:39 -020012325 if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012326 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012327 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012328 dev_priv->display.crtc_mode_set = haswell_crtc_mode_set;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020012329 dev_priv->display.crtc_enable = haswell_crtc_enable;
12330 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030012331 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiau70d21f02013-07-03 21:06:04 +010012332 if (INTEL_INFO(dev)->gen >= 9)
12333 dev_priv->display.update_primary_plane =
12334 skylake_update_primary_plane;
12335 else
12336 dev_priv->display.update_primary_plane =
12337 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030012338 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012339 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Jesse Barnes4c6baa52014-03-07 08:57:50 -080012340 dev_priv->display.get_plane_config = ironlake_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012341 dev_priv->display.crtc_mode_set = ironlake_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012342 dev_priv->display.crtc_enable = ironlake_crtc_enable;
12343 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012344 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012345 dev_priv->display.update_primary_plane =
12346 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012347 } else if (IS_VALLEYVIEW(dev)) {
12348 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012349 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Jesse Barnes89b667f2013-04-18 14:51:36 -070012350 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
12351 dev_priv->display.crtc_enable = valleyview_crtc_enable;
12352 dev_priv->display.crtc_disable = i9xx_crtc_disable;
12353 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012354 dev_priv->display.update_primary_plane =
12355 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012356 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012357 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Jesse Barnes1ad292b2014-03-07 08:57:49 -080012358 dev_priv->display.get_plane_config = i9xx_get_plane_config;
Eric Anholtf564048e2011-03-30 13:01:02 -070012359 dev_priv->display.crtc_mode_set = i9xx_crtc_mode_set;
Daniel Vetter76e5a892012-06-29 22:39:33 +020012360 dev_priv->display.crtc_enable = i9xx_crtc_enable;
12361 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012362 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070012363 dev_priv->display.update_primary_plane =
12364 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070012365 }
Jesse Barnese70236a2009-09-21 10:42:27 -070012366
Jesse Barnese70236a2009-09-21 10:42:27 -070012367 /* Returns the core display clock speed */
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070012368 if (IS_VALLEYVIEW(dev))
12369 dev_priv->display.get_display_clock_speed =
12370 valleyview_get_display_clock_speed;
12371 else if (IS_I945G(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070012372 dev_priv->display.get_display_clock_speed =
12373 i945_get_display_clock_speed;
12374 else if (IS_I915G(dev))
12375 dev_priv->display.get_display_clock_speed =
12376 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012377 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012378 dev_priv->display.get_display_clock_speed =
12379 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020012380 else if (IS_PINEVIEW(dev))
12381 dev_priv->display.get_display_clock_speed =
12382 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070012383 else if (IS_I915GM(dev))
12384 dev_priv->display.get_display_clock_speed =
12385 i915gm_get_display_clock_speed;
12386 else if (IS_I865G(dev))
12387 dev_priv->display.get_display_clock_speed =
12388 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020012389 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070012390 dev_priv->display.get_display_clock_speed =
12391 i855_get_display_clock_speed;
12392 else /* 852, 830 */
12393 dev_priv->display.get_display_clock_speed =
12394 i830_get_display_clock_speed;
12395
Jani Nikula7c10a2b2014-10-27 16:26:43 +020012396 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012397 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012398 } else if (IS_GEN6(dev)) {
12399 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012400 dev_priv->display.modeset_global_resources =
12401 snb_modeset_global_resources;
12402 } else if (IS_IVYBRIDGE(dev)) {
12403 /* FIXME: detect B0+ stepping and use auto training */
12404 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012405 dev_priv->display.modeset_global_resources =
12406 ivb_modeset_global_resources;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030012407 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012408 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053012409 dev_priv->display.modeset_global_resources =
12410 haswell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012411 } else if (IS_VALLEYVIEW(dev)) {
12412 dev_priv->display.modeset_global_resources =
12413 valleyview_modeset_global_resources;
Satheeshakrishna M02c29252014-04-08 15:46:54 +053012414 } else if (INTEL_INFO(dev)->gen >= 9) {
Satheeshakrishna M02c29252014-04-08 15:46:54 +053012415 dev_priv->display.modeset_global_resources =
12416 haswell_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070012417 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012418
12419 /* Default just returns -ENODEV to indicate unsupported */
12420 dev_priv->display.queue_flip = intel_default_queue_flip;
12421
12422 switch (INTEL_INFO(dev)->gen) {
12423 case 2:
12424 dev_priv->display.queue_flip = intel_gen2_queue_flip;
12425 break;
12426
12427 case 3:
12428 dev_priv->display.queue_flip = intel_gen3_queue_flip;
12429 break;
12430
12431 case 4:
12432 case 5:
12433 dev_priv->display.queue_flip = intel_gen4_queue_flip;
12434 break;
12435
12436 case 6:
12437 dev_priv->display.queue_flip = intel_gen6_queue_flip;
12438 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012439 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070012440 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070012441 dev_priv->display.queue_flip = intel_gen7_queue_flip;
12442 break;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070012443 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020012444
12445 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030012446
12447 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070012448}
12449
Jesse Barnesb690e962010-07-19 13:53:12 -070012450/*
12451 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
12452 * resume, or other times. This quirk makes sure that's the case for
12453 * affected systems.
12454 */
Akshay Joshi0206e352011-08-16 15:34:10 -040012455static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070012456{
12457 struct drm_i915_private *dev_priv = dev->dev_private;
12458
12459 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012460 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012461}
12462
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012463static void quirk_pipeb_force(struct drm_device *dev)
12464{
12465 struct drm_i915_private *dev_priv = dev->dev_private;
12466
12467 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
12468 DRM_INFO("applying pipe b force quirk\n");
12469}
12470
Keith Packard435793d2011-07-12 14:56:22 -070012471/*
12472 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
12473 */
12474static void quirk_ssc_force_disable(struct drm_device *dev)
12475{
12476 struct drm_i915_private *dev_priv = dev->dev_private;
12477 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012478 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070012479}
12480
Carsten Emde4dca20e2012-03-15 15:56:26 +010012481/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010012482 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
12483 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010012484 */
12485static void quirk_invert_brightness(struct drm_device *dev)
12486{
12487 struct drm_i915_private *dev_priv = dev->dev_private;
12488 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020012489 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070012490}
12491
Scot Doyle9c72cc62014-07-03 23:27:50 +000012492/* Some VBT's incorrectly indicate no backlight is present */
12493static void quirk_backlight_present(struct drm_device *dev)
12494{
12495 struct drm_i915_private *dev_priv = dev->dev_private;
12496 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
12497 DRM_INFO("applying backlight present quirk\n");
12498}
12499
Jesse Barnesb690e962010-07-19 13:53:12 -070012500struct intel_quirk {
12501 int device;
12502 int subsystem_vendor;
12503 int subsystem_device;
12504 void (*hook)(struct drm_device *dev);
12505};
12506
Egbert Eich5f85f172012-10-14 15:46:38 +020012507/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
12508struct intel_dmi_quirk {
12509 void (*hook)(struct drm_device *dev);
12510 const struct dmi_system_id (*dmi_id_list)[];
12511};
12512
12513static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
12514{
12515 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
12516 return 1;
12517}
12518
12519static const struct intel_dmi_quirk intel_dmi_quirks[] = {
12520 {
12521 .dmi_id_list = &(const struct dmi_system_id[]) {
12522 {
12523 .callback = intel_dmi_reverse_brightness,
12524 .ident = "NCR Corporation",
12525 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
12526 DMI_MATCH(DMI_PRODUCT_NAME, ""),
12527 },
12528 },
12529 { } /* terminating entry */
12530 },
12531 .hook = quirk_invert_brightness,
12532 },
12533};
12534
Ben Widawskyc43b5632012-04-16 14:07:40 -070012535static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070012536 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040012537 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070012538
Jesse Barnesb690e962010-07-19 13:53:12 -070012539 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
12540 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
12541
Jesse Barnesb690e962010-07-19 13:53:12 -070012542 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
12543 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
12544
Ville Syrjälä5f080c02014-08-15 01:22:06 +030012545 /* 830 needs to leave pipe A & dpll A up */
12546 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
12547
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012548 /* 830 needs to leave pipe B & dpll B up */
12549 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
12550
Keith Packard435793d2011-07-12 14:56:22 -070012551 /* Lenovo U160 cannot use SSC on LVDS */
12552 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020012553
12554 /* Sony Vaio Y cannot use SSC on LVDS */
12555 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010012556
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010012557 /* Acer Aspire 5734Z must invert backlight brightness */
12558 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
12559
12560 /* Acer/eMachines G725 */
12561 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
12562
12563 /* Acer/eMachines e725 */
12564 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
12565
12566 /* Acer/Packard Bell NCL20 */
12567 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
12568
12569 /* Acer Aspire 4736Z */
12570 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020012571
12572 /* Acer Aspire 5336 */
12573 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000012574
12575 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
12576 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000012577
Scot Doyledfb3d47b2014-08-21 16:08:02 +000012578 /* Acer C720 Chromebook (Core i3 4005U) */
12579 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
12580
Scot Doyled4967d82014-07-03 23:27:52 +000012581 /* Toshiba CB35 Chromebook (Celeron 2955U) */
12582 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000012583
12584 /* HP Chromebook 14 (Celeron 2955U) */
12585 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070012586};
12587
12588static void intel_init_quirks(struct drm_device *dev)
12589{
12590 struct pci_dev *d = dev->pdev;
12591 int i;
12592
12593 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
12594 struct intel_quirk *q = &intel_quirks[i];
12595
12596 if (d->device == q->device &&
12597 (d->subsystem_vendor == q->subsystem_vendor ||
12598 q->subsystem_vendor == PCI_ANY_ID) &&
12599 (d->subsystem_device == q->subsystem_device ||
12600 q->subsystem_device == PCI_ANY_ID))
12601 q->hook(dev);
12602 }
Egbert Eich5f85f172012-10-14 15:46:38 +020012603 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
12604 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
12605 intel_dmi_quirks[i].hook(dev);
12606 }
Jesse Barnesb690e962010-07-19 13:53:12 -070012607}
12608
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012609/* Disable the VGA plane that we never use */
12610static void i915_disable_vga(struct drm_device *dev)
12611{
12612 struct drm_i915_private *dev_priv = dev->dev_private;
12613 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012614 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012615
Ville Syrjälä2b37c612014-01-22 21:32:38 +020012616 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012617 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070012618 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012619 sr1 = inb(VGA_SR_DATA);
12620 outb(sr1 | 1<<5, VGA_SR_DATA);
12621 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
12622 udelay(300);
12623
Ville Syrjälä69769f92014-08-15 01:22:08 +030012624 /*
12625 * Fujitsu-Siemens Lifebook S6010 (830) has problems resuming
12626 * from S3 without preserving (some of?) the other bits.
12627 */
12628 I915_WRITE(vga_reg, dev_priv->bios_vgacntr | VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012629 POSTING_READ(vga_reg);
12630}
12631
Daniel Vetterf8175862012-04-10 15:50:11 +020012632void intel_modeset_init_hw(struct drm_device *dev)
12633{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030012634 intel_prepare_ddi(dev);
12635
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030012636 if (IS_VALLEYVIEW(dev))
12637 vlv_update_cdclk(dev);
12638
Daniel Vetterf8175862012-04-10 15:50:11 +020012639 intel_init_clock_gating(dev);
12640
Daniel Vetter8090c6b2012-06-24 16:42:32 +020012641 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020012642}
12643
Jesse Barnes79e53942008-11-07 14:24:08 -080012644void intel_modeset_init(struct drm_device *dev)
12645{
Jesse Barnes652c3932009-08-17 13:31:43 -070012646 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000012647 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012648 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080012649 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080012650
12651 drm_mode_config_init(dev);
12652
12653 dev->mode_config.min_width = 0;
12654 dev->mode_config.min_height = 0;
12655
Dave Airlie019d96c2011-09-29 16:20:42 +010012656 dev->mode_config.preferred_depth = 24;
12657 dev->mode_config.prefer_shadow = 1;
12658
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020012659 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080012660
Jesse Barnesb690e962010-07-19 13:53:12 -070012661 intel_init_quirks(dev);
12662
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030012663 intel_init_pm(dev);
12664
Ben Widawskye3c74752013-04-05 13:12:39 -070012665 if (INTEL_INFO(dev)->num_pipes == 0)
12666 return;
12667
Jesse Barnese70236a2009-09-21 10:42:27 -070012668 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020012669 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070012670
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012671 if (IS_GEN2(dev)) {
12672 dev->mode_config.max_width = 2048;
12673 dev->mode_config.max_height = 2048;
12674 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070012675 dev->mode_config.max_width = 4096;
12676 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080012677 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010012678 dev->mode_config.max_width = 8192;
12679 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080012680 }
Damien Lespiau068be562014-03-28 14:17:49 +000012681
Ville Syrjälädc41c152014-08-13 11:57:05 +030012682 if (IS_845G(dev) || IS_I865G(dev)) {
12683 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
12684 dev->mode_config.cursor_height = 1023;
12685 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000012686 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
12687 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
12688 } else {
12689 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
12690 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
12691 }
12692
Ben Widawsky5d4545a2013-01-17 12:45:15 -080012693 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080012694
Zhao Yakui28c97732009-10-09 11:39:41 +080012695 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012696 INTEL_INFO(dev)->num_pipes,
12697 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080012698
Damien Lespiau055e3932014-08-18 13:49:10 +010012699 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000012700 intel_crtc_init(dev, pipe);
Damien Lespiau1fe47782014-03-03 17:31:47 +000012701 for_each_sprite(pipe, sprite) {
12702 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012703 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030012704 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000012705 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070012706 }
Jesse Barnes79e53942008-11-07 14:24:08 -080012707 }
12708
Jesse Barnesf42bb702013-12-16 16:34:23 -080012709 intel_init_dpio(dev);
12710
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012711 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012712
Ville Syrjälä69769f92014-08-15 01:22:08 +030012713 /* save the BIOS value before clobbering it */
12714 dev_priv->bios_vgacntr = I915_READ(i915_vgacntrl_reg(dev));
Jesse Barnes9cce37f2010-08-13 15:11:26 -070012715 /* Just disable it once at startup */
12716 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080012717 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000012718
12719 /* Just in case the BIOS is doing something questionable. */
12720 intel_disable_fbc(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012721
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012722 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080012723 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020012724 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012725
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012726 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080012727 if (!crtc->active)
12728 continue;
12729
Jesse Barnes46f297f2014-03-07 08:57:48 -080012730 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080012731 * Note that reserving the BIOS fb up front prevents us
12732 * from stuffing other stolen allocations like the ring
12733 * on top. This prevents some ugliness at boot time, and
12734 * can even allow for smooth boot transitions if the BIOS
12735 * fb is large enough for the active pipe configuration.
12736 */
12737 if (dev_priv->display.get_plane_config) {
12738 dev_priv->display.get_plane_config(crtc,
12739 &crtc->plane_config);
12740 /*
12741 * If the fb is shared between multiple heads, we'll
12742 * just get the first one.
12743 */
Jesse Barnes484b41d2014-03-07 08:57:55 -080012744 intel_find_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080012745 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080012746 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010012747}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080012748
Daniel Vetter7fad7982012-07-04 17:51:47 +020012749static void intel_enable_pipe_a(struct drm_device *dev)
12750{
12751 struct intel_connector *connector;
12752 struct drm_connector *crt = NULL;
12753 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030012754 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020012755
12756 /* We can't just switch on the pipe A, we need to set things up with a
12757 * proper mode and output configuration. As a gross hack, enable pipe A
12758 * by enabling the load detect pipe once. */
12759 list_for_each_entry(connector,
12760 &dev->mode_config.connector_list,
12761 base.head) {
12762 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
12763 crt = &connector->base;
12764 break;
12765 }
12766 }
12767
12768 if (!crt)
12769 return;
12770
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030012771 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
12772 intel_release_load_detect_pipe(crt, &load_detect_temp);
Daniel Vetter7fad7982012-07-04 17:51:47 +020012773}
12774
Daniel Vetterfa555832012-10-10 23:14:00 +020012775static bool
12776intel_check_plane_mapping(struct intel_crtc *crtc)
12777{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012778 struct drm_device *dev = crtc->base.dev;
12779 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012780 u32 reg, val;
12781
Ben Widawsky7eb552a2013-03-13 14:05:41 -070012782 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020012783 return true;
12784
12785 reg = DSPCNTR(!crtc->plane);
12786 val = I915_READ(reg);
12787
12788 if ((val & DISPLAY_PLANE_ENABLE) &&
12789 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
12790 return false;
12791
12792 return true;
12793}
12794
Daniel Vetter24929352012-07-02 20:28:59 +020012795static void intel_sanitize_crtc(struct intel_crtc *crtc)
12796{
12797 struct drm_device *dev = crtc->base.dev;
12798 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020012799 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020012800
Daniel Vetter24929352012-07-02 20:28:59 +020012801 /* Clear any frame start delays used for debugging left by the BIOS */
Daniel Vetter3b117c82013-04-17 20:15:07 +020012802 reg = PIPECONF(crtc->config.cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020012803 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
12804
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012805 /* restore vblank interrupts to correct state */
Ville Syrjäläd297e102014-08-06 14:50:01 +030012806 if (crtc->active) {
12807 update_scanline_offset(crtc);
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012808 drm_vblank_on(dev, crtc->pipe);
Ville Syrjäläd297e102014-08-06 14:50:01 +030012809 } else
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030012810 drm_vblank_off(dev, crtc->pipe);
12811
Daniel Vetter24929352012-07-02 20:28:59 +020012812 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020012813 * disable the crtc (and hence change the state) if it is wrong. Note
12814 * that gen4+ has a fixed plane -> pipe mapping. */
12815 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020012816 struct intel_connector *connector;
12817 bool plane;
12818
Daniel Vetter24929352012-07-02 20:28:59 +020012819 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
12820 crtc->base.base.id);
12821
12822 /* Pipe has the wrong plane attached and the plane is active.
12823 * Temporarily change the plane mapping and disable everything
12824 * ... */
12825 plane = crtc->plane;
12826 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020012827 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020012828 dev_priv->display.crtc_disable(&crtc->base);
12829 crtc->plane = plane;
12830
12831 /* ... and break all links. */
12832 list_for_each_entry(connector, &dev->mode_config.connector_list,
12833 base.head) {
12834 if (connector->encoder->base.crtc != &crtc->base)
12835 continue;
12836
Egbert Eich7f1950f2014-04-25 10:56:22 +020012837 connector->base.dpms = DRM_MODE_DPMS_OFF;
12838 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012839 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012840 /* multiple connectors may have the same encoder:
12841 * handle them and break crtc link separately */
12842 list_for_each_entry(connector, &dev->mode_config.connector_list,
12843 base.head)
12844 if (connector->encoder->base.crtc == &crtc->base) {
12845 connector->encoder->base.crtc = NULL;
12846 connector->encoder->connectors_active = false;
12847 }
Daniel Vetter24929352012-07-02 20:28:59 +020012848
12849 WARN_ON(crtc->active);
12850 crtc->base.enabled = false;
12851 }
Daniel Vetter24929352012-07-02 20:28:59 +020012852
Daniel Vetter7fad7982012-07-04 17:51:47 +020012853 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
12854 crtc->pipe == PIPE_A && !crtc->active) {
12855 /* BIOS forgot to enable pipe A, this mostly happens after
12856 * resume. Force-enable the pipe to fix this, the update_dpms
12857 * call below we restore the pipe to the right state, but leave
12858 * the required bits on. */
12859 intel_enable_pipe_a(dev);
12860 }
12861
Daniel Vetter24929352012-07-02 20:28:59 +020012862 /* Adjust the state of the output pipe according to whether we
12863 * have active connectors/encoders. */
12864 intel_crtc_update_dpms(&crtc->base);
12865
12866 if (crtc->active != crtc->base.enabled) {
12867 struct intel_encoder *encoder;
12868
12869 /* This can happen either due to bugs in the get_hw_state
12870 * functions or because the pipe is force-enabled due to the
12871 * pipe A quirk. */
12872 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
12873 crtc->base.base.id,
12874 crtc->base.enabled ? "enabled" : "disabled",
12875 crtc->active ? "enabled" : "disabled");
12876
12877 crtc->base.enabled = crtc->active;
12878
12879 /* Because we only establish the connector -> encoder ->
12880 * crtc links if something is active, this means the
12881 * crtc is now deactivated. Break the links. connector
12882 * -> encoder links are only establish when things are
12883 * actually up, hence no need to break them. */
12884 WARN_ON(crtc->active);
12885
12886 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
12887 WARN_ON(encoder->connectors_active);
12888 encoder->base.crtc = NULL;
12889 }
12890 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012891
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030012892 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010012893 /*
12894 * We start out with underrun reporting disabled to avoid races.
12895 * For correct bookkeeping mark this on active crtcs.
12896 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020012897 * Also on gmch platforms we dont have any hardware bits to
12898 * disable the underrun reporting. Which means we need to start
12899 * out with underrun reporting disabled also on inactive pipes,
12900 * since otherwise we'll complain about the garbage we read when
12901 * e.g. coming up after runtime pm.
12902 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010012903 * No protection against concurrent access is required - at
12904 * worst a fifo underrun happens which also sets this to false.
12905 */
12906 crtc->cpu_fifo_underrun_disabled = true;
12907 crtc->pch_fifo_underrun_disabled = true;
12908 }
Daniel Vetter24929352012-07-02 20:28:59 +020012909}
12910
12911static void intel_sanitize_encoder(struct intel_encoder *encoder)
12912{
12913 struct intel_connector *connector;
12914 struct drm_device *dev = encoder->base.dev;
12915
12916 /* We need to check both for a crtc link (meaning that the
12917 * encoder is active and trying to read from a pipe) and the
12918 * pipe itself being active. */
12919 bool has_active_crtc = encoder->base.crtc &&
12920 to_intel_crtc(encoder->base.crtc)->active;
12921
12922 if (encoder->connectors_active && !has_active_crtc) {
12923 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
12924 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012925 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012926
12927 /* Connector is active, but has no active pipe. This is
12928 * fallout from our resume register restoring. Disable
12929 * the encoder manually again. */
12930 if (encoder->base.crtc) {
12931 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
12932 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012933 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020012934 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030012935 if (encoder->post_disable)
12936 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020012937 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020012938 encoder->base.crtc = NULL;
12939 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020012940
12941 /* Inconsistent output/port/pipe state happens presumably due to
12942 * a bug in one of the get_hw_state functions. Or someplace else
12943 * in our code, like the register restore mess on resume. Clamp
12944 * things to off as a safer default. */
12945 list_for_each_entry(connector,
12946 &dev->mode_config.connector_list,
12947 base.head) {
12948 if (connector->encoder != encoder)
12949 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020012950 connector->base.dpms = DRM_MODE_DPMS_OFF;
12951 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020012952 }
12953 }
12954 /* Enabled encoders without active connectors will be fixed in
12955 * the crtc fixup. */
12956}
12957
Imre Deak04098752014-02-18 00:02:16 +020012958void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012959{
12960 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020012961 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012962
Imre Deak04098752014-02-18 00:02:16 +020012963 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
12964 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
12965 i915_disable_vga(dev);
12966 }
12967}
12968
12969void i915_redisable_vga(struct drm_device *dev)
12970{
12971 struct drm_i915_private *dev_priv = dev->dev_private;
12972
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012973 /* This function can be called both from intel_modeset_setup_hw_state or
12974 * at a very early point in our resume sequence, where the power well
12975 * structures are not yet restored. Since this function is at a very
12976 * paranoid "someone might have enabled VGA while we were not looking"
12977 * level, just check if the power well is enabled instead of trying to
12978 * follow the "don't touch the power well if we don't need it" policy
12979 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012980 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030012981 return;
12982
Imre Deak04098752014-02-18 00:02:16 +020012983 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010012984}
12985
Ville Syrjälä98ec7732014-04-30 17:43:01 +030012986static bool primary_get_hw_state(struct intel_crtc *crtc)
12987{
12988 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
12989
12990 if (!crtc->active)
12991 return false;
12992
12993 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
12994}
12995
Daniel Vetter30e984d2013-06-05 13:34:17 +020012996static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020012997{
12998 struct drm_i915_private *dev_priv = dev->dev_private;
12999 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020013000 struct intel_crtc *crtc;
13001 struct intel_encoder *encoder;
13002 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020013003 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020013004
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013005 for_each_intel_crtc(dev, crtc) {
Daniel Vetter88adfff2013-03-28 10:42:01 +010013006 memset(&crtc->config, 0, sizeof(crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020013007
Daniel Vetter99535992014-04-13 12:00:33 +020013008 crtc->config.quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
13009
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010013010 crtc->active = dev_priv->display.get_pipe_config(crtc,
13011 &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013012
13013 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030013014 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020013015
13016 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
13017 crtc->base.base.id,
13018 crtc->active ? "enabled" : "disabled");
13019 }
13020
Daniel Vetter53589012013-06-05 13:34:16 +020013021 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13022 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13023
13024 pll->on = pll->get_hw_state(dev_priv, pll, &pll->hw_state);
13025 pll->active = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013026 for_each_intel_crtc(dev, crtc) {
Daniel Vetter53589012013-06-05 13:34:16 +020013027 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
13028 pll->active++;
13029 }
13030 pll->refcount = pll->active;
13031
Daniel Vetter35c95372013-07-17 06:55:04 +020013032 DRM_DEBUG_KMS("%s hw state readout: refcount %i, on %i\n",
13033 pll->name, pll->refcount, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013034
13035 if (pll->refcount)
13036 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020013037 }
13038
Damien Lespiaub2784e12014-08-05 11:29:37 +010013039 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013040 pipe = 0;
13041
13042 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070013043 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13044 encoder->base.crtc = &crtc->base;
Daniel Vetter1d37b682013-11-18 09:00:59 +010013045 encoder->get_config(encoder, &crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020013046 } else {
13047 encoder->base.crtc = NULL;
13048 }
13049
13050 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013051 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020013052 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030013053 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013054 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010013055 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020013056 }
13057
13058 list_for_each_entry(connector, &dev->mode_config.connector_list,
13059 base.head) {
13060 if (connector->get_hw_state(connector)) {
13061 connector->base.dpms = DRM_MODE_DPMS_ON;
13062 connector->encoder->connectors_active = true;
13063 connector->base.encoder = &connector->encoder->base;
13064 } else {
13065 connector->base.dpms = DRM_MODE_DPMS_OFF;
13066 connector->base.encoder = NULL;
13067 }
13068 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
13069 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013070 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020013071 connector->base.encoder ? "enabled" : "disabled");
13072 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020013073}
13074
13075/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
13076 * and i915 state tracking structures. */
13077void intel_modeset_setup_hw_state(struct drm_device *dev,
13078 bool force_restore)
13079{
13080 struct drm_i915_private *dev_priv = dev->dev_private;
13081 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013082 struct intel_crtc *crtc;
13083 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020013084 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020013085
13086 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020013087
Jesse Barnesbabea612013-06-26 18:57:38 +030013088 /*
13089 * Now that we have the config, copy it to each CRTC struct
13090 * Note that this could go away if we move to using crtc_config
13091 * checking everywhere.
13092 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013093 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020013094 if (crtc->active && i915.fastboot) {
Daniel Vetterf6a83282014-02-11 15:28:57 -080013095 intel_mode_from_pipe_config(&crtc->base.mode, &crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030013096 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
13097 crtc->base.base.id);
13098 drm_mode_debug_printmodeline(&crtc->base.mode);
13099 }
13100 }
13101
Daniel Vetter24929352012-07-02 20:28:59 +020013102 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010013103 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020013104 intel_sanitize_encoder(encoder);
13105 }
13106
Damien Lespiau055e3932014-08-18 13:49:10 +010013107 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020013108 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
13109 intel_sanitize_crtc(crtc);
Daniel Vetterc0b03412013-05-28 12:05:54 +020013110 intel_dump_pipe_config(crtc, &crtc->config, "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020013111 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013112
Daniel Vetter35c95372013-07-17 06:55:04 +020013113 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
13114 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
13115
13116 if (!pll->on || pll->active)
13117 continue;
13118
13119 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
13120
13121 pll->disable(dev_priv, pll);
13122 pll->on = false;
13123 }
13124
Ville Syrjälä96f90c52013-12-05 15:51:38 +020013125 if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030013126 ilk_wm_get_hw_state(dev);
13127
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013128 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030013129 i915_redisable_vga(dev);
13130
Daniel Vetterf30da182013-04-11 20:22:50 +020013131 /*
13132 * We need to use raw interfaces for restoring state to avoid
13133 * checking (bogus) intermediate states.
13134 */
Damien Lespiau055e3932014-08-18 13:49:10 +010013135 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070013136 struct drm_crtc *crtc =
13137 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020013138
13139 __intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y,
Matt Roperf4510a22014-04-01 15:22:40 -070013140 crtc->primary->fb);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010013141 }
13142 } else {
13143 intel_modeset_update_staged_output_state(dev);
13144 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020013145
13146 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010013147}
13148
13149void intel_modeset_gem_init(struct drm_device *dev)
13150{
Jesse Barnes484b41d2014-03-07 08:57:55 -080013151 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070013152 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013153
Imre Deakae484342014-03-31 15:10:44 +030013154 mutex_lock(&dev->struct_mutex);
13155 intel_init_gt_powersave(dev);
13156 mutex_unlock(&dev->struct_mutex);
13157
Chris Wilson1833b132012-05-09 11:56:28 +010013158 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020013159
13160 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080013161
13162 /*
13163 * Make sure any fbs we allocated at startup are properly
13164 * pinned & fenced. When we do the allocation it's too early
13165 * for this.
13166 */
13167 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010013168 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070013169 obj = intel_fb_obj(c->primary->fb);
13170 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080013171 continue;
13172
Matt Roper2ff8fde2014-07-08 07:50:07 -070013173 if (intel_pin_and_fence_fb_obj(dev, obj, NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080013174 DRM_ERROR("failed to pin boot fb on pipe %d\n",
13175 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100013176 drm_framebuffer_unreference(c->primary->fb);
13177 c->primary->fb = NULL;
Jesse Barnes484b41d2014-03-07 08:57:55 -080013178 }
13179 }
13180 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013181}
13182
Imre Deak4932e2c2014-02-11 17:12:48 +020013183void intel_connector_unregister(struct intel_connector *intel_connector)
13184{
13185 struct drm_connector *connector = &intel_connector->base;
13186
13187 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010013188 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020013189}
13190
Jesse Barnes79e53942008-11-07 14:24:08 -080013191void intel_modeset_cleanup(struct drm_device *dev)
13192{
Jesse Barnes652c3932009-08-17 13:31:43 -070013193 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030013194 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070013195
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013196 /*
13197 * Interrupts and polling as the first thing to avoid creating havoc.
13198 * Too much stuff here (turning of rps, connectors, ...) would
13199 * experience fancy races otherwise.
13200 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020013201 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070013202
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013203 /*
13204 * Due to the hpd irq storm handling the hotplug work can re-arm the
13205 * poll handlers. Hence disable polling after hpd handling is shut down.
13206 */
Keith Packardf87ea762010-10-03 19:36:26 -070013207 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020013208
Jesse Barnes652c3932009-08-17 13:31:43 -070013209 mutex_lock(&dev->struct_mutex);
13210
Jesse Barnes723bfd72010-10-07 16:01:13 -070013211 intel_unregister_dsm_handler();
13212
Chris Wilson973d04f2011-07-08 12:22:37 +010013213 intel_disable_fbc(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070013214
Daniel Vetter8090c6b2012-06-24 16:42:32 +020013215 intel_disable_gt_powersave(dev);
Chris Wilson0cdab212010-12-05 17:27:06 +000013216
Daniel Vetter930ebb42012-06-29 23:32:16 +020013217 ironlake_teardown_rc6(dev);
13218
Kristian Høgsberg69341a52009-11-11 12:19:17 -050013219 mutex_unlock(&dev->struct_mutex);
13220
Chris Wilson1630fe72011-07-08 12:22:42 +010013221 /* flush any delayed tasks or pending work */
13222 flush_scheduled_work();
13223
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013224 /* destroy the backlight and sysfs files before encoders/connectors */
13225 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020013226 struct intel_connector *intel_connector;
13227
13228 intel_connector = to_intel_connector(connector);
13229 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020013230 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030013231
Jesse Barnes79e53942008-11-07 14:24:08 -080013232 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010013233
13234 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030013235
13236 mutex_lock(&dev->struct_mutex);
13237 intel_cleanup_gt_powersave(dev);
13238 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013239}
13240
Dave Airlie28d52042009-09-21 14:33:58 +100013241/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080013242 * Return which encoder is currently attached for connector.
13243 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010013244struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080013245{
Chris Wilsondf0e9242010-09-09 16:20:55 +010013246 return &intel_attached_encoder(connector)->base;
13247}
Jesse Barnes79e53942008-11-07 14:24:08 -080013248
Chris Wilsondf0e9242010-09-09 16:20:55 +010013249void intel_connector_attach_encoder(struct intel_connector *connector,
13250 struct intel_encoder *encoder)
13251{
13252 connector->encoder = encoder;
13253 drm_mode_connector_attach_encoder(&connector->base,
13254 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080013255}
Dave Airlie28d52042009-09-21 14:33:58 +100013256
13257/*
13258 * set vga decode state - true == enable VGA decode
13259 */
13260int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
13261{
13262 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000013263 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100013264 u16 gmch_ctrl;
13265
Chris Wilson75fa0412014-02-07 18:37:02 -020013266 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
13267 DRM_ERROR("failed to read control word\n");
13268 return -EIO;
13269 }
13270
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020013271 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
13272 return 0;
13273
Dave Airlie28d52042009-09-21 14:33:58 +100013274 if (state)
13275 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
13276 else
13277 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020013278
13279 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
13280 DRM_ERROR("failed to write control word\n");
13281 return -EIO;
13282 }
13283
Dave Airlie28d52042009-09-21 14:33:58 +100013284 return 0;
13285}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013286
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013287struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013288
13289 u32 power_well_driver;
13290
Chris Wilson63b66e52013-08-08 15:12:06 +020013291 int num_transcoders;
13292
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013293 struct intel_cursor_error_state {
13294 u32 control;
13295 u32 position;
13296 u32 base;
13297 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010013298 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013299
13300 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013301 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013302 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030013303 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010013304 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013305
13306 struct intel_plane_error_state {
13307 u32 control;
13308 u32 stride;
13309 u32 size;
13310 u32 pos;
13311 u32 addr;
13312 u32 surface;
13313 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010013314 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020013315
13316 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020013317 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020013318 enum transcoder cpu_transcoder;
13319
13320 u32 conf;
13321
13322 u32 htotal;
13323 u32 hblank;
13324 u32 hsync;
13325 u32 vtotal;
13326 u32 vblank;
13327 u32 vsync;
13328 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013329};
13330
13331struct intel_display_error_state *
13332intel_display_capture_error_state(struct drm_device *dev)
13333{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013334 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013335 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020013336 int transcoders[] = {
13337 TRANSCODER_A,
13338 TRANSCODER_B,
13339 TRANSCODER_C,
13340 TRANSCODER_EDP,
13341 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013342 int i;
13343
Chris Wilson63b66e52013-08-08 15:12:06 +020013344 if (INTEL_INFO(dev)->num_pipes == 0)
13345 return NULL;
13346
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013347 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013348 if (error == NULL)
13349 return NULL;
13350
Imre Deak190be112013-11-25 17:15:31 +020013351 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013352 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
13353
Damien Lespiau055e3932014-08-18 13:49:10 +010013354 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020013355 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013356 __intel_display_power_is_enabled(dev_priv,
13357 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020013358 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013359 continue;
13360
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030013361 error->cursor[i].control = I915_READ(CURCNTR(i));
13362 error->cursor[i].position = I915_READ(CURPOS(i));
13363 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013364
13365 error->plane[i].control = I915_READ(DSPCNTR(i));
13366 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013367 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030013368 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013369 error->plane[i].pos = I915_READ(DSPPOS(i));
13370 }
Paulo Zanonica291362013-03-06 20:03:14 -030013371 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
13372 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013373 if (INTEL_INFO(dev)->gen >= 4) {
13374 error->plane[i].surface = I915_READ(DSPSURF(i));
13375 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
13376 }
13377
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013378 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030013379
Sonika Jindal3abfce72014-07-21 15:23:43 +053013380 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030013381 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020013382 }
13383
13384 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
13385 if (HAS_DDI(dev_priv->dev))
13386 error->num_transcoders++; /* Account for eDP. */
13387
13388 for (i = 0; i < error->num_transcoders; i++) {
13389 enum transcoder cpu_transcoder = transcoders[i];
13390
Imre Deakddf9c532013-11-27 22:02:02 +020013391 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013392 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020013393 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013394 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020013395 continue;
13396
Chris Wilson63b66e52013-08-08 15:12:06 +020013397 error->transcoder[i].cpu_transcoder = cpu_transcoder;
13398
13399 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
13400 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
13401 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
13402 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
13403 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
13404 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
13405 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013406 }
13407
13408 return error;
13409}
13410
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013411#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13412
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013413void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013414intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013415 struct drm_device *dev,
13416 struct intel_display_error_state *error)
13417{
Damien Lespiau055e3932014-08-18 13:49:10 +010013418 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013419 int i;
13420
Chris Wilson63b66e52013-08-08 15:12:06 +020013421 if (!error)
13422 return;
13423
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013424 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020013425 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013426 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030013427 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010013428 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013429 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020013430 err_printf(m, " Power: %s\n",
13431 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013432 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030013433 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013434
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013435 err_printf(m, "Plane [%d]:\n", i);
13436 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
13437 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013438 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013439 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
13440 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030013441 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030013442 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013443 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013444 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013445 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
13446 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013447 }
13448
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030013449 err_printf(m, "Cursor [%d]:\n", i);
13450 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
13451 err_printf(m, " POS: %08x\n", error->cursor[i].position);
13452 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013453 }
Chris Wilson63b66e52013-08-08 15:12:06 +020013454
13455 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010013456 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020013457 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020013458 err_printf(m, " Power: %s\n",
13459 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020013460 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
13461 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
13462 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
13463 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
13464 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
13465 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
13466 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
13467 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000013468}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013469
13470void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
13471{
13472 struct intel_crtc *crtc;
13473
13474 for_each_intel_crtc(dev, crtc) {
13475 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013476
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020013477 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013478
13479 work = crtc->unpin_work;
13480
13481 if (work && work->event &&
13482 work->event->base.file_priv == file) {
13483 kfree(work->event);
13484 work->event = NULL;
13485 }
13486
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020013487 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030013488 }
13489}