blob: ef58a775373e642cbc9e5c4f0501426946f9b02e [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"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats supported by all gen */
49#define COMMON_PRIMARY_FORMATS \
50 DRM_FORMAT_C8, \
51 DRM_FORMAT_RGB565, \
52 DRM_FORMAT_XRGB8888, \
53 DRM_FORMAT_ARGB8888
54
55/* Primary plane formats for gen <= 3 */
56static const uint32_t intel_primary_formats_gen2[] = {
57 COMMON_PRIMARY_FORMATS,
58 DRM_FORMAT_XRGB1555,
59 DRM_FORMAT_ARGB1555,
60};
61
62/* Primary plane formats for gen >= 4 */
63static const uint32_t intel_primary_formats_gen4[] = {
64 COMMON_PRIMARY_FORMATS, \
65 DRM_FORMAT_XBGR8888,
66 DRM_FORMAT_ABGR8888,
67 DRM_FORMAT_XRGB2101010,
68 DRM_FORMAT_ARGB2101010,
69 DRM_FORMAT_XBGR2101010,
70 DRM_FORMAT_ABGR2101010,
71};
72
Matt Roper3d7d6512014-06-10 08:28:13 -070073/* Cursor formats */
74static const uint32_t intel_cursor_formats[] = {
75 DRM_FORMAT_ARGB8888,
76};
77
Chris Wilson6b383a72010-09-13 13:54:26 +010078static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080079
Jesse Barnesf1f644d2013-06-27 00:39:25 +030080static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020081 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030082static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020083 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084
Damien Lespiaue7457a92013-08-08 22:28:59 +010085static int intel_set_mode(struct drm_crtc *crtc, struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020086 int x, int y, struct drm_framebuffer *old_fb,
87 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080088static int intel_framebuffer_init(struct drm_device *dev,
89 struct intel_framebuffer *ifb,
90 struct drm_mode_fb_cmd2 *mode_cmd,
91 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020092static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
93static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020094static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070095 struct intel_link_m_n *m_n,
96 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020097static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +020098static void haswell_set_pipeconf(struct drm_crtc *crtc);
99static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200100static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200101 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200102static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200103 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800104static void intel_begin_crtc_commit(struct drm_crtc *crtc);
105static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700106static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
107 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200108static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
109 int num_connectors);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100110
Dave Airlie0e32b392014-05-02 14:02:48 +1000111static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
112{
113 if (!connector->mst_port)
114 return connector->encoder;
115 else
116 return &connector->mst_port->mst_encoders[pipe]->base;
117}
118
Jesse Barnes79e53942008-11-07 14:24:08 -0800119typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400120 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800121} intel_range_t;
122
123typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400124 int dot_limit;
125 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126} intel_p2_t;
127
Ma Lingd4906092009-03-18 20:13:27 +0800128typedef struct intel_limit intel_limit_t;
129struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400130 intel_range_t dot, vco, n, m, m1, m2, p, p1;
131 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800132};
Jesse Barnes79e53942008-11-07 14:24:08 -0800133
Daniel Vetterd2acd212012-10-20 20:57:43 +0200134int
135intel_pch_rawclk(struct drm_device *dev)
136{
137 struct drm_i915_private *dev_priv = dev->dev_private;
138
139 WARN_ON(!HAS_PCH_SPLIT(dev));
140
141 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
142}
143
Chris Wilson021357a2010-09-07 20:54:59 +0100144static inline u32 /* units of 100MHz */
145intel_fdi_link_freq(struct drm_device *dev)
146{
Chris Wilson8b99e682010-10-13 09:59:17 +0100147 if (IS_GEN5(dev)) {
148 struct drm_i915_private *dev_priv = dev->dev_private;
149 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
150 } else
151 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100152}
153
Daniel Vetter5d536e22013-07-06 12:52:06 +0200154static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400155 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200156 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200157 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400158 .m = { .min = 96, .max = 140 },
159 .m1 = { .min = 18, .max = 26 },
160 .m2 = { .min = 6, .max = 16 },
161 .p = { .min = 4, .max = 128 },
162 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700163 .p2 = { .dot_limit = 165000,
164 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700165};
166
Daniel Vetter5d536e22013-07-06 12:52:06 +0200167static const intel_limit_t intel_limits_i8xx_dvo = {
168 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200169 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200170 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200171 .m = { .min = 96, .max = 140 },
172 .m1 = { .min = 18, .max = 26 },
173 .m2 = { .min = 6, .max = 16 },
174 .p = { .min = 4, .max = 128 },
175 .p1 = { .min = 2, .max = 33 },
176 .p2 = { .dot_limit = 165000,
177 .p2_slow = 4, .p2_fast = 4 },
178};
179
Keith Packarde4b36692009-06-05 19:22:17 -0700180static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400181 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200182 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200183 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400184 .m = { .min = 96, .max = 140 },
185 .m1 = { .min = 18, .max = 26 },
186 .m2 = { .min = 6, .max = 16 },
187 .p = { .min = 4, .max = 128 },
188 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700189 .p2 = { .dot_limit = 165000,
190 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700191};
Eric Anholt273e27c2011-03-30 13:01:10 -0700192
Keith Packarde4b36692009-06-05 19:22:17 -0700193static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400194 .dot = { .min = 20000, .max = 400000 },
195 .vco = { .min = 1400000, .max = 2800000 },
196 .n = { .min = 1, .max = 6 },
197 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100198 .m1 = { .min = 8, .max = 18 },
199 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400200 .p = { .min = 5, .max = 80 },
201 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700202 .p2 = { .dot_limit = 200000,
203 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700204};
205
206static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400207 .dot = { .min = 20000, .max = 400000 },
208 .vco = { .min = 1400000, .max = 2800000 },
209 .n = { .min = 1, .max = 6 },
210 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100211 .m1 = { .min = 8, .max = 18 },
212 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400213 .p = { .min = 7, .max = 98 },
214 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700215 .p2 = { .dot_limit = 112000,
216 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700217};
218
Eric Anholt273e27c2011-03-30 13:01:10 -0700219
Keith Packarde4b36692009-06-05 19:22:17 -0700220static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700221 .dot = { .min = 25000, .max = 270000 },
222 .vco = { .min = 1750000, .max = 3500000},
223 .n = { .min = 1, .max = 4 },
224 .m = { .min = 104, .max = 138 },
225 .m1 = { .min = 17, .max = 23 },
226 .m2 = { .min = 5, .max = 11 },
227 .p = { .min = 10, .max = 30 },
228 .p1 = { .min = 1, .max = 3},
229 .p2 = { .dot_limit = 270000,
230 .p2_slow = 10,
231 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800232 },
Keith Packarde4b36692009-06-05 19:22:17 -0700233};
234
235static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700236 .dot = { .min = 22000, .max = 400000 },
237 .vco = { .min = 1750000, .max = 3500000},
238 .n = { .min = 1, .max = 4 },
239 .m = { .min = 104, .max = 138 },
240 .m1 = { .min = 16, .max = 23 },
241 .m2 = { .min = 5, .max = 11 },
242 .p = { .min = 5, .max = 80 },
243 .p1 = { .min = 1, .max = 8},
244 .p2 = { .dot_limit = 165000,
245 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700246};
247
248static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700249 .dot = { .min = 20000, .max = 115000 },
250 .vco = { .min = 1750000, .max = 3500000 },
251 .n = { .min = 1, .max = 3 },
252 .m = { .min = 104, .max = 138 },
253 .m1 = { .min = 17, .max = 23 },
254 .m2 = { .min = 5, .max = 11 },
255 .p = { .min = 28, .max = 112 },
256 .p1 = { .min = 2, .max = 8 },
257 .p2 = { .dot_limit = 0,
258 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800259 },
Keith Packarde4b36692009-06-05 19:22:17 -0700260};
261
262static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700263 .dot = { .min = 80000, .max = 224000 },
264 .vco = { .min = 1750000, .max = 3500000 },
265 .n = { .min = 1, .max = 3 },
266 .m = { .min = 104, .max = 138 },
267 .m1 = { .min = 17, .max = 23 },
268 .m2 = { .min = 5, .max = 11 },
269 .p = { .min = 14, .max = 42 },
270 .p1 = { .min = 2, .max = 6 },
271 .p2 = { .dot_limit = 0,
272 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800273 },
Keith Packarde4b36692009-06-05 19:22:17 -0700274};
275
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500276static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400277 .dot = { .min = 20000, .max = 400000},
278 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700279 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400280 .n = { .min = 3, .max = 6 },
281 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700282 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .m1 = { .min = 0, .max = 0 },
284 .m2 = { .min = 0, .max = 254 },
285 .p = { .min = 5, .max = 80 },
286 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 .p2 = { .dot_limit = 200000,
288 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700289};
290
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500291static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400292 .dot = { .min = 20000, .max = 400000 },
293 .vco = { .min = 1700000, .max = 3500000 },
294 .n = { .min = 3, .max = 6 },
295 .m = { .min = 2, .max = 256 },
296 .m1 = { .min = 0, .max = 0 },
297 .m2 = { .min = 0, .max = 254 },
298 .p = { .min = 7, .max = 112 },
299 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700300 .p2 = { .dot_limit = 112000,
301 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700302};
303
Eric Anholt273e27c2011-03-30 13:01:10 -0700304/* Ironlake / Sandybridge
305 *
306 * We calculate clock using (register_value + 2) for N/M1/M2, so here
307 * the range value for them is (actual_value - 2).
308 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800309static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700310 .dot = { .min = 25000, .max = 350000 },
311 .vco = { .min = 1760000, .max = 3510000 },
312 .n = { .min = 1, .max = 5 },
313 .m = { .min = 79, .max = 127 },
314 .m1 = { .min = 12, .max = 22 },
315 .m2 = { .min = 5, .max = 9 },
316 .p = { .min = 5, .max = 80 },
317 .p1 = { .min = 1, .max = 8 },
318 .p2 = { .dot_limit = 225000,
319 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700320};
321
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800322static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700323 .dot = { .min = 25000, .max = 350000 },
324 .vco = { .min = 1760000, .max = 3510000 },
325 .n = { .min = 1, .max = 3 },
326 .m = { .min = 79, .max = 118 },
327 .m1 = { .min = 12, .max = 22 },
328 .m2 = { .min = 5, .max = 9 },
329 .p = { .min = 28, .max = 112 },
330 .p1 = { .min = 2, .max = 8 },
331 .p2 = { .dot_limit = 225000,
332 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800333};
334
335static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700336 .dot = { .min = 25000, .max = 350000 },
337 .vco = { .min = 1760000, .max = 3510000 },
338 .n = { .min = 1, .max = 3 },
339 .m = { .min = 79, .max = 127 },
340 .m1 = { .min = 12, .max = 22 },
341 .m2 = { .min = 5, .max = 9 },
342 .p = { .min = 14, .max = 56 },
343 .p1 = { .min = 2, .max = 8 },
344 .p2 = { .dot_limit = 225000,
345 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800346};
347
Eric Anholt273e27c2011-03-30 13:01:10 -0700348/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800349static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700350 .dot = { .min = 25000, .max = 350000 },
351 .vco = { .min = 1760000, .max = 3510000 },
352 .n = { .min = 1, .max = 2 },
353 .m = { .min = 79, .max = 126 },
354 .m1 = { .min = 12, .max = 22 },
355 .m2 = { .min = 5, .max = 9 },
356 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400357 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700358 .p2 = { .dot_limit = 225000,
359 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800360};
361
362static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .dot = { .min = 25000, .max = 350000 },
364 .vco = { .min = 1760000, .max = 3510000 },
365 .n = { .min = 1, .max = 3 },
366 .m = { .min = 79, .max = 126 },
367 .m1 = { .min = 12, .max = 22 },
368 .m2 = { .min = 5, .max = 9 },
369 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400370 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700371 .p2 = { .dot_limit = 225000,
372 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800373};
374
Ville Syrjälädc730512013-09-24 21:26:30 +0300375static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300376 /*
377 * These are the data rate limits (measured in fast clocks)
378 * since those are the strictest limits we have. The fast
379 * clock and actual rate limits are more relaxed, so checking
380 * them would make no difference.
381 */
382 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200383 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700384 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700385 .m1 = { .min = 2, .max = 3 },
386 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300387 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300388 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700389};
390
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300391static const intel_limit_t intel_limits_chv = {
392 /*
393 * These are the data rate limits (measured in fast clocks)
394 * since those are the strictest limits we have. The fast
395 * clock and actual rate limits are more relaxed, so checking
396 * them would make no difference.
397 */
398 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200399 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300400 .n = { .min = 1, .max = 1 },
401 .m1 = { .min = 2, .max = 2 },
402 .m2 = { .min = 24 << 22, .max = 175 << 22 },
403 .p1 = { .min = 2, .max = 4 },
404 .p2 = { .p2_slow = 1, .p2_fast = 14 },
405};
406
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200407static const intel_limit_t intel_limits_bxt = {
408 /* FIXME: find real dot limits */
409 .dot = { .min = 0, .max = INT_MAX },
410 .vco = { .min = 4800000, .max = 6480000 },
411 .n = { .min = 1, .max = 1 },
412 .m1 = { .min = 2, .max = 2 },
413 /* FIXME: find real m2 limits */
414 .m2 = { .min = 2 << 22, .max = 255 << 22 },
415 .p1 = { .min = 2, .max = 4 },
416 .p2 = { .p2_slow = 1, .p2_fast = 20 },
417};
418
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300419static void vlv_clock(int refclk, intel_clock_t *clock)
420{
421 clock->m = clock->m1 * clock->m2;
422 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200423 if (WARN_ON(clock->n == 0 || clock->p == 0))
424 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300425 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
426 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300427}
428
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300429/**
430 * Returns whether any output on the specified pipe is of the specified type
431 */
Damien Lespiau40935612014-10-29 11:16:59 +0000432bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300433{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300434 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300435 struct intel_encoder *encoder;
436
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300437 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300438 if (encoder->type == type)
439 return true;
440
441 return false;
442}
443
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200444/**
445 * Returns whether any output on the specified pipe will have the specified
446 * type after a staged modeset is complete, i.e., the same as
447 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
448 * encoder->crtc.
449 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200450static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
451 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200452{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200453 struct drm_atomic_state *state = crtc_state->base.state;
454 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200455 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200456 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 for (i = 0; i < state->num_connector; i++) {
459 if (!state->connectors[i])
460 continue;
461
462 connector_state = state->connector_states[i];
463 if (connector_state->crtc != crtc_state->base.crtc)
464 continue;
465
466 num_connectors++;
467
468 encoder = to_intel_encoder(connector_state->best_encoder);
469 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200470 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200471 }
472
473 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200474
475 return false;
476}
477
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200478static const intel_limit_t *
479intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800480{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200481 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800482 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800483
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200484 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100485 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000486 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800487 limit = &intel_limits_ironlake_dual_lvds_100m;
488 else
489 limit = &intel_limits_ironlake_dual_lvds;
490 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000491 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_single_lvds_100m;
493 else
494 limit = &intel_limits_ironlake_single_lvds;
495 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200496 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800497 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800498
499 return limit;
500}
501
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200502static const intel_limit_t *
503intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800504{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200505 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 const intel_limit_t *limit;
507
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200508 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100509 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800511 else
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200513 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
514 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200516 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800518 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700519 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800520
521 return limit;
522}
523
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200524static const intel_limit_t *
525intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800526{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200527 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800528 const intel_limit_t *limit;
529
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200530 if (IS_BROXTON(dev))
531 limit = &intel_limits_bxt;
532 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800534 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500536 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500538 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800539 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500540 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300541 } else if (IS_CHERRYVIEW(dev)) {
542 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700543 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300544 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100545 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100547 limit = &intel_limits_i9xx_lvds;
548 else
549 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800550 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200551 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700552 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700554 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200555 else
556 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800557 }
558 return limit;
559}
560
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500561/* m1 is reserved as 0 in Pineview, n is a ring counter */
562static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800563{
Shaohua Li21778322009-02-23 15:19:16 +0800564 clock->m = clock->m2 + 2;
565 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200566 if (WARN_ON(clock->n == 0 || clock->p == 0))
567 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300568 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
569 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800570}
571
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200572static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
573{
574 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
575}
576
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200577static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800578{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200579 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800580 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200581 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
582 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300583 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
584 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800585}
586
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300587static void chv_clock(int refclk, intel_clock_t *clock)
588{
589 clock->m = clock->m1 * clock->m2;
590 clock->p = clock->p1 * clock->p2;
591 if (WARN_ON(clock->n == 0 || clock->p == 0))
592 return;
593 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
594 clock->n << 22);
595 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
596}
597
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800598#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800599/**
600 * Returns whether the given set of divisors are valid for a given refclk with
601 * the given connectors.
602 */
603
Chris Wilson1b894b52010-12-14 20:04:54 +0000604static bool intel_PLL_is_valid(struct drm_device *dev,
605 const intel_limit_t *limit,
606 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800607{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300608 if (clock->n < limit->n.min || limit->n.max < clock->n)
609 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800610 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400611 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300616
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200617 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618 if (clock->m1 <= clock->m2)
619 INTELPllInvalid("m1 <= m2\n");
620
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200621 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300622 if (clock->p < limit->p.min || limit->p.max < clock->p)
623 INTELPllInvalid("p out of range\n");
624 if (clock->m < limit->m.min || limit->m.max < clock->m)
625 INTELPllInvalid("m out of range\n");
626 }
627
Jesse Barnes79e53942008-11-07 14:24:08 -0800628 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400629 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
631 * connector, etc., rather than just a single range.
632 */
633 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635
636 return true;
637}
638
Ma Lingd4906092009-03-18 20:13:27 +0800639static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200640i9xx_find_best_dpll(const intel_limit_t *limit,
641 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800642 int target, int refclk, intel_clock_t *match_clock,
643 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800644{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200645 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300646 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800647 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800648 int err = target;
649
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200650 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100652 * 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.
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100656 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 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
Akshay Joshi0206e352011-08-16 15:34:10 -0400667 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800668
Zhao Yakui42158662009-11-20 11:24:18 +0800669 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 Vetterc0efc382013-06-03 20:56:24 +0200673 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800674 break;
675 for (clock.n = limit->n.min;
676 clock.n <= limit->n.max; clock.n++) {
677 for (clock.p1 = limit->p1.min;
678 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800679 int this_err;
680
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200681 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000682 if (!intel_PLL_is_valid(dev, limit,
683 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800684 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800685 if (match_clock &&
686 clock.p != match_clock->p)
687 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800688
689 this_err = abs(clock.dot - target);
690 if (this_err < err) {
691 *best_clock = clock;
692 err = this_err;
693 }
694 }
695 }
696 }
697 }
698
699 return (err != target);
700}
701
Ma Lingd4906092009-03-18 20:13:27 +0800702static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200703pnv_find_best_dpll(const intel_limit_t *limit,
704 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200705 int target, int refclk, intel_clock_t *match_clock,
706 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200707{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200708 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300709 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200710 intel_clock_t clock;
711 int err = target;
712
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200713 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200714 /*
715 * For LVDS just rely on its current settings for dual-channel.
716 * We haven't figured out how to reliably set up different
717 * single/dual channel state, if we even can.
718 */
719 if (intel_is_dual_link_lvds(dev))
720 clock.p2 = limit->p2.p2_fast;
721 else
722 clock.p2 = limit->p2.p2_slow;
723 } else {
724 if (target < limit->p2.dot_limit)
725 clock.p2 = limit->p2.p2_slow;
726 else
727 clock.p2 = limit->p2.p2_fast;
728 }
729
730 memset(best_clock, 0, sizeof(*best_clock));
731
732 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
733 clock.m1++) {
734 for (clock.m2 = limit->m2.min;
735 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200736 for (clock.n = limit->n.min;
737 clock.n <= limit->n.max; clock.n++) {
738 for (clock.p1 = limit->p1.min;
739 clock.p1 <= limit->p1.max; clock.p1++) {
740 int this_err;
741
742 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800743 if (!intel_PLL_is_valid(dev, limit,
744 &clock))
745 continue;
746 if (match_clock &&
747 clock.p != match_clock->p)
748 continue;
749
750 this_err = abs(clock.dot - target);
751 if (this_err < err) {
752 *best_clock = clock;
753 err = this_err;
754 }
755 }
756 }
757 }
758 }
759
760 return (err != target);
761}
762
Ma Lingd4906092009-03-18 20:13:27 +0800763static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200764g4x_find_best_dpll(const intel_limit_t *limit,
765 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200766 int target, int refclk, intel_clock_t *match_clock,
767 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800768{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200769 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300770 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800771 intel_clock_t clock;
772 int max_n;
773 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400774 /* approximately equals target * 0.00585 */
775 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800776 found = false;
777
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200778 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100779 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800780 clock.p2 = limit->p2.p2_fast;
781 else
782 clock.p2 = limit->p2.p2_slow;
783 } else {
784 if (target < limit->p2.dot_limit)
785 clock.p2 = limit->p2.p2_slow;
786 else
787 clock.p2 = limit->p2.p2_fast;
788 }
789
790 memset(best_clock, 0, sizeof(*best_clock));
791 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200792 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800793 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200794 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800795 for (clock.m1 = limit->m1.max;
796 clock.m1 >= limit->m1.min; clock.m1--) {
797 for (clock.m2 = limit->m2.max;
798 clock.m2 >= limit->m2.min; clock.m2--) {
799 for (clock.p1 = limit->p1.max;
800 clock.p1 >= limit->p1.min; clock.p1--) {
801 int this_err;
802
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200803 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000804 if (!intel_PLL_is_valid(dev, limit,
805 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800806 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000807
808 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800809 if (this_err < err_most) {
810 *best_clock = clock;
811 err_most = this_err;
812 max_n = clock.n;
813 found = true;
814 }
815 }
816 }
817 }
818 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800819 return found;
820}
Ma Lingd4906092009-03-18 20:13:27 +0800821
Imre Deakd5dd62b2015-03-17 11:40:03 +0200822/*
823 * Check if the calculated PLL configuration is more optimal compared to the
824 * best configuration and error found so far. Return the calculated error.
825 */
826static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
827 const intel_clock_t *calculated_clock,
828 const intel_clock_t *best_clock,
829 unsigned int best_error_ppm,
830 unsigned int *error_ppm)
831{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200832 /*
833 * For CHV ignore the error and consider only the P value.
834 * Prefer a bigger P value based on HW requirements.
835 */
836 if (IS_CHERRYVIEW(dev)) {
837 *error_ppm = 0;
838
839 return calculated_clock->p > best_clock->p;
840 }
841
Imre Deak24be4e42015-03-17 11:40:04 +0200842 if (WARN_ON_ONCE(!target_freq))
843 return false;
844
Imre Deakd5dd62b2015-03-17 11:40:03 +0200845 *error_ppm = div_u64(1000000ULL *
846 abs(target_freq - calculated_clock->dot),
847 target_freq);
848 /*
849 * Prefer a better P value over a better (smaller) error if the error
850 * is small. Ensure this preference for future configurations too by
851 * setting the error to 0.
852 */
853 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
854 *error_ppm = 0;
855
856 return true;
857 }
858
859 return *error_ppm + 10 < best_error_ppm;
860}
861
Zhenyu Wang2c072452009-06-05 15:38:42 +0800862static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200863vlv_find_best_dpll(const intel_limit_t *limit,
864 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200865 int target, int refclk, intel_clock_t *match_clock,
866 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700867{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200868 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300869 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300870 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300871 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300872 /* min update 19.2 MHz */
873 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300874 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700875
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300876 target *= 5; /* fast clock */
877
878 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700879
880 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300881 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300882 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300883 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300884 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300885 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700886 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300887 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200888 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300889
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300890 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
891 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300892
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300893 vlv_clock(refclk, &clock);
894
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300895 if (!intel_PLL_is_valid(dev, limit,
896 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300897 continue;
898
Imre Deakd5dd62b2015-03-17 11:40:03 +0200899 if (!vlv_PLL_is_optimal(dev, target,
900 &clock,
901 best_clock,
902 bestppm, &ppm))
903 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300904
Imre Deakd5dd62b2015-03-17 11:40:03 +0200905 *best_clock = clock;
906 bestppm = ppm;
907 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700908 }
909 }
910 }
911 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700912
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300913 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700914}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700915
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300916static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200917chv_find_best_dpll(const intel_limit_t *limit,
918 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300919 int target, int refclk, intel_clock_t *match_clock,
920 intel_clock_t *best_clock)
921{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200922 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300923 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200924 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300925 intel_clock_t clock;
926 uint64_t m2;
927 int found = false;
928
929 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200930 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300931
932 /*
933 * Based on hardware doc, the n always set to 1, and m1 always
934 * set to 2. If requires to support 200Mhz refclk, we need to
935 * revisit this because n may not 1 anymore.
936 */
937 clock.n = 1, clock.m1 = 2;
938 target *= 5; /* fast clock */
939
940 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
941 for (clock.p2 = limit->p2.p2_fast;
942 clock.p2 >= limit->p2.p2_slow;
943 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200944 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300945
946 clock.p = clock.p1 * clock.p2;
947
948 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
949 clock.n) << 22, refclk * clock.m1);
950
951 if (m2 > INT_MAX/clock.m1)
952 continue;
953
954 clock.m2 = m2;
955
956 chv_clock(refclk, &clock);
957
958 if (!intel_PLL_is_valid(dev, limit, &clock))
959 continue;
960
Imre Deak9ca3ba02015-03-17 11:40:05 +0200961 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
962 best_error_ppm, &error_ppm))
963 continue;
964
965 *best_clock = clock;
966 best_error_ppm = error_ppm;
967 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300968 }
969 }
970
971 return found;
972}
973
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200974bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
975 intel_clock_t *best_clock)
976{
977 int refclk = i9xx_get_refclk(crtc_state, 0);
978
979 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
980 target_clock, refclk, NULL, best_clock);
981}
982
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300983bool intel_crtc_active(struct drm_crtc *crtc)
984{
985 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
986
987 /* Be paranoid as we can arrive here with only partial
988 * state retrieved from the hardware during setup.
989 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100990 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300991 * as Haswell has gained clock readout/fastboot support.
992 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000993 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300994 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700995 *
996 * FIXME: The intel_crtc->active here should be switched to
997 * crtc->state->active once we have proper CRTC states wired up
998 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300999 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001000 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001001 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001002}
1003
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001004enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1005 enum pipe pipe)
1006{
1007 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1008 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1009
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001010 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001011}
1012
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001013static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1014{
1015 struct drm_i915_private *dev_priv = dev->dev_private;
1016 u32 reg = PIPEDSL(pipe);
1017 u32 line1, line2;
1018 u32 line_mask;
1019
1020 if (IS_GEN2(dev))
1021 line_mask = DSL_LINEMASK_GEN2;
1022 else
1023 line_mask = DSL_LINEMASK_GEN3;
1024
1025 line1 = I915_READ(reg) & line_mask;
1026 mdelay(5);
1027 line2 = I915_READ(reg) & line_mask;
1028
1029 return line1 == line2;
1030}
1031
Keith Packardab7ad7f2010-10-03 00:33:06 -07001032/*
1033 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001034 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001035 *
1036 * After disabling a pipe, we can't wait for vblank in the usual way,
1037 * spinning on the vblank interrupt status bit, since we won't actually
1038 * see an interrupt when the pipe is disabled.
1039 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001040 * On Gen4 and above:
1041 * wait for the pipe register state bit to turn off
1042 *
1043 * Otherwise:
1044 * wait for the display line value to settle (it usually
1045 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001046 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001047 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001048static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001050 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001051 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001052 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001053 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001054
Keith Packardab7ad7f2010-10-03 00:33:06 -07001055 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001056 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057
Keith Packardab7ad7f2010-10-03 00:33:06 -07001058 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001059 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1060 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001061 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001063 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001064 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001065 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001066 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001067}
1068
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001069/*
1070 * ibx_digital_port_connected - is the specified port connected?
1071 * @dev_priv: i915 private structure
1072 * @port: the port to test
1073 *
1074 * Returns true if @port is connected, false otherwise.
1075 */
1076bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1077 struct intel_digital_port *port)
1078{
1079 u32 bit;
1080
Damien Lespiauc36346e2012-12-13 16:09:03 +00001081 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001082 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001083 case PORT_B:
1084 bit = SDE_PORTB_HOTPLUG;
1085 break;
1086 case PORT_C:
1087 bit = SDE_PORTC_HOTPLUG;
1088 break;
1089 case PORT_D:
1090 bit = SDE_PORTD_HOTPLUG;
1091 break;
1092 default:
1093 return true;
1094 }
1095 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001096 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001097 case PORT_B:
1098 bit = SDE_PORTB_HOTPLUG_CPT;
1099 break;
1100 case PORT_C:
1101 bit = SDE_PORTC_HOTPLUG_CPT;
1102 break;
1103 case PORT_D:
1104 bit = SDE_PORTD_HOTPLUG_CPT;
1105 break;
1106 default:
1107 return true;
1108 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001109 }
1110
1111 return I915_READ(SDEISR) & bit;
1112}
1113
Jesse Barnesb24e7172011-01-04 15:09:30 -08001114static const char *state_string(bool enabled)
1115{
1116 return enabled ? "on" : "off";
1117}
1118
1119/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001120void assert_pll(struct drm_i915_private *dev_priv,
1121 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001122{
1123 int reg;
1124 u32 val;
1125 bool cur_state;
1126
1127 reg = DPLL(pipe);
1128 val = I915_READ(reg);
1129 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001130 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001131 "PLL state assertion failure (expected %s, current %s)\n",
1132 state_string(state), state_string(cur_state));
1133}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134
Jani Nikula23538ef2013-08-27 15:12:22 +03001135/* XXX: the dsi pll is shared between MIPI DSI ports */
1136static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1137{
1138 u32 val;
1139 bool cur_state;
1140
1141 mutex_lock(&dev_priv->dpio_lock);
1142 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1143 mutex_unlock(&dev_priv->dpio_lock);
1144
1145 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001146 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001147 "DSI PLL state assertion failure (expected %s, current %s)\n",
1148 state_string(state), state_string(cur_state));
1149}
1150#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1151#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1152
Daniel Vetter55607e82013-06-16 21:42:39 +02001153struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001154intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001155{
Daniel Vettere2b78262013-06-07 23:10:03 +02001156 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1157
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001158 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001159 return NULL;
1160
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001161 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001162}
1163
Jesse Barnesb24e7172011-01-04 15:09:30 -08001164/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001165void assert_shared_dpll(struct drm_i915_private *dev_priv,
1166 struct intel_shared_dpll *pll,
1167 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001168{
Jesse Barnes040484a2011-01-03 12:14:26 -08001169 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001170 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001171
Chris Wilson92b27b02012-05-20 18:10:50 +01001172 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001173 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001174 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001175
Daniel Vetter53589012013-06-05 13:34:16 +02001176 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001177 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001178 "%s assertion failure (expected %s, current %s)\n",
1179 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001180}
Jesse Barnes040484a2011-01-03 12:14:26 -08001181
1182static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1183 enum pipe pipe, bool state)
1184{
1185 int reg;
1186 u32 val;
1187 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001188 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1189 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001190
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001191 if (HAS_DDI(dev_priv->dev)) {
1192 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001193 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001194 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001195 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001196 } else {
1197 reg = FDI_TX_CTL(pipe);
1198 val = I915_READ(reg);
1199 cur_state = !!(val & FDI_TX_ENABLE);
1200 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001201 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001202 "FDI TX state assertion failure (expected %s, current %s)\n",
1203 state_string(state), state_string(cur_state));
1204}
1205#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1206#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1207
1208static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1209 enum pipe pipe, bool state)
1210{
1211 int reg;
1212 u32 val;
1213 bool cur_state;
1214
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001215 reg = FDI_RX_CTL(pipe);
1216 val = I915_READ(reg);
1217 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001218 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001219 "FDI RX state assertion failure (expected %s, current %s)\n",
1220 state_string(state), state_string(cur_state));
1221}
1222#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1223#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1224
1225static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1226 enum pipe pipe)
1227{
1228 int reg;
1229 u32 val;
1230
1231 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001232 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001233 return;
1234
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001235 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001236 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001237 return;
1238
Jesse Barnes040484a2011-01-03 12:14:26 -08001239 reg = FDI_TX_CTL(pipe);
1240 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001241 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001242}
1243
Daniel Vetter55607e82013-06-16 21:42:39 +02001244void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1245 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001246{
1247 int reg;
1248 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001249 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001250
1251 reg = FDI_RX_CTL(pipe);
1252 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001253 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001254 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001255 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1256 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001257}
1258
Daniel Vetterb680c372014-09-19 18:27:27 +02001259void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1260 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001261{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001262 struct drm_device *dev = dev_priv->dev;
1263 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264 u32 val;
1265 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001266 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001267
Jani Nikulabedd4db2014-08-22 15:04:13 +03001268 if (WARN_ON(HAS_DDI(dev)))
1269 return;
1270
1271 if (HAS_PCH_SPLIT(dev)) {
1272 u32 port_sel;
1273
Jesse Barnesea0760c2011-01-04 15:09:32 -08001274 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001275 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1276
1277 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1278 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1279 panel_pipe = PIPE_B;
1280 /* XXX: else fix for eDP */
1281 } else if (IS_VALLEYVIEW(dev)) {
1282 /* presumably write lock depends on pipe, not port select */
1283 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1284 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001285 } else {
1286 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001287 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1288 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001289 }
1290
1291 val = I915_READ(pp_reg);
1292 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001293 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001294 locked = false;
1295
Rob Clarke2c719b2014-12-15 13:56:32 -05001296 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001297 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001298 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001299}
1300
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001301static void assert_cursor(struct drm_i915_private *dev_priv,
1302 enum pipe pipe, bool state)
1303{
1304 struct drm_device *dev = dev_priv->dev;
1305 bool cur_state;
1306
Paulo Zanonid9d82082014-02-27 16:30:56 -03001307 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001308 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001309 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001310 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001311
Rob Clarke2c719b2014-12-15 13:56:32 -05001312 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001313 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1314 pipe_name(pipe), state_string(state), state_string(cur_state));
1315}
1316#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1317#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1318
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001319void assert_pipe(struct drm_i915_private *dev_priv,
1320 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001321{
1322 int reg;
1323 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001324 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001325 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1326 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001327
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001328 /* if we need the pipe quirk it must be always on */
1329 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1330 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001331 state = true;
1332
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001333 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001334 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001335 cur_state = false;
1336 } else {
1337 reg = PIPECONF(cpu_transcoder);
1338 val = I915_READ(reg);
1339 cur_state = !!(val & PIPECONF_ENABLE);
1340 }
1341
Rob Clarke2c719b2014-12-15 13:56:32 -05001342 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001343 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001344 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001345}
1346
Chris Wilson931872f2012-01-16 23:01:13 +00001347static void assert_plane(struct drm_i915_private *dev_priv,
1348 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001349{
1350 int reg;
1351 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001352 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353
1354 reg = DSPCNTR(plane);
1355 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001356 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001357 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001358 "plane %c assertion failure (expected %s, current %s)\n",
1359 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001360}
1361
Chris Wilson931872f2012-01-16 23:01:13 +00001362#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1363#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1364
Jesse Barnesb24e7172011-01-04 15:09:30 -08001365static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1366 enum pipe pipe)
1367{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001368 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001369 int reg, i;
1370 u32 val;
1371 int cur_pipe;
1372
Ville Syrjälä653e1022013-06-04 13:49:05 +03001373 /* Primary planes are fixed to pipes on gen4+ */
1374 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001375 reg = DSPCNTR(pipe);
1376 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001377 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001378 "plane %c assertion failure, should be disabled but not\n",
1379 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001380 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001381 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001382
Jesse Barnesb24e7172011-01-04 15:09:30 -08001383 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001384 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385 reg = DSPCNTR(i);
1386 val = I915_READ(reg);
1387 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1388 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001389 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001390 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1391 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001392 }
1393}
1394
Jesse Barnes19332d72013-03-28 09:55:38 -07001395static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1396 enum pipe pipe)
1397{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001398 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001399 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001400 u32 val;
1401
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001402 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001403 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001404 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001405 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1407 sprite, pipe_name(pipe));
1408 }
1409 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001410 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001411 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001412 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001413 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001415 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001416 }
1417 } else if (INTEL_INFO(dev)->gen >= 7) {
1418 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001419 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001420 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001421 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001422 plane_name(pipe), pipe_name(pipe));
1423 } else if (INTEL_INFO(dev)->gen >= 5) {
1424 reg = DVSCNTR(pipe);
1425 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001426 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001427 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1428 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001429 }
1430}
1431
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001432static void assert_vblank_disabled(struct drm_crtc *crtc)
1433{
Rob Clarke2c719b2014-12-15 13:56:32 -05001434 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001435 drm_crtc_vblank_put(crtc);
1436}
1437
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001438static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001439{
1440 u32 val;
1441 bool enabled;
1442
Rob Clarke2c719b2014-12-15 13:56:32 -05001443 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001444
Jesse Barnes92f25842011-01-04 15:09:34 -08001445 val = I915_READ(PCH_DREF_CONTROL);
1446 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1447 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001448 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001449}
1450
Daniel Vetterab9412b2013-05-03 11:49:46 +02001451static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1452 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001453{
1454 int reg;
1455 u32 val;
1456 bool enabled;
1457
Daniel Vetterab9412b2013-05-03 11:49:46 +02001458 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001459 val = I915_READ(reg);
1460 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001461 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001462 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1463 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001464}
1465
Keith Packard4e634382011-08-06 10:39:45 -07001466static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1467 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001468{
1469 if ((val & DP_PORT_EN) == 0)
1470 return false;
1471
1472 if (HAS_PCH_CPT(dev_priv->dev)) {
1473 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1474 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1475 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1476 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001477 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1478 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1479 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001480 } else {
1481 if ((val & DP_PIPE_MASK) != (pipe << 30))
1482 return false;
1483 }
1484 return true;
1485}
1486
Keith Packard1519b992011-08-06 10:35:34 -07001487static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1488 enum pipe pipe, u32 val)
1489{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001490 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001491 return false;
1492
1493 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001494 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001495 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001496 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1497 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1498 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001499 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001500 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001501 return false;
1502 }
1503 return true;
1504}
1505
1506static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1507 enum pipe pipe, u32 val)
1508{
1509 if ((val & LVDS_PORT_EN) == 0)
1510 return false;
1511
1512 if (HAS_PCH_CPT(dev_priv->dev)) {
1513 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1514 return false;
1515 } else {
1516 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1517 return false;
1518 }
1519 return true;
1520}
1521
1522static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1523 enum pipe pipe, u32 val)
1524{
1525 if ((val & ADPA_DAC_ENABLE) == 0)
1526 return false;
1527 if (HAS_PCH_CPT(dev_priv->dev)) {
1528 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1529 return false;
1530 } else {
1531 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1532 return false;
1533 }
1534 return true;
1535}
1536
Jesse Barnes291906f2011-02-02 12:28:03 -08001537static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001538 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001539{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001540 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001541 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001542 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001543 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001544
Rob Clarke2c719b2014-12-15 13:56:32 -05001545 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001546 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001547 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001548}
1549
1550static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1551 enum pipe pipe, int reg)
1552{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001553 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001554 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001555 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001556 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001557
Rob Clarke2c719b2014-12-15 13:56:32 -05001558 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001559 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001560 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001561}
1562
1563static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1564 enum pipe pipe)
1565{
1566 int reg;
1567 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001568
Keith Packardf0575e92011-07-25 22:12:43 -07001569 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1570 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1571 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001572
1573 reg = PCH_ADPA;
1574 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001575 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001576 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001577 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001578
1579 reg = PCH_LVDS;
1580 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001581 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001582 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001583 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001584
Paulo Zanonie2debe92013-02-18 19:00:27 -03001585 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1586 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1587 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001588}
1589
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001590static void intel_init_dpio(struct drm_device *dev)
1591{
1592 struct drm_i915_private *dev_priv = dev->dev_private;
1593
1594 if (!IS_VALLEYVIEW(dev))
1595 return;
1596
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001597 /*
1598 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1599 * CHV x1 PHY (DP/HDMI D)
1600 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1601 */
1602 if (IS_CHERRYVIEW(dev)) {
1603 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1604 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1605 } else {
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1607 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001608}
1609
Ville Syrjäläd288f652014-10-28 13:20:22 +02001610static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001611 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001612{
Daniel Vetter426115c2013-07-11 22:13:42 +02001613 struct drm_device *dev = crtc->base.dev;
1614 struct drm_i915_private *dev_priv = dev->dev_private;
1615 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001616 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001617
Daniel Vetter426115c2013-07-11 22:13:42 +02001618 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001619
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001621 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1622
1623 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001624 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001625 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001626
Daniel Vetter426115c2013-07-11 22:13:42 +02001627 I915_WRITE(reg, dpll);
1628 POSTING_READ(reg);
1629 udelay(150);
1630
1631 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1632 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1633
Ville Syrjäläd288f652014-10-28 13:20:22 +02001634 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001635 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001636
1637 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001638 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001639 POSTING_READ(reg);
1640 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001641 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001642 POSTING_READ(reg);
1643 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001644 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001645 POSTING_READ(reg);
1646 udelay(150); /* wait for warmup */
1647}
1648
Ville Syrjäläd288f652014-10-28 13:20:22 +02001649static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001650 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001651{
1652 struct drm_device *dev = crtc->base.dev;
1653 struct drm_i915_private *dev_priv = dev->dev_private;
1654 int pipe = crtc->pipe;
1655 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001656 u32 tmp;
1657
1658 assert_pipe_disabled(dev_priv, crtc->pipe);
1659
1660 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1661
1662 mutex_lock(&dev_priv->dpio_lock);
1663
1664 /* Enable back the 10bit clock to display controller */
1665 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1666 tmp |= DPIO_DCLKP_EN;
1667 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1668
1669 /*
1670 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1671 */
1672 udelay(1);
1673
1674 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001675 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001676
1677 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001678 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001679 DRM_ERROR("PLL %d failed to lock\n", pipe);
1680
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001681 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001682 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001683 POSTING_READ(DPLL_MD(pipe));
1684
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001685 mutex_unlock(&dev_priv->dpio_lock);
1686}
1687
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001688static int intel_num_dvo_pipes(struct drm_device *dev)
1689{
1690 struct intel_crtc *crtc;
1691 int count = 0;
1692
1693 for_each_intel_crtc(dev, crtc)
1694 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001695 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001696
1697 return count;
1698}
1699
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001700static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001701{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001702 struct drm_device *dev = crtc->base.dev;
1703 struct drm_i915_private *dev_priv = dev->dev_private;
1704 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001705 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001706
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001707 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001708
1709 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001710 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001711
1712 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001713 if (IS_MOBILE(dev) && !IS_I830(dev))
1714 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001715
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001716 /* Enable DVO 2x clock on both PLLs if necessary */
1717 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1718 /*
1719 * It appears to be important that we don't enable this
1720 * for the current pipe before otherwise configuring the
1721 * PLL. No idea how this should be handled if multiple
1722 * DVO outputs are enabled simultaneosly.
1723 */
1724 dpll |= DPLL_DVO_2X_MODE;
1725 I915_WRITE(DPLL(!crtc->pipe),
1726 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1727 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001728
1729 /* Wait for the clocks to stabilize. */
1730 POSTING_READ(reg);
1731 udelay(150);
1732
1733 if (INTEL_INFO(dev)->gen >= 4) {
1734 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001735 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001736 } else {
1737 /* The pixel multiplier can only be updated once the
1738 * DPLL is enabled and the clocks are stable.
1739 *
1740 * So write it again.
1741 */
1742 I915_WRITE(reg, dpll);
1743 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001744
1745 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001746 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747 POSTING_READ(reg);
1748 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001749 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001750 POSTING_READ(reg);
1751 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001752 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001753 POSTING_READ(reg);
1754 udelay(150); /* wait for warmup */
1755}
1756
1757/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001758 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001759 * @dev_priv: i915 private structure
1760 * @pipe: pipe PLL to disable
1761 *
1762 * Disable the PLL for @pipe, making sure the pipe is off first.
1763 *
1764 * Note! This is for pre-ILK only.
1765 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001766static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001767{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001768 struct drm_device *dev = crtc->base.dev;
1769 struct drm_i915_private *dev_priv = dev->dev_private;
1770 enum pipe pipe = crtc->pipe;
1771
1772 /* Disable DVO 2x clock on both PLLs if necessary */
1773 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001774 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001775 intel_num_dvo_pipes(dev) == 1) {
1776 I915_WRITE(DPLL(PIPE_B),
1777 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1778 I915_WRITE(DPLL(PIPE_A),
1779 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1780 }
1781
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001782 /* Don't disable pipe or pipe PLLs if needed */
1783 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1784 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001785 return;
1786
1787 /* Make sure the pipe isn't still relying on us */
1788 assert_pipe_disabled(dev_priv, pipe);
1789
Daniel Vetter50b44a42013-06-05 13:34:33 +02001790 I915_WRITE(DPLL(pipe), 0);
1791 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001792}
1793
Jesse Barnesf6071162013-10-01 10:41:38 -07001794static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1795{
1796 u32 val = 0;
1797
1798 /* Make sure the pipe isn't still relying on us */
1799 assert_pipe_disabled(dev_priv, pipe);
1800
Imre Deake5cbfbf2014-01-09 17:08:16 +02001801 /*
1802 * Leave integrated clock source and reference clock enabled for pipe B.
1803 * The latter is needed for VGA hotplug / manual detection.
1804 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001805 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001806 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001807 I915_WRITE(DPLL(pipe), val);
1808 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001809
1810}
1811
1812static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1813{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001814 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001815 u32 val;
1816
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001817 /* Make sure the pipe isn't still relying on us */
1818 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001819
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001820 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001821 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001822 if (pipe != PIPE_A)
1823 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1824 I915_WRITE(DPLL(pipe), val);
1825 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001826
1827 mutex_lock(&dev_priv->dpio_lock);
1828
1829 /* Disable 10bit clock to display controller */
1830 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1831 val &= ~DPIO_DCLKP_EN;
1832 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1833
Ville Syrjälä61407f62014-05-27 16:32:55 +03001834 /* disable left/right clock distribution */
1835 if (pipe != PIPE_B) {
1836 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1837 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1838 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1839 } else {
1840 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1841 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1842 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1843 }
1844
Ville Syrjäläd7520482014-04-09 13:28:59 +03001845 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001846}
1847
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001848void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
1849 struct intel_digital_port *dport)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001850{
1851 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001852 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001853
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001854 switch (dport->port) {
1855 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001857 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001858 break;
1859 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001860 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001861 dpll_reg = DPLL(0);
1862 break;
1863 case PORT_D:
1864 port_mask = DPLL_PORTD_READY_MASK;
1865 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001866 break;
1867 default:
1868 BUG();
1869 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001870
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001871 if (wait_for((I915_READ(dpll_reg) & port_mask) == 0, 1000))
Jesse Barnes89b667f2013-04-18 14:51:36 -07001872 WARN(1, "timed out waiting for port %c ready: 0x%08x\n",
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001873 port_name(dport->port), I915_READ(dpll_reg));
Jesse Barnes89b667f2013-04-18 14:51:36 -07001874}
1875
Daniel Vetterb14b1052014-04-24 23:55:13 +02001876static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1877{
1878 struct drm_device *dev = crtc->base.dev;
1879 struct drm_i915_private *dev_priv = dev->dev_private;
1880 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1881
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001882 if (WARN_ON(pll == NULL))
1883 return;
1884
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001885 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001886 if (pll->active == 0) {
1887 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1888 WARN_ON(pll->on);
1889 assert_shared_dpll_disabled(dev_priv, pll);
1890
1891 pll->mode_set(dev_priv, pll);
1892 }
1893}
1894
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001895/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001896 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001897 * @dev_priv: i915 private structure
1898 * @pipe: pipe PLL to enable
1899 *
1900 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1901 * drives the transcoder clock.
1902 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001903static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001904{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001905 struct drm_device *dev = crtc->base.dev;
1906 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001907 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001908
Daniel Vetter87a875b2013-06-05 13:34:19 +02001909 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001910 return;
1911
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001912 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001913 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001914
Damien Lespiau74dd6922014-07-29 18:06:17 +01001915 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001916 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001917 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001918
Daniel Vettercdbd2312013-06-05 13:34:03 +02001919 if (pll->active++) {
1920 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001921 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001922 return;
1923 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001924 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001925
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001926 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1927
Daniel Vetter46edb022013-06-05 13:34:12 +02001928 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001929 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001930 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001931}
1932
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001933static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001934{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001935 struct drm_device *dev = crtc->base.dev;
1936 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001937 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001938
Jesse Barnes92f25842011-01-04 15:09:34 -08001939 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001940 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001941 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001942 return;
1943
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001944 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001945 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001946
Daniel Vetter46edb022013-06-05 13:34:12 +02001947 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1948 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001949 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Chris Wilson48da64a2012-05-13 20:16:12 +01001951 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001952 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001953 return;
1954 }
1955
Daniel Vettere9d69442013-06-05 13:34:15 +02001956 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001957 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001958 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001959 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001960
Daniel Vetter46edb022013-06-05 13:34:12 +02001961 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001962 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001963 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001964
1965 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001966}
1967
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001968static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1969 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001970{
Daniel Vetter23670b322012-11-01 09:15:30 +01001971 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001972 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001973 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001974 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001975
1976 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001977 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001978
1979 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001980 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001981 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* FDI must be feeding us bits for PCH ports */
1984 assert_fdi_tx_enabled(dev_priv, pipe);
1985 assert_fdi_rx_enabled(dev_priv, pipe);
1986
Daniel Vetter23670b322012-11-01 09:15:30 +01001987 if (HAS_PCH_CPT(dev)) {
1988 /* Workaround: Set the timing override bit before enabling the
1989 * pch transcoder. */
1990 reg = TRANS_CHICKEN2(pipe);
1991 val = I915_READ(reg);
1992 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1993 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001994 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001995
Daniel Vetterab9412b2013-05-03 11:49:46 +02001996 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001997 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02001998 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07001999
2000 if (HAS_PCH_IBX(dev_priv->dev)) {
2001 /*
2002 * make the BPC in transcoder be consistent with
2003 * that in pipeconf reg.
2004 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002005 val &= ~PIPECONF_BPC_MASK;
2006 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002007 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002008
2009 val &= ~TRANS_INTERLACE_MASK;
2010 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002011 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002012 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002013 val |= TRANS_LEGACY_INTERLACED_ILK;
2014 else
2015 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002016 else
2017 val |= TRANS_PROGRESSIVE;
2018
Jesse Barnes040484a2011-01-03 12:14:26 -08002019 I915_WRITE(reg, val | TRANS_ENABLE);
2020 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002021 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002022}
2023
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002024static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002025 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002026{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002027 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028
2029 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002030 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002033 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002034 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002036 /* Workaround: set timing override bit. */
2037 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002038 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002039 I915_WRITE(_TRANSA_CHICKEN2, val);
2040
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002041 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002042 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002044 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2045 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002046 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002047 else
2048 val |= TRANS_PROGRESSIVE;
2049
Daniel Vetterab9412b2013-05-03 11:49:46 +02002050 I915_WRITE(LPT_TRANSCONF, val);
2051 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002052 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002053}
2054
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002055static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2056 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002057{
Daniel Vetter23670b322012-11-01 09:15:30 +01002058 struct drm_device *dev = dev_priv->dev;
2059 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002060
2061 /* FDI relies on the transcoder */
2062 assert_fdi_tx_disabled(dev_priv, pipe);
2063 assert_fdi_rx_disabled(dev_priv, pipe);
2064
Jesse Barnes291906f2011-02-02 12:28:03 -08002065 /* Ports must be off as well */
2066 assert_pch_ports_disabled(dev_priv, pipe);
2067
Daniel Vetterab9412b2013-05-03 11:49:46 +02002068 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002069 val = I915_READ(reg);
2070 val &= ~TRANS_ENABLE;
2071 I915_WRITE(reg, val);
2072 /* wait for PCH transcoder off, transcoder state */
2073 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002074 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002075
2076 if (!HAS_PCH_IBX(dev)) {
2077 /* Workaround: Clear the timing override chicken bit again. */
2078 reg = TRANS_CHICKEN2(pipe);
2079 val = I915_READ(reg);
2080 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2081 I915_WRITE(reg, val);
2082 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002083}
2084
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002085static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002086{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002087 u32 val;
2088
Daniel Vetterab9412b2013-05-03 11:49:46 +02002089 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002090 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002091 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002092 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002093 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002094 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002095
2096 /* Workaround: clear timing override bit. */
2097 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002098 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002099 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002100}
2101
2102/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002103 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002104 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002105 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002106 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002107 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002108 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002109static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002110{
Paulo Zanoni03722642014-01-17 13:51:09 -02002111 struct drm_device *dev = crtc->base.dev;
2112 struct drm_i915_private *dev_priv = dev->dev_private;
2113 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002114 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2115 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002116 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002117 int reg;
2118 u32 val;
2119
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002120 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002121 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002122 assert_sprites_disabled(dev_priv, pipe);
2123
Paulo Zanoni681e5812012-12-06 11:12:38 -02002124 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002125 pch_transcoder = TRANSCODER_A;
2126 else
2127 pch_transcoder = pipe;
2128
Jesse Barnesb24e7172011-01-04 15:09:30 -08002129 /*
2130 * A pipe without a PLL won't actually be able to drive bits from
2131 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2132 * need the check.
2133 */
2134 if (!HAS_PCH_SPLIT(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002135 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002136 assert_dsi_pll_enabled(dev_priv);
2137 else
2138 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002139 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002140 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002141 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002142 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002143 assert_fdi_tx_pll_enabled(dev_priv,
2144 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002145 }
2146 /* FIXME: assert CPU port conditions for SNB+ */
2147 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002148
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002149 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002150 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002151 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002152 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2153 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002154 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002155 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002156
2157 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002158 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002159}
2160
2161/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002162 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002163 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002164 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002165 * Disable the pipe of @crtc, making sure that various hardware
2166 * specific requirements are met, if applicable, e.g. plane
2167 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002168 *
2169 * Will wait until the pipe has shut down before returning.
2170 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002171static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002173 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002174 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002175 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176 int reg;
2177 u32 val;
2178
2179 /*
2180 * Make sure planes won't keep trying to pump pixels to us,
2181 * or we might hang the display.
2182 */
2183 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002184 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002185 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002187 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002188 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002189 if ((val & PIPECONF_ENABLE) == 0)
2190 return;
2191
Ville Syrjälä67adc642014-08-15 01:21:57 +03002192 /*
2193 * Double wide has implications for planes
2194 * so best keep it disabled when not needed.
2195 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002196 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002197 val &= ~PIPECONF_DOUBLE_WIDE;
2198
2199 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002200 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2201 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002202 val &= ~PIPECONF_ENABLE;
2203
2204 I915_WRITE(reg, val);
2205 if ((val & PIPECONF_ENABLE) == 0)
2206 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002207}
2208
Keith Packardd74362c2011-07-28 14:47:14 -07002209/*
2210 * Plane regs are double buffered, going from enabled->disabled needs a
2211 * trigger in order to latch. The display address reg provides this.
2212 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002213void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2214 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002215{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002216 struct drm_device *dev = dev_priv->dev;
2217 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002218
2219 I915_WRITE(reg, I915_READ(reg));
2220 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002221}
2222
Jesse Barnesb24e7172011-01-04 15:09:30 -08002223/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002224 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002225 * @plane: plane to be enabled
2226 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002228 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002229 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002230static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2231 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002232{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002233 struct drm_device *dev = plane->dev;
2234 struct drm_i915_private *dev_priv = dev->dev_private;
2235 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002236
2237 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002238 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002239
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002240 if (intel_crtc->primary_enabled)
2241 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002242
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002243 intel_crtc->primary_enabled = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002244
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002245 dev_priv->display.update_primary_plane(crtc, plane->fb,
2246 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002247
2248 /*
2249 * BDW signals flip done immediately if the plane
2250 * is disabled, even if the plane enable is already
2251 * armed to occur at the next vblank :(
2252 */
2253 if (IS_BROADWELL(dev))
2254 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002255}
2256
Jesse Barnesb24e7172011-01-04 15:09:30 -08002257/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002258 * intel_disable_primary_hw_plane - disable the primary hardware plane
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002259 * @plane: plane to be disabled
2260 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002261 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002262 * Disable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002263 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002264static void intel_disable_primary_hw_plane(struct drm_plane *plane,
2265 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002266{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002267 struct drm_device *dev = plane->dev;
2268 struct drm_i915_private *dev_priv = dev->dev_private;
2269 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
2270
Matt Roper32b7eee2014-12-24 07:59:06 -08002271 if (WARN_ON(!intel_crtc->active))
2272 return;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002273
Ville Syrjälä98ec7732014-04-30 17:43:01 +03002274 if (!intel_crtc->primary_enabled)
2275 return;
Ville Syrjälä0037f712013-10-01 18:02:20 +03002276
Ville Syrjälä4c445e02013-10-09 17:24:58 +03002277 intel_crtc->primary_enabled = false;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002278
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002279 dev_priv->display.update_primary_plane(crtc, plane->fb,
2280 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002281}
2282
Chris Wilson693db182013-03-05 14:52:39 +00002283static bool need_vtd_wa(struct drm_device *dev)
2284{
2285#ifdef CONFIG_INTEL_IOMMU
2286 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2287 return true;
2288#endif
2289 return false;
2290}
2291
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002292unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002293intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2294 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002295{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002296 unsigned int tile_height;
2297 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002298
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002299 switch (fb_format_modifier) {
2300 case DRM_FORMAT_MOD_NONE:
2301 tile_height = 1;
2302 break;
2303 case I915_FORMAT_MOD_X_TILED:
2304 tile_height = IS_GEN2(dev) ? 16 : 8;
2305 break;
2306 case I915_FORMAT_MOD_Y_TILED:
2307 tile_height = 32;
2308 break;
2309 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002310 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2311 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002312 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002313 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002314 tile_height = 64;
2315 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002316 case 2:
2317 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002318 tile_height = 32;
2319 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002320 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002321 tile_height = 16;
2322 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002323 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002324 WARN_ONCE(1,
2325 "128-bit pixels are not supported for display!");
2326 tile_height = 16;
2327 break;
2328 }
2329 break;
2330 default:
2331 MISSING_CASE(fb_format_modifier);
2332 tile_height = 1;
2333 break;
2334 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002335
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002336 return tile_height;
2337}
2338
2339unsigned int
2340intel_fb_align_height(struct drm_device *dev, unsigned int height,
2341 uint32_t pixel_format, uint64_t fb_format_modifier)
2342{
2343 return ALIGN(height, intel_tile_height(dev, pixel_format,
2344 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002345}
2346
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002347static int
2348intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2349 const struct drm_plane_state *plane_state)
2350{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002351 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002352
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002353 *view = i915_ggtt_view_normal;
2354
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002355 if (!plane_state)
2356 return 0;
2357
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002358 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002359 return 0;
2360
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002361 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002362
2363 info->height = fb->height;
2364 info->pixel_format = fb->pixel_format;
2365 info->pitch = fb->pitches[0];
2366 info->fb_modifier = fb->modifier[0];
2367
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002368 return 0;
2369}
2370
Chris Wilson127bd2a2010-07-23 23:32:05 +01002371int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002372intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2373 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002374 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002375 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002376{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002377 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002378 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002379 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002380 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002381 u32 alignment;
2382 int ret;
2383
Matt Roperebcdd392014-07-09 16:22:11 -07002384 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2385
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002386 switch (fb->modifier[0]) {
2387 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002388 if (INTEL_INFO(dev)->gen >= 9)
2389 alignment = 256 * 1024;
2390 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002391 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002392 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002393 alignment = 4 * 1024;
2394 else
2395 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002396 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002397 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002398 if (INTEL_INFO(dev)->gen >= 9)
2399 alignment = 256 * 1024;
2400 else {
2401 /* pin() will align the object as required by fence */
2402 alignment = 0;
2403 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002404 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002405 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002406 case I915_FORMAT_MOD_Yf_TILED:
2407 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2408 "Y tiling bo slipped through, driver bug!\n"))
2409 return -EINVAL;
2410 alignment = 1 * 1024 * 1024;
2411 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002412 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002413 MISSING_CASE(fb->modifier[0]);
2414 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002415 }
2416
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002417 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2418 if (ret)
2419 return ret;
2420
Chris Wilson693db182013-03-05 14:52:39 +00002421 /* Note that the w/a also requires 64 PTE of padding following the
2422 * bo. We currently fill all unused PTE with the shadow page and so
2423 * we should always have valid PTE following the scanout preventing
2424 * the VT-d warning.
2425 */
2426 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2427 alignment = 256 * 1024;
2428
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002429 /*
2430 * Global gtt pte registers are special registers which actually forward
2431 * writes to a chunk of system memory. Which means that there is no risk
2432 * that the register values disappear as soon as we call
2433 * intel_runtime_pm_put(), so it is correct to wrap only the
2434 * pin/unpin/fence and not more.
2435 */
2436 intel_runtime_pm_get(dev_priv);
2437
Chris Wilsonce453d82011-02-21 14:43:56 +00002438 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002439 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002440 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002441 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002442 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002443
2444 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2445 * fence, whereas 965+ only requires a fence if using
2446 * framebuffer compression. For simplicity, we always install
2447 * a fence as the cost is not that onerous.
2448 */
Chris Wilson06d98132012-04-17 15:31:24 +01002449 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002450 if (ret)
2451 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002452
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002453 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002454
Chris Wilsonce453d82011-02-21 14:43:56 +00002455 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002456 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002457 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002458
2459err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002460 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002461err_interruptible:
2462 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002463 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002464 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002465}
2466
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002467static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2468 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002469{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002470 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002471 struct i915_ggtt_view view;
2472 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002473
Matt Roperebcdd392014-07-09 16:22:11 -07002474 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2475
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002476 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2477 WARN_ONCE(ret, "Couldn't get view from plane state!");
2478
Chris Wilson1690e1e2011-12-14 13:57:08 +01002479 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002480 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002481}
2482
Daniel Vetterc2c75132012-07-05 12:17:30 +02002483/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2484 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002485unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2486 unsigned int tiling_mode,
2487 unsigned int cpp,
2488 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002489{
Chris Wilsonbc752862013-02-21 20:04:31 +00002490 if (tiling_mode != I915_TILING_NONE) {
2491 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002492
Chris Wilsonbc752862013-02-21 20:04:31 +00002493 tile_rows = *y / 8;
2494 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002495
Chris Wilsonbc752862013-02-21 20:04:31 +00002496 tiles = *x / (512/cpp);
2497 *x %= 512/cpp;
2498
2499 return tile_rows * pitch * 8 + tiles * 4096;
2500 } else {
2501 unsigned int offset;
2502
2503 offset = *y * pitch + *x * cpp;
2504 *y = 0;
2505 *x = (offset & 4095) / cpp;
2506 return offset & -4096;
2507 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002508}
2509
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002510static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002511{
2512 switch (format) {
2513 case DISPPLANE_8BPP:
2514 return DRM_FORMAT_C8;
2515 case DISPPLANE_BGRX555:
2516 return DRM_FORMAT_XRGB1555;
2517 case DISPPLANE_BGRX565:
2518 return DRM_FORMAT_RGB565;
2519 default:
2520 case DISPPLANE_BGRX888:
2521 return DRM_FORMAT_XRGB8888;
2522 case DISPPLANE_RGBX888:
2523 return DRM_FORMAT_XBGR8888;
2524 case DISPPLANE_BGRX101010:
2525 return DRM_FORMAT_XRGB2101010;
2526 case DISPPLANE_RGBX101010:
2527 return DRM_FORMAT_XBGR2101010;
2528 }
2529}
2530
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002531static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2532{
2533 switch (format) {
2534 case PLANE_CTL_FORMAT_RGB_565:
2535 return DRM_FORMAT_RGB565;
2536 default:
2537 case PLANE_CTL_FORMAT_XRGB_8888:
2538 if (rgb_order) {
2539 if (alpha)
2540 return DRM_FORMAT_ABGR8888;
2541 else
2542 return DRM_FORMAT_XBGR8888;
2543 } else {
2544 if (alpha)
2545 return DRM_FORMAT_ARGB8888;
2546 else
2547 return DRM_FORMAT_XRGB8888;
2548 }
2549 case PLANE_CTL_FORMAT_XRGB_2101010:
2550 if (rgb_order)
2551 return DRM_FORMAT_XBGR2101010;
2552 else
2553 return DRM_FORMAT_XRGB2101010;
2554 }
2555}
2556
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002557static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002558intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2559 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560{
2561 struct drm_device *dev = crtc->base.dev;
2562 struct drm_i915_gem_object *obj = NULL;
2563 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002564 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002565 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2566 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2567 PAGE_SIZE);
2568
2569 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002570
Chris Wilsonff2652e2014-03-10 08:07:02 +00002571 if (plane_config->size == 0)
2572 return false;
2573
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002574 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2575 base_aligned,
2576 base_aligned,
2577 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002578 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002579 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002580
Damien Lespiau49af4492015-01-20 12:51:44 +00002581 obj->tiling_mode = plane_config->tiling;
2582 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002583 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002584
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002585 mode_cmd.pixel_format = fb->pixel_format;
2586 mode_cmd.width = fb->width;
2587 mode_cmd.height = fb->height;
2588 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002589 mode_cmd.modifier[0] = fb->modifier[0];
2590 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002591
2592 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002593 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002595 DRM_DEBUG_KMS("intel fb init failed\n");
2596 goto out_unref_obj;
2597 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002598 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002599
Daniel Vetterf6936e22015-03-26 12:17:05 +01002600 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002601 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002602
2603out_unref_obj:
2604 drm_gem_object_unreference(&obj->base);
2605 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606 return false;
2607}
2608
Matt Roperafd65eb2015-02-03 13:10:04 -08002609/* Update plane->state->fb to match plane->fb after driver-internal updates */
2610static void
2611update_state_fb(struct drm_plane *plane)
2612{
2613 if (plane->fb == plane->state->fb)
2614 return;
2615
2616 if (plane->state->fb)
2617 drm_framebuffer_unreference(plane->state->fb);
2618 plane->state->fb = plane->fb;
2619 if (plane->state->fb)
2620 drm_framebuffer_reference(plane->state->fb);
2621}
2622
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002623static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002624intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2625 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002626{
2627 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002628 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002629 struct drm_crtc *c;
2630 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002631 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002632 struct drm_plane *primary = intel_crtc->base.primary;
2633 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002634
Damien Lespiau2d140302015-02-05 17:22:18 +00002635 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002636 return;
2637
Daniel Vetterf6936e22015-03-26 12:17:05 +01002638 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002639 fb = &plane_config->fb->base;
2640 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002641 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002642
Damien Lespiau2d140302015-02-05 17:22:18 +00002643 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002644
2645 /*
2646 * Failed to alloc the obj, check to see if we should share
2647 * an fb with another CRTC instead
2648 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002649 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002650 i = to_intel_crtc(c);
2651
2652 if (c == &intel_crtc->base)
2653 continue;
2654
Matt Roper2ff8fde2014-07-08 07:50:07 -07002655 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002656 continue;
2657
Daniel Vetter88595ac2015-03-26 12:42:24 +01002658 fb = c->primary->fb;
2659 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002660 continue;
2661
Daniel Vetter88595ac2015-03-26 12:42:24 +01002662 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002663 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002664 drm_framebuffer_reference(fb);
2665 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002666 }
2667 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002668
2669 return;
2670
2671valid_fb:
2672 obj = intel_fb_obj(fb);
2673 if (obj->tiling_mode != I915_TILING_NONE)
2674 dev_priv->preserve_bios_swizzle = true;
2675
2676 primary->fb = fb;
2677 primary->state->crtc = &intel_crtc->base;
2678 primary->crtc = &intel_crtc->base;
2679 update_state_fb(primary);
2680 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002681}
2682
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002683static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2684 struct drm_framebuffer *fb,
2685 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002686{
2687 struct drm_device *dev = crtc->dev;
2688 struct drm_i915_private *dev_priv = dev->dev_private;
2689 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002690 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002691 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002692 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002693 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002694 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302695 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002696
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002697 if (!intel_crtc->primary_enabled) {
2698 I915_WRITE(reg, 0);
2699 if (INTEL_INFO(dev)->gen >= 4)
2700 I915_WRITE(DSPSURF(plane), 0);
2701 else
2702 I915_WRITE(DSPADDR(plane), 0);
2703 POSTING_READ(reg);
2704 return;
2705 }
2706
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002707 obj = intel_fb_obj(fb);
2708 if (WARN_ON(obj == NULL))
2709 return;
2710
2711 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2712
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002713 dspcntr = DISPPLANE_GAMMA_ENABLE;
2714
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002715 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002716
2717 if (INTEL_INFO(dev)->gen < 4) {
2718 if (intel_crtc->pipe == PIPE_B)
2719 dspcntr |= DISPPLANE_SEL_PIPE_B;
2720
2721 /* pipesrc and dspsize control the size that is scaled from,
2722 * which should always be the user's requested size.
2723 */
2724 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002725 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2726 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002727 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002728 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2729 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002730 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2731 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002732 I915_WRITE(PRIMPOS(plane), 0);
2733 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002734 }
2735
Ville Syrjälä57779d02012-10-31 17:50:14 +02002736 switch (fb->pixel_format) {
2737 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002738 dspcntr |= DISPPLANE_8BPP;
2739 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002740 case DRM_FORMAT_XRGB1555:
2741 case DRM_FORMAT_ARGB1555:
2742 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002743 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002744 case DRM_FORMAT_RGB565:
2745 dspcntr |= DISPPLANE_BGRX565;
2746 break;
2747 case DRM_FORMAT_XRGB8888:
2748 case DRM_FORMAT_ARGB8888:
2749 dspcntr |= DISPPLANE_BGRX888;
2750 break;
2751 case DRM_FORMAT_XBGR8888:
2752 case DRM_FORMAT_ABGR8888:
2753 dspcntr |= DISPPLANE_RGBX888;
2754 break;
2755 case DRM_FORMAT_XRGB2101010:
2756 case DRM_FORMAT_ARGB2101010:
2757 dspcntr |= DISPPLANE_BGRX101010;
2758 break;
2759 case DRM_FORMAT_XBGR2101010:
2760 case DRM_FORMAT_ABGR2101010:
2761 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002762 break;
2763 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002764 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002765 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002766
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002767 if (INTEL_INFO(dev)->gen >= 4 &&
2768 obj->tiling_mode != I915_TILING_NONE)
2769 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002770
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002771 if (IS_G4X(dev))
2772 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2773
Ville Syrjäläb98971272014-08-27 16:51:22 +03002774 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002775
Daniel Vetterc2c75132012-07-05 12:17:30 +02002776 if (INTEL_INFO(dev)->gen >= 4) {
2777 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002778 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002779 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002780 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002781 linear_offset -= intel_crtc->dspaddr_offset;
2782 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002783 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002784 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002785
Matt Roper8e7d6882015-01-21 16:35:41 -08002786 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302787 dspcntr |= DISPPLANE_ROTATE_180;
2788
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002789 x += (intel_crtc->config->pipe_src_w - 1);
2790 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302791
2792 /* Finding the last pixel of the last line of the display
2793 data and adding to linear_offset*/
2794 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002795 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2796 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302797 }
2798
2799 I915_WRITE(reg, dspcntr);
2800
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002801 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002802 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002803 I915_WRITE(DSPSURF(plane),
2804 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002805 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002806 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002807 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002808 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002809 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002810}
2811
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002812static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2813 struct drm_framebuffer *fb,
2814 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002815{
2816 struct drm_device *dev = crtc->dev;
2817 struct drm_i915_private *dev_priv = dev->dev_private;
2818 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002819 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002821 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002823 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302824 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002825
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002826 if (!intel_crtc->primary_enabled) {
2827 I915_WRITE(reg, 0);
2828 I915_WRITE(DSPSURF(plane), 0);
2829 POSTING_READ(reg);
2830 return;
2831 }
2832
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002833 obj = intel_fb_obj(fb);
2834 if (WARN_ON(obj == NULL))
2835 return;
2836
2837 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2838
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002839 dspcntr = DISPPLANE_GAMMA_ENABLE;
2840
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002841 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002842
2843 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2844 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2845
Ville Syrjälä57779d02012-10-31 17:50:14 +02002846 switch (fb->pixel_format) {
2847 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848 dspcntr |= DISPPLANE_8BPP;
2849 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002850 case DRM_FORMAT_RGB565:
2851 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002852 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002853 case DRM_FORMAT_XRGB8888:
2854 case DRM_FORMAT_ARGB8888:
2855 dspcntr |= DISPPLANE_BGRX888;
2856 break;
2857 case DRM_FORMAT_XBGR8888:
2858 case DRM_FORMAT_ABGR8888:
2859 dspcntr |= DISPPLANE_RGBX888;
2860 break;
2861 case DRM_FORMAT_XRGB2101010:
2862 case DRM_FORMAT_ARGB2101010:
2863 dspcntr |= DISPPLANE_BGRX101010;
2864 break;
2865 case DRM_FORMAT_XBGR2101010:
2866 case DRM_FORMAT_ABGR2101010:
2867 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002868 break;
2869 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002870 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002871 }
2872
2873 if (obj->tiling_mode != I915_TILING_NONE)
2874 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002875
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002876 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002877 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002878
Ville Syrjäläb98971272014-08-27 16:51:22 +03002879 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002880 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002881 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002882 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002883 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002884 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002885 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302886 dspcntr |= DISPPLANE_ROTATE_180;
2887
2888 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002889 x += (intel_crtc->config->pipe_src_w - 1);
2890 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302891
2892 /* Finding the last pixel of the last line of the display
2893 data and adding to linear_offset*/
2894 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002895 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2896 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302897 }
2898 }
2899
2900 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002901
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002902 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002903 I915_WRITE(DSPSURF(plane),
2904 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002905 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002906 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2907 } else {
2908 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2909 I915_WRITE(DSPLINOFF(plane), linear_offset);
2910 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002911 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002912}
2913
Damien Lespiaub3218032015-02-27 11:15:18 +00002914u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2915 uint32_t pixel_format)
2916{
2917 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2918
2919 /*
2920 * The stride is either expressed as a multiple of 64 bytes
2921 * chunks for linear buffers or in number of tiles for tiled
2922 * buffers.
2923 */
2924 switch (fb_modifier) {
2925 case DRM_FORMAT_MOD_NONE:
2926 return 64;
2927 case I915_FORMAT_MOD_X_TILED:
2928 if (INTEL_INFO(dev)->gen == 2)
2929 return 128;
2930 return 512;
2931 case I915_FORMAT_MOD_Y_TILED:
2932 /* No need to check for old gens and Y tiling since this is
2933 * about the display engine and those will be blocked before
2934 * we get here.
2935 */
2936 return 128;
2937 case I915_FORMAT_MOD_Yf_TILED:
2938 if (bits_per_pixel == 8)
2939 return 64;
2940 else
2941 return 128;
2942 default:
2943 MISSING_CASE(fb_modifier);
2944 return 64;
2945 }
2946}
2947
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002948unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2949 struct drm_i915_gem_object *obj)
2950{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002951 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002952
2953 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002954 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002955
2956 return i915_gem_obj_ggtt_offset_view(obj, view);
2957}
2958
Chandra Kondurua1b22782015-04-07 15:28:45 -07002959/*
2960 * This function detaches (aka. unbinds) unused scalers in hardware
2961 */
2962void skl_detach_scalers(struct intel_crtc *intel_crtc)
2963{
2964 struct drm_device *dev;
2965 struct drm_i915_private *dev_priv;
2966 struct intel_crtc_scaler_state *scaler_state;
2967 int i;
2968
2969 if (!intel_crtc || !intel_crtc->config)
2970 return;
2971
2972 dev = intel_crtc->base.dev;
2973 dev_priv = dev->dev_private;
2974 scaler_state = &intel_crtc->config->scaler_state;
2975
2976 /* loop through and disable scalers that aren't in use */
2977 for (i = 0; i < intel_crtc->num_scalers; i++) {
2978 if (!scaler_state->scalers[i].in_use) {
2979 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2980 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2981 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2982 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2983 intel_crtc->base.base.id, intel_crtc->pipe, i);
2984 }
2985 }
2986}
2987
Damien Lespiau70d21f02013-07-03 21:06:04 +01002988static void skylake_update_primary_plane(struct drm_crtc *crtc,
2989 struct drm_framebuffer *fb,
2990 int x, int y)
2991{
2992 struct drm_device *dev = crtc->dev;
2993 struct drm_i915_private *dev_priv = dev->dev_private;
2994 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Damien Lespiau70d21f02013-07-03 21:06:04 +01002995 struct drm_i915_gem_object *obj;
2996 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05302997 u32 plane_ctl, stride_div, stride;
2998 u32 tile_height, plane_offset, plane_size;
2999 unsigned int rotation;
3000 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003001 unsigned long surf_addr;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303002 struct drm_plane *plane;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003003
3004 if (!intel_crtc->primary_enabled) {
3005 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3006 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3007 POSTING_READ(PLANE_CTL(pipe, 0));
3008 return;
3009 }
3010
3011 plane_ctl = PLANE_CTL_ENABLE |
3012 PLANE_CTL_PIPE_GAMMA_ENABLE |
3013 PLANE_CTL_PIPE_CSC_ENABLE;
3014
3015 switch (fb->pixel_format) {
3016 case DRM_FORMAT_RGB565:
3017 plane_ctl |= PLANE_CTL_FORMAT_RGB_565;
3018 break;
3019 case DRM_FORMAT_XRGB8888:
3020 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3021 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02003022 case DRM_FORMAT_ARGB8888:
3023 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3024 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3025 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003026 case DRM_FORMAT_XBGR8888:
3027 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3028 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3029 break;
Jani Nikulaf75fb422015-02-10 13:15:49 +02003030 case DRM_FORMAT_ABGR8888:
3031 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3032 plane_ctl |= PLANE_CTL_FORMAT_XRGB_8888;
3033 plane_ctl |= PLANE_CTL_ALPHA_SW_PREMULTIPLY;
3034 break;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003035 case DRM_FORMAT_XRGB2101010:
3036 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
3037 break;
3038 case DRM_FORMAT_XBGR2101010:
3039 plane_ctl |= PLANE_CTL_ORDER_RGBX;
3040 plane_ctl |= PLANE_CTL_FORMAT_XRGB_2101010;
3041 break;
3042 default:
3043 BUG();
3044 }
3045
Daniel Vetter30af77c2015-02-10 17:16:11 +00003046 switch (fb->modifier[0]) {
3047 case DRM_FORMAT_MOD_NONE:
Damien Lespiau70d21f02013-07-03 21:06:04 +01003048 break;
Daniel Vetter30af77c2015-02-10 17:16:11 +00003049 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau70d21f02013-07-03 21:06:04 +01003050 plane_ctl |= PLANE_CTL_TILED_X;
Damien Lespiaub3218032015-02-27 11:15:18 +00003051 break;
3052 case I915_FORMAT_MOD_Y_TILED:
3053 plane_ctl |= PLANE_CTL_TILED_Y;
3054 break;
3055 case I915_FORMAT_MOD_Yf_TILED:
3056 plane_ctl |= PLANE_CTL_TILED_YF;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003057 break;
3058 default:
Damien Lespiaub3218032015-02-27 11:15:18 +00003059 MISSING_CASE(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003060 }
3061
3062 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303063
3064 plane = crtc->primary;
3065 rotation = plane->state->rotation;
3066 switch (rotation) {
3067 case BIT(DRM_ROTATE_90):
3068 plane_ctl |= PLANE_CTL_ROTATE_90;
3069 break;
3070
3071 case BIT(DRM_ROTATE_180):
Sonika Jindal1447dde2014-10-04 10:53:31 +01003072 plane_ctl |= PLANE_CTL_ROTATE_180;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303073 break;
3074
3075 case BIT(DRM_ROTATE_270):
3076 plane_ctl |= PLANE_CTL_ROTATE_270;
3077 break;
3078 }
Damien Lespiau70d21f02013-07-03 21:06:04 +01003079
Damien Lespiaub3218032015-02-27 11:15:18 +00003080 obj = intel_fb_obj(fb);
3081 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3082 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303083 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3084
3085 if (intel_rotation_90_or_270(rotation)) {
3086 /* stride = Surface height in tiles */
3087 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3088 fb->modifier[0]);
3089 stride = DIV_ROUND_UP(fb->height, tile_height);
3090 x_offset = stride * tile_height - y - (plane->state->src_h >> 16);
3091 y_offset = x;
3092 plane_size = ((plane->state->src_w >> 16) - 1) << 16 |
3093 ((plane->state->src_h >> 16) - 1);
3094 } else {
3095 stride = fb->pitches[0] / stride_div;
3096 x_offset = x;
3097 y_offset = y;
3098 plane_size = ((plane->state->src_h >> 16) - 1) << 16 |
3099 ((plane->state->src_w >> 16) - 1);
3100 }
3101 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003102
Damien Lespiau70d21f02013-07-03 21:06:04 +01003103 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003104 I915_WRITE(PLANE_POS(pipe, 0), 0);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303105 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3106 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3107 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003108 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003109
3110 POSTING_READ(PLANE_SURF(pipe, 0));
3111}
3112
Jesse Barnes17638cd2011-06-24 12:19:23 -07003113/* Assume fb object is pinned & idle & fenced and just update base pointers */
3114static int
3115intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3116 int x, int y, enum mode_set_atomic state)
3117{
3118 struct drm_device *dev = crtc->dev;
3119 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003120
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003121 if (dev_priv->display.disable_fbc)
3122 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003123
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003124 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3125
3126 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003127}
3128
Ville Syrjälä75147472014-11-24 18:28:11 +02003129static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003130{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003131 struct drm_crtc *crtc;
3132
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003133 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003134 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3135 enum plane plane = intel_crtc->plane;
3136
3137 intel_prepare_page_flip(dev, plane);
3138 intel_finish_page_flip_plane(dev, plane);
3139 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003140}
3141
3142static void intel_update_primary_planes(struct drm_device *dev)
3143{
3144 struct drm_i915_private *dev_priv = dev->dev_private;
3145 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003146
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003147 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003148 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3149
Rob Clark51fd3712013-11-19 12:10:12 -05003150 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003151 /*
3152 * FIXME: Once we have proper support for primary planes (and
3153 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003154 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003155 */
Matt Roperf4510a22014-04-01 15:22:40 -07003156 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003157 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003158 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003159 crtc->x,
3160 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003161 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003162 }
3163}
3164
Ville Syrjälä75147472014-11-24 18:28:11 +02003165void intel_prepare_reset(struct drm_device *dev)
3166{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003167 struct drm_i915_private *dev_priv = to_i915(dev);
3168 struct intel_crtc *crtc;
3169
Ville Syrjälä75147472014-11-24 18:28:11 +02003170 /* no reset support for gen2 */
3171 if (IS_GEN2(dev))
3172 return;
3173
3174 /* reset doesn't touch the display */
3175 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3176 return;
3177
3178 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003179
3180 /*
3181 * Disabling the crtcs gracefully seems nicer. Also the
3182 * g33 docs say we should at least disable all the planes.
3183 */
3184 for_each_intel_crtc(dev, crtc) {
3185 if (crtc->active)
3186 dev_priv->display.crtc_disable(&crtc->base);
3187 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003188}
3189
3190void intel_finish_reset(struct drm_device *dev)
3191{
3192 struct drm_i915_private *dev_priv = to_i915(dev);
3193
3194 /*
3195 * Flips in the rings will be nuked by the reset,
3196 * so complete all pending flips so that user space
3197 * will get its events and not get stuck.
3198 */
3199 intel_complete_page_flips(dev);
3200
3201 /* no reset support for gen2 */
3202 if (IS_GEN2(dev))
3203 return;
3204
3205 /* reset doesn't touch the display */
3206 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3207 /*
3208 * Flips in the rings have been nuked by the reset,
3209 * so update the base address of all primary
3210 * planes to the the last fb to make sure we're
3211 * showing the correct fb after a reset.
3212 */
3213 intel_update_primary_planes(dev);
3214 return;
3215 }
3216
3217 /*
3218 * The display has been reset as well,
3219 * so need a full re-initialization.
3220 */
3221 intel_runtime_pm_disable_interrupts(dev_priv);
3222 intel_runtime_pm_enable_interrupts(dev_priv);
3223
3224 intel_modeset_init_hw(dev);
3225
3226 spin_lock_irq(&dev_priv->irq_lock);
3227 if (dev_priv->display.hpd_irq_setup)
3228 dev_priv->display.hpd_irq_setup(dev);
3229 spin_unlock_irq(&dev_priv->irq_lock);
3230
3231 intel_modeset_setup_hw_state(dev, true);
3232
3233 intel_hpd_init(dev_priv);
3234
3235 drm_modeset_unlock_all(dev);
3236}
3237
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003238static int
Chris Wilson14667a42012-04-03 17:58:35 +01003239intel_finish_fb(struct drm_framebuffer *old_fb)
3240{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003241 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003242 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3243 bool was_interruptible = dev_priv->mm.interruptible;
3244 int ret;
3245
Chris Wilson14667a42012-04-03 17:58:35 +01003246 /* Big Hammer, we also need to ensure that any pending
3247 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3248 * current scanout is retired before unpinning the old
3249 * framebuffer.
3250 *
3251 * This should only fail upon a hung GPU, in which case we
3252 * can safely continue.
3253 */
3254 dev_priv->mm.interruptible = false;
3255 ret = i915_gem_object_finish_gpu(obj);
3256 dev_priv->mm.interruptible = was_interruptible;
3257
3258 return ret;
3259}
3260
Chris Wilson7d5e3792014-03-04 13:15:08 +00003261static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3262{
3263 struct drm_device *dev = crtc->dev;
3264 struct drm_i915_private *dev_priv = dev->dev_private;
3265 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003266 bool pending;
3267
3268 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3269 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3270 return false;
3271
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003272 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003273 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003274 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003275
3276 return pending;
3277}
3278
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003279static void intel_update_pipe_size(struct intel_crtc *crtc)
3280{
3281 struct drm_device *dev = crtc->base.dev;
3282 struct drm_i915_private *dev_priv = dev->dev_private;
3283 const struct drm_display_mode *adjusted_mode;
3284
3285 if (!i915.fastboot)
3286 return;
3287
3288 /*
3289 * Update pipe size and adjust fitter if needed: the reason for this is
3290 * that in compute_mode_changes we check the native mode (not the pfit
3291 * mode) to see if we can flip rather than do a full mode set. In the
3292 * fastboot case, we'll flip, but if we don't update the pipesrc and
3293 * pfit state, we'll end up with a big fb scanned out into the wrong
3294 * sized surface.
3295 *
3296 * To fix this properly, we need to hoist the checks up into
3297 * compute_mode_changes (or above), check the actual pfit state and
3298 * whether the platform allows pfit disable with pipe active, and only
3299 * then update the pipesrc and pfit state, even on the flip path.
3300 */
3301
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003302 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003303
3304 I915_WRITE(PIPESRC(crtc->pipe),
3305 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3306 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003307 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003308 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3309 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003310 I915_WRITE(PF_CTL(crtc->pipe), 0);
3311 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3312 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3313 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003314 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3315 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003316}
3317
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003318static void intel_fdi_normal_train(struct drm_crtc *crtc)
3319{
3320 struct drm_device *dev = crtc->dev;
3321 struct drm_i915_private *dev_priv = dev->dev_private;
3322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3323 int pipe = intel_crtc->pipe;
3324 u32 reg, temp;
3325
3326 /* enable normal train */
3327 reg = FDI_TX_CTL(pipe);
3328 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003329 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003330 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3331 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003332 } else {
3333 temp &= ~FDI_LINK_TRAIN_NONE;
3334 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003335 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003336 I915_WRITE(reg, temp);
3337
3338 reg = FDI_RX_CTL(pipe);
3339 temp = I915_READ(reg);
3340 if (HAS_PCH_CPT(dev)) {
3341 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3342 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3343 } else {
3344 temp &= ~FDI_LINK_TRAIN_NONE;
3345 temp |= FDI_LINK_TRAIN_NONE;
3346 }
3347 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3348
3349 /* wait one idle pattern time */
3350 POSTING_READ(reg);
3351 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003352
3353 /* IVB wants error correction enabled */
3354 if (IS_IVYBRIDGE(dev))
3355 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3356 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003357}
3358
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003359/* The FDI link training functions for ILK/Ibexpeak. */
3360static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3361{
3362 struct drm_device *dev = crtc->dev;
3363 struct drm_i915_private *dev_priv = dev->dev_private;
3364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3365 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003366 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003367
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003368 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003369 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003370
Adam Jacksone1a44742010-06-25 15:32:14 -04003371 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3372 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003373 reg = FDI_RX_IMR(pipe);
3374 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003375 temp &= ~FDI_RX_SYMBOL_LOCK;
3376 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003377 I915_WRITE(reg, temp);
3378 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003379 udelay(150);
3380
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003381 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003382 reg = FDI_TX_CTL(pipe);
3383 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003384 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003385 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386 temp &= ~FDI_LINK_TRAIN_NONE;
3387 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003389
Chris Wilson5eddb702010-09-11 13:48:45 +01003390 reg = FDI_RX_CTL(pipe);
3391 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003392 temp &= ~FDI_LINK_TRAIN_NONE;
3393 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003394 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3395
3396 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003397 udelay(150);
3398
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003399 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003400 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3401 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3402 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003403
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003405 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003406 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3408
3409 if ((temp & FDI_RX_BIT_LOCK)) {
3410 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 break;
3413 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003414 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003415 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417
3418 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_TX_CTL(pipe);
3420 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421 temp &= ~FDI_LINK_TRAIN_NONE;
3422 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 reg = FDI_RX_CTL(pipe);
3426 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 temp &= ~FDI_LINK_TRAIN_NONE;
3428 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003429 I915_WRITE(reg, temp);
3430
3431 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 udelay(150);
3433
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003435 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3438
3439 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 DRM_DEBUG_KMS("FDI train 2 done.\n");
3442 break;
3443 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003445 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447
3448 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003449
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450}
3451
Akshay Joshi0206e352011-08-16 15:34:10 -04003452static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3454 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3455 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3456 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3457};
3458
3459/* The FDI link training functions for SNB/Cougarpoint. */
3460static void gen6_fdi_link_train(struct drm_crtc *crtc)
3461{
3462 struct drm_device *dev = crtc->dev;
3463 struct drm_i915_private *dev_priv = dev->dev_private;
3464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3465 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003466 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467
Adam Jacksone1a44742010-06-25 15:32:14 -04003468 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3469 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 reg = FDI_RX_IMR(pipe);
3471 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003472 temp &= ~FDI_RX_SYMBOL_LOCK;
3473 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003474 I915_WRITE(reg, temp);
3475
3476 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003477 udelay(150);
3478
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003480 reg = FDI_TX_CTL(pipe);
3481 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003482 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003483 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 temp &= ~FDI_LINK_TRAIN_NONE;
3485 temp |= FDI_LINK_TRAIN_PATTERN_1;
3486 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3487 /* SNB-B */
3488 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490
Daniel Vetterd74cf322012-10-26 10:58:13 +02003491 I915_WRITE(FDI_RX_MISC(pipe),
3492 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3493
Chris Wilson5eddb702010-09-11 13:48:45 +01003494 reg = FDI_RX_CTL(pipe);
3495 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 if (HAS_PCH_CPT(dev)) {
3497 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3498 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3499 } else {
3500 temp &= ~FDI_LINK_TRAIN_NONE;
3501 temp |= FDI_LINK_TRAIN_PATTERN_1;
3502 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3504
3505 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 udelay(150);
3507
Akshay Joshi0206e352011-08-16 15:34:10 -04003508 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 reg = FDI_TX_CTL(pipe);
3510 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3512 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003513 I915_WRITE(reg, temp);
3514
3515 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003516 udelay(500);
3517
Sean Paulfa37d392012-03-02 12:53:39 -05003518 for (retry = 0; retry < 5; retry++) {
3519 reg = FDI_RX_IIR(pipe);
3520 temp = I915_READ(reg);
3521 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3522 if (temp & FDI_RX_BIT_LOCK) {
3523 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3524 DRM_DEBUG_KMS("FDI train 1 done.\n");
3525 break;
3526 }
3527 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 }
Sean Paulfa37d392012-03-02 12:53:39 -05003529 if (retry < 5)
3530 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 }
3532 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003533 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534
3535 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 reg = FDI_TX_CTL(pipe);
3537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 temp &= ~FDI_LINK_TRAIN_NONE;
3539 temp |= FDI_LINK_TRAIN_PATTERN_2;
3540 if (IS_GEN6(dev)) {
3541 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3542 /* SNB-B */
3543 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3544 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003546
Chris Wilson5eddb702010-09-11 13:48:45 +01003547 reg = FDI_RX_CTL(pipe);
3548 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549 if (HAS_PCH_CPT(dev)) {
3550 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3551 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3552 } else {
3553 temp &= ~FDI_LINK_TRAIN_NONE;
3554 temp |= FDI_LINK_TRAIN_PATTERN_2;
3555 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003556 I915_WRITE(reg, temp);
3557
3558 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003559 udelay(150);
3560
Akshay Joshi0206e352011-08-16 15:34:10 -04003561 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 reg = FDI_TX_CTL(pipe);
3563 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3565 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003566 I915_WRITE(reg, temp);
3567
3568 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003569 udelay(500);
3570
Sean Paulfa37d392012-03-02 12:53:39 -05003571 for (retry = 0; retry < 5; retry++) {
3572 reg = FDI_RX_IIR(pipe);
3573 temp = I915_READ(reg);
3574 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3575 if (temp & FDI_RX_SYMBOL_LOCK) {
3576 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3577 DRM_DEBUG_KMS("FDI train 2 done.\n");
3578 break;
3579 }
3580 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581 }
Sean Paulfa37d392012-03-02 12:53:39 -05003582 if (retry < 5)
3583 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 }
3585 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003586 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003587
3588 DRM_DEBUG_KMS("FDI train done.\n");
3589}
3590
Jesse Barnes357555c2011-04-28 15:09:55 -07003591/* Manual link training for Ivy Bridge A0 parts */
3592static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3593{
3594 struct drm_device *dev = crtc->dev;
3595 struct drm_i915_private *dev_priv = dev->dev_private;
3596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3597 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003598 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003599
3600 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3601 for train result */
3602 reg = FDI_RX_IMR(pipe);
3603 temp = I915_READ(reg);
3604 temp &= ~FDI_RX_SYMBOL_LOCK;
3605 temp &= ~FDI_RX_BIT_LOCK;
3606 I915_WRITE(reg, temp);
3607
3608 POSTING_READ(reg);
3609 udelay(150);
3610
Daniel Vetter01a415f2012-10-27 15:58:40 +02003611 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3612 I915_READ(FDI_RX_IIR(pipe)));
3613
Jesse Barnes139ccd32013-08-19 11:04:55 -07003614 /* Try each vswing and preemphasis setting twice before moving on */
3615 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3616 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003617 reg = FDI_TX_CTL(pipe);
3618 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003619 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3620 temp &= ~FDI_TX_ENABLE;
3621 I915_WRITE(reg, temp);
3622
3623 reg = FDI_RX_CTL(pipe);
3624 temp = I915_READ(reg);
3625 temp &= ~FDI_LINK_TRAIN_AUTO;
3626 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3627 temp &= ~FDI_RX_ENABLE;
3628 I915_WRITE(reg, temp);
3629
3630 /* enable CPU FDI TX and PCH FDI RX */
3631 reg = FDI_TX_CTL(pipe);
3632 temp = I915_READ(reg);
3633 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003634 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003635 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003636 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003637 temp |= snb_b_fdi_train_param[j/2];
3638 temp |= FDI_COMPOSITE_SYNC;
3639 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3640
3641 I915_WRITE(FDI_RX_MISC(pipe),
3642 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3643
3644 reg = FDI_RX_CTL(pipe);
3645 temp = I915_READ(reg);
3646 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3647 temp |= FDI_COMPOSITE_SYNC;
3648 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3649
3650 POSTING_READ(reg);
3651 udelay(1); /* should be 0.5us */
3652
3653 for (i = 0; i < 4; i++) {
3654 reg = FDI_RX_IIR(pipe);
3655 temp = I915_READ(reg);
3656 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3657
3658 if (temp & FDI_RX_BIT_LOCK ||
3659 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3660 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3661 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3662 i);
3663 break;
3664 }
3665 udelay(1); /* should be 0.5us */
3666 }
3667 if (i == 4) {
3668 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3669 continue;
3670 }
3671
3672 /* Train 2 */
3673 reg = FDI_TX_CTL(pipe);
3674 temp = I915_READ(reg);
3675 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3676 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3677 I915_WRITE(reg, temp);
3678
3679 reg = FDI_RX_CTL(pipe);
3680 temp = I915_READ(reg);
3681 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3682 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003683 I915_WRITE(reg, temp);
3684
3685 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003686 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003687
Jesse Barnes139ccd32013-08-19 11:04:55 -07003688 for (i = 0; i < 4; i++) {
3689 reg = FDI_RX_IIR(pipe);
3690 temp = I915_READ(reg);
3691 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003692
Jesse Barnes139ccd32013-08-19 11:04:55 -07003693 if (temp & FDI_RX_SYMBOL_LOCK ||
3694 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3695 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3696 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3697 i);
3698 goto train_done;
3699 }
3700 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003701 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003702 if (i == 4)
3703 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003704 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003705
Jesse Barnes139ccd32013-08-19 11:04:55 -07003706train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003707 DRM_DEBUG_KMS("FDI train done.\n");
3708}
3709
Daniel Vetter88cefb62012-08-12 19:27:14 +02003710static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003711{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003712 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003713 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003714 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003715 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003716
Jesse Barnesc64e3112010-09-10 11:27:03 -07003717
Jesse Barnes0e23b992010-09-10 11:10:00 -07003718 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003719 reg = FDI_RX_CTL(pipe);
3720 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003721 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003722 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003723 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003724 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3725
3726 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003727 udelay(200);
3728
3729 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003730 temp = I915_READ(reg);
3731 I915_WRITE(reg, temp | FDI_PCDCLK);
3732
3733 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003734 udelay(200);
3735
Paulo Zanoni20749732012-11-23 15:30:38 -02003736 /* Enable CPU FDI TX PLL, always on for Ironlake */
3737 reg = FDI_TX_CTL(pipe);
3738 temp = I915_READ(reg);
3739 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3740 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003741
Paulo Zanoni20749732012-11-23 15:30:38 -02003742 POSTING_READ(reg);
3743 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003744 }
3745}
3746
Daniel Vetter88cefb62012-08-12 19:27:14 +02003747static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3748{
3749 struct drm_device *dev = intel_crtc->base.dev;
3750 struct drm_i915_private *dev_priv = dev->dev_private;
3751 int pipe = intel_crtc->pipe;
3752 u32 reg, temp;
3753
3754 /* Switch from PCDclk to Rawclk */
3755 reg = FDI_RX_CTL(pipe);
3756 temp = I915_READ(reg);
3757 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3758
3759 /* Disable CPU FDI TX PLL */
3760 reg = FDI_TX_CTL(pipe);
3761 temp = I915_READ(reg);
3762 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3763
3764 POSTING_READ(reg);
3765 udelay(100);
3766
3767 reg = FDI_RX_CTL(pipe);
3768 temp = I915_READ(reg);
3769 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3770
3771 /* Wait for the clocks to turn off. */
3772 POSTING_READ(reg);
3773 udelay(100);
3774}
3775
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003776static void ironlake_fdi_disable(struct drm_crtc *crtc)
3777{
3778 struct drm_device *dev = crtc->dev;
3779 struct drm_i915_private *dev_priv = dev->dev_private;
3780 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3781 int pipe = intel_crtc->pipe;
3782 u32 reg, temp;
3783
3784 /* disable CPU FDI tx and PCH FDI rx */
3785 reg = FDI_TX_CTL(pipe);
3786 temp = I915_READ(reg);
3787 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3788 POSTING_READ(reg);
3789
3790 reg = FDI_RX_CTL(pipe);
3791 temp = I915_READ(reg);
3792 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003793 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003794 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3795
3796 POSTING_READ(reg);
3797 udelay(100);
3798
3799 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003800 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003801 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003802
3803 /* still set train pattern 1 */
3804 reg = FDI_TX_CTL(pipe);
3805 temp = I915_READ(reg);
3806 temp &= ~FDI_LINK_TRAIN_NONE;
3807 temp |= FDI_LINK_TRAIN_PATTERN_1;
3808 I915_WRITE(reg, temp);
3809
3810 reg = FDI_RX_CTL(pipe);
3811 temp = I915_READ(reg);
3812 if (HAS_PCH_CPT(dev)) {
3813 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3814 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3815 } else {
3816 temp &= ~FDI_LINK_TRAIN_NONE;
3817 temp |= FDI_LINK_TRAIN_PATTERN_1;
3818 }
3819 /* BPC in FDI rx is consistent with that in PIPECONF */
3820 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003821 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003822 I915_WRITE(reg, temp);
3823
3824 POSTING_READ(reg);
3825 udelay(100);
3826}
3827
Chris Wilson5dce5b932014-01-20 10:17:36 +00003828bool intel_has_pending_fb_unpin(struct drm_device *dev)
3829{
3830 struct intel_crtc *crtc;
3831
3832 /* Note that we don't need to be called with mode_config.lock here
3833 * as our list of CRTC objects is static for the lifetime of the
3834 * device and so cannot disappear as we iterate. Similarly, we can
3835 * happily treat the predicates as racy, atomic checks as userspace
3836 * cannot claim and pin a new fb without at least acquring the
3837 * struct_mutex and so serialising with us.
3838 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003839 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003840 if (atomic_read(&crtc->unpin_work_count) == 0)
3841 continue;
3842
3843 if (crtc->unpin_work)
3844 intel_wait_for_vblank(dev, crtc->pipe);
3845
3846 return true;
3847 }
3848
3849 return false;
3850}
3851
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003852static void page_flip_completed(struct intel_crtc *intel_crtc)
3853{
3854 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3855 struct intel_unpin_work *work = intel_crtc->unpin_work;
3856
3857 /* ensure that the unpin work is consistent wrt ->pending. */
3858 smp_rmb();
3859 intel_crtc->unpin_work = NULL;
3860
3861 if (work->event)
3862 drm_send_vblank_event(intel_crtc->base.dev,
3863 intel_crtc->pipe,
3864 work->event);
3865
3866 drm_crtc_vblank_put(&intel_crtc->base);
3867
3868 wake_up_all(&dev_priv->pending_flip_queue);
3869 queue_work(dev_priv->wq, &work->work);
3870
3871 trace_i915_flip_complete(intel_crtc->plane,
3872 work->pending_flip_obj);
3873}
3874
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003875void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003876{
Chris Wilson0f911282012-04-17 10:05:38 +01003877 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003878 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003879
Daniel Vetter2c10d572012-12-20 21:24:07 +01003880 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003881 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3882 !intel_crtc_has_pending_flip(crtc),
3883 60*HZ) == 0)) {
3884 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003885
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003886 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003887 if (intel_crtc->unpin_work) {
3888 WARN_ONCE(1, "Removing stuck page flip\n");
3889 page_flip_completed(intel_crtc);
3890 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003891 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003892 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003893
Chris Wilson975d5682014-08-20 13:13:34 +01003894 if (crtc->primary->fb) {
3895 mutex_lock(&dev->struct_mutex);
3896 intel_finish_fb(crtc->primary->fb);
3897 mutex_unlock(&dev->struct_mutex);
3898 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003899}
3900
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003901/* Program iCLKIP clock to the desired frequency */
3902static void lpt_program_iclkip(struct drm_crtc *crtc)
3903{
3904 struct drm_device *dev = crtc->dev;
3905 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003906 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003907 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3908 u32 temp;
3909
Daniel Vetter09153002012-12-12 14:06:44 +01003910 mutex_lock(&dev_priv->dpio_lock);
3911
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003912 /* It is necessary to ungate the pixclk gate prior to programming
3913 * the divisors, and gate it back when it is done.
3914 */
3915 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3916
3917 /* Disable SSCCTL */
3918 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003919 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3920 SBI_SSCCTL_DISABLE,
3921 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003922
3923 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003924 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003925 auxdiv = 1;
3926 divsel = 0x41;
3927 phaseinc = 0x20;
3928 } else {
3929 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003930 * but the adjusted_mode->crtc_clock in in KHz. To get the
3931 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003932 * convert the virtual clock precision to KHz here for higher
3933 * precision.
3934 */
3935 u32 iclk_virtual_root_freq = 172800 * 1000;
3936 u32 iclk_pi_range = 64;
3937 u32 desired_divisor, msb_divisor_value, pi_value;
3938
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003939 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 msb_divisor_value = desired_divisor / iclk_pi_range;
3941 pi_value = desired_divisor % iclk_pi_range;
3942
3943 auxdiv = 0;
3944 divsel = msb_divisor_value - 2;
3945 phaseinc = pi_value;
3946 }
3947
3948 /* This should not happen with any sane values */
3949 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3950 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3951 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3952 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3953
3954 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 +03003955 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003956 auxdiv,
3957 divsel,
3958 phasedir,
3959 phaseinc);
3960
3961 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003962 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003963 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3964 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3965 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3966 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3967 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3968 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003969 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003970
3971 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003972 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003973 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3974 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003975 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003976
3977 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003978 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003979 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003980 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003981
3982 /* Wait for initialization time */
3983 udelay(24);
3984
3985 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01003986
3987 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003988}
3989
Daniel Vetter275f01b22013-05-03 11:49:47 +02003990static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
3991 enum pipe pch_transcoder)
3992{
3993 struct drm_device *dev = crtc->base.dev;
3994 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003995 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02003996
3997 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
3998 I915_READ(HTOTAL(cpu_transcoder)));
3999 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4000 I915_READ(HBLANK(cpu_transcoder)));
4001 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4002 I915_READ(HSYNC(cpu_transcoder)));
4003
4004 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4005 I915_READ(VTOTAL(cpu_transcoder)));
4006 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4007 I915_READ(VBLANK(cpu_transcoder)));
4008 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4009 I915_READ(VSYNC(cpu_transcoder)));
4010 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4011 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4012}
4013
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004014static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004015{
4016 struct drm_i915_private *dev_priv = dev->dev_private;
4017 uint32_t temp;
4018
4019 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004020 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004021 return;
4022
4023 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4024 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4025
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004026 temp &= ~FDI_BC_BIFURCATION_SELECT;
4027 if (enable)
4028 temp |= FDI_BC_BIFURCATION_SELECT;
4029
4030 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004031 I915_WRITE(SOUTH_CHICKEN1, temp);
4032 POSTING_READ(SOUTH_CHICKEN1);
4033}
4034
4035static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4036{
4037 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004038
4039 switch (intel_crtc->pipe) {
4040 case PIPE_A:
4041 break;
4042 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004043 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004044 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004045 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004046 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004047
4048 break;
4049 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004050 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004051
4052 break;
4053 default:
4054 BUG();
4055 }
4056}
4057
Jesse Barnesf67a5592011-01-05 10:31:48 -08004058/*
4059 * Enable PCH resources required for PCH ports:
4060 * - PCH PLLs
4061 * - FDI training & RX/TX
4062 * - update transcoder timings
4063 * - DP transcoding bits
4064 * - transcoder
4065 */
4066static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004067{
4068 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004069 struct drm_i915_private *dev_priv = dev->dev_private;
4070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4071 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004072 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004073
Daniel Vetterab9412b2013-05-03 11:49:46 +02004074 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004075
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004076 if (IS_IVYBRIDGE(dev))
4077 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4078
Daniel Vettercd986ab2012-10-26 10:58:12 +02004079 /* Write the TU size bits before fdi link training, so that error
4080 * detection works. */
4081 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4082 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4083
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004084 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004085 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004086
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004087 /* We need to program the right clock selection before writing the pixel
4088 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004089 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004090 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004091
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004092 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004093 temp |= TRANS_DPLL_ENABLE(pipe);
4094 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004095 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004096 temp |= sel;
4097 else
4098 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004099 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004100 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004101
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004102 /* XXX: pch pll's can be enabled any time before we enable the PCH
4103 * transcoder, and we actually should do this to not upset any PCH
4104 * transcoder that already use the clock when we share it.
4105 *
4106 * Note that enable_shared_dpll tries to do the right thing, but
4107 * get_shared_dpll unconditionally resets the pll - we need that to have
4108 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004109 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004110
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004111 /* set transcoder timing, panel must allow it */
4112 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004113 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004115 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004116
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004117 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004118 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004119 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004120 reg = TRANS_DP_CTL(pipe);
4121 temp = I915_READ(reg);
4122 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004123 TRANS_DP_SYNC_MASK |
4124 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004125 temp |= (TRANS_DP_OUTPUT_ENABLE |
4126 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004127 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128
4129 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004130 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004131 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004132 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004133
4134 switch (intel_trans_dp_port_sel(crtc)) {
4135 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004136 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137 break;
4138 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004139 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140 break;
4141 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004142 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143 break;
4144 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004145 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146 }
4147
Chris Wilson5eddb702010-09-11 13:48:45 +01004148 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149 }
4150
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004151 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004152}
4153
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004154static void lpt_pch_enable(struct drm_crtc *crtc)
4155{
4156 struct drm_device *dev = crtc->dev;
4157 struct drm_i915_private *dev_priv = dev->dev_private;
4158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004159 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004160
Daniel Vetterab9412b2013-05-03 11:49:46 +02004161 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004162
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004163 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004164
Paulo Zanoni0540e482012-10-31 18:12:40 -02004165 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004166 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004167
Paulo Zanoni937bb612012-10-31 18:12:47 -02004168 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004169}
4170
Daniel Vetter716c2e52014-06-25 22:02:02 +03004171void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004172{
Daniel Vettere2b78262013-06-07 23:10:03 +02004173 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004174
4175 if (pll == NULL)
4176 return;
4177
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004178 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004179 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004180 return;
4181 }
4182
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004183 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4184 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004185 WARN_ON(pll->on);
4186 WARN_ON(pll->active);
4187 }
4188
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004189 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004190}
4191
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004192struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4193 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004194{
Daniel Vettere2b78262013-06-07 23:10:03 +02004195 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004196 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004197 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004198
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004199 if (HAS_PCH_IBX(dev_priv->dev)) {
4200 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004201 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004202 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004203
Daniel Vetter46edb022013-06-05 13:34:12 +02004204 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4205 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004206
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004207 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004208
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004209 goto found;
4210 }
4211
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004212 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4213 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004214
4215 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004216 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004217 continue;
4218
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004219 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004220 &pll->new_config->hw_state,
4221 sizeof(pll->new_config->hw_state)) == 0) {
4222 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004223 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004224 pll->new_config->crtc_mask,
4225 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004226 goto found;
4227 }
4228 }
4229
4230 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004231 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4232 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004233 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004234 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4235 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004236 goto found;
4237 }
4238 }
4239
4240 return NULL;
4241
4242found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004243 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004244 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004245
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004246 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004247 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4248 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004249
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004250 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004251
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004252 return pll;
4253}
4254
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004255/**
4256 * intel_shared_dpll_start_config - start a new PLL staged config
4257 * @dev_priv: DRM device
4258 * @clear_pipes: mask of pipes that will have their PLLs freed
4259 *
4260 * Starts a new PLL staged config, copying the current config but
4261 * releasing the references of pipes specified in clear_pipes.
4262 */
4263static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4264 unsigned clear_pipes)
4265{
4266 struct intel_shared_dpll *pll;
4267 enum intel_dpll_id i;
4268
4269 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4270 pll = &dev_priv->shared_dplls[i];
4271
4272 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4273 GFP_KERNEL);
4274 if (!pll->new_config)
4275 goto cleanup;
4276
4277 pll->new_config->crtc_mask &= ~clear_pipes;
4278 }
4279
4280 return 0;
4281
4282cleanup:
4283 while (--i >= 0) {
4284 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004285 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004286 pll->new_config = NULL;
4287 }
4288
4289 return -ENOMEM;
4290}
4291
4292static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4293{
4294 struct intel_shared_dpll *pll;
4295 enum intel_dpll_id i;
4296
4297 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4298 pll = &dev_priv->shared_dplls[i];
4299
4300 WARN_ON(pll->new_config == &pll->config);
4301
4302 pll->config = *pll->new_config;
4303 kfree(pll->new_config);
4304 pll->new_config = NULL;
4305 }
4306}
4307
4308static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4309{
4310 struct intel_shared_dpll *pll;
4311 enum intel_dpll_id i;
4312
4313 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4314 pll = &dev_priv->shared_dplls[i];
4315
4316 WARN_ON(pll->new_config == &pll->config);
4317
4318 kfree(pll->new_config);
4319 pll->new_config = NULL;
4320 }
4321}
4322
Daniel Vettera1520312013-05-03 11:49:50 +02004323static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004324{
4325 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004326 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004327 u32 temp;
4328
4329 temp = I915_READ(dslreg);
4330 udelay(500);
4331 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004332 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004333 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004334 }
4335}
4336
Chandra Kondurua1b22782015-04-07 15:28:45 -07004337/**
4338 * skl_update_scaler_users - Stages update to crtc's scaler state
4339 * @intel_crtc: crtc
4340 * @crtc_state: crtc_state
4341 * @plane: plane (NULL indicates crtc is requesting update)
4342 * @plane_state: plane's state
4343 * @force_detach: request unconditional detachment of scaler
4344 *
4345 * This function updates scaler state for requested plane or crtc.
4346 * To request scaler usage update for a plane, caller shall pass plane pointer.
4347 * To request scaler usage update for crtc, caller shall pass plane pointer
4348 * as NULL.
4349 *
4350 * Return
4351 * 0 - scaler_usage updated successfully
4352 * error - requested scaling cannot be supported or other error condition
4353 */
4354int
4355skl_update_scaler_users(
4356 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4357 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4358 int force_detach)
4359{
4360 int need_scaling;
4361 int idx;
4362 int src_w, src_h, dst_w, dst_h;
4363 int *scaler_id;
4364 struct drm_framebuffer *fb;
4365 struct intel_crtc_scaler_state *scaler_state;
4366
4367 if (!intel_crtc || !crtc_state)
4368 return 0;
4369
4370 scaler_state = &crtc_state->scaler_state;
4371
4372 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4373 fb = intel_plane ? plane_state->base.fb : NULL;
4374
4375 if (intel_plane) {
4376 src_w = drm_rect_width(&plane_state->src) >> 16;
4377 src_h = drm_rect_height(&plane_state->src) >> 16;
4378 dst_w = drm_rect_width(&plane_state->dst);
4379 dst_h = drm_rect_height(&plane_state->dst);
4380 scaler_id = &plane_state->scaler_id;
4381 } else {
4382 struct drm_display_mode *adjusted_mode =
4383 &crtc_state->base.adjusted_mode;
4384 src_w = crtc_state->pipe_src_w;
4385 src_h = crtc_state->pipe_src_h;
4386 dst_w = adjusted_mode->hdisplay;
4387 dst_h = adjusted_mode->vdisplay;
4388 scaler_id = &scaler_state->scaler_id;
4389 }
4390 need_scaling = (src_w != dst_w || src_h != dst_h);
4391
4392 /*
4393 * if plane is being disabled or scaler is no more required or force detach
4394 * - free scaler binded to this plane/crtc
4395 * - in order to do this, update crtc->scaler_usage
4396 *
4397 * Here scaler state in crtc_state is set free so that
4398 * scaler can be assigned to other user. Actual register
4399 * update to free the scaler is done in plane/panel-fit programming.
4400 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4401 */
4402 if (force_detach || !need_scaling || (intel_plane &&
4403 (!fb || !plane_state->visible))) {
4404 if (*scaler_id >= 0) {
4405 scaler_state->scaler_users &= ~(1 << idx);
4406 scaler_state->scalers[*scaler_id].in_use = 0;
4407
4408 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4409 "crtc_state = %p scaler_users = 0x%x\n",
4410 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4411 intel_plane ? intel_plane->base.base.id :
4412 intel_crtc->base.base.id, crtc_state,
4413 scaler_state->scaler_users);
4414 *scaler_id = -1;
4415 }
4416 return 0;
4417 }
4418
4419 /* range checks */
4420 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4421 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4422
4423 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4424 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4425 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4426 "size is out of scaler range\n",
4427 intel_plane ? "PLANE" : "CRTC",
4428 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4429 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4430 return -EINVAL;
4431 }
4432
4433 /* check colorkey */
4434 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4435 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4436 intel_plane->base.base.id);
4437 return -EINVAL;
4438 }
4439
4440 /* Check src format */
4441 if (intel_plane) {
4442 switch (fb->pixel_format) {
4443 case DRM_FORMAT_RGB565:
4444 case DRM_FORMAT_XBGR8888:
4445 case DRM_FORMAT_XRGB8888:
4446 case DRM_FORMAT_ABGR8888:
4447 case DRM_FORMAT_ARGB8888:
4448 case DRM_FORMAT_XRGB2101010:
4449 case DRM_FORMAT_ARGB2101010:
4450 case DRM_FORMAT_XBGR2101010:
4451 case DRM_FORMAT_ABGR2101010:
4452 case DRM_FORMAT_YUYV:
4453 case DRM_FORMAT_YVYU:
4454 case DRM_FORMAT_UYVY:
4455 case DRM_FORMAT_VYUY:
4456 break;
4457 default:
4458 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4459 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4460 return -EINVAL;
4461 }
4462 }
4463
4464 /* mark this plane as a scaler user in crtc_state */
4465 scaler_state->scaler_users |= (1 << idx);
4466 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4467 "crtc_state = %p scaler_users = 0x%x\n",
4468 intel_plane ? "PLANE" : "CRTC",
4469 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4470 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4471 return 0;
4472}
4473
4474static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004475{
4476 struct drm_device *dev = crtc->base.dev;
4477 struct drm_i915_private *dev_priv = dev->dev_private;
4478 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004479 struct intel_crtc_scaler_state *scaler_state =
4480 &crtc->config->scaler_state;
4481
4482 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4483
4484 /* To update pfit, first update scaler state */
4485 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4486 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4487 skl_detach_scalers(crtc);
4488 if (!enable)
4489 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004490
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004491 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004492 int id;
4493
4494 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4495 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4496 return;
4497 }
4498
4499 id = scaler_state->scaler_id;
4500 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4501 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4502 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4503 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4504
4505 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004506 }
4507}
4508
Jesse Barnesb074cec2013-04-25 12:55:02 -07004509static void ironlake_pfit_enable(struct intel_crtc *crtc)
4510{
4511 struct drm_device *dev = crtc->base.dev;
4512 struct drm_i915_private *dev_priv = dev->dev_private;
4513 int pipe = crtc->pipe;
4514
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004515 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004516 /* Force use of hard-coded filter coefficients
4517 * as some pre-programmed values are broken,
4518 * e.g. x201.
4519 */
4520 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4521 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4522 PF_PIPE_SEL_IVB(pipe));
4523 else
4524 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004525 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4526 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004527 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004528}
4529
Matt Roper4a3b8762014-12-23 10:41:51 -08004530static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004531{
4532 struct drm_device *dev = crtc->dev;
4533 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004534 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004535 struct intel_plane *intel_plane;
4536
Matt Roperaf2b6532014-04-01 15:22:32 -07004537 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4538 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004539 if (intel_plane->pipe == pipe)
4540 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004541 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004542}
4543
Matt Roper0d703d42015-03-04 10:49:04 -08004544/*
4545 * Disable a plane internally without actually modifying the plane's state.
4546 * This will allow us to easily restore the plane later by just reprogramming
4547 * its state.
4548 */
4549static void disable_plane_internal(struct drm_plane *plane)
4550{
4551 struct intel_plane *intel_plane = to_intel_plane(plane);
4552 struct drm_plane_state *state =
4553 plane->funcs->atomic_duplicate_state(plane);
4554 struct intel_plane_state *intel_state = to_intel_plane_state(state);
4555
4556 intel_state->visible = false;
4557 intel_plane->commit_plane(plane, intel_state);
4558
4559 intel_plane_destroy_state(plane, state);
4560}
4561
Matt Roper4a3b8762014-12-23 10:41:51 -08004562static void intel_disable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004563{
4564 struct drm_device *dev = crtc->dev;
4565 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004566 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004567 struct intel_plane *intel_plane;
4568
Matt Roperaf2b6532014-04-01 15:22:32 -07004569 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4570 intel_plane = to_intel_plane(plane);
Matt Roper0d703d42015-03-04 10:49:04 -08004571 if (plane->fb && intel_plane->pipe == pipe)
4572 disable_plane_internal(plane);
Matt Roperaf2b6532014-04-01 15:22:32 -07004573 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004574}
4575
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004576void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004577{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004578 struct drm_device *dev = crtc->base.dev;
4579 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004580
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004581 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004582 return;
4583
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004584 /* We can only enable IPS after we enable a plane and wait for a vblank */
4585 intel_wait_for_vblank(dev, crtc->pipe);
4586
Paulo Zanonid77e4532013-09-24 13:52:55 -03004587 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004588 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004589 mutex_lock(&dev_priv->rps.hw_lock);
4590 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4591 mutex_unlock(&dev_priv->rps.hw_lock);
4592 /* Quoting Art Runyan: "its not safe to expect any particular
4593 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004594 * mailbox." Moreover, the mailbox may return a bogus state,
4595 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004596 */
4597 } else {
4598 I915_WRITE(IPS_CTL, IPS_ENABLE);
4599 /* The bit only becomes 1 in the next vblank, so this wait here
4600 * is essentially intel_wait_for_vblank. If we don't have this
4601 * and don't wait for vblanks until the end of crtc_enable, then
4602 * the HW state readout code will complain that the expected
4603 * IPS_CTL value is not the one we read. */
4604 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4605 DRM_ERROR("Timed out waiting for IPS enable\n");
4606 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004607}
4608
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004609void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004610{
4611 struct drm_device *dev = crtc->base.dev;
4612 struct drm_i915_private *dev_priv = dev->dev_private;
4613
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004614 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004615 return;
4616
4617 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004618 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004619 mutex_lock(&dev_priv->rps.hw_lock);
4620 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4621 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004622 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4623 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4624 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004625 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004626 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004627 POSTING_READ(IPS_CTL);
4628 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004629
4630 /* We need to wait for a vblank before we can disable the plane. */
4631 intel_wait_for_vblank(dev, crtc->pipe);
4632}
4633
4634/** Loads the palette/gamma unit for the CRTC with the prepared values */
4635static void intel_crtc_load_lut(struct drm_crtc *crtc)
4636{
4637 struct drm_device *dev = crtc->dev;
4638 struct drm_i915_private *dev_priv = dev->dev_private;
4639 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4640 enum pipe pipe = intel_crtc->pipe;
4641 int palreg = PALETTE(pipe);
4642 int i;
4643 bool reenable_ips = false;
4644
4645 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004646 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004647 return;
4648
4649 if (!HAS_PCH_SPLIT(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004650 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004651 assert_dsi_pll_enabled(dev_priv);
4652 else
4653 assert_pll_enabled(dev_priv, pipe);
4654 }
4655
4656 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304657 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004658 palreg = LGC_PALETTE(pipe);
4659
4660 /* Workaround : Do not read or write the pipe palette/gamma data while
4661 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4662 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004663 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004664 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4665 GAMMA_MODE_MODE_SPLIT)) {
4666 hsw_disable_ips(intel_crtc);
4667 reenable_ips = true;
4668 }
4669
4670 for (i = 0; i < 256; i++) {
4671 I915_WRITE(palreg + 4 * i,
4672 (intel_crtc->lut_r[i] << 16) |
4673 (intel_crtc->lut_g[i] << 8) |
4674 intel_crtc->lut_b[i]);
4675 }
4676
4677 if (reenable_ips)
4678 hsw_enable_ips(intel_crtc);
4679}
4680
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004681static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4682{
4683 if (!enable && intel_crtc->overlay) {
4684 struct drm_device *dev = intel_crtc->base.dev;
4685 struct drm_i915_private *dev_priv = dev->dev_private;
4686
4687 mutex_lock(&dev->struct_mutex);
4688 dev_priv->mm.interruptible = false;
4689 (void) intel_overlay_switch_off(intel_crtc->overlay);
4690 dev_priv->mm.interruptible = true;
4691 mutex_unlock(&dev->struct_mutex);
4692 }
4693
4694 /* Let userspace switch the overlay on again. In most cases userspace
4695 * has to recompute where to put it anyway.
4696 */
4697}
4698
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004699static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004700{
4701 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4703 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004704
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004705 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004706 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004707 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004708 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004709
4710 hsw_enable_ips(intel_crtc);
4711
4712 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004713 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004714 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004715
4716 /*
4717 * FIXME: Once we grow proper nuclear flip support out of this we need
4718 * to compute the mask of flip planes precisely. For the time being
4719 * consider this a flip from a NULL plane.
4720 */
4721 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004722}
4723
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004724static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004725{
4726 struct drm_device *dev = crtc->dev;
4727 struct drm_i915_private *dev_priv = dev->dev_private;
4728 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4729 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004730
4731 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004732
Paulo Zanonie35fef22015-02-09 14:46:29 -02004733 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004734 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004735
4736 hsw_disable_ips(intel_crtc);
4737
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004738 intel_crtc_dpms_overlay(intel_crtc, false);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004739 intel_crtc_update_cursor(crtc, false);
Matt Roper4a3b8762014-12-23 10:41:51 -08004740 intel_disable_sprite_planes(crtc);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004741 intel_disable_primary_hw_plane(crtc->primary, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004742
Daniel Vetterf99d7062014-06-19 16:01:59 +02004743 /*
4744 * FIXME: Once we grow proper nuclear flip support out of this we need
4745 * to compute the mask of flip planes precisely. For the time being
4746 * consider this a flip to a NULL plane.
4747 */
4748 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004749}
4750
Jesse Barnesf67a5592011-01-05 10:31:48 -08004751static void ironlake_crtc_enable(struct drm_crtc *crtc)
4752{
4753 struct drm_device *dev = crtc->dev;
4754 struct drm_i915_private *dev_priv = dev->dev_private;
4755 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004756 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004757 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004758
Matt Roper83d65732015-02-25 13:12:16 -08004759 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004760
Jesse Barnesf67a5592011-01-05 10:31:48 -08004761 if (intel_crtc->active)
4762 return;
4763
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004764 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004765 intel_prepare_shared_dpll(intel_crtc);
4766
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004767 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304768 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004769
4770 intel_set_pipe_timings(intel_crtc);
4771
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004772 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004773 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004774 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004775 }
4776
4777 ironlake_set_pipeconf(crtc);
4778
Jesse Barnesf67a5592011-01-05 10:31:48 -08004779 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004780
Daniel Vettera72e4c92014-09-30 10:56:47 +02004781 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4782 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004783
Daniel Vetterf6736a12013-06-05 13:34:30 +02004784 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004785 if (encoder->pre_enable)
4786 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004787
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004788 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004789 /* Note: FDI PLL enabling _must_ be done before we enable the
4790 * cpu pipes, hence this is separate from all the other fdi/pch
4791 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004792 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004793 } else {
4794 assert_fdi_tx_disabled(dev_priv, pipe);
4795 assert_fdi_rx_disabled(dev_priv, pipe);
4796 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004797
Jesse Barnesb074cec2013-04-25 12:55:02 -07004798 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004799
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004800 /*
4801 * On ILK+ LUT must be loaded before the pipe is running but with
4802 * clocks enabled
4803 */
4804 intel_crtc_load_lut(crtc);
4805
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004806 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004807 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004808
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004809 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004810 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004811
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004812 assert_vblank_disabled(crtc);
4813 drm_crtc_vblank_on(crtc);
4814
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004815 for_each_encoder_on_crtc(dev, crtc, encoder)
4816 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004817
4818 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004819 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004820
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004821 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004822}
4823
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004824/* IPS only exists on ULT machines and is tied to pipe A. */
4825static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4826{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004827 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004828}
4829
Paulo Zanonie4916942013-09-20 16:21:19 -03004830/*
4831 * This implements the workaround described in the "notes" section of the mode
4832 * set sequence documentation. When going from no pipes or single pipe to
4833 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4834 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4835 */
4836static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4837{
4838 struct drm_device *dev = crtc->base.dev;
4839 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4840
4841 /* We want to get the other_active_crtc only if there's only 1 other
4842 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004843 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004844 if (!crtc_it->active || crtc_it == crtc)
4845 continue;
4846
4847 if (other_active_crtc)
4848 return;
4849
4850 other_active_crtc = crtc_it;
4851 }
4852 if (!other_active_crtc)
4853 return;
4854
4855 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4856 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4857}
4858
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004859static void haswell_crtc_enable(struct drm_crtc *crtc)
4860{
4861 struct drm_device *dev = crtc->dev;
4862 struct drm_i915_private *dev_priv = dev->dev_private;
4863 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4864 struct intel_encoder *encoder;
4865 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004866
Matt Roper83d65732015-02-25 13:12:16 -08004867 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004868
4869 if (intel_crtc->active)
4870 return;
4871
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004872 if (intel_crtc_to_shared_dpll(intel_crtc))
4873 intel_enable_shared_dpll(intel_crtc);
4874
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004875 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304876 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004877
4878 intel_set_pipe_timings(intel_crtc);
4879
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004880 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4881 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4882 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004883 }
4884
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004885 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004886 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004887 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004888 }
4889
4890 haswell_set_pipeconf(crtc);
4891
4892 intel_set_pipe_csc(crtc);
4893
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004894 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004895
Daniel Vettera72e4c92014-09-30 10:56:47 +02004896 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004897 for_each_encoder_on_crtc(dev, crtc, encoder)
4898 if (encoder->pre_enable)
4899 encoder->pre_enable(encoder);
4900
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004901 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004902 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4903 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004904 dev_priv->display.fdi_link_train(crtc);
4905 }
4906
Paulo Zanoni1f544382012-10-24 11:32:00 -02004907 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004908
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004909 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004910 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004911 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004912 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004913 else
4914 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004915
4916 /*
4917 * On ILK+ LUT must be loaded before the pipe is running but with
4918 * clocks enabled
4919 */
4920 intel_crtc_load_lut(crtc);
4921
Paulo Zanoni1f544382012-10-24 11:32:00 -02004922 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004923 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004924
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004925 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004926 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004927
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004928 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004929 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004930
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004931 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004932 intel_ddi_set_vc_payload_alloc(crtc, true);
4933
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004934 assert_vblank_disabled(crtc);
4935 drm_crtc_vblank_on(crtc);
4936
Jani Nikula8807e552013-08-30 19:40:32 +03004937 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004938 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004939 intel_opregion_notify_encoder(encoder, true);
4940 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004941
Paulo Zanonie4916942013-09-20 16:21:19 -03004942 /* If we change the relative order between pipe/planes enabling, we need
4943 * to change the workaround. */
4944 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004945 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004946}
4947
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004948static void ironlake_pfit_disable(struct intel_crtc *crtc)
4949{
4950 struct drm_device *dev = crtc->base.dev;
4951 struct drm_i915_private *dev_priv = dev->dev_private;
4952 int pipe = crtc->pipe;
4953
4954 /* To avoid upsetting the power well on haswell only disable the pfit if
4955 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004956 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004957 I915_WRITE(PF_CTL(pipe), 0);
4958 I915_WRITE(PF_WIN_POS(pipe), 0);
4959 I915_WRITE(PF_WIN_SZ(pipe), 0);
4960 }
4961}
4962
Jesse Barnes6be4a602010-09-10 10:26:01 -07004963static void ironlake_crtc_disable(struct drm_crtc *crtc)
4964{
4965 struct drm_device *dev = crtc->dev;
4966 struct drm_i915_private *dev_priv = dev->dev_private;
4967 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004968 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004969 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01004970 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004971
Chris Wilsonf7abfe82010-09-13 14:19:16 +01004972 if (!intel_crtc->active)
4973 return;
4974
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004975 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004976
Daniel Vetterea9d7582012-07-10 10:42:52 +02004977 for_each_encoder_on_crtc(dev, crtc, encoder)
4978 encoder->disable(encoder);
4979
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004980 drm_crtc_vblank_off(crtc);
4981 assert_vblank_disabled(crtc);
4982
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004983 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02004984 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02004985
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03004986 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004987
Daniel Vetter3f8dce32013-05-08 10:36:30 +02004988 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004989
Daniel Vetterbf49ec82012-09-06 22:15:40 +02004990 for_each_encoder_on_crtc(dev, crtc, encoder)
4991 if (encoder->post_disable)
4992 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004993
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004994 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02004995 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004996
Daniel Vetterd925c592013-06-05 13:34:04 +02004997 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004998
Daniel Vetterd925c592013-06-05 13:34:04 +02004999 if (HAS_PCH_CPT(dev)) {
5000 /* disable TRANS_DP_CTL */
5001 reg = TRANS_DP_CTL(pipe);
5002 temp = I915_READ(reg);
5003 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5004 TRANS_DP_PORT_SEL_MASK);
5005 temp |= TRANS_DP_PORT_SEL_NONE;
5006 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005007
Daniel Vetterd925c592013-06-05 13:34:04 +02005008 /* disable DPLL_SEL */
5009 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005010 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005011 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005012 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005013
5014 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005015 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005016
5017 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005018 }
5019
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005020 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005021 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005022
5023 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005024 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005025 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005026}
5027
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005028static void haswell_crtc_disable(struct drm_crtc *crtc)
5029{
5030 struct drm_device *dev = crtc->dev;
5031 struct drm_i915_private *dev_priv = dev->dev_private;
5032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5033 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005034 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005035
5036 if (!intel_crtc->active)
5037 return;
5038
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005039 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03005040
Jani Nikula8807e552013-08-30 19:40:32 +03005041 for_each_encoder_on_crtc(dev, crtc, encoder) {
5042 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005044 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005045
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005046 drm_crtc_vblank_off(crtc);
5047 assert_vblank_disabled(crtc);
5048
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005049 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005050 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5051 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005052 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005054 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005055 intel_ddi_set_vc_payload_alloc(crtc, false);
5056
Paulo Zanoniad80a812012-10-24 16:06:19 -02005057 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005058
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005059 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005060 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005061 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005062 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005063 else
5064 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005065
Paulo Zanoni1f544382012-10-24 11:32:00 -02005066 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005068 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005069 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005070 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005071 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005072
Imre Deak97b040a2014-06-25 22:01:50 +03005073 for_each_encoder_on_crtc(dev, crtc, encoder)
5074 if (encoder->post_disable)
5075 encoder->post_disable(encoder);
5076
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005077 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005078 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005079
5080 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005081 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005082 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005083
5084 if (intel_crtc_to_shared_dpll(intel_crtc))
5085 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005086}
5087
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005088static void ironlake_crtc_off(struct drm_crtc *crtc)
5089{
5090 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005091 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005092}
5093
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005094
Jesse Barnes2dd24552013-04-25 12:55:01 -07005095static void i9xx_pfit_enable(struct intel_crtc *crtc)
5096{
5097 struct drm_device *dev = crtc->base.dev;
5098 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005099 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005100
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005101 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005102 return;
5103
Daniel Vetterc0b03412013-05-28 12:05:54 +02005104 /*
5105 * The panel fitter should only be adjusted whilst the pipe is disabled,
5106 * according to register description and PRM.
5107 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005108 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5109 assert_pipe_disabled(dev_priv, crtc->pipe);
5110
Jesse Barnesb074cec2013-04-25 12:55:02 -07005111 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5112 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005113
5114 /* Border color in case we don't scale up to the full screen. Black by
5115 * default, change to something else for debugging. */
5116 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005117}
5118
Dave Airlied05410f2014-06-05 13:22:59 +10005119static enum intel_display_power_domain port_to_power_domain(enum port port)
5120{
5121 switch (port) {
5122 case PORT_A:
5123 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5124 case PORT_B:
5125 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5126 case PORT_C:
5127 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5128 case PORT_D:
5129 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5130 default:
5131 WARN_ON_ONCE(1);
5132 return POWER_DOMAIN_PORT_OTHER;
5133 }
5134}
5135
Imre Deak77d22dc2014-03-05 16:20:52 +02005136#define for_each_power_domain(domain, mask) \
5137 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5138 if ((1 << (domain)) & (mask))
5139
Imre Deak319be8a2014-03-04 19:22:57 +02005140enum intel_display_power_domain
5141intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005142{
Imre Deak319be8a2014-03-04 19:22:57 +02005143 struct drm_device *dev = intel_encoder->base.dev;
5144 struct intel_digital_port *intel_dig_port;
5145
5146 switch (intel_encoder->type) {
5147 case INTEL_OUTPUT_UNKNOWN:
5148 /* Only DDI platforms should ever use this output type */
5149 WARN_ON_ONCE(!HAS_DDI(dev));
5150 case INTEL_OUTPUT_DISPLAYPORT:
5151 case INTEL_OUTPUT_HDMI:
5152 case INTEL_OUTPUT_EDP:
5153 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005154 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005155 case INTEL_OUTPUT_DP_MST:
5156 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5157 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005158 case INTEL_OUTPUT_ANALOG:
5159 return POWER_DOMAIN_PORT_CRT;
5160 case INTEL_OUTPUT_DSI:
5161 return POWER_DOMAIN_PORT_DSI;
5162 default:
5163 return POWER_DOMAIN_PORT_OTHER;
5164 }
5165}
5166
5167static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5168{
5169 struct drm_device *dev = crtc->dev;
5170 struct intel_encoder *intel_encoder;
5171 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5172 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005173 unsigned long mask;
5174 enum transcoder transcoder;
5175
5176 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5177
5178 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5179 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005180 if (intel_crtc->config->pch_pfit.enabled ||
5181 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005182 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5183
Imre Deak319be8a2014-03-04 19:22:57 +02005184 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5185 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5186
Imre Deak77d22dc2014-03-05 16:20:52 +02005187 return mask;
5188}
5189
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005190static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005191{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005192 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005193 struct drm_i915_private *dev_priv = dev->dev_private;
5194 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5195 struct intel_crtc *crtc;
5196
5197 /*
5198 * First get all needed power domains, then put all unneeded, to avoid
5199 * any unnecessary toggling of the power wells.
5200 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005201 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005202 enum intel_display_power_domain domain;
5203
Matt Roper83d65732015-02-25 13:12:16 -08005204 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005205 continue;
5206
Imre Deak319be8a2014-03-04 19:22:57 +02005207 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005208
5209 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5210 intel_display_power_get(dev_priv, domain);
5211 }
5212
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005213 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005214 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005215
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005216 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005217 enum intel_display_power_domain domain;
5218
5219 for_each_power_domain(domain, crtc->enabled_power_domains)
5220 intel_display_power_put(dev_priv, domain);
5221
5222 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5223 }
5224
5225 intel_display_set_init_power(dev_priv, false);
5226}
5227
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305228void broxton_set_cdclk(struct drm_device *dev, int frequency)
5229{
5230 struct drm_i915_private *dev_priv = dev->dev_private;
5231 uint32_t divider;
5232 uint32_t ratio;
5233 uint32_t current_freq;
5234 int ret;
5235
5236 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5237 switch (frequency) {
5238 case 144000:
5239 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5240 ratio = BXT_DE_PLL_RATIO(60);
5241 break;
5242 case 288000:
5243 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5244 ratio = BXT_DE_PLL_RATIO(60);
5245 break;
5246 case 384000:
5247 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5248 ratio = BXT_DE_PLL_RATIO(60);
5249 break;
5250 case 576000:
5251 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5252 ratio = BXT_DE_PLL_RATIO(60);
5253 break;
5254 case 624000:
5255 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5256 ratio = BXT_DE_PLL_RATIO(65);
5257 break;
5258 case 19200:
5259 /*
5260 * Bypass frequency with DE PLL disabled. Init ratio, divider
5261 * to suppress GCC warning.
5262 */
5263 ratio = 0;
5264 divider = 0;
5265 break;
5266 default:
5267 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5268
5269 return;
5270 }
5271
5272 mutex_lock(&dev_priv->rps.hw_lock);
5273 /* Inform power controller of upcoming frequency change */
5274 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5275 0x80000000);
5276 mutex_unlock(&dev_priv->rps.hw_lock);
5277
5278 if (ret) {
5279 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5280 ret, frequency);
5281 return;
5282 }
5283
5284 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5285 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5286 current_freq = current_freq * 500 + 1000;
5287
5288 /*
5289 * DE PLL has to be disabled when
5290 * - setting to 19.2MHz (bypass, PLL isn't used)
5291 * - before setting to 624MHz (PLL needs toggling)
5292 * - before setting to any frequency from 624MHz (PLL needs toggling)
5293 */
5294 if (frequency == 19200 || frequency == 624000 ||
5295 current_freq == 624000) {
5296 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5297 /* Timeout 200us */
5298 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5299 1))
5300 DRM_ERROR("timout waiting for DE PLL unlock\n");
5301 }
5302
5303 if (frequency != 19200) {
5304 uint32_t val;
5305
5306 val = I915_READ(BXT_DE_PLL_CTL);
5307 val &= ~BXT_DE_PLL_RATIO_MASK;
5308 val |= ratio;
5309 I915_WRITE(BXT_DE_PLL_CTL, val);
5310
5311 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5312 /* Timeout 200us */
5313 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5314 DRM_ERROR("timeout waiting for DE PLL lock\n");
5315
5316 val = I915_READ(CDCLK_CTL);
5317 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5318 val |= divider;
5319 /*
5320 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5321 * enable otherwise.
5322 */
5323 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5324 if (frequency >= 500000)
5325 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5326
5327 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5328 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5329 val |= (frequency - 1000) / 500;
5330 I915_WRITE(CDCLK_CTL, val);
5331 }
5332
5333 mutex_lock(&dev_priv->rps.hw_lock);
5334 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5335 DIV_ROUND_UP(frequency, 25000));
5336 mutex_unlock(&dev_priv->rps.hw_lock);
5337
5338 if (ret) {
5339 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5340 ret, frequency);
5341 return;
5342 }
5343
5344 dev_priv->cdclk_freq = frequency;
5345}
5346
5347void broxton_init_cdclk(struct drm_device *dev)
5348{
5349 struct drm_i915_private *dev_priv = dev->dev_private;
5350 uint32_t val;
5351
5352 /*
5353 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5354 * or else the reset will hang because there is no PCH to respond.
5355 * Move the handshake programming to initialization sequence.
5356 * Previously was left up to BIOS.
5357 */
5358 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5359 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5360 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5361
5362 /* Enable PG1 for cdclk */
5363 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5364
5365 /* check if cd clock is enabled */
5366 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5367 DRM_DEBUG_KMS("Display already initialized\n");
5368 return;
5369 }
5370
5371 /*
5372 * FIXME:
5373 * - The initial CDCLK needs to be read from VBT.
5374 * Need to make this change after VBT has changes for BXT.
5375 * - check if setting the max (or any) cdclk freq is really necessary
5376 * here, it belongs to modeset time
5377 */
5378 broxton_set_cdclk(dev, 624000);
5379
5380 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5381 udelay(10);
5382
5383 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5384 DRM_ERROR("DBuf power enable timeout!\n");
5385}
5386
5387void broxton_uninit_cdclk(struct drm_device *dev)
5388{
5389 struct drm_i915_private *dev_priv = dev->dev_private;
5390
5391 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5392 udelay(10);
5393
5394 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5395 DRM_ERROR("DBuf power disable timeout!\n");
5396
5397 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5398 broxton_set_cdclk(dev, 19200);
5399
5400 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5401}
5402
Ville Syrjälädfcab172014-06-13 13:37:47 +03005403/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005404static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005405{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005406 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005407
Jesse Barnes586f49d2013-11-04 16:06:59 -08005408 /* Obtain SKU information */
5409 mutex_lock(&dev_priv->dpio_lock);
5410 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5411 CCK_FUSE_HPLL_FREQ_MASK;
5412 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005413
Ville Syrjälädfcab172014-06-13 13:37:47 +03005414 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005415}
5416
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005417static void vlv_update_cdclk(struct drm_device *dev)
5418{
5419 struct drm_i915_private *dev_priv = dev->dev_private;
5420
Vandana Kannan164dfd22014-11-24 13:37:41 +05305421 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005422 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305423 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005424
5425 /*
5426 * Program the gmbus_freq based on the cdclk frequency.
5427 * BSpec erroneously claims we should aim for 4MHz, but
5428 * in fact 1MHz is the correct frequency.
5429 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305430 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005431}
5432
Jesse Barnes30a970c2013-11-04 13:48:12 -08005433/* Adjust CDclk dividers to allow high res or save power if possible */
5434static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5435{
5436 struct drm_i915_private *dev_priv = dev->dev_private;
5437 u32 val, cmd;
5438
Vandana Kannan164dfd22014-11-24 13:37:41 +05305439 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5440 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005441
Ville Syrjälädfcab172014-06-13 13:37:47 +03005442 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005443 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005444 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005445 cmd = 1;
5446 else
5447 cmd = 0;
5448
5449 mutex_lock(&dev_priv->rps.hw_lock);
5450 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5451 val &= ~DSPFREQGUAR_MASK;
5452 val |= (cmd << DSPFREQGUAR_SHIFT);
5453 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5454 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5455 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5456 50)) {
5457 DRM_ERROR("timed out waiting for CDclk change\n");
5458 }
5459 mutex_unlock(&dev_priv->rps.hw_lock);
5460
Ville Syrjälädfcab172014-06-13 13:37:47 +03005461 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005462 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005463
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005464 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005465
5466 mutex_lock(&dev_priv->dpio_lock);
5467 /* adjust cdclk divider */
5468 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005469 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005470 val |= divider;
5471 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005472
5473 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5474 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5475 50))
5476 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005477 mutex_unlock(&dev_priv->dpio_lock);
5478 }
5479
5480 mutex_lock(&dev_priv->dpio_lock);
5481 /* adjust self-refresh exit latency value */
5482 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5483 val &= ~0x7f;
5484
5485 /*
5486 * For high bandwidth configs, we set a higher latency in the bunit
5487 * so that the core display fetch happens in time to avoid underruns.
5488 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005489 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005490 val |= 4500 / 250; /* 4.5 usec */
5491 else
5492 val |= 3000 / 250; /* 3.0 usec */
5493 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5494 mutex_unlock(&dev_priv->dpio_lock);
5495
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005496 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005497}
5498
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005499static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5500{
5501 struct drm_i915_private *dev_priv = dev->dev_private;
5502 u32 val, cmd;
5503
Vandana Kannan164dfd22014-11-24 13:37:41 +05305504 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5505 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005506
5507 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005508 case 333333:
5509 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005510 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005511 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005512 break;
5513 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005514 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005515 return;
5516 }
5517
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005518 /*
5519 * Specs are full of misinformation, but testing on actual
5520 * hardware has shown that we just need to write the desired
5521 * CCK divider into the Punit register.
5522 */
5523 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5524
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005525 mutex_lock(&dev_priv->rps.hw_lock);
5526 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5527 val &= ~DSPFREQGUAR_MASK_CHV;
5528 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5529 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5530 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5531 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5532 50)) {
5533 DRM_ERROR("timed out waiting for CDclk change\n");
5534 }
5535 mutex_unlock(&dev_priv->rps.hw_lock);
5536
5537 vlv_update_cdclk(dev);
5538}
5539
Jesse Barnes30a970c2013-11-04 13:48:12 -08005540static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5541 int max_pixclk)
5542{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005543 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005544 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005545
Jesse Barnes30a970c2013-11-04 13:48:12 -08005546 /*
5547 * Really only a few cases to deal with, as only 4 CDclks are supported:
5548 * 200MHz
5549 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005550 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005551 * 400MHz (VLV only)
5552 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5553 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005554 *
5555 * We seem to get an unstable or solid color picture at 200MHz.
5556 * Not sure what's wrong. For now use 200MHz only when all pipes
5557 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005558 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005559 if (!IS_CHERRYVIEW(dev_priv) &&
5560 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005561 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005562 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005563 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005564 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005565 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005566 else
5567 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005568}
5569
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305570static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5571 int max_pixclk)
5572{
5573 /*
5574 * FIXME:
5575 * - remove the guardband, it's not needed on BXT
5576 * - set 19.2MHz bypass frequency if there are no active pipes
5577 */
5578 if (max_pixclk > 576000*9/10)
5579 return 624000;
5580 else if (max_pixclk > 384000*9/10)
5581 return 576000;
5582 else if (max_pixclk > 288000*9/10)
5583 return 384000;
5584 else if (max_pixclk > 144000*9/10)
5585 return 288000;
5586 else
5587 return 144000;
5588}
5589
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005590/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005591static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005592{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005593 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005594 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005595 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005596 int max_pixclk = 0;
5597
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005598 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005599 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5600 if (IS_ERR(crtc_state))
5601 return PTR_ERR(crtc_state);
5602
5603 if (!crtc_state->base.enable)
5604 continue;
5605
5606 max_pixclk = max(max_pixclk,
5607 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005608 }
5609
5610 return max_pixclk;
5611}
5612
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005613static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005614 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005615{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005616 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005617 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005618 int max_pixclk = intel_mode_max_pixclk(state);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305619 int cdclk;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005620
5621 if (max_pixclk < 0)
5622 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005623
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305624 if (IS_VALLEYVIEW(dev_priv))
5625 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5626 else
5627 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5628
5629 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005630 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005631
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005632 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005633 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005634 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005635 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005636
5637 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005638}
5639
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005640static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5641{
5642 unsigned int credits, default_credits;
5643
5644 if (IS_CHERRYVIEW(dev_priv))
5645 default_credits = PFI_CREDIT(12);
5646 else
5647 default_credits = PFI_CREDIT(8);
5648
Vandana Kannan164dfd22014-11-24 13:37:41 +05305649 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005650 /* CHV suggested value is 31 or 63 */
5651 if (IS_CHERRYVIEW(dev_priv))
5652 credits = PFI_CREDIT_31;
5653 else
5654 credits = PFI_CREDIT(15);
5655 } else {
5656 credits = default_credits;
5657 }
5658
5659 /*
5660 * WA - write default credits before re-programming
5661 * FIXME: should we also set the resend bit here?
5662 */
5663 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5664 default_credits);
5665
5666 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5667 credits | PFI_CREDIT_RESEND);
5668
5669 /*
5670 * FIXME is this guaranteed to clear
5671 * immediately or should we poll for it?
5672 */
5673 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5674}
5675
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005676static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005677{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005678 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005679 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005680 int max_pixclk = intel_mode_max_pixclk(state);
5681 int req_cdclk;
5682
5683 /* The only reason this can fail is if we fail to add the crtc_state
5684 * to the atomic state. But that can't happen since the call to
5685 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5686 * can't have failed otherwise the mode set would be aborted) added all
5687 * the states already. */
5688 if (WARN_ON(max_pixclk < 0))
5689 return;
5690
5691 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005692
Vandana Kannan164dfd22014-11-24 13:37:41 +05305693 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005694 /*
5695 * FIXME: We can end up here with all power domains off, yet
5696 * with a CDCLK frequency other than the minimum. To account
5697 * for this take the PIPE-A power domain, which covers the HW
5698 * blocks needed for the following programming. This can be
5699 * removed once it's guaranteed that we get here either with
5700 * the minimum CDCLK set, or the required power domains
5701 * enabled.
5702 */
5703 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5704
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005705 if (IS_CHERRYVIEW(dev))
5706 cherryview_set_cdclk(dev, req_cdclk);
5707 else
5708 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005709
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005710 vlv_program_pfi_credits(dev_priv);
5711
Imre Deak738c05c2014-11-19 16:25:37 +02005712 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005713 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005714}
5715
Jesse Barnes89b667f2013-04-18 14:51:36 -07005716static void valleyview_crtc_enable(struct drm_crtc *crtc)
5717{
5718 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005719 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005720 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5721 struct intel_encoder *encoder;
5722 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005723 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005724
Matt Roper83d65732015-02-25 13:12:16 -08005725 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005726
5727 if (intel_crtc->active)
5728 return;
5729
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005730 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305731
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005732 if (!is_dsi) {
5733 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005734 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005735 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005736 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005737 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005738
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005739 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305740 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005741
5742 intel_set_pipe_timings(intel_crtc);
5743
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005744 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5745 struct drm_i915_private *dev_priv = dev->dev_private;
5746
5747 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5748 I915_WRITE(CHV_CANVAS(pipe), 0);
5749 }
5750
Daniel Vetter5b18e572014-04-24 23:55:06 +02005751 i9xx_set_pipeconf(intel_crtc);
5752
Jesse Barnes89b667f2013-04-18 14:51:36 -07005753 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005754
Daniel Vettera72e4c92014-09-30 10:56:47 +02005755 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005756
Jesse Barnes89b667f2013-04-18 14:51:36 -07005757 for_each_encoder_on_crtc(dev, crtc, encoder)
5758 if (encoder->pre_pll_enable)
5759 encoder->pre_pll_enable(encoder);
5760
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005761 if (!is_dsi) {
5762 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005763 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005764 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005765 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005766 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005767
5768 for_each_encoder_on_crtc(dev, crtc, encoder)
5769 if (encoder->pre_enable)
5770 encoder->pre_enable(encoder);
5771
Jesse Barnes2dd24552013-04-25 12:55:01 -07005772 i9xx_pfit_enable(intel_crtc);
5773
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005774 intel_crtc_load_lut(crtc);
5775
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005776 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005777 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005778
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005779 assert_vblank_disabled(crtc);
5780 drm_crtc_vblank_on(crtc);
5781
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005782 for_each_encoder_on_crtc(dev, crtc, encoder)
5783 encoder->enable(encoder);
5784
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005785 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005786
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005787 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005788 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005789}
5790
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005791static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5792{
5793 struct drm_device *dev = crtc->base.dev;
5794 struct drm_i915_private *dev_priv = dev->dev_private;
5795
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005796 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5797 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005798}
5799
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005800static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005801{
5802 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005803 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005804 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005805 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005806 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005807
Matt Roper83d65732015-02-25 13:12:16 -08005808 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005809
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005810 if (intel_crtc->active)
5811 return;
5812
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005813 i9xx_set_pll_dividers(intel_crtc);
5814
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005815 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305816 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005817
5818 intel_set_pipe_timings(intel_crtc);
5819
Daniel Vetter5b18e572014-04-24 23:55:06 +02005820 i9xx_set_pipeconf(intel_crtc);
5821
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005822 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005823
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005824 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005825 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005826
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005827 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005828 if (encoder->pre_enable)
5829 encoder->pre_enable(encoder);
5830
Daniel Vetterf6736a12013-06-05 13:34:30 +02005831 i9xx_enable_pll(intel_crtc);
5832
Jesse Barnes2dd24552013-04-25 12:55:01 -07005833 i9xx_pfit_enable(intel_crtc);
5834
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005835 intel_crtc_load_lut(crtc);
5836
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005837 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005838 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005839
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005840 assert_vblank_disabled(crtc);
5841 drm_crtc_vblank_on(crtc);
5842
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005843 for_each_encoder_on_crtc(dev, crtc, encoder)
5844 encoder->enable(encoder);
5845
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005846 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005847
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005848 /*
5849 * Gen2 reports pipe underruns whenever all planes are disabled.
5850 * So don't enable underrun reporting before at least some planes
5851 * are enabled.
5852 * FIXME: Need to fix the logic to work when we turn off all planes
5853 * but leave the pipe running.
5854 */
5855 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005856 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005857
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005858 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005859 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005860}
5861
Daniel Vetter87476d62013-04-11 16:29:06 +02005862static void i9xx_pfit_disable(struct intel_crtc *crtc)
5863{
5864 struct drm_device *dev = crtc->base.dev;
5865 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005866
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005867 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005868 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005869
5870 assert_pipe_disabled(dev_priv, crtc->pipe);
5871
Daniel Vetter328d8e82013-05-08 10:36:31 +02005872 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5873 I915_READ(PFIT_CONTROL));
5874 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005875}
5876
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005877static void i9xx_crtc_disable(struct drm_crtc *crtc)
5878{
5879 struct drm_device *dev = crtc->dev;
5880 struct drm_i915_private *dev_priv = dev->dev_private;
5881 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005882 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005883 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005884
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005885 if (!intel_crtc->active)
5886 return;
5887
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005888 /*
5889 * Gen2 reports pipe underruns whenever all planes are disabled.
5890 * So diasble underrun reporting before all the planes get disabled.
5891 * FIXME: Need to fix the logic to work when we turn off all planes
5892 * but leave the pipe running.
5893 */
5894 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005895 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005896
Imre Deak564ed192014-06-13 14:54:21 +03005897 /*
5898 * Vblank time updates from the shadow to live plane control register
5899 * are blocked if the memory self-refresh mode is active at that
5900 * moment. So to make sure the plane gets truly disabled, disable
5901 * first the self-refresh mode. The self-refresh enable bit in turn
5902 * will be checked/applied by the HW only at the next frame start
5903 * event which is after the vblank start event, so we need to have a
5904 * wait-for-vblank between disabling the plane and the pipe.
5905 */
5906 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005907 intel_crtc_disable_planes(crtc);
5908
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005909 /*
5910 * On gen2 planes are double buffered but the pipe isn't, so we must
5911 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005912 * We also need to wait on all gmch platforms because of the
5913 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005914 */
Imre Deak564ed192014-06-13 14:54:21 +03005915 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005916
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005917 for_each_encoder_on_crtc(dev, crtc, encoder)
5918 encoder->disable(encoder);
5919
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005920 drm_crtc_vblank_off(crtc);
5921 assert_vblank_disabled(crtc);
5922
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005923 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005924
Daniel Vetter87476d62013-04-11 16:29:06 +02005925 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005926
Jesse Barnes89b667f2013-04-18 14:51:36 -07005927 for_each_encoder_on_crtc(dev, crtc, encoder)
5928 if (encoder->post_disable)
5929 encoder->post_disable(encoder);
5930
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005931 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005932 if (IS_CHERRYVIEW(dev))
5933 chv_disable_pll(dev_priv, pipe);
5934 else if (IS_VALLEYVIEW(dev))
5935 vlv_disable_pll(dev_priv, pipe);
5936 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005937 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005938 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005939
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005940 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005941 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005942
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005943 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005944 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005945
Daniel Vetterefa96242014-04-24 23:55:02 +02005946 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005947 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02005948 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005949}
5950
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005951static void i9xx_crtc_off(struct drm_crtc *crtc)
5952{
5953}
5954
Borun Fub04c5bd2014-07-12 10:02:27 +05305955/* Master function to enable/disable CRTC and corresponding power wells */
5956void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01005957{
Chris Wilsoncdd59982010-09-08 16:30:16 +01005958 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005959 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005961 enum intel_display_power_domain domain;
5962 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02005963
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005964 if (enable) {
5965 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005966 domains = get_crtc_power_domains(crtc);
5967 for_each_power_domain(domain, domains)
5968 intel_display_power_get(dev_priv, domain);
5969 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005970
5971 dev_priv->display.crtc_enable(crtc);
5972 }
5973 } else {
5974 if (intel_crtc->active) {
5975 dev_priv->display.crtc_disable(crtc);
5976
Daniel Vettere1e9fb82014-06-25 22:02:04 +03005977 domains = intel_crtc->enabled_power_domains;
5978 for_each_power_domain(domain, domains)
5979 intel_display_power_put(dev_priv, domain);
5980 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02005981 }
5982 }
Borun Fub04c5bd2014-07-12 10:02:27 +05305983}
5984
5985/**
5986 * Sets the power management mode of the pipe and plane.
5987 */
5988void intel_crtc_update_dpms(struct drm_crtc *crtc)
5989{
5990 struct drm_device *dev = crtc->dev;
5991 struct intel_encoder *intel_encoder;
5992 bool enable = false;
5993
5994 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5995 enable |= intel_encoder->connectors_active;
5996
5997 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02005998}
5999
Daniel Vetter976f8a22012-07-08 22:34:21 +02006000static void intel_crtc_disable(struct drm_crtc *crtc)
6001{
6002 struct drm_device *dev = crtc->dev;
6003 struct drm_connector *connector;
6004 struct drm_i915_private *dev_priv = dev->dev_private;
6005
6006 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006007 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006008
6009 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006010 dev_priv->display.off(crtc);
6011
Matt Roper70a101f2015-04-08 18:56:53 -07006012 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006013
6014 /* Update computed state. */
6015 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6016 if (!connector->encoder || !connector->encoder->crtc)
6017 continue;
6018
6019 if (connector->encoder->crtc != crtc)
6020 continue;
6021
6022 connector->dpms = DRM_MODE_DPMS_OFF;
6023 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006024 }
6025}
6026
Chris Wilsonea5b2132010-08-04 13:50:23 +01006027void intel_encoder_destroy(struct drm_encoder *encoder)
6028{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006029 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006030
Chris Wilsonea5b2132010-08-04 13:50:23 +01006031 drm_encoder_cleanup(encoder);
6032 kfree(intel_encoder);
6033}
6034
Damien Lespiau92373292013-08-08 22:28:57 +01006035/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006036 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6037 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006038static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006039{
6040 if (mode == DRM_MODE_DPMS_ON) {
6041 encoder->connectors_active = true;
6042
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006043 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006044 } else {
6045 encoder->connectors_active = false;
6046
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006047 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006048 }
6049}
6050
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006051/* Cross check the actual hw state with our own modeset state tracking (and it's
6052 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006053static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006054{
6055 if (connector->get_hw_state(connector)) {
6056 struct intel_encoder *encoder = connector->encoder;
6057 struct drm_crtc *crtc;
6058 bool encoder_enabled;
6059 enum pipe pipe;
6060
6061 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6062 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006063 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006064
Dave Airlie0e32b392014-05-02 14:02:48 +10006065 /* there is no real hw state for MST connectors */
6066 if (connector->mst_port)
6067 return;
6068
Rob Clarke2c719b2014-12-15 13:56:32 -05006069 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006070 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006071 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006072 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006073
Dave Airlie36cd7442014-05-02 13:44:18 +10006074 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006075 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006076 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006077
Dave Airlie36cd7442014-05-02 13:44:18 +10006078 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006079 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6080 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006081 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006082
Dave Airlie36cd7442014-05-02 13:44:18 +10006083 crtc = encoder->base.crtc;
6084
Matt Roper83d65732015-02-25 13:12:16 -08006085 I915_STATE_WARN(!crtc->state->enable,
6086 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006087 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6088 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006089 "encoder active on the wrong pipe\n");
6090 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006091 }
6092}
6093
Ander Conselvan de Oliveira9bdbd0b2015-04-10 10:59:10 +03006094int intel_connector_init(struct intel_connector *connector)
6095{
6096 struct drm_connector_state *connector_state;
6097
6098 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6099 if (!connector_state)
6100 return -ENOMEM;
6101
6102 connector->base.state = connector_state;
6103 return 0;
6104}
6105
6106struct intel_connector *intel_connector_alloc(void)
6107{
6108 struct intel_connector *connector;
6109
6110 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6111 if (!connector)
6112 return NULL;
6113
6114 if (intel_connector_init(connector) < 0) {
6115 kfree(connector);
6116 return NULL;
6117 }
6118
6119 return connector;
6120}
6121
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006122/* Even simpler default implementation, if there's really no special case to
6123 * consider. */
6124void intel_connector_dpms(struct drm_connector *connector, int mode)
6125{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006126 /* All the simple cases only support two dpms states. */
6127 if (mode != DRM_MODE_DPMS_ON)
6128 mode = DRM_MODE_DPMS_OFF;
6129
6130 if (mode == connector->dpms)
6131 return;
6132
6133 connector->dpms = mode;
6134
6135 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006136 if (connector->encoder)
6137 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006138
Daniel Vetterb9805142012-08-31 17:37:33 +02006139 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006140}
6141
Daniel Vetterf0947c32012-07-02 13:10:34 +02006142/* Simple connector->get_hw_state implementation for encoders that support only
6143 * one connector and no cloning and hence the encoder state determines the state
6144 * of the connector. */
6145bool intel_connector_get_hw_state(struct intel_connector *connector)
6146{
Daniel Vetter24929352012-07-02 20:28:59 +02006147 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006148 struct intel_encoder *encoder = connector->encoder;
6149
6150 return encoder->get_hw_state(encoder, &pipe);
6151}
6152
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006153static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006154{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006155 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6156 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006157
6158 return 0;
6159}
6160
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006161static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006162 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006163{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006164 struct drm_atomic_state *state = pipe_config->base.state;
6165 struct intel_crtc *other_crtc;
6166 struct intel_crtc_state *other_crtc_state;
6167
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006168 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6169 pipe_name(pipe), pipe_config->fdi_lanes);
6170 if (pipe_config->fdi_lanes > 4) {
6171 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6172 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006173 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006174 }
6175
Paulo Zanonibafb6552013-11-02 21:07:44 -07006176 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006177 if (pipe_config->fdi_lanes > 2) {
6178 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6179 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006180 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006181 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006182 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006183 }
6184 }
6185
6186 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006187 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006188
6189 /* Ivybridge 3 pipe is really complicated */
6190 switch (pipe) {
6191 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006192 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006193 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006194 if (pipe_config->fdi_lanes <= 2)
6195 return 0;
6196
6197 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6198 other_crtc_state =
6199 intel_atomic_get_crtc_state(state, other_crtc);
6200 if (IS_ERR(other_crtc_state))
6201 return PTR_ERR(other_crtc_state);
6202
6203 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006204 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6205 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006206 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006207 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006208 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006209 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006210 if (pipe_config->fdi_lanes > 2) {
6211 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6212 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006213 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006214 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006215
6216 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6217 other_crtc_state =
6218 intel_atomic_get_crtc_state(state, other_crtc);
6219 if (IS_ERR(other_crtc_state))
6220 return PTR_ERR(other_crtc_state);
6221
6222 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006223 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006224 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006225 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006226 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006227 default:
6228 BUG();
6229 }
6230}
6231
Daniel Vettere29c22c2013-02-21 00:00:16 +01006232#define RETRY 1
6233static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006234 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006235{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006236 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006237 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006238 int lane, link_bw, fdi_dotclock, ret;
6239 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006240
Daniel Vettere29c22c2013-02-21 00:00:16 +01006241retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006242 /* FDI is a binary signal running at ~2.7GHz, encoding
6243 * each output octet as 10 bits. The actual frequency
6244 * is stored as a divider into a 100MHz clock, and the
6245 * mode pixel clock is stored in units of 1KHz.
6246 * Hence the bw of each lane in terms of the mode signal
6247 * is:
6248 */
6249 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6250
Damien Lespiau241bfc32013-09-25 16:45:37 +01006251 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006252
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006253 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006254 pipe_config->pipe_bpp);
6255
6256 pipe_config->fdi_lanes = lane;
6257
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006258 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006259 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006260
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006261 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6262 intel_crtc->pipe, pipe_config);
6263 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006264 pipe_config->pipe_bpp -= 2*3;
6265 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6266 pipe_config->pipe_bpp);
6267 needs_recompute = true;
6268 pipe_config->bw_constrained = true;
6269
6270 goto retry;
6271 }
6272
6273 if (needs_recompute)
6274 return RETRY;
6275
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006276 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006277}
6278
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006279static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006280 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006281{
Jani Nikulad330a952014-01-21 11:24:25 +02006282 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006283 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006284 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006285}
6286
Daniel Vettera43f6e02013-06-07 23:10:32 +02006287static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006288 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006289{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006290 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006291 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006292 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006293 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006294
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006295 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006296 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006297 int clock_limit =
6298 dev_priv->display.get_display_clock_speed(dev);
6299
6300 /*
6301 * Enable pixel doubling when the dot clock
6302 * is > 90% of the (display) core speed.
6303 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006304 * GDG double wide on either pipe,
6305 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006306 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006307 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006308 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006309 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006310 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006311 }
6312
Damien Lespiau241bfc32013-09-25 16:45:37 +01006313 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006314 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006315 }
Chris Wilson89749352010-09-12 18:25:19 +01006316
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006317 /*
6318 * Pipe horizontal size must be even in:
6319 * - DVO ganged mode
6320 * - LVDS dual channel mode
6321 * - Double wide pipe
6322 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006323 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006324 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6325 pipe_config->pipe_src_w &= ~1;
6326
Damien Lespiau8693a822013-05-03 18:48:11 +01006327 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6328 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006329 */
6330 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6331 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006332 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006333
Damien Lespiauf5adf942013-06-24 18:29:34 +01006334 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006335 hsw_compute_ips_config(crtc, pipe_config);
6336
Daniel Vetter877d48d2013-04-19 11:24:43 +02006337 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006338 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006339
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006340 /* FIXME: remove below call once atomic mode set is place and all crtc
6341 * related checks called from atomic_crtc_check function */
6342 ret = 0;
6343 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6344 crtc, pipe_config->base.state);
6345 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6346
6347 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006348}
6349
Ville Syrjälä1652d192015-03-31 14:12:01 +03006350static int skylake_get_display_clock_speed(struct drm_device *dev)
6351{
6352 struct drm_i915_private *dev_priv = to_i915(dev);
6353 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6354 uint32_t cdctl = I915_READ(CDCLK_CTL);
6355 uint32_t linkrate;
6356
6357 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6358 WARN(1, "LCPLL1 not enabled\n");
6359 return 24000; /* 24MHz is the cd freq with NSSC ref */
6360 }
6361
6362 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6363 return 540000;
6364
6365 linkrate = (I915_READ(DPLL_CTRL1) &
6366 DPLL_CRTL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
6367
6368 if (linkrate == DPLL_CRTL1_LINK_RATE_2160 ||
6369 linkrate == DPLL_CRTL1_LINK_RATE_1080) {
6370 /* vco 8640 */
6371 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6372 case CDCLK_FREQ_450_432:
6373 return 432000;
6374 case CDCLK_FREQ_337_308:
6375 return 308570;
6376 case CDCLK_FREQ_675_617:
6377 return 617140;
6378 default:
6379 WARN(1, "Unknown cd freq selection\n");
6380 }
6381 } else {
6382 /* vco 8100 */
6383 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6384 case CDCLK_FREQ_450_432:
6385 return 450000;
6386 case CDCLK_FREQ_337_308:
6387 return 337500;
6388 case CDCLK_FREQ_675_617:
6389 return 675000;
6390 default:
6391 WARN(1, "Unknown cd freq selection\n");
6392 }
6393 }
6394
6395 /* error case, do as if DPLL0 isn't enabled */
6396 return 24000;
6397}
6398
6399static int broadwell_get_display_clock_speed(struct drm_device *dev)
6400{
6401 struct drm_i915_private *dev_priv = dev->dev_private;
6402 uint32_t lcpll = I915_READ(LCPLL_CTL);
6403 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6404
6405 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6406 return 800000;
6407 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6408 return 450000;
6409 else if (freq == LCPLL_CLK_FREQ_450)
6410 return 450000;
6411 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6412 return 540000;
6413 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6414 return 337500;
6415 else
6416 return 675000;
6417}
6418
6419static int haswell_get_display_clock_speed(struct drm_device *dev)
6420{
6421 struct drm_i915_private *dev_priv = dev->dev_private;
6422 uint32_t lcpll = I915_READ(LCPLL_CTL);
6423 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6424
6425 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6426 return 800000;
6427 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6428 return 450000;
6429 else if (freq == LCPLL_CLK_FREQ_450)
6430 return 450000;
6431 else if (IS_HSW_ULT(dev))
6432 return 337500;
6433 else
6434 return 540000;
6435}
6436
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006437static int valleyview_get_display_clock_speed(struct drm_device *dev)
6438{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006439 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006440 u32 val;
6441 int divider;
6442
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006443 if (dev_priv->hpll_freq == 0)
6444 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6445
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006446 mutex_lock(&dev_priv->dpio_lock);
6447 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6448 mutex_unlock(&dev_priv->dpio_lock);
6449
6450 divider = val & DISPLAY_FREQUENCY_VALUES;
6451
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006452 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6453 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6454 "cdclk change in progress\n");
6455
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006456 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006457}
6458
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006459static int ilk_get_display_clock_speed(struct drm_device *dev)
6460{
6461 return 450000;
6462}
6463
Jesse Barnese70236a2009-09-21 10:42:27 -07006464static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006465{
Jesse Barnese70236a2009-09-21 10:42:27 -07006466 return 400000;
6467}
Jesse Barnes79e53942008-11-07 14:24:08 -08006468
Jesse Barnese70236a2009-09-21 10:42:27 -07006469static int i915_get_display_clock_speed(struct drm_device *dev)
6470{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006471 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006472}
Jesse Barnes79e53942008-11-07 14:24:08 -08006473
Jesse Barnese70236a2009-09-21 10:42:27 -07006474static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6475{
6476 return 200000;
6477}
Jesse Barnes79e53942008-11-07 14:24:08 -08006478
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006479static int pnv_get_display_clock_speed(struct drm_device *dev)
6480{
6481 u16 gcfgc = 0;
6482
6483 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6484
6485 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6486 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006487 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006488 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006489 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006490 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006491 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006492 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6493 return 200000;
6494 default:
6495 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6496 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006497 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006498 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006499 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006500 }
6501}
6502
Jesse Barnese70236a2009-09-21 10:42:27 -07006503static int i915gm_get_display_clock_speed(struct drm_device *dev)
6504{
6505 u16 gcfgc = 0;
6506
6507 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6508
6509 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006510 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006511 else {
6512 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6513 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006514 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006515 default:
6516 case GC_DISPLAY_CLOCK_190_200_MHZ:
6517 return 190000;
6518 }
6519 }
6520}
Jesse Barnes79e53942008-11-07 14:24:08 -08006521
Jesse Barnese70236a2009-09-21 10:42:27 -07006522static int i865_get_display_clock_speed(struct drm_device *dev)
6523{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006524 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006525}
6526
6527static int i855_get_display_clock_speed(struct drm_device *dev)
6528{
6529 u16 hpllcc = 0;
6530 /* Assume that the hardware is in the high speed state. This
6531 * should be the default.
6532 */
6533 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6534 case GC_CLOCK_133_200:
6535 case GC_CLOCK_100_200:
6536 return 200000;
6537 case GC_CLOCK_166_250:
6538 return 250000;
6539 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006540 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006541 }
6542
6543 /* Shouldn't happen */
6544 return 0;
6545}
6546
6547static int i830_get_display_clock_speed(struct drm_device *dev)
6548{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006549 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006550}
6551
Zhenyu Wang2c072452009-06-05 15:38:42 +08006552static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006553intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006554{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006555 while (*num > DATA_LINK_M_N_MASK ||
6556 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006557 *num >>= 1;
6558 *den >>= 1;
6559 }
6560}
6561
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006562static void compute_m_n(unsigned int m, unsigned int n,
6563 uint32_t *ret_m, uint32_t *ret_n)
6564{
6565 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6566 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6567 intel_reduce_m_n_ratio(ret_m, ret_n);
6568}
6569
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006570void
6571intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6572 int pixel_clock, int link_clock,
6573 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006574{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006575 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006576
6577 compute_m_n(bits_per_pixel * pixel_clock,
6578 link_clock * nlanes * 8,
6579 &m_n->gmch_m, &m_n->gmch_n);
6580
6581 compute_m_n(pixel_clock, link_clock,
6582 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006583}
6584
Chris Wilsona7615032011-01-12 17:04:08 +00006585static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6586{
Jani Nikulad330a952014-01-21 11:24:25 +02006587 if (i915.panel_use_ssc >= 0)
6588 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006589 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006590 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006591}
6592
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006593static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6594 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006595{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006596 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006597 struct drm_i915_private *dev_priv = dev->dev_private;
6598 int refclk;
6599
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006600 WARN_ON(!crtc_state->base.state);
6601
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006602 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006603 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006604 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006605 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006606 refclk = dev_priv->vbt.lvds_ssc_freq;
6607 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006608 } else if (!IS_GEN2(dev)) {
6609 refclk = 96000;
6610 } else {
6611 refclk = 48000;
6612 }
6613
6614 return refclk;
6615}
6616
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006617static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006618{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006619 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006620}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006621
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006622static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6623{
6624 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006625}
6626
Daniel Vetterf47709a2013-03-28 10:42:02 +01006627static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006628 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006629 intel_clock_t *reduced_clock)
6630{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006631 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006632 u32 fp, fp2 = 0;
6633
6634 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006635 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006636 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006637 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006638 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006639 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006640 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006641 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006642 }
6643
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006644 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006645
Daniel Vetterf47709a2013-03-28 10:42:02 +01006646 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006647 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006648 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006649 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006650 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006651 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006652 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006653 }
6654}
6655
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006656static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6657 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006658{
6659 u32 reg_val;
6660
6661 /*
6662 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6663 * and set it to a reasonable value instead.
6664 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006665 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006666 reg_val &= 0xffffff00;
6667 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006668 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006669
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006670 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006671 reg_val &= 0x8cffffff;
6672 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006673 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006674
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006675 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006676 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006677 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006678
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006679 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006680 reg_val &= 0x00ffffff;
6681 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006682 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006683}
6684
Daniel Vetterb5518422013-05-03 11:49:48 +02006685static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6686 struct intel_link_m_n *m_n)
6687{
6688 struct drm_device *dev = crtc->base.dev;
6689 struct drm_i915_private *dev_priv = dev->dev_private;
6690 int pipe = crtc->pipe;
6691
Daniel Vettere3b95f12013-05-03 11:49:49 +02006692 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6693 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6694 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6695 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006696}
6697
6698static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006699 struct intel_link_m_n *m_n,
6700 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006701{
6702 struct drm_device *dev = crtc->base.dev;
6703 struct drm_i915_private *dev_priv = dev->dev_private;
6704 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006705 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006706
6707 if (INTEL_INFO(dev)->gen >= 5) {
6708 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6709 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6710 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6711 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006712 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6713 * for gen < 8) and if DRRS is supported (to make sure the
6714 * registers are not unnecessarily accessed).
6715 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306716 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006717 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006718 I915_WRITE(PIPE_DATA_M2(transcoder),
6719 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6720 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6721 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6722 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6723 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006724 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006725 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6726 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6727 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6728 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006729 }
6730}
6731
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306732void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006733{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306734 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6735
6736 if (m_n == M1_N1) {
6737 dp_m_n = &crtc->config->dp_m_n;
6738 dp_m2_n2 = &crtc->config->dp_m2_n2;
6739 } else if (m_n == M2_N2) {
6740
6741 /*
6742 * M2_N2 registers are not supported. Hence m2_n2 divider value
6743 * needs to be programmed into M1_N1.
6744 */
6745 dp_m_n = &crtc->config->dp_m2_n2;
6746 } else {
6747 DRM_ERROR("Unsupported divider value\n");
6748 return;
6749 }
6750
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006751 if (crtc->config->has_pch_encoder)
6752 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006753 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306754 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006755}
6756
Ville Syrjäläd288f652014-10-28 13:20:22 +02006757static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006758 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006759{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006760 u32 dpll, dpll_md;
6761
6762 /*
6763 * Enable DPIO clock input. We should never disable the reference
6764 * clock for pipe B, since VGA hotplug / manual detection depends
6765 * on it.
6766 */
6767 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6768 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6769 /* We should never disable this, set it here for state tracking */
6770 if (crtc->pipe == PIPE_B)
6771 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6772 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006773 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006774
Ville Syrjäläd288f652014-10-28 13:20:22 +02006775 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006776 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006777 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006778}
6779
Ville Syrjäläd288f652014-10-28 13:20:22 +02006780static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006781 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006782{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006783 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006784 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006785 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006786 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006787 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006788 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006789
Daniel Vetter09153002012-12-12 14:06:44 +01006790 mutex_lock(&dev_priv->dpio_lock);
6791
Ville Syrjäläd288f652014-10-28 13:20:22 +02006792 bestn = pipe_config->dpll.n;
6793 bestm1 = pipe_config->dpll.m1;
6794 bestm2 = pipe_config->dpll.m2;
6795 bestp1 = pipe_config->dpll.p1;
6796 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006797
Jesse Barnes89b667f2013-04-18 14:51:36 -07006798 /* See eDP HDMI DPIO driver vbios notes doc */
6799
6800 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006801 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006802 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006803
6804 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006805 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006806
6807 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006808 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006809 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006810 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006811
6812 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006813 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006814
6815 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006816 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6817 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6818 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006819 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006820
6821 /*
6822 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6823 * but we don't support that).
6824 * Note: don't use the DAC post divider as it seems unstable.
6825 */
6826 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006827 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006828
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006829 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006830 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006831
Jesse Barnes89b667f2013-04-18 14:51:36 -07006832 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006833 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006834 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6835 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006836 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006837 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006838 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006839 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006840 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006841
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006842 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006843 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006844 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006845 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006846 0x0df40000);
6847 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006848 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006849 0x0df70000);
6850 } else { /* HDMI or VGA */
6851 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006852 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006853 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006854 0x0df70000);
6855 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006856 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006857 0x0df40000);
6858 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006859
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006860 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006861 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006862 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6863 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006864 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006865 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006866
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006867 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006868 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006869}
6870
Ville Syrjäläd288f652014-10-28 13:20:22 +02006871static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006872 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006873{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006874 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006875 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6876 DPLL_VCO_ENABLE;
6877 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006878 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006879
Ville Syrjäläd288f652014-10-28 13:20:22 +02006880 pipe_config->dpll_hw_state.dpll_md =
6881 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006882}
6883
Ville Syrjäläd288f652014-10-28 13:20:22 +02006884static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006885 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006886{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006887 struct drm_device *dev = crtc->base.dev;
6888 struct drm_i915_private *dev_priv = dev->dev_private;
6889 int pipe = crtc->pipe;
6890 int dpll_reg = DPLL(crtc->pipe);
6891 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306892 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006893 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306894 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306895 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006896
Ville Syrjäläd288f652014-10-28 13:20:22 +02006897 bestn = pipe_config->dpll.n;
6898 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6899 bestm1 = pipe_config->dpll.m1;
6900 bestm2 = pipe_config->dpll.m2 >> 22;
6901 bestp1 = pipe_config->dpll.p1;
6902 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306903 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306904 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306905 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006906
6907 /*
6908 * Enable Refclk and SSC
6909 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006910 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006911 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006912
6913 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006914
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006915 /* p1 and p2 divider */
6916 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6917 5 << DPIO_CHV_S1_DIV_SHIFT |
6918 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6919 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6920 1 << DPIO_CHV_K_DIV_SHIFT);
6921
6922 /* Feedback post-divider - m2 */
6923 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6924
6925 /* Feedback refclk divider - n and m1 */
6926 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6927 DPIO_CHV_M1_DIV_BY_2 |
6928 1 << DPIO_CHV_N_DIV_SHIFT);
6929
6930 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306931 if (bestm2_frac)
6932 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006933
6934 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306935 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6936 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6937 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6938 if (bestm2_frac)
6939 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6940 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006941
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306942 /* Program digital lock detect threshold */
6943 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
6944 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
6945 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
6946 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
6947 if (!bestm2_frac)
6948 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
6949 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
6950
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006951 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306952 if (vco == 5400000) {
6953 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
6954 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
6955 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
6956 tribuf_calcntr = 0x9;
6957 } else if (vco <= 6200000) {
6958 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
6959 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
6960 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6961 tribuf_calcntr = 0x9;
6962 } else if (vco <= 6480000) {
6963 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6964 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6965 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6966 tribuf_calcntr = 0x8;
6967 } else {
6968 /* Not supported. Apply the same limits as in the max case */
6969 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
6970 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
6971 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
6972 tribuf_calcntr = 0;
6973 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006974 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
6975
Ville Syrjälä968040b2015-03-11 22:52:08 +02006976 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306977 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
6978 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
6979 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
6980
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006981 /* AFC Recal */
6982 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
6983 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
6984 DPIO_AFC_RECAL);
6985
6986 mutex_unlock(&dev_priv->dpio_lock);
6987}
6988
Ville Syrjäläd288f652014-10-28 13:20:22 +02006989/**
6990 * vlv_force_pll_on - forcibly enable just the PLL
6991 * @dev_priv: i915 private structure
6992 * @pipe: pipe PLL to enable
6993 * @dpll: PLL configuration
6994 *
6995 * Enable the PLL for @pipe using the supplied @dpll config. To be used
6996 * in cases where we need the PLL enabled even when @pipe is not going to
6997 * be enabled.
6998 */
6999void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7000 const struct dpll *dpll)
7001{
7002 struct intel_crtc *crtc =
7003 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007004 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007005 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007006 .pixel_multiplier = 1,
7007 .dpll = *dpll,
7008 };
7009
7010 if (IS_CHERRYVIEW(dev)) {
7011 chv_update_pll(crtc, &pipe_config);
7012 chv_prepare_pll(crtc, &pipe_config);
7013 chv_enable_pll(crtc, &pipe_config);
7014 } else {
7015 vlv_update_pll(crtc, &pipe_config);
7016 vlv_prepare_pll(crtc, &pipe_config);
7017 vlv_enable_pll(crtc, &pipe_config);
7018 }
7019}
7020
7021/**
7022 * vlv_force_pll_off - forcibly disable just the PLL
7023 * @dev_priv: i915 private structure
7024 * @pipe: pipe PLL to disable
7025 *
7026 * Disable the PLL for @pipe. To be used in cases where we need
7027 * the PLL enabled even when @pipe is not going to be enabled.
7028 */
7029void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7030{
7031 if (IS_CHERRYVIEW(dev))
7032 chv_disable_pll(to_i915(dev), pipe);
7033 else
7034 vlv_disable_pll(to_i915(dev), pipe);
7035}
7036
Daniel Vetterf47709a2013-03-28 10:42:02 +01007037static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007038 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007039 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007040 int num_connectors)
7041{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007042 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007043 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007044 u32 dpll;
7045 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007046 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007047
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007048 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307049
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007050 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7051 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007052
7053 dpll = DPLL_VGA_MODE_DIS;
7054
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007055 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007056 dpll |= DPLLB_MODE_LVDS;
7057 else
7058 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007059
Daniel Vetteref1b4602013-06-01 17:17:04 +02007060 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007061 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007062 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007063 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007064
7065 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007066 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007067
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007068 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007069 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007070
7071 /* compute bitmask from p1 value */
7072 if (IS_PINEVIEW(dev))
7073 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7074 else {
7075 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7076 if (IS_G4X(dev) && reduced_clock)
7077 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7078 }
7079 switch (clock->p2) {
7080 case 5:
7081 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7082 break;
7083 case 7:
7084 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7085 break;
7086 case 10:
7087 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7088 break;
7089 case 14:
7090 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7091 break;
7092 }
7093 if (INTEL_INFO(dev)->gen >= 4)
7094 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7095
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007096 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007097 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007098 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007099 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7100 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7101 else
7102 dpll |= PLL_REF_INPUT_DREFCLK;
7103
7104 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007105 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007106
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007107 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007108 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007109 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007110 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007111 }
7112}
7113
Daniel Vetterf47709a2013-03-28 10:42:02 +01007114static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007115 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007116 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007117 int num_connectors)
7118{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007119 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007120 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007121 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007122 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007123
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007124 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307125
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007126 dpll = DPLL_VGA_MODE_DIS;
7127
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007128 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007129 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7130 } else {
7131 if (clock->p1 == 2)
7132 dpll |= PLL_P1_DIVIDE_BY_TWO;
7133 else
7134 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7135 if (clock->p2 == 4)
7136 dpll |= PLL_P2_DIVIDE_BY_4;
7137 }
7138
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007139 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007140 dpll |= DPLL_DVO_2X_MODE;
7141
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007142 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007143 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7144 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7145 else
7146 dpll |= PLL_REF_INPUT_DREFCLK;
7147
7148 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007149 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007150}
7151
Daniel Vetter8a654f32013-06-01 17:16:22 +02007152static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007153{
7154 struct drm_device *dev = intel_crtc->base.dev;
7155 struct drm_i915_private *dev_priv = dev->dev_private;
7156 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007157 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007158 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007159 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007160 uint32_t crtc_vtotal, crtc_vblank_end;
7161 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007162
7163 /* We need to be careful not to changed the adjusted mode, for otherwise
7164 * the hw state checker will get angry at the mismatch. */
7165 crtc_vtotal = adjusted_mode->crtc_vtotal;
7166 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007167
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007168 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007169 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007170 crtc_vtotal -= 1;
7171 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007172
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007173 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007174 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7175 else
7176 vsyncshift = adjusted_mode->crtc_hsync_start -
7177 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007178 if (vsyncshift < 0)
7179 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007180 }
7181
7182 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007183 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007184
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007185 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007186 (adjusted_mode->crtc_hdisplay - 1) |
7187 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007188 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007189 (adjusted_mode->crtc_hblank_start - 1) |
7190 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007191 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007192 (adjusted_mode->crtc_hsync_start - 1) |
7193 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7194
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007195 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007196 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007197 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007198 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007199 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007200 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007201 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007202 (adjusted_mode->crtc_vsync_start - 1) |
7203 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7204
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007205 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7206 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7207 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7208 * bits. */
7209 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7210 (pipe == PIPE_B || pipe == PIPE_C))
7211 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7212
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007213 /* pipesrc controls the size that is scaled from, which should
7214 * always be the user's requested size.
7215 */
7216 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007217 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7218 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007219}
7220
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007221static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007222 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007223{
7224 struct drm_device *dev = crtc->base.dev;
7225 struct drm_i915_private *dev_priv = dev->dev_private;
7226 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7227 uint32_t tmp;
7228
7229 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007230 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7231 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007232 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007233 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7234 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007235 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007236 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7237 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007238
7239 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007240 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7241 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007242 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007243 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7244 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007245 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007246 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7247 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007248
7249 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007250 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7251 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7252 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007253 }
7254
7255 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007256 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7257 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7258
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007259 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7260 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007261}
7262
Daniel Vetterf6a83282014-02-11 15:28:57 -08007263void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007264 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007265{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007266 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7267 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7268 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7269 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007270
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007271 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7272 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7273 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7274 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007275
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007276 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007277
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007278 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7279 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007280}
7281
Daniel Vetter84b046f2013-02-19 18:48:54 +01007282static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7283{
7284 struct drm_device *dev = intel_crtc->base.dev;
7285 struct drm_i915_private *dev_priv = dev->dev_private;
7286 uint32_t pipeconf;
7287
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007288 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007289
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007290 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7291 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7292 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007293
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007294 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007295 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007296
Daniel Vetterff9ce462013-04-24 14:57:17 +02007297 /* only g4x and later have fancy bpc/dither controls */
7298 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007299 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007300 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007301 pipeconf |= PIPECONF_DITHER_EN |
7302 PIPECONF_DITHER_TYPE_SP;
7303
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007304 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007305 case 18:
7306 pipeconf |= PIPECONF_6BPC;
7307 break;
7308 case 24:
7309 pipeconf |= PIPECONF_8BPC;
7310 break;
7311 case 30:
7312 pipeconf |= PIPECONF_10BPC;
7313 break;
7314 default:
7315 /* Case prevented by intel_choose_pipe_bpp_dither. */
7316 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007317 }
7318 }
7319
7320 if (HAS_PIPE_CXSR(dev)) {
7321 if (intel_crtc->lowfreq_avail) {
7322 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7323 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7324 } else {
7325 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007326 }
7327 }
7328
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007329 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007330 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007331 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007332 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7333 else
7334 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7335 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007336 pipeconf |= PIPECONF_PROGRESSIVE;
7337
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007338 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007339 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007340
Daniel Vetter84b046f2013-02-19 18:48:54 +01007341 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7342 POSTING_READ(PIPECONF(intel_crtc->pipe));
7343}
7344
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007345static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7346 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007347{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007348 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007349 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007350 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007351 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007352 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007353 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007354 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007355 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007356 struct drm_atomic_state *state = crtc_state->base.state;
7357 struct drm_connector_state *connector_state;
7358 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007359
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007360 for (i = 0; i < state->num_connector; i++) {
7361 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007362 continue;
7363
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007364 connector_state = state->connector_states[i];
7365 if (connector_state->crtc != &crtc->base)
7366 continue;
7367
7368 encoder = to_intel_encoder(connector_state->best_encoder);
7369
Chris Wilson5eddb702010-09-11 13:48:45 +01007370 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007371 case INTEL_OUTPUT_LVDS:
7372 is_lvds = true;
7373 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007374 case INTEL_OUTPUT_DSI:
7375 is_dsi = true;
7376 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007377 default:
7378 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007379 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007380
Eric Anholtc751ce42010-03-25 11:48:48 -07007381 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007382 }
7383
Jani Nikulaf2335332013-09-13 11:03:09 +03007384 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007385 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007386
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007387 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007388 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007389
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007390 /*
7391 * Returns a set of divisors for the desired target clock with
7392 * the given refclk, or FALSE. The returned values represent
7393 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7394 * 2) / p1 / p2.
7395 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007396 limit = intel_limit(crtc_state, refclk);
7397 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007398 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007399 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007400 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007401 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7402 return -EINVAL;
7403 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007404
Jani Nikulaf2335332013-09-13 11:03:09 +03007405 if (is_lvds && dev_priv->lvds_downclock_avail) {
7406 /*
7407 * Ensure we match the reduced clock's P to the target
7408 * clock. If the clocks don't match, we can't switch
7409 * the display clock by using the FP0/FP1. In such case
7410 * we will disable the LVDS downclock feature.
7411 */
7412 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007413 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007414 dev_priv->lvds_downclock,
7415 refclk, &clock,
7416 &reduced_clock);
7417 }
7418 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007419 crtc_state->dpll.n = clock.n;
7420 crtc_state->dpll.m1 = clock.m1;
7421 crtc_state->dpll.m2 = clock.m2;
7422 crtc_state->dpll.p1 = clock.p1;
7423 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007424 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007425
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007426 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007427 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307428 has_reduced_clock ? &reduced_clock : NULL,
7429 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007430 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007431 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007432 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007433 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007434 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007435 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007436 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007437 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007438 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007439
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007440 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007441}
7442
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007443static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007444 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007445{
7446 struct drm_device *dev = crtc->base.dev;
7447 struct drm_i915_private *dev_priv = dev->dev_private;
7448 uint32_t tmp;
7449
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007450 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7451 return;
7452
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007453 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007454 if (!(tmp & PFIT_ENABLE))
7455 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007456
Daniel Vetter06922822013-07-11 13:35:40 +02007457 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007458 if (INTEL_INFO(dev)->gen < 4) {
7459 if (crtc->pipe != PIPE_B)
7460 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007461 } else {
7462 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7463 return;
7464 }
7465
Daniel Vetter06922822013-07-11 13:35:40 +02007466 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007467 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7468 if (INTEL_INFO(dev)->gen < 5)
7469 pipe_config->gmch_pfit.lvds_border_bits =
7470 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7471}
7472
Jesse Barnesacbec812013-09-20 11:29:32 -07007473static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007474 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007475{
7476 struct drm_device *dev = crtc->base.dev;
7477 struct drm_i915_private *dev_priv = dev->dev_private;
7478 int pipe = pipe_config->cpu_transcoder;
7479 intel_clock_t clock;
7480 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007481 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007482
Shobhit Kumarf573de52014-07-30 20:32:37 +05307483 /* In case of MIPI DPLL will not even be used */
7484 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7485 return;
7486
Jesse Barnesacbec812013-09-20 11:29:32 -07007487 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007488 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007489 mutex_unlock(&dev_priv->dpio_lock);
7490
7491 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7492 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7493 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7494 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7495 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7496
Ville Syrjäläf6466282013-10-14 14:50:31 +03007497 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007498
Ville Syrjäläf6466282013-10-14 14:50:31 +03007499 /* clock.dot is the fast clock */
7500 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007501}
7502
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007503static void
7504i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7505 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007506{
7507 struct drm_device *dev = crtc->base.dev;
7508 struct drm_i915_private *dev_priv = dev->dev_private;
7509 u32 val, base, offset;
7510 int pipe = crtc->pipe, plane = crtc->plane;
7511 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007512 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007513 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007514 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007515
Damien Lespiau42a7b082015-02-05 19:35:13 +00007516 val = I915_READ(DSPCNTR(plane));
7517 if (!(val & DISPLAY_PLANE_ENABLE))
7518 return;
7519
Damien Lespiaud9806c92015-01-21 14:07:19 +00007520 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007521 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007522 DRM_DEBUG_KMS("failed to alloc fb\n");
7523 return;
7524 }
7525
Damien Lespiau1b842c82015-01-21 13:50:54 +00007526 fb = &intel_fb->base;
7527
Daniel Vetter18c52472015-02-10 17:16:09 +00007528 if (INTEL_INFO(dev)->gen >= 4) {
7529 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007530 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007531 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7532 }
7533 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007534
7535 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007536 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007537 fb->pixel_format = fourcc;
7538 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007539
7540 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007541 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007542 offset = I915_READ(DSPTILEOFF(plane));
7543 else
7544 offset = I915_READ(DSPLINOFF(plane));
7545 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7546 } else {
7547 base = I915_READ(DSPADDR(plane));
7548 }
7549 plane_config->base = base;
7550
7551 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007552 fb->width = ((val >> 16) & 0xfff) + 1;
7553 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007554
7555 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007556 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007557
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007558 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007559 fb->pixel_format,
7560 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007561
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007562 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007563
Damien Lespiau2844a922015-01-20 12:51:48 +00007564 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7565 pipe_name(pipe), plane, fb->width, fb->height,
7566 fb->bits_per_pixel, base, fb->pitches[0],
7567 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007568
Damien Lespiau2d140302015-02-05 17:22:18 +00007569 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007570}
7571
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007572static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007573 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007574{
7575 struct drm_device *dev = crtc->base.dev;
7576 struct drm_i915_private *dev_priv = dev->dev_private;
7577 int pipe = pipe_config->cpu_transcoder;
7578 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7579 intel_clock_t clock;
7580 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7581 int refclk = 100000;
7582
7583 mutex_lock(&dev_priv->dpio_lock);
7584 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7585 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7586 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7587 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7588 mutex_unlock(&dev_priv->dpio_lock);
7589
7590 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7591 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7592 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7593 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7594 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7595
7596 chv_clock(refclk, &clock);
7597
7598 /* clock.dot is the fast clock */
7599 pipe_config->port_clock = clock.dot / 5;
7600}
7601
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007602static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007603 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007604{
7605 struct drm_device *dev = crtc->base.dev;
7606 struct drm_i915_private *dev_priv = dev->dev_private;
7607 uint32_t tmp;
7608
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007609 if (!intel_display_power_is_enabled(dev_priv,
7610 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007611 return false;
7612
Daniel Vettere143a212013-07-04 12:01:15 +02007613 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007614 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007615
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007616 tmp = I915_READ(PIPECONF(crtc->pipe));
7617 if (!(tmp & PIPECONF_ENABLE))
7618 return false;
7619
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007620 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7621 switch (tmp & PIPECONF_BPC_MASK) {
7622 case PIPECONF_6BPC:
7623 pipe_config->pipe_bpp = 18;
7624 break;
7625 case PIPECONF_8BPC:
7626 pipe_config->pipe_bpp = 24;
7627 break;
7628 case PIPECONF_10BPC:
7629 pipe_config->pipe_bpp = 30;
7630 break;
7631 default:
7632 break;
7633 }
7634 }
7635
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007636 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7637 pipe_config->limited_color_range = true;
7638
Ville Syrjälä282740f2013-09-04 18:30:03 +03007639 if (INTEL_INFO(dev)->gen < 4)
7640 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7641
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007642 intel_get_pipe_timings(crtc, pipe_config);
7643
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007644 i9xx_get_pfit_config(crtc, pipe_config);
7645
Daniel Vetter6c49f242013-06-06 12:45:25 +02007646 if (INTEL_INFO(dev)->gen >= 4) {
7647 tmp = I915_READ(DPLL_MD(crtc->pipe));
7648 pipe_config->pixel_multiplier =
7649 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7650 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007651 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007652 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7653 tmp = I915_READ(DPLL(crtc->pipe));
7654 pipe_config->pixel_multiplier =
7655 ((tmp & SDVO_MULTIPLIER_MASK)
7656 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7657 } else {
7658 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7659 * port and will be fixed up in the encoder->get_config
7660 * function. */
7661 pipe_config->pixel_multiplier = 1;
7662 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007663 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7664 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007665 /*
7666 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7667 * on 830. Filter it out here so that we don't
7668 * report errors due to that.
7669 */
7670 if (IS_I830(dev))
7671 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7672
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007673 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7674 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007675 } else {
7676 /* Mask out read-only status bits. */
7677 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7678 DPLL_PORTC_READY_MASK |
7679 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007680 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007681
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007682 if (IS_CHERRYVIEW(dev))
7683 chv_crtc_clock_get(crtc, pipe_config);
7684 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007685 vlv_crtc_clock_get(crtc, pipe_config);
7686 else
7687 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007688
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007689 return true;
7690}
7691
Paulo Zanonidde86e22012-12-01 12:04:25 -02007692static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007693{
7694 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007695 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007696 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007697 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007698 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007699 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007700 bool has_ck505 = false;
7701 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007702
7703 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007704 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007705 switch (encoder->type) {
7706 case INTEL_OUTPUT_LVDS:
7707 has_panel = true;
7708 has_lvds = true;
7709 break;
7710 case INTEL_OUTPUT_EDP:
7711 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007712 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007713 has_cpu_edp = true;
7714 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007715 default:
7716 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007717 }
7718 }
7719
Keith Packard99eb6a02011-09-26 14:29:12 -07007720 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007721 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007722 can_ssc = has_ck505;
7723 } else {
7724 has_ck505 = false;
7725 can_ssc = true;
7726 }
7727
Imre Deak2de69052013-05-08 13:14:04 +03007728 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7729 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007730
7731 /* Ironlake: try to setup display ref clock before DPLL
7732 * enabling. This is only under driver's control after
7733 * PCH B stepping, previous chipset stepping should be
7734 * ignoring this setting.
7735 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007736 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007737
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007738 /* As we must carefully and slowly disable/enable each source in turn,
7739 * compute the final state we want first and check if we need to
7740 * make any changes at all.
7741 */
7742 final = val;
7743 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007744 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007745 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007746 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007747 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7748
7749 final &= ~DREF_SSC_SOURCE_MASK;
7750 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7751 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007752
Keith Packard199e5d72011-09-22 12:01:57 -07007753 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007754 final |= DREF_SSC_SOURCE_ENABLE;
7755
7756 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7757 final |= DREF_SSC1_ENABLE;
7758
7759 if (has_cpu_edp) {
7760 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7761 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7762 else
7763 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7764 } else
7765 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7766 } else {
7767 final |= DREF_SSC_SOURCE_DISABLE;
7768 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7769 }
7770
7771 if (final == val)
7772 return;
7773
7774 /* Always enable nonspread source */
7775 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7776
7777 if (has_ck505)
7778 val |= DREF_NONSPREAD_CK505_ENABLE;
7779 else
7780 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7781
7782 if (has_panel) {
7783 val &= ~DREF_SSC_SOURCE_MASK;
7784 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007785
Keith Packard199e5d72011-09-22 12:01:57 -07007786 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007787 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007788 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007789 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007790 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007791 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007792
7793 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007794 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007795 POSTING_READ(PCH_DREF_CONTROL);
7796 udelay(200);
7797
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007798 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007799
7800 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007801 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007802 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007803 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007804 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007805 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007806 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007807 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007808 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007809
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007810 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007811 POSTING_READ(PCH_DREF_CONTROL);
7812 udelay(200);
7813 } else {
7814 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7815
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007816 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007817
7818 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007819 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007820
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007821 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007822 POSTING_READ(PCH_DREF_CONTROL);
7823 udelay(200);
7824
7825 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007826 val &= ~DREF_SSC_SOURCE_MASK;
7827 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007828
7829 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007830 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007831
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007832 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007833 POSTING_READ(PCH_DREF_CONTROL);
7834 udelay(200);
7835 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007836
7837 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007838}
7839
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007840static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007841{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007842 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007843
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007844 tmp = I915_READ(SOUTH_CHICKEN2);
7845 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7846 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007847
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007848 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7849 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7850 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007851
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007852 tmp = I915_READ(SOUTH_CHICKEN2);
7853 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7854 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007855
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007856 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7857 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7858 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007859}
7860
7861/* WaMPhyProgramming:hsw */
7862static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7863{
7864 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007865
7866 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7867 tmp &= ~(0xFF << 24);
7868 tmp |= (0x12 << 24);
7869 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7870
Paulo Zanonidde86e22012-12-01 12:04:25 -02007871 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7872 tmp |= (1 << 11);
7873 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7874
7875 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7876 tmp |= (1 << 11);
7877 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7878
Paulo Zanonidde86e22012-12-01 12:04:25 -02007879 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7880 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7881 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7882
7883 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7884 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7885 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7886
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007887 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7888 tmp &= ~(7 << 13);
7889 tmp |= (5 << 13);
7890 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007891
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007892 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7893 tmp &= ~(7 << 13);
7894 tmp |= (5 << 13);
7895 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007896
7897 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7898 tmp &= ~0xFF;
7899 tmp |= 0x1C;
7900 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7901
7902 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7903 tmp &= ~0xFF;
7904 tmp |= 0x1C;
7905 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7906
7907 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7908 tmp &= ~(0xFF << 16);
7909 tmp |= (0x1C << 16);
7910 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7911
7912 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7913 tmp &= ~(0xFF << 16);
7914 tmp |= (0x1C << 16);
7915 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7916
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007917 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7918 tmp |= (1 << 27);
7919 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007920
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007921 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7922 tmp |= (1 << 27);
7923 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007924
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007925 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7926 tmp &= ~(0xF << 28);
7927 tmp |= (4 << 28);
7928 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007929
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007930 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7931 tmp &= ~(0xF << 28);
7932 tmp |= (4 << 28);
7933 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007934}
7935
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007936/* Implements 3 different sequences from BSpec chapter "Display iCLK
7937 * Programming" based on the parameters passed:
7938 * - Sequence to enable CLKOUT_DP
7939 * - Sequence to enable CLKOUT_DP without spread
7940 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7941 */
7942static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7943 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007944{
7945 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007946 uint32_t reg, tmp;
7947
7948 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
7949 with_spread = true;
7950 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
7951 with_fdi, "LP PCH doesn't have FDI\n"))
7952 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007953
7954 mutex_lock(&dev_priv->dpio_lock);
7955
7956 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7957 tmp &= ~SBI_SSCCTL_DISABLE;
7958 tmp |= SBI_SSCCTL_PATHALT;
7959 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
7960
7961 udelay(24);
7962
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007963 if (with_spread) {
7964 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7965 tmp &= ~SBI_SSCCTL_PATHALT;
7966 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007967
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007968 if (with_fdi) {
7969 lpt_reset_fdi_mphy(dev_priv);
7970 lpt_program_fdi_mphy(dev_priv);
7971 }
7972 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02007973
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007974 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7975 SBI_GEN0 : SBI_DBUFF0;
7976 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7977 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7978 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01007979
7980 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007981}
7982
Paulo Zanoni47701c32013-07-23 11:19:25 -03007983/* Sequence to disable CLKOUT_DP */
7984static void lpt_disable_clkout_dp(struct drm_device *dev)
7985{
7986 struct drm_i915_private *dev_priv = dev->dev_private;
7987 uint32_t reg, tmp;
7988
7989 mutex_lock(&dev_priv->dpio_lock);
7990
7991 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
7992 SBI_GEN0 : SBI_DBUFF0;
7993 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
7994 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
7995 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
7996
7997 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
7998 if (!(tmp & SBI_SSCCTL_DISABLE)) {
7999 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8000 tmp |= SBI_SSCCTL_PATHALT;
8001 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8002 udelay(32);
8003 }
8004 tmp |= SBI_SSCCTL_DISABLE;
8005 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8006 }
8007
8008 mutex_unlock(&dev_priv->dpio_lock);
8009}
8010
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008011static void lpt_init_pch_refclk(struct drm_device *dev)
8012{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008013 struct intel_encoder *encoder;
8014 bool has_vga = false;
8015
Damien Lespiaub2784e12014-08-05 11:29:37 +01008016 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008017 switch (encoder->type) {
8018 case INTEL_OUTPUT_ANALOG:
8019 has_vga = true;
8020 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008021 default:
8022 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008023 }
8024 }
8025
Paulo Zanoni47701c32013-07-23 11:19:25 -03008026 if (has_vga)
8027 lpt_enable_clkout_dp(dev, true, true);
8028 else
8029 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008030}
8031
Paulo Zanonidde86e22012-12-01 12:04:25 -02008032/*
8033 * Initialize reference clocks when the driver loads
8034 */
8035void intel_init_pch_refclk(struct drm_device *dev)
8036{
8037 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8038 ironlake_init_pch_refclk(dev);
8039 else if (HAS_PCH_LPT(dev))
8040 lpt_init_pch_refclk(dev);
8041}
8042
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008043static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008044{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008045 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008046 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008047 struct drm_atomic_state *state = crtc_state->base.state;
8048 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008049 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008050 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008051 bool is_lvds = false;
8052
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008053 for (i = 0; i < state->num_connector; i++) {
8054 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008055 continue;
8056
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008057 connector_state = state->connector_states[i];
8058 if (connector_state->crtc != crtc_state->base.crtc)
8059 continue;
8060
8061 encoder = to_intel_encoder(connector_state->best_encoder);
8062
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008063 switch (encoder->type) {
8064 case INTEL_OUTPUT_LVDS:
8065 is_lvds = true;
8066 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008067 default:
8068 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008069 }
8070 num_connectors++;
8071 }
8072
8073 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008074 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008075 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008076 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008077 }
8078
8079 return 120000;
8080}
8081
Daniel Vetter6ff93602013-04-19 11:24:36 +02008082static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008083{
8084 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8086 int pipe = intel_crtc->pipe;
8087 uint32_t val;
8088
Daniel Vetter78114072013-06-13 00:54:57 +02008089 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008090
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008091 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008092 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008093 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008094 break;
8095 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008096 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008097 break;
8098 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008099 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008100 break;
8101 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008102 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008103 break;
8104 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008105 /* Case prevented by intel_choose_pipe_bpp_dither. */
8106 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008107 }
8108
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008109 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008110 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8111
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008112 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008113 val |= PIPECONF_INTERLACED_ILK;
8114 else
8115 val |= PIPECONF_PROGRESSIVE;
8116
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008117 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008118 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008119
Paulo Zanonic8203562012-09-12 10:06:29 -03008120 I915_WRITE(PIPECONF(pipe), val);
8121 POSTING_READ(PIPECONF(pipe));
8122}
8123
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008124/*
8125 * Set up the pipe CSC unit.
8126 *
8127 * Currently only full range RGB to limited range RGB conversion
8128 * is supported, but eventually this should handle various
8129 * RGB<->YCbCr scenarios as well.
8130 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008131static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008132{
8133 struct drm_device *dev = crtc->dev;
8134 struct drm_i915_private *dev_priv = dev->dev_private;
8135 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8136 int pipe = intel_crtc->pipe;
8137 uint16_t coeff = 0x7800; /* 1.0 */
8138
8139 /*
8140 * TODO: Check what kind of values actually come out of the pipe
8141 * with these coeff/postoff values and adjust to get the best
8142 * accuracy. Perhaps we even need to take the bpc value into
8143 * consideration.
8144 */
8145
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008146 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008147 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8148
8149 /*
8150 * GY/GU and RY/RU should be the other way around according
8151 * to BSpec, but reality doesn't agree. Just set them up in
8152 * a way that results in the correct picture.
8153 */
8154 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8155 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8156
8157 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8158 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8159
8160 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8161 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8162
8163 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8164 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8165 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8166
8167 if (INTEL_INFO(dev)->gen > 6) {
8168 uint16_t postoff = 0;
8169
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008170 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008171 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008172
8173 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8174 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8175 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8176
8177 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8178 } else {
8179 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8180
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008181 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008182 mode |= CSC_BLACK_SCREEN_OFFSET;
8183
8184 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8185 }
8186}
8187
Daniel Vetter6ff93602013-04-19 11:24:36 +02008188static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008189{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008190 struct drm_device *dev = crtc->dev;
8191 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008192 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008193 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008194 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008195 uint32_t val;
8196
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008197 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008198
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008199 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008200 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8201
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008202 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008203 val |= PIPECONF_INTERLACED_ILK;
8204 else
8205 val |= PIPECONF_PROGRESSIVE;
8206
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008207 I915_WRITE(PIPECONF(cpu_transcoder), val);
8208 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008209
8210 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8211 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008212
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308213 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008214 val = 0;
8215
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008216 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008217 case 18:
8218 val |= PIPEMISC_DITHER_6_BPC;
8219 break;
8220 case 24:
8221 val |= PIPEMISC_DITHER_8_BPC;
8222 break;
8223 case 30:
8224 val |= PIPEMISC_DITHER_10_BPC;
8225 break;
8226 case 36:
8227 val |= PIPEMISC_DITHER_12_BPC;
8228 break;
8229 default:
8230 /* Case prevented by pipe_config_set_bpp. */
8231 BUG();
8232 }
8233
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008234 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008235 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8236
8237 I915_WRITE(PIPEMISC(pipe), val);
8238 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008239}
8240
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008241static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008242 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008243 intel_clock_t *clock,
8244 bool *has_reduced_clock,
8245 intel_clock_t *reduced_clock)
8246{
8247 struct drm_device *dev = crtc->dev;
8248 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008249 int refclk;
8250 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008251 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008252
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008253 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008254
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008255 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008256
8257 /*
8258 * Returns a set of divisors for the desired target clock with the given
8259 * refclk, or FALSE. The returned values represent the clock equation:
8260 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8261 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008262 limit = intel_limit(crtc_state, refclk);
8263 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008264 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008265 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008266 if (!ret)
8267 return false;
8268
8269 if (is_lvds && dev_priv->lvds_downclock_avail) {
8270 /*
8271 * Ensure we match the reduced clock's P to the target clock.
8272 * If the clocks don't match, we can't switch the display clock
8273 * by using the FP0/FP1. In such case we will disable the LVDS
8274 * downclock feature.
8275 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008276 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008277 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008278 dev_priv->lvds_downclock,
8279 refclk, clock,
8280 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008281 }
8282
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008283 return true;
8284}
8285
Paulo Zanonid4b19312012-11-29 11:29:32 -02008286int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8287{
8288 /*
8289 * Account for spread spectrum to avoid
8290 * oversubscribing the link. Max center spread
8291 * is 2.5%; use 5% for safety's sake.
8292 */
8293 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008294 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008295}
8296
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008297static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008298{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008299 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008300}
8301
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008302static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008303 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008304 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008305 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008306{
8307 struct drm_crtc *crtc = &intel_crtc->base;
8308 struct drm_device *dev = crtc->dev;
8309 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008310 struct drm_atomic_state *state = crtc_state->base.state;
8311 struct drm_connector_state *connector_state;
8312 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008313 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008314 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008315 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008316
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008317 for (i = 0; i < state->num_connector; i++) {
8318 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008319 continue;
8320
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008321 connector_state = state->connector_states[i];
8322 if (connector_state->crtc != crtc_state->base.crtc)
8323 continue;
8324
8325 encoder = to_intel_encoder(connector_state->best_encoder);
8326
8327 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008328 case INTEL_OUTPUT_LVDS:
8329 is_lvds = true;
8330 break;
8331 case INTEL_OUTPUT_SDVO:
8332 case INTEL_OUTPUT_HDMI:
8333 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008334 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008335 default:
8336 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008337 }
8338
8339 num_connectors++;
8340 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008341
Chris Wilsonc1858122010-12-03 21:35:48 +00008342 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008343 factor = 21;
8344 if (is_lvds) {
8345 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008346 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008347 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008348 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008349 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008350 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008351
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008352 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008353 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008354
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008355 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8356 *fp2 |= FP_CB_TUNE;
8357
Chris Wilson5eddb702010-09-11 13:48:45 +01008358 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008359
Eric Anholta07d6782011-03-30 13:01:08 -07008360 if (is_lvds)
8361 dpll |= DPLLB_MODE_LVDS;
8362 else
8363 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008364
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008365 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008366 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008367
8368 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008369 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008370 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008371 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008372
Eric Anholta07d6782011-03-30 13:01:08 -07008373 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008374 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008375 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008376 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008377
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008378 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008379 case 5:
8380 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8381 break;
8382 case 7:
8383 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8384 break;
8385 case 10:
8386 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8387 break;
8388 case 14:
8389 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8390 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008391 }
8392
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008393 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008394 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008395 else
8396 dpll |= PLL_REF_INPUT_DREFCLK;
8397
Daniel Vetter959e16d2013-06-05 13:34:21 +02008398 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008399}
8400
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008401static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8402 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008403{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008404 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008405 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008406 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008407 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008408 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008409 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008410
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008411 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008412
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008413 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8414 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8415
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008416 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008417 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008418 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008419 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8420 return -EINVAL;
8421 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008422 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008423 if (!crtc_state->clock_set) {
8424 crtc_state->dpll.n = clock.n;
8425 crtc_state->dpll.m1 = clock.m1;
8426 crtc_state->dpll.m2 = clock.m2;
8427 crtc_state->dpll.p1 = clock.p1;
8428 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008429 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008430
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008431 /* CPU eDP is the only output that doesn't need a PCH PLL of its own. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008432 if (crtc_state->has_pch_encoder) {
8433 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008434 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008435 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008436
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008437 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008438 &fp, &reduced_clock,
8439 has_reduced_clock ? &fp2 : NULL);
8440
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008441 crtc_state->dpll_hw_state.dpll = dpll;
8442 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008443 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008444 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008445 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008446 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008447
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008448 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008449 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008450 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008451 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008452 return -EINVAL;
8453 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008454 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008455
Rodrigo Viviab585de2015-03-24 12:40:09 -07008456 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008457 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008458 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008459 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008460
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008461 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008462}
8463
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008464static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8465 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008466{
8467 struct drm_device *dev = crtc->base.dev;
8468 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008469 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008470
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008471 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8472 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8473 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8474 & ~TU_SIZE_MASK;
8475 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8476 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8477 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8478}
8479
8480static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8481 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008482 struct intel_link_m_n *m_n,
8483 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008484{
8485 struct drm_device *dev = crtc->base.dev;
8486 struct drm_i915_private *dev_priv = dev->dev_private;
8487 enum pipe pipe = crtc->pipe;
8488
8489 if (INTEL_INFO(dev)->gen >= 5) {
8490 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8491 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8492 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8493 & ~TU_SIZE_MASK;
8494 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8495 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8496 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008497 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8498 * gen < 8) and if DRRS is supported (to make sure the
8499 * registers are not unnecessarily read).
8500 */
8501 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008502 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008503 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8504 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8505 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8506 & ~TU_SIZE_MASK;
8507 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8508 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8509 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8510 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008511 } else {
8512 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8513 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8514 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8515 & ~TU_SIZE_MASK;
8516 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8517 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8518 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8519 }
8520}
8521
8522void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008523 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008524{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008525 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008526 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8527 else
8528 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008529 &pipe_config->dp_m_n,
8530 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008531}
8532
Daniel Vetter72419202013-04-04 13:28:53 +02008533static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008534 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008535{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008536 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008537 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008538}
8539
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008540static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008541 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008542{
8543 struct drm_device *dev = crtc->base.dev;
8544 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008545 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8546 uint32_t ps_ctrl = 0;
8547 int id = -1;
8548 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008549
Chandra Kondurua1b22782015-04-07 15:28:45 -07008550 /* find scaler attached to this pipe */
8551 for (i = 0; i < crtc->num_scalers; i++) {
8552 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8553 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8554 id = i;
8555 pipe_config->pch_pfit.enabled = true;
8556 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8557 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8558 break;
8559 }
8560 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008561
Chandra Kondurua1b22782015-04-07 15:28:45 -07008562 scaler_state->scaler_id = id;
8563 if (id >= 0) {
8564 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8565 } else {
8566 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008567 }
8568}
8569
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008570static void
8571skylake_get_initial_plane_config(struct intel_crtc *crtc,
8572 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008573{
8574 struct drm_device *dev = crtc->base.dev;
8575 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008576 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008577 int pipe = crtc->pipe;
8578 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008579 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008580 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008581 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008582
Damien Lespiaud9806c92015-01-21 14:07:19 +00008583 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008584 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008585 DRM_DEBUG_KMS("failed to alloc fb\n");
8586 return;
8587 }
8588
Damien Lespiau1b842c82015-01-21 13:50:54 +00008589 fb = &intel_fb->base;
8590
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008591 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008592 if (!(val & PLANE_CTL_ENABLE))
8593 goto error;
8594
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008595 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8596 fourcc = skl_format_to_fourcc(pixel_format,
8597 val & PLANE_CTL_ORDER_RGBX,
8598 val & PLANE_CTL_ALPHA_MASK);
8599 fb->pixel_format = fourcc;
8600 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8601
Damien Lespiau40f46282015-02-27 11:15:21 +00008602 tiling = val & PLANE_CTL_TILED_MASK;
8603 switch (tiling) {
8604 case PLANE_CTL_TILED_LINEAR:
8605 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8606 break;
8607 case PLANE_CTL_TILED_X:
8608 plane_config->tiling = I915_TILING_X;
8609 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8610 break;
8611 case PLANE_CTL_TILED_Y:
8612 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8613 break;
8614 case PLANE_CTL_TILED_YF:
8615 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8616 break;
8617 default:
8618 MISSING_CASE(tiling);
8619 goto error;
8620 }
8621
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008622 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8623 plane_config->base = base;
8624
8625 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8626
8627 val = I915_READ(PLANE_SIZE(pipe, 0));
8628 fb->height = ((val >> 16) & 0xfff) + 1;
8629 fb->width = ((val >> 0) & 0x1fff) + 1;
8630
8631 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008632 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8633 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008634 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8635
8636 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008637 fb->pixel_format,
8638 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008639
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008640 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008641
8642 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8643 pipe_name(pipe), fb->width, fb->height,
8644 fb->bits_per_pixel, base, fb->pitches[0],
8645 plane_config->size);
8646
Damien Lespiau2d140302015-02-05 17:22:18 +00008647 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008648 return;
8649
8650error:
8651 kfree(fb);
8652}
8653
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008654static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008655 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008656{
8657 struct drm_device *dev = crtc->base.dev;
8658 struct drm_i915_private *dev_priv = dev->dev_private;
8659 uint32_t tmp;
8660
8661 tmp = I915_READ(PF_CTL(crtc->pipe));
8662
8663 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008664 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008665 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8666 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008667
8668 /* We currently do not free assignements of panel fitters on
8669 * ivb/hsw (since we don't use the higher upscaling modes which
8670 * differentiates them) so just WARN about this case for now. */
8671 if (IS_GEN7(dev)) {
8672 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8673 PF_PIPE_SEL_IVB(crtc->pipe));
8674 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008675 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008676}
8677
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008678static void
8679ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8680 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008681{
8682 struct drm_device *dev = crtc->base.dev;
8683 struct drm_i915_private *dev_priv = dev->dev_private;
8684 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008685 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008686 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008687 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008688 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008689 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008690
Damien Lespiau42a7b082015-02-05 19:35:13 +00008691 val = I915_READ(DSPCNTR(pipe));
8692 if (!(val & DISPLAY_PLANE_ENABLE))
8693 return;
8694
Damien Lespiaud9806c92015-01-21 14:07:19 +00008695 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008696 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008697 DRM_DEBUG_KMS("failed to alloc fb\n");
8698 return;
8699 }
8700
Damien Lespiau1b842c82015-01-21 13:50:54 +00008701 fb = &intel_fb->base;
8702
Daniel Vetter18c52472015-02-10 17:16:09 +00008703 if (INTEL_INFO(dev)->gen >= 4) {
8704 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008705 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008706 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8707 }
8708 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008709
8710 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008711 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008712 fb->pixel_format = fourcc;
8713 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008714
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008715 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008716 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008717 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008718 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008719 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008720 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008721 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008722 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008723 }
8724 plane_config->base = base;
8725
8726 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008727 fb->width = ((val >> 16) & 0xfff) + 1;
8728 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008729
8730 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008731 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008732
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008733 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008734 fb->pixel_format,
8735 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008736
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008737 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008738
Damien Lespiau2844a922015-01-20 12:51:48 +00008739 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8740 pipe_name(pipe), fb->width, fb->height,
8741 fb->bits_per_pixel, base, fb->pitches[0],
8742 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008743
Damien Lespiau2d140302015-02-05 17:22:18 +00008744 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008745}
8746
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008747static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008748 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008749{
8750 struct drm_device *dev = crtc->base.dev;
8751 struct drm_i915_private *dev_priv = dev->dev_private;
8752 uint32_t tmp;
8753
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008754 if (!intel_display_power_is_enabled(dev_priv,
8755 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008756 return false;
8757
Daniel Vettere143a212013-07-04 12:01:15 +02008758 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008759 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008760
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008761 tmp = I915_READ(PIPECONF(crtc->pipe));
8762 if (!(tmp & PIPECONF_ENABLE))
8763 return false;
8764
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008765 switch (tmp & PIPECONF_BPC_MASK) {
8766 case PIPECONF_6BPC:
8767 pipe_config->pipe_bpp = 18;
8768 break;
8769 case PIPECONF_8BPC:
8770 pipe_config->pipe_bpp = 24;
8771 break;
8772 case PIPECONF_10BPC:
8773 pipe_config->pipe_bpp = 30;
8774 break;
8775 case PIPECONF_12BPC:
8776 pipe_config->pipe_bpp = 36;
8777 break;
8778 default:
8779 break;
8780 }
8781
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008782 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8783 pipe_config->limited_color_range = true;
8784
Daniel Vetterab9412b2013-05-03 11:49:46 +02008785 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008786 struct intel_shared_dpll *pll;
8787
Daniel Vetter88adfff2013-03-28 10:42:01 +01008788 pipe_config->has_pch_encoder = true;
8789
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008790 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8791 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8792 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008793
8794 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008795
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008796 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008797 pipe_config->shared_dpll =
8798 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008799 } else {
8800 tmp = I915_READ(PCH_DPLL_SEL);
8801 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8802 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8803 else
8804 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8805 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008806
8807 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8808
8809 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8810 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008811
8812 tmp = pipe_config->dpll_hw_state.dpll;
8813 pipe_config->pixel_multiplier =
8814 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8815 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008816
8817 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008818 } else {
8819 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008820 }
8821
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008822 intel_get_pipe_timings(crtc, pipe_config);
8823
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008824 ironlake_get_pfit_config(crtc, pipe_config);
8825
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008826 return true;
8827}
8828
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008829static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8830{
8831 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008832 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008833
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008834 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008835 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008836 pipe_name(crtc->pipe));
8837
Rob Clarke2c719b2014-12-15 13:56:32 -05008838 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8839 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8840 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8841 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8842 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8843 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008844 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008845 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008846 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008847 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008848 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008849 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008850 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008851 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008852 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008853
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008854 /*
8855 * In theory we can still leave IRQs enabled, as long as only the HPD
8856 * interrupts remain enabled. We used to check for that, but since it's
8857 * gen-specific and since we only disable LCPLL after we fully disable
8858 * the interrupts, the check below should be enough.
8859 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008860 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008861}
8862
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008863static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8864{
8865 struct drm_device *dev = dev_priv->dev;
8866
8867 if (IS_HASWELL(dev))
8868 return I915_READ(D_COMP_HSW);
8869 else
8870 return I915_READ(D_COMP_BDW);
8871}
8872
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008873static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8874{
8875 struct drm_device *dev = dev_priv->dev;
8876
8877 if (IS_HASWELL(dev)) {
8878 mutex_lock(&dev_priv->rps.hw_lock);
8879 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8880 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008881 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008882 mutex_unlock(&dev_priv->rps.hw_lock);
8883 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008884 I915_WRITE(D_COMP_BDW, val);
8885 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008886 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008887}
8888
8889/*
8890 * This function implements pieces of two sequences from BSpec:
8891 * - Sequence for display software to disable LCPLL
8892 * - Sequence for display software to allow package C8+
8893 * The steps implemented here are just the steps that actually touch the LCPLL
8894 * register. Callers should take care of disabling all the display engine
8895 * functions, doing the mode unset, fixing interrupts, etc.
8896 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008897static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8898 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008899{
8900 uint32_t val;
8901
8902 assert_can_disable_lcpll(dev_priv);
8903
8904 val = I915_READ(LCPLL_CTL);
8905
8906 if (switch_to_fclk) {
8907 val |= LCPLL_CD_SOURCE_FCLK;
8908 I915_WRITE(LCPLL_CTL, val);
8909
8910 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8911 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8912 DRM_ERROR("Switching to FCLK failed\n");
8913
8914 val = I915_READ(LCPLL_CTL);
8915 }
8916
8917 val |= LCPLL_PLL_DISABLE;
8918 I915_WRITE(LCPLL_CTL, val);
8919 POSTING_READ(LCPLL_CTL);
8920
8921 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8922 DRM_ERROR("LCPLL still locked\n");
8923
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008924 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008925 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008926 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008927 ndelay(100);
8928
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008929 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8930 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008931 DRM_ERROR("D_COMP RCOMP still in progress\n");
8932
8933 if (allow_power_down) {
8934 val = I915_READ(LCPLL_CTL);
8935 val |= LCPLL_POWER_DOWN_ALLOW;
8936 I915_WRITE(LCPLL_CTL, val);
8937 POSTING_READ(LCPLL_CTL);
8938 }
8939}
8940
8941/*
8942 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8943 * source.
8944 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008945static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008946{
8947 uint32_t val;
8948
8949 val = I915_READ(LCPLL_CTL);
8950
8951 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
8952 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
8953 return;
8954
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008955 /*
8956 * Make sure we're not on PC8 state before disabling PC8, otherwise
8957 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03008958 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02008959 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03008960
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008961 if (val & LCPLL_POWER_DOWN_ALLOW) {
8962 val &= ~LCPLL_POWER_DOWN_ALLOW;
8963 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02008964 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008965 }
8966
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008967 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008968 val |= D_COMP_COMP_FORCE;
8969 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008970 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008971
8972 val = I915_READ(LCPLL_CTL);
8973 val &= ~LCPLL_PLL_DISABLE;
8974 I915_WRITE(LCPLL_CTL, val);
8975
8976 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
8977 DRM_ERROR("LCPLL not locked yet\n");
8978
8979 if (val & LCPLL_CD_SOURCE_FCLK) {
8980 val = I915_READ(LCPLL_CTL);
8981 val &= ~LCPLL_CD_SOURCE_FCLK;
8982 I915_WRITE(LCPLL_CTL, val);
8983
8984 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
8985 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
8986 DRM_ERROR("Switching back to LCPLL failed\n");
8987 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03008988
Mika Kuoppala59bad942015-01-16 11:34:40 +02008989 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008990}
8991
Paulo Zanoni765dab672014-03-07 20:08:18 -03008992/*
8993 * Package states C8 and deeper are really deep PC states that can only be
8994 * reached when all the devices on the system allow it, so even if the graphics
8995 * device allows PC8+, it doesn't mean the system will actually get to these
8996 * states. Our driver only allows PC8+ when going into runtime PM.
8997 *
8998 * The requirements for PC8+ are that all the outputs are disabled, the power
8999 * well is disabled and most interrupts are disabled, and these are also
9000 * requirements for runtime PM. When these conditions are met, we manually do
9001 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9002 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9003 * hang the machine.
9004 *
9005 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9006 * the state of some registers, so when we come back from PC8+ we need to
9007 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9008 * need to take care of the registers kept by RC6. Notice that this happens even
9009 * if we don't put the device in PCI D3 state (which is what currently happens
9010 * because of the runtime PM support).
9011 *
9012 * For more, read "Display Sequences for Package C8" on the hardware
9013 * documentation.
9014 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009015void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009016{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009017 struct drm_device *dev = dev_priv->dev;
9018 uint32_t val;
9019
Paulo Zanonic67a4702013-08-19 13:18:09 -03009020 DRM_DEBUG_KMS("Enabling package C8+\n");
9021
Paulo Zanonic67a4702013-08-19 13:18:09 -03009022 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9023 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9024 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9025 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9026 }
9027
9028 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009029 hsw_disable_lcpll(dev_priv, true, true);
9030}
9031
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009032void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009033{
9034 struct drm_device *dev = dev_priv->dev;
9035 uint32_t val;
9036
Paulo Zanonic67a4702013-08-19 13:18:09 -03009037 DRM_DEBUG_KMS("Disabling package C8+\n");
9038
9039 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009040 lpt_init_pch_refclk(dev);
9041
9042 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9043 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9044 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9045 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9046 }
9047
9048 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009049}
9050
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309051static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9052{
9053 struct drm_device *dev = state->dev;
9054 struct drm_i915_private *dev_priv = dev->dev_private;
9055 int max_pixclk = intel_mode_max_pixclk(state);
9056 int req_cdclk;
9057
9058 /* see the comment in valleyview_modeset_global_resources */
9059 if (WARN_ON(max_pixclk < 0))
9060 return;
9061
9062 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9063
9064 if (req_cdclk != dev_priv->cdclk_freq)
9065 broxton_set_cdclk(dev, req_cdclk);
9066}
9067
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009068static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9069 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009070{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009071 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009072 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009073
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009074 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009075
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009076 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009077}
9078
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009079static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9080 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009081 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009082{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009083 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009084
9085 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9086 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9087
9088 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009089 case SKL_DPLL0:
9090 /*
9091 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9092 * of the shared DPLL framework and thus needs to be read out
9093 * separately
9094 */
9095 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9096 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9097 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009098 case SKL_DPLL1:
9099 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9100 break;
9101 case SKL_DPLL2:
9102 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9103 break;
9104 case SKL_DPLL3:
9105 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9106 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009107 }
9108}
9109
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009110static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9111 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009112 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009113{
9114 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9115
9116 switch (pipe_config->ddi_pll_sel) {
9117 case PORT_CLK_SEL_WRPLL1:
9118 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9119 break;
9120 case PORT_CLK_SEL_WRPLL2:
9121 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9122 break;
9123 }
9124}
9125
Daniel Vetter26804af2014-06-25 22:01:55 +03009126static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009127 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009128{
9129 struct drm_device *dev = crtc->base.dev;
9130 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009131 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009132 enum port port;
9133 uint32_t tmp;
9134
9135 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9136
9137 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9138
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009139 if (IS_SKYLAKE(dev))
9140 skylake_get_ddi_pll(dev_priv, port, pipe_config);
9141 else
9142 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009143
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009144 if (pipe_config->shared_dpll >= 0) {
9145 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9146
9147 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9148 &pipe_config->dpll_hw_state));
9149 }
9150
Daniel Vetter26804af2014-06-25 22:01:55 +03009151 /*
9152 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9153 * DDI E. So just check whether this pipe is wired to DDI E and whether
9154 * the PCH transcoder is on.
9155 */
Damien Lespiauca370452013-12-03 13:56:24 +00009156 if (INTEL_INFO(dev)->gen < 9 &&
9157 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009158 pipe_config->has_pch_encoder = true;
9159
9160 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9161 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9162 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9163
9164 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9165 }
9166}
9167
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009168static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009169 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009170{
9171 struct drm_device *dev = crtc->base.dev;
9172 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009173 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009174 uint32_t tmp;
9175
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009176 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009177 POWER_DOMAIN_PIPE(crtc->pipe)))
9178 return false;
9179
Daniel Vettere143a212013-07-04 12:01:15 +02009180 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009181 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9182
Daniel Vettereccb1402013-05-22 00:50:22 +02009183 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9184 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9185 enum pipe trans_edp_pipe;
9186 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9187 default:
9188 WARN(1, "unknown pipe linked to edp transcoder\n");
9189 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9190 case TRANS_DDI_EDP_INPUT_A_ON:
9191 trans_edp_pipe = PIPE_A;
9192 break;
9193 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9194 trans_edp_pipe = PIPE_B;
9195 break;
9196 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9197 trans_edp_pipe = PIPE_C;
9198 break;
9199 }
9200
9201 if (trans_edp_pipe == crtc->pipe)
9202 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9203 }
9204
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009205 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009206 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009207 return false;
9208
Daniel Vettereccb1402013-05-22 00:50:22 +02009209 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009210 if (!(tmp & PIPECONF_ENABLE))
9211 return false;
9212
Daniel Vetter26804af2014-06-25 22:01:55 +03009213 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009214
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009215 intel_get_pipe_timings(crtc, pipe_config);
9216
Chandra Kondurua1b22782015-04-07 15:28:45 -07009217 if (INTEL_INFO(dev)->gen >= 9) {
9218 skl_init_scalers(dev, crtc, pipe_config);
9219 }
9220
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009221 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009222 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009223 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009224 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009225 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009226 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009227 else
9228 MISSING_CASE(INTEL_INFO(dev)->gen);
9229
Chandra Kondurua1b22782015-04-07 15:28:45 -07009230 } else {
9231 pipe_config->scaler_state.scaler_id = -1;
9232 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009233 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009234
Jesse Barnese59150d2014-01-07 13:30:45 -08009235 if (IS_HASWELL(dev))
9236 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9237 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009238
Clint Taylorebb69c92014-09-30 10:30:22 -07009239 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9240 pipe_config->pixel_multiplier =
9241 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9242 } else {
9243 pipe_config->pixel_multiplier = 1;
9244 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009245
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009246 return true;
9247}
9248
Chris Wilson560b85b2010-08-07 11:01:38 +01009249static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9250{
9251 struct drm_device *dev = crtc->dev;
9252 struct drm_i915_private *dev_priv = dev->dev_private;
9253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009254 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009255
Ville Syrjälädc41c152014-08-13 11:57:05 +03009256 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009257 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9258 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009259 unsigned int stride = roundup_pow_of_two(width) * 4;
9260
9261 switch (stride) {
9262 default:
9263 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9264 width, stride);
9265 stride = 256;
9266 /* fallthrough */
9267 case 256:
9268 case 512:
9269 case 1024:
9270 case 2048:
9271 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009272 }
9273
Ville Syrjälädc41c152014-08-13 11:57:05 +03009274 cntl |= CURSOR_ENABLE |
9275 CURSOR_GAMMA_ENABLE |
9276 CURSOR_FORMAT_ARGB |
9277 CURSOR_STRIDE(stride);
9278
9279 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009280 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009281
Ville Syrjälädc41c152014-08-13 11:57:05 +03009282 if (intel_crtc->cursor_cntl != 0 &&
9283 (intel_crtc->cursor_base != base ||
9284 intel_crtc->cursor_size != size ||
9285 intel_crtc->cursor_cntl != cntl)) {
9286 /* On these chipsets we can only modify the base/size/stride
9287 * whilst the cursor is disabled.
9288 */
9289 I915_WRITE(_CURACNTR, 0);
9290 POSTING_READ(_CURACNTR);
9291 intel_crtc->cursor_cntl = 0;
9292 }
9293
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009294 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009295 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009296 intel_crtc->cursor_base = base;
9297 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009298
9299 if (intel_crtc->cursor_size != size) {
9300 I915_WRITE(CURSIZE, size);
9301 intel_crtc->cursor_size = size;
9302 }
9303
Chris Wilson4b0e3332014-05-30 16:35:26 +03009304 if (intel_crtc->cursor_cntl != cntl) {
9305 I915_WRITE(_CURACNTR, cntl);
9306 POSTING_READ(_CURACNTR);
9307 intel_crtc->cursor_cntl = cntl;
9308 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009309}
9310
9311static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9312{
9313 struct drm_device *dev = crtc->dev;
9314 struct drm_i915_private *dev_priv = dev->dev_private;
9315 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9316 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009317 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009318
Chris Wilson4b0e3332014-05-30 16:35:26 +03009319 cntl = 0;
9320 if (base) {
9321 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009322 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309323 case 64:
9324 cntl |= CURSOR_MODE_64_ARGB_AX;
9325 break;
9326 case 128:
9327 cntl |= CURSOR_MODE_128_ARGB_AX;
9328 break;
9329 case 256:
9330 cntl |= CURSOR_MODE_256_ARGB_AX;
9331 break;
9332 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009333 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309334 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009335 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009336 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009337
9338 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9339 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009340 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009341
Matt Roper8e7d6882015-01-21 16:35:41 -08009342 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009343 cntl |= CURSOR_ROTATE_180;
9344
Chris Wilson4b0e3332014-05-30 16:35:26 +03009345 if (intel_crtc->cursor_cntl != cntl) {
9346 I915_WRITE(CURCNTR(pipe), cntl);
9347 POSTING_READ(CURCNTR(pipe));
9348 intel_crtc->cursor_cntl = cntl;
9349 }
9350
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009351 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009352 I915_WRITE(CURBASE(pipe), base);
9353 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009354
9355 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009356}
9357
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009358/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009359static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9360 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009361{
9362 struct drm_device *dev = crtc->dev;
9363 struct drm_i915_private *dev_priv = dev->dev_private;
9364 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9365 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009366 int x = crtc->cursor_x;
9367 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009368 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009369
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009370 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009371 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009372
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009373 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009374 base = 0;
9375
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009376 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009377 base = 0;
9378
9379 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009380 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009381 base = 0;
9382
9383 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9384 x = -x;
9385 }
9386 pos |= x << CURSOR_X_SHIFT;
9387
9388 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009389 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009390 base = 0;
9391
9392 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9393 y = -y;
9394 }
9395 pos |= y << CURSOR_Y_SHIFT;
9396
Chris Wilson4b0e3332014-05-30 16:35:26 +03009397 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009398 return;
9399
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009400 I915_WRITE(CURPOS(pipe), pos);
9401
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009402 /* ILK+ do this automagically */
9403 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009404 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009405 base += (intel_crtc->base.cursor->state->crtc_h *
9406 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009407 }
9408
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009409 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009410 i845_update_cursor(crtc, base);
9411 else
9412 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009413}
9414
Ville Syrjälädc41c152014-08-13 11:57:05 +03009415static bool cursor_size_ok(struct drm_device *dev,
9416 uint32_t width, uint32_t height)
9417{
9418 if (width == 0 || height == 0)
9419 return false;
9420
9421 /*
9422 * 845g/865g are special in that they are only limited by
9423 * the width of their cursors, the height is arbitrary up to
9424 * the precision of the register. Everything else requires
9425 * square cursors, limited to a few power-of-two sizes.
9426 */
9427 if (IS_845G(dev) || IS_I865G(dev)) {
9428 if ((width & 63) != 0)
9429 return false;
9430
9431 if (width > (IS_845G(dev) ? 64 : 512))
9432 return false;
9433
9434 if (height > 1023)
9435 return false;
9436 } else {
9437 switch (width | height) {
9438 case 256:
9439 case 128:
9440 if (IS_GEN2(dev))
9441 return false;
9442 case 64:
9443 break;
9444 default:
9445 return false;
9446 }
9447 }
9448
9449 return true;
9450}
9451
Jesse Barnes79e53942008-11-07 14:24:08 -08009452static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009453 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009454{
James Simmons72034252010-08-03 01:33:19 +01009455 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009456 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009457
James Simmons72034252010-08-03 01:33:19 +01009458 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009459 intel_crtc->lut_r[i] = red[i] >> 8;
9460 intel_crtc->lut_g[i] = green[i] >> 8;
9461 intel_crtc->lut_b[i] = blue[i] >> 8;
9462 }
9463
9464 intel_crtc_load_lut(crtc);
9465}
9466
Jesse Barnes79e53942008-11-07 14:24:08 -08009467/* VESA 640x480x72Hz mode to set on the pipe */
9468static struct drm_display_mode load_detect_mode = {
9469 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9470 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9471};
9472
Daniel Vettera8bb6812014-02-10 18:00:39 +01009473struct drm_framebuffer *
9474__intel_framebuffer_create(struct drm_device *dev,
9475 struct drm_mode_fb_cmd2 *mode_cmd,
9476 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009477{
9478 struct intel_framebuffer *intel_fb;
9479 int ret;
9480
9481 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9482 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009483 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009484 return ERR_PTR(-ENOMEM);
9485 }
9486
9487 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009488 if (ret)
9489 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009490
9491 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009492err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009493 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009494 kfree(intel_fb);
9495
9496 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009497}
9498
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009499static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009500intel_framebuffer_create(struct drm_device *dev,
9501 struct drm_mode_fb_cmd2 *mode_cmd,
9502 struct drm_i915_gem_object *obj)
9503{
9504 struct drm_framebuffer *fb;
9505 int ret;
9506
9507 ret = i915_mutex_lock_interruptible(dev);
9508 if (ret)
9509 return ERR_PTR(ret);
9510 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9511 mutex_unlock(&dev->struct_mutex);
9512
9513 return fb;
9514}
9515
Chris Wilsond2dff872011-04-19 08:36:26 +01009516static u32
9517intel_framebuffer_pitch_for_width(int width, int bpp)
9518{
9519 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9520 return ALIGN(pitch, 64);
9521}
9522
9523static u32
9524intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9525{
9526 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009527 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009528}
9529
9530static struct drm_framebuffer *
9531intel_framebuffer_create_for_mode(struct drm_device *dev,
9532 struct drm_display_mode *mode,
9533 int depth, int bpp)
9534{
9535 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009536 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009537
9538 obj = i915_gem_alloc_object(dev,
9539 intel_framebuffer_size_for_mode(mode, bpp));
9540 if (obj == NULL)
9541 return ERR_PTR(-ENOMEM);
9542
9543 mode_cmd.width = mode->hdisplay;
9544 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009545 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9546 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009547 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009548
9549 return intel_framebuffer_create(dev, &mode_cmd, obj);
9550}
9551
9552static struct drm_framebuffer *
9553mode_fits_in_fbdev(struct drm_device *dev,
9554 struct drm_display_mode *mode)
9555{
Daniel Vetter4520f532013-10-09 09:18:51 +02009556#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009557 struct drm_i915_private *dev_priv = dev->dev_private;
9558 struct drm_i915_gem_object *obj;
9559 struct drm_framebuffer *fb;
9560
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009561 if (!dev_priv->fbdev)
9562 return NULL;
9563
9564 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009565 return NULL;
9566
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009567 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009568 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009569
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009570 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009571 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9572 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009573 return NULL;
9574
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009575 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009576 return NULL;
9577
9578 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009579#else
9580 return NULL;
9581#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009582}
9583
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009584bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009585 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009586 struct intel_load_detect_pipe *old,
9587 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009588{
9589 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009590 struct intel_encoder *intel_encoder =
9591 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009592 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009593 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009594 struct drm_crtc *crtc = NULL;
9595 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009596 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009597 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009598 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009599 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009600 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009601
Chris Wilsond2dff872011-04-19 08:36:26 +01009602 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009603 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009604 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009605
Rob Clark51fd3712013-11-19 12:10:12 -05009606retry:
9607 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9608 if (ret)
9609 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009610
Jesse Barnes79e53942008-11-07 14:24:08 -08009611 /*
9612 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009613 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009614 * - if the connector already has an assigned crtc, use it (but make
9615 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009616 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009617 * - try to find the first unused crtc that can drive this connector,
9618 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009619 */
9620
9621 /* See if we already have a CRTC for this connector */
9622 if (encoder->crtc) {
9623 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009624
Rob Clark51fd3712013-11-19 12:10:12 -05009625 ret = drm_modeset_lock(&crtc->mutex, ctx);
9626 if (ret)
9627 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009628 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9629 if (ret)
9630 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009631
Daniel Vetter24218aa2012-08-12 19:27:11 +02009632 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009633 old->load_detect_temp = false;
9634
9635 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009636 if (connector->dpms != DRM_MODE_DPMS_ON)
9637 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009638
Chris Wilson71731882011-04-19 23:10:58 +01009639 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009640 }
9641
9642 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009643 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009644 i++;
9645 if (!(encoder->possible_crtcs & (1 << i)))
9646 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009647 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009648 continue;
9649 /* This can occur when applying the pipe A quirk on resume. */
9650 if (to_intel_crtc(possible_crtc)->new_enabled)
9651 continue;
9652
9653 crtc = possible_crtc;
9654 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009655 }
9656
9657 /*
9658 * If we didn't find an unused CRTC, don't use any.
9659 */
9660 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009661 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009662 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009663 }
9664
Rob Clark51fd3712013-11-19 12:10:12 -05009665 ret = drm_modeset_lock(&crtc->mutex, ctx);
9666 if (ret)
9667 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009668 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9669 if (ret)
9670 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009671 intel_encoder->new_crtc = to_intel_crtc(crtc);
9672 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009673
9674 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009675 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009676 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009677 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009678 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009679
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009680 state = drm_atomic_state_alloc(dev);
9681 if (!state)
9682 return false;
9683
9684 state->acquire_ctx = ctx;
9685
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009686 connector_state = drm_atomic_get_connector_state(state, connector);
9687 if (IS_ERR(connector_state)) {
9688 ret = PTR_ERR(connector_state);
9689 goto fail;
9690 }
9691
9692 connector_state->crtc = crtc;
9693 connector_state->best_encoder = &intel_encoder->base;
9694
Chris Wilson64927112011-04-20 07:25:26 +01009695 if (!mode)
9696 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009697
Chris Wilsond2dff872011-04-19 08:36:26 +01009698 /* We need a framebuffer large enough to accommodate all accesses
9699 * that the plane may generate whilst we perform load detection.
9700 * We can not rely on the fbcon either being present (we get called
9701 * during its initialisation to detect all boot displays, or it may
9702 * not even exist) or that it is large enough to satisfy the
9703 * requested mode.
9704 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009705 fb = mode_fits_in_fbdev(dev, mode);
9706 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009707 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009708 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9709 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009710 } else
9711 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009712 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009713 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009714 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009715 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009716
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009717 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009718 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009719 if (old->release_fb)
9720 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009721 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009722 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009723 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009724
Jesse Barnes79e53942008-11-07 14:24:08 -08009725 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009726 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009727 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009728
9729 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009730 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009731fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009732 if (state) {
9733 drm_atomic_state_free(state);
9734 state = NULL;
9735 }
9736
Rob Clark51fd3712013-11-19 12:10:12 -05009737 if (ret == -EDEADLK) {
9738 drm_modeset_backoff(ctx);
9739 goto retry;
9740 }
9741
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009742 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009743}
9744
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009745void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009746 struct intel_load_detect_pipe *old,
9747 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009748{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009749 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009750 struct intel_encoder *intel_encoder =
9751 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009752 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009753 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009754 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009755 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009756 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009757
Chris Wilsond2dff872011-04-19 08:36:26 +01009758 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009759 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009760 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009761
Chris Wilson8261b192011-04-19 23:18:09 +01009762 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009763 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009764 if (!state)
9765 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009766
9767 state->acquire_ctx = ctx;
9768
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009769 connector_state = drm_atomic_get_connector_state(state, connector);
9770 if (IS_ERR(connector_state))
9771 goto fail;
9772
Daniel Vetterfc303102012-07-09 10:40:58 +02009773 to_intel_connector(connector)->new_encoder = NULL;
9774 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009775 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009776
9777 connector_state->best_encoder = NULL;
9778 connector_state->crtc = NULL;
9779
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009780 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9781
9782 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009783
Daniel Vetter36206362012-12-10 20:42:17 +01009784 if (old->release_fb) {
9785 drm_framebuffer_unregister_private(old->release_fb);
9786 drm_framebuffer_unreference(old->release_fb);
9787 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009788
Chris Wilson0622a532011-04-21 09:32:11 +01009789 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009790 }
9791
Eric Anholtc751ce42010-03-25 11:48:48 -07009792 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009793 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9794 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009795
9796 return;
9797fail:
9798 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9799 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009800}
9801
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009802static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009803 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009804{
9805 struct drm_i915_private *dev_priv = dev->dev_private;
9806 u32 dpll = pipe_config->dpll_hw_state.dpll;
9807
9808 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009809 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009810 else if (HAS_PCH_SPLIT(dev))
9811 return 120000;
9812 else if (!IS_GEN2(dev))
9813 return 96000;
9814 else
9815 return 48000;
9816}
9817
Jesse Barnes79e53942008-11-07 14:24:08 -08009818/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009819static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009820 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009821{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009822 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009823 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009824 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009825 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009826 u32 fp;
9827 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009828 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009829
9830 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009831 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009832 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009833 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009834
9835 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009836 if (IS_PINEVIEW(dev)) {
9837 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9838 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009839 } else {
9840 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9841 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9842 }
9843
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009844 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009845 if (IS_PINEVIEW(dev))
9846 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9847 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009848 else
9849 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009850 DPLL_FPA01_P1_POST_DIV_SHIFT);
9851
9852 switch (dpll & DPLL_MODE_MASK) {
9853 case DPLLB_MODE_DAC_SERIAL:
9854 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9855 5 : 10;
9856 break;
9857 case DPLLB_MODE_LVDS:
9858 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9859 7 : 14;
9860 break;
9861 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009862 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009863 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009864 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009865 }
9866
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009867 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009868 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009869 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009870 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009871 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009872 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009873 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009874
9875 if (is_lvds) {
9876 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9877 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009878
9879 if (lvds & LVDS_CLKB_POWER_UP)
9880 clock.p2 = 7;
9881 else
9882 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009883 } else {
9884 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9885 clock.p1 = 2;
9886 else {
9887 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9888 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9889 }
9890 if (dpll & PLL_P2_DIVIDE_BY_4)
9891 clock.p2 = 4;
9892 else
9893 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009894 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009895
9896 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009897 }
9898
Ville Syrjälä18442d02013-09-13 16:00:08 +03009899 /*
9900 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009901 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009902 * encoder's get_config() function.
9903 */
9904 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009905}
9906
Ville Syrjälä6878da02013-09-13 15:59:11 +03009907int intel_dotclock_calculate(int link_freq,
9908 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009909{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009910 /*
9911 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009912 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009913 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009914 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009915 *
9916 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009917 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009918 */
9919
Ville Syrjälä6878da02013-09-13 15:59:11 +03009920 if (!m_n->link_n)
9921 return 0;
9922
9923 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
9924}
9925
Ville Syrjälä18442d02013-09-13 16:00:08 +03009926static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009927 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +03009928{
9929 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009930
9931 /* read out port_clock from the DPLL */
9932 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +03009933
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009934 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +03009935 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +01009936 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +03009937 * agree once we know their relationship in the encoder's
9938 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009939 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02009940 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +03009941 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
9942 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -08009943}
9944
9945/** Returns the currently programmed mode of the given pipe. */
9946struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
9947 struct drm_crtc *crtc)
9948{
Jesse Barnes548f2452011-02-17 10:40:53 -08009949 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -08009950 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009951 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009952 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009953 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -02009954 int htot = I915_READ(HTOTAL(cpu_transcoder));
9955 int hsync = I915_READ(HSYNC(cpu_transcoder));
9956 int vtot = I915_READ(VTOTAL(cpu_transcoder));
9957 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +03009958 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08009959
9960 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
9961 if (!mode)
9962 return NULL;
9963
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009964 /*
9965 * Construct a pipe_config sufficient for getting the clock info
9966 * back out of crtc_clock_get.
9967 *
9968 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
9969 * to use a real value here instead.
9970 */
Ville Syrjälä293623f2013-09-13 16:18:46 +03009971 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009972 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009973 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
9974 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
9975 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009976 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
9977
Ville Syrjälä773ae032013-09-23 17:48:20 +03009978 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -08009979 mode->hdisplay = (htot & 0xffff) + 1;
9980 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
9981 mode->hsync_start = (hsync & 0xffff) + 1;
9982 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
9983 mode->vdisplay = (vtot & 0xffff) + 1;
9984 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
9985 mode->vsync_start = (vsync & 0xffff) + 1;
9986 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
9987
9988 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -08009989
9990 return mode;
9991}
9992
Jesse Barnes652c3932009-08-17 13:31:43 -07009993static void intel_decrease_pllclock(struct drm_crtc *crtc)
9994{
9995 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +03009996 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -07009997 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -07009998
Sonika Jindalbaff2962014-07-22 11:16:35 +05309999 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010000 return;
10001
10002 if (!dev_priv->lvds_downclock_avail)
10003 return;
10004
10005 /*
10006 * Since this is called by a timer, we should never get here in
10007 * the manual case.
10008 */
10009 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010010 int pipe = intel_crtc->pipe;
10011 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010012 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010013
Zhao Yakui44d98a62009-10-09 11:39:40 +080010014 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010015
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010016 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010017
Chris Wilson074b5e12012-05-02 12:07:06 +010010018 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010019 dpll |= DISPLAY_RATE_SELECT_FPA1;
10020 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010021 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010022 dpll = I915_READ(dpll_reg);
10023 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010024 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010025 }
10026
10027}
10028
Chris Wilsonf047e392012-07-21 12:31:41 +010010029void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010030{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010031 struct drm_i915_private *dev_priv = dev->dev_private;
10032
Chris Wilsonf62a0072014-02-21 17:55:39 +000010033 if (dev_priv->mm.busy)
10034 return;
10035
Paulo Zanoni43694d62014-03-07 20:08:08 -030010036 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010037 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010038 if (INTEL_INFO(dev)->gen >= 6)
10039 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010040 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010041}
10042
10043void intel_mark_idle(struct drm_device *dev)
10044{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010045 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010046 struct drm_crtc *crtc;
10047
Chris Wilsonf62a0072014-02-21 17:55:39 +000010048 if (!dev_priv->mm.busy)
10049 return;
10050
10051 dev_priv->mm.busy = false;
10052
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010053 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010054 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010055 continue;
10056
10057 intel_decrease_pllclock(crtc);
10058 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010059
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010060 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010061 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010062
Paulo Zanoni43694d62014-03-07 20:08:08 -030010063 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010064}
10065
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010066static void intel_crtc_set_state(struct intel_crtc *crtc,
10067 struct intel_crtc_state *crtc_state)
10068{
10069 kfree(crtc->config);
10070 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +020010071 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010072}
10073
Jesse Barnes79e53942008-11-07 14:24:08 -080010074static void intel_crtc_destroy(struct drm_crtc *crtc)
10075{
10076 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010077 struct drm_device *dev = crtc->dev;
10078 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010079
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010080 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010081 work = intel_crtc->unpin_work;
10082 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010083 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010084
10085 if (work) {
10086 cancel_work_sync(&work->work);
10087 kfree(work);
10088 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010089
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010090 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010091 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010092
Jesse Barnes79e53942008-11-07 14:24:08 -080010093 kfree(intel_crtc);
10094}
10095
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010096static void intel_unpin_work_fn(struct work_struct *__work)
10097{
10098 struct intel_unpin_work *work =
10099 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010100 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010101 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010102
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010103 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010104 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010105 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010106
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010107 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010108
10109 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010110 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010111 mutex_unlock(&dev->struct_mutex);
10112
Daniel Vetterf99d7062014-06-19 16:01:59 +020010113 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010114 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010115
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010116 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10117 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10118
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010119 kfree(work);
10120}
10121
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010122static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010123 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010124{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10126 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010127 unsigned long flags;
10128
10129 /* Ignore early vblank irqs */
10130 if (intel_crtc == NULL)
10131 return;
10132
Daniel Vetterf3260382014-09-15 14:55:23 +020010133 /*
10134 * This is called both by irq handlers and the reset code (to complete
10135 * lost pageflips) so needs the full irqsave spinlocks.
10136 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010137 spin_lock_irqsave(&dev->event_lock, flags);
10138 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010139
10140 /* Ensure we don't miss a work->pending update ... */
10141 smp_rmb();
10142
10143 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010144 spin_unlock_irqrestore(&dev->event_lock, flags);
10145 return;
10146 }
10147
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010148 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010149
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010150 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010151}
10152
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010153void intel_finish_page_flip(struct drm_device *dev, int pipe)
10154{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010155 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010156 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10157
Mario Kleiner49b14a52010-12-09 07:00:07 +010010158 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010159}
10160
10161void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10162{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010163 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010164 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10165
Mario Kleiner49b14a52010-12-09 07:00:07 +010010166 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010167}
10168
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010169/* Is 'a' after or equal to 'b'? */
10170static bool g4x_flip_count_after_eq(u32 a, u32 b)
10171{
10172 return !((a - b) & 0x80000000);
10173}
10174
10175static bool page_flip_finished(struct intel_crtc *crtc)
10176{
10177 struct drm_device *dev = crtc->base.dev;
10178 struct drm_i915_private *dev_priv = dev->dev_private;
10179
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010180 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10181 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10182 return true;
10183
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010184 /*
10185 * The relevant registers doen't exist on pre-ctg.
10186 * As the flip done interrupt doesn't trigger for mmio
10187 * flips on gmch platforms, a flip count check isn't
10188 * really needed there. But since ctg has the registers,
10189 * include it in the check anyway.
10190 */
10191 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10192 return true;
10193
10194 /*
10195 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10196 * used the same base address. In that case the mmio flip might
10197 * have completed, but the CS hasn't even executed the flip yet.
10198 *
10199 * A flip count check isn't enough as the CS might have updated
10200 * the base address just after start of vblank, but before we
10201 * managed to process the interrupt. This means we'd complete the
10202 * CS flip too soon.
10203 *
10204 * Combining both checks should get us a good enough result. It may
10205 * still happen that the CS flip has been executed, but has not
10206 * yet actually completed. But in case the base address is the same
10207 * anyway, we don't really care.
10208 */
10209 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10210 crtc->unpin_work->gtt_offset &&
10211 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10212 crtc->unpin_work->flip_count);
10213}
10214
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010215void intel_prepare_page_flip(struct drm_device *dev, int plane)
10216{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010217 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010218 struct intel_crtc *intel_crtc =
10219 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10220 unsigned long flags;
10221
Daniel Vetterf3260382014-09-15 14:55:23 +020010222
10223 /*
10224 * This is called both by irq handlers and the reset code (to complete
10225 * lost pageflips) so needs the full irqsave spinlocks.
10226 *
10227 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010228 * generate a page-flip completion irq, i.e. every modeset
10229 * is also accompanied by a spurious intel_prepare_page_flip().
10230 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010231 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010232 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010233 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010234 spin_unlock_irqrestore(&dev->event_lock, flags);
10235}
10236
Robin Schroereba905b2014-05-18 02:24:50 +020010237static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010238{
10239 /* Ensure that the work item is consistent when activating it ... */
10240 smp_wmb();
10241 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10242 /* and that it is marked active as soon as the irq could fire. */
10243 smp_wmb();
10244}
10245
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010246static int intel_gen2_queue_flip(struct drm_device *dev,
10247 struct drm_crtc *crtc,
10248 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010249 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010250 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010251 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010252{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010254 u32 flip_mask;
10255 int ret;
10256
Daniel Vetter6d90c952012-04-26 23:28:05 +020010257 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010258 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010259 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010260
10261 /* Can't queue multiple flips, so wait for the previous
10262 * one to finish before executing the next.
10263 */
10264 if (intel_crtc->plane)
10265 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10266 else
10267 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010268 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10269 intel_ring_emit(ring, MI_NOOP);
10270 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10271 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10272 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010273 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010274 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010275
10276 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010277 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010278 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010279}
10280
10281static int intel_gen3_queue_flip(struct drm_device *dev,
10282 struct drm_crtc *crtc,
10283 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010284 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010285 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010286 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010287{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010289 u32 flip_mask;
10290 int ret;
10291
Daniel Vetter6d90c952012-04-26 23:28:05 +020010292 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010293 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010294 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010295
10296 if (intel_crtc->plane)
10297 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10298 else
10299 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010300 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10301 intel_ring_emit(ring, MI_NOOP);
10302 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10303 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10304 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010305 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010306 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010307
Chris Wilsone7d841c2012-12-03 11:36:30 +000010308 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010309 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010310 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010311}
10312
10313static int intel_gen4_queue_flip(struct drm_device *dev,
10314 struct drm_crtc *crtc,
10315 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010316 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010317 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010318 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010319{
10320 struct drm_i915_private *dev_priv = dev->dev_private;
10321 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10322 uint32_t pf, pipesrc;
10323 int ret;
10324
Daniel Vetter6d90c952012-04-26 23:28:05 +020010325 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010326 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010327 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010328
10329 /* i965+ uses the linear or tiled offsets from the
10330 * Display Registers (which do not change across a page-flip)
10331 * so we need only reprogram the base address.
10332 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010333 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10334 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10335 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010336 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010337 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010338
10339 /* XXX Enabling the panel-fitter across page-flip is so far
10340 * untested on non-native modes, so ignore it for now.
10341 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10342 */
10343 pf = 0;
10344 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010345 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010346
10347 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010348 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010349 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010350}
10351
10352static int intel_gen6_queue_flip(struct drm_device *dev,
10353 struct drm_crtc *crtc,
10354 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010355 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010356 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010357 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010358{
10359 struct drm_i915_private *dev_priv = dev->dev_private;
10360 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10361 uint32_t pf, pipesrc;
10362 int ret;
10363
Daniel Vetter6d90c952012-04-26 23:28:05 +020010364 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010365 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010366 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010367
Daniel Vetter6d90c952012-04-26 23:28:05 +020010368 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10369 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10370 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010371 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010372
Chris Wilson99d9acd2012-04-17 20:37:00 +010010373 /* Contrary to the suggestions in the documentation,
10374 * "Enable Panel Fitter" does not seem to be required when page
10375 * flipping with a non-native mode, and worse causes a normal
10376 * modeset to fail.
10377 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10378 */
10379 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010380 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010381 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010382
10383 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010384 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010385 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010386}
10387
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010388static int intel_gen7_queue_flip(struct drm_device *dev,
10389 struct drm_crtc *crtc,
10390 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010391 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010392 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010393 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010394{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010396 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010397 int len, ret;
10398
Robin Schroereba905b2014-05-18 02:24:50 +020010399 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010400 case PLANE_A:
10401 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10402 break;
10403 case PLANE_B:
10404 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10405 break;
10406 case PLANE_C:
10407 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10408 break;
10409 default:
10410 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010411 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010412 }
10413
Chris Wilsonffe74d72013-08-26 20:58:12 +010010414 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010415 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010416 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010417 /*
10418 * On Gen 8, SRM is now taking an extra dword to accommodate
10419 * 48bits addresses, and we need a NOOP for the batch size to
10420 * stay even.
10421 */
10422 if (IS_GEN8(dev))
10423 len += 2;
10424 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010425
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010426 /*
10427 * BSpec MI_DISPLAY_FLIP for IVB:
10428 * "The full packet must be contained within the same cache line."
10429 *
10430 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10431 * cacheline, if we ever start emitting more commands before
10432 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10433 * then do the cacheline alignment, and finally emit the
10434 * MI_DISPLAY_FLIP.
10435 */
10436 ret = intel_ring_cacheline_align(ring);
10437 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010438 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010439
Chris Wilsonffe74d72013-08-26 20:58:12 +010010440 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010441 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010442 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010443
Chris Wilsonffe74d72013-08-26 20:58:12 +010010444 /* Unmask the flip-done completion message. Note that the bspec says that
10445 * we should do this for both the BCS and RCS, and that we must not unmask
10446 * more than one flip event at any time (or ensure that one flip message
10447 * can be sent by waiting for flip-done prior to queueing new flips).
10448 * Experimentation says that BCS works despite DERRMR masking all
10449 * flip-done completion events and that unmasking all planes at once
10450 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10451 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10452 */
10453 if (ring->id == RCS) {
10454 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10455 intel_ring_emit(ring, DERRMR);
10456 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10457 DERRMR_PIPEB_PRI_FLIP_DONE |
10458 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010459 if (IS_GEN8(dev))
10460 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10461 MI_SRM_LRM_GLOBAL_GTT);
10462 else
10463 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10464 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010465 intel_ring_emit(ring, DERRMR);
10466 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010467 if (IS_GEN8(dev)) {
10468 intel_ring_emit(ring, 0);
10469 intel_ring_emit(ring, MI_NOOP);
10470 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010471 }
10472
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010473 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010474 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010475 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010476 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010477
10478 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010479 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010480 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010481}
10482
Sourab Gupta84c33a62014-06-02 16:47:17 +053010483static bool use_mmio_flip(struct intel_engine_cs *ring,
10484 struct drm_i915_gem_object *obj)
10485{
10486 /*
10487 * This is not being used for older platforms, because
10488 * non-availability of flip done interrupt forces us to use
10489 * CS flips. Older platforms derive flip done using some clever
10490 * tricks involving the flip_pending status bits and vblank irqs.
10491 * So using MMIO flips there would disrupt this mechanism.
10492 */
10493
Chris Wilson8e09bf82014-07-08 10:40:30 +010010494 if (ring == NULL)
10495 return true;
10496
Sourab Gupta84c33a62014-06-02 16:47:17 +053010497 if (INTEL_INFO(ring->dev)->gen < 5)
10498 return false;
10499
10500 if (i915.use_mmio_flip < 0)
10501 return false;
10502 else if (i915.use_mmio_flip > 0)
10503 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010504 else if (i915.enable_execlists)
10505 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010506 else
John Harrison41c52412014-11-24 18:49:43 +000010507 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010508}
10509
Damien Lespiauff944562014-11-20 14:58:16 +000010510static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10511{
10512 struct drm_device *dev = intel_crtc->base.dev;
10513 struct drm_i915_private *dev_priv = dev->dev_private;
10514 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
10515 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
10516 struct drm_i915_gem_object *obj = intel_fb->obj;
10517 const enum pipe pipe = intel_crtc->pipe;
10518 u32 ctl, stride;
10519
10520 ctl = I915_READ(PLANE_CTL(pipe, 0));
10521 ctl &= ~PLANE_CTL_TILED_MASK;
10522 if (obj->tiling_mode == I915_TILING_X)
10523 ctl |= PLANE_CTL_TILED_X;
10524
10525 /*
10526 * The stride is either expressed as a multiple of 64 bytes chunks for
10527 * linear buffers or in number of tiles for tiled buffers.
10528 */
10529 stride = fb->pitches[0] >> 6;
10530 if (obj->tiling_mode == I915_TILING_X)
10531 stride = fb->pitches[0] >> 9; /* X tiles are 512 bytes wide */
10532
10533 /*
10534 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10535 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10536 */
10537 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10538 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10539
10540 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10541 POSTING_READ(PLANE_SURF(pipe, 0));
10542}
10543
10544static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010545{
10546 struct drm_device *dev = intel_crtc->base.dev;
10547 struct drm_i915_private *dev_priv = dev->dev_private;
10548 struct intel_framebuffer *intel_fb =
10549 to_intel_framebuffer(intel_crtc->base.primary->fb);
10550 struct drm_i915_gem_object *obj = intel_fb->obj;
10551 u32 dspcntr;
10552 u32 reg;
10553
Sourab Gupta84c33a62014-06-02 16:47:17 +053010554 reg = DSPCNTR(intel_crtc->plane);
10555 dspcntr = I915_READ(reg);
10556
Damien Lespiauc5d97472014-10-25 00:11:11 +010010557 if (obj->tiling_mode != I915_TILING_NONE)
10558 dspcntr |= DISPPLANE_TILED;
10559 else
10560 dspcntr &= ~DISPPLANE_TILED;
10561
Sourab Gupta84c33a62014-06-02 16:47:17 +053010562 I915_WRITE(reg, dspcntr);
10563
10564 I915_WRITE(DSPSURF(intel_crtc->plane),
10565 intel_crtc->unpin_work->gtt_offset);
10566 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010567
Damien Lespiauff944562014-11-20 14:58:16 +000010568}
10569
10570/*
10571 * XXX: This is the temporary way to update the plane registers until we get
10572 * around to using the usual plane update functions for MMIO flips
10573 */
10574static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10575{
10576 struct drm_device *dev = intel_crtc->base.dev;
10577 bool atomic_update;
10578 u32 start_vbl_count;
10579
10580 intel_mark_page_flip_active(intel_crtc);
10581
10582 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10583
10584 if (INTEL_INFO(dev)->gen >= 9)
10585 skl_do_mmio_flip(intel_crtc);
10586 else
10587 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10588 ilk_do_mmio_flip(intel_crtc);
10589
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010590 if (atomic_update)
10591 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010592}
10593
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010594static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010595{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010596 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010597 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010598 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010599
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010600 mmio_flip = &crtc->mmio_flip;
10601 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010602 WARN_ON(__i915_wait_request(mmio_flip->req,
10603 crtc->reset_counter,
10604 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010605
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010606 intel_do_mmio_flip(crtc);
10607 if (mmio_flip->req) {
10608 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010609 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010610 mutex_unlock(&crtc->base.dev->struct_mutex);
10611 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010612}
10613
10614static int intel_queue_mmio_flip(struct drm_device *dev,
10615 struct drm_crtc *crtc,
10616 struct drm_framebuffer *fb,
10617 struct drm_i915_gem_object *obj,
10618 struct intel_engine_cs *ring,
10619 uint32_t flags)
10620{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010621 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010622
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010623 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10624 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010625
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010626 schedule_work(&intel_crtc->mmio_flip.work);
10627
Sourab Gupta84c33a62014-06-02 16:47:17 +053010628 return 0;
10629}
10630
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010631static int intel_default_queue_flip(struct drm_device *dev,
10632 struct drm_crtc *crtc,
10633 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010634 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010635 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010636 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010637{
10638 return -ENODEV;
10639}
10640
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010641static bool __intel_pageflip_stall_check(struct drm_device *dev,
10642 struct drm_crtc *crtc)
10643{
10644 struct drm_i915_private *dev_priv = dev->dev_private;
10645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10646 struct intel_unpin_work *work = intel_crtc->unpin_work;
10647 u32 addr;
10648
10649 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10650 return true;
10651
10652 if (!work->enable_stall_check)
10653 return false;
10654
10655 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010656 if (work->flip_queued_req &&
10657 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010658 return false;
10659
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010660 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010661 }
10662
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010663 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010664 return false;
10665
10666 /* Potential stall - if we see that the flip has happened,
10667 * assume a missed interrupt. */
10668 if (INTEL_INFO(dev)->gen >= 4)
10669 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10670 else
10671 addr = I915_READ(DSPADDR(intel_crtc->plane));
10672
10673 /* There is a potential issue here with a false positive after a flip
10674 * to the same address. We could address this by checking for a
10675 * non-incrementing frame counter.
10676 */
10677 return addr == work->gtt_offset;
10678}
10679
10680void intel_check_page_flip(struct drm_device *dev, int pipe)
10681{
10682 struct drm_i915_private *dev_priv = dev->dev_private;
10683 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10684 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010685 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010686
Dave Gordon6c51d462015-03-06 15:34:26 +000010687 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010688
10689 if (crtc == NULL)
10690 return;
10691
Daniel Vetterf3260382014-09-15 14:55:23 +020010692 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010693 work = intel_crtc->unpin_work;
10694 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010695 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010696 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010697 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010698 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010699 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010700 if (work != NULL &&
10701 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10702 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010703 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010704}
10705
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010706static int intel_crtc_page_flip(struct drm_crtc *crtc,
10707 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010708 struct drm_pending_vblank_event *event,
10709 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010710{
10711 struct drm_device *dev = crtc->dev;
10712 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010713 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010714 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010715 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010716 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010717 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010718 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010719 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010720 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010721 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010722
Matt Roper2ff8fde2014-07-08 07:50:07 -070010723 /*
10724 * drm_mode_page_flip_ioctl() should already catch this, but double
10725 * check to be safe. In the future we may enable pageflipping from
10726 * a disabled primary plane.
10727 */
10728 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10729 return -EBUSY;
10730
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010731 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010732 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010733 return -EINVAL;
10734
10735 /*
10736 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10737 * Note that pitch changes could also affect these register.
10738 */
10739 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010740 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10741 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010742 return -EINVAL;
10743
Chris Wilsonf900db42014-02-20 09:26:13 +000010744 if (i915_terminally_wedged(&dev_priv->gpu_error))
10745 goto out_hang;
10746
Daniel Vetterb14c5672013-09-19 12:18:32 +020010747 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010748 if (work == NULL)
10749 return -ENOMEM;
10750
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010751 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010752 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010753 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010754 INIT_WORK(&work->work, intel_unpin_work_fn);
10755
Daniel Vetter87b6b102014-05-15 15:33:46 +020010756 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010757 if (ret)
10758 goto free_work;
10759
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010760 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010761 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010762 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010763 /* Before declaring the flip queue wedged, check if
10764 * the hardware completed the operation behind our backs.
10765 */
10766 if (__intel_pageflip_stall_check(dev, crtc)) {
10767 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10768 page_flip_completed(intel_crtc);
10769 } else {
10770 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010771 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010772
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010773 drm_crtc_vblank_put(crtc);
10774 kfree(work);
10775 return -EBUSY;
10776 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010777 }
10778 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010779 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010780
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010781 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10782 flush_workqueue(dev_priv->wq);
10783
Jesse Barnes75dfca82010-02-10 15:09:44 -080010784 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010785 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010786 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010787
Matt Roperf4510a22014-04-01 15:22:40 -070010788 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010789 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010790
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010791 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010792
Chris Wilson89ed88b2015-02-16 14:31:49 +000010793 ret = i915_mutex_lock_interruptible(dev);
10794 if (ret)
10795 goto cleanup;
10796
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010797 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010798 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010799
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010800 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010801 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010802
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010803 if (IS_VALLEYVIEW(dev)) {
10804 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010805 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010806 /* vlv: DISPLAY_FLIP fails to change tiling */
10807 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010808 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010809 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010810 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010811 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010812 if (ring == NULL || ring->id != RCS)
10813 ring = &dev_priv->ring[BCS];
10814 } else {
10815 ring = &dev_priv->ring[RCS];
10816 }
10817
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010818 mmio_flip = use_mmio_flip(ring, obj);
10819
10820 /* When using CS flips, we want to emit semaphores between rings.
10821 * However, when using mmio flips we will create a task to do the
10822 * synchronisation, so all we want here is to pin the framebuffer
10823 * into the display plane and skip any waits.
10824 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010825 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010826 crtc->primary->state,
10827 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010828 if (ret)
10829 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010830
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010831 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10832 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010833
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010834 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010835 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10836 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010837 if (ret)
10838 goto cleanup_unpin;
10839
John Harrisonf06cc1b2014-11-24 18:49:37 +000010840 i915_gem_request_assign(&work->flip_queued_req,
10841 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010842 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010843 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010844 page_flip_flags);
10845 if (ret)
10846 goto cleanup_unpin;
10847
John Harrisonf06cc1b2014-11-24 18:49:37 +000010848 i915_gem_request_assign(&work->flip_queued_req,
10849 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010850 }
10851
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010852 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010853 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010854
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010855 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010856 INTEL_FRONTBUFFER_PRIMARY(pipe));
10857
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010858 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010859 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010860 mutex_unlock(&dev->struct_mutex);
10861
Jesse Barnese5510fa2010-07-01 16:48:37 -070010862 trace_i915_flip_request(intel_crtc->plane, obj);
10863
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010864 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010865
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010866cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010867 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010868cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010869 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010870 mutex_unlock(&dev->struct_mutex);
10871cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010872 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010873 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010874
Chris Wilson89ed88b2015-02-16 14:31:49 +000010875 drm_gem_object_unreference_unlocked(&obj->base);
10876 drm_framebuffer_unreference(work->old_fb);
10877
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010878 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010879 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010880 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010881
Daniel Vetter87b6b102014-05-15 15:33:46 +020010882 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010883free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010884 kfree(work);
10885
Chris Wilsonf900db42014-02-20 09:26:13 +000010886 if (ret == -EIO) {
10887out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010888 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010889 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010890 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010891 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010892 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010893 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010894 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010895 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010896}
10897
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010898static struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010899 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10900 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010901 .atomic_begin = intel_begin_crtc_commit,
10902 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010903};
10904
Daniel Vetter9a935852012-07-05 22:34:27 +020010905/**
10906 * intel_modeset_update_staged_output_state
10907 *
10908 * Updates the staged output configuration state, e.g. after we've read out the
10909 * current hw state.
10910 */
10911static void intel_modeset_update_staged_output_state(struct drm_device *dev)
10912{
Ville Syrjälä76688512014-01-10 11:28:06 +020010913 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010914 struct intel_encoder *encoder;
10915 struct intel_connector *connector;
10916
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010917 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010918 connector->new_encoder =
10919 to_intel_encoder(connector->base.encoder);
10920 }
10921
Damien Lespiaub2784e12014-08-05 11:29:37 +010010922 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010923 encoder->new_crtc =
10924 to_intel_crtc(encoder->base.crtc);
10925 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010926
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010927 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010928 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020010929 }
Daniel Vetter9a935852012-07-05 22:34:27 +020010930}
10931
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010932/* Transitional helper to copy current connector/encoder state to
10933 * connector->state. This is needed so that code that is partially
10934 * converted to atomic does the right thing.
10935 */
10936static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
10937{
10938 struct intel_connector *connector;
10939
10940 for_each_intel_connector(dev, connector) {
10941 if (connector->base.encoder) {
10942 connector->base.state->best_encoder =
10943 connector->base.encoder;
10944 connector->base.state->crtc =
10945 connector->base.encoder->crtc;
10946 } else {
10947 connector->base.state->best_encoder = NULL;
10948 connector->base.state->crtc = NULL;
10949 }
10950 }
10951}
10952
Daniel Vetter9a935852012-07-05 22:34:27 +020010953/**
10954 * intel_modeset_commit_output_state
10955 *
10956 * This function copies the stage display pipe configuration to the real one.
10957 */
10958static void intel_modeset_commit_output_state(struct drm_device *dev)
10959{
Ville Syrjälä76688512014-01-10 11:28:06 +020010960 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020010961 struct intel_encoder *encoder;
10962 struct intel_connector *connector;
10963
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020010964 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010965 connector->base.encoder = &connector->new_encoder->base;
10966 }
10967
Damien Lespiaub2784e12014-08-05 11:29:37 +010010968 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020010969 encoder->base.crtc = &encoder->new_crtc->base;
10970 }
Ville Syrjälä76688512014-01-10 11:28:06 +020010971
Damien Lespiaud3fcc802014-05-13 23:32:22 +010010972 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080010973 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020010974 crtc->base.enabled = crtc->new_enabled;
10975 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020010976
10977 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020010978}
10979
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010980static void
Robin Schroereba905b2014-05-18 02:24:50 +020010981connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010982 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010010983{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010984 int bpp = pipe_config->pipe_bpp;
10985
10986 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
10987 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030010988 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020010989
10990 /* Don't use an invalid EDID bpc value */
10991 if (connector->base.display_info.bpc &&
10992 connector->base.display_info.bpc * 3 < bpp) {
10993 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
10994 bpp, connector->base.display_info.bpc*3);
10995 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
10996 }
10997
10998 /* Clamp bpp to 8 on screens without EDID 1.4 */
10999 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11000 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11001 bpp);
11002 pipe_config->pipe_bpp = 24;
11003 }
11004}
11005
11006static int
11007compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011008 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011009{
11010 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011011 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011012 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011013 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011014
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011015 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011016 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011017 else if (INTEL_INFO(dev)->gen >= 5)
11018 bpp = 12*3;
11019 else
11020 bpp = 8*3;
11021
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011022
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011023 pipe_config->pipe_bpp = bpp;
11024
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011025 state = pipe_config->base.state;
11026
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011027 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011028 for (i = 0; i < state->num_connector; i++) {
11029 if (!state->connectors[i])
11030 continue;
11031
11032 connector = to_intel_connector(state->connectors[i]);
11033 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011034 continue;
11035
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011036 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011037 }
11038
11039 return bpp;
11040}
11041
Daniel Vetter644db712013-09-19 14:53:58 +020011042static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11043{
11044 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11045 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011046 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011047 mode->crtc_hdisplay, mode->crtc_hsync_start,
11048 mode->crtc_hsync_end, mode->crtc_htotal,
11049 mode->crtc_vdisplay, mode->crtc_vsync_start,
11050 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11051}
11052
Daniel Vetterc0b03412013-05-28 12:05:54 +020011053static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011054 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011055 const char *context)
11056{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011057 struct drm_device *dev = crtc->base.dev;
11058 struct drm_plane *plane;
11059 struct intel_plane *intel_plane;
11060 struct intel_plane_state *state;
11061 struct drm_framebuffer *fb;
11062
11063 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11064 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011065
11066 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11067 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11068 pipe_config->pipe_bpp, pipe_config->dither);
11069 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11070 pipe_config->has_pch_encoder,
11071 pipe_config->fdi_lanes,
11072 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11073 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11074 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011075 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11076 pipe_config->has_dp_encoder,
11077 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11078 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11079 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011080
11081 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11082 pipe_config->has_dp_encoder,
11083 pipe_config->dp_m2_n2.gmch_m,
11084 pipe_config->dp_m2_n2.gmch_n,
11085 pipe_config->dp_m2_n2.link_m,
11086 pipe_config->dp_m2_n2.link_n,
11087 pipe_config->dp_m2_n2.tu);
11088
Daniel Vetter55072d12014-11-20 16:10:28 +010011089 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11090 pipe_config->has_audio,
11091 pipe_config->has_infoframe);
11092
Daniel Vetterc0b03412013-05-28 12:05:54 +020011093 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011094 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011095 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011096 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11097 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011098 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011099 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11100 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011101 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11102 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11103 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011104 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11105 pipe_config->gmch_pfit.control,
11106 pipe_config->gmch_pfit.pgm_ratios,
11107 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011108 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011109 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011110 pipe_config->pch_pfit.size,
11111 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011112 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011113 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011114
11115 DRM_DEBUG_KMS("planes on this crtc\n");
11116 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11117 intel_plane = to_intel_plane(plane);
11118 if (intel_plane->pipe != crtc->pipe)
11119 continue;
11120
11121 state = to_intel_plane_state(plane->state);
11122 fb = state->base.fb;
11123 if (!fb) {
11124 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11125 "disabled, scaler_id = %d\n",
11126 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11127 plane->base.id, intel_plane->pipe,
11128 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11129 drm_plane_index(plane), state->scaler_id);
11130 continue;
11131 }
11132
11133 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11134 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11135 plane->base.id, intel_plane->pipe,
11136 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11137 drm_plane_index(plane));
11138 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11139 fb->base.id, fb->width, fb->height, fb->pixel_format);
11140 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11141 state->scaler_id,
11142 state->src.x1 >> 16, state->src.y1 >> 16,
11143 drm_rect_width(&state->src) >> 16,
11144 drm_rect_height(&state->src) >> 16,
11145 state->dst.x1, state->dst.y1,
11146 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11147 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011148}
11149
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011150static bool encoders_cloneable(const struct intel_encoder *a,
11151 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011152{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011153 /* masks could be asymmetric, so check both ways */
11154 return a == b || (a->cloneable & (1 << b->type) &&
11155 b->cloneable & (1 << a->type));
11156}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011157
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011158static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11159 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011160 struct intel_encoder *encoder)
11161{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011162 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011163 struct drm_connector_state *connector_state;
11164 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011165
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011166 for (i = 0; i < state->num_connector; i++) {
11167 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011168 continue;
11169
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011170 connector_state = state->connector_states[i];
11171 if (connector_state->crtc != &crtc->base)
11172 continue;
11173
11174 source_encoder =
11175 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011176 if (!encoders_cloneable(encoder, source_encoder))
11177 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011178 }
11179
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011180 return true;
11181}
11182
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011183static bool check_encoder_cloning(struct drm_atomic_state *state,
11184 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011185{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011186 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011187 struct drm_connector_state *connector_state;
11188 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011189
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011190 for (i = 0; i < state->num_connector; i++) {
11191 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011192 continue;
11193
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011194 connector_state = state->connector_states[i];
11195 if (connector_state->crtc != &crtc->base)
11196 continue;
11197
11198 encoder = to_intel_encoder(connector_state->best_encoder);
11199 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011200 return false;
11201 }
11202
11203 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011204}
11205
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011206static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011207{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011208 struct drm_device *dev = state->dev;
11209 struct intel_encoder *encoder;
11210 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011211 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011212 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011213
11214 /*
11215 * Walk the connector list instead of the encoder
11216 * list to detect the problem on ddi platforms
11217 * where there's just one encoder per digital port.
11218 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011219 for (i = 0; i < state->num_connector; i++) {
11220 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011221 continue;
11222
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011223 connector_state = state->connector_states[i];
11224 if (!connector_state->best_encoder)
11225 continue;
11226
11227 encoder = to_intel_encoder(connector_state->best_encoder);
11228
11229 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011230
11231 switch (encoder->type) {
11232 unsigned int port_mask;
11233 case INTEL_OUTPUT_UNKNOWN:
11234 if (WARN_ON(!HAS_DDI(dev)))
11235 break;
11236 case INTEL_OUTPUT_DISPLAYPORT:
11237 case INTEL_OUTPUT_HDMI:
11238 case INTEL_OUTPUT_EDP:
11239 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11240
11241 /* the same port mustn't appear more than once */
11242 if (used_ports & port_mask)
11243 return false;
11244
11245 used_ports |= port_mask;
11246 default:
11247 break;
11248 }
11249 }
11250
11251 return true;
11252}
11253
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011254static void
11255clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11256{
11257 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011258 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011259
Chandra Konduru663a3642015-04-07 15:28:41 -070011260 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011261 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011262 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011263 memset(crtc_state, 0, sizeof *crtc_state);
11264 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011265 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011266}
11267
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011268static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011269intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011270 struct drm_display_mode *mode,
11271 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020011272{
Daniel Vetter7758a112012-07-08 19:40:39 +020011273 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011274 struct intel_connector *connector;
11275 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011276 struct intel_crtc_state *pipe_config;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011277 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011278 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011279 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011280
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011281 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011282 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11283 return ERR_PTR(-EINVAL);
11284 }
11285
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011286 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011287 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11288 return ERR_PTR(-EINVAL);
11289 }
11290
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011291 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11292 if (IS_ERR(pipe_config))
11293 return pipe_config;
11294
11295 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011296
Matt Roper07878242015-02-25 11:43:26 -080011297 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011298 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11299 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011300
Daniel Vettere143a212013-07-04 12:01:15 +020011301 pipe_config->cpu_transcoder =
11302 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011303 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011304
Imre Deak2960bc92013-07-30 13:36:32 +030011305 /*
11306 * Sanitize sync polarity flags based on requested ones. If neither
11307 * positive or negative polarity is requested, treat this as meaning
11308 * negative polarity.
11309 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011310 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011311 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011312 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011313
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011314 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011315 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011316 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011317
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011318 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11319 * plane pixel format and any sink constraints into account. Returns the
11320 * source plane bpp so that dithering can be selected on mismatches
11321 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011322 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11323 pipe_config);
11324 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011325 goto fail;
11326
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011327 /*
11328 * Determine the real pipe dimensions. Note that stereo modes can
11329 * increase the actual pipe size due to the frame doubling and
11330 * insertion of additional space for blanks between the frame. This
11331 * is stored in the crtc timings. We use the requested mode to do this
11332 * computation to clearly distinguish it from the adjusted mode, which
11333 * can be changed by the connectors in the below retry loop.
11334 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011335 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011336 &pipe_config->pipe_src_w,
11337 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011338
Daniel Vettere29c22c2013-02-21 00:00:16 +010011339encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011340 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011341 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011342 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011343
Daniel Vetter135c81b2013-07-21 21:37:09 +020011344 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011345 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11346 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011347
Daniel Vetter7758a112012-07-08 19:40:39 +020011348 /* Pass our mode to the connectors and the CRTC to give them a chance to
11349 * adjust it according to limitations or connector properties, and also
11350 * a chance to reject the mode entirely.
11351 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011352 for (i = 0; i < state->num_connector; i++) {
11353 connector = to_intel_connector(state->connectors[i]);
11354 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020011355 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010011356
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011357 connector_state = state->connector_states[i];
11358 if (connector_state->crtc != crtc)
11359 continue;
11360
11361 encoder = to_intel_encoder(connector_state->best_encoder);
11362
Daniel Vetterefea6e82013-07-21 21:36:59 +020011363 if (!(encoder->compute_config(encoder, pipe_config))) {
11364 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011365 goto fail;
11366 }
11367 }
11368
Daniel Vetterff9a6752013-06-01 17:16:21 +020011369 /* Set default port clock if not overwritten by the encoder. Needs to be
11370 * done afterwards in case the encoder adjusts the mode. */
11371 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011372 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011373 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011374
Daniel Vettera43f6e02013-06-07 23:10:32 +020011375 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011376 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011377 DRM_DEBUG_KMS("CRTC fixup failed\n");
11378 goto fail;
11379 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011380
11381 if (ret == RETRY) {
11382 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11383 ret = -EINVAL;
11384 goto fail;
11385 }
11386
11387 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11388 retry = false;
11389 goto encoder_retry;
11390 }
11391
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011392 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011393 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011394 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011395
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011396 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020011397fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010011398 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020011399}
11400
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011401/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11402 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11403static void
11404intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11405 unsigned *prepare_pipes, unsigned *disable_pipes)
11406{
11407 struct intel_crtc *intel_crtc;
11408 struct drm_device *dev = crtc->dev;
11409 struct intel_encoder *encoder;
11410 struct intel_connector *connector;
11411 struct drm_crtc *tmp_crtc;
11412
11413 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11414
11415 /* Check which crtcs have changed outputs connected to them, these need
11416 * to be part of the prepare_pipes mask. We don't (yet) support global
11417 * modeset across multiple crtcs, so modeset_pipes will only have one
11418 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011419 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011420 if (connector->base.encoder == &connector->new_encoder->base)
11421 continue;
11422
11423 if (connector->base.encoder) {
11424 tmp_crtc = connector->base.encoder->crtc;
11425
11426 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11427 }
11428
11429 if (connector->new_encoder)
11430 *prepare_pipes |=
11431 1 << connector->new_encoder->new_crtc->pipe;
11432 }
11433
Damien Lespiaub2784e12014-08-05 11:29:37 +010011434 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011435 if (encoder->base.crtc == &encoder->new_crtc->base)
11436 continue;
11437
11438 if (encoder->base.crtc) {
11439 tmp_crtc = encoder->base.crtc;
11440
11441 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11442 }
11443
11444 if (encoder->new_crtc)
11445 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11446 }
11447
Ville Syrjälä76688512014-01-10 11:28:06 +020011448 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011449 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011450 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011451 continue;
11452
Ville Syrjälä76688512014-01-10 11:28:06 +020011453 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011454 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011455 else
11456 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011457 }
11458
11459
11460 /* set_mode is also used to update properties on life display pipes. */
11461 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011462 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011463 *prepare_pipes |= 1 << intel_crtc->pipe;
11464
Daniel Vetterb6c51642013-04-12 18:48:43 +020011465 /*
11466 * For simplicity do a full modeset on any pipe where the output routing
11467 * changed. We could be more clever, but that would require us to be
11468 * more careful with calling the relevant encoder->mode_set functions.
11469 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011470 if (*prepare_pipes)
11471 *modeset_pipes = *prepare_pipes;
11472
11473 /* ... and mask these out. */
11474 *modeset_pipes &= ~(*disable_pipes);
11475 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011476
11477 /*
11478 * HACK: We don't (yet) fully support global modesets. intel_set_config
11479 * obies this rule, but the modeset restore mode of
11480 * intel_modeset_setup_hw_state does not.
11481 */
11482 *modeset_pipes &= 1 << intel_crtc->pipe;
11483 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011484
11485 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11486 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011487}
11488
Daniel Vetterea9d7582012-07-10 10:42:52 +020011489static bool intel_crtc_in_use(struct drm_crtc *crtc)
11490{
11491 struct drm_encoder *encoder;
11492 struct drm_device *dev = crtc->dev;
11493
11494 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11495 if (encoder->crtc == crtc)
11496 return true;
11497
11498 return false;
11499}
11500
11501static void
11502intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11503{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011504 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011505 struct intel_encoder *intel_encoder;
11506 struct intel_crtc *intel_crtc;
11507 struct drm_connector *connector;
11508
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011509 intel_shared_dpll_commit(dev_priv);
11510
Damien Lespiaub2784e12014-08-05 11:29:37 +010011511 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011512 if (!intel_encoder->base.crtc)
11513 continue;
11514
11515 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11516
11517 if (prepare_pipes & (1 << intel_crtc->pipe))
11518 intel_encoder->connectors_active = false;
11519 }
11520
11521 intel_modeset_commit_output_state(dev);
11522
Ville Syrjälä76688512014-01-10 11:28:06 +020011523 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011524 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011525 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011526 }
11527
11528 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11529 if (!connector->encoder || !connector->encoder->crtc)
11530 continue;
11531
11532 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11533
11534 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011535 struct drm_property *dpms_property =
11536 dev->mode_config.dpms_property;
11537
Daniel Vetterea9d7582012-07-10 10:42:52 +020011538 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011539 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011540 dpms_property,
11541 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011542
11543 intel_encoder = to_intel_encoder(connector->encoder);
11544 intel_encoder->connectors_active = true;
11545 }
11546 }
11547
11548}
11549
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011550static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011551{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011552 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011553
11554 if (clock1 == clock2)
11555 return true;
11556
11557 if (!clock1 || !clock2)
11558 return false;
11559
11560 diff = abs(clock1 - clock2);
11561
11562 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11563 return true;
11564
11565 return false;
11566}
11567
Daniel Vetter25c5b262012-07-08 22:08:04 +020011568#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11569 list_for_each_entry((intel_crtc), \
11570 &(dev)->mode_config.crtc_list, \
11571 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011572 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011573
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011574static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011575intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011576 struct intel_crtc_state *current_config,
11577 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011578{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011579#define PIPE_CONF_CHECK_X(name) \
11580 if (current_config->name != pipe_config->name) { \
11581 DRM_ERROR("mismatch in " #name " " \
11582 "(expected 0x%08x, found 0x%08x)\n", \
11583 current_config->name, \
11584 pipe_config->name); \
11585 return false; \
11586 }
11587
Daniel Vetter08a24032013-04-19 11:25:34 +020011588#define PIPE_CONF_CHECK_I(name) \
11589 if (current_config->name != pipe_config->name) { \
11590 DRM_ERROR("mismatch in " #name " " \
11591 "(expected %i, found %i)\n", \
11592 current_config->name, \
11593 pipe_config->name); \
11594 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011595 }
11596
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011597/* This is required for BDW+ where there is only one set of registers for
11598 * switching between high and low RR.
11599 * This macro can be used whenever a comparison has to be made between one
11600 * hw state and multiple sw state variables.
11601 */
11602#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11603 if ((current_config->name != pipe_config->name) && \
11604 (current_config->alt_name != pipe_config->name)) { \
11605 DRM_ERROR("mismatch in " #name " " \
11606 "(expected %i or %i, found %i)\n", \
11607 current_config->name, \
11608 current_config->alt_name, \
11609 pipe_config->name); \
11610 return false; \
11611 }
11612
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011613#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11614 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011615 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011616 "(expected %i, found %i)\n", \
11617 current_config->name & (mask), \
11618 pipe_config->name & (mask)); \
11619 return false; \
11620 }
11621
Ville Syrjälä5e550652013-09-06 23:29:07 +030011622#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11623 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11624 DRM_ERROR("mismatch in " #name " " \
11625 "(expected %i, found %i)\n", \
11626 current_config->name, \
11627 pipe_config->name); \
11628 return false; \
11629 }
11630
Daniel Vetterbb760062013-06-06 14:55:52 +020011631#define PIPE_CONF_QUIRK(quirk) \
11632 ((current_config->quirks | pipe_config->quirks) & (quirk))
11633
Daniel Vettereccb1402013-05-22 00:50:22 +020011634 PIPE_CONF_CHECK_I(cpu_transcoder);
11635
Daniel Vetter08a24032013-04-19 11:25:34 +020011636 PIPE_CONF_CHECK_I(has_pch_encoder);
11637 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011638 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11639 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11640 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11641 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11642 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011643
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011644 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011645
11646 if (INTEL_INFO(dev)->gen < 8) {
11647 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11648 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11649 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11650 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11651 PIPE_CONF_CHECK_I(dp_m_n.tu);
11652
11653 if (current_config->has_drrs) {
11654 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11655 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11656 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11657 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11658 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11659 }
11660 } else {
11661 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11662 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11663 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11664 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11665 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11666 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011667
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011668 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11669 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11670 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11671 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11672 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11673 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011674
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011675 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11676 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11677 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11678 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11679 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11680 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011681
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011682 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011683 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011684 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11685 IS_VALLEYVIEW(dev))
11686 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011687 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011688
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011689 PIPE_CONF_CHECK_I(has_audio);
11690
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011691 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011692 DRM_MODE_FLAG_INTERLACE);
11693
Daniel Vetterbb760062013-06-06 14:55:52 +020011694 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011695 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011696 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011697 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011698 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011699 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011700 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011701 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011702 DRM_MODE_FLAG_NVSYNC);
11703 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011704
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011705 PIPE_CONF_CHECK_I(pipe_src_w);
11706 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011707
Daniel Vetter99535992014-04-13 12:00:33 +020011708 /*
11709 * FIXME: BIOS likes to set up a cloned config with lvds+external
11710 * screen. Since we don't yet re-compute the pipe config when moving
11711 * just the lvds port away to another pipe the sw tracking won't match.
11712 *
11713 * Proper atomic modesets with recomputed global state will fix this.
11714 * Until then just don't check gmch state for inherited modes.
11715 */
11716 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11717 PIPE_CONF_CHECK_I(gmch_pfit.control);
11718 /* pfit ratios are autocomputed by the hw on gen4+ */
11719 if (INTEL_INFO(dev)->gen < 4)
11720 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11721 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11722 }
11723
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011724 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11725 if (current_config->pch_pfit.enabled) {
11726 PIPE_CONF_CHECK_I(pch_pfit.pos);
11727 PIPE_CONF_CHECK_I(pch_pfit.size);
11728 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011729
Chandra Kondurua1b22782015-04-07 15:28:45 -070011730 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11731
Jesse Barnese59150d2014-01-07 13:30:45 -080011732 /* BDW+ don't expose a synchronous way to read the state */
11733 if (IS_HASWELL(dev))
11734 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011735
Ville Syrjälä282740f2013-09-04 18:30:03 +030011736 PIPE_CONF_CHECK_I(double_wide);
11737
Daniel Vetter26804af2014-06-25 22:01:55 +030011738 PIPE_CONF_CHECK_X(ddi_pll_sel);
11739
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011740 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011741 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011742 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011743 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11744 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011745 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011746 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11747 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11748 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011749
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011750 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11751 PIPE_CONF_CHECK_I(pipe_bpp);
11752
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011753 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011754 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011755
Daniel Vetter66e985c2013-06-05 13:34:20 +020011756#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011757#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011758#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011759#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011760#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011761#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011762
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011763 return true;
11764}
11765
Damien Lespiau08db6652014-11-04 17:06:52 +000011766static void check_wm_state(struct drm_device *dev)
11767{
11768 struct drm_i915_private *dev_priv = dev->dev_private;
11769 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11770 struct intel_crtc *intel_crtc;
11771 int plane;
11772
11773 if (INTEL_INFO(dev)->gen < 9)
11774 return;
11775
11776 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11777 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11778
11779 for_each_intel_crtc(dev, intel_crtc) {
11780 struct skl_ddb_entry *hw_entry, *sw_entry;
11781 const enum pipe pipe = intel_crtc->pipe;
11782
11783 if (!intel_crtc->active)
11784 continue;
11785
11786 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011787 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011788 hw_entry = &hw_ddb.plane[pipe][plane];
11789 sw_entry = &sw_ddb->plane[pipe][plane];
11790
11791 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11792 continue;
11793
11794 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11795 "(expected (%u,%u), found (%u,%u))\n",
11796 pipe_name(pipe), plane + 1,
11797 sw_entry->start, sw_entry->end,
11798 hw_entry->start, hw_entry->end);
11799 }
11800
11801 /* cursor */
11802 hw_entry = &hw_ddb.cursor[pipe];
11803 sw_entry = &sw_ddb->cursor[pipe];
11804
11805 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11806 continue;
11807
11808 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11809 "(expected (%u,%u), found (%u,%u))\n",
11810 pipe_name(pipe),
11811 sw_entry->start, sw_entry->end,
11812 hw_entry->start, hw_entry->end);
11813 }
11814}
11815
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011816static void
11817check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011818{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011819 struct intel_connector *connector;
11820
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011821 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011822 /* This also checks the encoder/connector hw state with the
11823 * ->get_hw_state callbacks. */
11824 intel_connector_check_state(connector);
11825
Rob Clarke2c719b2014-12-15 13:56:32 -050011826 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011827 "connector's staged encoder doesn't match current encoder\n");
11828 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011829}
11830
11831static void
11832check_encoder_state(struct drm_device *dev)
11833{
11834 struct intel_encoder *encoder;
11835 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011836
Damien Lespiaub2784e12014-08-05 11:29:37 +010011837 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011838 bool enabled = false;
11839 bool active = false;
11840 enum pipe pipe, tracked_pipe;
11841
11842 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11843 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011844 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011845
Rob Clarke2c719b2014-12-15 13:56:32 -050011846 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011847 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011848 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011849 "encoder's active_connectors set, but no crtc\n");
11850
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011851 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011852 if (connector->base.encoder != &encoder->base)
11853 continue;
11854 enabled = true;
11855 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11856 active = true;
11857 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011858 /*
11859 * for MST connectors if we unplug the connector is gone
11860 * away but the encoder is still connected to a crtc
11861 * until a modeset happens in response to the hotplug.
11862 */
11863 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11864 continue;
11865
Rob Clarke2c719b2014-12-15 13:56:32 -050011866 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011867 "encoder's enabled state mismatch "
11868 "(expected %i, found %i)\n",
11869 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011870 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011871 "active encoder with no crtc\n");
11872
Rob Clarke2c719b2014-12-15 13:56:32 -050011873 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011874 "encoder's computed active state doesn't match tracked active state "
11875 "(expected %i, found %i)\n", active, encoder->connectors_active);
11876
11877 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011878 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011879 "encoder's hw state doesn't match sw tracking "
11880 "(expected %i, found %i)\n",
11881 encoder->connectors_active, active);
11882
11883 if (!encoder->base.crtc)
11884 continue;
11885
11886 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011887 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011888 "active encoder's pipe doesn't match"
11889 "(expected %i, found %i)\n",
11890 tracked_pipe, pipe);
11891
11892 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011893}
11894
11895static void
11896check_crtc_state(struct drm_device *dev)
11897{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011898 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011899 struct intel_crtc *crtc;
11900 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011901 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011902
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011903 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011904 bool enabled = false;
11905 bool active = false;
11906
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011907 memset(&pipe_config, 0, sizeof(pipe_config));
11908
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011909 DRM_DEBUG_KMS("[CRTC:%d]\n",
11910 crtc->base.base.id);
11911
Matt Roper83d65732015-02-25 13:12:16 -080011912 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011913 "active crtc, but not enabled in sw tracking\n");
11914
Damien Lespiaub2784e12014-08-05 11:29:37 +010011915 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011916 if (encoder->base.crtc != &crtc->base)
11917 continue;
11918 enabled = true;
11919 if (encoder->connectors_active)
11920 active = true;
11921 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020011922
Rob Clarke2c719b2014-12-15 13:56:32 -050011923 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011924 "crtc's computed active state doesn't match tracked active state "
11925 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080011926 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011927 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080011928 "(expected %i, found %i)\n", enabled,
11929 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011930
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011931 active = dev_priv->display.get_pipe_config(crtc,
11932 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020011933
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030011934 /* hw state is inconsistent with the pipe quirk */
11935 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
11936 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020011937 active = crtc->active;
11938
Damien Lespiaub2784e12014-08-05 11:29:37 +010011939 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030011940 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020011941 if (encoder->base.crtc != &crtc->base)
11942 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010011943 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020011944 encoder->get_config(encoder, &pipe_config);
11945 }
11946
Rob Clarke2c719b2014-12-15 13:56:32 -050011947 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011948 "crtc active state doesn't match with hw state "
11949 "(expected %i, found %i)\n", crtc->active, active);
11950
Daniel Vetterc0b03412013-05-28 12:05:54 +020011951 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011952 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050011953 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020011954 intel_dump_pipe_config(crtc, &pipe_config,
11955 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020011956 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011957 "[sw state]");
11958 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011959 }
11960}
11961
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011962static void
11963check_shared_dpll_state(struct drm_device *dev)
11964{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011965 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011966 struct intel_crtc *crtc;
11967 struct intel_dpll_hw_state dpll_hw_state;
11968 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020011969
11970 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
11971 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
11972 int enabled_crtcs = 0, active_crtcs = 0;
11973 bool active;
11974
11975 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
11976
11977 DRM_DEBUG_KMS("%s\n", pll->name);
11978
11979 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
11980
Rob Clarke2c719b2014-12-15 13:56:32 -050011981 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020011982 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020011983 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050011984 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020011985 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011986 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020011987 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011988 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020011989 "pll on state mismatch (expected %i, found %i)\n",
11990 pll->on, active);
11991
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011992 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011993 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020011994 enabled_crtcs++;
11995 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
11996 active_crtcs++;
11997 }
Rob Clarke2c719b2014-12-15 13:56:32 -050011998 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020011999 "pll active crtcs mismatch (expected %i, found %i)\n",
12000 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012001 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012002 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012003 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012004
Rob Clarke2c719b2014-12-15 13:56:32 -050012005 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012006 sizeof(dpll_hw_state)),
12007 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012008 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012009}
12010
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012011void
12012intel_modeset_check_state(struct drm_device *dev)
12013{
Damien Lespiau08db6652014-11-04 17:06:52 +000012014 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012015 check_connector_state(dev);
12016 check_encoder_state(dev);
12017 check_crtc_state(dev);
12018 check_shared_dpll_state(dev);
12019}
12020
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012021void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012022 int dotclock)
12023{
12024 /*
12025 * FDI already provided one idea for the dotclock.
12026 * Yell if the encoder disagrees.
12027 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012028 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012029 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012030 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012031}
12032
Ville Syrjälä80715b22014-05-15 20:23:23 +030012033static void update_scanline_offset(struct intel_crtc *crtc)
12034{
12035 struct drm_device *dev = crtc->base.dev;
12036
12037 /*
12038 * The scanline counter increments at the leading edge of hsync.
12039 *
12040 * On most platforms it starts counting from vtotal-1 on the
12041 * first active line. That means the scanline counter value is
12042 * always one less than what we would expect. Ie. just after
12043 * start of vblank, which also occurs at start of hsync (on the
12044 * last active line), the scanline counter will read vblank_start-1.
12045 *
12046 * On gen2 the scanline counter starts counting from 1 instead
12047 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12048 * to keep the value positive), instead of adding one.
12049 *
12050 * On HSW+ the behaviour of the scanline counter depends on the output
12051 * type. For DP ports it behaves like most other platforms, but on HDMI
12052 * there's an extra 1 line difference. So we need to add two instead of
12053 * one to the value.
12054 */
12055 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012056 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012057 int vtotal;
12058
12059 vtotal = mode->crtc_vtotal;
12060 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12061 vtotal /= 2;
12062
12063 crtc->scanline_offset = vtotal - 1;
12064 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012065 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012066 crtc->scanline_offset = 2;
12067 } else
12068 crtc->scanline_offset = 1;
12069}
12070
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012071static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012072intel_modeset_compute_config(struct drm_crtc *crtc,
12073 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012074 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012075 unsigned *modeset_pipes,
12076 unsigned *prepare_pipes,
12077 unsigned *disable_pipes)
12078{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012079 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012080 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012081 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012082 int ret = 0;
12083
12084 ret = drm_atomic_add_affected_connectors(state, crtc);
12085 if (ret)
12086 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012087
12088 intel_modeset_affected_pipes(crtc, modeset_pipes,
12089 prepare_pipes, disable_pipes);
12090
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012091 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12092 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12093 if (IS_ERR(pipe_config))
12094 return pipe_config;
12095
12096 pipe_config->base.enable = false;
12097 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012098
12099 /*
12100 * Note this needs changes when we start tracking multiple modes
12101 * and crtcs. At that point we'll need to compute the whole config
12102 * (i.e. one pipe_config for each crtc) rather than just the one
12103 * for this crtc.
12104 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012105 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12106 /* FIXME: For now we still expect modeset_pipes has at most
12107 * one bit set. */
12108 if (WARN_ON(&intel_crtc->base != crtc))
12109 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012110
Daniel Vetter8805aa72015-04-10 16:22:38 +020012111 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012112 if (IS_ERR(pipe_config))
12113 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012114
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012115 pipe_config->base.enable = true;
12116
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012117 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12118 "[modeset]");
12119 }
12120
12121 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012122}
12123
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012124static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012125 unsigned modeset_pipes,
12126 unsigned disable_pipes)
12127{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012128 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012129 struct drm_i915_private *dev_priv = to_i915(dev);
12130 unsigned clear_pipes = modeset_pipes | disable_pipes;
12131 struct intel_crtc *intel_crtc;
12132 int ret = 0;
12133
12134 if (!dev_priv->display.crtc_compute_clock)
12135 return 0;
12136
12137 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12138 if (ret)
12139 goto done;
12140
12141 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012142 struct intel_crtc_state *crtc_state =
12143 intel_atomic_get_crtc_state(state, intel_crtc);
12144
12145 /* Modeset pipes should have a new state by now */
12146 if (WARN_ON(IS_ERR(crtc_state)))
12147 continue;
12148
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012149 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012150 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012151 if (ret) {
12152 intel_shared_dpll_abort_config(dev_priv);
12153 goto done;
12154 }
12155 }
12156
12157done:
12158 return ret;
12159}
12160
Daniel Vetterf30da182013-04-11 20:22:50 +020012161static int __intel_set_mode(struct drm_crtc *crtc,
12162 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012163 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012164 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012165 unsigned modeset_pipes,
12166 unsigned prepare_pipes,
12167 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020012168{
12169 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012170 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012171 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012172 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012173 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020012174 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012175 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012176
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012177 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012178 if (!saved_mode)
12179 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020012180
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012181 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12182 if (!crtc_state_copy) {
12183 ret = -ENOMEM;
12184 goto done;
12185 }
12186
Tim Gardner3ac18232012-12-07 07:54:26 -070012187 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012188
Jesse Barnes30a970c2013-11-04 13:48:12 -080012189 /*
12190 * See if the config requires any additional preparation, e.g.
12191 * to adjust global state with pipes off. We need to do this
12192 * here so we can get the modeset_pipe updated config for the new
12193 * mode set on this crtc. For other crtcs we need to use the
12194 * adjusted_mode bits in the crtc directly.
12195 */
Vandana Kannanf8437dd12014-11-24 13:37:39 +053012196 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012197 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12198 if (ret)
12199 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012200
Ville Syrjäläc164f832013-11-05 22:34:12 +020012201 /* may have added more to prepare_pipes than we should */
12202 prepare_pipes &= ~disable_pipes;
12203 }
12204
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012205 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012206 if (ret)
12207 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020012208
Daniel Vetter460da9162013-03-27 00:44:51 +010012209 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12210 intel_crtc_disable(&intel_crtc->base);
12211
Daniel Vetterea9d7582012-07-10 10:42:52 +020012212 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012213 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012214 dev_priv->display.crtc_disable(&intel_crtc->base);
12215 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012216
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012217 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12218 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012219 *
12220 * Note we'll need to fix this up when we start tracking multiple
12221 * pipes; here we assume a single modeset_pipe and only track the
12222 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012223 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012224 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020012225 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012226 /* mode_set/enable/disable functions rely on a correct pipe
12227 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012228 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012229
12230 /*
12231 * Calculate and store various constants which
12232 * are later needed by vblank and swap-completion
12233 * timestamping. They are derived from true hwmode.
12234 */
12235 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012236 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012237 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012238
Daniel Vetterea9d7582012-07-10 10:42:52 +020012239 /* Only after disabling all output pipelines that will be changed can we
12240 * update the the output configuration. */
12241 intel_modeset_update_state(dev, prepare_pipes);
12242
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012243 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012244
Daniel Vetter25c5b262012-07-08 22:08:04 +020012245 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080012246 struct drm_plane *primary = intel_crtc->base.primary;
12247 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020012248
Gustavo Padovan455a6802014-12-01 15:40:11 -080012249 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012250 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12251 fb, 0, 0,
12252 hdisplay, vdisplay,
12253 x << 16, y << 16,
12254 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020012255 }
12256
12257 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030012258 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12259 update_scanline_offset(intel_crtc);
12260
Daniel Vetter25c5b262012-07-08 22:08:04 +020012261 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012262 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012263
Daniel Vettera6778b32012-07-02 09:56:42 +020012264 /* FIXME: add subpixel order */
12265done:
Matt Roper83d65732015-02-25 13:12:16 -080012266 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070012267 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012268
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012269 if (ret == 0 && pipe_config) {
12270 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12271
12272 /* The pipe_config will be freed with the atomic state, so
12273 * make a copy. */
12274 memcpy(crtc_state_copy, intel_crtc->config,
12275 sizeof *crtc_state_copy);
12276 intel_crtc->config = crtc_state_copy;
12277 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012278 } else {
12279 kfree(crtc_state_copy);
12280 }
12281
Tim Gardner3ac18232012-12-07 07:54:26 -070012282 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020012283 return ret;
12284}
12285
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012286static int intel_set_mode_pipes(struct drm_crtc *crtc,
12287 struct drm_display_mode *mode,
12288 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012289 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012290 unsigned modeset_pipes,
12291 unsigned prepare_pipes,
12292 unsigned disable_pipes)
12293{
12294 int ret;
12295
12296 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12297 prepare_pipes, disable_pipes);
12298
12299 if (ret == 0)
12300 intel_modeset_check_state(crtc->dev);
12301
12302 return ret;
12303}
12304
Damien Lespiaue7457a92013-08-08 22:28:59 +010012305static int intel_set_mode(struct drm_crtc *crtc,
12306 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012307 int x, int y, struct drm_framebuffer *fb,
12308 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012309{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012310 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012311 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012312 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012313
Daniel Vetter8805aa72015-04-10 16:22:38 +020012314 pipe_config = intel_modeset_compute_config(crtc, mode, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012315 &modeset_pipes,
12316 &prepare_pipes,
12317 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020012318
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012319 if (IS_ERR(pipe_config)) {
12320 ret = PTR_ERR(pipe_config);
12321 goto out;
12322 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012323
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012324 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12325 modeset_pipes, prepare_pipes,
12326 disable_pipes);
12327 if (ret)
12328 goto out;
12329
12330out:
12331 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012332}
12333
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012334void intel_crtc_restore_mode(struct drm_crtc *crtc)
12335{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012336 struct drm_device *dev = crtc->dev;
12337 struct drm_atomic_state *state;
12338 struct intel_encoder *encoder;
12339 struct intel_connector *connector;
12340 struct drm_connector_state *connector_state;
12341
12342 state = drm_atomic_state_alloc(dev);
12343 if (!state) {
12344 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12345 crtc->base.id);
12346 return;
12347 }
12348
12349 state->acquire_ctx = dev->mode_config.acquire_ctx;
12350
12351 /* The force restore path in the HW readout code relies on the staged
12352 * config still keeping the user requested config while the actual
12353 * state has been overwritten by the configuration read from HW. We
12354 * need to copy the staged config to the atomic state, otherwise the
12355 * mode set will just reapply the state the HW is already in. */
12356 for_each_intel_encoder(dev, encoder) {
12357 if (&encoder->new_crtc->base != crtc)
12358 continue;
12359
12360 for_each_intel_connector(dev, connector) {
12361 if (connector->new_encoder != encoder)
12362 continue;
12363
12364 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12365 if (IS_ERR(connector_state)) {
12366 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12367 connector->base.base.id,
12368 connector->base.name,
12369 PTR_ERR(connector_state));
12370 continue;
12371 }
12372
12373 connector_state->crtc = crtc;
12374 connector_state->best_encoder = &encoder->base;
12375 }
12376 }
12377
12378 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12379 state);
12380
12381 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012382}
12383
Daniel Vetter25c5b262012-07-08 22:08:04 +020012384#undef for_each_intel_crtc_masked
12385
Daniel Vetterd9e55602012-07-04 22:16:09 +020012386static void intel_set_config_free(struct intel_set_config *config)
12387{
12388 if (!config)
12389 return;
12390
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012391 kfree(config->save_connector_encoders);
12392 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012393 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012394 kfree(config);
12395}
12396
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012397static int intel_set_config_save_state(struct drm_device *dev,
12398 struct intel_set_config *config)
12399{
Ville Syrjälä76688512014-01-10 11:28:06 +020012400 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012401 struct drm_encoder *encoder;
12402 struct drm_connector *connector;
12403 int count;
12404
Ville Syrjälä76688512014-01-10 11:28:06 +020012405 config->save_crtc_enabled =
12406 kcalloc(dev->mode_config.num_crtc,
12407 sizeof(bool), GFP_KERNEL);
12408 if (!config->save_crtc_enabled)
12409 return -ENOMEM;
12410
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012411 config->save_encoder_crtcs =
12412 kcalloc(dev->mode_config.num_encoder,
12413 sizeof(struct drm_crtc *), GFP_KERNEL);
12414 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012415 return -ENOMEM;
12416
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012417 config->save_connector_encoders =
12418 kcalloc(dev->mode_config.num_connector,
12419 sizeof(struct drm_encoder *), GFP_KERNEL);
12420 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012421 return -ENOMEM;
12422
12423 /* Copy data. Note that driver private data is not affected.
12424 * Should anything bad happen only the expected state is
12425 * restored, not the drivers personal bookkeeping.
12426 */
12427 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012428 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012429 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012430 }
12431
12432 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012433 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012434 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012435 }
12436
12437 count = 0;
12438 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012439 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012440 }
12441
12442 return 0;
12443}
12444
12445static void intel_set_config_restore_state(struct drm_device *dev,
12446 struct intel_set_config *config)
12447{
Ville Syrjälä76688512014-01-10 11:28:06 +020012448 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012449 struct intel_encoder *encoder;
12450 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012451 int count;
12452
12453 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012454 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012455 crtc->new_enabled = config->save_crtc_enabled[count++];
12456 }
12457
12458 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012459 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012460 encoder->new_crtc =
12461 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012462 }
12463
12464 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012465 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012466 connector->new_encoder =
12467 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012468 }
12469}
12470
Imre Deake3de42b2013-05-03 19:44:07 +020012471static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012472is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012473{
12474 int i;
12475
Chris Wilson2e57f472013-07-17 12:14:40 +010012476 if (set->num_connectors == 0)
12477 return false;
12478
12479 if (WARN_ON(set->connectors == NULL))
12480 return false;
12481
12482 for (i = 0; i < set->num_connectors; i++)
12483 if (set->connectors[i]->encoder &&
12484 set->connectors[i]->encoder->crtc == set->crtc &&
12485 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012486 return true;
12487
12488 return false;
12489}
12490
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012491static void
12492intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12493 struct intel_set_config *config)
12494{
12495
12496 /* We should be able to check here if the fb has the same properties
12497 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012498 if (is_crtc_connector_off(set)) {
12499 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012500 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012501 /*
12502 * If we have no fb, we can only flip as long as the crtc is
12503 * active, otherwise we need a full mode set. The crtc may
12504 * be active if we've only disabled the primary plane, or
12505 * in fastboot situations.
12506 */
Matt Roperf4510a22014-04-01 15:22:40 -070012507 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012508 struct intel_crtc *intel_crtc =
12509 to_intel_crtc(set->crtc);
12510
Matt Roper3b150f02014-05-29 08:06:53 -070012511 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012512 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12513 config->fb_changed = true;
12514 } else {
12515 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12516 config->mode_changed = true;
12517 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012518 } else if (set->fb == NULL) {
12519 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012520 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012521 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012522 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012523 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012524 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012525 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012526 }
12527
Daniel Vetter835c5872012-07-10 18:11:08 +020012528 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012529 config->fb_changed = true;
12530
12531 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12532 DRM_DEBUG_KMS("modes are different, full mode set\n");
12533 drm_mode_debug_printmodeline(&set->crtc->mode);
12534 drm_mode_debug_printmodeline(set->mode);
12535 config->mode_changed = true;
12536 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012537
12538 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12539 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012540}
12541
Daniel Vetter2e431052012-07-04 22:42:15 +020012542static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012543intel_modeset_stage_output_state(struct drm_device *dev,
12544 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012545 struct intel_set_config *config,
12546 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012547{
Daniel Vetter9a935852012-07-05 22:34:27 +020012548 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012549 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012550 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012551 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012552 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012553
Damien Lespiau9abdda72013-02-13 13:29:23 +000012554 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012555 * of connectors. For paranoia, double-check this. */
12556 WARN_ON(!set->fb && (set->num_connectors != 0));
12557 WARN_ON(set->fb && (set->num_connectors == 0));
12558
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012559 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012560 /* Otherwise traverse passed in connector list and get encoders
12561 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012562 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012563 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012564 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012565 break;
12566 }
12567 }
12568
Daniel Vetter9a935852012-07-05 22:34:27 +020012569 /* If we disable the crtc, disable all its connectors. Also, if
12570 * the connector is on the changing crtc but not on the new
12571 * connector list, disable it. */
12572 if ((!set->fb || ro == set->num_connectors) &&
12573 connector->base.encoder &&
12574 connector->base.encoder->crtc == set->crtc) {
12575 connector->new_encoder = NULL;
12576
12577 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12578 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012579 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012580 }
12581
12582
12583 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012584 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12585 connector->base.base.id,
12586 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012587 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012588 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012589 }
12590 /* connector->new_encoder is now updated for all connectors. */
12591
12592 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012593 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012594 struct drm_crtc *new_crtc;
12595
Daniel Vetter9a935852012-07-05 22:34:27 +020012596 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012597 continue;
12598
Daniel Vetter9a935852012-07-05 22:34:27 +020012599 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012600
12601 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012602 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012603 new_crtc = set->crtc;
12604 }
12605
12606 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012607 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12608 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012609 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012610 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012611 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012612
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012613 connector_state =
12614 drm_atomic_get_connector_state(state, &connector->base);
12615 if (IS_ERR(connector_state))
12616 return PTR_ERR(connector_state);
12617
12618 connector_state->crtc = new_crtc;
12619 connector_state->best_encoder = &connector->new_encoder->base;
12620
Daniel Vetter9a935852012-07-05 22:34:27 +020012621 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12622 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012623 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012624 new_crtc->base.id);
12625 }
12626
12627 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012628 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012629 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012630 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012631 if (connector->new_encoder == encoder) {
12632 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012633 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012634 }
12635 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012636
12637 if (num_connectors == 0)
12638 encoder->new_crtc = NULL;
12639 else if (num_connectors > 1)
12640 return -EINVAL;
12641
Daniel Vetter9a935852012-07-05 22:34:27 +020012642 /* Only now check for crtc changes so we don't miss encoders
12643 * that will be disabled. */
12644 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012645 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12646 encoder->base.base.id,
12647 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012648 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012649 }
12650 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012651 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012652 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012653 connector_state =
12654 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012655 if (IS_ERR(connector_state))
12656 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012657
12658 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012659 if (connector->new_encoder != connector->encoder)
12660 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012661 } else {
12662 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012663 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012664 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012665 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012666 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012667 crtc->new_enabled = false;
12668
Damien Lespiaub2784e12014-08-05 11:29:37 +010012669 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012670 if (encoder->new_crtc == crtc) {
12671 crtc->new_enabled = true;
12672 break;
12673 }
12674 }
12675
Matt Roper83d65732015-02-25 13:12:16 -080012676 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012677 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12678 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012679 crtc->new_enabled ? "en" : "dis");
12680 config->mode_changed = true;
12681 }
12682 }
12683
Daniel Vetter2e431052012-07-04 22:42:15 +020012684 return 0;
12685}
12686
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012687static void disable_crtc_nofb(struct intel_crtc *crtc)
12688{
12689 struct drm_device *dev = crtc->base.dev;
12690 struct intel_encoder *encoder;
12691 struct intel_connector *connector;
12692
12693 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12694 pipe_name(crtc->pipe));
12695
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012696 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012697 if (connector->new_encoder &&
12698 connector->new_encoder->new_crtc == crtc)
12699 connector->new_encoder = NULL;
12700 }
12701
Damien Lespiaub2784e12014-08-05 11:29:37 +010012702 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012703 if (encoder->new_crtc == crtc)
12704 encoder->new_crtc = NULL;
12705 }
12706
12707 crtc->new_enabled = false;
12708}
12709
Daniel Vetter2e431052012-07-04 22:42:15 +020012710static int intel_crtc_set_config(struct drm_mode_set *set)
12711{
12712 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012713 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012714 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012715 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012716 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012717 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012718 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012719
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012720 BUG_ON(!set);
12721 BUG_ON(!set->crtc);
12722 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012723
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012724 /* Enforce sane interface api - has been abused by the fb helper. */
12725 BUG_ON(!set->mode && set->fb);
12726 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012727
Daniel Vetter2e431052012-07-04 22:42:15 +020012728 if (set->fb) {
12729 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12730 set->crtc->base.id, set->fb->base.id,
12731 (int)set->num_connectors, set->x, set->y);
12732 } else {
12733 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012734 }
12735
12736 dev = set->crtc->dev;
12737
12738 ret = -ENOMEM;
12739 config = kzalloc(sizeof(*config), GFP_KERNEL);
12740 if (!config)
12741 goto out_config;
12742
12743 ret = intel_set_config_save_state(dev, config);
12744 if (ret)
12745 goto out_config;
12746
12747 save_set.crtc = set->crtc;
12748 save_set.mode = &set->crtc->mode;
12749 save_set.x = set->crtc->x;
12750 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012751 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012752
12753 /* Compute whether we need a full modeset, only an fb base update or no
12754 * change at all. In the future we might also check whether only the
12755 * mode changed, e.g. for LVDS where we only change the panel fitter in
12756 * such cases. */
12757 intel_set_config_compute_mode_changes(set, config);
12758
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012759 state = drm_atomic_state_alloc(dev);
12760 if (!state) {
12761 ret = -ENOMEM;
12762 goto out_config;
12763 }
12764
12765 state->acquire_ctx = dev->mode_config.acquire_ctx;
12766
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012767 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012768 if (ret)
12769 goto fail;
12770
Jesse Barnes50f52752014-11-07 13:11:00 -080012771 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Daniel Vetter8805aa72015-04-10 16:22:38 +020012772 state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012773 &modeset_pipes,
12774 &prepare_pipes,
12775 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012776 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012777 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012778 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012779 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012780 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012781 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012782 config->mode_changed = true;
12783
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012784 /*
12785 * Note we have an issue here with infoframes: current code
12786 * only updates them on the full mode set path per hw
12787 * requirements. So here we should be checking for any
12788 * required changes and forcing a mode set.
12789 */
Jesse Barnes20664592014-11-05 14:26:09 -080012790 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012791
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012792 intel_update_pipe_size(to_intel_crtc(set->crtc));
12793
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012794 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012795 ret = intel_set_mode_pipes(set->crtc, set->mode,
12796 set->x, set->y, set->fb, pipe_config,
12797 modeset_pipes, prepare_pipes,
12798 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012799 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012800 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012801 struct drm_plane *primary = set->crtc->primary;
12802 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012803
Gustavo Padovan455a6802014-12-01 15:40:11 -080012804 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012805 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12806 0, 0, hdisplay, vdisplay,
12807 set->x << 16, set->y << 16,
12808 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012809
12810 /*
12811 * We need to make sure the primary plane is re-enabled if it
12812 * has previously been turned off.
12813 */
12814 if (!intel_crtc->primary_enabled && ret == 0) {
12815 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030012816 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012817 }
12818
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012819 /*
12820 * In the fastboot case this may be our only check of the
12821 * state after boot. It would be better to only do it on
12822 * the first update, but we don't have a nice way of doing that
12823 * (and really, set_config isn't used much for high freq page
12824 * flipping, so increasing its cost here shouldn't be a big
12825 * deal).
12826 */
Jani Nikulad330a952014-01-21 11:24:25 +020012827 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012828 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012829 }
12830
Chris Wilson2d05eae2013-05-03 17:36:25 +010012831 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012832 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12833 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012834fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012835 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012836
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012837 drm_atomic_state_clear(state);
12838
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012839 /*
12840 * HACK: if the pipe was on, but we didn't have a framebuffer,
12841 * force the pipe off to avoid oopsing in the modeset code
12842 * due to fb==NULL. This should only happen during boot since
12843 * we don't yet reconstruct the FB from the hardware state.
12844 */
12845 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12846 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12847
Chris Wilson2d05eae2013-05-03 17:36:25 +010012848 /* Try to restore the config */
12849 if (config->mode_changed &&
12850 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012851 save_set.x, save_set.y, save_set.fb,
12852 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012853 DRM_ERROR("failed to restore config after modeset failure\n");
12854 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012855
Daniel Vetterd9e55602012-07-04 22:16:09 +020012856out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012857 if (state)
12858 drm_atomic_state_free(state);
12859
Daniel Vetterd9e55602012-07-04 22:16:09 +020012860 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012861 return ret;
12862}
12863
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012864static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012865 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012866 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012867 .destroy = intel_crtc_destroy,
12868 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012869 .atomic_duplicate_state = intel_crtc_duplicate_state,
12870 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012871};
12872
Daniel Vetter53589012013-06-05 13:34:16 +020012873static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12874 struct intel_shared_dpll *pll,
12875 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012876{
Daniel Vetter53589012013-06-05 13:34:16 +020012877 uint32_t val;
12878
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012879 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012880 return false;
12881
Daniel Vetter53589012013-06-05 13:34:16 +020012882 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012883 hw_state->dpll = val;
12884 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12885 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012886
12887 return val & DPLL_VCO_ENABLE;
12888}
12889
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012890static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12891 struct intel_shared_dpll *pll)
12892{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012893 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12894 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012895}
12896
Daniel Vettere7b903d2013-06-05 13:34:14 +020012897static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12898 struct intel_shared_dpll *pll)
12899{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012900 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012901 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012902
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012903 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012904
12905 /* Wait for the clocks to stabilize. */
12906 POSTING_READ(PCH_DPLL(pll->id));
12907 udelay(150);
12908
12909 /* The pixel multiplier can only be updated once the
12910 * DPLL is enabled and the clocks are stable.
12911 *
12912 * So write it again.
12913 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012914 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012915 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012916 udelay(200);
12917}
12918
12919static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
12920 struct intel_shared_dpll *pll)
12921{
12922 struct drm_device *dev = dev_priv->dev;
12923 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012924
12925 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012926 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020012927 if (intel_crtc_to_shared_dpll(crtc) == pll)
12928 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
12929 }
12930
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012931 I915_WRITE(PCH_DPLL(pll->id), 0);
12932 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020012933 udelay(200);
12934}
12935
Daniel Vetter46edb022013-06-05 13:34:12 +020012936static char *ibx_pch_dpll_names[] = {
12937 "PCH DPLL A",
12938 "PCH DPLL B",
12939};
12940
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012941static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012942{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012943 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012944 int i;
12945
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012946 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012947
Daniel Vettere72f9fb2013-06-05 13:34:06 +020012948 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020012949 dev_priv->shared_dplls[i].id = i;
12950 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012951 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020012952 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
12953 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020012954 dev_priv->shared_dplls[i].get_hw_state =
12955 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012956 }
12957}
12958
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012959static void intel_shared_dpll_init(struct drm_device *dev)
12960{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012961 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012962
Daniel Vetter9cd86932014-06-25 22:01:57 +030012963 if (HAS_DDI(dev))
12964 intel_ddi_pll_init(dev);
12965 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012966 ibx_pch_dpll_init(dev);
12967 else
12968 dev_priv->num_shared_dpll = 0;
12969
12970 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020012971}
12972
Matt Roper6beb8c232014-12-01 15:40:14 -080012973/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000012974 * intel_wm_need_update - Check whether watermarks need updating
12975 * @plane: drm plane
12976 * @state: new plane state
12977 *
12978 * Check current plane state versus the new one to determine whether
12979 * watermarks need to be recalculated.
12980 *
12981 * Returns true or false.
12982 */
12983bool intel_wm_need_update(struct drm_plane *plane,
12984 struct drm_plane_state *state)
12985{
12986 /* Update watermarks on tiling changes. */
12987 if (!plane->state->fb || !state->fb ||
12988 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
12989 plane->state->rotation != state->rotation)
12990 return true;
12991
12992 return false;
12993}
12994
12995/**
Matt Roper6beb8c232014-12-01 15:40:14 -080012996 * intel_prepare_plane_fb - Prepare fb for usage on plane
12997 * @plane: drm plane to prepare for
12998 * @fb: framebuffer to prepare for presentation
12999 *
13000 * Prepares a framebuffer for usage on a display plane. Generally this
13001 * involves pinning the underlying object and updating the frontbuffer tracking
13002 * bits. Some older platforms need special physical address handling for
13003 * cursor planes.
13004 *
13005 * Returns 0 on success, negative error code on failure.
13006 */
13007int
13008intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013009 struct drm_framebuffer *fb,
13010 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013011{
13012 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013013 struct intel_plane *intel_plane = to_intel_plane(plane);
13014 enum pipe pipe = intel_plane->pipe;
13015 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13016 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13017 unsigned frontbuffer_bits = 0;
13018 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013019
Matt Roperea2c67b2014-12-23 10:41:52 -080013020 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013021 return 0;
13022
Matt Roper6beb8c232014-12-01 15:40:14 -080013023 switch (plane->type) {
13024 case DRM_PLANE_TYPE_PRIMARY:
13025 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13026 break;
13027 case DRM_PLANE_TYPE_CURSOR:
13028 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13029 break;
13030 case DRM_PLANE_TYPE_OVERLAY:
13031 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13032 break;
13033 }
Matt Roper465c1202014-05-29 08:06:54 -070013034
Matt Roper4c345742014-07-09 16:22:10 -070013035 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013036
Matt Roper6beb8c232014-12-01 15:40:14 -080013037 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13038 INTEL_INFO(dev)->cursor_needs_physical) {
13039 int align = IS_I830(dev) ? 16 * 1024 : 256;
13040 ret = i915_gem_object_attach_phys(obj, align);
13041 if (ret)
13042 DRM_DEBUG_KMS("failed to attach phys object\n");
13043 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013044 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013045 }
13046
13047 if (ret == 0)
13048 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13049
13050 mutex_unlock(&dev->struct_mutex);
13051
13052 return ret;
13053}
13054
Matt Roper38f3ce32014-12-02 07:45:25 -080013055/**
13056 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13057 * @plane: drm plane to clean up for
13058 * @fb: old framebuffer that was on plane
13059 *
13060 * Cleans up a framebuffer that has just been removed from a plane.
13061 */
13062void
13063intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013064 struct drm_framebuffer *fb,
13065 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013066{
13067 struct drm_device *dev = plane->dev;
13068 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13069
13070 if (WARN_ON(!obj))
13071 return;
13072
13073 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13074 !INTEL_INFO(dev)->cursor_needs_physical) {
13075 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013076 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013077 mutex_unlock(&dev->struct_mutex);
13078 }
Matt Roper465c1202014-05-29 08:06:54 -070013079}
13080
13081static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013082intel_check_primary_plane(struct drm_plane *plane,
13083 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013084{
Matt Roper32b7eee2014-12-24 07:59:06 -080013085 struct drm_device *dev = plane->dev;
13086 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013087 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013088 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013089 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013090 struct drm_rect *dest = &state->dst;
13091 struct drm_rect *src = &state->src;
13092 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013093 bool can_position = false;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013094 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013095
Matt Roperea2c67b2014-12-23 10:41:52 -080013096 crtc = crtc ? crtc : plane->crtc;
13097 intel_crtc = to_intel_crtc(crtc);
13098
Sonika Jindald8106362015-04-10 14:37:28 +053013099 if (INTEL_INFO(dev)->gen >= 9)
13100 can_position = true;
13101
Matt Roperc59cb172014-12-01 15:40:16 -080013102 ret = drm_plane_helper_check_update(plane, crtc, fb,
13103 src, dest, clip,
13104 DRM_PLANE_HELPER_NO_SCALING,
13105 DRM_PLANE_HELPER_NO_SCALING,
Sonika Jindald8106362015-04-10 14:37:28 +053013106 can_position, true,
13107 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013108 if (ret)
13109 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013110
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013111 if (intel_crtc->active) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013112 intel_crtc->atomic.wait_for_flips = true;
13113
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013114 /*
13115 * FBC does not work on some platforms for rotated
13116 * planes, so disable it when rotation is not 0 and
13117 * update it when rotation is set back to 0.
13118 *
13119 * FIXME: This is redundant with the fbc update done in
13120 * the primary plane enable function except that that
13121 * one is done too late. We eventually need to unify
13122 * this.
13123 */
13124 if (intel_crtc->primary_enabled &&
13125 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013126 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013127 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013128 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013129 }
13130
13131 if (state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013132 /*
13133 * BDW signals flip done immediately if the plane
13134 * is disabled, even if the plane enable is already
13135 * armed to occur at the next vblank :(
13136 */
13137 if (IS_BROADWELL(dev) && !intel_crtc->primary_enabled)
13138 intel_crtc->atomic.wait_vblank = true;
13139 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013140
Matt Roper32b7eee2014-12-24 07:59:06 -080013141 intel_crtc->atomic.fb_bits |=
13142 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13143
13144 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013145
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013146 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013147 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013148 }
13149
13150 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013151}
13152
Sonika Jindal48404c12014-08-22 14:06:04 +053013153static void
13154intel_commit_primary_plane(struct drm_plane *plane,
13155 struct intel_plane_state *state)
13156{
Matt Roper2b875c22014-12-01 15:40:13 -080013157 struct drm_crtc *crtc = state->base.crtc;
13158 struct drm_framebuffer *fb = state->base.fb;
13159 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013160 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013161 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013162 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013163
Matt Roperea2c67b2014-12-23 10:41:52 -080013164 crtc = crtc ? crtc : plane->crtc;
13165 intel_crtc = to_intel_crtc(crtc);
13166
Matt Ropercf4c7c12014-12-04 10:27:42 -080013167 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013168 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013169 crtc->y = src->y1 >> 16;
13170
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013171 if (intel_crtc->active) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013172 if (state->visible) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013173 /* FIXME: kill this fastboot hack */
13174 intel_update_pipe_size(intel_crtc);
13175
13176 intel_crtc->primary_enabled = true;
13177
13178 dev_priv->display.update_primary_plane(crtc, plane->fb,
13179 crtc->x, crtc->y);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013180 } else {
13181 /*
13182 * If clipping results in a non-visible primary plane,
13183 * we'll disable the primary plane. Note that this is
13184 * a bit different than what happens if userspace
13185 * explicitly disables the plane by passing fb=0
13186 * because plane->fb still gets set and pinned.
13187 */
13188 intel_disable_primary_hw_plane(plane, crtc);
13189 }
Matt Roper32b7eee2014-12-24 07:59:06 -080013190 }
13191}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013192
Matt Roper32b7eee2014-12-24 07:59:06 -080013193static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13194{
13195 struct drm_device *dev = crtc->dev;
13196 struct drm_i915_private *dev_priv = dev->dev_private;
13197 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013198 struct intel_plane *intel_plane;
13199 struct drm_plane *p;
13200 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013201
Matt Roperea2c67b2014-12-23 10:41:52 -080013202 /* Track fb's for any planes being disabled */
13203 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13204 intel_plane = to_intel_plane(p);
13205
13206 if (intel_crtc->atomic.disabled_planes &
13207 (1 << drm_plane_index(p))) {
13208 switch (p->type) {
13209 case DRM_PLANE_TYPE_PRIMARY:
13210 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13211 break;
13212 case DRM_PLANE_TYPE_CURSOR:
13213 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13214 break;
13215 case DRM_PLANE_TYPE_OVERLAY:
13216 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13217 break;
13218 }
13219
13220 mutex_lock(&dev->struct_mutex);
13221 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13222 mutex_unlock(&dev->struct_mutex);
13223 }
13224 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013225
Matt Roper32b7eee2014-12-24 07:59:06 -080013226 if (intel_crtc->atomic.wait_for_flips)
13227 intel_crtc_wait_for_pending_flips(crtc);
13228
13229 if (intel_crtc->atomic.disable_fbc)
13230 intel_fbc_disable(dev);
13231
13232 if (intel_crtc->atomic.pre_disable_primary)
13233 intel_pre_disable_primary(crtc);
13234
13235 if (intel_crtc->atomic.update_wm)
13236 intel_update_watermarks(crtc);
13237
13238 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013239
13240 /* Perform vblank evasion around commit operation */
13241 if (intel_crtc->active)
13242 intel_crtc->atomic.evade =
13243 intel_pipe_update_start(intel_crtc,
13244 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013245}
13246
13247static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13248{
13249 struct drm_device *dev = crtc->dev;
13250 struct drm_i915_private *dev_priv = dev->dev_private;
13251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13252 struct drm_plane *p;
13253
Matt Roperc34c9ee2014-12-23 10:41:50 -080013254 if (intel_crtc->atomic.evade)
13255 intel_pipe_update_end(intel_crtc,
13256 intel_crtc->atomic.start_vbl_count);
13257
Matt Roper32b7eee2014-12-24 07:59:06 -080013258 intel_runtime_pm_put(dev_priv);
13259
13260 if (intel_crtc->atomic.wait_vblank)
13261 intel_wait_for_vblank(dev, intel_crtc->pipe);
13262
13263 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13264
13265 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013266 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013267 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013268 mutex_unlock(&dev->struct_mutex);
13269 }
Matt Roper465c1202014-05-29 08:06:54 -070013270
Matt Roper32b7eee2014-12-24 07:59:06 -080013271 if (intel_crtc->atomic.post_enable_primary)
13272 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013273
Matt Roper32b7eee2014-12-24 07:59:06 -080013274 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13275 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13276 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13277 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013278
Matt Roper32b7eee2014-12-24 07:59:06 -080013279 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013280}
13281
Matt Ropercf4c7c12014-12-04 10:27:42 -080013282/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013283 * intel_plane_destroy - destroy a plane
13284 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013285 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013286 * Common destruction function for all types of planes (primary, cursor,
13287 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013288 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013289void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013290{
13291 struct intel_plane *intel_plane = to_intel_plane(plane);
13292 drm_plane_cleanup(plane);
13293 kfree(intel_plane);
13294}
13295
Matt Roper65a3fea2015-01-21 16:35:42 -080013296const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013297 .update_plane = drm_atomic_helper_update_plane,
13298 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013299 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013300 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013301 .atomic_get_property = intel_plane_atomic_get_property,
13302 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013303 .atomic_duplicate_state = intel_plane_duplicate_state,
13304 .atomic_destroy_state = intel_plane_destroy_state,
13305
Matt Roper465c1202014-05-29 08:06:54 -070013306};
13307
13308static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13309 int pipe)
13310{
13311 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013312 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013313 const uint32_t *intel_primary_formats;
13314 int num_formats;
13315
13316 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13317 if (primary == NULL)
13318 return NULL;
13319
Matt Roper8e7d6882015-01-21 16:35:41 -080013320 state = intel_create_plane_state(&primary->base);
13321 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013322 kfree(primary);
13323 return NULL;
13324 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013325 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013326
Matt Roper465c1202014-05-29 08:06:54 -070013327 primary->can_scale = false;
13328 primary->max_downscale = 1;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013329 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013330 primary->pipe = pipe;
13331 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013332 primary->check_plane = intel_check_primary_plane;
13333 primary->commit_plane = intel_commit_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013334 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013335 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13336 primary->plane = !pipe;
13337
13338 if (INTEL_INFO(dev)->gen <= 3) {
13339 intel_primary_formats = intel_primary_formats_gen2;
13340 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13341 } else {
13342 intel_primary_formats = intel_primary_formats_gen4;
13343 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13344 }
13345
13346 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013347 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013348 intel_primary_formats, num_formats,
13349 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013350
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013351 if (INTEL_INFO(dev)->gen >= 4)
13352 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013353
Matt Roperea2c67b2014-12-23 10:41:52 -080013354 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13355
Matt Roper465c1202014-05-29 08:06:54 -070013356 return &primary->base;
13357}
13358
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013359void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13360{
13361 if (!dev->mode_config.rotation_property) {
13362 unsigned long flags = BIT(DRM_ROTATE_0) |
13363 BIT(DRM_ROTATE_180);
13364
13365 if (INTEL_INFO(dev)->gen >= 9)
13366 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13367
13368 dev->mode_config.rotation_property =
13369 drm_mode_create_rotation_property(dev, flags);
13370 }
13371 if (dev->mode_config.rotation_property)
13372 drm_object_attach_property(&plane->base.base,
13373 dev->mode_config.rotation_property,
13374 plane->base.state->rotation);
13375}
13376
Matt Roper3d7d6512014-06-10 08:28:13 -070013377static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013378intel_check_cursor_plane(struct drm_plane *plane,
13379 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013380{
Matt Roper2b875c22014-12-01 15:40:13 -080013381 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013382 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013383 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013384 struct drm_rect *dest = &state->dst;
13385 struct drm_rect *src = &state->src;
13386 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013387 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013388 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013389 unsigned stride;
13390 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013391
Matt Roperea2c67b2014-12-23 10:41:52 -080013392 crtc = crtc ? crtc : plane->crtc;
13393 intel_crtc = to_intel_crtc(crtc);
13394
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013395 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013396 src, dest, clip,
13397 DRM_PLANE_HELPER_NO_SCALING,
13398 DRM_PLANE_HELPER_NO_SCALING,
13399 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013400 if (ret)
13401 return ret;
13402
13403
13404 /* if we want to turn off the cursor ignore width and height */
13405 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013406 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013407
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013408 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013409 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13410 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13411 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013412 return -EINVAL;
13413 }
13414
Matt Roperea2c67b2014-12-23 10:41:52 -080013415 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13416 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013417 DRM_DEBUG_KMS("buffer is too small\n");
13418 return -ENOMEM;
13419 }
13420
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013421 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013422 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13423 ret = -EINVAL;
13424 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013425
Matt Roper32b7eee2014-12-24 07:59:06 -080013426finish:
13427 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013428 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013429 intel_crtc->atomic.update_wm = true;
13430
13431 intel_crtc->atomic.fb_bits |=
13432 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13433 }
13434
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013435 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013436}
13437
Matt Roperf4a2cf22014-12-01 15:40:12 -080013438static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013439intel_commit_cursor_plane(struct drm_plane *plane,
13440 struct intel_plane_state *state)
13441{
Matt Roper2b875c22014-12-01 15:40:13 -080013442 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013443 struct drm_device *dev = plane->dev;
13444 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013445 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013446 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013447
Matt Roperea2c67b2014-12-23 10:41:52 -080013448 crtc = crtc ? crtc : plane->crtc;
13449 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013450
Matt Roperea2c67b2014-12-23 10:41:52 -080013451 plane->fb = state->base.fb;
13452 crtc->cursor_x = state->base.crtc_x;
13453 crtc->cursor_y = state->base.crtc_y;
13454
Gustavo Padovana912f122014-12-01 15:40:10 -080013455 if (intel_crtc->cursor_bo == obj)
13456 goto update;
13457
Matt Roperf4a2cf22014-12-01 15:40:12 -080013458 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013459 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013460 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013461 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013462 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013463 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013464
Gustavo Padovana912f122014-12-01 15:40:10 -080013465 intel_crtc->cursor_addr = addr;
13466 intel_crtc->cursor_bo = obj;
13467update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013468
Matt Roper32b7eee2014-12-24 07:59:06 -080013469 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013470 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013471}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013472
Matt Roper3d7d6512014-06-10 08:28:13 -070013473static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13474 int pipe)
13475{
13476 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013477 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013478
13479 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13480 if (cursor == NULL)
13481 return NULL;
13482
Matt Roper8e7d6882015-01-21 16:35:41 -080013483 state = intel_create_plane_state(&cursor->base);
13484 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013485 kfree(cursor);
13486 return NULL;
13487 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013488 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013489
Matt Roper3d7d6512014-06-10 08:28:13 -070013490 cursor->can_scale = false;
13491 cursor->max_downscale = 1;
13492 cursor->pipe = pipe;
13493 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013494 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013495 cursor->check_plane = intel_check_cursor_plane;
13496 cursor->commit_plane = intel_commit_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013497
13498 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013499 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013500 intel_cursor_formats,
13501 ARRAY_SIZE(intel_cursor_formats),
13502 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013503
13504 if (INTEL_INFO(dev)->gen >= 4) {
13505 if (!dev->mode_config.rotation_property)
13506 dev->mode_config.rotation_property =
13507 drm_mode_create_rotation_property(dev,
13508 BIT(DRM_ROTATE_0) |
13509 BIT(DRM_ROTATE_180));
13510 if (dev->mode_config.rotation_property)
13511 drm_object_attach_property(&cursor->base.base,
13512 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013513 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013514 }
13515
Matt Roperea2c67b2014-12-23 10:41:52 -080013516 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13517
Matt Roper3d7d6512014-06-10 08:28:13 -070013518 return &cursor->base;
13519}
13520
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013521static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13522 struct intel_crtc_state *crtc_state)
13523{
13524 int i;
13525 struct intel_scaler *intel_scaler;
13526 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13527
13528 for (i = 0; i < intel_crtc->num_scalers; i++) {
13529 intel_scaler = &scaler_state->scalers[i];
13530 intel_scaler->in_use = 0;
13531 intel_scaler->id = i;
13532
13533 intel_scaler->mode = PS_SCALER_MODE_DYN;
13534 }
13535
13536 scaler_state->scaler_id = -1;
13537}
13538
Hannes Ederb358d0a2008-12-18 21:18:47 +010013539static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013540{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013541 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013542 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013543 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013544 struct drm_plane *primary = NULL;
13545 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013546 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013547
Daniel Vetter955382f2013-09-19 14:05:45 +020013548 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013549 if (intel_crtc == NULL)
13550 return;
13551
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013552 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13553 if (!crtc_state)
13554 goto fail;
13555 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013556 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013557
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013558 /* initialize shared scalers */
13559 if (INTEL_INFO(dev)->gen >= 9) {
13560 if (pipe == PIPE_C)
13561 intel_crtc->num_scalers = 1;
13562 else
13563 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13564
13565 skl_init_scalers(dev, intel_crtc, crtc_state);
13566 }
13567
Matt Roper465c1202014-05-29 08:06:54 -070013568 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013569 if (!primary)
13570 goto fail;
13571
13572 cursor = intel_cursor_plane_create(dev, pipe);
13573 if (!cursor)
13574 goto fail;
13575
Matt Roper465c1202014-05-29 08:06:54 -070013576 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013577 cursor, &intel_crtc_funcs);
13578 if (ret)
13579 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013580
13581 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013582 for (i = 0; i < 256; i++) {
13583 intel_crtc->lut_r[i] = i;
13584 intel_crtc->lut_g[i] = i;
13585 intel_crtc->lut_b[i] = i;
13586 }
13587
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013588 /*
13589 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013590 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013591 */
Jesse Barnes80824002009-09-10 15:28:06 -070013592 intel_crtc->pipe = pipe;
13593 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013594 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013595 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013596 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013597 }
13598
Chris Wilson4b0e3332014-05-30 16:35:26 +030013599 intel_crtc->cursor_base = ~0;
13600 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013601 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013602
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013603 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13604 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13605 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13606 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13607
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013608 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13609
Jesse Barnes79e53942008-11-07 14:24:08 -080013610 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013611
13612 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013613 return;
13614
13615fail:
13616 if (primary)
13617 drm_plane_cleanup(primary);
13618 if (cursor)
13619 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013620 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013621 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013622}
13623
Jesse Barnes752aa882013-10-31 18:55:49 +020013624enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13625{
13626 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013627 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013628
Rob Clark51fd3712013-11-19 12:10:12 -050013629 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013630
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013631 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013632 return INVALID_PIPE;
13633
13634 return to_intel_crtc(encoder->crtc)->pipe;
13635}
13636
Carl Worth08d7b3d2009-04-29 14:43:54 -070013637int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013638 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013639{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013640 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013641 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013642 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013643
Rob Clark7707e652014-07-17 23:30:04 -040013644 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013645
Rob Clark7707e652014-07-17 23:30:04 -040013646 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013647 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013648 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013649 }
13650
Rob Clark7707e652014-07-17 23:30:04 -040013651 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013652 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013653
Daniel Vetterc05422d2009-08-11 16:05:30 +020013654 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013655}
13656
Daniel Vetter66a92782012-07-12 20:08:18 +020013657static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013658{
Daniel Vetter66a92782012-07-12 20:08:18 +020013659 struct drm_device *dev = encoder->base.dev;
13660 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013661 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013662 int entry = 0;
13663
Damien Lespiaub2784e12014-08-05 11:29:37 +010013664 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013665 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013666 index_mask |= (1 << entry);
13667
Jesse Barnes79e53942008-11-07 14:24:08 -080013668 entry++;
13669 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013670
Jesse Barnes79e53942008-11-07 14:24:08 -080013671 return index_mask;
13672}
13673
Chris Wilson4d302442010-12-14 19:21:29 +000013674static bool has_edp_a(struct drm_device *dev)
13675{
13676 struct drm_i915_private *dev_priv = dev->dev_private;
13677
13678 if (!IS_MOBILE(dev))
13679 return false;
13680
13681 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13682 return false;
13683
Damien Lespiaue3589902014-02-07 19:12:50 +000013684 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013685 return false;
13686
13687 return true;
13688}
13689
Jesse Barnes84b4e042014-06-25 08:24:29 -070013690static bool intel_crt_present(struct drm_device *dev)
13691{
13692 struct drm_i915_private *dev_priv = dev->dev_private;
13693
Damien Lespiau884497e2013-12-03 13:56:23 +000013694 if (INTEL_INFO(dev)->gen >= 9)
13695 return false;
13696
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013697 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013698 return false;
13699
13700 if (IS_CHERRYVIEW(dev))
13701 return false;
13702
13703 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13704 return false;
13705
13706 return true;
13707}
13708
Jesse Barnes79e53942008-11-07 14:24:08 -080013709static void intel_setup_outputs(struct drm_device *dev)
13710{
Eric Anholt725e30a2009-01-22 13:01:02 -080013711 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013712 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013713 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013714
Daniel Vetterc9093352013-06-06 22:22:47 +020013715 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013716
Jesse Barnes84b4e042014-06-25 08:24:29 -070013717 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013718 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013719
Vandana Kannanc776eb22014-08-19 12:05:01 +053013720 if (IS_BROXTON(dev)) {
13721 /*
13722 * FIXME: Broxton doesn't support port detection via the
13723 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13724 * detect the ports.
13725 */
13726 intel_ddi_init(dev, PORT_A);
13727 intel_ddi_init(dev, PORT_B);
13728 intel_ddi_init(dev, PORT_C);
13729 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013730 int found;
13731
Jesse Barnesde31fac2015-03-06 15:53:32 -080013732 /*
13733 * Haswell uses DDI functions to detect digital outputs.
13734 * On SKL pre-D0 the strap isn't connected, so we assume
13735 * it's there.
13736 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013737 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013738 /* WaIgnoreDDIAStrap: skl */
13739 if (found ||
13740 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013741 intel_ddi_init(dev, PORT_A);
13742
13743 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13744 * register */
13745 found = I915_READ(SFUSE_STRAP);
13746
13747 if (found & SFUSE_STRAP_DDIB_DETECTED)
13748 intel_ddi_init(dev, PORT_B);
13749 if (found & SFUSE_STRAP_DDIC_DETECTED)
13750 intel_ddi_init(dev, PORT_C);
13751 if (found & SFUSE_STRAP_DDID_DETECTED)
13752 intel_ddi_init(dev, PORT_D);
13753 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013754 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013755 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013756
13757 if (has_edp_a(dev))
13758 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013759
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013760 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013761 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013762 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013763 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013764 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013765 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013766 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013767 }
13768
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013769 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013770 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013771
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013772 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013773 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013774
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013775 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013776 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013777
Daniel Vetter270b3042012-10-27 15:52:05 +020013778 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013779 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013780 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013781 /*
13782 * The DP_DETECTED bit is the latched state of the DDC
13783 * SDA pin at boot. However since eDP doesn't require DDC
13784 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13785 * eDP ports may have been muxed to an alternate function.
13786 * Thus we can't rely on the DP_DETECTED bit alone to detect
13787 * eDP ports. Consult the VBT as well as DP_DETECTED to
13788 * detect eDP ports.
13789 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013790 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13791 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013792 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13793 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013794 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13795 intel_dp_is_edp(dev, PORT_B))
13796 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013797
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013798 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13799 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013800 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13801 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013802 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13803 intel_dp_is_edp(dev, PORT_C))
13804 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013805
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013806 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013807 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013808 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13809 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013810 /* eDP not supported on port D, so don't check VBT */
13811 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13812 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013813 }
13814
Jani Nikula3cfca972013-08-27 15:12:26 +030013815 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013816 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013817 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013818
Paulo Zanonie2debe92013-02-18 19:00:27 -030013819 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013820 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013821 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013822 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13823 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013824 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013825 }
Ma Ling27185ae2009-08-24 13:50:23 +080013826
Imre Deake7281ea2013-05-08 13:14:08 +030013827 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013828 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013829 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013830
13831 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013832
Paulo Zanonie2debe92013-02-18 19:00:27 -030013833 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013834 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013835 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013836 }
Ma Ling27185ae2009-08-24 13:50:23 +080013837
Paulo Zanonie2debe92013-02-18 19:00:27 -030013838 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013839
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013840 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
13841 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013842 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013843 }
Imre Deake7281ea2013-05-08 13:14:08 +030013844 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013845 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080013846 }
Ma Ling27185ae2009-08-24 13:50:23 +080013847
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013848 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030013849 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013850 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070013851 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013852 intel_dvo_init(dev);
13853
Zhenyu Wang103a1962009-11-27 11:44:36 +080013854 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080013855 intel_tv_init(dev);
13856
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080013857 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070013858
Damien Lespiaub2784e12014-08-05 11:29:37 +010013859 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010013860 encoder->base.possible_crtcs = encoder->crtc_mask;
13861 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020013862 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080013863 }
Chris Wilson47356eb2011-01-11 17:06:04 +000013864
Paulo Zanonidde86e22012-12-01 12:04:25 -020013865 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020013866
13867 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013868}
13869
13870static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
13871{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013872 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080013873 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080013874
Daniel Vetteref2d6332014-02-10 18:00:38 +010013875 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013876 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010013877 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030013878 drm_gem_object_unreference(&intel_fb->obj->base);
13879 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080013880 kfree(intel_fb);
13881}
13882
13883static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000013884 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080013885 unsigned int *handle)
13886{
13887 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000013888 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080013889
Chris Wilson05394f32010-11-08 19:18:58 +000013890 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080013891}
13892
13893static const struct drm_framebuffer_funcs intel_fb_funcs = {
13894 .destroy = intel_user_framebuffer_destroy,
13895 .create_handle = intel_user_framebuffer_create_handle,
13896};
13897
Damien Lespiaub3218032015-02-27 11:15:18 +000013898static
13899u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
13900 uint32_t pixel_format)
13901{
13902 u32 gen = INTEL_INFO(dev)->gen;
13903
13904 if (gen >= 9) {
13905 /* "The stride in bytes must not exceed the of the size of 8K
13906 * pixels and 32K bytes."
13907 */
13908 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
13909 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
13910 return 32*1024;
13911 } else if (gen >= 4) {
13912 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13913 return 16*1024;
13914 else
13915 return 32*1024;
13916 } else if (gen >= 3) {
13917 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
13918 return 8*1024;
13919 else
13920 return 16*1024;
13921 } else {
13922 /* XXX DSPC is limited to 4k tiled */
13923 return 8*1024;
13924 }
13925}
13926
Daniel Vetterb5ea6422014-03-02 21:18:00 +010013927static int intel_framebuffer_init(struct drm_device *dev,
13928 struct intel_framebuffer *intel_fb,
13929 struct drm_mode_fb_cmd2 *mode_cmd,
13930 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080013931{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000013932 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080013933 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000013934 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080013935
Daniel Vetterdd4916c2013-10-09 21:23:51 +020013936 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
13937
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013938 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
13939 /* Enforce that fb modifier and tiling mode match, but only for
13940 * X-tiled. This is needed for FBC. */
13941 if (!!(obj->tiling_mode == I915_TILING_X) !=
13942 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
13943 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
13944 return -EINVAL;
13945 }
13946 } else {
13947 if (obj->tiling_mode == I915_TILING_X)
13948 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
13949 else if (obj->tiling_mode == I915_TILING_Y) {
13950 DRM_DEBUG("No Y tiling for legacy addfb\n");
13951 return -EINVAL;
13952 }
13953 }
13954
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000013955 /* Passed in modifier sanity checking. */
13956 switch (mode_cmd->modifier[0]) {
13957 case I915_FORMAT_MOD_Y_TILED:
13958 case I915_FORMAT_MOD_Yf_TILED:
13959 if (INTEL_INFO(dev)->gen < 9) {
13960 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
13961 mode_cmd->modifier[0]);
13962 return -EINVAL;
13963 }
13964 case DRM_FORMAT_MOD_NONE:
13965 case I915_FORMAT_MOD_X_TILED:
13966 break;
13967 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070013968 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
13969 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010013970 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013971 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013972
Damien Lespiaub3218032015-02-27 11:15:18 +000013973 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
13974 mode_cmd->pixel_format);
13975 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
13976 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
13977 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010013978 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013979 }
Chris Wilson57cd6502010-08-08 12:34:44 +010013980
Damien Lespiaub3218032015-02-27 11:15:18 +000013981 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
13982 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013983 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000013984 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
13985 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013986 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010013987 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013988 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013989 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013990
Daniel Vetter2a80ead2015-02-10 17:16:06 +000013991 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013992 mode_cmd->pitches[0] != obj->stride) {
13993 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
13994 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013995 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000013996 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020013997
Ville Syrjälä57779d02012-10-31 17:50:14 +020013998 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080013999 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014000 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014001 case DRM_FORMAT_RGB565:
14002 case DRM_FORMAT_XRGB8888:
14003 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014004 break;
14005 case DRM_FORMAT_XRGB1555:
14006 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014007 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014008 DRM_DEBUG("unsupported pixel format: %s\n",
14009 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014010 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014011 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014012 break;
14013 case DRM_FORMAT_XBGR8888:
14014 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014015 case DRM_FORMAT_XRGB2101010:
14016 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014017 case DRM_FORMAT_XBGR2101010:
14018 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014019 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014020 DRM_DEBUG("unsupported pixel format: %s\n",
14021 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014022 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014023 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014024 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014025 case DRM_FORMAT_YUYV:
14026 case DRM_FORMAT_UYVY:
14027 case DRM_FORMAT_YVYU:
14028 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014029 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014030 DRM_DEBUG("unsupported pixel format: %s\n",
14031 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014032 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014033 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014034 break;
14035 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014036 DRM_DEBUG("unsupported pixel format: %s\n",
14037 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014038 return -EINVAL;
14039 }
14040
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014041 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14042 if (mode_cmd->offsets[0] != 0)
14043 return -EINVAL;
14044
Damien Lespiauec2c9812015-01-20 12:51:45 +000014045 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014046 mode_cmd->pixel_format,
14047 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014048 /* FIXME drm helper for size checks (especially planar formats)? */
14049 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14050 return -EINVAL;
14051
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014052 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14053 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014054 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014055
Jesse Barnes79e53942008-11-07 14:24:08 -080014056 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14057 if (ret) {
14058 DRM_ERROR("framebuffer init failed %d\n", ret);
14059 return ret;
14060 }
14061
Jesse Barnes79e53942008-11-07 14:24:08 -080014062 return 0;
14063}
14064
Jesse Barnes79e53942008-11-07 14:24:08 -080014065static struct drm_framebuffer *
14066intel_user_framebuffer_create(struct drm_device *dev,
14067 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014068 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014069{
Chris Wilson05394f32010-11-08 19:18:58 +000014070 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014071
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014072 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14073 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014074 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014075 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014076
Chris Wilsond2dff872011-04-19 08:36:26 +010014077 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014078}
14079
Daniel Vetter4520f532013-10-09 09:18:51 +020014080#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014081static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014082{
14083}
14084#endif
14085
Jesse Barnes79e53942008-11-07 14:24:08 -080014086static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014087 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014088 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014089 .atomic_check = intel_atomic_check,
14090 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014091};
14092
Jesse Barnese70236a2009-09-21 10:42:27 -070014093/* Set up chip specific display functions */
14094static void intel_init_display(struct drm_device *dev)
14095{
14096 struct drm_i915_private *dev_priv = dev->dev_private;
14097
Daniel Vetteree9300b2013-06-03 22:40:22 +020014098 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14099 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014100 else if (IS_CHERRYVIEW(dev))
14101 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014102 else if (IS_VALLEYVIEW(dev))
14103 dev_priv->display.find_dpll = vlv_find_best_dpll;
14104 else if (IS_PINEVIEW(dev))
14105 dev_priv->display.find_dpll = pnv_find_best_dpll;
14106 else
14107 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14108
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014109 if (INTEL_INFO(dev)->gen >= 9) {
14110 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014111 dev_priv->display.get_initial_plane_config =
14112 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014113 dev_priv->display.crtc_compute_clock =
14114 haswell_crtc_compute_clock;
14115 dev_priv->display.crtc_enable = haswell_crtc_enable;
14116 dev_priv->display.crtc_disable = haswell_crtc_disable;
14117 dev_priv->display.off = ironlake_crtc_off;
14118 dev_priv->display.update_primary_plane =
14119 skylake_update_primary_plane;
14120 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014121 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014122 dev_priv->display.get_initial_plane_config =
14123 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014124 dev_priv->display.crtc_compute_clock =
14125 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014126 dev_priv->display.crtc_enable = haswell_crtc_enable;
14127 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014128 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014129 dev_priv->display.update_primary_plane =
14130 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014131 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014132 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014133 dev_priv->display.get_initial_plane_config =
14134 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014135 dev_priv->display.crtc_compute_clock =
14136 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014137 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14138 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014139 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014140 dev_priv->display.update_primary_plane =
14141 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014142 } else if (IS_VALLEYVIEW(dev)) {
14143 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014144 dev_priv->display.get_initial_plane_config =
14145 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014146 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014147 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14148 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14149 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014150 dev_priv->display.update_primary_plane =
14151 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014152 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014153 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014154 dev_priv->display.get_initial_plane_config =
14155 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014156 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014157 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14158 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014159 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014160 dev_priv->display.update_primary_plane =
14161 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014162 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014163
Jesse Barnese70236a2009-09-21 10:42:27 -070014164 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014165 if (IS_SKYLAKE(dev))
14166 dev_priv->display.get_display_clock_speed =
14167 skylake_get_display_clock_speed;
14168 else if (IS_BROADWELL(dev))
14169 dev_priv->display.get_display_clock_speed =
14170 broadwell_get_display_clock_speed;
14171 else if (IS_HASWELL(dev))
14172 dev_priv->display.get_display_clock_speed =
14173 haswell_get_display_clock_speed;
14174 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014175 dev_priv->display.get_display_clock_speed =
14176 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014177 else if (IS_GEN5(dev))
14178 dev_priv->display.get_display_clock_speed =
14179 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014180 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14181 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014182 dev_priv->display.get_display_clock_speed =
14183 i945_get_display_clock_speed;
14184 else if (IS_I915G(dev))
14185 dev_priv->display.get_display_clock_speed =
14186 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014187 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014188 dev_priv->display.get_display_clock_speed =
14189 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014190 else if (IS_PINEVIEW(dev))
14191 dev_priv->display.get_display_clock_speed =
14192 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014193 else if (IS_I915GM(dev))
14194 dev_priv->display.get_display_clock_speed =
14195 i915gm_get_display_clock_speed;
14196 else if (IS_I865G(dev))
14197 dev_priv->display.get_display_clock_speed =
14198 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014199 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014200 dev_priv->display.get_display_clock_speed =
14201 i855_get_display_clock_speed;
14202 else /* 852, 830 */
14203 dev_priv->display.get_display_clock_speed =
14204 i830_get_display_clock_speed;
14205
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014206 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014207 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014208 } else if (IS_GEN6(dev)) {
14209 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014210 } else if (IS_IVYBRIDGE(dev)) {
14211 /* FIXME: detect B0+ stepping and use auto training */
14212 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014213 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014214 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014215 } else if (IS_VALLEYVIEW(dev)) {
14216 dev_priv->display.modeset_global_resources =
14217 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014218 } else if (IS_BROXTON(dev)) {
14219 dev_priv->display.modeset_global_resources =
14220 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014221 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014222
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014223 switch (INTEL_INFO(dev)->gen) {
14224 case 2:
14225 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14226 break;
14227
14228 case 3:
14229 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14230 break;
14231
14232 case 4:
14233 case 5:
14234 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14235 break;
14236
14237 case 6:
14238 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14239 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014240 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014241 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014242 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14243 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014244 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014245 /* Drop through - unsupported since execlist only. */
14246 default:
14247 /* Default just returns -ENODEV to indicate unsupported */
14248 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014249 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014250
14251 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014252
14253 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014254}
14255
Jesse Barnesb690e962010-07-19 13:53:12 -070014256/*
14257 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14258 * resume, or other times. This quirk makes sure that's the case for
14259 * affected systems.
14260 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014261static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014262{
14263 struct drm_i915_private *dev_priv = dev->dev_private;
14264
14265 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014266 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014267}
14268
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014269static void quirk_pipeb_force(struct drm_device *dev)
14270{
14271 struct drm_i915_private *dev_priv = dev->dev_private;
14272
14273 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14274 DRM_INFO("applying pipe b force quirk\n");
14275}
14276
Keith Packard435793d2011-07-12 14:56:22 -070014277/*
14278 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14279 */
14280static void quirk_ssc_force_disable(struct drm_device *dev)
14281{
14282 struct drm_i915_private *dev_priv = dev->dev_private;
14283 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014284 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014285}
14286
Carsten Emde4dca20e2012-03-15 15:56:26 +010014287/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014288 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14289 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014290 */
14291static void quirk_invert_brightness(struct drm_device *dev)
14292{
14293 struct drm_i915_private *dev_priv = dev->dev_private;
14294 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014295 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014296}
14297
Scot Doyle9c72cc62014-07-03 23:27:50 +000014298/* Some VBT's incorrectly indicate no backlight is present */
14299static void quirk_backlight_present(struct drm_device *dev)
14300{
14301 struct drm_i915_private *dev_priv = dev->dev_private;
14302 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14303 DRM_INFO("applying backlight present quirk\n");
14304}
14305
Jesse Barnesb690e962010-07-19 13:53:12 -070014306struct intel_quirk {
14307 int device;
14308 int subsystem_vendor;
14309 int subsystem_device;
14310 void (*hook)(struct drm_device *dev);
14311};
14312
Egbert Eich5f85f172012-10-14 15:46:38 +020014313/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14314struct intel_dmi_quirk {
14315 void (*hook)(struct drm_device *dev);
14316 const struct dmi_system_id (*dmi_id_list)[];
14317};
14318
14319static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14320{
14321 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14322 return 1;
14323}
14324
14325static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14326 {
14327 .dmi_id_list = &(const struct dmi_system_id[]) {
14328 {
14329 .callback = intel_dmi_reverse_brightness,
14330 .ident = "NCR Corporation",
14331 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14332 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14333 },
14334 },
14335 { } /* terminating entry */
14336 },
14337 .hook = quirk_invert_brightness,
14338 },
14339};
14340
Ben Widawskyc43b5632012-04-16 14:07:40 -070014341static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014342 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014343 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014344
Jesse Barnesb690e962010-07-19 13:53:12 -070014345 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14346 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14347
Jesse Barnesb690e962010-07-19 13:53:12 -070014348 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14349 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14350
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014351 /* 830 needs to leave pipe A & dpll A up */
14352 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14353
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014354 /* 830 needs to leave pipe B & dpll B up */
14355 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14356
Keith Packard435793d2011-07-12 14:56:22 -070014357 /* Lenovo U160 cannot use SSC on LVDS */
14358 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014359
14360 /* Sony Vaio Y cannot use SSC on LVDS */
14361 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014362
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014363 /* Acer Aspire 5734Z must invert backlight brightness */
14364 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14365
14366 /* Acer/eMachines G725 */
14367 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14368
14369 /* Acer/eMachines e725 */
14370 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14371
14372 /* Acer/Packard Bell NCL20 */
14373 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14374
14375 /* Acer Aspire 4736Z */
14376 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014377
14378 /* Acer Aspire 5336 */
14379 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014380
14381 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14382 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014383
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014384 /* Acer C720 Chromebook (Core i3 4005U) */
14385 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14386
jens steinb2a96012014-10-28 20:25:53 +010014387 /* Apple Macbook 2,1 (Core 2 T7400) */
14388 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14389
Scot Doyled4967d82014-07-03 23:27:52 +000014390 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14391 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014392
14393 /* HP Chromebook 14 (Celeron 2955U) */
14394 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014395
14396 /* Dell Chromebook 11 */
14397 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014398};
14399
14400static void intel_init_quirks(struct drm_device *dev)
14401{
14402 struct pci_dev *d = dev->pdev;
14403 int i;
14404
14405 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14406 struct intel_quirk *q = &intel_quirks[i];
14407
14408 if (d->device == q->device &&
14409 (d->subsystem_vendor == q->subsystem_vendor ||
14410 q->subsystem_vendor == PCI_ANY_ID) &&
14411 (d->subsystem_device == q->subsystem_device ||
14412 q->subsystem_device == PCI_ANY_ID))
14413 q->hook(dev);
14414 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014415 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14416 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14417 intel_dmi_quirks[i].hook(dev);
14418 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014419}
14420
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014421/* Disable the VGA plane that we never use */
14422static void i915_disable_vga(struct drm_device *dev)
14423{
14424 struct drm_i915_private *dev_priv = dev->dev_private;
14425 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014426 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014427
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014428 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014429 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014430 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014431 sr1 = inb(VGA_SR_DATA);
14432 outb(sr1 | 1<<5, VGA_SR_DATA);
14433 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14434 udelay(300);
14435
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014436 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014437 POSTING_READ(vga_reg);
14438}
14439
Daniel Vetterf8175862012-04-10 15:50:11 +020014440void intel_modeset_init_hw(struct drm_device *dev)
14441{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014442 intel_prepare_ddi(dev);
14443
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014444 if (IS_VALLEYVIEW(dev))
14445 vlv_update_cdclk(dev);
14446
Daniel Vetterf8175862012-04-10 15:50:11 +020014447 intel_init_clock_gating(dev);
14448
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014449 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014450}
14451
Jesse Barnes79e53942008-11-07 14:24:08 -080014452void intel_modeset_init(struct drm_device *dev)
14453{
Jesse Barnes652c3932009-08-17 13:31:43 -070014454 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014455 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014456 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014457 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014458
14459 drm_mode_config_init(dev);
14460
14461 dev->mode_config.min_width = 0;
14462 dev->mode_config.min_height = 0;
14463
Dave Airlie019d96c2011-09-29 16:20:42 +010014464 dev->mode_config.preferred_depth = 24;
14465 dev->mode_config.prefer_shadow = 1;
14466
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014467 dev->mode_config.allow_fb_modifiers = true;
14468
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014469 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014470
Jesse Barnesb690e962010-07-19 13:53:12 -070014471 intel_init_quirks(dev);
14472
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014473 intel_init_pm(dev);
14474
Ben Widawskye3c74752013-04-05 13:12:39 -070014475 if (INTEL_INFO(dev)->num_pipes == 0)
14476 return;
14477
Jesse Barnese70236a2009-09-21 10:42:27 -070014478 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014479 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014480
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014481 if (IS_GEN2(dev)) {
14482 dev->mode_config.max_width = 2048;
14483 dev->mode_config.max_height = 2048;
14484 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014485 dev->mode_config.max_width = 4096;
14486 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014487 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014488 dev->mode_config.max_width = 8192;
14489 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014490 }
Damien Lespiau068be562014-03-28 14:17:49 +000014491
Ville Syrjälädc41c152014-08-13 11:57:05 +030014492 if (IS_845G(dev) || IS_I865G(dev)) {
14493 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14494 dev->mode_config.cursor_height = 1023;
14495 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014496 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14497 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14498 } else {
14499 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14500 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14501 }
14502
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014503 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014504
Zhao Yakui28c97732009-10-09 11:39:41 +080014505 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014506 INTEL_INFO(dev)->num_pipes,
14507 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014508
Damien Lespiau055e3932014-08-18 13:49:10 +010014509 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014510 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014511 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014512 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014513 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014514 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014515 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014516 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014517 }
14518
Jesse Barnesf42bb702013-12-16 16:34:23 -080014519 intel_init_dpio(dev);
14520
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014521 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014522
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014523 /* Just disable it once at startup */
14524 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014525 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014526
14527 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014528 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014529
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014530 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014531 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014532 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014533
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014534 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014535 if (!crtc->active)
14536 continue;
14537
Jesse Barnes46f297f2014-03-07 08:57:48 -080014538 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014539 * Note that reserving the BIOS fb up front prevents us
14540 * from stuffing other stolen allocations like the ring
14541 * on top. This prevents some ugliness at boot time, and
14542 * can even allow for smooth boot transitions if the BIOS
14543 * fb is large enough for the active pipe configuration.
14544 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014545 if (dev_priv->display.get_initial_plane_config) {
14546 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014547 &crtc->plane_config);
14548 /*
14549 * If the fb is shared between multiple heads, we'll
14550 * just get the first one.
14551 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014552 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014553 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014554 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014555}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014556
Daniel Vetter7fad7982012-07-04 17:51:47 +020014557static void intel_enable_pipe_a(struct drm_device *dev)
14558{
14559 struct intel_connector *connector;
14560 struct drm_connector *crt = NULL;
14561 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014562 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014563
14564 /* We can't just switch on the pipe A, we need to set things up with a
14565 * proper mode and output configuration. As a gross hack, enable pipe A
14566 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014567 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014568 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14569 crt = &connector->base;
14570 break;
14571 }
14572 }
14573
14574 if (!crt)
14575 return;
14576
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014577 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014578 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014579}
14580
Daniel Vetterfa555832012-10-10 23:14:00 +020014581static bool
14582intel_check_plane_mapping(struct intel_crtc *crtc)
14583{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014584 struct drm_device *dev = crtc->base.dev;
14585 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014586 u32 reg, val;
14587
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014588 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014589 return true;
14590
14591 reg = DSPCNTR(!crtc->plane);
14592 val = I915_READ(reg);
14593
14594 if ((val & DISPLAY_PLANE_ENABLE) &&
14595 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14596 return false;
14597
14598 return true;
14599}
14600
Daniel Vetter24929352012-07-02 20:28:59 +020014601static void intel_sanitize_crtc(struct intel_crtc *crtc)
14602{
14603 struct drm_device *dev = crtc->base.dev;
14604 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014605 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014606
Daniel Vetter24929352012-07-02 20:28:59 +020014607 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014608 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014609 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14610
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014611 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014612 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014613 if (crtc->active) {
14614 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014615 drm_crtc_vblank_on(&crtc->base);
14616 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014617
Daniel Vetter24929352012-07-02 20:28:59 +020014618 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014619 * disable the crtc (and hence change the state) if it is wrong. Note
14620 * that gen4+ has a fixed plane -> pipe mapping. */
14621 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014622 struct intel_connector *connector;
14623 bool plane;
14624
Daniel Vetter24929352012-07-02 20:28:59 +020014625 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14626 crtc->base.base.id);
14627
14628 /* Pipe has the wrong plane attached and the plane is active.
14629 * Temporarily change the plane mapping and disable everything
14630 * ... */
14631 plane = crtc->plane;
14632 crtc->plane = !plane;
Daniel Vetter9c8958b2014-07-14 19:35:31 +020014633 crtc->primary_enabled = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014634 dev_priv->display.crtc_disable(&crtc->base);
14635 crtc->plane = plane;
14636
14637 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014638 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014639 if (connector->encoder->base.crtc != &crtc->base)
14640 continue;
14641
Egbert Eich7f1950f2014-04-25 10:56:22 +020014642 connector->base.dpms = DRM_MODE_DPMS_OFF;
14643 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014644 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014645 /* multiple connectors may have the same encoder:
14646 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014647 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014648 if (connector->encoder->base.crtc == &crtc->base) {
14649 connector->encoder->base.crtc = NULL;
14650 connector->encoder->connectors_active = false;
14651 }
Daniel Vetter24929352012-07-02 20:28:59 +020014652
14653 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014654 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014655 crtc->base.enabled = false;
14656 }
Daniel Vetter24929352012-07-02 20:28:59 +020014657
Daniel Vetter7fad7982012-07-04 17:51:47 +020014658 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14659 crtc->pipe == PIPE_A && !crtc->active) {
14660 /* BIOS forgot to enable pipe A, this mostly happens after
14661 * resume. Force-enable the pipe to fix this, the update_dpms
14662 * call below we restore the pipe to the right state, but leave
14663 * the required bits on. */
14664 intel_enable_pipe_a(dev);
14665 }
14666
Daniel Vetter24929352012-07-02 20:28:59 +020014667 /* Adjust the state of the output pipe according to whether we
14668 * have active connectors/encoders. */
14669 intel_crtc_update_dpms(&crtc->base);
14670
Matt Roper83d65732015-02-25 13:12:16 -080014671 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014672 struct intel_encoder *encoder;
14673
14674 /* This can happen either due to bugs in the get_hw_state
14675 * functions or because the pipe is force-enabled due to the
14676 * pipe A quirk. */
14677 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14678 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014679 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014680 crtc->active ? "enabled" : "disabled");
14681
Matt Roper83d65732015-02-25 13:12:16 -080014682 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014683 crtc->base.enabled = crtc->active;
14684
14685 /* Because we only establish the connector -> encoder ->
14686 * crtc links if something is active, this means the
14687 * crtc is now deactivated. Break the links. connector
14688 * -> encoder links are only establish when things are
14689 * actually up, hence no need to break them. */
14690 WARN_ON(crtc->active);
14691
14692 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14693 WARN_ON(encoder->connectors_active);
14694 encoder->base.crtc = NULL;
14695 }
14696 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014697
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014698 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014699 /*
14700 * We start out with underrun reporting disabled to avoid races.
14701 * For correct bookkeeping mark this on active crtcs.
14702 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014703 * Also on gmch platforms we dont have any hardware bits to
14704 * disable the underrun reporting. Which means we need to start
14705 * out with underrun reporting disabled also on inactive pipes,
14706 * since otherwise we'll complain about the garbage we read when
14707 * e.g. coming up after runtime pm.
14708 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014709 * No protection against concurrent access is required - at
14710 * worst a fifo underrun happens which also sets this to false.
14711 */
14712 crtc->cpu_fifo_underrun_disabled = true;
14713 crtc->pch_fifo_underrun_disabled = true;
14714 }
Daniel Vetter24929352012-07-02 20:28:59 +020014715}
14716
14717static void intel_sanitize_encoder(struct intel_encoder *encoder)
14718{
14719 struct intel_connector *connector;
14720 struct drm_device *dev = encoder->base.dev;
14721
14722 /* We need to check both for a crtc link (meaning that the
14723 * encoder is active and trying to read from a pipe) and the
14724 * pipe itself being active. */
14725 bool has_active_crtc = encoder->base.crtc &&
14726 to_intel_crtc(encoder->base.crtc)->active;
14727
14728 if (encoder->connectors_active && !has_active_crtc) {
14729 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14730 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014731 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014732
14733 /* Connector is active, but has no active pipe. This is
14734 * fallout from our resume register restoring. Disable
14735 * the encoder manually again. */
14736 if (encoder->base.crtc) {
14737 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14738 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014739 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014740 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014741 if (encoder->post_disable)
14742 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014743 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014744 encoder->base.crtc = NULL;
14745 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014746
14747 /* Inconsistent output/port/pipe state happens presumably due to
14748 * a bug in one of the get_hw_state functions. Or someplace else
14749 * in our code, like the register restore mess on resume. Clamp
14750 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014751 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014752 if (connector->encoder != encoder)
14753 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014754 connector->base.dpms = DRM_MODE_DPMS_OFF;
14755 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014756 }
14757 }
14758 /* Enabled encoders without active connectors will be fixed in
14759 * the crtc fixup. */
14760}
14761
Imre Deak04098752014-02-18 00:02:16 +020014762void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014763{
14764 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014765 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014766
Imre Deak04098752014-02-18 00:02:16 +020014767 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14768 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14769 i915_disable_vga(dev);
14770 }
14771}
14772
14773void i915_redisable_vga(struct drm_device *dev)
14774{
14775 struct drm_i915_private *dev_priv = dev->dev_private;
14776
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014777 /* This function can be called both from intel_modeset_setup_hw_state or
14778 * at a very early point in our resume sequence, where the power well
14779 * structures are not yet restored. Since this function is at a very
14780 * paranoid "someone might have enabled VGA while we were not looking"
14781 * level, just check if the power well is enabled instead of trying to
14782 * follow the "don't touch the power well if we don't need it" policy
14783 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014784 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014785 return;
14786
Imre Deak04098752014-02-18 00:02:16 +020014787 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014788}
14789
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014790static bool primary_get_hw_state(struct intel_crtc *crtc)
14791{
14792 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14793
14794 if (!crtc->active)
14795 return false;
14796
14797 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14798}
14799
Daniel Vetter30e984d2013-06-05 13:34:17 +020014800static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014801{
14802 struct drm_i915_private *dev_priv = dev->dev_private;
14803 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014804 struct intel_crtc *crtc;
14805 struct intel_encoder *encoder;
14806 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014807 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014808
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014809 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014810 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014811
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014812 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014813
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014814 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014815 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014816
Matt Roper83d65732015-02-25 13:12:16 -080014817 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014818 crtc->base.enabled = crtc->active;
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014819 crtc->primary_enabled = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014820
14821 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14822 crtc->base.base.id,
14823 crtc->active ? "enabled" : "disabled");
14824 }
14825
Daniel Vetter53589012013-06-05 13:34:16 +020014826 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14827 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14828
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014829 pll->on = pll->get_hw_state(dev_priv, pll,
14830 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014831 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014832 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014833 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014834 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020014835 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014836 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014837 }
Daniel Vetter53589012013-06-05 13:34:16 +020014838 }
Daniel Vetter53589012013-06-05 13:34:16 +020014839
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020014840 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014841 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014842
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014843 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030014844 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020014845 }
14846
Damien Lespiaub2784e12014-08-05 11:29:37 +010014847 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014848 pipe = 0;
14849
14850 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070014851 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14852 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014853 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014854 } else {
14855 encoder->base.crtc = NULL;
14856 }
14857
14858 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014859 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020014860 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014861 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014862 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010014863 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020014864 }
14865
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014866 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014867 if (connector->get_hw_state(connector)) {
14868 connector->base.dpms = DRM_MODE_DPMS_ON;
14869 connector->encoder->connectors_active = true;
14870 connector->base.encoder = &connector->encoder->base;
14871 } else {
14872 connector->base.dpms = DRM_MODE_DPMS_OFF;
14873 connector->base.encoder = NULL;
14874 }
14875 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
14876 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030014877 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020014878 connector->base.encoder ? "enabled" : "disabled");
14879 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020014880}
14881
14882/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
14883 * and i915 state tracking structures. */
14884void intel_modeset_setup_hw_state(struct drm_device *dev,
14885 bool force_restore)
14886{
14887 struct drm_i915_private *dev_priv = dev->dev_private;
14888 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014889 struct intel_crtc *crtc;
14890 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020014891 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020014892
14893 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020014894
Jesse Barnesbabea612013-06-26 18:57:38 +030014895 /*
14896 * Now that we have the config, copy it to each CRTC struct
14897 * Note that this could go away if we move to using crtc_config
14898 * checking everywhere.
14899 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014900 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020014901 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014902 intel_mode_from_pipe_config(&crtc->base.mode,
14903 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030014904 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
14905 crtc->base.base.id);
14906 drm_mode_debug_printmodeline(&crtc->base.mode);
14907 }
14908 }
14909
Daniel Vetter24929352012-07-02 20:28:59 +020014910 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010014911 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020014912 intel_sanitize_encoder(encoder);
14913 }
14914
Damien Lespiau055e3932014-08-18 13:49:10 +010014915 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020014916 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
14917 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014918 intel_dump_pipe_config(crtc, crtc->config,
14919 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020014920 }
Daniel Vetter9a935852012-07-05 22:34:27 +020014921
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020014922 intel_modeset_update_connector_atomic_state(dev);
14923
Daniel Vetter35c95372013-07-17 06:55:04 +020014924 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14925 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14926
14927 if (!pll->on || pll->active)
14928 continue;
14929
14930 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
14931
14932 pll->disable(dev_priv, pll);
14933 pll->on = false;
14934 }
14935
Pradeep Bhat30789992014-11-04 17:06:45 +000014936 if (IS_GEN9(dev))
14937 skl_wm_get_hw_state(dev);
14938 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030014939 ilk_wm_get_hw_state(dev);
14940
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014941 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030014942 i915_redisable_vga(dev);
14943
Daniel Vetterf30da182013-04-11 20:22:50 +020014944 /*
14945 * We need to use raw interfaces for restoring state to avoid
14946 * checking (bogus) intermediate states.
14947 */
Damien Lespiau055e3932014-08-18 13:49:10 +010014948 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070014949 struct drm_crtc *crtc =
14950 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020014951
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020014952 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010014953 }
14954 } else {
14955 intel_modeset_update_staged_output_state(dev);
14956 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020014957
14958 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010014959}
14960
14961void intel_modeset_gem_init(struct drm_device *dev)
14962{
Jesse Barnes92122782014-10-09 12:57:42 -070014963 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014964 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070014965 struct drm_i915_gem_object *obj;
Jesse Barnes484b41d2014-03-07 08:57:55 -080014966
Imre Deakae484342014-03-31 15:10:44 +030014967 mutex_lock(&dev->struct_mutex);
14968 intel_init_gt_powersave(dev);
14969 mutex_unlock(&dev->struct_mutex);
14970
Jesse Barnes92122782014-10-09 12:57:42 -070014971 /*
14972 * There may be no VBT; and if the BIOS enabled SSC we can
14973 * just keep using it to avoid unnecessary flicker. Whereas if the
14974 * BIOS isn't using it, don't assume it will work even if the VBT
14975 * indicates as much.
14976 */
14977 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
14978 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
14979 DREF_SSC1_ENABLE);
14980
Chris Wilson1833b132012-05-09 11:56:28 +010014981 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020014982
14983 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080014984
14985 /*
14986 * Make sure any fbs we allocated at startup are properly
14987 * pinned & fenced. When we do the allocation it's too early
14988 * for this.
14989 */
14990 mutex_lock(&dev->struct_mutex);
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010014991 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070014992 obj = intel_fb_obj(c->primary->fb);
14993 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080014994 continue;
14995
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014996 if (intel_pin_and_fence_fb_obj(c->primary,
14997 c->primary->fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000014998 c->primary->state,
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +000014999 NULL)) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015000 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15001 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015002 drm_framebuffer_unreference(c->primary->fb);
15003 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015004 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015005 }
15006 }
15007 mutex_unlock(&dev->struct_mutex);
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015008
15009 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015010}
15011
Imre Deak4932e2c2014-02-11 17:12:48 +020015012void intel_connector_unregister(struct intel_connector *intel_connector)
15013{
15014 struct drm_connector *connector = &intel_connector->base;
15015
15016 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015017 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015018}
15019
Jesse Barnes79e53942008-11-07 14:24:08 -080015020void intel_modeset_cleanup(struct drm_device *dev)
15021{
Jesse Barnes652c3932009-08-17 13:31:43 -070015022 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015023 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015024
Imre Deak2eb52522014-11-19 15:30:05 +020015025 intel_disable_gt_powersave(dev);
15026
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015027 intel_backlight_unregister(dev);
15028
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015029 /*
15030 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015031 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015032 * experience fancy races otherwise.
15033 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015034 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015035
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015036 /*
15037 * Due to the hpd irq storm handling the hotplug work can re-arm the
15038 * poll handlers. Hence disable polling after hpd handling is shut down.
15039 */
Keith Packardf87ea762010-10-03 19:36:26 -070015040 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015041
Jesse Barnes652c3932009-08-17 13:31:43 -070015042 mutex_lock(&dev->struct_mutex);
15043
Jesse Barnes723bfd72010-10-07 16:01:13 -070015044 intel_unregister_dsm_handler();
15045
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015046 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015047
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015048 mutex_unlock(&dev->struct_mutex);
15049
Chris Wilson1630fe72011-07-08 12:22:42 +010015050 /* flush any delayed tasks or pending work */
15051 flush_scheduled_work();
15052
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015053 /* destroy the backlight and sysfs files before encoders/connectors */
15054 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015055 struct intel_connector *intel_connector;
15056
15057 intel_connector = to_intel_connector(connector);
15058 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015059 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015060
Jesse Barnes79e53942008-11-07 14:24:08 -080015061 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015062
15063 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015064
15065 mutex_lock(&dev->struct_mutex);
15066 intel_cleanup_gt_powersave(dev);
15067 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015068}
15069
Dave Airlie28d52042009-09-21 14:33:58 +100015070/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015071 * Return which encoder is currently attached for connector.
15072 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015073struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015074{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015075 return &intel_attached_encoder(connector)->base;
15076}
Jesse Barnes79e53942008-11-07 14:24:08 -080015077
Chris Wilsondf0e9242010-09-09 16:20:55 +010015078void intel_connector_attach_encoder(struct intel_connector *connector,
15079 struct intel_encoder *encoder)
15080{
15081 connector->encoder = encoder;
15082 drm_mode_connector_attach_encoder(&connector->base,
15083 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015084}
Dave Airlie28d52042009-09-21 14:33:58 +100015085
15086/*
15087 * set vga decode state - true == enable VGA decode
15088 */
15089int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15090{
15091 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015092 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015093 u16 gmch_ctrl;
15094
Chris Wilson75fa0412014-02-07 18:37:02 -020015095 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15096 DRM_ERROR("failed to read control word\n");
15097 return -EIO;
15098 }
15099
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015100 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15101 return 0;
15102
Dave Airlie28d52042009-09-21 14:33:58 +100015103 if (state)
15104 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15105 else
15106 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015107
15108 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15109 DRM_ERROR("failed to write control word\n");
15110 return -EIO;
15111 }
15112
Dave Airlie28d52042009-09-21 14:33:58 +100015113 return 0;
15114}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015115
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015116struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015117
15118 u32 power_well_driver;
15119
Chris Wilson63b66e52013-08-08 15:12:06 +020015120 int num_transcoders;
15121
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015122 struct intel_cursor_error_state {
15123 u32 control;
15124 u32 position;
15125 u32 base;
15126 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015127 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015128
15129 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015130 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015131 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015132 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015133 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015134
15135 struct intel_plane_error_state {
15136 u32 control;
15137 u32 stride;
15138 u32 size;
15139 u32 pos;
15140 u32 addr;
15141 u32 surface;
15142 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015143 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015144
15145 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015146 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015147 enum transcoder cpu_transcoder;
15148
15149 u32 conf;
15150
15151 u32 htotal;
15152 u32 hblank;
15153 u32 hsync;
15154 u32 vtotal;
15155 u32 vblank;
15156 u32 vsync;
15157 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015158};
15159
15160struct intel_display_error_state *
15161intel_display_capture_error_state(struct drm_device *dev)
15162{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015163 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015164 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015165 int transcoders[] = {
15166 TRANSCODER_A,
15167 TRANSCODER_B,
15168 TRANSCODER_C,
15169 TRANSCODER_EDP,
15170 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015171 int i;
15172
Chris Wilson63b66e52013-08-08 15:12:06 +020015173 if (INTEL_INFO(dev)->num_pipes == 0)
15174 return NULL;
15175
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015176 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015177 if (error == NULL)
15178 return NULL;
15179
Imre Deak190be112013-11-25 17:15:31 +020015180 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015181 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15182
Damien Lespiau055e3932014-08-18 13:49:10 +010015183 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015184 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015185 __intel_display_power_is_enabled(dev_priv,
15186 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015187 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015188 continue;
15189
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015190 error->cursor[i].control = I915_READ(CURCNTR(i));
15191 error->cursor[i].position = I915_READ(CURPOS(i));
15192 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015193
15194 error->plane[i].control = I915_READ(DSPCNTR(i));
15195 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015196 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015197 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015198 error->plane[i].pos = I915_READ(DSPPOS(i));
15199 }
Paulo Zanonica291362013-03-06 20:03:14 -030015200 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15201 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015202 if (INTEL_INFO(dev)->gen >= 4) {
15203 error->plane[i].surface = I915_READ(DSPSURF(i));
15204 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15205 }
15206
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015207 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015208
Sonika Jindal3abfce72014-07-21 15:23:43 +053015209 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015210 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015211 }
15212
15213 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15214 if (HAS_DDI(dev_priv->dev))
15215 error->num_transcoders++; /* Account for eDP. */
15216
15217 for (i = 0; i < error->num_transcoders; i++) {
15218 enum transcoder cpu_transcoder = transcoders[i];
15219
Imre Deakddf9c532013-11-27 22:02:02 +020015220 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015221 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015222 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015223 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015224 continue;
15225
Chris Wilson63b66e52013-08-08 15:12:06 +020015226 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15227
15228 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15229 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15230 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15231 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15232 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15233 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15234 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015235 }
15236
15237 return error;
15238}
15239
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015240#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15241
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015242void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015243intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015244 struct drm_device *dev,
15245 struct intel_display_error_state *error)
15246{
Damien Lespiau055e3932014-08-18 13:49:10 +010015247 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015248 int i;
15249
Chris Wilson63b66e52013-08-08 15:12:06 +020015250 if (!error)
15251 return;
15252
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015253 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015254 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015255 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015256 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015257 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015258 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015259 err_printf(m, " Power: %s\n",
15260 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015261 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015262 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015263
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015264 err_printf(m, "Plane [%d]:\n", i);
15265 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15266 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015267 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015268 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15269 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015270 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015271 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015272 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015273 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015274 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15275 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015276 }
15277
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015278 err_printf(m, "Cursor [%d]:\n", i);
15279 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15280 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15281 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015282 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015283
15284 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015285 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015286 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015287 err_printf(m, " Power: %s\n",
15288 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015289 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15290 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15291 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15292 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15293 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15294 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15295 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15296 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015297}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015298
15299void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15300{
15301 struct intel_crtc *crtc;
15302
15303 for_each_intel_crtc(dev, crtc) {
15304 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015305
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015306 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015307
15308 work = crtc->unpin_work;
15309
15310 if (work && work->event &&
15311 work->event->base.file_priv == file) {
15312 kfree(work->event);
15313 work->event = NULL;
15314 }
15315
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015316 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015317 }
15318}