blob: a912934e65a2de359a1c9a169b8a136d2797b55b [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 */
Imre Deak50360402015-01-16 00:55:16 -08002134 if (HAS_GMCH_DISPLAY(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);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002239 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002240
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002241 dev_priv->display.update_primary_plane(crtc, plane->fb,
2242 crtc->x, crtc->y);
Ville Syrjälä33c3b0d2014-06-24 13:59:28 +03002243
2244 /*
2245 * BDW signals flip done immediately if the plane
2246 * is disabled, even if the plane enable is already
2247 * armed to occur at the next vblank :(
2248 */
2249 if (IS_BROADWELL(dev))
2250 intel_wait_for_vblank(dev, intel_crtc->pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002251}
2252
Chris Wilson693db182013-03-05 14:52:39 +00002253static bool need_vtd_wa(struct drm_device *dev)
2254{
2255#ifdef CONFIG_INTEL_IOMMU
2256 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2257 return true;
2258#endif
2259 return false;
2260}
2261
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002262unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002263intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2264 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002265{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002266 unsigned int tile_height;
2267 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002268
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002269 switch (fb_format_modifier) {
2270 case DRM_FORMAT_MOD_NONE:
2271 tile_height = 1;
2272 break;
2273 case I915_FORMAT_MOD_X_TILED:
2274 tile_height = IS_GEN2(dev) ? 16 : 8;
2275 break;
2276 case I915_FORMAT_MOD_Y_TILED:
2277 tile_height = 32;
2278 break;
2279 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002280 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2281 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002282 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002283 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002284 tile_height = 64;
2285 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002286 case 2:
2287 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002288 tile_height = 32;
2289 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002290 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002291 tile_height = 16;
2292 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002293 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002294 WARN_ONCE(1,
2295 "128-bit pixels are not supported for display!");
2296 tile_height = 16;
2297 break;
2298 }
2299 break;
2300 default:
2301 MISSING_CASE(fb_format_modifier);
2302 tile_height = 1;
2303 break;
2304 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002305
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002306 return tile_height;
2307}
2308
2309unsigned int
2310intel_fb_align_height(struct drm_device *dev, unsigned int height,
2311 uint32_t pixel_format, uint64_t fb_format_modifier)
2312{
2313 return ALIGN(height, intel_tile_height(dev, pixel_format,
2314 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002315}
2316
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002317static int
2318intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2319 const struct drm_plane_state *plane_state)
2320{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002321 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002322
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002323 *view = i915_ggtt_view_normal;
2324
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002325 if (!plane_state)
2326 return 0;
2327
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002328 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002329 return 0;
2330
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002331 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002332
2333 info->height = fb->height;
2334 info->pixel_format = fb->pixel_format;
2335 info->pitch = fb->pitches[0];
2336 info->fb_modifier = fb->modifier[0];
2337
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002338 return 0;
2339}
2340
Chris Wilson127bd2a2010-07-23 23:32:05 +01002341int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002342intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2343 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002344 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002345 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002346{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002347 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002348 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002349 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002350 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002351 u32 alignment;
2352 int ret;
2353
Matt Roperebcdd392014-07-09 16:22:11 -07002354 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2355
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002356 switch (fb->modifier[0]) {
2357 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002358 if (INTEL_INFO(dev)->gen >= 9)
2359 alignment = 256 * 1024;
2360 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002361 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002362 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002363 alignment = 4 * 1024;
2364 else
2365 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002366 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002367 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002368 if (INTEL_INFO(dev)->gen >= 9)
2369 alignment = 256 * 1024;
2370 else {
2371 /* pin() will align the object as required by fence */
2372 alignment = 0;
2373 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002374 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002375 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002376 case I915_FORMAT_MOD_Yf_TILED:
2377 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2378 "Y tiling bo slipped through, driver bug!\n"))
2379 return -EINVAL;
2380 alignment = 1 * 1024 * 1024;
2381 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002382 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002383 MISSING_CASE(fb->modifier[0]);
2384 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002385 }
2386
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002387 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2388 if (ret)
2389 return ret;
2390
Chris Wilson693db182013-03-05 14:52:39 +00002391 /* Note that the w/a also requires 64 PTE of padding following the
2392 * bo. We currently fill all unused PTE with the shadow page and so
2393 * we should always have valid PTE following the scanout preventing
2394 * the VT-d warning.
2395 */
2396 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2397 alignment = 256 * 1024;
2398
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002399 /*
2400 * Global gtt pte registers are special registers which actually forward
2401 * writes to a chunk of system memory. Which means that there is no risk
2402 * that the register values disappear as soon as we call
2403 * intel_runtime_pm_put(), so it is correct to wrap only the
2404 * pin/unpin/fence and not more.
2405 */
2406 intel_runtime_pm_get(dev_priv);
2407
Chris Wilsonce453d82011-02-21 14:43:56 +00002408 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002409 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002410 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002411 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002412 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002413
2414 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2415 * fence, whereas 965+ only requires a fence if using
2416 * framebuffer compression. For simplicity, we always install
2417 * a fence as the cost is not that onerous.
2418 */
Chris Wilson06d98132012-04-17 15:31:24 +01002419 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002420 if (ret)
2421 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002422
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002423 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002424
Chris Wilsonce453d82011-02-21 14:43:56 +00002425 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002426 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002427 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002428
2429err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002430 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002431err_interruptible:
2432 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002433 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002434 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002435}
2436
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002437static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2438 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002439{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002440 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002441 struct i915_ggtt_view view;
2442 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002443
Matt Roperebcdd392014-07-09 16:22:11 -07002444 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2445
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002446 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2447 WARN_ONCE(ret, "Couldn't get view from plane state!");
2448
Chris Wilson1690e1e2011-12-14 13:57:08 +01002449 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002450 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002451}
2452
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2454 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002455unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2456 unsigned int tiling_mode,
2457 unsigned int cpp,
2458 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002459{
Chris Wilsonbc752862013-02-21 20:04:31 +00002460 if (tiling_mode != I915_TILING_NONE) {
2461 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002462
Chris Wilsonbc752862013-02-21 20:04:31 +00002463 tile_rows = *y / 8;
2464 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002465
Chris Wilsonbc752862013-02-21 20:04:31 +00002466 tiles = *x / (512/cpp);
2467 *x %= 512/cpp;
2468
2469 return tile_rows * pitch * 8 + tiles * 4096;
2470 } else {
2471 unsigned int offset;
2472
2473 offset = *y * pitch + *x * cpp;
2474 *y = 0;
2475 *x = (offset & 4095) / cpp;
2476 return offset & -4096;
2477 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002478}
2479
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002480static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002481{
2482 switch (format) {
2483 case DISPPLANE_8BPP:
2484 return DRM_FORMAT_C8;
2485 case DISPPLANE_BGRX555:
2486 return DRM_FORMAT_XRGB1555;
2487 case DISPPLANE_BGRX565:
2488 return DRM_FORMAT_RGB565;
2489 default:
2490 case DISPPLANE_BGRX888:
2491 return DRM_FORMAT_XRGB8888;
2492 case DISPPLANE_RGBX888:
2493 return DRM_FORMAT_XBGR8888;
2494 case DISPPLANE_BGRX101010:
2495 return DRM_FORMAT_XRGB2101010;
2496 case DISPPLANE_RGBX101010:
2497 return DRM_FORMAT_XBGR2101010;
2498 }
2499}
2500
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002501static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2502{
2503 switch (format) {
2504 case PLANE_CTL_FORMAT_RGB_565:
2505 return DRM_FORMAT_RGB565;
2506 default:
2507 case PLANE_CTL_FORMAT_XRGB_8888:
2508 if (rgb_order) {
2509 if (alpha)
2510 return DRM_FORMAT_ABGR8888;
2511 else
2512 return DRM_FORMAT_XBGR8888;
2513 } else {
2514 if (alpha)
2515 return DRM_FORMAT_ARGB8888;
2516 else
2517 return DRM_FORMAT_XRGB8888;
2518 }
2519 case PLANE_CTL_FORMAT_XRGB_2101010:
2520 if (rgb_order)
2521 return DRM_FORMAT_XBGR2101010;
2522 else
2523 return DRM_FORMAT_XRGB2101010;
2524 }
2525}
2526
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002527static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002528intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2529 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002530{
2531 struct drm_device *dev = crtc->base.dev;
2532 struct drm_i915_gem_object *obj = NULL;
2533 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002534 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002535 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2536 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2537 PAGE_SIZE);
2538
2539 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540
Chris Wilsonff2652e2014-03-10 08:07:02 +00002541 if (plane_config->size == 0)
2542 return false;
2543
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002544 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2545 base_aligned,
2546 base_aligned,
2547 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002548 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002549 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002550
Damien Lespiau49af4492015-01-20 12:51:44 +00002551 obj->tiling_mode = plane_config->tiling;
2552 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002553 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002555 mode_cmd.pixel_format = fb->pixel_format;
2556 mode_cmd.width = fb->width;
2557 mode_cmd.height = fb->height;
2558 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002559 mode_cmd.modifier[0] = fb->modifier[0];
2560 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002561
2562 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002563 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002564 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002565 DRM_DEBUG_KMS("intel fb init failed\n");
2566 goto out_unref_obj;
2567 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002568 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002569
Daniel Vetterf6936e22015-03-26 12:17:05 +01002570 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002571 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002572
2573out_unref_obj:
2574 drm_gem_object_unreference(&obj->base);
2575 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002576 return false;
2577}
2578
Matt Roperafd65eb2015-02-03 13:10:04 -08002579/* Update plane->state->fb to match plane->fb after driver-internal updates */
2580static void
2581update_state_fb(struct drm_plane *plane)
2582{
2583 if (plane->fb == plane->state->fb)
2584 return;
2585
2586 if (plane->state->fb)
2587 drm_framebuffer_unreference(plane->state->fb);
2588 plane->state->fb = plane->fb;
2589 if (plane->state->fb)
2590 drm_framebuffer_reference(plane->state->fb);
2591}
2592
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002593static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002594intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2595 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596{
2597 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002598 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002599 struct drm_crtc *c;
2600 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002601 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002602 struct drm_plane *primary = intel_crtc->base.primary;
2603 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002604
Damien Lespiau2d140302015-02-05 17:22:18 +00002605 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606 return;
2607
Daniel Vetterf6936e22015-03-26 12:17:05 +01002608 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002609 fb = &plane_config->fb->base;
2610 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002611 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002612
Damien Lespiau2d140302015-02-05 17:22:18 +00002613 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002614
2615 /*
2616 * Failed to alloc the obj, check to see if we should share
2617 * an fb with another CRTC instead
2618 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002619 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002620 i = to_intel_crtc(c);
2621
2622 if (c == &intel_crtc->base)
2623 continue;
2624
Matt Roper2ff8fde2014-07-08 07:50:07 -07002625 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002626 continue;
2627
Daniel Vetter88595ac2015-03-26 12:42:24 +01002628 fb = c->primary->fb;
2629 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002630 continue;
2631
Daniel Vetter88595ac2015-03-26 12:42:24 +01002632 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002633 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002634 drm_framebuffer_reference(fb);
2635 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002636 }
2637 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002638
2639 return;
2640
2641valid_fb:
2642 obj = intel_fb_obj(fb);
2643 if (obj->tiling_mode != I915_TILING_NONE)
2644 dev_priv->preserve_bios_swizzle = true;
2645
2646 primary->fb = fb;
2647 primary->state->crtc = &intel_crtc->base;
2648 primary->crtc = &intel_crtc->base;
2649 update_state_fb(primary);
2650 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002651}
2652
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002653static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2654 struct drm_framebuffer *fb,
2655 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002656{
2657 struct drm_device *dev = crtc->dev;
2658 struct drm_i915_private *dev_priv = dev->dev_private;
2659 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002660 struct drm_plane *primary = crtc->primary;
2661 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002662 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002663 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002664 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002665 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002666 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302667 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002668
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002669 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002670 I915_WRITE(reg, 0);
2671 if (INTEL_INFO(dev)->gen >= 4)
2672 I915_WRITE(DSPSURF(plane), 0);
2673 else
2674 I915_WRITE(DSPADDR(plane), 0);
2675 POSTING_READ(reg);
2676 return;
2677 }
2678
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002679 obj = intel_fb_obj(fb);
2680 if (WARN_ON(obj == NULL))
2681 return;
2682
2683 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2684
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002685 dspcntr = DISPPLANE_GAMMA_ENABLE;
2686
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002687 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002688
2689 if (INTEL_INFO(dev)->gen < 4) {
2690 if (intel_crtc->pipe == PIPE_B)
2691 dspcntr |= DISPPLANE_SEL_PIPE_B;
2692
2693 /* pipesrc and dspsize control the size that is scaled from,
2694 * which should always be the user's requested size.
2695 */
2696 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002697 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2698 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002699 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002700 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2701 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002702 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2703 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002704 I915_WRITE(PRIMPOS(plane), 0);
2705 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002706 }
2707
Ville Syrjälä57779d02012-10-31 17:50:14 +02002708 switch (fb->pixel_format) {
2709 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002710 dspcntr |= DISPPLANE_8BPP;
2711 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002712 case DRM_FORMAT_XRGB1555:
2713 case DRM_FORMAT_ARGB1555:
2714 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002715 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002716 case DRM_FORMAT_RGB565:
2717 dspcntr |= DISPPLANE_BGRX565;
2718 break;
2719 case DRM_FORMAT_XRGB8888:
2720 case DRM_FORMAT_ARGB8888:
2721 dspcntr |= DISPPLANE_BGRX888;
2722 break;
2723 case DRM_FORMAT_XBGR8888:
2724 case DRM_FORMAT_ABGR8888:
2725 dspcntr |= DISPPLANE_RGBX888;
2726 break;
2727 case DRM_FORMAT_XRGB2101010:
2728 case DRM_FORMAT_ARGB2101010:
2729 dspcntr |= DISPPLANE_BGRX101010;
2730 break;
2731 case DRM_FORMAT_XBGR2101010:
2732 case DRM_FORMAT_ABGR2101010:
2733 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002734 break;
2735 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002736 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002737 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002738
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002739 if (INTEL_INFO(dev)->gen >= 4 &&
2740 obj->tiling_mode != I915_TILING_NONE)
2741 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002742
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002743 if (IS_G4X(dev))
2744 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2745
Ville Syrjäläb98971272014-08-27 16:51:22 +03002746 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002747
Daniel Vetterc2c75132012-07-05 12:17:30 +02002748 if (INTEL_INFO(dev)->gen >= 4) {
2749 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002750 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002751 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002752 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002753 linear_offset -= intel_crtc->dspaddr_offset;
2754 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002755 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002756 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002757
Matt Roper8e7d6882015-01-21 16:35:41 -08002758 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302759 dspcntr |= DISPPLANE_ROTATE_180;
2760
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002761 x += (intel_crtc->config->pipe_src_w - 1);
2762 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302763
2764 /* Finding the last pixel of the last line of the display
2765 data and adding to linear_offset*/
2766 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002767 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2768 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302769 }
2770
2771 I915_WRITE(reg, dspcntr);
2772
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002773 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002774 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002775 I915_WRITE(DSPSURF(plane),
2776 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002777 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002778 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002779 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002780 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002781 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002782}
2783
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002784static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2785 struct drm_framebuffer *fb,
2786 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002787{
2788 struct drm_device *dev = crtc->dev;
2789 struct drm_i915_private *dev_priv = dev->dev_private;
2790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002791 struct drm_plane *primary = crtc->primary;
2792 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002793 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002794 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002795 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002796 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002797 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302798 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002800 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002801 I915_WRITE(reg, 0);
2802 I915_WRITE(DSPSURF(plane), 0);
2803 POSTING_READ(reg);
2804 return;
2805 }
2806
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002807 obj = intel_fb_obj(fb);
2808 if (WARN_ON(obj == NULL))
2809 return;
2810
2811 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2812
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002813 dspcntr = DISPPLANE_GAMMA_ENABLE;
2814
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002815 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002816
2817 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2818 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2819
Ville Syrjälä57779d02012-10-31 17:50:14 +02002820 switch (fb->pixel_format) {
2821 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822 dspcntr |= DISPPLANE_8BPP;
2823 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002824 case DRM_FORMAT_RGB565:
2825 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002826 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002827 case DRM_FORMAT_XRGB8888:
2828 case DRM_FORMAT_ARGB8888:
2829 dspcntr |= DISPPLANE_BGRX888;
2830 break;
2831 case DRM_FORMAT_XBGR8888:
2832 case DRM_FORMAT_ABGR8888:
2833 dspcntr |= DISPPLANE_RGBX888;
2834 break;
2835 case DRM_FORMAT_XRGB2101010:
2836 case DRM_FORMAT_ARGB2101010:
2837 dspcntr |= DISPPLANE_BGRX101010;
2838 break;
2839 case DRM_FORMAT_XBGR2101010:
2840 case DRM_FORMAT_ABGR2101010:
2841 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002842 break;
2843 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002844 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002845 }
2846
2847 if (obj->tiling_mode != I915_TILING_NONE)
2848 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002849
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002850 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002851 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002852
Ville Syrjäläb98971272014-08-27 16:51:22 +03002853 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002854 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002855 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002856 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002857 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002858 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002859 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302860 dspcntr |= DISPPLANE_ROTATE_180;
2861
2862 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002863 x += (intel_crtc->config->pipe_src_w - 1);
2864 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302865
2866 /* Finding the last pixel of the last line of the display
2867 data and adding to linear_offset*/
2868 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002869 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2870 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302871 }
2872 }
2873
2874 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002875
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002876 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002877 I915_WRITE(DSPSURF(plane),
2878 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002879 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002880 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2881 } else {
2882 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2883 I915_WRITE(DSPLINOFF(plane), linear_offset);
2884 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002885 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002886}
2887
Damien Lespiaub3218032015-02-27 11:15:18 +00002888u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2889 uint32_t pixel_format)
2890{
2891 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2892
2893 /*
2894 * The stride is either expressed as a multiple of 64 bytes
2895 * chunks for linear buffers or in number of tiles for tiled
2896 * buffers.
2897 */
2898 switch (fb_modifier) {
2899 case DRM_FORMAT_MOD_NONE:
2900 return 64;
2901 case I915_FORMAT_MOD_X_TILED:
2902 if (INTEL_INFO(dev)->gen == 2)
2903 return 128;
2904 return 512;
2905 case I915_FORMAT_MOD_Y_TILED:
2906 /* No need to check for old gens and Y tiling since this is
2907 * about the display engine and those will be blocked before
2908 * we get here.
2909 */
2910 return 128;
2911 case I915_FORMAT_MOD_Yf_TILED:
2912 if (bits_per_pixel == 8)
2913 return 64;
2914 else
2915 return 128;
2916 default:
2917 MISSING_CASE(fb_modifier);
2918 return 64;
2919 }
2920}
2921
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002922unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2923 struct drm_i915_gem_object *obj)
2924{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002925 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002926
2927 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002928 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002929
2930 return i915_gem_obj_ggtt_offset_view(obj, view);
2931}
2932
Chandra Kondurua1b22782015-04-07 15:28:45 -07002933/*
2934 * This function detaches (aka. unbinds) unused scalers in hardware
2935 */
2936void skl_detach_scalers(struct intel_crtc *intel_crtc)
2937{
2938 struct drm_device *dev;
2939 struct drm_i915_private *dev_priv;
2940 struct intel_crtc_scaler_state *scaler_state;
2941 int i;
2942
2943 if (!intel_crtc || !intel_crtc->config)
2944 return;
2945
2946 dev = intel_crtc->base.dev;
2947 dev_priv = dev->dev_private;
2948 scaler_state = &intel_crtc->config->scaler_state;
2949
2950 /* loop through and disable scalers that aren't in use */
2951 for (i = 0; i < intel_crtc->num_scalers; i++) {
2952 if (!scaler_state->scalers[i].in_use) {
2953 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2954 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2955 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2956 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2957 intel_crtc->base.base.id, intel_crtc->pipe, i);
2958 }
2959 }
2960}
2961
Chandra Konduru6156a452015-04-27 13:48:39 -07002962u32 skl_plane_ctl_format(uint32_t pixel_format)
2963{
2964 u32 plane_ctl_format = 0;
2965 switch (pixel_format) {
2966 case DRM_FORMAT_RGB565:
2967 plane_ctl_format = PLANE_CTL_FORMAT_RGB_565;
2968 break;
2969 case DRM_FORMAT_XBGR8888:
2970 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
2971 break;
2972 case DRM_FORMAT_XRGB8888:
2973 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888;
2974 break;
2975 /*
2976 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2977 * to be already pre-multiplied. We need to add a knob (or a different
2978 * DRM_FORMAT) for user-space to configure that.
2979 */
2980 case DRM_FORMAT_ABGR8888:
2981 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
2982 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2983 break;
2984 case DRM_FORMAT_ARGB8888:
2985 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_8888 |
2986 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
2987 break;
2988 case DRM_FORMAT_XRGB2101010:
2989 plane_ctl_format = PLANE_CTL_FORMAT_XRGB_2101010;
2990 break;
2991 case DRM_FORMAT_XBGR2101010:
2992 plane_ctl_format = PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
2993 break;
2994 case DRM_FORMAT_YUYV:
2995 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
2996 break;
2997 case DRM_FORMAT_YVYU:
2998 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
2999 break;
3000 case DRM_FORMAT_UYVY:
3001 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
3002 break;
3003 case DRM_FORMAT_VYUY:
3004 plane_ctl_format = PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
3005 break;
3006 default:
3007 BUG();
3008 }
3009 return plane_ctl_format;
3010}
3011
3012u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
3013{
3014 u32 plane_ctl_tiling = 0;
3015 switch (fb_modifier) {
3016 case DRM_FORMAT_MOD_NONE:
3017 break;
3018 case I915_FORMAT_MOD_X_TILED:
3019 plane_ctl_tiling = PLANE_CTL_TILED_X;
3020 break;
3021 case I915_FORMAT_MOD_Y_TILED:
3022 plane_ctl_tiling = PLANE_CTL_TILED_Y;
3023 break;
3024 case I915_FORMAT_MOD_Yf_TILED:
3025 plane_ctl_tiling = PLANE_CTL_TILED_YF;
3026 break;
3027 default:
3028 MISSING_CASE(fb_modifier);
3029 }
3030 return plane_ctl_tiling;
3031}
3032
3033u32 skl_plane_ctl_rotation(unsigned int rotation)
3034{
3035 u32 plane_ctl_rotation = 0;
3036 switch (rotation) {
3037 case BIT(DRM_ROTATE_0):
3038 break;
3039 case BIT(DRM_ROTATE_90):
3040 plane_ctl_rotation = PLANE_CTL_ROTATE_90;
3041 break;
3042 case BIT(DRM_ROTATE_180):
3043 plane_ctl_rotation = PLANE_CTL_ROTATE_180;
3044 break;
3045 case BIT(DRM_ROTATE_270):
3046 plane_ctl_rotation = PLANE_CTL_ROTATE_270;
3047 break;
3048 default:
3049 MISSING_CASE(rotation);
3050 }
3051
3052 return plane_ctl_rotation;
3053}
3054
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055static void skylake_update_primary_plane(struct drm_crtc *crtc,
3056 struct drm_framebuffer *fb,
3057 int x, int y)
3058{
3059 struct drm_device *dev = crtc->dev;
3060 struct drm_i915_private *dev_priv = dev->dev_private;
3061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003062 struct drm_plane *plane = crtc->primary;
3063 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003064 struct drm_i915_gem_object *obj;
3065 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303066 u32 plane_ctl, stride_div, stride;
3067 u32 tile_height, plane_offset, plane_size;
3068 unsigned int rotation;
3069 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003070 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003071 struct intel_crtc_state *crtc_state = intel_crtc->config;
3072 struct intel_plane_state *plane_state;
3073 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3074 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3075 int scaler_id = -1;
3076
Chandra Konduru6156a452015-04-27 13:48:39 -07003077 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003078
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003079 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003080 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3081 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3082 POSTING_READ(PLANE_CTL(pipe, 0));
3083 return;
3084 }
3085
3086 plane_ctl = PLANE_CTL_ENABLE |
3087 PLANE_CTL_PIPE_GAMMA_ENABLE |
3088 PLANE_CTL_PIPE_CSC_ENABLE;
3089
Chandra Konduru6156a452015-04-27 13:48:39 -07003090 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3091 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003092 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303093
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303094 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003095 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003096
Damien Lespiaub3218032015-02-27 11:15:18 +00003097 obj = intel_fb_obj(fb);
3098 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3099 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303100 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3101
Chandra Konduru6156a452015-04-27 13:48:39 -07003102 /*
3103 * FIXME: intel_plane_state->src, dst aren't set when transitional
3104 * update_plane helpers are called from legacy paths.
3105 * Once full atomic crtc is available, below check can be avoided.
3106 */
3107 if (drm_rect_width(&plane_state->src)) {
3108 scaler_id = plane_state->scaler_id;
3109 src_x = plane_state->src.x1 >> 16;
3110 src_y = plane_state->src.y1 >> 16;
3111 src_w = drm_rect_width(&plane_state->src) >> 16;
3112 src_h = drm_rect_height(&plane_state->src) >> 16;
3113 dst_x = plane_state->dst.x1;
3114 dst_y = plane_state->dst.y1;
3115 dst_w = drm_rect_width(&plane_state->dst);
3116 dst_h = drm_rect_height(&plane_state->dst);
3117
3118 WARN_ON(x != src_x || y != src_y);
3119 } else {
3120 src_w = intel_crtc->config->pipe_src_w;
3121 src_h = intel_crtc->config->pipe_src_h;
3122 }
3123
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303124 if (intel_rotation_90_or_270(rotation)) {
3125 /* stride = Surface height in tiles */
3126 tile_height = intel_tile_height(dev, fb->bits_per_pixel,
3127 fb->modifier[0]);
3128 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003129 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303130 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003131 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303132 } else {
3133 stride = fb->pitches[0] / stride_div;
3134 x_offset = x;
3135 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003136 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303137 }
3138 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003139
Damien Lespiau70d21f02013-07-03 21:06:04 +01003140 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303141 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3142 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3143 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003144
3145 if (scaler_id >= 0) {
3146 uint32_t ps_ctrl = 0;
3147
3148 WARN_ON(!dst_w || !dst_h);
3149 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3150 crtc_state->scaler_state.scalers[scaler_id].mode;
3151 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3152 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3153 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3154 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3155 I915_WRITE(PLANE_POS(pipe, 0), 0);
3156 } else {
3157 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3158 }
3159
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003160 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003161
3162 POSTING_READ(PLANE_SURF(pipe, 0));
3163}
3164
Jesse Barnes17638cd2011-06-24 12:19:23 -07003165/* Assume fb object is pinned & idle & fenced and just update base pointers */
3166static int
3167intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3168 int x, int y, enum mode_set_atomic state)
3169{
3170 struct drm_device *dev = crtc->dev;
3171 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003172
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003173 if (dev_priv->display.disable_fbc)
3174 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003175
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003176 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3177
3178 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003179}
3180
Ville Syrjälä75147472014-11-24 18:28:11 +02003181static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003182{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003183 struct drm_crtc *crtc;
3184
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003185 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3187 enum plane plane = intel_crtc->plane;
3188
3189 intel_prepare_page_flip(dev, plane);
3190 intel_finish_page_flip_plane(dev, plane);
3191 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003192}
3193
3194static void intel_update_primary_planes(struct drm_device *dev)
3195{
3196 struct drm_i915_private *dev_priv = dev->dev_private;
3197 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003198
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003199 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3201
Rob Clark51fd3712013-11-19 12:10:12 -05003202 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003203 /*
3204 * FIXME: Once we have proper support for primary planes (and
3205 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003206 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003207 */
Matt Roperf4510a22014-04-01 15:22:40 -07003208 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003209 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003210 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003211 crtc->x,
3212 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003213 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003214 }
3215}
3216
Ville Syrjälä75147472014-11-24 18:28:11 +02003217void intel_prepare_reset(struct drm_device *dev)
3218{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003219 struct drm_i915_private *dev_priv = to_i915(dev);
3220 struct intel_crtc *crtc;
3221
Ville Syrjälä75147472014-11-24 18:28:11 +02003222 /* no reset support for gen2 */
3223 if (IS_GEN2(dev))
3224 return;
3225
3226 /* reset doesn't touch the display */
3227 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3228 return;
3229
3230 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003231
3232 /*
3233 * Disabling the crtcs gracefully seems nicer. Also the
3234 * g33 docs say we should at least disable all the planes.
3235 */
3236 for_each_intel_crtc(dev, crtc) {
3237 if (crtc->active)
3238 dev_priv->display.crtc_disable(&crtc->base);
3239 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003240}
3241
3242void intel_finish_reset(struct drm_device *dev)
3243{
3244 struct drm_i915_private *dev_priv = to_i915(dev);
3245
3246 /*
3247 * Flips in the rings will be nuked by the reset,
3248 * so complete all pending flips so that user space
3249 * will get its events and not get stuck.
3250 */
3251 intel_complete_page_flips(dev);
3252
3253 /* no reset support for gen2 */
3254 if (IS_GEN2(dev))
3255 return;
3256
3257 /* reset doesn't touch the display */
3258 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3259 /*
3260 * Flips in the rings have been nuked by the reset,
3261 * so update the base address of all primary
3262 * planes to the the last fb to make sure we're
3263 * showing the correct fb after a reset.
3264 */
3265 intel_update_primary_planes(dev);
3266 return;
3267 }
3268
3269 /*
3270 * The display has been reset as well,
3271 * so need a full re-initialization.
3272 */
3273 intel_runtime_pm_disable_interrupts(dev_priv);
3274 intel_runtime_pm_enable_interrupts(dev_priv);
3275
3276 intel_modeset_init_hw(dev);
3277
3278 spin_lock_irq(&dev_priv->irq_lock);
3279 if (dev_priv->display.hpd_irq_setup)
3280 dev_priv->display.hpd_irq_setup(dev);
3281 spin_unlock_irq(&dev_priv->irq_lock);
3282
3283 intel_modeset_setup_hw_state(dev, true);
3284
3285 intel_hpd_init(dev_priv);
3286
3287 drm_modeset_unlock_all(dev);
3288}
3289
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05003290static int
Chris Wilson14667a42012-04-03 17:58:35 +01003291intel_finish_fb(struct drm_framebuffer *old_fb)
3292{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003293 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson14667a42012-04-03 17:58:35 +01003294 struct drm_i915_private *dev_priv = obj->base.dev->dev_private;
3295 bool was_interruptible = dev_priv->mm.interruptible;
3296 int ret;
3297
Chris Wilson14667a42012-04-03 17:58:35 +01003298 /* Big Hammer, we also need to ensure that any pending
3299 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3300 * current scanout is retired before unpinning the old
3301 * framebuffer.
3302 *
3303 * This should only fail upon a hung GPU, in which case we
3304 * can safely continue.
3305 */
3306 dev_priv->mm.interruptible = false;
3307 ret = i915_gem_object_finish_gpu(obj);
3308 dev_priv->mm.interruptible = was_interruptible;
3309
3310 return ret;
3311}
3312
Chris Wilson7d5e3792014-03-04 13:15:08 +00003313static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3314{
3315 struct drm_device *dev = crtc->dev;
3316 struct drm_i915_private *dev_priv = dev->dev_private;
3317 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003318 bool pending;
3319
3320 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3321 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3322 return false;
3323
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003324 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003325 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003326 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003327
3328 return pending;
3329}
3330
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003331static void intel_update_pipe_size(struct intel_crtc *crtc)
3332{
3333 struct drm_device *dev = crtc->base.dev;
3334 struct drm_i915_private *dev_priv = dev->dev_private;
3335 const struct drm_display_mode *adjusted_mode;
3336
3337 if (!i915.fastboot)
3338 return;
3339
3340 /*
3341 * Update pipe size and adjust fitter if needed: the reason for this is
3342 * that in compute_mode_changes we check the native mode (not the pfit
3343 * mode) to see if we can flip rather than do a full mode set. In the
3344 * fastboot case, we'll flip, but if we don't update the pipesrc and
3345 * pfit state, we'll end up with a big fb scanned out into the wrong
3346 * sized surface.
3347 *
3348 * To fix this properly, we need to hoist the checks up into
3349 * compute_mode_changes (or above), check the actual pfit state and
3350 * whether the platform allows pfit disable with pipe active, and only
3351 * then update the pipesrc and pfit state, even on the flip path.
3352 */
3353
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003354 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003355
3356 I915_WRITE(PIPESRC(crtc->pipe),
3357 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3358 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003359 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003360 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3361 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003362 I915_WRITE(PF_CTL(crtc->pipe), 0);
3363 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3364 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3365 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003366 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3367 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003368}
3369
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003370static void intel_fdi_normal_train(struct drm_crtc *crtc)
3371{
3372 struct drm_device *dev = crtc->dev;
3373 struct drm_i915_private *dev_priv = dev->dev_private;
3374 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3375 int pipe = intel_crtc->pipe;
3376 u32 reg, temp;
3377
3378 /* enable normal train */
3379 reg = FDI_TX_CTL(pipe);
3380 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003381 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003382 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3383 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003384 } else {
3385 temp &= ~FDI_LINK_TRAIN_NONE;
3386 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003387 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003388 I915_WRITE(reg, temp);
3389
3390 reg = FDI_RX_CTL(pipe);
3391 temp = I915_READ(reg);
3392 if (HAS_PCH_CPT(dev)) {
3393 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3394 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3395 } else {
3396 temp &= ~FDI_LINK_TRAIN_NONE;
3397 temp |= FDI_LINK_TRAIN_NONE;
3398 }
3399 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3400
3401 /* wait one idle pattern time */
3402 POSTING_READ(reg);
3403 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003404
3405 /* IVB wants error correction enabled */
3406 if (IS_IVYBRIDGE(dev))
3407 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3408 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003409}
3410
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003411/* The FDI link training functions for ILK/Ibexpeak. */
3412static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3413{
3414 struct drm_device *dev = crtc->dev;
3415 struct drm_i915_private *dev_priv = dev->dev_private;
3416 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3417 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003419
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003420 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003421 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003422
Adam Jacksone1a44742010-06-25 15:32:14 -04003423 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3424 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003425 reg = FDI_RX_IMR(pipe);
3426 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003427 temp &= ~FDI_RX_SYMBOL_LOCK;
3428 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003429 I915_WRITE(reg, temp);
3430 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003431 udelay(150);
3432
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 reg = FDI_TX_CTL(pipe);
3435 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003436 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003437 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 temp &= ~FDI_LINK_TRAIN_NONE;
3439 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 reg = FDI_RX_CTL(pipe);
3443 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 temp &= ~FDI_LINK_TRAIN_NONE;
3445 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3447
3448 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449 udelay(150);
3450
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003451 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003452 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3453 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3454 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003455
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003457 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003458 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3460
3461 if ((temp & FDI_RX_BIT_LOCK)) {
3462 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 break;
3465 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003466 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003467 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003468 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469
3470 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 reg = FDI_TX_CTL(pipe);
3472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 temp &= ~FDI_LINK_TRAIN_NONE;
3474 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476
Chris Wilson5eddb702010-09-11 13:48:45 +01003477 reg = FDI_RX_CTL(pipe);
3478 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 temp &= ~FDI_LINK_TRAIN_NONE;
3480 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003481 I915_WRITE(reg, temp);
3482
3483 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003484 udelay(150);
3485
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003487 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003488 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3490
3491 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493 DRM_DEBUG_KMS("FDI train 2 done.\n");
3494 break;
3495 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003497 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003498 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499
3500 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003501
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502}
3503
Akshay Joshi0206e352011-08-16 15:34:10 -04003504static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3506 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3507 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3508 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3509};
3510
3511/* The FDI link training functions for SNB/Cougarpoint. */
3512static void gen6_fdi_link_train(struct drm_crtc *crtc)
3513{
3514 struct drm_device *dev = crtc->dev;
3515 struct drm_i915_private *dev_priv = dev->dev_private;
3516 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3517 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003518 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003519
Adam Jacksone1a44742010-06-25 15:32:14 -04003520 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3521 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003522 reg = FDI_RX_IMR(pipe);
3523 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003524 temp &= ~FDI_RX_SYMBOL_LOCK;
3525 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 I915_WRITE(reg, temp);
3527
3528 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003529 udelay(150);
3530
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 reg = FDI_TX_CTL(pipe);
3533 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003534 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003535 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536 temp &= ~FDI_LINK_TRAIN_NONE;
3537 temp |= FDI_LINK_TRAIN_PATTERN_1;
3538 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3539 /* SNB-B */
3540 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542
Daniel Vetterd74cf322012-10-26 10:58:13 +02003543 I915_WRITE(FDI_RX_MISC(pipe),
3544 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3545
Chris Wilson5eddb702010-09-11 13:48:45 +01003546 reg = FDI_RX_CTL(pipe);
3547 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548 if (HAS_PCH_CPT(dev)) {
3549 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3550 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3551 } else {
3552 temp &= ~FDI_LINK_TRAIN_NONE;
3553 temp |= FDI_LINK_TRAIN_PATTERN_1;
3554 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003555 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3556
3557 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003558 udelay(150);
3559
Akshay Joshi0206e352011-08-16 15:34:10 -04003560 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003561 reg = FDI_TX_CTL(pipe);
3562 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3564 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003565 I915_WRITE(reg, temp);
3566
3567 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003568 udelay(500);
3569
Sean Paulfa37d392012-03-02 12:53:39 -05003570 for (retry = 0; retry < 5; retry++) {
3571 reg = FDI_RX_IIR(pipe);
3572 temp = I915_READ(reg);
3573 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3574 if (temp & FDI_RX_BIT_LOCK) {
3575 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3576 DRM_DEBUG_KMS("FDI train 1 done.\n");
3577 break;
3578 }
3579 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003580 }
Sean Paulfa37d392012-03-02 12:53:39 -05003581 if (retry < 5)
3582 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003583 }
3584 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003585 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586
3587 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003588 reg = FDI_TX_CTL(pipe);
3589 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003590 temp &= ~FDI_LINK_TRAIN_NONE;
3591 temp |= FDI_LINK_TRAIN_PATTERN_2;
3592 if (IS_GEN6(dev)) {
3593 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3594 /* SNB-B */
3595 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3596 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003597 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003598
Chris Wilson5eddb702010-09-11 13:48:45 +01003599 reg = FDI_RX_CTL(pipe);
3600 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003601 if (HAS_PCH_CPT(dev)) {
3602 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3603 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3604 } else {
3605 temp &= ~FDI_LINK_TRAIN_NONE;
3606 temp |= FDI_LINK_TRAIN_PATTERN_2;
3607 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003608 I915_WRITE(reg, temp);
3609
3610 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003611 udelay(150);
3612
Akshay Joshi0206e352011-08-16 15:34:10 -04003613 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003614 reg = FDI_TX_CTL(pipe);
3615 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003616 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3617 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003618 I915_WRITE(reg, temp);
3619
3620 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003621 udelay(500);
3622
Sean Paulfa37d392012-03-02 12:53:39 -05003623 for (retry = 0; retry < 5; retry++) {
3624 reg = FDI_RX_IIR(pipe);
3625 temp = I915_READ(reg);
3626 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3627 if (temp & FDI_RX_SYMBOL_LOCK) {
3628 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3629 DRM_DEBUG_KMS("FDI train 2 done.\n");
3630 break;
3631 }
3632 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003633 }
Sean Paulfa37d392012-03-02 12:53:39 -05003634 if (retry < 5)
3635 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003636 }
3637 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003638 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003639
3640 DRM_DEBUG_KMS("FDI train done.\n");
3641}
3642
Jesse Barnes357555c2011-04-28 15:09:55 -07003643/* Manual link training for Ivy Bridge A0 parts */
3644static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3645{
3646 struct drm_device *dev = crtc->dev;
3647 struct drm_i915_private *dev_priv = dev->dev_private;
3648 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3649 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003651
3652 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3653 for train result */
3654 reg = FDI_RX_IMR(pipe);
3655 temp = I915_READ(reg);
3656 temp &= ~FDI_RX_SYMBOL_LOCK;
3657 temp &= ~FDI_RX_BIT_LOCK;
3658 I915_WRITE(reg, temp);
3659
3660 POSTING_READ(reg);
3661 udelay(150);
3662
Daniel Vetter01a415f2012-10-27 15:58:40 +02003663 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3664 I915_READ(FDI_RX_IIR(pipe)));
3665
Jesse Barnes139ccd32013-08-19 11:04:55 -07003666 /* Try each vswing and preemphasis setting twice before moving on */
3667 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3668 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003669 reg = FDI_TX_CTL(pipe);
3670 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003671 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3672 temp &= ~FDI_TX_ENABLE;
3673 I915_WRITE(reg, temp);
3674
3675 reg = FDI_RX_CTL(pipe);
3676 temp = I915_READ(reg);
3677 temp &= ~FDI_LINK_TRAIN_AUTO;
3678 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3679 temp &= ~FDI_RX_ENABLE;
3680 I915_WRITE(reg, temp);
3681
3682 /* enable CPU FDI TX and PCH FDI RX */
3683 reg = FDI_TX_CTL(pipe);
3684 temp = I915_READ(reg);
3685 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003686 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003687 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003688 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003689 temp |= snb_b_fdi_train_param[j/2];
3690 temp |= FDI_COMPOSITE_SYNC;
3691 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3692
3693 I915_WRITE(FDI_RX_MISC(pipe),
3694 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3695
3696 reg = FDI_RX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3699 temp |= FDI_COMPOSITE_SYNC;
3700 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3701
3702 POSTING_READ(reg);
3703 udelay(1); /* should be 0.5us */
3704
3705 for (i = 0; i < 4; i++) {
3706 reg = FDI_RX_IIR(pipe);
3707 temp = I915_READ(reg);
3708 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3709
3710 if (temp & FDI_RX_BIT_LOCK ||
3711 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3712 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3713 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3714 i);
3715 break;
3716 }
3717 udelay(1); /* should be 0.5us */
3718 }
3719 if (i == 4) {
3720 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3721 continue;
3722 }
3723
3724 /* Train 2 */
3725 reg = FDI_TX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3728 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3729 I915_WRITE(reg, temp);
3730
3731 reg = FDI_RX_CTL(pipe);
3732 temp = I915_READ(reg);
3733 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3734 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003735 I915_WRITE(reg, temp);
3736
3737 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003738 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003739
Jesse Barnes139ccd32013-08-19 11:04:55 -07003740 for (i = 0; i < 4; i++) {
3741 reg = FDI_RX_IIR(pipe);
3742 temp = I915_READ(reg);
3743 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003744
Jesse Barnes139ccd32013-08-19 11:04:55 -07003745 if (temp & FDI_RX_SYMBOL_LOCK ||
3746 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3747 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3748 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3749 i);
3750 goto train_done;
3751 }
3752 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003753 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003754 if (i == 4)
3755 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003756 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003757
Jesse Barnes139ccd32013-08-19 11:04:55 -07003758train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003759 DRM_DEBUG_KMS("FDI train done.\n");
3760}
3761
Daniel Vetter88cefb62012-08-12 19:27:14 +02003762static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003763{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003764 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003765 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003766 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003767 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003768
Jesse Barnesc64e3112010-09-10 11:27:03 -07003769
Jesse Barnes0e23b992010-09-10 11:10:00 -07003770 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003771 reg = FDI_RX_CTL(pipe);
3772 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003773 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003774 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003775 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003776 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3777
3778 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003779 udelay(200);
3780
3781 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003782 temp = I915_READ(reg);
3783 I915_WRITE(reg, temp | FDI_PCDCLK);
3784
3785 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003786 udelay(200);
3787
Paulo Zanoni20749732012-11-23 15:30:38 -02003788 /* Enable CPU FDI TX PLL, always on for Ironlake */
3789 reg = FDI_TX_CTL(pipe);
3790 temp = I915_READ(reg);
3791 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3792 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003793
Paulo Zanoni20749732012-11-23 15:30:38 -02003794 POSTING_READ(reg);
3795 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003796 }
3797}
3798
Daniel Vetter88cefb62012-08-12 19:27:14 +02003799static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3800{
3801 struct drm_device *dev = intel_crtc->base.dev;
3802 struct drm_i915_private *dev_priv = dev->dev_private;
3803 int pipe = intel_crtc->pipe;
3804 u32 reg, temp;
3805
3806 /* Switch from PCDclk to Rawclk */
3807 reg = FDI_RX_CTL(pipe);
3808 temp = I915_READ(reg);
3809 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3810
3811 /* Disable CPU FDI TX PLL */
3812 reg = FDI_TX_CTL(pipe);
3813 temp = I915_READ(reg);
3814 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3815
3816 POSTING_READ(reg);
3817 udelay(100);
3818
3819 reg = FDI_RX_CTL(pipe);
3820 temp = I915_READ(reg);
3821 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3822
3823 /* Wait for the clocks to turn off. */
3824 POSTING_READ(reg);
3825 udelay(100);
3826}
3827
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003828static void ironlake_fdi_disable(struct drm_crtc *crtc)
3829{
3830 struct drm_device *dev = crtc->dev;
3831 struct drm_i915_private *dev_priv = dev->dev_private;
3832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3833 int pipe = intel_crtc->pipe;
3834 u32 reg, temp;
3835
3836 /* disable CPU FDI tx and PCH FDI rx */
3837 reg = FDI_TX_CTL(pipe);
3838 temp = I915_READ(reg);
3839 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3840 POSTING_READ(reg);
3841
3842 reg = FDI_RX_CTL(pipe);
3843 temp = I915_READ(reg);
3844 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003845 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003846 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3847
3848 POSTING_READ(reg);
3849 udelay(100);
3850
3851 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003852 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003853 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003854
3855 /* still set train pattern 1 */
3856 reg = FDI_TX_CTL(pipe);
3857 temp = I915_READ(reg);
3858 temp &= ~FDI_LINK_TRAIN_NONE;
3859 temp |= FDI_LINK_TRAIN_PATTERN_1;
3860 I915_WRITE(reg, temp);
3861
3862 reg = FDI_RX_CTL(pipe);
3863 temp = I915_READ(reg);
3864 if (HAS_PCH_CPT(dev)) {
3865 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3866 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3867 } else {
3868 temp &= ~FDI_LINK_TRAIN_NONE;
3869 temp |= FDI_LINK_TRAIN_PATTERN_1;
3870 }
3871 /* BPC in FDI rx is consistent with that in PIPECONF */
3872 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003873 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003874 I915_WRITE(reg, temp);
3875
3876 POSTING_READ(reg);
3877 udelay(100);
3878}
3879
Chris Wilson5dce5b932014-01-20 10:17:36 +00003880bool intel_has_pending_fb_unpin(struct drm_device *dev)
3881{
3882 struct intel_crtc *crtc;
3883
3884 /* Note that we don't need to be called with mode_config.lock here
3885 * as our list of CRTC objects is static for the lifetime of the
3886 * device and so cannot disappear as we iterate. Similarly, we can
3887 * happily treat the predicates as racy, atomic checks as userspace
3888 * cannot claim and pin a new fb without at least acquring the
3889 * struct_mutex and so serialising with us.
3890 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003891 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003892 if (atomic_read(&crtc->unpin_work_count) == 0)
3893 continue;
3894
3895 if (crtc->unpin_work)
3896 intel_wait_for_vblank(dev, crtc->pipe);
3897
3898 return true;
3899 }
3900
3901 return false;
3902}
3903
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003904static void page_flip_completed(struct intel_crtc *intel_crtc)
3905{
3906 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3907 struct intel_unpin_work *work = intel_crtc->unpin_work;
3908
3909 /* ensure that the unpin work is consistent wrt ->pending. */
3910 smp_rmb();
3911 intel_crtc->unpin_work = NULL;
3912
3913 if (work->event)
3914 drm_send_vblank_event(intel_crtc->base.dev,
3915 intel_crtc->pipe,
3916 work->event);
3917
3918 drm_crtc_vblank_put(&intel_crtc->base);
3919
3920 wake_up_all(&dev_priv->pending_flip_queue);
3921 queue_work(dev_priv->wq, &work->work);
3922
3923 trace_i915_flip_complete(intel_crtc->plane,
3924 work->pending_flip_obj);
3925}
3926
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003927void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003928{
Chris Wilson0f911282012-04-17 10:05:38 +01003929 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003930 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003931
Daniel Vetter2c10d572012-12-20 21:24:07 +01003932 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003933 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3934 !intel_crtc_has_pending_flip(crtc),
3935 60*HZ) == 0)) {
3936 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003937
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003938 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003939 if (intel_crtc->unpin_work) {
3940 WARN_ONCE(1, "Removing stuck page flip\n");
3941 page_flip_completed(intel_crtc);
3942 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003943 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003944 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003945
Chris Wilson975d5682014-08-20 13:13:34 +01003946 if (crtc->primary->fb) {
3947 mutex_lock(&dev->struct_mutex);
3948 intel_finish_fb(crtc->primary->fb);
3949 mutex_unlock(&dev->struct_mutex);
3950 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003951}
3952
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953/* Program iCLKIP clock to the desired frequency */
3954static void lpt_program_iclkip(struct drm_crtc *crtc)
3955{
3956 struct drm_device *dev = crtc->dev;
3957 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003958 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003959 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3960 u32 temp;
3961
Daniel Vetter09153002012-12-12 14:06:44 +01003962 mutex_lock(&dev_priv->dpio_lock);
3963
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003964 /* It is necessary to ungate the pixclk gate prior to programming
3965 * the divisors, and gate it back when it is done.
3966 */
3967 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3968
3969 /* Disable SSCCTL */
3970 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003971 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3972 SBI_SSCCTL_DISABLE,
3973 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003974
3975 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003976 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003977 auxdiv = 1;
3978 divsel = 0x41;
3979 phaseinc = 0x20;
3980 } else {
3981 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003982 * but the adjusted_mode->crtc_clock in in KHz. To get the
3983 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003984 * convert the virtual clock precision to KHz here for higher
3985 * precision.
3986 */
3987 u32 iclk_virtual_root_freq = 172800 * 1000;
3988 u32 iclk_pi_range = 64;
3989 u32 desired_divisor, msb_divisor_value, pi_value;
3990
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003991 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003992 msb_divisor_value = desired_divisor / iclk_pi_range;
3993 pi_value = desired_divisor % iclk_pi_range;
3994
3995 auxdiv = 0;
3996 divsel = msb_divisor_value - 2;
3997 phaseinc = pi_value;
3998 }
3999
4000 /* This should not happen with any sane values */
4001 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
4002 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
4003 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
4004 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
4005
4006 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 +03004007 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004008 auxdiv,
4009 divsel,
4010 phasedir,
4011 phaseinc);
4012
4013 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004014 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004015 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4016 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4017 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4018 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4019 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4020 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004021 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004022
4023 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004024 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004025 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4026 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004027 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004028
4029 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004030 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004031 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004032 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004033
4034 /* Wait for initialization time */
4035 udelay(24);
4036
4037 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004038
4039 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004040}
4041
Daniel Vetter275f01b22013-05-03 11:49:47 +02004042static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4043 enum pipe pch_transcoder)
4044{
4045 struct drm_device *dev = crtc->base.dev;
4046 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004047 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004048
4049 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4050 I915_READ(HTOTAL(cpu_transcoder)));
4051 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4052 I915_READ(HBLANK(cpu_transcoder)));
4053 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4054 I915_READ(HSYNC(cpu_transcoder)));
4055
4056 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4057 I915_READ(VTOTAL(cpu_transcoder)));
4058 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4059 I915_READ(VBLANK(cpu_transcoder)));
4060 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4061 I915_READ(VSYNC(cpu_transcoder)));
4062 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4063 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4064}
4065
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004066static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004067{
4068 struct drm_i915_private *dev_priv = dev->dev_private;
4069 uint32_t temp;
4070
4071 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004072 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004073 return;
4074
4075 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4076 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4077
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004078 temp &= ~FDI_BC_BIFURCATION_SELECT;
4079 if (enable)
4080 temp |= FDI_BC_BIFURCATION_SELECT;
4081
4082 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004083 I915_WRITE(SOUTH_CHICKEN1, temp);
4084 POSTING_READ(SOUTH_CHICKEN1);
4085}
4086
4087static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4088{
4089 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004090
4091 switch (intel_crtc->pipe) {
4092 case PIPE_A:
4093 break;
4094 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004095 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004096 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004097 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004098 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004099
4100 break;
4101 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004102 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004103
4104 break;
4105 default:
4106 BUG();
4107 }
4108}
4109
Jesse Barnesf67a5592011-01-05 10:31:48 -08004110/*
4111 * Enable PCH resources required for PCH ports:
4112 * - PCH PLLs
4113 * - FDI training & RX/TX
4114 * - update transcoder timings
4115 * - DP transcoding bits
4116 * - transcoder
4117 */
4118static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004119{
4120 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004121 struct drm_i915_private *dev_priv = dev->dev_private;
4122 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4123 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004124 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004125
Daniel Vetterab9412b2013-05-03 11:49:46 +02004126 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004127
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004128 if (IS_IVYBRIDGE(dev))
4129 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4130
Daniel Vettercd986ab2012-10-26 10:58:12 +02004131 /* Write the TU size bits before fdi link training, so that error
4132 * detection works. */
4133 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4134 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4135
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004136 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004137 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004138
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004139 /* We need to program the right clock selection before writing the pixel
4140 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004141 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004142 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004143
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004144 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004145 temp |= TRANS_DPLL_ENABLE(pipe);
4146 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004147 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004148 temp |= sel;
4149 else
4150 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004151 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004152 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004154 /* XXX: pch pll's can be enabled any time before we enable the PCH
4155 * transcoder, and we actually should do this to not upset any PCH
4156 * transcoder that already use the clock when we share it.
4157 *
4158 * Note that enable_shared_dpll tries to do the right thing, but
4159 * get_shared_dpll unconditionally resets the pll - we need that to have
4160 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004161 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004162
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004163 /* set transcoder timing, panel must allow it */
4164 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004165 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004167 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004168
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004169 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004170 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004171 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004172 reg = TRANS_DP_CTL(pipe);
4173 temp = I915_READ(reg);
4174 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004175 TRANS_DP_SYNC_MASK |
4176 TRANS_DP_BPC_MASK);
Chris Wilson5eddb702010-09-11 13:48:45 +01004177 temp |= (TRANS_DP_OUTPUT_ENABLE |
4178 TRANS_DP_ENH_FRAMING);
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004179 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004180
4181 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004182 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004183 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004184 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004185
4186 switch (intel_trans_dp_port_sel(crtc)) {
4187 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004188 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004189 break;
4190 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004191 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004192 break;
4193 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004194 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004195 break;
4196 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004197 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004198 }
4199
Chris Wilson5eddb702010-09-11 13:48:45 +01004200 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004201 }
4202
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004203 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004204}
4205
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004206static void lpt_pch_enable(struct drm_crtc *crtc)
4207{
4208 struct drm_device *dev = crtc->dev;
4209 struct drm_i915_private *dev_priv = dev->dev_private;
4210 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004211 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004212
Daniel Vetterab9412b2013-05-03 11:49:46 +02004213 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004214
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004215 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004216
Paulo Zanoni0540e482012-10-31 18:12:40 -02004217 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004218 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004219
Paulo Zanoni937bb612012-10-31 18:12:47 -02004220 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004221}
4222
Daniel Vetter716c2e52014-06-25 22:02:02 +03004223void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004224{
Daniel Vettere2b78262013-06-07 23:10:03 +02004225 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004226
4227 if (pll == NULL)
4228 return;
4229
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004230 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004231 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004232 return;
4233 }
4234
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004235 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4236 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004237 WARN_ON(pll->on);
4238 WARN_ON(pll->active);
4239 }
4240
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004241 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004242}
4243
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004244struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4245 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004246{
Daniel Vettere2b78262013-06-07 23:10:03 +02004247 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004248 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004249 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004250
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004251 if (HAS_PCH_IBX(dev_priv->dev)) {
4252 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004253 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004254 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004255
Daniel Vetter46edb022013-06-05 13:34:12 +02004256 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4257 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004258
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004259 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004260
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004261 goto found;
4262 }
4263
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304264 if (IS_BROXTON(dev_priv->dev)) {
4265 /* PLL is attached to port in bxt */
4266 struct intel_encoder *encoder;
4267 struct intel_digital_port *intel_dig_port;
4268
4269 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4270 if (WARN_ON(!encoder))
4271 return NULL;
4272
4273 intel_dig_port = enc_to_dig_port(&encoder->base);
4274 /* 1:1 mapping between ports and PLLs */
4275 i = (enum intel_dpll_id)intel_dig_port->port;
4276 pll = &dev_priv->shared_dplls[i];
4277 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4278 crtc->base.base.id, pll->name);
4279 WARN_ON(pll->new_config->crtc_mask);
4280
4281 goto found;
4282 }
4283
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004284 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4285 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004286
4287 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004288 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004289 continue;
4290
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004291 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004292 &pll->new_config->hw_state,
4293 sizeof(pll->new_config->hw_state)) == 0) {
4294 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004295 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004296 pll->new_config->crtc_mask,
4297 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004298 goto found;
4299 }
4300 }
4301
4302 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004303 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4304 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004305 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004306 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4307 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004308 goto found;
4309 }
4310 }
4311
4312 return NULL;
4313
4314found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004315 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004316 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004317
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004318 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004319 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4320 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004321
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004322 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004323
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004324 return pll;
4325}
4326
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004327/**
4328 * intel_shared_dpll_start_config - start a new PLL staged config
4329 * @dev_priv: DRM device
4330 * @clear_pipes: mask of pipes that will have their PLLs freed
4331 *
4332 * Starts a new PLL staged config, copying the current config but
4333 * releasing the references of pipes specified in clear_pipes.
4334 */
4335static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4336 unsigned clear_pipes)
4337{
4338 struct intel_shared_dpll *pll;
4339 enum intel_dpll_id i;
4340
4341 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4342 pll = &dev_priv->shared_dplls[i];
4343
4344 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4345 GFP_KERNEL);
4346 if (!pll->new_config)
4347 goto cleanup;
4348
4349 pll->new_config->crtc_mask &= ~clear_pipes;
4350 }
4351
4352 return 0;
4353
4354cleanup:
4355 while (--i >= 0) {
4356 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004357 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004358 pll->new_config = NULL;
4359 }
4360
4361 return -ENOMEM;
4362}
4363
4364static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4365{
4366 struct intel_shared_dpll *pll;
4367 enum intel_dpll_id i;
4368
4369 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4370 pll = &dev_priv->shared_dplls[i];
4371
4372 WARN_ON(pll->new_config == &pll->config);
4373
4374 pll->config = *pll->new_config;
4375 kfree(pll->new_config);
4376 pll->new_config = NULL;
4377 }
4378}
4379
4380static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4381{
4382 struct intel_shared_dpll *pll;
4383 enum intel_dpll_id i;
4384
4385 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4386 pll = &dev_priv->shared_dplls[i];
4387
4388 WARN_ON(pll->new_config == &pll->config);
4389
4390 kfree(pll->new_config);
4391 pll->new_config = NULL;
4392 }
4393}
4394
Daniel Vettera1520312013-05-03 11:49:50 +02004395static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004396{
4397 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004398 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004399 u32 temp;
4400
4401 temp = I915_READ(dslreg);
4402 udelay(500);
4403 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004404 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004405 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004406 }
4407}
4408
Chandra Kondurua1b22782015-04-07 15:28:45 -07004409/**
4410 * skl_update_scaler_users - Stages update to crtc's scaler state
4411 * @intel_crtc: crtc
4412 * @crtc_state: crtc_state
4413 * @plane: plane (NULL indicates crtc is requesting update)
4414 * @plane_state: plane's state
4415 * @force_detach: request unconditional detachment of scaler
4416 *
4417 * This function updates scaler state for requested plane or crtc.
4418 * To request scaler usage update for a plane, caller shall pass plane pointer.
4419 * To request scaler usage update for crtc, caller shall pass plane pointer
4420 * as NULL.
4421 *
4422 * Return
4423 * 0 - scaler_usage updated successfully
4424 * error - requested scaling cannot be supported or other error condition
4425 */
4426int
4427skl_update_scaler_users(
4428 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4429 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4430 int force_detach)
4431{
4432 int need_scaling;
4433 int idx;
4434 int src_w, src_h, dst_w, dst_h;
4435 int *scaler_id;
4436 struct drm_framebuffer *fb;
4437 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004438 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004439
4440 if (!intel_crtc || !crtc_state)
4441 return 0;
4442
4443 scaler_state = &crtc_state->scaler_state;
4444
4445 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4446 fb = intel_plane ? plane_state->base.fb : NULL;
4447
4448 if (intel_plane) {
4449 src_w = drm_rect_width(&plane_state->src) >> 16;
4450 src_h = drm_rect_height(&plane_state->src) >> 16;
4451 dst_w = drm_rect_width(&plane_state->dst);
4452 dst_h = drm_rect_height(&plane_state->dst);
4453 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004454 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004455 } else {
4456 struct drm_display_mode *adjusted_mode =
4457 &crtc_state->base.adjusted_mode;
4458 src_w = crtc_state->pipe_src_w;
4459 src_h = crtc_state->pipe_src_h;
4460 dst_w = adjusted_mode->hdisplay;
4461 dst_h = adjusted_mode->vdisplay;
4462 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004463 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004464 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004465
4466 need_scaling = intel_rotation_90_or_270(rotation) ?
4467 (src_h != dst_w || src_w != dst_h):
4468 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004469
4470 /*
4471 * if plane is being disabled or scaler is no more required or force detach
4472 * - free scaler binded to this plane/crtc
4473 * - in order to do this, update crtc->scaler_usage
4474 *
4475 * Here scaler state in crtc_state is set free so that
4476 * scaler can be assigned to other user. Actual register
4477 * update to free the scaler is done in plane/panel-fit programming.
4478 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4479 */
4480 if (force_detach || !need_scaling || (intel_plane &&
4481 (!fb || !plane_state->visible))) {
4482 if (*scaler_id >= 0) {
4483 scaler_state->scaler_users &= ~(1 << idx);
4484 scaler_state->scalers[*scaler_id].in_use = 0;
4485
4486 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4487 "crtc_state = %p scaler_users = 0x%x\n",
4488 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4489 intel_plane ? intel_plane->base.base.id :
4490 intel_crtc->base.base.id, crtc_state,
4491 scaler_state->scaler_users);
4492 *scaler_id = -1;
4493 }
4494 return 0;
4495 }
4496
4497 /* range checks */
4498 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4499 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4500
4501 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4502 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4503 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4504 "size is out of scaler range\n",
4505 intel_plane ? "PLANE" : "CRTC",
4506 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4507 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4508 return -EINVAL;
4509 }
4510
4511 /* check colorkey */
4512 if (intel_plane && intel_plane->ckey.flags != I915_SET_COLORKEY_NONE) {
4513 DRM_DEBUG_KMS("PLANE:%d scaling with color key not allowed",
4514 intel_plane->base.base.id);
4515 return -EINVAL;
4516 }
4517
4518 /* Check src format */
4519 if (intel_plane) {
4520 switch (fb->pixel_format) {
4521 case DRM_FORMAT_RGB565:
4522 case DRM_FORMAT_XBGR8888:
4523 case DRM_FORMAT_XRGB8888:
4524 case DRM_FORMAT_ABGR8888:
4525 case DRM_FORMAT_ARGB8888:
4526 case DRM_FORMAT_XRGB2101010:
4527 case DRM_FORMAT_ARGB2101010:
4528 case DRM_FORMAT_XBGR2101010:
4529 case DRM_FORMAT_ABGR2101010:
4530 case DRM_FORMAT_YUYV:
4531 case DRM_FORMAT_YVYU:
4532 case DRM_FORMAT_UYVY:
4533 case DRM_FORMAT_VYUY:
4534 break;
4535 default:
4536 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4537 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4538 return -EINVAL;
4539 }
4540 }
4541
4542 /* mark this plane as a scaler user in crtc_state */
4543 scaler_state->scaler_users |= (1 << idx);
4544 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4545 "crtc_state = %p scaler_users = 0x%x\n",
4546 intel_plane ? "PLANE" : "CRTC",
4547 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4548 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4549 return 0;
4550}
4551
4552static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004553{
4554 struct drm_device *dev = crtc->base.dev;
4555 struct drm_i915_private *dev_priv = dev->dev_private;
4556 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004557 struct intel_crtc_scaler_state *scaler_state =
4558 &crtc->config->scaler_state;
4559
4560 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4561
4562 /* To update pfit, first update scaler state */
4563 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4564 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4565 skl_detach_scalers(crtc);
4566 if (!enable)
4567 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004568
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004569 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004570 int id;
4571
4572 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4573 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4574 return;
4575 }
4576
4577 id = scaler_state->scaler_id;
4578 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4579 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4580 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4581 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4582
4583 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004584 }
4585}
4586
Jesse Barnesb074cec2013-04-25 12:55:02 -07004587static void ironlake_pfit_enable(struct intel_crtc *crtc)
4588{
4589 struct drm_device *dev = crtc->base.dev;
4590 struct drm_i915_private *dev_priv = dev->dev_private;
4591 int pipe = crtc->pipe;
4592
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004593 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004594 /* Force use of hard-coded filter coefficients
4595 * as some pre-programmed values are broken,
4596 * e.g. x201.
4597 */
4598 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4599 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4600 PF_PIPE_SEL_IVB(pipe));
4601 else
4602 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004603 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4604 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004605 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004606}
4607
Matt Roper4a3b8762014-12-23 10:41:51 -08004608static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004609{
4610 struct drm_device *dev = crtc->dev;
4611 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004612 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004613 struct intel_plane *intel_plane;
4614
Matt Roperaf2b6532014-04-01 15:22:32 -07004615 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4616 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004617 if (intel_plane->pipe == pipe)
4618 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004619 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004620}
4621
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004622void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004623{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004624 struct drm_device *dev = crtc->base.dev;
4625 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004626
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004627 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004628 return;
4629
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004630 /* We can only enable IPS after we enable a plane and wait for a vblank */
4631 intel_wait_for_vblank(dev, crtc->pipe);
4632
Paulo Zanonid77e4532013-09-24 13:52:55 -03004633 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004634 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004635 mutex_lock(&dev_priv->rps.hw_lock);
4636 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4637 mutex_unlock(&dev_priv->rps.hw_lock);
4638 /* Quoting Art Runyan: "its not safe to expect any particular
4639 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004640 * mailbox." Moreover, the mailbox may return a bogus state,
4641 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004642 */
4643 } else {
4644 I915_WRITE(IPS_CTL, IPS_ENABLE);
4645 /* The bit only becomes 1 in the next vblank, so this wait here
4646 * is essentially intel_wait_for_vblank. If we don't have this
4647 * and don't wait for vblanks until the end of crtc_enable, then
4648 * the HW state readout code will complain that the expected
4649 * IPS_CTL value is not the one we read. */
4650 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4651 DRM_ERROR("Timed out waiting for IPS enable\n");
4652 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004653}
4654
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004655void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004656{
4657 struct drm_device *dev = crtc->base.dev;
4658 struct drm_i915_private *dev_priv = dev->dev_private;
4659
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004660 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004661 return;
4662
4663 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004664 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004665 mutex_lock(&dev_priv->rps.hw_lock);
4666 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4667 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004668 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4669 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4670 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004671 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004672 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004673 POSTING_READ(IPS_CTL);
4674 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004675
4676 /* We need to wait for a vblank before we can disable the plane. */
4677 intel_wait_for_vblank(dev, crtc->pipe);
4678}
4679
4680/** Loads the palette/gamma unit for the CRTC with the prepared values */
4681static void intel_crtc_load_lut(struct drm_crtc *crtc)
4682{
4683 struct drm_device *dev = crtc->dev;
4684 struct drm_i915_private *dev_priv = dev->dev_private;
4685 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4686 enum pipe pipe = intel_crtc->pipe;
4687 int palreg = PALETTE(pipe);
4688 int i;
4689 bool reenable_ips = false;
4690
4691 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004692 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004693 return;
4694
Imre Deak50360402015-01-16 00:55:16 -08004695 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004696 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004697 assert_dsi_pll_enabled(dev_priv);
4698 else
4699 assert_pll_enabled(dev_priv, pipe);
4700 }
4701
4702 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304703 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004704 palreg = LGC_PALETTE(pipe);
4705
4706 /* Workaround : Do not read or write the pipe palette/gamma data while
4707 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4708 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004709 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004710 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4711 GAMMA_MODE_MODE_SPLIT)) {
4712 hsw_disable_ips(intel_crtc);
4713 reenable_ips = true;
4714 }
4715
4716 for (i = 0; i < 256; i++) {
4717 I915_WRITE(palreg + 4 * i,
4718 (intel_crtc->lut_r[i] << 16) |
4719 (intel_crtc->lut_g[i] << 8) |
4720 intel_crtc->lut_b[i]);
4721 }
4722
4723 if (reenable_ips)
4724 hsw_enable_ips(intel_crtc);
4725}
4726
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004727static void intel_crtc_dpms_overlay(struct intel_crtc *intel_crtc, bool enable)
4728{
4729 if (!enable && intel_crtc->overlay) {
4730 struct drm_device *dev = intel_crtc->base.dev;
4731 struct drm_i915_private *dev_priv = dev->dev_private;
4732
4733 mutex_lock(&dev->struct_mutex);
4734 dev_priv->mm.interruptible = false;
4735 (void) intel_overlay_switch_off(intel_crtc->overlay);
4736 dev_priv->mm.interruptible = true;
4737 mutex_unlock(&dev->struct_mutex);
4738 }
4739
4740 /* Let userspace switch the overlay on again. In most cases userspace
4741 * has to recompute where to put it anyway.
4742 */
4743}
4744
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004745static void intel_crtc_enable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004746{
4747 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004748 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4749 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004750
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03004751 intel_enable_primary_hw_plane(crtc->primary, crtc);
Matt Roper4a3b8762014-12-23 10:41:51 -08004752 intel_enable_sprite_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004753 intel_crtc_update_cursor(crtc, true);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004754 intel_crtc_dpms_overlay(intel_crtc, true);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004755
4756 hsw_enable_ips(intel_crtc);
4757
4758 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004759 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004760 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004761
4762 /*
4763 * FIXME: Once we grow proper nuclear flip support out of this we need
4764 * to compute the mask of flip planes precisely. For the time being
4765 * consider this a flip from a NULL plane.
4766 */
4767 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004768}
4769
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004770static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004771{
4772 struct drm_device *dev = crtc->dev;
4773 struct drm_i915_private *dev_priv = dev->dev_private;
4774 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004775 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004776 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004777
4778 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004779
Paulo Zanonie35fef22015-02-09 14:46:29 -02004780 if (dev_priv->fbc.crtc == intel_crtc)
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004781 intel_fbc_disable(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004782
4783 hsw_disable_ips(intel_crtc);
4784
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004785 intel_crtc_dpms_overlay(intel_crtc, false);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004786 for_each_intel_plane(dev, intel_plane) {
4787 if (intel_plane->pipe == pipe) {
4788 struct drm_crtc *from = intel_plane->base.crtc;
4789
4790 intel_plane->disable_plane(&intel_plane->base,
4791 from ?: crtc, true);
4792 }
4793 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004794
Daniel Vetterf99d7062014-06-19 16:01:59 +02004795 /*
4796 * FIXME: Once we grow proper nuclear flip support out of this we need
4797 * to compute the mask of flip planes precisely. For the time being
4798 * consider this a flip to a NULL plane.
4799 */
4800 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004801}
4802
Jesse Barnesf67a5592011-01-05 10:31:48 -08004803static void ironlake_crtc_enable(struct drm_crtc *crtc)
4804{
4805 struct drm_device *dev = crtc->dev;
4806 struct drm_i915_private *dev_priv = dev->dev_private;
4807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004808 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004809 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004810
Matt Roper83d65732015-02-25 13:12:16 -08004811 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004812
Jesse Barnesf67a5592011-01-05 10:31:48 -08004813 if (intel_crtc->active)
4814 return;
4815
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004816 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004817 intel_prepare_shared_dpll(intel_crtc);
4818
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004819 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304820 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004821
4822 intel_set_pipe_timings(intel_crtc);
4823
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004824 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004825 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004826 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004827 }
4828
4829 ironlake_set_pipeconf(crtc);
4830
Jesse Barnesf67a5592011-01-05 10:31:48 -08004831 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004832
Daniel Vettera72e4c92014-09-30 10:56:47 +02004833 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4834 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004835
Daniel Vetterf6736a12013-06-05 13:34:30 +02004836 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004837 if (encoder->pre_enable)
4838 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004839
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004840 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004841 /* Note: FDI PLL enabling _must_ be done before we enable the
4842 * cpu pipes, hence this is separate from all the other fdi/pch
4843 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004844 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004845 } else {
4846 assert_fdi_tx_disabled(dev_priv, pipe);
4847 assert_fdi_rx_disabled(dev_priv, pipe);
4848 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004849
Jesse Barnesb074cec2013-04-25 12:55:02 -07004850 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004851
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004852 /*
4853 * On ILK+ LUT must be loaded before the pipe is running but with
4854 * clocks enabled
4855 */
4856 intel_crtc_load_lut(crtc);
4857
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004858 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004859 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004860
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004861 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004862 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004863
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004864 assert_vblank_disabled(crtc);
4865 drm_crtc_vblank_on(crtc);
4866
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004867 for_each_encoder_on_crtc(dev, crtc, encoder)
4868 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004869
4870 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004871 cpt_verify_modeset(dev, intel_crtc->pipe);
Daniel Vetter6ce94102012-10-04 19:20:03 +02004872
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004873 intel_crtc_enable_planes(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004874}
4875
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004876/* IPS only exists on ULT machines and is tied to pipe A. */
4877static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4878{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004879 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004880}
4881
Paulo Zanonie4916942013-09-20 16:21:19 -03004882/*
4883 * This implements the workaround described in the "notes" section of the mode
4884 * set sequence documentation. When going from no pipes or single pipe to
4885 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4886 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4887 */
4888static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4889{
4890 struct drm_device *dev = crtc->base.dev;
4891 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4892
4893 /* We want to get the other_active_crtc only if there's only 1 other
4894 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004895 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004896 if (!crtc_it->active || crtc_it == crtc)
4897 continue;
4898
4899 if (other_active_crtc)
4900 return;
4901
4902 other_active_crtc = crtc_it;
4903 }
4904 if (!other_active_crtc)
4905 return;
4906
4907 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4908 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4909}
4910
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004911static void haswell_crtc_enable(struct drm_crtc *crtc)
4912{
4913 struct drm_device *dev = crtc->dev;
4914 struct drm_i915_private *dev_priv = dev->dev_private;
4915 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4916 struct intel_encoder *encoder;
4917 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004918
Matt Roper83d65732015-02-25 13:12:16 -08004919 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920
4921 if (intel_crtc->active)
4922 return;
4923
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004924 if (intel_crtc_to_shared_dpll(intel_crtc))
4925 intel_enable_shared_dpll(intel_crtc);
4926
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004927 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304928 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004929
4930 intel_set_pipe_timings(intel_crtc);
4931
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004932 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4933 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4934 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004935 }
4936
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004937 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004938 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004939 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004940 }
4941
4942 haswell_set_pipeconf(crtc);
4943
4944 intel_set_pipe_csc(crtc);
4945
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004946 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004947
Daniel Vettera72e4c92014-09-30 10:56:47 +02004948 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004949 for_each_encoder_on_crtc(dev, crtc, encoder)
4950 if (encoder->pre_enable)
4951 encoder->pre_enable(encoder);
4952
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004953 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004954 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4955 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004956 dev_priv->display.fdi_link_train(crtc);
4957 }
4958
Paulo Zanoni1f544382012-10-24 11:32:00 -02004959 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004960
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004961 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004962 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004963 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004964 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004965 else
4966 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004967
4968 /*
4969 * On ILK+ LUT must be loaded before the pipe is running but with
4970 * clocks enabled
4971 */
4972 intel_crtc_load_lut(crtc);
4973
Paulo Zanoni1f544382012-10-24 11:32:00 -02004974 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004975 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004976
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004977 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004978 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004980 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004981 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004982
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004983 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004984 intel_ddi_set_vc_payload_alloc(crtc, true);
4985
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004986 assert_vblank_disabled(crtc);
4987 drm_crtc_vblank_on(crtc);
4988
Jani Nikula8807e552013-08-30 19:40:32 +03004989 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004990 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004991 intel_opregion_notify_encoder(encoder, true);
4992 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004993
Paulo Zanonie4916942013-09-20 16:21:19 -03004994 /* If we change the relative order between pipe/planes enabling, we need
4995 * to change the workaround. */
4996 haswell_mode_set_planes_workaround(intel_crtc);
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004997 intel_crtc_enable_planes(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004998}
4999
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005000static void ironlake_pfit_disable(struct intel_crtc *crtc)
5001{
5002 struct drm_device *dev = crtc->base.dev;
5003 struct drm_i915_private *dev_priv = dev->dev_private;
5004 int pipe = crtc->pipe;
5005
5006 /* To avoid upsetting the power well on haswell only disable the pfit if
5007 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005008 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005009 I915_WRITE(PF_CTL(pipe), 0);
5010 I915_WRITE(PF_WIN_POS(pipe), 0);
5011 I915_WRITE(PF_WIN_SZ(pipe), 0);
5012 }
5013}
5014
Jesse Barnes6be4a602010-09-10 10:26:01 -07005015static void ironlake_crtc_disable(struct drm_crtc *crtc)
5016{
5017 struct drm_device *dev = crtc->dev;
5018 struct drm_i915_private *dev_priv = dev->dev_private;
5019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005020 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005021 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005022 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005023
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005024 if (!intel_crtc->active)
5025 return;
5026
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005027 intel_crtc_disable_planes(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02005028
Daniel Vetterea9d7582012-07-10 10:42:52 +02005029 for_each_encoder_on_crtc(dev, crtc, encoder)
5030 encoder->disable(encoder);
5031
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005032 drm_crtc_vblank_off(crtc);
5033 assert_vblank_disabled(crtc);
5034
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005035 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005036 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005037
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005038 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005039
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005040 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005041
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005042 for_each_encoder_on_crtc(dev, crtc, encoder)
5043 if (encoder->post_disable)
5044 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005045
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005046 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005047 ironlake_fdi_disable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005048
Daniel Vetterd925c592013-06-05 13:34:04 +02005049 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005050
Daniel Vetterd925c592013-06-05 13:34:04 +02005051 if (HAS_PCH_CPT(dev)) {
5052 /* disable TRANS_DP_CTL */
5053 reg = TRANS_DP_CTL(pipe);
5054 temp = I915_READ(reg);
5055 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5056 TRANS_DP_PORT_SEL_MASK);
5057 temp |= TRANS_DP_PORT_SEL_NONE;
5058 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005059
Daniel Vetterd925c592013-06-05 13:34:04 +02005060 /* disable DPLL_SEL */
5061 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005062 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005063 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005064 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005065
5066 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005067 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005068
5069 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005070 }
5071
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005072 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005073 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005074
5075 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005076 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005077 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005078}
5079
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005080static void haswell_crtc_disable(struct drm_crtc *crtc)
5081{
5082 struct drm_device *dev = crtc->dev;
5083 struct drm_i915_private *dev_priv = dev->dev_private;
5084 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5085 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005086 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005087
5088 if (!intel_crtc->active)
5089 return;
5090
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03005091 intel_crtc_disable_planes(crtc);
Ville Syrjälädda9a662013-09-19 17:00:37 -03005092
Jani Nikula8807e552013-08-30 19:40:32 +03005093 for_each_encoder_on_crtc(dev, crtc, encoder) {
5094 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005095 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005096 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005097
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005098 drm_crtc_vblank_off(crtc);
5099 assert_vblank_disabled(crtc);
5100
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005101 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005102 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5103 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005104 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005105
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005106 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005107 intel_ddi_set_vc_payload_alloc(crtc, false);
5108
Paulo Zanoniad80a812012-10-24 16:06:19 -02005109 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005110
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005111 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005112 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005113 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005114 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005115 else
5116 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005117
Paulo Zanoni1f544382012-10-24 11:32:00 -02005118 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005119
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005120 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005121 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005122 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005123 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005124
Imre Deak97b040a2014-06-25 22:01:50 +03005125 for_each_encoder_on_crtc(dev, crtc, encoder)
5126 if (encoder->post_disable)
5127 encoder->post_disable(encoder);
5128
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005129 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005130 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005131
5132 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005133 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005134 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005135
5136 if (intel_crtc_to_shared_dpll(intel_crtc))
5137 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005138}
5139
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005140static void ironlake_crtc_off(struct drm_crtc *crtc)
5141{
5142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005143 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005144}
5145
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005146
Jesse Barnes2dd24552013-04-25 12:55:01 -07005147static void i9xx_pfit_enable(struct intel_crtc *crtc)
5148{
5149 struct drm_device *dev = crtc->base.dev;
5150 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005151 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005152
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005153 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005154 return;
5155
Daniel Vetterc0b03412013-05-28 12:05:54 +02005156 /*
5157 * The panel fitter should only be adjusted whilst the pipe is disabled,
5158 * according to register description and PRM.
5159 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005160 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5161 assert_pipe_disabled(dev_priv, crtc->pipe);
5162
Jesse Barnesb074cec2013-04-25 12:55:02 -07005163 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5164 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005165
5166 /* Border color in case we don't scale up to the full screen. Black by
5167 * default, change to something else for debugging. */
5168 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005169}
5170
Dave Airlied05410f2014-06-05 13:22:59 +10005171static enum intel_display_power_domain port_to_power_domain(enum port port)
5172{
5173 switch (port) {
5174 case PORT_A:
5175 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5176 case PORT_B:
5177 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5178 case PORT_C:
5179 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5180 case PORT_D:
5181 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5182 default:
5183 WARN_ON_ONCE(1);
5184 return POWER_DOMAIN_PORT_OTHER;
5185 }
5186}
5187
Imre Deak77d22dc2014-03-05 16:20:52 +02005188#define for_each_power_domain(domain, mask) \
5189 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5190 if ((1 << (domain)) & (mask))
5191
Imre Deak319be8a2014-03-04 19:22:57 +02005192enum intel_display_power_domain
5193intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005194{
Imre Deak319be8a2014-03-04 19:22:57 +02005195 struct drm_device *dev = intel_encoder->base.dev;
5196 struct intel_digital_port *intel_dig_port;
5197
5198 switch (intel_encoder->type) {
5199 case INTEL_OUTPUT_UNKNOWN:
5200 /* Only DDI platforms should ever use this output type */
5201 WARN_ON_ONCE(!HAS_DDI(dev));
5202 case INTEL_OUTPUT_DISPLAYPORT:
5203 case INTEL_OUTPUT_HDMI:
5204 case INTEL_OUTPUT_EDP:
5205 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005206 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005207 case INTEL_OUTPUT_DP_MST:
5208 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5209 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005210 case INTEL_OUTPUT_ANALOG:
5211 return POWER_DOMAIN_PORT_CRT;
5212 case INTEL_OUTPUT_DSI:
5213 return POWER_DOMAIN_PORT_DSI;
5214 default:
5215 return POWER_DOMAIN_PORT_OTHER;
5216 }
5217}
5218
5219static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5220{
5221 struct drm_device *dev = crtc->dev;
5222 struct intel_encoder *intel_encoder;
5223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5224 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005225 unsigned long mask;
5226 enum transcoder transcoder;
5227
5228 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5229
5230 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5231 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005232 if (intel_crtc->config->pch_pfit.enabled ||
5233 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005234 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5235
Imre Deak319be8a2014-03-04 19:22:57 +02005236 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5237 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5238
Imre Deak77d22dc2014-03-05 16:20:52 +02005239 return mask;
5240}
5241
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005242static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005243{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005244 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005245 struct drm_i915_private *dev_priv = dev->dev_private;
5246 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5247 struct intel_crtc *crtc;
5248
5249 /*
5250 * First get all needed power domains, then put all unneeded, to avoid
5251 * any unnecessary toggling of the power wells.
5252 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005253 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005254 enum intel_display_power_domain domain;
5255
Matt Roper83d65732015-02-25 13:12:16 -08005256 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005257 continue;
5258
Imre Deak319be8a2014-03-04 19:22:57 +02005259 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005260
5261 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5262 intel_display_power_get(dev_priv, domain);
5263 }
5264
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005265 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005266 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005267
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005268 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005269 enum intel_display_power_domain domain;
5270
5271 for_each_power_domain(domain, crtc->enabled_power_domains)
5272 intel_display_power_put(dev_priv, domain);
5273
5274 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5275 }
5276
5277 intel_display_set_init_power(dev_priv, false);
5278}
5279
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305280void broxton_set_cdclk(struct drm_device *dev, int frequency)
5281{
5282 struct drm_i915_private *dev_priv = dev->dev_private;
5283 uint32_t divider;
5284 uint32_t ratio;
5285 uint32_t current_freq;
5286 int ret;
5287
5288 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5289 switch (frequency) {
5290 case 144000:
5291 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5292 ratio = BXT_DE_PLL_RATIO(60);
5293 break;
5294 case 288000:
5295 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5296 ratio = BXT_DE_PLL_RATIO(60);
5297 break;
5298 case 384000:
5299 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5300 ratio = BXT_DE_PLL_RATIO(60);
5301 break;
5302 case 576000:
5303 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5304 ratio = BXT_DE_PLL_RATIO(60);
5305 break;
5306 case 624000:
5307 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5308 ratio = BXT_DE_PLL_RATIO(65);
5309 break;
5310 case 19200:
5311 /*
5312 * Bypass frequency with DE PLL disabled. Init ratio, divider
5313 * to suppress GCC warning.
5314 */
5315 ratio = 0;
5316 divider = 0;
5317 break;
5318 default:
5319 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5320
5321 return;
5322 }
5323
5324 mutex_lock(&dev_priv->rps.hw_lock);
5325 /* Inform power controller of upcoming frequency change */
5326 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5327 0x80000000);
5328 mutex_unlock(&dev_priv->rps.hw_lock);
5329
5330 if (ret) {
5331 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5332 ret, frequency);
5333 return;
5334 }
5335
5336 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5337 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5338 current_freq = current_freq * 500 + 1000;
5339
5340 /*
5341 * DE PLL has to be disabled when
5342 * - setting to 19.2MHz (bypass, PLL isn't used)
5343 * - before setting to 624MHz (PLL needs toggling)
5344 * - before setting to any frequency from 624MHz (PLL needs toggling)
5345 */
5346 if (frequency == 19200 || frequency == 624000 ||
5347 current_freq == 624000) {
5348 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5349 /* Timeout 200us */
5350 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5351 1))
5352 DRM_ERROR("timout waiting for DE PLL unlock\n");
5353 }
5354
5355 if (frequency != 19200) {
5356 uint32_t val;
5357
5358 val = I915_READ(BXT_DE_PLL_CTL);
5359 val &= ~BXT_DE_PLL_RATIO_MASK;
5360 val |= ratio;
5361 I915_WRITE(BXT_DE_PLL_CTL, val);
5362
5363 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5364 /* Timeout 200us */
5365 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5366 DRM_ERROR("timeout waiting for DE PLL lock\n");
5367
5368 val = I915_READ(CDCLK_CTL);
5369 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5370 val |= divider;
5371 /*
5372 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5373 * enable otherwise.
5374 */
5375 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5376 if (frequency >= 500000)
5377 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5378
5379 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5380 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5381 val |= (frequency - 1000) / 500;
5382 I915_WRITE(CDCLK_CTL, val);
5383 }
5384
5385 mutex_lock(&dev_priv->rps.hw_lock);
5386 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5387 DIV_ROUND_UP(frequency, 25000));
5388 mutex_unlock(&dev_priv->rps.hw_lock);
5389
5390 if (ret) {
5391 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5392 ret, frequency);
5393 return;
5394 }
5395
5396 dev_priv->cdclk_freq = frequency;
5397}
5398
5399void broxton_init_cdclk(struct drm_device *dev)
5400{
5401 struct drm_i915_private *dev_priv = dev->dev_private;
5402 uint32_t val;
5403
5404 /*
5405 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5406 * or else the reset will hang because there is no PCH to respond.
5407 * Move the handshake programming to initialization sequence.
5408 * Previously was left up to BIOS.
5409 */
5410 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5411 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5412 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5413
5414 /* Enable PG1 for cdclk */
5415 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5416
5417 /* check if cd clock is enabled */
5418 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5419 DRM_DEBUG_KMS("Display already initialized\n");
5420 return;
5421 }
5422
5423 /*
5424 * FIXME:
5425 * - The initial CDCLK needs to be read from VBT.
5426 * Need to make this change after VBT has changes for BXT.
5427 * - check if setting the max (or any) cdclk freq is really necessary
5428 * here, it belongs to modeset time
5429 */
5430 broxton_set_cdclk(dev, 624000);
5431
5432 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005433 POSTING_READ(DBUF_CTL);
5434
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305435 udelay(10);
5436
5437 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5438 DRM_ERROR("DBuf power enable timeout!\n");
5439}
5440
5441void broxton_uninit_cdclk(struct drm_device *dev)
5442{
5443 struct drm_i915_private *dev_priv = dev->dev_private;
5444
5445 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005446 POSTING_READ(DBUF_CTL);
5447
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305448 udelay(10);
5449
5450 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5451 DRM_ERROR("DBuf power disable timeout!\n");
5452
5453 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5454 broxton_set_cdclk(dev, 19200);
5455
5456 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5457}
5458
Ville Syrjälädfcab172014-06-13 13:37:47 +03005459/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005460static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005461{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005462 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005463
Jesse Barnes586f49d2013-11-04 16:06:59 -08005464 /* Obtain SKU information */
5465 mutex_lock(&dev_priv->dpio_lock);
5466 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5467 CCK_FUSE_HPLL_FREQ_MASK;
5468 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005469
Ville Syrjälädfcab172014-06-13 13:37:47 +03005470 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005471}
5472
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005473static void vlv_update_cdclk(struct drm_device *dev)
5474{
5475 struct drm_i915_private *dev_priv = dev->dev_private;
5476
Vandana Kannan164dfd22014-11-24 13:37:41 +05305477 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005478 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305479 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005480
5481 /*
5482 * Program the gmbus_freq based on the cdclk frequency.
5483 * BSpec erroneously claims we should aim for 4MHz, but
5484 * in fact 1MHz is the correct frequency.
5485 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305486 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005487}
5488
Jesse Barnes30a970c2013-11-04 13:48:12 -08005489/* Adjust CDclk dividers to allow high res or save power if possible */
5490static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5491{
5492 struct drm_i915_private *dev_priv = dev->dev_private;
5493 u32 val, cmd;
5494
Vandana Kannan164dfd22014-11-24 13:37:41 +05305495 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5496 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005497
Ville Syrjälädfcab172014-06-13 13:37:47 +03005498 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005499 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005500 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005501 cmd = 1;
5502 else
5503 cmd = 0;
5504
5505 mutex_lock(&dev_priv->rps.hw_lock);
5506 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5507 val &= ~DSPFREQGUAR_MASK;
5508 val |= (cmd << DSPFREQGUAR_SHIFT);
5509 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5510 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5511 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5512 50)) {
5513 DRM_ERROR("timed out waiting for CDclk change\n");
5514 }
5515 mutex_unlock(&dev_priv->rps.hw_lock);
5516
Ville Syrjälädfcab172014-06-13 13:37:47 +03005517 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005518 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005519
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005520 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005521
5522 mutex_lock(&dev_priv->dpio_lock);
5523 /* adjust cdclk divider */
5524 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005525 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005526 val |= divider;
5527 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005528
5529 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5530 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5531 50))
5532 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005533 mutex_unlock(&dev_priv->dpio_lock);
5534 }
5535
5536 mutex_lock(&dev_priv->dpio_lock);
5537 /* adjust self-refresh exit latency value */
5538 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5539 val &= ~0x7f;
5540
5541 /*
5542 * For high bandwidth configs, we set a higher latency in the bunit
5543 * so that the core display fetch happens in time to avoid underruns.
5544 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005545 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005546 val |= 4500 / 250; /* 4.5 usec */
5547 else
5548 val |= 3000 / 250; /* 3.0 usec */
5549 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5550 mutex_unlock(&dev_priv->dpio_lock);
5551
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005552 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005553}
5554
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005555static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5556{
5557 struct drm_i915_private *dev_priv = dev->dev_private;
5558 u32 val, cmd;
5559
Vandana Kannan164dfd22014-11-24 13:37:41 +05305560 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5561 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005562
5563 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005564 case 333333:
5565 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005566 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005567 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005568 break;
5569 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005570 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005571 return;
5572 }
5573
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005574 /*
5575 * Specs are full of misinformation, but testing on actual
5576 * hardware has shown that we just need to write the desired
5577 * CCK divider into the Punit register.
5578 */
5579 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5580
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005581 mutex_lock(&dev_priv->rps.hw_lock);
5582 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5583 val &= ~DSPFREQGUAR_MASK_CHV;
5584 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5585 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5586 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5587 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5588 50)) {
5589 DRM_ERROR("timed out waiting for CDclk change\n");
5590 }
5591 mutex_unlock(&dev_priv->rps.hw_lock);
5592
5593 vlv_update_cdclk(dev);
5594}
5595
Jesse Barnes30a970c2013-11-04 13:48:12 -08005596static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5597 int max_pixclk)
5598{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005599 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005600 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005601
Jesse Barnes30a970c2013-11-04 13:48:12 -08005602 /*
5603 * Really only a few cases to deal with, as only 4 CDclks are supported:
5604 * 200MHz
5605 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005606 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005607 * 400MHz (VLV only)
5608 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5609 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005610 *
5611 * We seem to get an unstable or solid color picture at 200MHz.
5612 * Not sure what's wrong. For now use 200MHz only when all pipes
5613 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005614 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005615 if (!IS_CHERRYVIEW(dev_priv) &&
5616 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005617 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005618 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005619 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005620 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005621 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005622 else
5623 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005624}
5625
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305626static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5627 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005628{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305629 /*
5630 * FIXME:
5631 * - remove the guardband, it's not needed on BXT
5632 * - set 19.2MHz bypass frequency if there are no active pipes
5633 */
5634 if (max_pixclk > 576000*9/10)
5635 return 624000;
5636 else if (max_pixclk > 384000*9/10)
5637 return 576000;
5638 else if (max_pixclk > 288000*9/10)
5639 return 384000;
5640 else if (max_pixclk > 144000*9/10)
5641 return 288000;
5642 else
5643 return 144000;
5644}
5645
Jesse Barnes30a970c2013-11-04 13:48:12 -08005646/* compute the max pixel clock for new configuration */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005647static int intel_mode_max_pixclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005648{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005649 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005650 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005651 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005652 int max_pixclk = 0;
5653
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005654 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005655 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
5656 if (IS_ERR(crtc_state))
5657 return PTR_ERR(crtc_state);
5658
5659 if (!crtc_state->base.enable)
5660 continue;
5661
5662 max_pixclk = max(max_pixclk,
5663 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005664 }
5665
5666 return max_pixclk;
5667}
5668
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005669static int valleyview_modeset_global_pipes(struct drm_atomic_state *state,
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005670 unsigned *prepare_pipes)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005671{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005672 struct drm_i915_private *dev_priv = to_i915(state->dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005673 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005674 int max_pixclk = intel_mode_max_pixclk(state);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305675 int cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005676
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005677 if (max_pixclk < 0)
5678 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005679
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305680 if (IS_VALLEYVIEW(dev_priv))
5681 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5682 else
5683 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5684
5685 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005686 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005687
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005688 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005689 for_each_intel_crtc(state->dev, intel_crtc)
Matt Roper83d65732015-02-25 13:12:16 -08005690 if (intel_crtc->base.state->enable)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005691 *prepare_pipes |= (1 << intel_crtc->pipe);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005692
5693 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005694}
5695
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005696static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5697{
5698 unsigned int credits, default_credits;
5699
5700 if (IS_CHERRYVIEW(dev_priv))
5701 default_credits = PFI_CREDIT(12);
5702 else
5703 default_credits = PFI_CREDIT(8);
5704
Vandana Kannan164dfd22014-11-24 13:37:41 +05305705 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005706 /* CHV suggested value is 31 or 63 */
5707 if (IS_CHERRYVIEW(dev_priv))
5708 credits = PFI_CREDIT_31;
5709 else
5710 credits = PFI_CREDIT(15);
5711 } else {
5712 credits = default_credits;
5713 }
5714
5715 /*
5716 * WA - write default credits before re-programming
5717 * FIXME: should we also set the resend bit here?
5718 */
5719 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5720 default_credits);
5721
5722 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5723 credits | PFI_CREDIT_RESEND);
5724
5725 /*
5726 * FIXME is this guaranteed to clear
5727 * immediately or should we poll for it?
5728 */
5729 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5730}
5731
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005732static void valleyview_modeset_global_resources(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005733{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005734 struct drm_device *dev = state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005735 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005736 int max_pixclk = intel_mode_max_pixclk(state);
5737 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005738
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005739 /* The only reason this can fail is if we fail to add the crtc_state
5740 * to the atomic state. But that can't happen since the call to
5741 * intel_mode_max_pixclk() in valleyview_modeset_global_pipes() (which
5742 * can't have failed otherwise the mode set would be aborted) added all
5743 * the states already. */
5744 if (WARN_ON(max_pixclk < 0))
5745 return;
5746
5747 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005748
Vandana Kannan164dfd22014-11-24 13:37:41 +05305749 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02005750 /*
5751 * FIXME: We can end up here with all power domains off, yet
5752 * with a CDCLK frequency other than the minimum. To account
5753 * for this take the PIPE-A power domain, which covers the HW
5754 * blocks needed for the following programming. This can be
5755 * removed once it's guaranteed that we get here either with
5756 * the minimum CDCLK set, or the required power domains
5757 * enabled.
5758 */
5759 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
5760
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005761 if (IS_CHERRYVIEW(dev))
5762 cherryview_set_cdclk(dev, req_cdclk);
5763 else
5764 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02005765
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005766 vlv_program_pfi_credits(dev_priv);
5767
Imre Deak738c05c2014-11-19 16:25:37 +02005768 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005769 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08005770}
5771
Jesse Barnes89b667f2013-04-18 14:51:36 -07005772static void valleyview_crtc_enable(struct drm_crtc *crtc)
5773{
5774 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005775 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005776 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5777 struct intel_encoder *encoder;
5778 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03005779 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005780
Matt Roper83d65732015-02-25 13:12:16 -08005781 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005782
5783 if (intel_crtc->active)
5784 return;
5785
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005786 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05305787
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005788 if (!is_dsi) {
5789 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005790 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005791 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005792 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03005793 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02005794
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005795 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305796 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005797
5798 intel_set_pipe_timings(intel_crtc);
5799
Ville Syrjäläc14b0482014-10-16 20:52:34 +03005800 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
5801 struct drm_i915_private *dev_priv = dev->dev_private;
5802
5803 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
5804 I915_WRITE(CHV_CANVAS(pipe), 0);
5805 }
5806
Daniel Vetter5b18e572014-04-24 23:55:06 +02005807 i9xx_set_pipeconf(intel_crtc);
5808
Jesse Barnes89b667f2013-04-18 14:51:36 -07005809 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07005810
Daniel Vettera72e4c92014-09-30 10:56:47 +02005811 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005812
Jesse Barnes89b667f2013-04-18 14:51:36 -07005813 for_each_encoder_on_crtc(dev, crtc, encoder)
5814 if (encoder->pre_pll_enable)
5815 encoder->pre_pll_enable(encoder);
5816
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005817 if (!is_dsi) {
5818 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005819 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005820 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005821 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03005822 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07005823
5824 for_each_encoder_on_crtc(dev, crtc, encoder)
5825 if (encoder->pre_enable)
5826 encoder->pre_enable(encoder);
5827
Jesse Barnes2dd24552013-04-25 12:55:01 -07005828 i9xx_pfit_enable(intel_crtc);
5829
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005830 intel_crtc_load_lut(crtc);
5831
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005832 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005833 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005834
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005835 assert_vblank_disabled(crtc);
5836 drm_crtc_vblank_on(crtc);
5837
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005838 for_each_encoder_on_crtc(dev, crtc, encoder)
5839 encoder->enable(encoder);
5840
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005841 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005842
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005843 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005844 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07005845}
5846
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005847static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
5848{
5849 struct drm_device *dev = crtc->base.dev;
5850 struct drm_i915_private *dev_priv = dev->dev_private;
5851
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005852 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
5853 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005854}
5855
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005856static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08005857{
5858 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02005859 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08005860 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005861 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08005862 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08005863
Matt Roper83d65732015-02-25 13:12:16 -08005864 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02005865
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005866 if (intel_crtc->active)
5867 return;
5868
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02005869 i9xx_set_pll_dividers(intel_crtc);
5870
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005871 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05305872 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02005873
5874 intel_set_pipe_timings(intel_crtc);
5875
Daniel Vetter5b18e572014-04-24 23:55:06 +02005876 i9xx_set_pipeconf(intel_crtc);
5877
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005878 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01005879
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005880 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005881 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005882
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02005883 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02005884 if (encoder->pre_enable)
5885 encoder->pre_enable(encoder);
5886
Daniel Vetterf6736a12013-06-05 13:34:30 +02005887 i9xx_enable_pll(intel_crtc);
5888
Jesse Barnes2dd24552013-04-25 12:55:01 -07005889 i9xx_pfit_enable(intel_crtc);
5890
Ville Syrjälä63cbb072013-06-04 13:48:59 +03005891 intel_crtc_load_lut(crtc);
5892
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005893 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005894 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02005895
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005896 assert_vblank_disabled(crtc);
5897 drm_crtc_vblank_on(crtc);
5898
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005899 for_each_encoder_on_crtc(dev, crtc, encoder)
5900 encoder->enable(encoder);
5901
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005902 intel_crtc_enable_planes(crtc);
Daniel Vetterd40d9182014-05-21 11:45:40 +02005903
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005904 /*
5905 * Gen2 reports pipe underruns whenever all planes are disabled.
5906 * So don't enable underrun reporting before at least some planes
5907 * are enabled.
5908 * FIXME: Need to fix the logic to work when we turn off all planes
5909 * but leave the pipe running.
5910 */
5911 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005912 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005913
Ville Syrjälä56b80e12014-05-16 19:40:22 +03005914 /* Underruns don't raise interrupts, so check manually. */
Daniel Vettera72e4c92014-09-30 10:56:47 +02005915 i9xx_check_fifo_underruns(dev_priv);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005916}
5917
Daniel Vetter87476d62013-04-11 16:29:06 +02005918static void i9xx_pfit_disable(struct intel_crtc *crtc)
5919{
5920 struct drm_device *dev = crtc->base.dev;
5921 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02005922
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005923 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02005924 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02005925
5926 assert_pipe_disabled(dev_priv, crtc->pipe);
5927
Daniel Vetter328d8e82013-05-08 10:36:31 +02005928 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
5929 I915_READ(PFIT_CONTROL));
5930 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02005931}
5932
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005933static void i9xx_crtc_disable(struct drm_crtc *crtc)
5934{
5935 struct drm_device *dev = crtc->dev;
5936 struct drm_i915_private *dev_priv = dev->dev_private;
5937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005938 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005939 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005940
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005941 if (!intel_crtc->active)
5942 return;
5943
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005944 /*
5945 * Gen2 reports pipe underruns whenever all planes are disabled.
5946 * So diasble underrun reporting before all the planes get disabled.
5947 * FIXME: Need to fix the logic to work when we turn off all planes
5948 * but leave the pipe running.
5949 */
5950 if (IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005951 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005952
Imre Deak564ed192014-06-13 14:54:21 +03005953 /*
5954 * Vblank time updates from the shadow to live plane control register
5955 * are blocked if the memory self-refresh mode is active at that
5956 * moment. So to make sure the plane gets truly disabled, disable
5957 * first the self-refresh mode. The self-refresh enable bit in turn
5958 * will be checked/applied by the HW only at the next frame start
5959 * event which is after the vblank start event, so we need to have a
5960 * wait-for-vblank between disabling the plane and the pipe.
5961 */
5962 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä9ab04602014-05-08 19:23:14 +03005963 intel_crtc_disable_planes(crtc);
5964
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005965 /*
5966 * On gen2 planes are double buffered but the pipe isn't, so we must
5967 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03005968 * We also need to wait on all gmch platforms because of the
5969 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005970 */
Imre Deak564ed192014-06-13 14:54:21 +03005971 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03005972
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03005973 for_each_encoder_on_crtc(dev, crtc, encoder)
5974 encoder->disable(encoder);
5975
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005976 drm_crtc_vblank_off(crtc);
5977 assert_vblank_disabled(crtc);
5978
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005979 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005980
Daniel Vetter87476d62013-04-11 16:29:06 +02005981 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02005982
Jesse Barnes89b667f2013-04-18 14:51:36 -07005983 for_each_encoder_on_crtc(dev, crtc, encoder)
5984 if (encoder->post_disable)
5985 encoder->post_disable(encoder);
5986
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03005987 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005988 if (IS_CHERRYVIEW(dev))
5989 chv_disable_pll(dev_priv, pipe);
5990 else if (IS_VALLEYVIEW(dev))
5991 vlv_disable_pll(dev_priv, pipe);
5992 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03005993 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03005994 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07005995
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005996 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02005997 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03005998
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005999 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006000 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006001
Daniel Vetterefa96242014-04-24 23:55:02 +02006002 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006003 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006004 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006005}
6006
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006007static void i9xx_crtc_off(struct drm_crtc *crtc)
6008{
6009}
6010
Borun Fub04c5bd2014-07-12 10:02:27 +05306011/* Master function to enable/disable CRTC and corresponding power wells */
6012void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006013{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006014 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006015 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006017 enum intel_display_power_domain domain;
6018 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006019
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006020 if (enable) {
6021 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006022 domains = get_crtc_power_domains(crtc);
6023 for_each_power_domain(domain, domains)
6024 intel_display_power_get(dev_priv, domain);
6025 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006026
6027 dev_priv->display.crtc_enable(crtc);
6028 }
6029 } else {
6030 if (intel_crtc->active) {
6031 dev_priv->display.crtc_disable(crtc);
6032
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006033 domains = intel_crtc->enabled_power_domains;
6034 for_each_power_domain(domain, domains)
6035 intel_display_power_put(dev_priv, domain);
6036 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006037 }
6038 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306039}
6040
6041/**
6042 * Sets the power management mode of the pipe and plane.
6043 */
6044void intel_crtc_update_dpms(struct drm_crtc *crtc)
6045{
6046 struct drm_device *dev = crtc->dev;
6047 struct intel_encoder *intel_encoder;
6048 bool enable = false;
6049
6050 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6051 enable |= intel_encoder->connectors_active;
6052
6053 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006054}
6055
Daniel Vetter976f8a22012-07-08 22:34:21 +02006056static void intel_crtc_disable(struct drm_crtc *crtc)
6057{
6058 struct drm_device *dev = crtc->dev;
6059 struct drm_connector *connector;
6060 struct drm_i915_private *dev_priv = dev->dev_private;
6061
6062 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006063 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006064
6065 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006066 dev_priv->display.off(crtc);
6067
Matt Roper70a101f2015-04-08 18:56:53 -07006068 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006069
6070 /* Update computed state. */
6071 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6072 if (!connector->encoder || !connector->encoder->crtc)
6073 continue;
6074
6075 if (connector->encoder->crtc != crtc)
6076 continue;
6077
6078 connector->dpms = DRM_MODE_DPMS_OFF;
6079 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006080 }
6081}
6082
Chris Wilsonea5b2132010-08-04 13:50:23 +01006083void intel_encoder_destroy(struct drm_encoder *encoder)
6084{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006085 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006086
Chris Wilsonea5b2132010-08-04 13:50:23 +01006087 drm_encoder_cleanup(encoder);
6088 kfree(intel_encoder);
6089}
6090
Damien Lespiau92373292013-08-08 22:28:57 +01006091/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006092 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6093 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006094static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006095{
6096 if (mode == DRM_MODE_DPMS_ON) {
6097 encoder->connectors_active = true;
6098
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006099 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006100 } else {
6101 encoder->connectors_active = false;
6102
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006103 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006104 }
6105}
6106
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006107/* Cross check the actual hw state with our own modeset state tracking (and it's
6108 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006109static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006110{
6111 if (connector->get_hw_state(connector)) {
6112 struct intel_encoder *encoder = connector->encoder;
6113 struct drm_crtc *crtc;
6114 bool encoder_enabled;
6115 enum pipe pipe;
6116
6117 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6118 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006119 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006120
Dave Airlie0e32b392014-05-02 14:02:48 +10006121 /* there is no real hw state for MST connectors */
6122 if (connector->mst_port)
6123 return;
6124
Rob Clarke2c719b2014-12-15 13:56:32 -05006125 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006126 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006127 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006128 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006129
Dave Airlie36cd7442014-05-02 13:44:18 +10006130 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006131 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006132 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006133
Dave Airlie36cd7442014-05-02 13:44:18 +10006134 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006135 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6136 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006137 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006138
Dave Airlie36cd7442014-05-02 13:44:18 +10006139 crtc = encoder->base.crtc;
6140
Matt Roper83d65732015-02-25 13:12:16 -08006141 I915_STATE_WARN(!crtc->state->enable,
6142 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006143 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6144 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006145 "encoder active on the wrong pipe\n");
6146 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006147 }
6148}
6149
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006150int intel_connector_init(struct intel_connector *connector)
6151{
6152 struct drm_connector_state *connector_state;
6153
6154 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6155 if (!connector_state)
6156 return -ENOMEM;
6157
6158 connector->base.state = connector_state;
6159 return 0;
6160}
6161
6162struct intel_connector *intel_connector_alloc(void)
6163{
6164 struct intel_connector *connector;
6165
6166 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6167 if (!connector)
6168 return NULL;
6169
6170 if (intel_connector_init(connector) < 0) {
6171 kfree(connector);
6172 return NULL;
6173 }
6174
6175 return connector;
6176}
6177
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006178/* Even simpler default implementation, if there's really no special case to
6179 * consider. */
6180void intel_connector_dpms(struct drm_connector *connector, int mode)
6181{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006182 /* All the simple cases only support two dpms states. */
6183 if (mode != DRM_MODE_DPMS_ON)
6184 mode = DRM_MODE_DPMS_OFF;
6185
6186 if (mode == connector->dpms)
6187 return;
6188
6189 connector->dpms = mode;
6190
6191 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006192 if (connector->encoder)
6193 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006194
Daniel Vetterb9805142012-08-31 17:37:33 +02006195 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006196}
6197
Daniel Vetterf0947c32012-07-02 13:10:34 +02006198/* Simple connector->get_hw_state implementation for encoders that support only
6199 * one connector and no cloning and hence the encoder state determines the state
6200 * of the connector. */
6201bool intel_connector_get_hw_state(struct intel_connector *connector)
6202{
Daniel Vetter24929352012-07-02 20:28:59 +02006203 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006204 struct intel_encoder *encoder = connector->encoder;
6205
6206 return encoder->get_hw_state(encoder, &pipe);
6207}
6208
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006209static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006210{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006211 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6212 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006213
6214 return 0;
6215}
6216
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006217static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006218 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006219{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006220 struct drm_atomic_state *state = pipe_config->base.state;
6221 struct intel_crtc *other_crtc;
6222 struct intel_crtc_state *other_crtc_state;
6223
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006224 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6225 pipe_name(pipe), pipe_config->fdi_lanes);
6226 if (pipe_config->fdi_lanes > 4) {
6227 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6228 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006229 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006230 }
6231
Paulo Zanonibafb6552013-11-02 21:07:44 -07006232 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006233 if (pipe_config->fdi_lanes > 2) {
6234 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6235 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006236 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006237 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006238 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006239 }
6240 }
6241
6242 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006243 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006244
6245 /* Ivybridge 3 pipe is really complicated */
6246 switch (pipe) {
6247 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006248 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006249 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006250 if (pipe_config->fdi_lanes <= 2)
6251 return 0;
6252
6253 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6254 other_crtc_state =
6255 intel_atomic_get_crtc_state(state, other_crtc);
6256 if (IS_ERR(other_crtc_state))
6257 return PTR_ERR(other_crtc_state);
6258
6259 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006260 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6261 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006262 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006263 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006264 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006265 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006266 if (pipe_config->fdi_lanes > 2) {
6267 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6268 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006269 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006270 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006271
6272 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6273 other_crtc_state =
6274 intel_atomic_get_crtc_state(state, other_crtc);
6275 if (IS_ERR(other_crtc_state))
6276 return PTR_ERR(other_crtc_state);
6277
6278 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006279 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006280 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006281 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006282 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006283 default:
6284 BUG();
6285 }
6286}
6287
Daniel Vettere29c22c2013-02-21 00:00:16 +01006288#define RETRY 1
6289static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006290 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006291{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006292 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006293 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006294 int lane, link_bw, fdi_dotclock, ret;
6295 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006296
Daniel Vettere29c22c2013-02-21 00:00:16 +01006297retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006298 /* FDI is a binary signal running at ~2.7GHz, encoding
6299 * each output octet as 10 bits. The actual frequency
6300 * is stored as a divider into a 100MHz clock, and the
6301 * mode pixel clock is stored in units of 1KHz.
6302 * Hence the bw of each lane in terms of the mode signal
6303 * is:
6304 */
6305 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6306
Damien Lespiau241bfc32013-09-25 16:45:37 +01006307 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006308
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006309 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006310 pipe_config->pipe_bpp);
6311
6312 pipe_config->fdi_lanes = lane;
6313
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006314 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006315 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006316
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006317 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6318 intel_crtc->pipe, pipe_config);
6319 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006320 pipe_config->pipe_bpp -= 2*3;
6321 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6322 pipe_config->pipe_bpp);
6323 needs_recompute = true;
6324 pipe_config->bw_constrained = true;
6325
6326 goto retry;
6327 }
6328
6329 if (needs_recompute)
6330 return RETRY;
6331
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006332 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006333}
6334
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006335static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006336 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006337{
Jani Nikulad330a952014-01-21 11:24:25 +02006338 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006339 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006340 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006341}
6342
Daniel Vettera43f6e02013-06-07 23:10:32 +02006343static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006344 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006345{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006346 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006347 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006348 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006349 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006350
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006351 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006352 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006353 int clock_limit =
6354 dev_priv->display.get_display_clock_speed(dev);
6355
6356 /*
6357 * Enable pixel doubling when the dot clock
6358 * is > 90% of the (display) core speed.
6359 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006360 * GDG double wide on either pipe,
6361 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006362 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006363 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006364 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006365 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006366 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006367 }
6368
Damien Lespiau241bfc32013-09-25 16:45:37 +01006369 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006370 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006371 }
Chris Wilson89749352010-09-12 18:25:19 +01006372
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006373 /*
6374 * Pipe horizontal size must be even in:
6375 * - DVO ganged mode
6376 * - LVDS dual channel mode
6377 * - Double wide pipe
6378 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006379 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006380 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6381 pipe_config->pipe_src_w &= ~1;
6382
Damien Lespiau8693a822013-05-03 18:48:11 +01006383 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6384 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006385 */
6386 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6387 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006388 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006389
Damien Lespiauf5adf942013-06-24 18:29:34 +01006390 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006391 hsw_compute_ips_config(crtc, pipe_config);
6392
Daniel Vetter877d48d2013-04-19 11:24:43 +02006393 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006394 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006395
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006396 /* FIXME: remove below call once atomic mode set is place and all crtc
6397 * related checks called from atomic_crtc_check function */
6398 ret = 0;
6399 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6400 crtc, pipe_config->base.state);
6401 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6402
6403 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006404}
6405
Ville Syrjälä1652d192015-03-31 14:12:01 +03006406static int skylake_get_display_clock_speed(struct drm_device *dev)
6407{
6408 struct drm_i915_private *dev_priv = to_i915(dev);
6409 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6410 uint32_t cdctl = I915_READ(CDCLK_CTL);
6411 uint32_t linkrate;
6412
6413 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6414 WARN(1, "LCPLL1 not enabled\n");
6415 return 24000; /* 24MHz is the cd freq with NSSC ref */
6416 }
6417
6418 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6419 return 540000;
6420
6421 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006422 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006423
Damien Lespiau71cd8422015-04-30 16:39:17 +01006424 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6425 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006426 /* vco 8640 */
6427 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6428 case CDCLK_FREQ_450_432:
6429 return 432000;
6430 case CDCLK_FREQ_337_308:
6431 return 308570;
6432 case CDCLK_FREQ_675_617:
6433 return 617140;
6434 default:
6435 WARN(1, "Unknown cd freq selection\n");
6436 }
6437 } else {
6438 /* vco 8100 */
6439 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6440 case CDCLK_FREQ_450_432:
6441 return 450000;
6442 case CDCLK_FREQ_337_308:
6443 return 337500;
6444 case CDCLK_FREQ_675_617:
6445 return 675000;
6446 default:
6447 WARN(1, "Unknown cd freq selection\n");
6448 }
6449 }
6450
6451 /* error case, do as if DPLL0 isn't enabled */
6452 return 24000;
6453}
6454
6455static int broadwell_get_display_clock_speed(struct drm_device *dev)
6456{
6457 struct drm_i915_private *dev_priv = dev->dev_private;
6458 uint32_t lcpll = I915_READ(LCPLL_CTL);
6459 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6460
6461 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6462 return 800000;
6463 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6464 return 450000;
6465 else if (freq == LCPLL_CLK_FREQ_450)
6466 return 450000;
6467 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6468 return 540000;
6469 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6470 return 337500;
6471 else
6472 return 675000;
6473}
6474
6475static int haswell_get_display_clock_speed(struct drm_device *dev)
6476{
6477 struct drm_i915_private *dev_priv = dev->dev_private;
6478 uint32_t lcpll = I915_READ(LCPLL_CTL);
6479 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6480
6481 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6482 return 800000;
6483 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6484 return 450000;
6485 else if (freq == LCPLL_CLK_FREQ_450)
6486 return 450000;
6487 else if (IS_HSW_ULT(dev))
6488 return 337500;
6489 else
6490 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006491}
6492
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006493static int valleyview_get_display_clock_speed(struct drm_device *dev)
6494{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006495 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006496 u32 val;
6497 int divider;
6498
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006499 if (dev_priv->hpll_freq == 0)
6500 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6501
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006502 mutex_lock(&dev_priv->dpio_lock);
6503 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6504 mutex_unlock(&dev_priv->dpio_lock);
6505
6506 divider = val & DISPLAY_FREQUENCY_VALUES;
6507
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006508 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6509 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6510 "cdclk change in progress\n");
6511
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006512 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006513}
6514
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006515static int ilk_get_display_clock_speed(struct drm_device *dev)
6516{
6517 return 450000;
6518}
6519
Jesse Barnese70236a2009-09-21 10:42:27 -07006520static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006521{
Jesse Barnese70236a2009-09-21 10:42:27 -07006522 return 400000;
6523}
Jesse Barnes79e53942008-11-07 14:24:08 -08006524
Jesse Barnese70236a2009-09-21 10:42:27 -07006525static int i915_get_display_clock_speed(struct drm_device *dev)
6526{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006527 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006528}
Jesse Barnes79e53942008-11-07 14:24:08 -08006529
Jesse Barnese70236a2009-09-21 10:42:27 -07006530static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6531{
6532 return 200000;
6533}
Jesse Barnes79e53942008-11-07 14:24:08 -08006534
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006535static int pnv_get_display_clock_speed(struct drm_device *dev)
6536{
6537 u16 gcfgc = 0;
6538
6539 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6540
6541 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6542 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006543 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006544 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006545 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006546 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006547 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006548 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6549 return 200000;
6550 default:
6551 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6552 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006553 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006554 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006555 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006556 }
6557}
6558
Jesse Barnese70236a2009-09-21 10:42:27 -07006559static int i915gm_get_display_clock_speed(struct drm_device *dev)
6560{
6561 u16 gcfgc = 0;
6562
6563 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6564
6565 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006566 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006567 else {
6568 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6569 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006570 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006571 default:
6572 case GC_DISPLAY_CLOCK_190_200_MHZ:
6573 return 190000;
6574 }
6575 }
6576}
Jesse Barnes79e53942008-11-07 14:24:08 -08006577
Jesse Barnese70236a2009-09-21 10:42:27 -07006578static int i865_get_display_clock_speed(struct drm_device *dev)
6579{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006580 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006581}
6582
6583static int i855_get_display_clock_speed(struct drm_device *dev)
6584{
6585 u16 hpllcc = 0;
6586 /* Assume that the hardware is in the high speed state. This
6587 * should be the default.
6588 */
6589 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6590 case GC_CLOCK_133_200:
6591 case GC_CLOCK_100_200:
6592 return 200000;
6593 case GC_CLOCK_166_250:
6594 return 250000;
6595 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006596 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006597 }
6598
6599 /* Shouldn't happen */
6600 return 0;
6601}
6602
6603static int i830_get_display_clock_speed(struct drm_device *dev)
6604{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006605 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006606}
6607
Zhenyu Wang2c072452009-06-05 15:38:42 +08006608static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006609intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006610{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006611 while (*num > DATA_LINK_M_N_MASK ||
6612 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006613 *num >>= 1;
6614 *den >>= 1;
6615 }
6616}
6617
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006618static void compute_m_n(unsigned int m, unsigned int n,
6619 uint32_t *ret_m, uint32_t *ret_n)
6620{
6621 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6622 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6623 intel_reduce_m_n_ratio(ret_m, ret_n);
6624}
6625
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006626void
6627intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6628 int pixel_clock, int link_clock,
6629 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006630{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006631 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006632
6633 compute_m_n(bits_per_pixel * pixel_clock,
6634 link_clock * nlanes * 8,
6635 &m_n->gmch_m, &m_n->gmch_n);
6636
6637 compute_m_n(pixel_clock, link_clock,
6638 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006639}
6640
Chris Wilsona7615032011-01-12 17:04:08 +00006641static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6642{
Jani Nikulad330a952014-01-21 11:24:25 +02006643 if (i915.panel_use_ssc >= 0)
6644 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006645 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006646 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006647}
6648
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006649static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6650 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006651{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006652 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006653 struct drm_i915_private *dev_priv = dev->dev_private;
6654 int refclk;
6655
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006656 WARN_ON(!crtc_state->base.state);
6657
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006658 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006659 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006660 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006661 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006662 refclk = dev_priv->vbt.lvds_ssc_freq;
6663 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006664 } else if (!IS_GEN2(dev)) {
6665 refclk = 96000;
6666 } else {
6667 refclk = 48000;
6668 }
6669
6670 return refclk;
6671}
6672
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006673static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006674{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006675 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006676}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006677
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006678static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6679{
6680 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006681}
6682
Daniel Vetterf47709a2013-03-28 10:42:02 +01006683static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006684 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006685 intel_clock_t *reduced_clock)
6686{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006687 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006688 u32 fp, fp2 = 0;
6689
6690 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006691 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006692 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006693 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006694 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006695 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006696 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006697 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006698 }
6699
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006700 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006701
Daniel Vetterf47709a2013-03-28 10:42:02 +01006702 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006703 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006704 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006705 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006706 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006707 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006708 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006709 }
6710}
6711
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006712static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6713 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006714{
6715 u32 reg_val;
6716
6717 /*
6718 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6719 * and set it to a reasonable value instead.
6720 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006721 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006722 reg_val &= 0xffffff00;
6723 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006724 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006725
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006726 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006727 reg_val &= 0x8cffffff;
6728 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006729 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006730
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006731 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006732 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006733 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006734
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006735 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006736 reg_val &= 0x00ffffff;
6737 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006738 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006739}
6740
Daniel Vetterb5518422013-05-03 11:49:48 +02006741static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6742 struct intel_link_m_n *m_n)
6743{
6744 struct drm_device *dev = crtc->base.dev;
6745 struct drm_i915_private *dev_priv = dev->dev_private;
6746 int pipe = crtc->pipe;
6747
Daniel Vettere3b95f12013-05-03 11:49:49 +02006748 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6749 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
6750 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
6751 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006752}
6753
6754static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07006755 struct intel_link_m_n *m_n,
6756 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02006757{
6758 struct drm_device *dev = crtc->base.dev;
6759 struct drm_i915_private *dev_priv = dev->dev_private;
6760 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006761 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02006762
6763 if (INTEL_INFO(dev)->gen >= 5) {
6764 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
6765 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
6766 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
6767 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07006768 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
6769 * for gen < 8) and if DRRS is supported (to make sure the
6770 * registers are not unnecessarily accessed).
6771 */
Durgadoss R44395bf2015-02-13 15:33:02 +05306772 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006773 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07006774 I915_WRITE(PIPE_DATA_M2(transcoder),
6775 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
6776 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
6777 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
6778 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
6779 }
Daniel Vetterb5518422013-05-03 11:49:48 +02006780 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02006781 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
6782 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
6783 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
6784 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02006785 }
6786}
6787
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306788void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006789{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306790 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
6791
6792 if (m_n == M1_N1) {
6793 dp_m_n = &crtc->config->dp_m_n;
6794 dp_m2_n2 = &crtc->config->dp_m2_n2;
6795 } else if (m_n == M2_N2) {
6796
6797 /*
6798 * M2_N2 registers are not supported. Hence m2_n2 divider value
6799 * needs to be programmed into M1_N1.
6800 */
6801 dp_m_n = &crtc->config->dp_m2_n2;
6802 } else {
6803 DRM_ERROR("Unsupported divider value\n");
6804 return;
6805 }
6806
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006807 if (crtc->config->has_pch_encoder)
6808 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006809 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306810 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02006811}
6812
Ville Syrjäläd288f652014-10-28 13:20:22 +02006813static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006814 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006815{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006816 u32 dpll, dpll_md;
6817
6818 /*
6819 * Enable DPIO clock input. We should never disable the reference
6820 * clock for pipe B, since VGA hotplug / manual detection depends
6821 * on it.
6822 */
6823 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
6824 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
6825 /* We should never disable this, set it here for state tracking */
6826 if (crtc->pipe == PIPE_B)
6827 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
6828 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006829 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006830
Ville Syrjäläd288f652014-10-28 13:20:22 +02006831 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006832 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02006833 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006834}
6835
Ville Syrjäläd288f652014-10-28 13:20:22 +02006836static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006837 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006838{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006839 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006840 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006841 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006842 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006843 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006844 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006845
Daniel Vetter09153002012-12-12 14:06:44 +01006846 mutex_lock(&dev_priv->dpio_lock);
6847
Ville Syrjäläd288f652014-10-28 13:20:22 +02006848 bestn = pipe_config->dpll.n;
6849 bestm1 = pipe_config->dpll.m1;
6850 bestm2 = pipe_config->dpll.m2;
6851 bestp1 = pipe_config->dpll.p1;
6852 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006853
Jesse Barnes89b667f2013-04-18 14:51:36 -07006854 /* See eDP HDMI DPIO driver vbios notes doc */
6855
6856 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006857 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006858 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006859
6860 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006861 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006862
6863 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006864 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006865 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006866 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006867
6868 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006869 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006870
6871 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006872 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
6873 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
6874 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006875 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07006876
6877 /*
6878 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
6879 * but we don't support that).
6880 * Note: don't use the DAC post divider as it seems unstable.
6881 */
6882 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006883 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006884
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006885 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006886 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006887
Jesse Barnes89b667f2013-04-18 14:51:36 -07006888 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02006889 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006890 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
6891 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006892 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03006893 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006894 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006895 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006896 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006897
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02006898 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07006899 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006900 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006901 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006902 0x0df40000);
6903 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006904 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006905 0x0df70000);
6906 } else { /* HDMI or VGA */
6907 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02006908 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006909 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006910 0x0df70000);
6911 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006912 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07006913 0x0df40000);
6914 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006915
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006916 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006917 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006918 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
6919 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006920 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006921 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006922
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006923 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01006924 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07006925}
6926
Ville Syrjäläd288f652014-10-28 13:20:22 +02006927static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006928 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006929{
Ville Syrjäläd288f652014-10-28 13:20:22 +02006930 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006931 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
6932 DPLL_VCO_ENABLE;
6933 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02006934 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006935
Ville Syrjäläd288f652014-10-28 13:20:22 +02006936 pipe_config->dpll_hw_state.dpll_md =
6937 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006938}
6939
Ville Syrjäläd288f652014-10-28 13:20:22 +02006940static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006941 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006942{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006943 struct drm_device *dev = crtc->base.dev;
6944 struct drm_i915_private *dev_priv = dev->dev_private;
6945 int pipe = crtc->pipe;
6946 int dpll_reg = DPLL(crtc->pipe);
6947 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306948 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006949 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306950 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306951 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006952
Ville Syrjäläd288f652014-10-28 13:20:22 +02006953 bestn = pipe_config->dpll.n;
6954 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
6955 bestm1 = pipe_config->dpll.m1;
6956 bestm2 = pipe_config->dpll.m2 >> 22;
6957 bestp1 = pipe_config->dpll.p1;
6958 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306959 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306960 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05306961 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006962
6963 /*
6964 * Enable Refclk and SSC
6965 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006966 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02006967 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03006968
6969 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006970
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006971 /* p1 and p2 divider */
6972 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
6973 5 << DPIO_CHV_S1_DIV_SHIFT |
6974 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
6975 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
6976 1 << DPIO_CHV_K_DIV_SHIFT);
6977
6978 /* Feedback post-divider - m2 */
6979 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
6980
6981 /* Feedback refclk divider - n and m1 */
6982 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
6983 DPIO_CHV_M1_DIV_BY_2 |
6984 1 << DPIO_CHV_N_DIV_SHIFT);
6985
6986 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306987 if (bestm2_frac)
6988 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006989
6990 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05306991 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
6992 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
6993 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
6994 if (bestm2_frac)
6995 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
6996 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006997
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05306998 /* Program digital lock detect threshold */
6999 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7000 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7001 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7002 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7003 if (!bestm2_frac)
7004 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7005 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7006
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007007 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307008 if (vco == 5400000) {
7009 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7010 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7011 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7012 tribuf_calcntr = 0x9;
7013 } else if (vco <= 6200000) {
7014 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7015 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7016 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7017 tribuf_calcntr = 0x9;
7018 } else if (vco <= 6480000) {
7019 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7020 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7021 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7022 tribuf_calcntr = 0x8;
7023 } else {
7024 /* Not supported. Apply the same limits as in the max case */
7025 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7026 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7027 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7028 tribuf_calcntr = 0;
7029 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007030 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7031
Ville Syrjälä968040b2015-03-11 22:52:08 +02007032 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307033 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7034 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7035 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7036
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007037 /* AFC Recal */
7038 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7039 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7040 DPIO_AFC_RECAL);
7041
7042 mutex_unlock(&dev_priv->dpio_lock);
7043}
7044
Ville Syrjäläd288f652014-10-28 13:20:22 +02007045/**
7046 * vlv_force_pll_on - forcibly enable just the PLL
7047 * @dev_priv: i915 private structure
7048 * @pipe: pipe PLL to enable
7049 * @dpll: PLL configuration
7050 *
7051 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7052 * in cases where we need the PLL enabled even when @pipe is not going to
7053 * be enabled.
7054 */
7055void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7056 const struct dpll *dpll)
7057{
7058 struct intel_crtc *crtc =
7059 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007060 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007061 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007062 .pixel_multiplier = 1,
7063 .dpll = *dpll,
7064 };
7065
7066 if (IS_CHERRYVIEW(dev)) {
7067 chv_update_pll(crtc, &pipe_config);
7068 chv_prepare_pll(crtc, &pipe_config);
7069 chv_enable_pll(crtc, &pipe_config);
7070 } else {
7071 vlv_update_pll(crtc, &pipe_config);
7072 vlv_prepare_pll(crtc, &pipe_config);
7073 vlv_enable_pll(crtc, &pipe_config);
7074 }
7075}
7076
7077/**
7078 * vlv_force_pll_off - forcibly disable just the PLL
7079 * @dev_priv: i915 private structure
7080 * @pipe: pipe PLL to disable
7081 *
7082 * Disable the PLL for @pipe. To be used in cases where we need
7083 * the PLL enabled even when @pipe is not going to be enabled.
7084 */
7085void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7086{
7087 if (IS_CHERRYVIEW(dev))
7088 chv_disable_pll(to_i915(dev), pipe);
7089 else
7090 vlv_disable_pll(to_i915(dev), pipe);
7091}
7092
Daniel Vetterf47709a2013-03-28 10:42:02 +01007093static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007094 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007095 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007096 int num_connectors)
7097{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007098 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007099 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007100 u32 dpll;
7101 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007102 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007103
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007104 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307105
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007106 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7107 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007108
7109 dpll = DPLL_VGA_MODE_DIS;
7110
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007111 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007112 dpll |= DPLLB_MODE_LVDS;
7113 else
7114 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007115
Daniel Vetteref1b4602013-06-01 17:17:04 +02007116 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007117 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007118 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007119 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007120
7121 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007122 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007123
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007124 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007125 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007126
7127 /* compute bitmask from p1 value */
7128 if (IS_PINEVIEW(dev))
7129 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7130 else {
7131 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7132 if (IS_G4X(dev) && reduced_clock)
7133 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7134 }
7135 switch (clock->p2) {
7136 case 5:
7137 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7138 break;
7139 case 7:
7140 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7141 break;
7142 case 10:
7143 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7144 break;
7145 case 14:
7146 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7147 break;
7148 }
7149 if (INTEL_INFO(dev)->gen >= 4)
7150 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7151
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007152 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007153 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007154 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007155 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7156 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7157 else
7158 dpll |= PLL_REF_INPUT_DREFCLK;
7159
7160 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007161 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007162
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007163 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007164 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007165 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007166 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007167 }
7168}
7169
Daniel Vetterf47709a2013-03-28 10:42:02 +01007170static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007171 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007172 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007173 int num_connectors)
7174{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007175 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007176 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007177 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007178 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007179
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007180 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307181
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007182 dpll = DPLL_VGA_MODE_DIS;
7183
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007184 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007185 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7186 } else {
7187 if (clock->p1 == 2)
7188 dpll |= PLL_P1_DIVIDE_BY_TWO;
7189 else
7190 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7191 if (clock->p2 == 4)
7192 dpll |= PLL_P2_DIVIDE_BY_4;
7193 }
7194
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007195 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007196 dpll |= DPLL_DVO_2X_MODE;
7197
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007198 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007199 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7200 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7201 else
7202 dpll |= PLL_REF_INPUT_DREFCLK;
7203
7204 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007205 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007206}
7207
Daniel Vetter8a654f32013-06-01 17:16:22 +02007208static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007209{
7210 struct drm_device *dev = intel_crtc->base.dev;
7211 struct drm_i915_private *dev_priv = dev->dev_private;
7212 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007213 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007214 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007215 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007216 uint32_t crtc_vtotal, crtc_vblank_end;
7217 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007218
7219 /* We need to be careful not to changed the adjusted mode, for otherwise
7220 * the hw state checker will get angry at the mismatch. */
7221 crtc_vtotal = adjusted_mode->crtc_vtotal;
7222 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007223
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007224 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007225 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007226 crtc_vtotal -= 1;
7227 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007228
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007229 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007230 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7231 else
7232 vsyncshift = adjusted_mode->crtc_hsync_start -
7233 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007234 if (vsyncshift < 0)
7235 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007236 }
7237
7238 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007239 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007240
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007241 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007242 (adjusted_mode->crtc_hdisplay - 1) |
7243 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007244 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007245 (adjusted_mode->crtc_hblank_start - 1) |
7246 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007247 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007248 (adjusted_mode->crtc_hsync_start - 1) |
7249 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7250
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007251 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007252 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007253 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007254 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007255 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007256 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007257 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007258 (adjusted_mode->crtc_vsync_start - 1) |
7259 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7260
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007261 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7262 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7263 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7264 * bits. */
7265 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7266 (pipe == PIPE_B || pipe == PIPE_C))
7267 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7268
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007269 /* pipesrc controls the size that is scaled from, which should
7270 * always be the user's requested size.
7271 */
7272 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007273 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7274 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007275}
7276
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007277static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007278 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007279{
7280 struct drm_device *dev = crtc->base.dev;
7281 struct drm_i915_private *dev_priv = dev->dev_private;
7282 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7283 uint32_t tmp;
7284
7285 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007286 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7287 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007288 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007289 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7290 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007291 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007292 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7293 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007294
7295 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007296 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7297 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007298 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007299 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7300 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007301 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007302 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7303 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007304
7305 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007306 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7307 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7308 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007309 }
7310
7311 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007312 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7313 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7314
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007315 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7316 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007317}
7318
Daniel Vetterf6a83282014-02-11 15:28:57 -08007319void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007320 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007321{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007322 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7323 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7324 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7325 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007326
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007327 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7328 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7329 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7330 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007331
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007332 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007333
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007334 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7335 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007336}
7337
Daniel Vetter84b046f2013-02-19 18:48:54 +01007338static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7339{
7340 struct drm_device *dev = intel_crtc->base.dev;
7341 struct drm_i915_private *dev_priv = dev->dev_private;
7342 uint32_t pipeconf;
7343
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007344 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007345
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007346 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7347 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7348 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007349
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007350 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007351 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007352
Daniel Vetterff9ce462013-04-24 14:57:17 +02007353 /* only g4x and later have fancy bpc/dither controls */
7354 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007355 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007356 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007357 pipeconf |= PIPECONF_DITHER_EN |
7358 PIPECONF_DITHER_TYPE_SP;
7359
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007360 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007361 case 18:
7362 pipeconf |= PIPECONF_6BPC;
7363 break;
7364 case 24:
7365 pipeconf |= PIPECONF_8BPC;
7366 break;
7367 case 30:
7368 pipeconf |= PIPECONF_10BPC;
7369 break;
7370 default:
7371 /* Case prevented by intel_choose_pipe_bpp_dither. */
7372 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007373 }
7374 }
7375
7376 if (HAS_PIPE_CXSR(dev)) {
7377 if (intel_crtc->lowfreq_avail) {
7378 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7379 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7380 } else {
7381 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007382 }
7383 }
7384
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007385 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007386 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007387 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007388 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7389 else
7390 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7391 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007392 pipeconf |= PIPECONF_PROGRESSIVE;
7393
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007394 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007395 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007396
Daniel Vetter84b046f2013-02-19 18:48:54 +01007397 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7398 POSTING_READ(PIPECONF(intel_crtc->pipe));
7399}
7400
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007401static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7402 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007403{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007404 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007405 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007406 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007407 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007408 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007409 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007410 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007411 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007412 struct drm_atomic_state *state = crtc_state->base.state;
7413 struct drm_connector_state *connector_state;
7414 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007415
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007416 for (i = 0; i < state->num_connector; i++) {
7417 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02007418 continue;
7419
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007420 connector_state = state->connector_states[i];
7421 if (connector_state->crtc != &crtc->base)
7422 continue;
7423
7424 encoder = to_intel_encoder(connector_state->best_encoder);
7425
Chris Wilson5eddb702010-09-11 13:48:45 +01007426 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007427 case INTEL_OUTPUT_LVDS:
7428 is_lvds = true;
7429 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007430 case INTEL_OUTPUT_DSI:
7431 is_dsi = true;
7432 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007433 default:
7434 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007435 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007436
Eric Anholtc751ce42010-03-25 11:48:48 -07007437 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007438 }
7439
Jani Nikulaf2335332013-09-13 11:03:09 +03007440 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007441 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007442
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007443 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007444 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007445
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007446 /*
7447 * Returns a set of divisors for the desired target clock with
7448 * the given refclk, or FALSE. The returned values represent
7449 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7450 * 2) / p1 / p2.
7451 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007452 limit = intel_limit(crtc_state, refclk);
7453 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007454 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007455 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007456 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007457 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7458 return -EINVAL;
7459 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007460
Jani Nikulaf2335332013-09-13 11:03:09 +03007461 if (is_lvds && dev_priv->lvds_downclock_avail) {
7462 /*
7463 * Ensure we match the reduced clock's P to the target
7464 * clock. If the clocks don't match, we can't switch
7465 * the display clock by using the FP0/FP1. In such case
7466 * we will disable the LVDS downclock feature.
7467 */
7468 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007469 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007470 dev_priv->lvds_downclock,
7471 refclk, &clock,
7472 &reduced_clock);
7473 }
7474 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007475 crtc_state->dpll.n = clock.n;
7476 crtc_state->dpll.m1 = clock.m1;
7477 crtc_state->dpll.m2 = clock.m2;
7478 crtc_state->dpll.p1 = clock.p1;
7479 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007480 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007481
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007482 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007483 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307484 has_reduced_clock ? &reduced_clock : NULL,
7485 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007486 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007487 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007488 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007489 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007490 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007491 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007492 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007493 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007494 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007495
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007496 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007497}
7498
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007499static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007500 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007501{
7502 struct drm_device *dev = crtc->base.dev;
7503 struct drm_i915_private *dev_priv = dev->dev_private;
7504 uint32_t tmp;
7505
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007506 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7507 return;
7508
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007509 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007510 if (!(tmp & PFIT_ENABLE))
7511 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007512
Daniel Vetter06922822013-07-11 13:35:40 +02007513 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007514 if (INTEL_INFO(dev)->gen < 4) {
7515 if (crtc->pipe != PIPE_B)
7516 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007517 } else {
7518 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7519 return;
7520 }
7521
Daniel Vetter06922822013-07-11 13:35:40 +02007522 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007523 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7524 if (INTEL_INFO(dev)->gen < 5)
7525 pipe_config->gmch_pfit.lvds_border_bits =
7526 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7527}
7528
Jesse Barnesacbec812013-09-20 11:29:32 -07007529static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007530 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007531{
7532 struct drm_device *dev = crtc->base.dev;
7533 struct drm_i915_private *dev_priv = dev->dev_private;
7534 int pipe = pipe_config->cpu_transcoder;
7535 intel_clock_t clock;
7536 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007537 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007538
Shobhit Kumarf573de52014-07-30 20:32:37 +05307539 /* In case of MIPI DPLL will not even be used */
7540 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7541 return;
7542
Jesse Barnesacbec812013-09-20 11:29:32 -07007543 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007544 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007545 mutex_unlock(&dev_priv->dpio_lock);
7546
7547 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7548 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7549 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7550 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7551 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7552
Ville Syrjäläf6466282013-10-14 14:50:31 +03007553 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007554
Ville Syrjäläf6466282013-10-14 14:50:31 +03007555 /* clock.dot is the fast clock */
7556 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007557}
7558
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007559static void
7560i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7561 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007562{
7563 struct drm_device *dev = crtc->base.dev;
7564 struct drm_i915_private *dev_priv = dev->dev_private;
7565 u32 val, base, offset;
7566 int pipe = crtc->pipe, plane = crtc->plane;
7567 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007568 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007569 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007570 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007571
Damien Lespiau42a7b082015-02-05 19:35:13 +00007572 val = I915_READ(DSPCNTR(plane));
7573 if (!(val & DISPLAY_PLANE_ENABLE))
7574 return;
7575
Damien Lespiaud9806c92015-01-21 14:07:19 +00007576 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007577 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007578 DRM_DEBUG_KMS("failed to alloc fb\n");
7579 return;
7580 }
7581
Damien Lespiau1b842c82015-01-21 13:50:54 +00007582 fb = &intel_fb->base;
7583
Daniel Vetter18c52472015-02-10 17:16:09 +00007584 if (INTEL_INFO(dev)->gen >= 4) {
7585 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007586 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007587 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7588 }
7589 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007590
7591 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007592 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007593 fb->pixel_format = fourcc;
7594 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007595
7596 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007597 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007598 offset = I915_READ(DSPTILEOFF(plane));
7599 else
7600 offset = I915_READ(DSPLINOFF(plane));
7601 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7602 } else {
7603 base = I915_READ(DSPADDR(plane));
7604 }
7605 plane_config->base = base;
7606
7607 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007608 fb->width = ((val >> 16) & 0xfff) + 1;
7609 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007610
7611 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007612 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007613
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007614 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007615 fb->pixel_format,
7616 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007617
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007618 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007619
Damien Lespiau2844a922015-01-20 12:51:48 +00007620 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7621 pipe_name(pipe), plane, fb->width, fb->height,
7622 fb->bits_per_pixel, base, fb->pitches[0],
7623 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007624
Damien Lespiau2d140302015-02-05 17:22:18 +00007625 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007626}
7627
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007628static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007629 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007630{
7631 struct drm_device *dev = crtc->base.dev;
7632 struct drm_i915_private *dev_priv = dev->dev_private;
7633 int pipe = pipe_config->cpu_transcoder;
7634 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7635 intel_clock_t clock;
7636 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7637 int refclk = 100000;
7638
7639 mutex_lock(&dev_priv->dpio_lock);
7640 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7641 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7642 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7643 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7644 mutex_unlock(&dev_priv->dpio_lock);
7645
7646 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7647 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7648 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7649 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7650 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7651
7652 chv_clock(refclk, &clock);
7653
7654 /* clock.dot is the fast clock */
7655 pipe_config->port_clock = clock.dot / 5;
7656}
7657
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007658static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007659 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007660{
7661 struct drm_device *dev = crtc->base.dev;
7662 struct drm_i915_private *dev_priv = dev->dev_private;
7663 uint32_t tmp;
7664
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007665 if (!intel_display_power_is_enabled(dev_priv,
7666 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007667 return false;
7668
Daniel Vettere143a212013-07-04 12:01:15 +02007669 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007670 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007671
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007672 tmp = I915_READ(PIPECONF(crtc->pipe));
7673 if (!(tmp & PIPECONF_ENABLE))
7674 return false;
7675
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007676 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7677 switch (tmp & PIPECONF_BPC_MASK) {
7678 case PIPECONF_6BPC:
7679 pipe_config->pipe_bpp = 18;
7680 break;
7681 case PIPECONF_8BPC:
7682 pipe_config->pipe_bpp = 24;
7683 break;
7684 case PIPECONF_10BPC:
7685 pipe_config->pipe_bpp = 30;
7686 break;
7687 default:
7688 break;
7689 }
7690 }
7691
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007692 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7693 pipe_config->limited_color_range = true;
7694
Ville Syrjälä282740f2013-09-04 18:30:03 +03007695 if (INTEL_INFO(dev)->gen < 4)
7696 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7697
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007698 intel_get_pipe_timings(crtc, pipe_config);
7699
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007700 i9xx_get_pfit_config(crtc, pipe_config);
7701
Daniel Vetter6c49f242013-06-06 12:45:25 +02007702 if (INTEL_INFO(dev)->gen >= 4) {
7703 tmp = I915_READ(DPLL_MD(crtc->pipe));
7704 pipe_config->pixel_multiplier =
7705 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7706 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007707 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007708 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7709 tmp = I915_READ(DPLL(crtc->pipe));
7710 pipe_config->pixel_multiplier =
7711 ((tmp & SDVO_MULTIPLIER_MASK)
7712 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7713 } else {
7714 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7715 * port and will be fixed up in the encoder->get_config
7716 * function. */
7717 pipe_config->pixel_multiplier = 1;
7718 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007719 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7720 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007721 /*
7722 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7723 * on 830. Filter it out here so that we don't
7724 * report errors due to that.
7725 */
7726 if (IS_I830(dev))
7727 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7728
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007729 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7730 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007731 } else {
7732 /* Mask out read-only status bits. */
7733 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7734 DPLL_PORTC_READY_MASK |
7735 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007736 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007737
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007738 if (IS_CHERRYVIEW(dev))
7739 chv_crtc_clock_get(crtc, pipe_config);
7740 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007741 vlv_crtc_clock_get(crtc, pipe_config);
7742 else
7743 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03007744
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007745 return true;
7746}
7747
Paulo Zanonidde86e22012-12-01 12:04:25 -02007748static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07007749{
7750 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007751 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007752 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007753 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007754 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07007755 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07007756 bool has_ck505 = false;
7757 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007758
7759 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01007760 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07007761 switch (encoder->type) {
7762 case INTEL_OUTPUT_LVDS:
7763 has_panel = true;
7764 has_lvds = true;
7765 break;
7766 case INTEL_OUTPUT_EDP:
7767 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03007768 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07007769 has_cpu_edp = true;
7770 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007771 default:
7772 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007773 }
7774 }
7775
Keith Packard99eb6a02011-09-26 14:29:12 -07007776 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007777 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07007778 can_ssc = has_ck505;
7779 } else {
7780 has_ck505 = false;
7781 can_ssc = true;
7782 }
7783
Imre Deak2de69052013-05-08 13:14:04 +03007784 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
7785 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007786
7787 /* Ironlake: try to setup display ref clock before DPLL
7788 * enabling. This is only under driver's control after
7789 * PCH B stepping, previous chipset stepping should be
7790 * ignoring this setting.
7791 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007792 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007793
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007794 /* As we must carefully and slowly disable/enable each source in turn,
7795 * compute the final state we want first and check if we need to
7796 * make any changes at all.
7797 */
7798 final = val;
7799 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07007800 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007801 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07007802 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007803 final |= DREF_NONSPREAD_SOURCE_ENABLE;
7804
7805 final &= ~DREF_SSC_SOURCE_MASK;
7806 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
7807 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007808
Keith Packard199e5d72011-09-22 12:01:57 -07007809 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007810 final |= DREF_SSC_SOURCE_ENABLE;
7811
7812 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7813 final |= DREF_SSC1_ENABLE;
7814
7815 if (has_cpu_edp) {
7816 if (intel_panel_use_ssc(dev_priv) && can_ssc)
7817 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
7818 else
7819 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
7820 } else
7821 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7822 } else {
7823 final |= DREF_SSC_SOURCE_DISABLE;
7824 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
7825 }
7826
7827 if (final == val)
7828 return;
7829
7830 /* Always enable nonspread source */
7831 val &= ~DREF_NONSPREAD_SOURCE_MASK;
7832
7833 if (has_ck505)
7834 val |= DREF_NONSPREAD_CK505_ENABLE;
7835 else
7836 val |= DREF_NONSPREAD_SOURCE_ENABLE;
7837
7838 if (has_panel) {
7839 val &= ~DREF_SSC_SOURCE_MASK;
7840 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007841
Keith Packard199e5d72011-09-22 12:01:57 -07007842 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07007843 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007844 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007845 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02007846 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007847 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007848
7849 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007850 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007851 POSTING_READ(PCH_DREF_CONTROL);
7852 udelay(200);
7853
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007854 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07007855
7856 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07007857 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07007858 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07007859 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007860 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02007861 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007862 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07007863 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007864 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007865
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007866 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007867 POSTING_READ(PCH_DREF_CONTROL);
7868 udelay(200);
7869 } else {
7870 DRM_DEBUG_KMS("Disabling SSC entirely\n");
7871
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007872 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07007873
7874 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007875 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007876
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007877 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07007878 POSTING_READ(PCH_DREF_CONTROL);
7879 udelay(200);
7880
7881 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007882 val &= ~DREF_SSC_SOURCE_MASK;
7883 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007884
7885 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007886 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07007887
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007888 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007889 POSTING_READ(PCH_DREF_CONTROL);
7890 udelay(200);
7891 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07007892
7893 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07007894}
7895
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007896static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02007897{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007898 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007899
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007900 tmp = I915_READ(SOUTH_CHICKEN2);
7901 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
7902 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007903
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007904 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
7905 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
7906 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02007907
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007908 tmp = I915_READ(SOUTH_CHICKEN2);
7909 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
7910 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007911
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007912 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
7913 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
7914 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007915}
7916
7917/* WaMPhyProgramming:hsw */
7918static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
7919{
7920 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02007921
7922 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
7923 tmp &= ~(0xFF << 24);
7924 tmp |= (0x12 << 24);
7925 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
7926
Paulo Zanonidde86e22012-12-01 12:04:25 -02007927 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
7928 tmp |= (1 << 11);
7929 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
7930
7931 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
7932 tmp |= (1 << 11);
7933 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
7934
Paulo Zanonidde86e22012-12-01 12:04:25 -02007935 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
7936 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7937 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
7938
7939 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
7940 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
7941 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
7942
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007943 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
7944 tmp &= ~(7 << 13);
7945 tmp |= (5 << 13);
7946 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007947
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007948 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
7949 tmp &= ~(7 << 13);
7950 tmp |= (5 << 13);
7951 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007952
7953 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
7954 tmp &= ~0xFF;
7955 tmp |= 0x1C;
7956 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
7957
7958 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
7959 tmp &= ~0xFF;
7960 tmp |= 0x1C;
7961 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
7962
7963 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
7964 tmp &= ~(0xFF << 16);
7965 tmp |= (0x1C << 16);
7966 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
7967
7968 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
7969 tmp &= ~(0xFF << 16);
7970 tmp |= (0x1C << 16);
7971 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
7972
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007973 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
7974 tmp |= (1 << 27);
7975 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007976
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007977 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
7978 tmp |= (1 << 27);
7979 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007980
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007981 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
7982 tmp &= ~(0xF << 28);
7983 tmp |= (4 << 28);
7984 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02007985
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03007986 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
7987 tmp &= ~(0xF << 28);
7988 tmp |= (4 << 28);
7989 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03007990}
7991
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03007992/* Implements 3 different sequences from BSpec chapter "Display iCLK
7993 * Programming" based on the parameters passed:
7994 * - Sequence to enable CLKOUT_DP
7995 * - Sequence to enable CLKOUT_DP without spread
7996 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
7997 */
7998static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
7999 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008000{
8001 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008002 uint32_t reg, tmp;
8003
8004 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8005 with_spread = true;
8006 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8007 with_fdi, "LP PCH doesn't have FDI\n"))
8008 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008009
8010 mutex_lock(&dev_priv->dpio_lock);
8011
8012 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8013 tmp &= ~SBI_SSCCTL_DISABLE;
8014 tmp |= SBI_SSCCTL_PATHALT;
8015 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8016
8017 udelay(24);
8018
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008019 if (with_spread) {
8020 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8021 tmp &= ~SBI_SSCCTL_PATHALT;
8022 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008023
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008024 if (with_fdi) {
8025 lpt_reset_fdi_mphy(dev_priv);
8026 lpt_program_fdi_mphy(dev_priv);
8027 }
8028 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008029
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008030 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8031 SBI_GEN0 : SBI_DBUFF0;
8032 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8033 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8034 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008035
8036 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008037}
8038
Paulo Zanoni47701c32013-07-23 11:19:25 -03008039/* Sequence to disable CLKOUT_DP */
8040static void lpt_disable_clkout_dp(struct drm_device *dev)
8041{
8042 struct drm_i915_private *dev_priv = dev->dev_private;
8043 uint32_t reg, tmp;
8044
8045 mutex_lock(&dev_priv->dpio_lock);
8046
8047 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8048 SBI_GEN0 : SBI_DBUFF0;
8049 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8050 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8051 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8052
8053 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8054 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8055 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8056 tmp |= SBI_SSCCTL_PATHALT;
8057 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8058 udelay(32);
8059 }
8060 tmp |= SBI_SSCCTL_DISABLE;
8061 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8062 }
8063
8064 mutex_unlock(&dev_priv->dpio_lock);
8065}
8066
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008067static void lpt_init_pch_refclk(struct drm_device *dev)
8068{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008069 struct intel_encoder *encoder;
8070 bool has_vga = false;
8071
Damien Lespiaub2784e12014-08-05 11:29:37 +01008072 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008073 switch (encoder->type) {
8074 case INTEL_OUTPUT_ANALOG:
8075 has_vga = true;
8076 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008077 default:
8078 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008079 }
8080 }
8081
Paulo Zanoni47701c32013-07-23 11:19:25 -03008082 if (has_vga)
8083 lpt_enable_clkout_dp(dev, true, true);
8084 else
8085 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008086}
8087
Paulo Zanonidde86e22012-12-01 12:04:25 -02008088/*
8089 * Initialize reference clocks when the driver loads
8090 */
8091void intel_init_pch_refclk(struct drm_device *dev)
8092{
8093 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8094 ironlake_init_pch_refclk(dev);
8095 else if (HAS_PCH_LPT(dev))
8096 lpt_init_pch_refclk(dev);
8097}
8098
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008099static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008100{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008101 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008102 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008103 struct drm_atomic_state *state = crtc_state->base.state;
8104 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008105 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008106 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008107 bool is_lvds = false;
8108
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008109 for (i = 0; i < state->num_connector; i++) {
8110 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008111 continue;
8112
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008113 connector_state = state->connector_states[i];
8114 if (connector_state->crtc != crtc_state->base.crtc)
8115 continue;
8116
8117 encoder = to_intel_encoder(connector_state->best_encoder);
8118
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008119 switch (encoder->type) {
8120 case INTEL_OUTPUT_LVDS:
8121 is_lvds = true;
8122 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008123 default:
8124 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008125 }
8126 num_connectors++;
8127 }
8128
8129 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008130 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008131 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008132 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008133 }
8134
8135 return 120000;
8136}
8137
Daniel Vetter6ff93602013-04-19 11:24:36 +02008138static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008139{
8140 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8141 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8142 int pipe = intel_crtc->pipe;
8143 uint32_t val;
8144
Daniel Vetter78114072013-06-13 00:54:57 +02008145 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008146
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008147 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008148 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008149 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008150 break;
8151 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008152 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008153 break;
8154 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008155 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008156 break;
8157 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008158 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008159 break;
8160 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008161 /* Case prevented by intel_choose_pipe_bpp_dither. */
8162 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008163 }
8164
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008165 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008166 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8167
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008168 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008169 val |= PIPECONF_INTERLACED_ILK;
8170 else
8171 val |= PIPECONF_PROGRESSIVE;
8172
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008173 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008174 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008175
Paulo Zanonic8203562012-09-12 10:06:29 -03008176 I915_WRITE(PIPECONF(pipe), val);
8177 POSTING_READ(PIPECONF(pipe));
8178}
8179
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008180/*
8181 * Set up the pipe CSC unit.
8182 *
8183 * Currently only full range RGB to limited range RGB conversion
8184 * is supported, but eventually this should handle various
8185 * RGB<->YCbCr scenarios as well.
8186 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008187static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008188{
8189 struct drm_device *dev = crtc->dev;
8190 struct drm_i915_private *dev_priv = dev->dev_private;
8191 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8192 int pipe = intel_crtc->pipe;
8193 uint16_t coeff = 0x7800; /* 1.0 */
8194
8195 /*
8196 * TODO: Check what kind of values actually come out of the pipe
8197 * with these coeff/postoff values and adjust to get the best
8198 * accuracy. Perhaps we even need to take the bpc value into
8199 * consideration.
8200 */
8201
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008202 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008203 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8204
8205 /*
8206 * GY/GU and RY/RU should be the other way around according
8207 * to BSpec, but reality doesn't agree. Just set them up in
8208 * a way that results in the correct picture.
8209 */
8210 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8211 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8212
8213 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8214 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8215
8216 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8217 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8218
8219 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8220 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8221 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8222
8223 if (INTEL_INFO(dev)->gen > 6) {
8224 uint16_t postoff = 0;
8225
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008226 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008227 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008228
8229 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8230 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8231 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8232
8233 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8234 } else {
8235 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8236
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008237 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008238 mode |= CSC_BLACK_SCREEN_OFFSET;
8239
8240 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8241 }
8242}
8243
Daniel Vetter6ff93602013-04-19 11:24:36 +02008244static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008245{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008246 struct drm_device *dev = crtc->dev;
8247 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008248 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008249 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008250 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008251 uint32_t val;
8252
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008253 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008254
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008255 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008256 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8257
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008258 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008259 val |= PIPECONF_INTERLACED_ILK;
8260 else
8261 val |= PIPECONF_PROGRESSIVE;
8262
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008263 I915_WRITE(PIPECONF(cpu_transcoder), val);
8264 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008265
8266 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8267 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008268
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308269 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008270 val = 0;
8271
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008272 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008273 case 18:
8274 val |= PIPEMISC_DITHER_6_BPC;
8275 break;
8276 case 24:
8277 val |= PIPEMISC_DITHER_8_BPC;
8278 break;
8279 case 30:
8280 val |= PIPEMISC_DITHER_10_BPC;
8281 break;
8282 case 36:
8283 val |= PIPEMISC_DITHER_12_BPC;
8284 break;
8285 default:
8286 /* Case prevented by pipe_config_set_bpp. */
8287 BUG();
8288 }
8289
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008290 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008291 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8292
8293 I915_WRITE(PIPEMISC(pipe), val);
8294 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008295}
8296
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008297static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008298 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008299 intel_clock_t *clock,
8300 bool *has_reduced_clock,
8301 intel_clock_t *reduced_clock)
8302{
8303 struct drm_device *dev = crtc->dev;
8304 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008305 int refclk;
8306 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008307 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008308
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008309 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008310
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008311 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008312
8313 /*
8314 * Returns a set of divisors for the desired target clock with the given
8315 * refclk, or FALSE. The returned values represent the clock equation:
8316 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8317 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008318 limit = intel_limit(crtc_state, refclk);
8319 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008320 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008321 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008322 if (!ret)
8323 return false;
8324
8325 if (is_lvds && dev_priv->lvds_downclock_avail) {
8326 /*
8327 * Ensure we match the reduced clock's P to the target clock.
8328 * If the clocks don't match, we can't switch the display clock
8329 * by using the FP0/FP1. In such case we will disable the LVDS
8330 * downclock feature.
8331 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008332 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008333 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008334 dev_priv->lvds_downclock,
8335 refclk, clock,
8336 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008337 }
8338
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008339 return true;
8340}
8341
Paulo Zanonid4b19312012-11-29 11:29:32 -02008342int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8343{
8344 /*
8345 * Account for spread spectrum to avoid
8346 * oversubscribing the link. Max center spread
8347 * is 2.5%; use 5% for safety's sake.
8348 */
8349 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008350 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008351}
8352
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008353static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008354{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008355 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008356}
8357
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008358static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008359 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008360 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008361 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008362{
8363 struct drm_crtc *crtc = &intel_crtc->base;
8364 struct drm_device *dev = crtc->dev;
8365 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008366 struct drm_atomic_state *state = crtc_state->base.state;
8367 struct drm_connector_state *connector_state;
8368 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008369 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008370 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008371 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008372
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008373 for (i = 0; i < state->num_connector; i++) {
8374 if (!state->connectors[i])
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +02008375 continue;
8376
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008377 connector_state = state->connector_states[i];
8378 if (connector_state->crtc != crtc_state->base.crtc)
8379 continue;
8380
8381 encoder = to_intel_encoder(connector_state->best_encoder);
8382
8383 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008384 case INTEL_OUTPUT_LVDS:
8385 is_lvds = true;
8386 break;
8387 case INTEL_OUTPUT_SDVO:
8388 case INTEL_OUTPUT_HDMI:
8389 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008390 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008391 default:
8392 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008393 }
8394
8395 num_connectors++;
8396 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008397
Chris Wilsonc1858122010-12-03 21:35:48 +00008398 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008399 factor = 21;
8400 if (is_lvds) {
8401 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008402 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008403 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008404 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008405 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008406 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008407
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008408 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008409 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008410
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008411 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8412 *fp2 |= FP_CB_TUNE;
8413
Chris Wilson5eddb702010-09-11 13:48:45 +01008414 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008415
Eric Anholta07d6782011-03-30 13:01:08 -07008416 if (is_lvds)
8417 dpll |= DPLLB_MODE_LVDS;
8418 else
8419 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008420
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008421 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008422 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008423
8424 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008425 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008426 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008427 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008428
Eric Anholta07d6782011-03-30 13:01:08 -07008429 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008430 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008431 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008432 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008433
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008434 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008435 case 5:
8436 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8437 break;
8438 case 7:
8439 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8440 break;
8441 case 10:
8442 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8443 break;
8444 case 14:
8445 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8446 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008447 }
8448
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008449 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008450 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008451 else
8452 dpll |= PLL_REF_INPUT_DREFCLK;
8453
Daniel Vetter959e16d2013-06-05 13:34:21 +02008454 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008455}
8456
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008457static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8458 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008459{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008460 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008461 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008462 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008463 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008464 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008465 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008466
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008467 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008468
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008469 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8470 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8471
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008472 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008473 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008474 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008475 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8476 return -EINVAL;
8477 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008478 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008479 if (!crtc_state->clock_set) {
8480 crtc_state->dpll.n = clock.n;
8481 crtc_state->dpll.m1 = clock.m1;
8482 crtc_state->dpll.m2 = clock.m2;
8483 crtc_state->dpll.p1 = clock.p1;
8484 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008485 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008486
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008487 /* 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 +02008488 if (crtc_state->has_pch_encoder) {
8489 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008490 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008491 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008492
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008493 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008494 &fp, &reduced_clock,
8495 has_reduced_clock ? &fp2 : NULL);
8496
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008497 crtc_state->dpll_hw_state.dpll = dpll;
8498 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008499 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008500 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008501 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008502 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008503
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008504 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008505 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008506 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008507 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008508 return -EINVAL;
8509 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008510 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008511
Rodrigo Viviab585de2015-03-24 12:40:09 -07008512 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008513 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008514 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008515 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008516
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008517 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008518}
8519
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008520static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8521 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008522{
8523 struct drm_device *dev = crtc->base.dev;
8524 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008525 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008526
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008527 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8528 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8529 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8530 & ~TU_SIZE_MASK;
8531 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8532 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8533 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8534}
8535
8536static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8537 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008538 struct intel_link_m_n *m_n,
8539 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008540{
8541 struct drm_device *dev = crtc->base.dev;
8542 struct drm_i915_private *dev_priv = dev->dev_private;
8543 enum pipe pipe = crtc->pipe;
8544
8545 if (INTEL_INFO(dev)->gen >= 5) {
8546 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8547 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8548 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8549 & ~TU_SIZE_MASK;
8550 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8551 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8552 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008553 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8554 * gen < 8) and if DRRS is supported (to make sure the
8555 * registers are not unnecessarily read).
8556 */
8557 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008558 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008559 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8560 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8561 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8562 & ~TU_SIZE_MASK;
8563 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8564 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8565 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8566 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008567 } else {
8568 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8569 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8570 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8571 & ~TU_SIZE_MASK;
8572 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8573 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8574 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8575 }
8576}
8577
8578void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008579 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008580{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008581 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008582 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8583 else
8584 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008585 &pipe_config->dp_m_n,
8586 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008587}
8588
Daniel Vetter72419202013-04-04 13:28:53 +02008589static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008590 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008591{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008592 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008593 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008594}
8595
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008596static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008597 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008598{
8599 struct drm_device *dev = crtc->base.dev;
8600 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008601 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8602 uint32_t ps_ctrl = 0;
8603 int id = -1;
8604 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008605
Chandra Kondurua1b22782015-04-07 15:28:45 -07008606 /* find scaler attached to this pipe */
8607 for (i = 0; i < crtc->num_scalers; i++) {
8608 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8609 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8610 id = i;
8611 pipe_config->pch_pfit.enabled = true;
8612 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8613 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8614 break;
8615 }
8616 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008617
Chandra Kondurua1b22782015-04-07 15:28:45 -07008618 scaler_state->scaler_id = id;
8619 if (id >= 0) {
8620 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8621 } else {
8622 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008623 }
8624}
8625
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008626static void
8627skylake_get_initial_plane_config(struct intel_crtc *crtc,
8628 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008629{
8630 struct drm_device *dev = crtc->base.dev;
8631 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008632 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008633 int pipe = crtc->pipe;
8634 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008635 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008636 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008637 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008638
Damien Lespiaud9806c92015-01-21 14:07:19 +00008639 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008640 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008641 DRM_DEBUG_KMS("failed to alloc fb\n");
8642 return;
8643 }
8644
Damien Lespiau1b842c82015-01-21 13:50:54 +00008645 fb = &intel_fb->base;
8646
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008647 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008648 if (!(val & PLANE_CTL_ENABLE))
8649 goto error;
8650
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008651 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8652 fourcc = skl_format_to_fourcc(pixel_format,
8653 val & PLANE_CTL_ORDER_RGBX,
8654 val & PLANE_CTL_ALPHA_MASK);
8655 fb->pixel_format = fourcc;
8656 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8657
Damien Lespiau40f46282015-02-27 11:15:21 +00008658 tiling = val & PLANE_CTL_TILED_MASK;
8659 switch (tiling) {
8660 case PLANE_CTL_TILED_LINEAR:
8661 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8662 break;
8663 case PLANE_CTL_TILED_X:
8664 plane_config->tiling = I915_TILING_X;
8665 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8666 break;
8667 case PLANE_CTL_TILED_Y:
8668 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8669 break;
8670 case PLANE_CTL_TILED_YF:
8671 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8672 break;
8673 default:
8674 MISSING_CASE(tiling);
8675 goto error;
8676 }
8677
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008678 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8679 plane_config->base = base;
8680
8681 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8682
8683 val = I915_READ(PLANE_SIZE(pipe, 0));
8684 fb->height = ((val >> 16) & 0xfff) + 1;
8685 fb->width = ((val >> 0) & 0x1fff) + 1;
8686
8687 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008688 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8689 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008690 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8691
8692 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008693 fb->pixel_format,
8694 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008695
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008696 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008697
8698 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8699 pipe_name(pipe), fb->width, fb->height,
8700 fb->bits_per_pixel, base, fb->pitches[0],
8701 plane_config->size);
8702
Damien Lespiau2d140302015-02-05 17:22:18 +00008703 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008704 return;
8705
8706error:
8707 kfree(fb);
8708}
8709
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008710static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008711 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008712{
8713 struct drm_device *dev = crtc->base.dev;
8714 struct drm_i915_private *dev_priv = dev->dev_private;
8715 uint32_t tmp;
8716
8717 tmp = I915_READ(PF_CTL(crtc->pipe));
8718
8719 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008720 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008721 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8722 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008723
8724 /* We currently do not free assignements of panel fitters on
8725 * ivb/hsw (since we don't use the higher upscaling modes which
8726 * differentiates them) so just WARN about this case for now. */
8727 if (IS_GEN7(dev)) {
8728 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8729 PF_PIPE_SEL_IVB(crtc->pipe));
8730 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008731 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008732}
8733
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008734static void
8735ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8736 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008737{
8738 struct drm_device *dev = crtc->base.dev;
8739 struct drm_i915_private *dev_priv = dev->dev_private;
8740 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008741 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008742 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008743 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008744 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008745 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008746
Damien Lespiau42a7b082015-02-05 19:35:13 +00008747 val = I915_READ(DSPCNTR(pipe));
8748 if (!(val & DISPLAY_PLANE_ENABLE))
8749 return;
8750
Damien Lespiaud9806c92015-01-21 14:07:19 +00008751 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008752 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008753 DRM_DEBUG_KMS("failed to alloc fb\n");
8754 return;
8755 }
8756
Damien Lespiau1b842c82015-01-21 13:50:54 +00008757 fb = &intel_fb->base;
8758
Daniel Vetter18c52472015-02-10 17:16:09 +00008759 if (INTEL_INFO(dev)->gen >= 4) {
8760 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008761 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008762 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8763 }
8764 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008765
8766 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008767 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008768 fb->pixel_format = fourcc;
8769 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008770
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008771 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008772 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008773 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008774 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00008775 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008776 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008777 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008778 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008779 }
8780 plane_config->base = base;
8781
8782 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008783 fb->width = ((val >> 16) & 0xfff) + 1;
8784 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008785
8786 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008787 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008788
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008789 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008790 fb->pixel_format,
8791 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008792
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008793 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008794
Damien Lespiau2844a922015-01-20 12:51:48 +00008795 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8796 pipe_name(pipe), fb->width, fb->height,
8797 fb->bits_per_pixel, base, fb->pitches[0],
8798 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008799
Damien Lespiau2d140302015-02-05 17:22:18 +00008800 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008801}
8802
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008803static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008804 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008805{
8806 struct drm_device *dev = crtc->base.dev;
8807 struct drm_i915_private *dev_priv = dev->dev_private;
8808 uint32_t tmp;
8809
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008810 if (!intel_display_power_is_enabled(dev_priv,
8811 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03008812 return false;
8813
Daniel Vettere143a212013-07-04 12:01:15 +02008814 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008815 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008816
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008817 tmp = I915_READ(PIPECONF(crtc->pipe));
8818 if (!(tmp & PIPECONF_ENABLE))
8819 return false;
8820
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008821 switch (tmp & PIPECONF_BPC_MASK) {
8822 case PIPECONF_6BPC:
8823 pipe_config->pipe_bpp = 18;
8824 break;
8825 case PIPECONF_8BPC:
8826 pipe_config->pipe_bpp = 24;
8827 break;
8828 case PIPECONF_10BPC:
8829 pipe_config->pipe_bpp = 30;
8830 break;
8831 case PIPECONF_12BPC:
8832 pipe_config->pipe_bpp = 36;
8833 break;
8834 default:
8835 break;
8836 }
8837
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008838 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
8839 pipe_config->limited_color_range = true;
8840
Daniel Vetterab9412b2013-05-03 11:49:46 +02008841 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02008842 struct intel_shared_dpll *pll;
8843
Daniel Vetter88adfff2013-03-28 10:42:01 +01008844 pipe_config->has_pch_encoder = true;
8845
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008846 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
8847 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
8848 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02008849
8850 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008851
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008852 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02008853 pipe_config->shared_dpll =
8854 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008855 } else {
8856 tmp = I915_READ(PCH_DPLL_SEL);
8857 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
8858 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
8859 else
8860 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
8861 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02008862
8863 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
8864
8865 WARN_ON(!pll->get_hw_state(dev_priv, pll,
8866 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02008867
8868 tmp = pipe_config->dpll_hw_state.dpll;
8869 pipe_config->pixel_multiplier =
8870 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
8871 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03008872
8873 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02008874 } else {
8875 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02008876 }
8877
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008878 intel_get_pipe_timings(crtc, pipe_config);
8879
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008880 ironlake_get_pfit_config(crtc, pipe_config);
8881
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008882 return true;
8883}
8884
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008885static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
8886{
8887 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008888 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008889
Damien Lespiaud3fcc802014-05-13 23:32:22 +01008890 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05008891 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008892 pipe_name(crtc->pipe));
8893
Rob Clarke2c719b2014-12-15 13:56:32 -05008894 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
8895 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
8896 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
8897 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
8898 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
8899 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008900 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03008901 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05008902 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03008903 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008904 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008905 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008906 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008907 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05008908 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008909
Paulo Zanoni9926ada2014-04-01 19:39:47 -03008910 /*
8911 * In theory we can still leave IRQs enabled, as long as only the HPD
8912 * interrupts remain enabled. We used to check for that, but since it's
8913 * gen-specific and since we only disable LCPLL after we fully disable
8914 * the interrupts, the check below should be enough.
8915 */
Rob Clarke2c719b2014-12-15 13:56:32 -05008916 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008917}
8918
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008919static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
8920{
8921 struct drm_device *dev = dev_priv->dev;
8922
8923 if (IS_HASWELL(dev))
8924 return I915_READ(D_COMP_HSW);
8925 else
8926 return I915_READ(D_COMP_BDW);
8927}
8928
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008929static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
8930{
8931 struct drm_device *dev = dev_priv->dev;
8932
8933 if (IS_HASWELL(dev)) {
8934 mutex_lock(&dev_priv->rps.hw_lock);
8935 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
8936 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03008937 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008938 mutex_unlock(&dev_priv->rps.hw_lock);
8939 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008940 I915_WRITE(D_COMP_BDW, val);
8941 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008942 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008943}
8944
8945/*
8946 * This function implements pieces of two sequences from BSpec:
8947 * - Sequence for display software to disable LCPLL
8948 * - Sequence for display software to allow package C8+
8949 * The steps implemented here are just the steps that actually touch the LCPLL
8950 * register. Callers should take care of disabling all the display engine
8951 * functions, doing the mode unset, fixing interrupts, etc.
8952 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03008953static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
8954 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008955{
8956 uint32_t val;
8957
8958 assert_can_disable_lcpll(dev_priv);
8959
8960 val = I915_READ(LCPLL_CTL);
8961
8962 if (switch_to_fclk) {
8963 val |= LCPLL_CD_SOURCE_FCLK;
8964 I915_WRITE(LCPLL_CTL, val);
8965
8966 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
8967 LCPLL_CD_SOURCE_FCLK_DONE, 1))
8968 DRM_ERROR("Switching to FCLK failed\n");
8969
8970 val = I915_READ(LCPLL_CTL);
8971 }
8972
8973 val |= LCPLL_PLL_DISABLE;
8974 I915_WRITE(LCPLL_CTL, val);
8975 POSTING_READ(LCPLL_CTL);
8976
8977 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
8978 DRM_ERROR("LCPLL still locked\n");
8979
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008980 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008981 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03008982 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008983 ndelay(100);
8984
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03008985 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
8986 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03008987 DRM_ERROR("D_COMP RCOMP still in progress\n");
8988
8989 if (allow_power_down) {
8990 val = I915_READ(LCPLL_CTL);
8991 val |= LCPLL_POWER_DOWN_ALLOW;
8992 I915_WRITE(LCPLL_CTL, val);
8993 POSTING_READ(LCPLL_CTL);
8994 }
8995}
8996
8997/*
8998 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
8999 * source.
9000 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009001static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009002{
9003 uint32_t val;
9004
9005 val = I915_READ(LCPLL_CTL);
9006
9007 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9008 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9009 return;
9010
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009011 /*
9012 * Make sure we're not on PC8 state before disabling PC8, otherwise
9013 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009014 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009015 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009016
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009017 if (val & LCPLL_POWER_DOWN_ALLOW) {
9018 val &= ~LCPLL_POWER_DOWN_ALLOW;
9019 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009020 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009021 }
9022
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009023 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009024 val |= D_COMP_COMP_FORCE;
9025 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009026 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009027
9028 val = I915_READ(LCPLL_CTL);
9029 val &= ~LCPLL_PLL_DISABLE;
9030 I915_WRITE(LCPLL_CTL, val);
9031
9032 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9033 DRM_ERROR("LCPLL not locked yet\n");
9034
9035 if (val & LCPLL_CD_SOURCE_FCLK) {
9036 val = I915_READ(LCPLL_CTL);
9037 val &= ~LCPLL_CD_SOURCE_FCLK;
9038 I915_WRITE(LCPLL_CTL, val);
9039
9040 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9041 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9042 DRM_ERROR("Switching back to LCPLL failed\n");
9043 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009044
Mika Kuoppala59bad942015-01-16 11:34:40 +02009045 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009046}
9047
Paulo Zanoni765dab672014-03-07 20:08:18 -03009048/*
9049 * Package states C8 and deeper are really deep PC states that can only be
9050 * reached when all the devices on the system allow it, so even if the graphics
9051 * device allows PC8+, it doesn't mean the system will actually get to these
9052 * states. Our driver only allows PC8+ when going into runtime PM.
9053 *
9054 * The requirements for PC8+ are that all the outputs are disabled, the power
9055 * well is disabled and most interrupts are disabled, and these are also
9056 * requirements for runtime PM. When these conditions are met, we manually do
9057 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9058 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9059 * hang the machine.
9060 *
9061 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9062 * the state of some registers, so when we come back from PC8+ we need to
9063 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9064 * need to take care of the registers kept by RC6. Notice that this happens even
9065 * if we don't put the device in PCI D3 state (which is what currently happens
9066 * because of the runtime PM support).
9067 *
9068 * For more, read "Display Sequences for Package C8" on the hardware
9069 * documentation.
9070 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009071void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009072{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009073 struct drm_device *dev = dev_priv->dev;
9074 uint32_t val;
9075
Paulo Zanonic67a4702013-08-19 13:18:09 -03009076 DRM_DEBUG_KMS("Enabling package C8+\n");
9077
Paulo Zanonic67a4702013-08-19 13:18:09 -03009078 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9079 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9080 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9081 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9082 }
9083
9084 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009085 hsw_disable_lcpll(dev_priv, true, true);
9086}
9087
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009088void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009089{
9090 struct drm_device *dev = dev_priv->dev;
9091 uint32_t val;
9092
Paulo Zanonic67a4702013-08-19 13:18:09 -03009093 DRM_DEBUG_KMS("Disabling package C8+\n");
9094
9095 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009096 lpt_init_pch_refclk(dev);
9097
9098 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9099 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9100 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9101 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9102 }
9103
9104 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009105}
9106
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309107static void broxton_modeset_global_resources(struct drm_atomic_state *state)
9108{
9109 struct drm_device *dev = state->dev;
9110 struct drm_i915_private *dev_priv = dev->dev_private;
9111 int max_pixclk = intel_mode_max_pixclk(state);
9112 int req_cdclk;
9113
9114 /* see the comment in valleyview_modeset_global_resources */
9115 if (WARN_ON(max_pixclk < 0))
9116 return;
9117
9118 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9119
9120 if (req_cdclk != dev_priv->cdclk_freq)
9121 broxton_set_cdclk(dev, req_cdclk);
9122}
9123
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009124static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9125 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009126{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009127 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009128 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009129
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009130 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009131
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009132 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009133}
9134
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309135static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9136 enum port port,
9137 struct intel_crtc_state *pipe_config)
9138{
9139 switch (port) {
9140 case PORT_A:
9141 pipe_config->ddi_pll_sel = SKL_DPLL0;
9142 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9143 break;
9144 case PORT_B:
9145 pipe_config->ddi_pll_sel = SKL_DPLL1;
9146 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9147 break;
9148 case PORT_C:
9149 pipe_config->ddi_pll_sel = SKL_DPLL2;
9150 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9151 break;
9152 default:
9153 DRM_ERROR("Incorrect port type\n");
9154 }
9155}
9156
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009157static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9158 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009159 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009160{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009161 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009162
9163 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9164 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9165
9166 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009167 case SKL_DPLL0:
9168 /*
9169 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9170 * of the shared DPLL framework and thus needs to be read out
9171 * separately
9172 */
9173 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9174 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9175 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009176 case SKL_DPLL1:
9177 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9178 break;
9179 case SKL_DPLL2:
9180 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9181 break;
9182 case SKL_DPLL3:
9183 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9184 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009185 }
9186}
9187
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009188static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9189 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009190 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009191{
9192 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9193
9194 switch (pipe_config->ddi_pll_sel) {
9195 case PORT_CLK_SEL_WRPLL1:
9196 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9197 break;
9198 case PORT_CLK_SEL_WRPLL2:
9199 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9200 break;
9201 }
9202}
9203
Daniel Vetter26804af2014-06-25 22:01:55 +03009204static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009205 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009206{
9207 struct drm_device *dev = crtc->base.dev;
9208 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009209 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009210 enum port port;
9211 uint32_t tmp;
9212
9213 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9214
9215 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9216
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009217 if (IS_SKYLAKE(dev))
9218 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309219 else if (IS_BROXTON(dev))
9220 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009221 else
9222 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009223
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009224 if (pipe_config->shared_dpll >= 0) {
9225 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9226
9227 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9228 &pipe_config->dpll_hw_state));
9229 }
9230
Daniel Vetter26804af2014-06-25 22:01:55 +03009231 /*
9232 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9233 * DDI E. So just check whether this pipe is wired to DDI E and whether
9234 * the PCH transcoder is on.
9235 */
Damien Lespiauca370452013-12-03 13:56:24 +00009236 if (INTEL_INFO(dev)->gen < 9 &&
9237 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009238 pipe_config->has_pch_encoder = true;
9239
9240 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9241 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9242 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9243
9244 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9245 }
9246}
9247
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009248static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009249 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009250{
9251 struct drm_device *dev = crtc->base.dev;
9252 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009253 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009254 uint32_t tmp;
9255
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009256 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009257 POWER_DOMAIN_PIPE(crtc->pipe)))
9258 return false;
9259
Daniel Vettere143a212013-07-04 12:01:15 +02009260 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009261 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9262
Daniel Vettereccb1402013-05-22 00:50:22 +02009263 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9264 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9265 enum pipe trans_edp_pipe;
9266 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9267 default:
9268 WARN(1, "unknown pipe linked to edp transcoder\n");
9269 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9270 case TRANS_DDI_EDP_INPUT_A_ON:
9271 trans_edp_pipe = PIPE_A;
9272 break;
9273 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9274 trans_edp_pipe = PIPE_B;
9275 break;
9276 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9277 trans_edp_pipe = PIPE_C;
9278 break;
9279 }
9280
9281 if (trans_edp_pipe == crtc->pipe)
9282 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9283 }
9284
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009285 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009286 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009287 return false;
9288
Daniel Vettereccb1402013-05-22 00:50:22 +02009289 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009290 if (!(tmp & PIPECONF_ENABLE))
9291 return false;
9292
Daniel Vetter26804af2014-06-25 22:01:55 +03009293 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009294
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009295 intel_get_pipe_timings(crtc, pipe_config);
9296
Chandra Kondurua1b22782015-04-07 15:28:45 -07009297 if (INTEL_INFO(dev)->gen >= 9) {
9298 skl_init_scalers(dev, crtc, pipe_config);
9299 }
9300
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009301 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009302 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009303 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009304 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009305 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009306 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009307 else
9308 MISSING_CASE(INTEL_INFO(dev)->gen);
9309
Chandra Kondurua1b22782015-04-07 15:28:45 -07009310 } else {
9311 pipe_config->scaler_state.scaler_id = -1;
9312 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009313 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009314
Jesse Barnese59150d2014-01-07 13:30:45 -08009315 if (IS_HASWELL(dev))
9316 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9317 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009318
Clint Taylorebb69c92014-09-30 10:30:22 -07009319 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9320 pipe_config->pixel_multiplier =
9321 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9322 } else {
9323 pipe_config->pixel_multiplier = 1;
9324 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009325
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009326 return true;
9327}
9328
Chris Wilson560b85b2010-08-07 11:01:38 +01009329static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9330{
9331 struct drm_device *dev = crtc->dev;
9332 struct drm_i915_private *dev_priv = dev->dev_private;
9333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009334 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009335
Ville Syrjälädc41c152014-08-13 11:57:05 +03009336 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009337 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9338 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009339 unsigned int stride = roundup_pow_of_two(width) * 4;
9340
9341 switch (stride) {
9342 default:
9343 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9344 width, stride);
9345 stride = 256;
9346 /* fallthrough */
9347 case 256:
9348 case 512:
9349 case 1024:
9350 case 2048:
9351 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009352 }
9353
Ville Syrjälädc41c152014-08-13 11:57:05 +03009354 cntl |= CURSOR_ENABLE |
9355 CURSOR_GAMMA_ENABLE |
9356 CURSOR_FORMAT_ARGB |
9357 CURSOR_STRIDE(stride);
9358
9359 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009360 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009361
Ville Syrjälädc41c152014-08-13 11:57:05 +03009362 if (intel_crtc->cursor_cntl != 0 &&
9363 (intel_crtc->cursor_base != base ||
9364 intel_crtc->cursor_size != size ||
9365 intel_crtc->cursor_cntl != cntl)) {
9366 /* On these chipsets we can only modify the base/size/stride
9367 * whilst the cursor is disabled.
9368 */
9369 I915_WRITE(_CURACNTR, 0);
9370 POSTING_READ(_CURACNTR);
9371 intel_crtc->cursor_cntl = 0;
9372 }
9373
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009374 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009375 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009376 intel_crtc->cursor_base = base;
9377 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009378
9379 if (intel_crtc->cursor_size != size) {
9380 I915_WRITE(CURSIZE, size);
9381 intel_crtc->cursor_size = size;
9382 }
9383
Chris Wilson4b0e3332014-05-30 16:35:26 +03009384 if (intel_crtc->cursor_cntl != cntl) {
9385 I915_WRITE(_CURACNTR, cntl);
9386 POSTING_READ(_CURACNTR);
9387 intel_crtc->cursor_cntl = cntl;
9388 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009389}
9390
9391static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9392{
9393 struct drm_device *dev = crtc->dev;
9394 struct drm_i915_private *dev_priv = dev->dev_private;
9395 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9396 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009397 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009398
Chris Wilson4b0e3332014-05-30 16:35:26 +03009399 cntl = 0;
9400 if (base) {
9401 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009402 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309403 case 64:
9404 cntl |= CURSOR_MODE_64_ARGB_AX;
9405 break;
9406 case 128:
9407 cntl |= CURSOR_MODE_128_ARGB_AX;
9408 break;
9409 case 256:
9410 cntl |= CURSOR_MODE_256_ARGB_AX;
9411 break;
9412 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009413 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309414 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009415 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009416 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009417
9418 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9419 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009420 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009421
Matt Roper8e7d6882015-01-21 16:35:41 -08009422 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009423 cntl |= CURSOR_ROTATE_180;
9424
Chris Wilson4b0e3332014-05-30 16:35:26 +03009425 if (intel_crtc->cursor_cntl != cntl) {
9426 I915_WRITE(CURCNTR(pipe), cntl);
9427 POSTING_READ(CURCNTR(pipe));
9428 intel_crtc->cursor_cntl = cntl;
9429 }
9430
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009431 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009432 I915_WRITE(CURBASE(pipe), base);
9433 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009434
9435 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009436}
9437
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009438/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009439static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9440 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009441{
9442 struct drm_device *dev = crtc->dev;
9443 struct drm_i915_private *dev_priv = dev->dev_private;
9444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9445 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009446 int x = crtc->cursor_x;
9447 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009448 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009449
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009450 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009451 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009452
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009453 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009454 base = 0;
9455
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009456 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009457 base = 0;
9458
9459 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009460 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009461 base = 0;
9462
9463 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9464 x = -x;
9465 }
9466 pos |= x << CURSOR_X_SHIFT;
9467
9468 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009469 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009470 base = 0;
9471
9472 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9473 y = -y;
9474 }
9475 pos |= y << CURSOR_Y_SHIFT;
9476
Chris Wilson4b0e3332014-05-30 16:35:26 +03009477 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009478 return;
9479
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009480 I915_WRITE(CURPOS(pipe), pos);
9481
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009482 /* ILK+ do this automagically */
9483 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009484 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009485 base += (intel_crtc->base.cursor->state->crtc_h *
9486 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009487 }
9488
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009489 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009490 i845_update_cursor(crtc, base);
9491 else
9492 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009493}
9494
Ville Syrjälädc41c152014-08-13 11:57:05 +03009495static bool cursor_size_ok(struct drm_device *dev,
9496 uint32_t width, uint32_t height)
9497{
9498 if (width == 0 || height == 0)
9499 return false;
9500
9501 /*
9502 * 845g/865g are special in that they are only limited by
9503 * the width of their cursors, the height is arbitrary up to
9504 * the precision of the register. Everything else requires
9505 * square cursors, limited to a few power-of-two sizes.
9506 */
9507 if (IS_845G(dev) || IS_I865G(dev)) {
9508 if ((width & 63) != 0)
9509 return false;
9510
9511 if (width > (IS_845G(dev) ? 64 : 512))
9512 return false;
9513
9514 if (height > 1023)
9515 return false;
9516 } else {
9517 switch (width | height) {
9518 case 256:
9519 case 128:
9520 if (IS_GEN2(dev))
9521 return false;
9522 case 64:
9523 break;
9524 default:
9525 return false;
9526 }
9527 }
9528
9529 return true;
9530}
9531
Jesse Barnes79e53942008-11-07 14:24:08 -08009532static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009533 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009534{
James Simmons72034252010-08-03 01:33:19 +01009535 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009536 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009537
James Simmons72034252010-08-03 01:33:19 +01009538 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009539 intel_crtc->lut_r[i] = red[i] >> 8;
9540 intel_crtc->lut_g[i] = green[i] >> 8;
9541 intel_crtc->lut_b[i] = blue[i] >> 8;
9542 }
9543
9544 intel_crtc_load_lut(crtc);
9545}
9546
Jesse Barnes79e53942008-11-07 14:24:08 -08009547/* VESA 640x480x72Hz mode to set on the pipe */
9548static struct drm_display_mode load_detect_mode = {
9549 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9550 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9551};
9552
Daniel Vettera8bb6812014-02-10 18:00:39 +01009553struct drm_framebuffer *
9554__intel_framebuffer_create(struct drm_device *dev,
9555 struct drm_mode_fb_cmd2 *mode_cmd,
9556 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009557{
9558 struct intel_framebuffer *intel_fb;
9559 int ret;
9560
9561 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9562 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009563 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009564 return ERR_PTR(-ENOMEM);
9565 }
9566
9567 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009568 if (ret)
9569 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009570
9571 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009572err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009573 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009574 kfree(intel_fb);
9575
9576 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009577}
9578
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009579static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009580intel_framebuffer_create(struct drm_device *dev,
9581 struct drm_mode_fb_cmd2 *mode_cmd,
9582 struct drm_i915_gem_object *obj)
9583{
9584 struct drm_framebuffer *fb;
9585 int ret;
9586
9587 ret = i915_mutex_lock_interruptible(dev);
9588 if (ret)
9589 return ERR_PTR(ret);
9590 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9591 mutex_unlock(&dev->struct_mutex);
9592
9593 return fb;
9594}
9595
Chris Wilsond2dff872011-04-19 08:36:26 +01009596static u32
9597intel_framebuffer_pitch_for_width(int width, int bpp)
9598{
9599 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9600 return ALIGN(pitch, 64);
9601}
9602
9603static u32
9604intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9605{
9606 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009607 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009608}
9609
9610static struct drm_framebuffer *
9611intel_framebuffer_create_for_mode(struct drm_device *dev,
9612 struct drm_display_mode *mode,
9613 int depth, int bpp)
9614{
9615 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009616 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009617
9618 obj = i915_gem_alloc_object(dev,
9619 intel_framebuffer_size_for_mode(mode, bpp));
9620 if (obj == NULL)
9621 return ERR_PTR(-ENOMEM);
9622
9623 mode_cmd.width = mode->hdisplay;
9624 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009625 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9626 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009627 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009628
9629 return intel_framebuffer_create(dev, &mode_cmd, obj);
9630}
9631
9632static struct drm_framebuffer *
9633mode_fits_in_fbdev(struct drm_device *dev,
9634 struct drm_display_mode *mode)
9635{
Daniel Vetter4520f532013-10-09 09:18:51 +02009636#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009637 struct drm_i915_private *dev_priv = dev->dev_private;
9638 struct drm_i915_gem_object *obj;
9639 struct drm_framebuffer *fb;
9640
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009641 if (!dev_priv->fbdev)
9642 return NULL;
9643
9644 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009645 return NULL;
9646
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009647 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009648 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009649
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009650 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009651 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9652 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009653 return NULL;
9654
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009655 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009656 return NULL;
9657
9658 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009659#else
9660 return NULL;
9661#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009662}
9663
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009664bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009665 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009666 struct intel_load_detect_pipe *old,
9667 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009668{
9669 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009670 struct intel_encoder *intel_encoder =
9671 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009672 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009673 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009674 struct drm_crtc *crtc = NULL;
9675 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009676 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009677 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009678 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009679 struct drm_connector_state *connector_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009680 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009681
Chris Wilsond2dff872011-04-19 08:36:26 +01009682 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009683 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009684 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009685
Rob Clark51fd3712013-11-19 12:10:12 -05009686retry:
9687 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9688 if (ret)
9689 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009690
Jesse Barnes79e53942008-11-07 14:24:08 -08009691 /*
9692 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009693 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009694 * - if the connector already has an assigned crtc, use it (but make
9695 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009696 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009697 * - try to find the first unused crtc that can drive this connector,
9698 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009699 */
9700
9701 /* See if we already have a CRTC for this connector */
9702 if (encoder->crtc) {
9703 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009704
Rob Clark51fd3712013-11-19 12:10:12 -05009705 ret = drm_modeset_lock(&crtc->mutex, ctx);
9706 if (ret)
9707 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009708 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9709 if (ret)
9710 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +01009711
Daniel Vetter24218aa2012-08-12 19:27:11 +02009712 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009713 old->load_detect_temp = false;
9714
9715 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009716 if (connector->dpms != DRM_MODE_DPMS_ON)
9717 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +01009718
Chris Wilson71731882011-04-19 23:10:58 +01009719 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -08009720 }
9721
9722 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01009723 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009724 i++;
9725 if (!(encoder->possible_crtcs & (1 << i)))
9726 continue;
Matt Roper83d65732015-02-25 13:12:16 -08009727 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +03009728 continue;
9729 /* This can occur when applying the pipe A quirk on resume. */
9730 if (to_intel_crtc(possible_crtc)->new_enabled)
9731 continue;
9732
9733 crtc = possible_crtc;
9734 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08009735 }
9736
9737 /*
9738 * If we didn't find an unused CRTC, don't use any.
9739 */
9740 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +01009741 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -05009742 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -08009743 }
9744
Rob Clark51fd3712013-11-19 12:10:12 -05009745 ret = drm_modeset_lock(&crtc->mutex, ctx);
9746 if (ret)
9747 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009748 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
9749 if (ret)
9750 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +02009751 intel_encoder->new_crtc = to_intel_crtc(crtc);
9752 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08009753
9754 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009755 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +02009756 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +01009757 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +01009758 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -08009759
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009760 state = drm_atomic_state_alloc(dev);
9761 if (!state)
9762 return false;
9763
9764 state->acquire_ctx = ctx;
9765
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009766 connector_state = drm_atomic_get_connector_state(state, connector);
9767 if (IS_ERR(connector_state)) {
9768 ret = PTR_ERR(connector_state);
9769 goto fail;
9770 }
9771
9772 connector_state->crtc = crtc;
9773 connector_state->best_encoder = &intel_encoder->base;
9774
Chris Wilson64927112011-04-20 07:25:26 +01009775 if (!mode)
9776 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -08009777
Chris Wilsond2dff872011-04-19 08:36:26 +01009778 /* We need a framebuffer large enough to accommodate all accesses
9779 * that the plane may generate whilst we perform load detection.
9780 * We can not rely on the fbcon either being present (we get called
9781 * during its initialisation to detect all boot displays, or it may
9782 * not even exist) or that it is large enough to satisfy the
9783 * requested mode.
9784 */
Daniel Vetter94352cf2012-07-05 22:51:56 +02009785 fb = mode_fits_in_fbdev(dev, mode);
9786 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009787 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009788 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
9789 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +01009790 } else
9791 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +02009792 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +01009793 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009794 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009795 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009796
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009797 if (intel_set_mode(crtc, mode, 0, 0, fb, state)) {
Chris Wilson64927112011-04-20 07:25:26 +01009798 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +01009799 if (old->release_fb)
9800 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009801 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -08009802 }
Daniel Vetter9128b042015-03-03 17:31:21 +01009803 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +01009804
Jesse Barnes79e53942008-11-07 14:24:08 -08009805 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -07009806 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +01009807 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009808
9809 fail:
Matt Roper83d65732015-02-25 13:12:16 -08009810 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -05009811fail_unlock:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009812 if (state) {
9813 drm_atomic_state_free(state);
9814 state = NULL;
9815 }
9816
Rob Clark51fd3712013-11-19 12:10:12 -05009817 if (ret == -EDEADLK) {
9818 drm_modeset_backoff(ctx);
9819 goto retry;
9820 }
9821
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009822 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -08009823}
9824
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009825void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +02009826 struct intel_load_detect_pipe *old,
9827 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009828{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009829 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009830 struct intel_encoder *intel_encoder =
9831 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +01009832 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +01009833 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009834 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009835 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009836 struct drm_connector_state *connector_state;
Jesse Barnes79e53942008-11-07 14:24:08 -08009837
Chris Wilsond2dff872011-04-19 08:36:26 +01009838 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009839 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009840 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009841
Chris Wilson8261b192011-04-19 23:18:09 +01009842 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009843 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009844 if (!state)
9845 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009846
9847 state->acquire_ctx = ctx;
9848
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009849 connector_state = drm_atomic_get_connector_state(state, connector);
9850 if (IS_ERR(connector_state))
9851 goto fail;
9852
Daniel Vetterfc303102012-07-09 10:40:58 +02009853 to_intel_connector(connector)->new_encoder = NULL;
9854 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +02009855 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009856
9857 connector_state->best_encoder = NULL;
9858 connector_state->crtc = NULL;
9859
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009860 intel_set_mode(crtc, NULL, 0, 0, NULL, state);
9861
9862 drm_atomic_state_free(state);
Chris Wilsond2dff872011-04-19 08:36:26 +01009863
Daniel Vetter36206362012-12-10 20:42:17 +01009864 if (old->release_fb) {
9865 drm_framebuffer_unregister_private(old->release_fb);
9866 drm_framebuffer_unreference(old->release_fb);
9867 }
Chris Wilsond2dff872011-04-19 08:36:26 +01009868
Chris Wilson0622a532011-04-21 09:32:11 +01009869 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009870 }
9871
Eric Anholtc751ce42010-03-25 11:48:48 -07009872 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +02009873 if (old->dpms_mode != DRM_MODE_DPMS_ON)
9874 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009875
9876 return;
9877fail:
9878 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
9879 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -08009880}
9881
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009882static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009883 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009884{
9885 struct drm_i915_private *dev_priv = dev->dev_private;
9886 u32 dpll = pipe_config->dpll_hw_state.dpll;
9887
9888 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +02009889 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009890 else if (HAS_PCH_SPLIT(dev))
9891 return 120000;
9892 else if (!IS_GEN2(dev))
9893 return 96000;
9894 else
9895 return 48000;
9896}
9897
Jesse Barnes79e53942008-11-07 14:24:08 -08009898/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009899static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009900 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08009901{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009902 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08009903 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009904 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +03009905 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -08009906 u32 fp;
9907 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009908 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -08009909
9910 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +03009911 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009912 else
Ville Syrjälä293623f2013-09-13 16:18:46 +03009913 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009914
9915 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009916 if (IS_PINEVIEW(dev)) {
9917 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
9918 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +08009919 } else {
9920 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
9921 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
9922 }
9923
Chris Wilsona6c45cf2010-09-17 00:32:17 +01009924 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -05009925 if (IS_PINEVIEW(dev))
9926 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
9927 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +08009928 else
9929 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -08009930 DPLL_FPA01_P1_POST_DIV_SHIFT);
9931
9932 switch (dpll & DPLL_MODE_MASK) {
9933 case DPLLB_MODE_DAC_SERIAL:
9934 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
9935 5 : 10;
9936 break;
9937 case DPLLB_MODE_LVDS:
9938 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
9939 7 : 14;
9940 break;
9941 default:
Zhao Yakui28c97732009-10-09 11:39:41 +08009942 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -08009943 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009944 return;
Jesse Barnes79e53942008-11-07 14:24:08 -08009945 }
9946
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009947 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009948 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +02009949 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009950 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009951 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +02009952 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009953 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -08009954
9955 if (is_lvds) {
9956 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
9957 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +02009958
9959 if (lvds & LVDS_CLKB_POWER_UP)
9960 clock.p2 = 7;
9961 else
9962 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -08009963 } else {
9964 if (dpll & PLL_P1_DIVIDE_BY_TWO)
9965 clock.p1 = 2;
9966 else {
9967 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
9968 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
9969 }
9970 if (dpll & PLL_P2_DIVIDE_BY_4)
9971 clock.p2 = 4;
9972 else
9973 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -08009974 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +03009975
9976 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -08009977 }
9978
Ville Syrjälä18442d02013-09-13 16:00:08 +03009979 /*
9980 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +01009981 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +03009982 * encoder's get_config() function.
9983 */
9984 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009985}
9986
Ville Syrjälä6878da02013-09-13 15:59:11 +03009987int intel_dotclock_calculate(int link_freq,
9988 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009989{
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009990 /*
9991 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009992 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009993 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009994 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +03009995 *
9996 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +03009997 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -08009998 */
9999
Ville Syrjälä6878da02013-09-13 15:59:11 +030010000 if (!m_n->link_n)
10001 return 0;
10002
10003 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10004}
10005
Ville Syrjälä18442d02013-09-13 16:00:08 +030010006static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010007 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010008{
10009 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010010
10011 /* read out port_clock from the DPLL */
10012 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010013
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010014 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010015 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010016 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010017 * agree once we know their relationship in the encoder's
10018 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010019 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010020 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010021 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10022 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010023}
10024
10025/** Returns the currently programmed mode of the given pipe. */
10026struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10027 struct drm_crtc *crtc)
10028{
Jesse Barnes548f2452011-02-17 10:40:53 -080010029 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010030 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010031 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010032 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010033 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010034 int htot = I915_READ(HTOTAL(cpu_transcoder));
10035 int hsync = I915_READ(HSYNC(cpu_transcoder));
10036 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10037 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010038 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010039
10040 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10041 if (!mode)
10042 return NULL;
10043
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010044 /*
10045 * Construct a pipe_config sufficient for getting the clock info
10046 * back out of crtc_clock_get.
10047 *
10048 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10049 * to use a real value here instead.
10050 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010051 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010052 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010053 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10054 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10055 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010056 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10057
Ville Syrjälä773ae032013-09-23 17:48:20 +030010058 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010059 mode->hdisplay = (htot & 0xffff) + 1;
10060 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10061 mode->hsync_start = (hsync & 0xffff) + 1;
10062 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10063 mode->vdisplay = (vtot & 0xffff) + 1;
10064 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10065 mode->vsync_start = (vsync & 0xffff) + 1;
10066 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10067
10068 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010069
10070 return mode;
10071}
10072
Jesse Barnes652c3932009-08-17 13:31:43 -070010073static void intel_decrease_pllclock(struct drm_crtc *crtc)
10074{
10075 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010076 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010078
Sonika Jindalbaff2962014-07-22 11:16:35 +053010079 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010080 return;
10081
10082 if (!dev_priv->lvds_downclock_avail)
10083 return;
10084
10085 /*
10086 * Since this is called by a timer, we should never get here in
10087 * the manual case.
10088 */
10089 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010090 int pipe = intel_crtc->pipe;
10091 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010092 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010093
Zhao Yakui44d98a62009-10-09 11:39:40 +080010094 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010095
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010096 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010097
Chris Wilson074b5e12012-05-02 12:07:06 +010010098 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010099 dpll |= DISPLAY_RATE_SELECT_FPA1;
10100 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010101 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010102 dpll = I915_READ(dpll_reg);
10103 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010104 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010105 }
10106
10107}
10108
Chris Wilsonf047e392012-07-21 12:31:41 +010010109void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010110{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010111 struct drm_i915_private *dev_priv = dev->dev_private;
10112
Chris Wilsonf62a0072014-02-21 17:55:39 +000010113 if (dev_priv->mm.busy)
10114 return;
10115
Paulo Zanoni43694d62014-03-07 20:08:08 -030010116 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010117 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010118 if (INTEL_INFO(dev)->gen >= 6)
10119 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010120 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010121}
10122
10123void intel_mark_idle(struct drm_device *dev)
10124{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010125 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010126 struct drm_crtc *crtc;
10127
Chris Wilsonf62a0072014-02-21 17:55:39 +000010128 if (!dev_priv->mm.busy)
10129 return;
10130
10131 dev_priv->mm.busy = false;
10132
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010133 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010134 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010135 continue;
10136
10137 intel_decrease_pllclock(crtc);
10138 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010139
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010140 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010141 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010142
Paulo Zanoni43694d62014-03-07 20:08:08 -030010143 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010144}
10145
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010146static void intel_crtc_set_state(struct intel_crtc *crtc,
10147 struct intel_crtc_state *crtc_state)
10148{
10149 kfree(crtc->config);
10150 crtc->config = crtc_state;
Ander Conselvan de Oliveira16f3f652015-01-15 14:55:27 +020010151 crtc->base.state = &crtc_state->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010152}
10153
Jesse Barnes79e53942008-11-07 14:24:08 -080010154static void intel_crtc_destroy(struct drm_crtc *crtc)
10155{
10156 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010157 struct drm_device *dev = crtc->dev;
10158 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010159
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010160 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010161 work = intel_crtc->unpin_work;
10162 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010163 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010164
10165 if (work) {
10166 cancel_work_sync(&work->work);
10167 kfree(work);
10168 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010169
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020010170 intel_crtc_set_state(intel_crtc, NULL);
Jesse Barnes79e53942008-11-07 14:24:08 -080010171 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010172
Jesse Barnes79e53942008-11-07 14:24:08 -080010173 kfree(intel_crtc);
10174}
10175
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010176static void intel_unpin_work_fn(struct work_struct *__work)
10177{
10178 struct intel_unpin_work *work =
10179 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010180 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010181 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010182
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010183 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010184 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010185 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010186
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010187 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010188
10189 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010190 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010191 mutex_unlock(&dev->struct_mutex);
10192
Daniel Vetterf99d7062014-06-19 16:01:59 +020010193 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010194 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010195
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010196 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10197 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10198
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010199 kfree(work);
10200}
10201
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010202static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010203 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010204{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010205 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10206 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010207 unsigned long flags;
10208
10209 /* Ignore early vblank irqs */
10210 if (intel_crtc == NULL)
10211 return;
10212
Daniel Vetterf3260382014-09-15 14:55:23 +020010213 /*
10214 * This is called both by irq handlers and the reset code (to complete
10215 * lost pageflips) so needs the full irqsave spinlocks.
10216 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010217 spin_lock_irqsave(&dev->event_lock, flags);
10218 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010219
10220 /* Ensure we don't miss a work->pending update ... */
10221 smp_rmb();
10222
10223 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010224 spin_unlock_irqrestore(&dev->event_lock, flags);
10225 return;
10226 }
10227
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010228 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010229
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010230 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010231}
10232
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010233void intel_finish_page_flip(struct drm_device *dev, int pipe)
10234{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010235 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010236 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10237
Mario Kleiner49b14a52010-12-09 07:00:07 +010010238 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010239}
10240
10241void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10242{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010243 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010244 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10245
Mario Kleiner49b14a52010-12-09 07:00:07 +010010246 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010247}
10248
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010249/* Is 'a' after or equal to 'b'? */
10250static bool g4x_flip_count_after_eq(u32 a, u32 b)
10251{
10252 return !((a - b) & 0x80000000);
10253}
10254
10255static bool page_flip_finished(struct intel_crtc *crtc)
10256{
10257 struct drm_device *dev = crtc->base.dev;
10258 struct drm_i915_private *dev_priv = dev->dev_private;
10259
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010260 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10261 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10262 return true;
10263
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010264 /*
10265 * The relevant registers doen't exist on pre-ctg.
10266 * As the flip done interrupt doesn't trigger for mmio
10267 * flips on gmch platforms, a flip count check isn't
10268 * really needed there. But since ctg has the registers,
10269 * include it in the check anyway.
10270 */
10271 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10272 return true;
10273
10274 /*
10275 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10276 * used the same base address. In that case the mmio flip might
10277 * have completed, but the CS hasn't even executed the flip yet.
10278 *
10279 * A flip count check isn't enough as the CS might have updated
10280 * the base address just after start of vblank, but before we
10281 * managed to process the interrupt. This means we'd complete the
10282 * CS flip too soon.
10283 *
10284 * Combining both checks should get us a good enough result. It may
10285 * still happen that the CS flip has been executed, but has not
10286 * yet actually completed. But in case the base address is the same
10287 * anyway, we don't really care.
10288 */
10289 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10290 crtc->unpin_work->gtt_offset &&
10291 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10292 crtc->unpin_work->flip_count);
10293}
10294
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010295void intel_prepare_page_flip(struct drm_device *dev, int plane)
10296{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010297 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010298 struct intel_crtc *intel_crtc =
10299 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10300 unsigned long flags;
10301
Daniel Vetterf3260382014-09-15 14:55:23 +020010302
10303 /*
10304 * This is called both by irq handlers and the reset code (to complete
10305 * lost pageflips) so needs the full irqsave spinlocks.
10306 *
10307 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010308 * generate a page-flip completion irq, i.e. every modeset
10309 * is also accompanied by a spurious intel_prepare_page_flip().
10310 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010311 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010312 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010313 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010314 spin_unlock_irqrestore(&dev->event_lock, flags);
10315}
10316
Robin Schroereba905b2014-05-18 02:24:50 +020010317static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010318{
10319 /* Ensure that the work item is consistent when activating it ... */
10320 smp_wmb();
10321 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10322 /* and that it is marked active as soon as the irq could fire. */
10323 smp_wmb();
10324}
10325
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010326static int intel_gen2_queue_flip(struct drm_device *dev,
10327 struct drm_crtc *crtc,
10328 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010329 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010330 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010331 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010332{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010333 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010334 u32 flip_mask;
10335 int ret;
10336
Daniel Vetter6d90c952012-04-26 23:28:05 +020010337 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010338 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010339 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010340
10341 /* Can't queue multiple flips, so wait for the previous
10342 * one to finish before executing the next.
10343 */
10344 if (intel_crtc->plane)
10345 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10346 else
10347 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010348 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10349 intel_ring_emit(ring, MI_NOOP);
10350 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10351 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10352 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010353 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010354 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010355
10356 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010357 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010358 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010359}
10360
10361static int intel_gen3_queue_flip(struct drm_device *dev,
10362 struct drm_crtc *crtc,
10363 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010364 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010365 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010366 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010367{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010369 u32 flip_mask;
10370 int ret;
10371
Daniel Vetter6d90c952012-04-26 23:28:05 +020010372 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010373 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010374 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010375
10376 if (intel_crtc->plane)
10377 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10378 else
10379 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010380 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10381 intel_ring_emit(ring, MI_NOOP);
10382 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10383 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10384 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010385 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010386 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010387
Chris Wilsone7d841c2012-12-03 11:36:30 +000010388 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010389 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010390 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010391}
10392
10393static int intel_gen4_queue_flip(struct drm_device *dev,
10394 struct drm_crtc *crtc,
10395 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010396 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010397 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010398 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010399{
10400 struct drm_i915_private *dev_priv = dev->dev_private;
10401 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10402 uint32_t pf, pipesrc;
10403 int ret;
10404
Daniel Vetter6d90c952012-04-26 23:28:05 +020010405 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010406 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010407 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010408
10409 /* i965+ uses the linear or tiled offsets from the
10410 * Display Registers (which do not change across a page-flip)
10411 * so we need only reprogram the base address.
10412 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010413 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10414 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10415 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010416 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010417 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010418
10419 /* XXX Enabling the panel-fitter across page-flip is so far
10420 * untested on non-native modes, so ignore it for now.
10421 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10422 */
10423 pf = 0;
10424 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010425 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010426
10427 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010428 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010429 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010430}
10431
10432static int intel_gen6_queue_flip(struct drm_device *dev,
10433 struct drm_crtc *crtc,
10434 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010435 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010436 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010437 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010438{
10439 struct drm_i915_private *dev_priv = dev->dev_private;
10440 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10441 uint32_t pf, pipesrc;
10442 int ret;
10443
Daniel Vetter6d90c952012-04-26 23:28:05 +020010444 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010445 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010446 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010447
Daniel Vetter6d90c952012-04-26 23:28:05 +020010448 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10449 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10450 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010451 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010452
Chris Wilson99d9acd2012-04-17 20:37:00 +010010453 /* Contrary to the suggestions in the documentation,
10454 * "Enable Panel Fitter" does not seem to be required when page
10455 * flipping with a non-native mode, and worse causes a normal
10456 * modeset to fail.
10457 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10458 */
10459 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010460 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010461 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010462
10463 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010464 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010465 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010466}
10467
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010468static int intel_gen7_queue_flip(struct drm_device *dev,
10469 struct drm_crtc *crtc,
10470 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010471 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010472 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010473 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010474{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010475 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010476 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010477 int len, ret;
10478
Robin Schroereba905b2014-05-18 02:24:50 +020010479 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010480 case PLANE_A:
10481 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10482 break;
10483 case PLANE_B:
10484 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10485 break;
10486 case PLANE_C:
10487 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10488 break;
10489 default:
10490 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010491 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010492 }
10493
Chris Wilsonffe74d72013-08-26 20:58:12 +010010494 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010495 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010496 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010497 /*
10498 * On Gen 8, SRM is now taking an extra dword to accommodate
10499 * 48bits addresses, and we need a NOOP for the batch size to
10500 * stay even.
10501 */
10502 if (IS_GEN8(dev))
10503 len += 2;
10504 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010505
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010506 /*
10507 * BSpec MI_DISPLAY_FLIP for IVB:
10508 * "The full packet must be contained within the same cache line."
10509 *
10510 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10511 * cacheline, if we ever start emitting more commands before
10512 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10513 * then do the cacheline alignment, and finally emit the
10514 * MI_DISPLAY_FLIP.
10515 */
10516 ret = intel_ring_cacheline_align(ring);
10517 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010518 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010519
Chris Wilsonffe74d72013-08-26 20:58:12 +010010520 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010521 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010522 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010523
Chris Wilsonffe74d72013-08-26 20:58:12 +010010524 /* Unmask the flip-done completion message. Note that the bspec says that
10525 * we should do this for both the BCS and RCS, and that we must not unmask
10526 * more than one flip event at any time (or ensure that one flip message
10527 * can be sent by waiting for flip-done prior to queueing new flips).
10528 * Experimentation says that BCS works despite DERRMR masking all
10529 * flip-done completion events and that unmasking all planes at once
10530 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10531 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10532 */
10533 if (ring->id == RCS) {
10534 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10535 intel_ring_emit(ring, DERRMR);
10536 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10537 DERRMR_PIPEB_PRI_FLIP_DONE |
10538 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010539 if (IS_GEN8(dev))
10540 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10541 MI_SRM_LRM_GLOBAL_GTT);
10542 else
10543 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10544 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010545 intel_ring_emit(ring, DERRMR);
10546 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010547 if (IS_GEN8(dev)) {
10548 intel_ring_emit(ring, 0);
10549 intel_ring_emit(ring, MI_NOOP);
10550 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010551 }
10552
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010553 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010554 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010555 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010556 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010557
10558 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010559 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010560 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010561}
10562
Sourab Gupta84c33a62014-06-02 16:47:17 +053010563static bool use_mmio_flip(struct intel_engine_cs *ring,
10564 struct drm_i915_gem_object *obj)
10565{
10566 /*
10567 * This is not being used for older platforms, because
10568 * non-availability of flip done interrupt forces us to use
10569 * CS flips. Older platforms derive flip done using some clever
10570 * tricks involving the flip_pending status bits and vblank irqs.
10571 * So using MMIO flips there would disrupt this mechanism.
10572 */
10573
Chris Wilson8e09bf82014-07-08 10:40:30 +010010574 if (ring == NULL)
10575 return true;
10576
Sourab Gupta84c33a62014-06-02 16:47:17 +053010577 if (INTEL_INFO(ring->dev)->gen < 5)
10578 return false;
10579
10580 if (i915.use_mmio_flip < 0)
10581 return false;
10582 else if (i915.use_mmio_flip > 0)
10583 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010584 else if (i915.enable_execlists)
10585 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010586 else
John Harrison41c52412014-11-24 18:49:43 +000010587 return ring != i915_gem_request_get_ring(obj->last_read_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010588}
10589
Damien Lespiauff944562014-11-20 14:58:16 +000010590static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10591{
10592 struct drm_device *dev = intel_crtc->base.dev;
10593 struct drm_i915_private *dev_priv = dev->dev_private;
10594 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010595 const enum pipe pipe = intel_crtc->pipe;
10596 u32 ctl, stride;
10597
10598 ctl = I915_READ(PLANE_CTL(pipe, 0));
10599 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010600 switch (fb->modifier[0]) {
10601 case DRM_FORMAT_MOD_NONE:
10602 break;
10603 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010604 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010605 break;
10606 case I915_FORMAT_MOD_Y_TILED:
10607 ctl |= PLANE_CTL_TILED_Y;
10608 break;
10609 case I915_FORMAT_MOD_Yf_TILED:
10610 ctl |= PLANE_CTL_TILED_YF;
10611 break;
10612 default:
10613 MISSING_CASE(fb->modifier[0]);
10614 }
Damien Lespiauff944562014-11-20 14:58:16 +000010615
10616 /*
10617 * The stride is either expressed as a multiple of 64 bytes chunks for
10618 * linear buffers or in number of tiles for tiled buffers.
10619 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010620 stride = fb->pitches[0] /
10621 intel_fb_stride_alignment(dev, fb->modifier[0],
10622 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010623
10624 /*
10625 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10626 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10627 */
10628 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10629 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10630
10631 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10632 POSTING_READ(PLANE_SURF(pipe, 0));
10633}
10634
10635static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010636{
10637 struct drm_device *dev = intel_crtc->base.dev;
10638 struct drm_i915_private *dev_priv = dev->dev_private;
10639 struct intel_framebuffer *intel_fb =
10640 to_intel_framebuffer(intel_crtc->base.primary->fb);
10641 struct drm_i915_gem_object *obj = intel_fb->obj;
10642 u32 dspcntr;
10643 u32 reg;
10644
Sourab Gupta84c33a62014-06-02 16:47:17 +053010645 reg = DSPCNTR(intel_crtc->plane);
10646 dspcntr = I915_READ(reg);
10647
Damien Lespiauc5d97472014-10-25 00:11:11 +010010648 if (obj->tiling_mode != I915_TILING_NONE)
10649 dspcntr |= DISPPLANE_TILED;
10650 else
10651 dspcntr &= ~DISPPLANE_TILED;
10652
Sourab Gupta84c33a62014-06-02 16:47:17 +053010653 I915_WRITE(reg, dspcntr);
10654
10655 I915_WRITE(DSPSURF(intel_crtc->plane),
10656 intel_crtc->unpin_work->gtt_offset);
10657 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010658
Damien Lespiauff944562014-11-20 14:58:16 +000010659}
10660
10661/*
10662 * XXX: This is the temporary way to update the plane registers until we get
10663 * around to using the usual plane update functions for MMIO flips
10664 */
10665static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10666{
10667 struct drm_device *dev = intel_crtc->base.dev;
10668 bool atomic_update;
10669 u32 start_vbl_count;
10670
10671 intel_mark_page_flip_active(intel_crtc);
10672
10673 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10674
10675 if (INTEL_INFO(dev)->gen >= 9)
10676 skl_do_mmio_flip(intel_crtc);
10677 else
10678 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10679 ilk_do_mmio_flip(intel_crtc);
10680
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010681 if (atomic_update)
10682 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010683}
10684
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010685static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010686{
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010687 struct intel_crtc *crtc =
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010688 container_of(work, struct intel_crtc, mmio_flip.work);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010689 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010690
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010691 mmio_flip = &crtc->mmio_flip;
10692 if (mmio_flip->req)
John Harrison9c654812014-11-24 18:49:35 +000010693 WARN_ON(__i915_wait_request(mmio_flip->req,
10694 crtc->reset_counter,
10695 false, NULL, NULL) != 0);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010696
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010697 intel_do_mmio_flip(crtc);
10698 if (mmio_flip->req) {
10699 mutex_lock(&crtc->base.dev->struct_mutex);
John Harrison146d84f2014-12-05 13:49:33 +000010700 i915_gem_request_assign(&mmio_flip->req, NULL);
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010701 mutex_unlock(&crtc->base.dev->struct_mutex);
10702 }
Sourab Gupta84c33a62014-06-02 16:47:17 +053010703}
10704
10705static int intel_queue_mmio_flip(struct drm_device *dev,
10706 struct drm_crtc *crtc,
10707 struct drm_framebuffer *fb,
10708 struct drm_i915_gem_object *obj,
10709 struct intel_engine_cs *ring,
10710 uint32_t flags)
10711{
Sourab Gupta84c33a62014-06-02 16:47:17 +053010712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010713
John Harrisoncc8c4cc2014-11-24 18:49:34 +000010714 i915_gem_request_assign(&intel_crtc->mmio_flip.req,
10715 obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010716
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020010717 schedule_work(&intel_crtc->mmio_flip.work);
10718
Sourab Gupta84c33a62014-06-02 16:47:17 +053010719 return 0;
10720}
10721
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010722static int intel_default_queue_flip(struct drm_device *dev,
10723 struct drm_crtc *crtc,
10724 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010725 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010726 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010727 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010728{
10729 return -ENODEV;
10730}
10731
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010732static bool __intel_pageflip_stall_check(struct drm_device *dev,
10733 struct drm_crtc *crtc)
10734{
10735 struct drm_i915_private *dev_priv = dev->dev_private;
10736 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10737 struct intel_unpin_work *work = intel_crtc->unpin_work;
10738 u32 addr;
10739
10740 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
10741 return true;
10742
10743 if (!work->enable_stall_check)
10744 return false;
10745
10746 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010010747 if (work->flip_queued_req &&
10748 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010749 return false;
10750
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010751 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010752 }
10753
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010754 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010755 return false;
10756
10757 /* Potential stall - if we see that the flip has happened,
10758 * assume a missed interrupt. */
10759 if (INTEL_INFO(dev)->gen >= 4)
10760 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
10761 else
10762 addr = I915_READ(DSPADDR(intel_crtc->plane));
10763
10764 /* There is a potential issue here with a false positive after a flip
10765 * to the same address. We could address this by checking for a
10766 * non-incrementing frame counter.
10767 */
10768 return addr == work->gtt_offset;
10769}
10770
10771void intel_check_page_flip(struct drm_device *dev, int pipe)
10772{
10773 struct drm_i915_private *dev_priv = dev->dev_private;
10774 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10775 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010776 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020010777
Dave Gordon6c51d462015-03-06 15:34:26 +000010778 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010779
10780 if (crtc == NULL)
10781 return;
10782
Daniel Vetterf3260382014-09-15 14:55:23 +020010783 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010784 work = intel_crtc->unpin_work;
10785 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010786 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010010787 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010788 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010010789 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010790 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010010791 if (work != NULL &&
10792 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
10793 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020010794 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010795}
10796
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010797static int intel_crtc_page_flip(struct drm_crtc *crtc,
10798 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010799 struct drm_pending_vblank_event *event,
10800 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010801{
10802 struct drm_device *dev = crtc->dev;
10803 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070010804 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070010805 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080010807 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020010808 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010809 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010810 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010811 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010010812 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010813
Matt Roper2ff8fde2014-07-08 07:50:07 -070010814 /*
10815 * drm_mode_page_flip_ioctl() should already catch this, but double
10816 * check to be safe. In the future we may enable pageflipping from
10817 * a disabled primary plane.
10818 */
10819 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
10820 return -EBUSY;
10821
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010822 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070010823 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010824 return -EINVAL;
10825
10826 /*
10827 * TILEOFF/LINOFF registers can't be changed via MI display flips.
10828 * Note that pitch changes could also affect these register.
10829 */
10830 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070010831 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
10832 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030010833 return -EINVAL;
10834
Chris Wilsonf900db42014-02-20 09:26:13 +000010835 if (i915_terminally_wedged(&dev_priv->gpu_error))
10836 goto out_hang;
10837
Daniel Vetterb14c5672013-09-19 12:18:32 +020010838 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010839 if (work == NULL)
10840 return -ENOMEM;
10841
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010842 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010843 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010844 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010845 INIT_WORK(&work->work, intel_unpin_work_fn);
10846
Daniel Vetter87b6b102014-05-15 15:33:46 +020010847 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010848 if (ret)
10849 goto free_work;
10850
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010851 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010852 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010853 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010854 /* Before declaring the flip queue wedged, check if
10855 * the hardware completed the operation behind our backs.
10856 */
10857 if (__intel_pageflip_stall_check(dev, crtc)) {
10858 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
10859 page_flip_completed(intel_crtc);
10860 } else {
10861 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010862 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010010863
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010864 drm_crtc_vblank_put(crtc);
10865 kfree(work);
10866 return -EBUSY;
10867 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010868 }
10869 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010870 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010871
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010872 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
10873 flush_workqueue(dev_priv->wq);
10874
Jesse Barnes75dfca82010-02-10 15:09:44 -080010875 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010876 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000010877 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010878
Matt Roperf4510a22014-04-01 15:22:40 -070010879 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010880 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080010881
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010882 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010883
Chris Wilson89ed88b2015-02-16 14:31:49 +000010884 ret = i915_mutex_lock_interruptible(dev);
10885 if (ret)
10886 goto cleanup;
10887
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010888 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020010889 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010010890
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010891 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020010892 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010893
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010894 if (IS_VALLEYVIEW(dev)) {
10895 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010896 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010010897 /* vlv: DISPLAY_FLIP fails to change tiling */
10898 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000010899 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010010900 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010901 } else if (INTEL_INFO(dev)->gen >= 7) {
John Harrison41c52412014-11-24 18:49:43 +000010902 ring = i915_gem_request_get_ring(obj->last_read_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010903 if (ring == NULL || ring->id != RCS)
10904 ring = &dev_priv->ring[BCS];
10905 } else {
10906 ring = &dev_priv->ring[RCS];
10907 }
10908
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010909 mmio_flip = use_mmio_flip(ring, obj);
10910
10911 /* When using CS flips, we want to emit semaphores between rings.
10912 * However, when using mmio flips we will create a task to do the
10913 * synchronisation, so all we want here is to pin the framebuffer
10914 * into the display plane and skip any waits.
10915 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010916 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010917 crtc->primary->state,
10918 mmio_flip ? i915_gem_request_get_ring(obj->last_read_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010919 if (ret)
10920 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010921
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000010922 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
10923 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010924
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010010925 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010926 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
10927 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010928 if (ret)
10929 goto cleanup_unpin;
10930
John Harrisonf06cc1b2014-11-24 18:49:37 +000010931 i915_gem_request_assign(&work->flip_queued_req,
10932 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010933 } else {
Sourab Gupta84c33a62014-06-02 16:47:17 +053010934 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010935 page_flip_flags);
10936 if (ret)
10937 goto cleanup_unpin;
10938
John Harrisonf06cc1b2014-11-24 18:49:37 +000010939 i915_gem_request_assign(&work->flip_queued_req,
10940 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010941 }
10942
Daniel Vetter1e3feef2015-02-13 21:03:45 +010010943 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010944 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010945
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000010946 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020010947 INTEL_FRONTBUFFER_PRIMARY(pipe));
10948
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010949 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010950 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010951 mutex_unlock(&dev->struct_mutex);
10952
Jesse Barnese5510fa2010-07-01 16:48:37 -070010953 trace_i915_flip_request(intel_crtc->plane, obj);
10954
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010955 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010010956
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010957cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010958 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010959cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010960 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010961 mutex_unlock(&dev->struct_mutex);
10962cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070010963 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080010964 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010010965
Chris Wilson89ed88b2015-02-16 14:31:49 +000010966 drm_gem_object_unreference_unlocked(&obj->base);
10967 drm_framebuffer_unreference(work->old_fb);
10968
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010969 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010970 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010971 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010010972
Daniel Vetter87b6b102014-05-15 15:33:46 +020010973 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070010974free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010010975 kfree(work);
10976
Chris Wilsonf900db42014-02-20 09:26:13 +000010977 if (ret == -EIO) {
10978out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080010979 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010980 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010981 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020010982 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010983 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010010984 }
Chris Wilsonf900db42014-02-20 09:26:13 +000010985 }
Chris Wilson96b099f2010-06-07 14:03:04 +010010986 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010987}
10988
Jani Nikula65b38e02015-04-13 11:26:56 +030010989static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010990 .mode_set_base_atomic = intel_pipe_set_base_atomic,
10991 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080010992 .atomic_begin = intel_begin_crtc_commit,
10993 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010010994};
10995
Daniel Vetter9a935852012-07-05 22:34:27 +020010996/**
10997 * intel_modeset_update_staged_output_state
10998 *
10999 * Updates the staged output configuration state, e.g. after we've read out the
11000 * current hw state.
11001 */
11002static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11003{
Ville Syrjälä76688512014-01-10 11:28:06 +020011004 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011005 struct intel_encoder *encoder;
11006 struct intel_connector *connector;
11007
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011008 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011009 connector->new_encoder =
11010 to_intel_encoder(connector->base.encoder);
11011 }
11012
Damien Lespiaub2784e12014-08-05 11:29:37 +010011013 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011014 encoder->new_crtc =
11015 to_intel_crtc(encoder->base.crtc);
11016 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011017
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011018 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011019 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011020 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011021}
11022
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011023/* Transitional helper to copy current connector/encoder state to
11024 * connector->state. This is needed so that code that is partially
11025 * converted to atomic does the right thing.
11026 */
11027static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11028{
11029 struct intel_connector *connector;
11030
11031 for_each_intel_connector(dev, connector) {
11032 if (connector->base.encoder) {
11033 connector->base.state->best_encoder =
11034 connector->base.encoder;
11035 connector->base.state->crtc =
11036 connector->base.encoder->crtc;
11037 } else {
11038 connector->base.state->best_encoder = NULL;
11039 connector->base.state->crtc = NULL;
11040 }
11041 }
11042}
11043
Daniel Vetter9a935852012-07-05 22:34:27 +020011044/**
11045 * intel_modeset_commit_output_state
11046 *
11047 * This function copies the stage display pipe configuration to the real one.
11048 */
11049static void intel_modeset_commit_output_state(struct drm_device *dev)
11050{
Ville Syrjälä76688512014-01-10 11:28:06 +020011051 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011052 struct intel_encoder *encoder;
11053 struct intel_connector *connector;
11054
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011055 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011056 connector->base.encoder = &connector->new_encoder->base;
11057 }
11058
Damien Lespiaub2784e12014-08-05 11:29:37 +010011059 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011060 encoder->base.crtc = &encoder->new_crtc->base;
11061 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011062
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011063 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011064 crtc->base.state->enable = crtc->new_enabled;
Ville Syrjälä76688512014-01-10 11:28:06 +020011065 crtc->base.enabled = crtc->new_enabled;
11066 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011067
11068 intel_modeset_update_connector_atomic_state(dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011069}
11070
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011071static void
Robin Schroereba905b2014-05-18 02:24:50 +020011072connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011073 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011074{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011075 int bpp = pipe_config->pipe_bpp;
11076
11077 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11078 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011079 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011080
11081 /* Don't use an invalid EDID bpc value */
11082 if (connector->base.display_info.bpc &&
11083 connector->base.display_info.bpc * 3 < bpp) {
11084 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11085 bpp, connector->base.display_info.bpc*3);
11086 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11087 }
11088
11089 /* Clamp bpp to 8 on screens without EDID 1.4 */
11090 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11091 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11092 bpp);
11093 pipe_config->pipe_bpp = 24;
11094 }
11095}
11096
11097static int
11098compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011099 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011100{
11101 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011102 struct drm_atomic_state *state;
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011103 struct intel_connector *connector;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011104 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011105
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011106 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011107 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011108 else if (INTEL_INFO(dev)->gen >= 5)
11109 bpp = 12*3;
11110 else
11111 bpp = 8*3;
11112
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011113
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011114 pipe_config->pipe_bpp = bpp;
11115
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011116 state = pipe_config->base.state;
11117
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011118 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011119 for (i = 0; i < state->num_connector; i++) {
11120 if (!state->connectors[i])
11121 continue;
11122
11123 connector = to_intel_connector(state->connectors[i]);
11124 if (state->connector_states[i]->crtc != &crtc->base)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011125 continue;
11126
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011127 connected_sink_compute_bpp(connector, pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011128 }
11129
11130 return bpp;
11131}
11132
Daniel Vetter644db712013-09-19 14:53:58 +020011133static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11134{
11135 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11136 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011137 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011138 mode->crtc_hdisplay, mode->crtc_hsync_start,
11139 mode->crtc_hsync_end, mode->crtc_htotal,
11140 mode->crtc_vdisplay, mode->crtc_vsync_start,
11141 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11142}
11143
Daniel Vetterc0b03412013-05-28 12:05:54 +020011144static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011145 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011146 const char *context)
11147{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011148 struct drm_device *dev = crtc->base.dev;
11149 struct drm_plane *plane;
11150 struct intel_plane *intel_plane;
11151 struct intel_plane_state *state;
11152 struct drm_framebuffer *fb;
11153
11154 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11155 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011156
11157 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11158 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11159 pipe_config->pipe_bpp, pipe_config->dither);
11160 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11161 pipe_config->has_pch_encoder,
11162 pipe_config->fdi_lanes,
11163 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11164 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11165 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011166 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11167 pipe_config->has_dp_encoder,
11168 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11169 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11170 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011171
11172 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11173 pipe_config->has_dp_encoder,
11174 pipe_config->dp_m2_n2.gmch_m,
11175 pipe_config->dp_m2_n2.gmch_n,
11176 pipe_config->dp_m2_n2.link_m,
11177 pipe_config->dp_m2_n2.link_n,
11178 pipe_config->dp_m2_n2.tu);
11179
Daniel Vetter55072d12014-11-20 16:10:28 +010011180 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11181 pipe_config->has_audio,
11182 pipe_config->has_infoframe);
11183
Daniel Vetterc0b03412013-05-28 12:05:54 +020011184 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011185 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011186 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011187 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11188 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011189 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011190 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11191 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011192 DRM_DEBUG_KMS("num_scalers: %d\n", crtc->num_scalers);
11193 DRM_DEBUG_KMS("scaler_users: 0x%x\n", pipe_config->scaler_state.scaler_users);
11194 DRM_DEBUG_KMS("scaler id: %d\n", pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011195 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11196 pipe_config->gmch_pfit.control,
11197 pipe_config->gmch_pfit.pgm_ratios,
11198 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011199 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011200 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011201 pipe_config->pch_pfit.size,
11202 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011203 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011204 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011205
11206 DRM_DEBUG_KMS("planes on this crtc\n");
11207 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11208 intel_plane = to_intel_plane(plane);
11209 if (intel_plane->pipe != crtc->pipe)
11210 continue;
11211
11212 state = to_intel_plane_state(plane->state);
11213 fb = state->base.fb;
11214 if (!fb) {
11215 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11216 "disabled, scaler_id = %d\n",
11217 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11218 plane->base.id, intel_plane->pipe,
11219 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11220 drm_plane_index(plane), state->scaler_id);
11221 continue;
11222 }
11223
11224 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11225 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11226 plane->base.id, intel_plane->pipe,
11227 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11228 drm_plane_index(plane));
11229 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11230 fb->base.id, fb->width, fb->height, fb->pixel_format);
11231 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11232 state->scaler_id,
11233 state->src.x1 >> 16, state->src.y1 >> 16,
11234 drm_rect_width(&state->src) >> 16,
11235 drm_rect_height(&state->src) >> 16,
11236 state->dst.x1, state->dst.y1,
11237 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11238 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011239}
11240
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011241static bool encoders_cloneable(const struct intel_encoder *a,
11242 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011243{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011244 /* masks could be asymmetric, so check both ways */
11245 return a == b || (a->cloneable & (1 << b->type) &&
11246 b->cloneable & (1 << a->type));
11247}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011248
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011249static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11250 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011251 struct intel_encoder *encoder)
11252{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011253 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011254 struct drm_connector_state *connector_state;
11255 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011256
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011257 for (i = 0; i < state->num_connector; i++) {
11258 if (!state->connectors[i])
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011259 continue;
11260
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011261 connector_state = state->connector_states[i];
11262 if (connector_state->crtc != &crtc->base)
11263 continue;
11264
11265 source_encoder =
11266 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011267 if (!encoders_cloneable(encoder, source_encoder))
11268 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011269 }
11270
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011271 return true;
11272}
11273
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011274static bool check_encoder_cloning(struct drm_atomic_state *state,
11275 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011276{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011277 struct intel_encoder *encoder;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011278 struct drm_connector_state *connector_state;
11279 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011280
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011281 for (i = 0; i < state->num_connector; i++) {
11282 if (!state->connectors[i])
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011283 continue;
11284
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011285 connector_state = state->connector_states[i];
11286 if (connector_state->crtc != &crtc->base)
11287 continue;
11288
11289 encoder = to_intel_encoder(connector_state->best_encoder);
11290 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011291 return false;
11292 }
11293
11294 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011295}
11296
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011297static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011298{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011299 struct drm_device *dev = state->dev;
11300 struct intel_encoder *encoder;
11301 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011302 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011303 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011304
11305 /*
11306 * Walk the connector list instead of the encoder
11307 * list to detect the problem on ddi platforms
11308 * where there's just one encoder per digital port.
11309 */
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011310 for (i = 0; i < state->num_connector; i++) {
11311 if (!state->connectors[i])
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011312 continue;
11313
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011314 connector_state = state->connector_states[i];
11315 if (!connector_state->best_encoder)
11316 continue;
11317
11318 encoder = to_intel_encoder(connector_state->best_encoder);
11319
11320 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011321
11322 switch (encoder->type) {
11323 unsigned int port_mask;
11324 case INTEL_OUTPUT_UNKNOWN:
11325 if (WARN_ON(!HAS_DDI(dev)))
11326 break;
11327 case INTEL_OUTPUT_DISPLAYPORT:
11328 case INTEL_OUTPUT_HDMI:
11329 case INTEL_OUTPUT_EDP:
11330 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11331
11332 /* the same port mustn't appear more than once */
11333 if (used_ports & port_mask)
11334 return false;
11335
11336 used_ports |= port_mask;
11337 default:
11338 break;
11339 }
11340 }
11341
11342 return true;
11343}
11344
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011345static void
11346clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11347{
11348 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011349 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011350
Chandra Konduru663a3642015-04-07 15:28:41 -070011351 /* Clear only the intel specific part of the crtc state excluding scalers */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011352 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011353 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011354 memset(crtc_state, 0, sizeof *crtc_state);
11355 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011356 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011357}
11358
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011359static struct intel_crtc_state *
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011360intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011361 struct drm_display_mode *mode,
11362 struct drm_atomic_state *state)
Daniel Vetter7758a112012-07-08 19:40:39 +020011363{
Daniel Vetter7758a112012-07-08 19:40:39 +020011364 struct intel_encoder *encoder;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011365 struct intel_connector *connector;
11366 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011367 struct intel_crtc_state *pipe_config;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011368 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011369 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011370 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011371
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011372 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011373 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11374 return ERR_PTR(-EINVAL);
11375 }
11376
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011377 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011378 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
11379 return ERR_PTR(-EINVAL);
11380 }
11381
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011382 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
11383 if (IS_ERR(pipe_config))
11384 return pipe_config;
11385
11386 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011387
Matt Roper07878242015-02-25 11:43:26 -080011388 pipe_config->base.crtc = crtc;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011389 drm_mode_copy(&pipe_config->base.adjusted_mode, mode);
11390 drm_mode_copy(&pipe_config->base.mode, mode);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011391
Daniel Vettere143a212013-07-04 12:01:15 +020011392 pipe_config->cpu_transcoder =
11393 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011394 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011395
Imre Deak2960bc92013-07-30 13:36:32 +030011396 /*
11397 * Sanitize sync polarity flags based on requested ones. If neither
11398 * positive or negative polarity is requested, treat this as meaning
11399 * negative polarity.
11400 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011401 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011402 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011403 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011404
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011405 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011406 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011407 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011408
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011409 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11410 * plane pixel format and any sink constraints into account. Returns the
11411 * source plane bpp so that dithering can be selected on mismatches
11412 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011413 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11414 pipe_config);
11415 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011416 goto fail;
11417
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011418 /*
11419 * Determine the real pipe dimensions. Note that stereo modes can
11420 * increase the actual pipe size due to the frame doubling and
11421 * insertion of additional space for blanks between the frame. This
11422 * is stored in the crtc timings. We use the requested mode to do this
11423 * computation to clearly distinguish it from the adjusted mode, which
11424 * can be changed by the connectors in the below retry loop.
11425 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011426 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011427 &pipe_config->pipe_src_w,
11428 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011429
Daniel Vettere29c22c2013-02-21 00:00:16 +010011430encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011431 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011432 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011433 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011434
Daniel Vetter135c81b2013-07-21 21:37:09 +020011435 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011436 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11437 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011438
Daniel Vetter7758a112012-07-08 19:40:39 +020011439 /* Pass our mode to the connectors and the CRTC to give them a chance to
11440 * adjust it according to limitations or connector properties, and also
11441 * a chance to reject the mode entirely.
11442 */
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011443 for (i = 0; i < state->num_connector; i++) {
11444 connector = to_intel_connector(state->connectors[i]);
11445 if (!connector)
Daniel Vetter7758a112012-07-08 19:40:39 +020011446 continue;
Daniel Vetter7ae89232013-03-27 00:44:52 +010011447
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011448 connector_state = state->connector_states[i];
11449 if (connector_state->crtc != crtc)
11450 continue;
11451
11452 encoder = to_intel_encoder(connector_state->best_encoder);
11453
Daniel Vetterefea6e82013-07-21 21:36:59 +020011454 if (!(encoder->compute_config(encoder, pipe_config))) {
11455 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011456 goto fail;
11457 }
11458 }
11459
Daniel Vetterff9a6752013-06-01 17:16:21 +020011460 /* Set default port clock if not overwritten by the encoder. Needs to be
11461 * done afterwards in case the encoder adjusts the mode. */
11462 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011463 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011464 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011465
Daniel Vettera43f6e02013-06-07 23:10:32 +020011466 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011467 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011468 DRM_DEBUG_KMS("CRTC fixup failed\n");
11469 goto fail;
11470 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011471
11472 if (ret == RETRY) {
11473 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11474 ret = -EINVAL;
11475 goto fail;
11476 }
11477
11478 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11479 retry = false;
11480 goto encoder_retry;
11481 }
11482
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011483 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011484 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011485 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011486
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011487 return pipe_config;
Daniel Vetter7758a112012-07-08 19:40:39 +020011488fail:
Daniel Vettere29c22c2013-02-21 00:00:16 +010011489 return ERR_PTR(ret);
Daniel Vetter7758a112012-07-08 19:40:39 +020011490}
11491
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011492/* Computes which crtcs are affected and sets the relevant bits in the mask. For
11493 * simplicity we use the crtc's pipe number (because it's easier to obtain). */
11494static void
11495intel_modeset_affected_pipes(struct drm_crtc *crtc, unsigned *modeset_pipes,
11496 unsigned *prepare_pipes, unsigned *disable_pipes)
11497{
11498 struct intel_crtc *intel_crtc;
11499 struct drm_device *dev = crtc->dev;
11500 struct intel_encoder *encoder;
11501 struct intel_connector *connector;
11502 struct drm_crtc *tmp_crtc;
11503
11504 *disable_pipes = *modeset_pipes = *prepare_pipes = 0;
11505
11506 /* Check which crtcs have changed outputs connected to them, these need
11507 * to be part of the prepare_pipes mask. We don't (yet) support global
11508 * modeset across multiple crtcs, so modeset_pipes will only have one
11509 * bit set at most. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011510 for_each_intel_connector(dev, connector) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011511 if (connector->base.encoder == &connector->new_encoder->base)
11512 continue;
11513
11514 if (connector->base.encoder) {
11515 tmp_crtc = connector->base.encoder->crtc;
11516
11517 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11518 }
11519
11520 if (connector->new_encoder)
11521 *prepare_pipes |=
11522 1 << connector->new_encoder->new_crtc->pipe;
11523 }
11524
Damien Lespiaub2784e12014-08-05 11:29:37 +010011525 for_each_intel_encoder(dev, encoder) {
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011526 if (encoder->base.crtc == &encoder->new_crtc->base)
11527 continue;
11528
11529 if (encoder->base.crtc) {
11530 tmp_crtc = encoder->base.crtc;
11531
11532 *prepare_pipes |= 1 << to_intel_crtc(tmp_crtc)->pipe;
11533 }
11534
11535 if (encoder->new_crtc)
11536 *prepare_pipes |= 1 << encoder->new_crtc->pipe;
11537 }
11538
Ville Syrjälä76688512014-01-10 11:28:06 +020011539 /* Check for pipes that will be enabled/disabled ... */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011540 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011541 if (intel_crtc->base.state->enable == intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011542 continue;
11543
Ville Syrjälä76688512014-01-10 11:28:06 +020011544 if (!intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011545 *disable_pipes |= 1 << intel_crtc->pipe;
Ville Syrjälä76688512014-01-10 11:28:06 +020011546 else
11547 *prepare_pipes |= 1 << intel_crtc->pipe;
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011548 }
11549
11550
11551 /* set_mode is also used to update properties on life display pipes. */
11552 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä76688512014-01-10 11:28:06 +020011553 if (intel_crtc->new_enabled)
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011554 *prepare_pipes |= 1 << intel_crtc->pipe;
11555
Daniel Vetterb6c51642013-04-12 18:48:43 +020011556 /*
11557 * For simplicity do a full modeset on any pipe where the output routing
11558 * changed. We could be more clever, but that would require us to be
11559 * more careful with calling the relevant encoder->mode_set functions.
11560 */
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011561 if (*prepare_pipes)
11562 *modeset_pipes = *prepare_pipes;
11563
11564 /* ... and mask these out. */
11565 *modeset_pipes &= ~(*disable_pipes);
11566 *prepare_pipes &= ~(*disable_pipes);
Daniel Vetterb6c51642013-04-12 18:48:43 +020011567
11568 /*
11569 * HACK: We don't (yet) fully support global modesets. intel_set_config
11570 * obies this rule, but the modeset restore mode of
11571 * intel_modeset_setup_hw_state does not.
11572 */
11573 *modeset_pipes &= 1 << intel_crtc->pipe;
11574 *prepare_pipes &= 1 << intel_crtc->pipe;
Daniel Vettere3641d32013-04-11 19:49:07 +020011575
11576 DRM_DEBUG_KMS("set mode pipe masks: modeset: %x, prepare: %x, disable: %x\n",
11577 *modeset_pipes, *prepare_pipes, *disable_pipes);
Daniel Vettere2e1ed42012-07-08 21:14:38 +020011578}
11579
Daniel Vetterea9d7582012-07-10 10:42:52 +020011580static bool intel_crtc_in_use(struct drm_crtc *crtc)
11581{
11582 struct drm_encoder *encoder;
11583 struct drm_device *dev = crtc->dev;
11584
11585 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11586 if (encoder->crtc == crtc)
11587 return true;
11588
11589 return false;
11590}
11591
11592static void
11593intel_modeset_update_state(struct drm_device *dev, unsigned prepare_pipes)
11594{
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011595 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011596 struct intel_encoder *intel_encoder;
11597 struct intel_crtc *intel_crtc;
11598 struct drm_connector *connector;
11599
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011600 intel_shared_dpll_commit(dev_priv);
11601
Damien Lespiaub2784e12014-08-05 11:29:37 +010011602 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011603 if (!intel_encoder->base.crtc)
11604 continue;
11605
11606 intel_crtc = to_intel_crtc(intel_encoder->base.crtc);
11607
11608 if (prepare_pipes & (1 << intel_crtc->pipe))
11609 intel_encoder->connectors_active = false;
11610 }
11611
11612 intel_modeset_commit_output_state(dev);
11613
Ville Syrjälä76688512014-01-10 11:28:06 +020011614 /* Double check state. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011615 for_each_intel_crtc(dev, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011616 WARN_ON(intel_crtc->base.state->enable != intel_crtc_in_use(&intel_crtc->base));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011617 }
11618
11619 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11620 if (!connector->encoder || !connector->encoder->crtc)
11621 continue;
11622
11623 intel_crtc = to_intel_crtc(connector->encoder->crtc);
11624
11625 if (prepare_pipes & (1 << intel_crtc->pipe)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011626 struct drm_property *dpms_property =
11627 dev->mode_config.dpms_property;
11628
Daniel Vetterea9d7582012-07-10 10:42:52 +020011629 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011630 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011631 dpms_property,
11632 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011633
11634 intel_encoder = to_intel_encoder(connector->encoder);
11635 intel_encoder->connectors_active = true;
11636 }
11637 }
11638
11639}
11640
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011641static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011642{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011643 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011644
11645 if (clock1 == clock2)
11646 return true;
11647
11648 if (!clock1 || !clock2)
11649 return false;
11650
11651 diff = abs(clock1 - clock2);
11652
11653 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11654 return true;
11655
11656 return false;
11657}
11658
Daniel Vetter25c5b262012-07-08 22:08:04 +020011659#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11660 list_for_each_entry((intel_crtc), \
11661 &(dev)->mode_config.crtc_list, \
11662 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011663 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011664
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011665static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011666intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011667 struct intel_crtc_state *current_config,
11668 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011669{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011670#define PIPE_CONF_CHECK_X(name) \
11671 if (current_config->name != pipe_config->name) { \
11672 DRM_ERROR("mismatch in " #name " " \
11673 "(expected 0x%08x, found 0x%08x)\n", \
11674 current_config->name, \
11675 pipe_config->name); \
11676 return false; \
11677 }
11678
Daniel Vetter08a24032013-04-19 11:25:34 +020011679#define PIPE_CONF_CHECK_I(name) \
11680 if (current_config->name != pipe_config->name) { \
11681 DRM_ERROR("mismatch in " #name " " \
11682 "(expected %i, found %i)\n", \
11683 current_config->name, \
11684 pipe_config->name); \
11685 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011686 }
11687
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011688/* This is required for BDW+ where there is only one set of registers for
11689 * switching between high and low RR.
11690 * This macro can be used whenever a comparison has to be made between one
11691 * hw state and multiple sw state variables.
11692 */
11693#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11694 if ((current_config->name != pipe_config->name) && \
11695 (current_config->alt_name != pipe_config->name)) { \
11696 DRM_ERROR("mismatch in " #name " " \
11697 "(expected %i or %i, found %i)\n", \
11698 current_config->name, \
11699 current_config->alt_name, \
11700 pipe_config->name); \
11701 return false; \
11702 }
11703
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011704#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11705 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011706 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011707 "(expected %i, found %i)\n", \
11708 current_config->name & (mask), \
11709 pipe_config->name & (mask)); \
11710 return false; \
11711 }
11712
Ville Syrjälä5e550652013-09-06 23:29:07 +030011713#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11714 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11715 DRM_ERROR("mismatch in " #name " " \
11716 "(expected %i, found %i)\n", \
11717 current_config->name, \
11718 pipe_config->name); \
11719 return false; \
11720 }
11721
Daniel Vetterbb760062013-06-06 14:55:52 +020011722#define PIPE_CONF_QUIRK(quirk) \
11723 ((current_config->quirks | pipe_config->quirks) & (quirk))
11724
Daniel Vettereccb1402013-05-22 00:50:22 +020011725 PIPE_CONF_CHECK_I(cpu_transcoder);
11726
Daniel Vetter08a24032013-04-19 11:25:34 +020011727 PIPE_CONF_CHECK_I(has_pch_encoder);
11728 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020011729 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
11730 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
11731 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
11732 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
11733 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020011734
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011735 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011736
11737 if (INTEL_INFO(dev)->gen < 8) {
11738 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
11739 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
11740 PIPE_CONF_CHECK_I(dp_m_n.link_m);
11741 PIPE_CONF_CHECK_I(dp_m_n.link_n);
11742 PIPE_CONF_CHECK_I(dp_m_n.tu);
11743
11744 if (current_config->has_drrs) {
11745 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
11746 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
11747 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
11748 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
11749 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
11750 }
11751 } else {
11752 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
11753 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
11754 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
11755 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
11756 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
11757 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011758
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011759 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
11760 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
11761 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
11762 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
11763 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
11764 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011765
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011766 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
11767 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
11768 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
11769 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
11770 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
11771 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011772
Daniel Vetterc93f54c2013-06-27 19:47:19 +020011773 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020011774 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020011775 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
11776 IS_VALLEYVIEW(dev))
11777 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080011778 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020011779
Daniel Vetter9ed109a2014-04-24 23:54:52 +020011780 PIPE_CONF_CHECK_I(has_audio);
11781
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011782 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011783 DRM_MODE_FLAG_INTERLACE);
11784
Daniel Vetterbb760062013-06-06 14:55:52 +020011785 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011786 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011787 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011788 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011789 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011790 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011791 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011792 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020011793 DRM_MODE_FLAG_NVSYNC);
11794 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011795
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011796 PIPE_CONF_CHECK_I(pipe_src_w);
11797 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011798
Daniel Vetter99535992014-04-13 12:00:33 +020011799 /*
11800 * FIXME: BIOS likes to set up a cloned config with lvds+external
11801 * screen. Since we don't yet re-compute the pipe config when moving
11802 * just the lvds port away to another pipe the sw tracking won't match.
11803 *
11804 * Proper atomic modesets with recomputed global state will fix this.
11805 * Until then just don't check gmch state for inherited modes.
11806 */
11807 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
11808 PIPE_CONF_CHECK_I(gmch_pfit.control);
11809 /* pfit ratios are autocomputed by the hw on gen4+ */
11810 if (INTEL_INFO(dev)->gen < 4)
11811 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
11812 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
11813 }
11814
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011815 PIPE_CONF_CHECK_I(pch_pfit.enabled);
11816 if (current_config->pch_pfit.enabled) {
11817 PIPE_CONF_CHECK_I(pch_pfit.pos);
11818 PIPE_CONF_CHECK_I(pch_pfit.size);
11819 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011820
Chandra Kondurua1b22782015-04-07 15:28:45 -070011821 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
11822
Jesse Barnese59150d2014-01-07 13:30:45 -080011823 /* BDW+ don't expose a synchronous way to read the state */
11824 if (IS_HASWELL(dev))
11825 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011826
Ville Syrjälä282740f2013-09-04 18:30:03 +030011827 PIPE_CONF_CHECK_I(double_wide);
11828
Daniel Vetter26804af2014-06-25 22:01:55 +030011829 PIPE_CONF_CHECK_X(ddi_pll_sel);
11830
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011831 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011832 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020011833 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020011834 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
11835 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030011836 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000011837 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
11838 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
11839 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020011840
Ville Syrjälä42571ae2013-09-06 23:29:00 +030011841 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
11842 PIPE_CONF_CHECK_I(pipe_bpp);
11843
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011844 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080011845 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030011846
Daniel Vetter66e985c2013-06-05 13:34:20 +020011847#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020011848#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011849#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011850#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030011851#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020011852#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020011853
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011854 return true;
11855}
11856
Damien Lespiau08db6652014-11-04 17:06:52 +000011857static void check_wm_state(struct drm_device *dev)
11858{
11859 struct drm_i915_private *dev_priv = dev->dev_private;
11860 struct skl_ddb_allocation hw_ddb, *sw_ddb;
11861 struct intel_crtc *intel_crtc;
11862 int plane;
11863
11864 if (INTEL_INFO(dev)->gen < 9)
11865 return;
11866
11867 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
11868 sw_ddb = &dev_priv->wm.skl_hw.ddb;
11869
11870 for_each_intel_crtc(dev, intel_crtc) {
11871 struct skl_ddb_entry *hw_entry, *sw_entry;
11872 const enum pipe pipe = intel_crtc->pipe;
11873
11874 if (!intel_crtc->active)
11875 continue;
11876
11877 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000011878 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000011879 hw_entry = &hw_ddb.plane[pipe][plane];
11880 sw_entry = &sw_ddb->plane[pipe][plane];
11881
11882 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11883 continue;
11884
11885 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
11886 "(expected (%u,%u), found (%u,%u))\n",
11887 pipe_name(pipe), plane + 1,
11888 sw_entry->start, sw_entry->end,
11889 hw_entry->start, hw_entry->end);
11890 }
11891
11892 /* cursor */
11893 hw_entry = &hw_ddb.cursor[pipe];
11894 sw_entry = &sw_ddb->cursor[pipe];
11895
11896 if (skl_ddb_entry_equal(hw_entry, sw_entry))
11897 continue;
11898
11899 DRM_ERROR("mismatch in DDB state pipe %c cursor "
11900 "(expected (%u,%u), found (%u,%u))\n",
11901 pipe_name(pipe),
11902 sw_entry->start, sw_entry->end,
11903 hw_entry->start, hw_entry->end);
11904 }
11905}
11906
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011907static void
11908check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011909{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011910 struct intel_connector *connector;
11911
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011912 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011913 /* This also checks the encoder/connector hw state with the
11914 * ->get_hw_state callbacks. */
11915 intel_connector_check_state(connector);
11916
Rob Clarke2c719b2014-12-15 13:56:32 -050011917 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011918 "connector's staged encoder doesn't match current encoder\n");
11919 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011920}
11921
11922static void
11923check_encoder_state(struct drm_device *dev)
11924{
11925 struct intel_encoder *encoder;
11926 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011927
Damien Lespiaub2784e12014-08-05 11:29:37 +010011928 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011929 bool enabled = false;
11930 bool active = false;
11931 enum pipe pipe, tracked_pipe;
11932
11933 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
11934 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030011935 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011936
Rob Clarke2c719b2014-12-15 13:56:32 -050011937 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011938 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050011939 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011940 "encoder's active_connectors set, but no crtc\n");
11941
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011942 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011943 if (connector->base.encoder != &encoder->base)
11944 continue;
11945 enabled = true;
11946 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
11947 active = true;
11948 }
Dave Airlie0e32b392014-05-02 14:02:48 +100011949 /*
11950 * for MST connectors if we unplug the connector is gone
11951 * away but the encoder is still connected to a crtc
11952 * until a modeset happens in response to the hotplug.
11953 */
11954 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
11955 continue;
11956
Rob Clarke2c719b2014-12-15 13:56:32 -050011957 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011958 "encoder's enabled state mismatch "
11959 "(expected %i, found %i)\n",
11960 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050011961 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011962 "active encoder with no crtc\n");
11963
Rob Clarke2c719b2014-12-15 13:56:32 -050011964 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011965 "encoder's computed active state doesn't match tracked active state "
11966 "(expected %i, found %i)\n", active, encoder->connectors_active);
11967
11968 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050011969 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011970 "encoder's hw state doesn't match sw tracking "
11971 "(expected %i, found %i)\n",
11972 encoder->connectors_active, active);
11973
11974 if (!encoder->base.crtc)
11975 continue;
11976
11977 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050011978 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011979 "active encoder's pipe doesn't match"
11980 "(expected %i, found %i)\n",
11981 tracked_pipe, pipe);
11982
11983 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011984}
11985
11986static void
11987check_crtc_state(struct drm_device *dev)
11988{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011989 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020011990 struct intel_crtc *crtc;
11991 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011992 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011993
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011994 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020011995 bool enabled = false;
11996 bool active = false;
11997
Jesse Barnes045ac3b2013-05-14 17:08:26 -070011998 memset(&pipe_config, 0, sizeof(pipe_config));
11999
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012000 DRM_DEBUG_KMS("[CRTC:%d]\n",
12001 crtc->base.base.id);
12002
Matt Roper83d65732015-02-25 13:12:16 -080012003 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012004 "active crtc, but not enabled in sw tracking\n");
12005
Damien Lespiaub2784e12014-08-05 11:29:37 +010012006 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012007 if (encoder->base.crtc != &crtc->base)
12008 continue;
12009 enabled = true;
12010 if (encoder->connectors_active)
12011 active = true;
12012 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012013
Rob Clarke2c719b2014-12-15 13:56:32 -050012014 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012015 "crtc's computed active state doesn't match tracked active state "
12016 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012017 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012018 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012019 "(expected %i, found %i)\n", enabled,
12020 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012021
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012022 active = dev_priv->display.get_pipe_config(crtc,
12023 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012024
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012025 /* hw state is inconsistent with the pipe quirk */
12026 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12027 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012028 active = crtc->active;
12029
Damien Lespiaub2784e12014-08-05 11:29:37 +010012030 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012031 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012032 if (encoder->base.crtc != &crtc->base)
12033 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012034 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012035 encoder->get_config(encoder, &pipe_config);
12036 }
12037
Rob Clarke2c719b2014-12-15 13:56:32 -050012038 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012039 "crtc active state doesn't match with hw state "
12040 "(expected %i, found %i)\n", crtc->active, active);
12041
Daniel Vetterc0b03412013-05-28 12:05:54 +020012042 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012043 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012044 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012045 intel_dump_pipe_config(crtc, &pipe_config,
12046 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012047 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012048 "[sw state]");
12049 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012050 }
12051}
12052
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012053static void
12054check_shared_dpll_state(struct drm_device *dev)
12055{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012056 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012057 struct intel_crtc *crtc;
12058 struct intel_dpll_hw_state dpll_hw_state;
12059 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012060
12061 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12062 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12063 int enabled_crtcs = 0, active_crtcs = 0;
12064 bool active;
12065
12066 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12067
12068 DRM_DEBUG_KMS("%s\n", pll->name);
12069
12070 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12071
Rob Clarke2c719b2014-12-15 13:56:32 -050012072 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012073 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012074 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012075 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012076 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012077 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012078 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012079 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012080 "pll on state mismatch (expected %i, found %i)\n",
12081 pll->on, active);
12082
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012083 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012084 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012085 enabled_crtcs++;
12086 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12087 active_crtcs++;
12088 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012089 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012090 "pll active crtcs mismatch (expected %i, found %i)\n",
12091 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012092 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012093 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012094 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012095
Rob Clarke2c719b2014-12-15 13:56:32 -050012096 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012097 sizeof(dpll_hw_state)),
12098 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012099 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012100}
12101
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012102void
12103intel_modeset_check_state(struct drm_device *dev)
12104{
Damien Lespiau08db6652014-11-04 17:06:52 +000012105 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012106 check_connector_state(dev);
12107 check_encoder_state(dev);
12108 check_crtc_state(dev);
12109 check_shared_dpll_state(dev);
12110}
12111
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012112void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012113 int dotclock)
12114{
12115 /*
12116 * FDI already provided one idea for the dotclock.
12117 * Yell if the encoder disagrees.
12118 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012119 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012120 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012121 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012122}
12123
Ville Syrjälä80715b22014-05-15 20:23:23 +030012124static void update_scanline_offset(struct intel_crtc *crtc)
12125{
12126 struct drm_device *dev = crtc->base.dev;
12127
12128 /*
12129 * The scanline counter increments at the leading edge of hsync.
12130 *
12131 * On most platforms it starts counting from vtotal-1 on the
12132 * first active line. That means the scanline counter value is
12133 * always one less than what we would expect. Ie. just after
12134 * start of vblank, which also occurs at start of hsync (on the
12135 * last active line), the scanline counter will read vblank_start-1.
12136 *
12137 * On gen2 the scanline counter starts counting from 1 instead
12138 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12139 * to keep the value positive), instead of adding one.
12140 *
12141 * On HSW+ the behaviour of the scanline counter depends on the output
12142 * type. For DP ports it behaves like most other platforms, but on HDMI
12143 * there's an extra 1 line difference. So we need to add two instead of
12144 * one to the value.
12145 */
12146 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012147 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012148 int vtotal;
12149
12150 vtotal = mode->crtc_vtotal;
12151 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12152 vtotal /= 2;
12153
12154 crtc->scanline_offset = vtotal - 1;
12155 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012156 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012157 crtc->scanline_offset = 2;
12158 } else
12159 crtc->scanline_offset = 1;
12160}
12161
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012162static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012163intel_modeset_compute_config(struct drm_crtc *crtc,
12164 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012165 struct drm_atomic_state *state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012166 unsigned *modeset_pipes,
12167 unsigned *prepare_pipes,
12168 unsigned *disable_pipes)
12169{
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012170 struct drm_device *dev = crtc->dev;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012171 struct intel_crtc_state *pipe_config = NULL;
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012172 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012173 int ret = 0;
12174
12175 ret = drm_atomic_add_affected_connectors(state, crtc);
12176 if (ret)
12177 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012178
12179 intel_modeset_affected_pipes(crtc, modeset_pipes,
12180 prepare_pipes, disable_pipes);
12181
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012182 for_each_intel_crtc_masked(dev, *disable_pipes, intel_crtc) {
12183 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
12184 if (IS_ERR(pipe_config))
12185 return pipe_config;
12186
12187 pipe_config->base.enable = false;
12188 }
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012189
12190 /*
12191 * Note this needs changes when we start tracking multiple modes
12192 * and crtcs. At that point we'll need to compute the whole config
12193 * (i.e. one pipe_config for each crtc) rather than just the one
12194 * for this crtc.
12195 */
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012196 for_each_intel_crtc_masked(dev, *modeset_pipes, intel_crtc) {
12197 /* FIXME: For now we still expect modeset_pipes has at most
12198 * one bit set. */
12199 if (WARN_ON(&intel_crtc->base != crtc))
12200 continue;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012201
Daniel Vetter8805aa72015-04-10 16:22:38 +020012202 pipe_config = intel_modeset_pipe_config(crtc, mode, state);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012203 if (IS_ERR(pipe_config))
12204 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012205
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012206 pipe_config->base.enable = true;
12207
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012208 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,
12209 "[modeset]");
12210 }
12211
12212 return intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012213}
12214
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012215static int __intel_set_mode_setup_plls(struct drm_atomic_state *state,
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012216 unsigned modeset_pipes,
12217 unsigned disable_pipes)
12218{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012219 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012220 struct drm_i915_private *dev_priv = to_i915(dev);
12221 unsigned clear_pipes = modeset_pipes | disable_pipes;
12222 struct intel_crtc *intel_crtc;
12223 int ret = 0;
12224
12225 if (!dev_priv->display.crtc_compute_clock)
12226 return 0;
12227
12228 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12229 if (ret)
12230 goto done;
12231
12232 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012233 struct intel_crtc_state *crtc_state =
12234 intel_atomic_get_crtc_state(state, intel_crtc);
12235
12236 /* Modeset pipes should have a new state by now */
12237 if (WARN_ON(IS_ERR(crtc_state)))
12238 continue;
12239
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012240 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012241 crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012242 if (ret) {
12243 intel_shared_dpll_abort_config(dev_priv);
12244 goto done;
12245 }
12246 }
12247
12248done:
12249 return ret;
12250}
12251
Daniel Vetterf30da182013-04-11 20:22:50 +020012252static int __intel_set_mode(struct drm_crtc *crtc,
12253 struct drm_display_mode *mode,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012254 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012255 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012256 unsigned modeset_pipes,
12257 unsigned prepare_pipes,
12258 unsigned disable_pipes)
Daniel Vettera6778b32012-07-02 09:56:42 +020012259{
12260 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012261 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012262 struct drm_display_mode *saved_mode;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012263 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012264 struct intel_crtc_state *crtc_state_copy = NULL;
Daniel Vetter25c5b262012-07-08 22:08:04 +020012265 struct intel_crtc *intel_crtc;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012266 int ret = 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012267
Ville Syrjälä4b4b9232013-10-26 17:59:30 +030012268 saved_mode = kmalloc(sizeof(*saved_mode), GFP_KERNEL);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012269 if (!saved_mode)
12270 return -ENOMEM;
Daniel Vettera6778b32012-07-02 09:56:42 +020012271
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012272 crtc_state_copy = kmalloc(sizeof(*crtc_state_copy), GFP_KERNEL);
12273 if (!crtc_state_copy) {
12274 ret = -ENOMEM;
12275 goto done;
12276 }
12277
Tim Gardner3ac18232012-12-07 07:54:26 -070012278 *saved_mode = crtc->mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012279
Jesse Barnes30a970c2013-11-04 13:48:12 -080012280 /*
12281 * See if the config requires any additional preparation, e.g.
12282 * to adjust global state with pipes off. We need to do this
12283 * here so we can get the modeset_pipe updated config for the new
12284 * mode set on this crtc. For other crtcs we need to use the
12285 * adjusted_mode bits in the crtc directly.
12286 */
Vandana Kannanf8437dd12014-11-24 13:37:39 +053012287 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012288 ret = valleyview_modeset_global_pipes(state, &prepare_pipes);
12289 if (ret)
12290 goto done;
Jesse Barnes30a970c2013-11-04 13:48:12 -080012291
Ville Syrjäläc164f832013-11-05 22:34:12 +020012292 /* may have added more to prepare_pipes than we should */
12293 prepare_pipes &= ~disable_pipes;
12294 }
12295
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012296 ret = __intel_set_mode_setup_plls(state, modeset_pipes, disable_pipes);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012297 if (ret)
12298 goto done;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +020012299
Daniel Vetter460da9162013-03-27 00:44:51 +010012300 for_each_intel_crtc_masked(dev, disable_pipes, intel_crtc)
12301 intel_crtc_disable(&intel_crtc->base);
12302
Daniel Vetterea9d7582012-07-10 10:42:52 +020012303 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012304 if (intel_crtc->base.state->enable)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012305 dev_priv->display.crtc_disable(&intel_crtc->base);
12306 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012307
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012308 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12309 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012310 *
12311 * Note we'll need to fix this up when we start tracking multiple
12312 * pipes; here we assume a single modeset_pipe and only track the
12313 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012314 */
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012315 if (modeset_pipes) {
Daniel Vetter25c5b262012-07-08 22:08:04 +020012316 crtc->mode = *mode;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012317 /* mode_set/enable/disable functions rely on a correct pipe
12318 * config. */
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020012319 intel_crtc_set_state(to_intel_crtc(crtc), pipe_config);
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012320
12321 /*
12322 * Calculate and store various constants which
12323 * are later needed by vblank and swap-completion
12324 * timestamping. They are derived from true hwmode.
12325 */
12326 drm_calc_timestamping_constants(crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012327 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012328 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012329
Daniel Vetterea9d7582012-07-10 10:42:52 +020012330 /* Only after disabling all output pipelines that will be changed can we
12331 * update the the output configuration. */
12332 intel_modeset_update_state(dev, prepare_pipes);
12333
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012334 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012335
Daniel Vetter25c5b262012-07-08 22:08:04 +020012336 for_each_intel_crtc_masked(dev, modeset_pipes, intel_crtc) {
Gustavo Padovan455a6802014-12-01 15:40:11 -080012337 struct drm_plane *primary = intel_crtc->base.primary;
12338 int vdisplay, hdisplay;
Daniel Vetter4c107942014-04-24 23:55:05 +020012339
Gustavo Padovan455a6802014-12-01 15:40:11 -080012340 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012341 ret = drm_plane_helper_update(primary, &intel_crtc->base,
12342 fb, 0, 0,
12343 hdisplay, vdisplay,
12344 x << 16, y << 16,
12345 hdisplay << 16, vdisplay << 16);
Daniel Vettera6778b32012-07-02 09:56:42 +020012346 }
12347
12348 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ville Syrjälä80715b22014-05-15 20:23:23 +030012349 for_each_intel_crtc_masked(dev, prepare_pipes, intel_crtc) {
12350 update_scanline_offset(intel_crtc);
12351
Daniel Vetter25c5b262012-07-08 22:08:04 +020012352 dev_priv->display.crtc_enable(&intel_crtc->base);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012353 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012354
Daniel Vettera6778b32012-07-02 09:56:42 +020012355 /* FIXME: add subpixel order */
12356done:
Matt Roper83d65732015-02-25 13:12:16 -080012357 if (ret && crtc->state->enable)
Tim Gardner3ac18232012-12-07 07:54:26 -070012358 crtc->mode = *saved_mode;
Daniel Vettera6778b32012-07-02 09:56:42 +020012359
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012360 if (ret == 0 && pipe_config) {
12361 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
12362
12363 /* The pipe_config will be freed with the atomic state, so
12364 * make a copy. */
12365 memcpy(crtc_state_copy, intel_crtc->config,
12366 sizeof *crtc_state_copy);
12367 intel_crtc->config = crtc_state_copy;
12368 intel_crtc->base.state = &crtc_state_copy->base;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012369 } else {
12370 kfree(crtc_state_copy);
12371 }
12372
Tim Gardner3ac18232012-12-07 07:54:26 -070012373 kfree(saved_mode);
Daniel Vettera6778b32012-07-02 09:56:42 +020012374 return ret;
12375}
12376
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012377static int intel_set_mode_pipes(struct drm_crtc *crtc,
12378 struct drm_display_mode *mode,
12379 int x, int y, struct drm_framebuffer *fb,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012380 struct intel_crtc_state *pipe_config,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012381 unsigned modeset_pipes,
12382 unsigned prepare_pipes,
12383 unsigned disable_pipes)
12384{
12385 int ret;
12386
12387 ret = __intel_set_mode(crtc, mode, x, y, fb, pipe_config, modeset_pipes,
12388 prepare_pipes, disable_pipes);
12389
12390 if (ret == 0)
12391 intel_modeset_check_state(crtc->dev);
12392
12393 return ret;
12394}
12395
Damien Lespiaue7457a92013-08-08 22:28:59 +010012396static int intel_set_mode(struct drm_crtc *crtc,
12397 struct drm_display_mode *mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012398 int x, int y, struct drm_framebuffer *fb,
12399 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012400{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012401 struct intel_crtc_state *pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012402 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012403 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012404
Daniel Vetter8805aa72015-04-10 16:22:38 +020012405 pipe_config = intel_modeset_compute_config(crtc, mode, state,
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012406 &modeset_pipes,
12407 &prepare_pipes,
12408 &disable_pipes);
Daniel Vetterf30da182013-04-11 20:22:50 +020012409
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012410 if (IS_ERR(pipe_config)) {
12411 ret = PTR_ERR(pipe_config);
12412 goto out;
12413 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012414
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012415 ret = intel_set_mode_pipes(crtc, mode, x, y, fb, pipe_config,
12416 modeset_pipes, prepare_pipes,
12417 disable_pipes);
12418 if (ret)
12419 goto out;
12420
12421out:
12422 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012423}
12424
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012425void intel_crtc_restore_mode(struct drm_crtc *crtc)
12426{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012427 struct drm_device *dev = crtc->dev;
12428 struct drm_atomic_state *state;
12429 struct intel_encoder *encoder;
12430 struct intel_connector *connector;
12431 struct drm_connector_state *connector_state;
12432
12433 state = drm_atomic_state_alloc(dev);
12434 if (!state) {
12435 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12436 crtc->base.id);
12437 return;
12438 }
12439
12440 state->acquire_ctx = dev->mode_config.acquire_ctx;
12441
12442 /* The force restore path in the HW readout code relies on the staged
12443 * config still keeping the user requested config while the actual
12444 * state has been overwritten by the configuration read from HW. We
12445 * need to copy the staged config to the atomic state, otherwise the
12446 * mode set will just reapply the state the HW is already in. */
12447 for_each_intel_encoder(dev, encoder) {
12448 if (&encoder->new_crtc->base != crtc)
12449 continue;
12450
12451 for_each_intel_connector(dev, connector) {
12452 if (connector->new_encoder != encoder)
12453 continue;
12454
12455 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12456 if (IS_ERR(connector_state)) {
12457 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12458 connector->base.base.id,
12459 connector->base.name,
12460 PTR_ERR(connector_state));
12461 continue;
12462 }
12463
12464 connector_state->crtc = crtc;
12465 connector_state->best_encoder = &encoder->base;
12466 }
12467 }
12468
12469 intel_set_mode(crtc, &crtc->mode, crtc->x, crtc->y, crtc->primary->fb,
12470 state);
12471
12472 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012473}
12474
Daniel Vetter25c5b262012-07-08 22:08:04 +020012475#undef for_each_intel_crtc_masked
12476
Daniel Vetterd9e55602012-07-04 22:16:09 +020012477static void intel_set_config_free(struct intel_set_config *config)
12478{
12479 if (!config)
12480 return;
12481
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012482 kfree(config->save_connector_encoders);
12483 kfree(config->save_encoder_crtcs);
Ville Syrjälä76688512014-01-10 11:28:06 +020012484 kfree(config->save_crtc_enabled);
Daniel Vetterd9e55602012-07-04 22:16:09 +020012485 kfree(config);
12486}
12487
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012488static int intel_set_config_save_state(struct drm_device *dev,
12489 struct intel_set_config *config)
12490{
Ville Syrjälä76688512014-01-10 11:28:06 +020012491 struct drm_crtc *crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012492 struct drm_encoder *encoder;
12493 struct drm_connector *connector;
12494 int count;
12495
Ville Syrjälä76688512014-01-10 11:28:06 +020012496 config->save_crtc_enabled =
12497 kcalloc(dev->mode_config.num_crtc,
12498 sizeof(bool), GFP_KERNEL);
12499 if (!config->save_crtc_enabled)
12500 return -ENOMEM;
12501
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012502 config->save_encoder_crtcs =
12503 kcalloc(dev->mode_config.num_encoder,
12504 sizeof(struct drm_crtc *), GFP_KERNEL);
12505 if (!config->save_encoder_crtcs)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012506 return -ENOMEM;
12507
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012508 config->save_connector_encoders =
12509 kcalloc(dev->mode_config.num_connector,
12510 sizeof(struct drm_encoder *), GFP_KERNEL);
12511 if (!config->save_connector_encoders)
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012512 return -ENOMEM;
12513
12514 /* Copy data. Note that driver private data is not affected.
12515 * Should anything bad happen only the expected state is
12516 * restored, not the drivers personal bookkeeping.
12517 */
12518 count = 0;
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010012519 for_each_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012520 config->save_crtc_enabled[count++] = crtc->state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020012521 }
12522
12523 count = 0;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012524 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012525 config->save_encoder_crtcs[count++] = encoder->crtc;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012526 }
12527
12528 count = 0;
12529 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Daniel Vetter1aa4b622012-07-05 16:20:48 +020012530 config->save_connector_encoders[count++] = connector->encoder;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012531 }
12532
12533 return 0;
12534}
12535
12536static void intel_set_config_restore_state(struct drm_device *dev,
12537 struct intel_set_config *config)
12538{
Ville Syrjälä76688512014-01-10 11:28:06 +020012539 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020012540 struct intel_encoder *encoder;
12541 struct intel_connector *connector;
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012542 int count;
12543
12544 count = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012545 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012546 crtc->new_enabled = config->save_crtc_enabled[count++];
12547 }
12548
12549 count = 0;
Damien Lespiaub2784e12014-08-05 11:29:37 +010012550 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012551 encoder->new_crtc =
12552 to_intel_crtc(config->save_encoder_crtcs[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012553 }
12554
12555 count = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012556 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012557 connector->new_encoder =
12558 to_intel_encoder(config->save_connector_encoders[count++]);
Daniel Vetter85f9eb72012-07-04 22:24:08 +020012559 }
12560}
12561
Imre Deake3de42b2013-05-03 19:44:07 +020012562static bool
Chris Wilson2e57f472013-07-17 12:14:40 +010012563is_crtc_connector_off(struct drm_mode_set *set)
Imre Deake3de42b2013-05-03 19:44:07 +020012564{
12565 int i;
12566
Chris Wilson2e57f472013-07-17 12:14:40 +010012567 if (set->num_connectors == 0)
12568 return false;
12569
12570 if (WARN_ON(set->connectors == NULL))
12571 return false;
12572
12573 for (i = 0; i < set->num_connectors; i++)
12574 if (set->connectors[i]->encoder &&
12575 set->connectors[i]->encoder->crtc == set->crtc &&
12576 set->connectors[i]->dpms != DRM_MODE_DPMS_ON)
Imre Deake3de42b2013-05-03 19:44:07 +020012577 return true;
12578
12579 return false;
12580}
12581
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012582static void
12583intel_set_config_compute_mode_changes(struct drm_mode_set *set,
12584 struct intel_set_config *config)
12585{
12586
12587 /* We should be able to check here if the fb has the same properties
12588 * and then just flip_or_move it */
Chris Wilson2e57f472013-07-17 12:14:40 +010012589 if (is_crtc_connector_off(set)) {
12590 config->mode_changed = true;
Matt Roperf4510a22014-04-01 15:22:40 -070012591 } else if (set->crtc->primary->fb != set->fb) {
Matt Roper3b150f02014-05-29 08:06:53 -070012592 /*
12593 * If we have no fb, we can only flip as long as the crtc is
12594 * active, otherwise we need a full mode set. The crtc may
12595 * be active if we've only disabled the primary plane, or
12596 * in fastboot situations.
12597 */
Matt Roperf4510a22014-04-01 15:22:40 -070012598 if (set->crtc->primary->fb == NULL) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012599 struct intel_crtc *intel_crtc =
12600 to_intel_crtc(set->crtc);
12601
Matt Roper3b150f02014-05-29 08:06:53 -070012602 if (intel_crtc->active) {
Jesse Barnes319d9822013-06-26 01:38:19 +030012603 DRM_DEBUG_KMS("crtc has no fb, will flip\n");
12604 config->fb_changed = true;
12605 } else {
12606 DRM_DEBUG_KMS("inactive crtc, full mode set\n");
12607 config->mode_changed = true;
12608 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012609 } else if (set->fb == NULL) {
12610 config->mode_changed = true;
Daniel Vetter72f49012013-03-28 16:01:35 +010012611 } else if (set->fb->pixel_format !=
Matt Roperf4510a22014-04-01 15:22:40 -070012612 set->crtc->primary->fb->pixel_format) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012613 config->mode_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012614 } else {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012615 config->fb_changed = true;
Imre Deake3de42b2013-05-03 19:44:07 +020012616 }
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012617 }
12618
Daniel Vetter835c5872012-07-10 18:11:08 +020012619 if (set->fb && (set->x != set->crtc->x || set->y != set->crtc->y))
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012620 config->fb_changed = true;
12621
12622 if (set->mode && !drm_mode_equal(set->mode, &set->crtc->mode)) {
12623 DRM_DEBUG_KMS("modes are different, full mode set\n");
12624 drm_mode_debug_printmodeline(&set->crtc->mode);
12625 drm_mode_debug_printmodeline(set->mode);
12626 config->mode_changed = true;
12627 }
Chris Wilsona1d95702013-08-13 18:48:47 +010012628
12629 DRM_DEBUG_KMS("computed changes for [CRTC:%d], mode_changed=%d, fb_changed=%d\n",
12630 set->crtc->base.id, config->mode_changed, config->fb_changed);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012631}
12632
Daniel Vetter2e431052012-07-04 22:42:15 +020012633static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012634intel_modeset_stage_output_state(struct drm_device *dev,
12635 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012636 struct intel_set_config *config,
12637 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012638{
Daniel Vetter9a935852012-07-05 22:34:27 +020012639 struct intel_connector *connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012640 struct drm_connector_state *connector_state;
Daniel Vetter9a935852012-07-05 22:34:27 +020012641 struct intel_encoder *encoder;
Ville Syrjälä76688512014-01-10 11:28:06 +020012642 struct intel_crtc *crtc;
Paulo Zanonif3f08572013-08-12 14:56:53 -030012643 int ro;
Daniel Vetter50f56112012-07-02 09:35:43 +020012644
Damien Lespiau9abdda72013-02-13 13:29:23 +000012645 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012646 * of connectors. For paranoia, double-check this. */
12647 WARN_ON(!set->fb && (set->num_connectors != 0));
12648 WARN_ON(set->fb && (set->num_connectors == 0));
12649
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012650 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012651 /* Otherwise traverse passed in connector list and get encoders
12652 * for them. */
Daniel Vetter50f56112012-07-02 09:35:43 +020012653 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012654 if (set->connectors[ro] == &connector->base) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012655 connector->new_encoder = intel_find_encoder(connector, to_intel_crtc(set->crtc)->pipe);
Daniel Vetter50f56112012-07-02 09:35:43 +020012656 break;
12657 }
12658 }
12659
Daniel Vetter9a935852012-07-05 22:34:27 +020012660 /* If we disable the crtc, disable all its connectors. Also, if
12661 * the connector is on the changing crtc but not on the new
12662 * connector list, disable it. */
12663 if ((!set->fb || ro == set->num_connectors) &&
12664 connector->base.encoder &&
12665 connector->base.encoder->crtc == set->crtc) {
12666 connector->new_encoder = NULL;
12667
12668 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12669 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012670 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012671 }
12672
12673
12674 if (&connector->new_encoder->base != connector->base.encoder) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012675 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] encoder changed, full mode switch\n",
12676 connector->base.base.id,
12677 connector->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012678 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012679 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012680 }
12681 /* connector->new_encoder is now updated for all connectors. */
12682
12683 /* Update crtc of enabled connectors. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012684 for_each_intel_connector(dev, connector) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012685 struct drm_crtc *new_crtc;
12686
Daniel Vetter9a935852012-07-05 22:34:27 +020012687 if (!connector->new_encoder)
Daniel Vetter50f56112012-07-02 09:35:43 +020012688 continue;
12689
Daniel Vetter9a935852012-07-05 22:34:27 +020012690 new_crtc = connector->new_encoder->base.crtc;
Daniel Vetter50f56112012-07-02 09:35:43 +020012691
12692 for (ro = 0; ro < set->num_connectors; ro++) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012693 if (set->connectors[ro] == &connector->base)
Daniel Vetter50f56112012-07-02 09:35:43 +020012694 new_crtc = set->crtc;
12695 }
12696
12697 /* Make sure the new CRTC will work with the encoder */
Thierry Reding14509912014-01-13 12:00:22 +010012698 if (!drm_encoder_crtc_ok(&connector->new_encoder->base,
12699 new_crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012700 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012701 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012702 connector->new_encoder->new_crtc = to_intel_crtc(new_crtc);
Daniel Vetter9a935852012-07-05 22:34:27 +020012703
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012704 connector_state =
12705 drm_atomic_get_connector_state(state, &connector->base);
12706 if (IS_ERR(connector_state))
12707 return PTR_ERR(connector_state);
12708
12709 connector_state->crtc = new_crtc;
12710 connector_state->best_encoder = &connector->new_encoder->base;
12711
Daniel Vetter9a935852012-07-05 22:34:27 +020012712 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12713 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012714 connector->base.name,
Daniel Vetter9a935852012-07-05 22:34:27 +020012715 new_crtc->base.id);
12716 }
12717
12718 /* Check for any encoders that needs to be disabled. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010012719 for_each_intel_encoder(dev, encoder) {
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012720 int num_connectors = 0;
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012721 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020012722 if (connector->new_encoder == encoder) {
12723 WARN_ON(!connector->new_encoder->new_crtc);
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012724 num_connectors++;
Daniel Vetter9a935852012-07-05 22:34:27 +020012725 }
12726 }
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012727
12728 if (num_connectors == 0)
12729 encoder->new_crtc = NULL;
12730 else if (num_connectors > 1)
12731 return -EINVAL;
12732
Daniel Vetter9a935852012-07-05 22:34:27 +020012733 /* Only now check for crtc changes so we don't miss encoders
12734 * that will be disabled. */
12735 if (&encoder->new_crtc->base != encoder->base.crtc) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012736 DRM_DEBUG_KMS("[ENCODER:%d:%s] crtc changed, full mode switch\n",
12737 encoder->base.base.id,
12738 encoder->base.name);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012739 config->mode_changed = true;
Daniel Vetter50f56112012-07-02 09:35:43 +020012740 }
12741 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012742 /* Now we've also updated encoder->new_crtc for all encoders. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012743 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012744 connector_state =
12745 drm_atomic_get_connector_state(state, &connector->base);
Ander Conselvan de Oliveira9d918c12015-03-27 15:33:51 +020012746 if (IS_ERR(connector_state))
12747 return PTR_ERR(connector_state);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012748
12749 if (connector->new_encoder) {
Dave Airlie0e32b392014-05-02 14:02:48 +100012750 if (connector->new_encoder != connector->encoder)
12751 connector->encoder = connector->new_encoder;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012752 } else {
12753 connector_state->crtc = NULL;
Ander Conselvan de Oliveiraf61cccf2015-03-31 11:35:00 +030012754 connector_state->best_encoder = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012755 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012756 }
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012757 for_each_intel_crtc(dev, crtc) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012758 crtc->new_enabled = false;
12759
Damien Lespiaub2784e12014-08-05 11:29:37 +010012760 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä76688512014-01-10 11:28:06 +020012761 if (encoder->new_crtc == crtc) {
12762 crtc->new_enabled = true;
12763 break;
12764 }
12765 }
12766
Matt Roper83d65732015-02-25 13:12:16 -080012767 if (crtc->new_enabled != crtc->base.state->enable) {
Ander Conselvan de Oliveira10634182015-03-03 15:21:57 +020012768 DRM_DEBUG_KMS("[CRTC:%d] %sabled, full mode switch\n",
12769 crtc->base.base.id,
Ville Syrjälä76688512014-01-10 11:28:06 +020012770 crtc->new_enabled ? "en" : "dis");
12771 config->mode_changed = true;
12772 }
12773 }
12774
Daniel Vetter2e431052012-07-04 22:42:15 +020012775 return 0;
12776}
12777
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012778static void disable_crtc_nofb(struct intel_crtc *crtc)
12779{
12780 struct drm_device *dev = crtc->base.dev;
12781 struct intel_encoder *encoder;
12782 struct intel_connector *connector;
12783
12784 DRM_DEBUG_KMS("Trying to restore without FB -> disabling pipe %c\n",
12785 pipe_name(crtc->pipe));
12786
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012787 for_each_intel_connector(dev, connector) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012788 if (connector->new_encoder &&
12789 connector->new_encoder->new_crtc == crtc)
12790 connector->new_encoder = NULL;
12791 }
12792
Damien Lespiaub2784e12014-08-05 11:29:37 +010012793 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012794 if (encoder->new_crtc == crtc)
12795 encoder->new_crtc = NULL;
12796 }
12797
12798 crtc->new_enabled = false;
12799}
12800
Daniel Vetter2e431052012-07-04 22:42:15 +020012801static int intel_crtc_set_config(struct drm_mode_set *set)
12802{
12803 struct drm_device *dev;
Daniel Vetter2e431052012-07-04 22:42:15 +020012804 struct drm_mode_set save_set;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012805 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020012806 struct intel_set_config *config;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012807 struct intel_crtc_state *pipe_config;
Jesse Barnes50f52752014-11-07 13:11:00 -080012808 unsigned modeset_pipes, prepare_pipes, disable_pipes;
Daniel Vetter2e431052012-07-04 22:42:15 +020012809 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012810
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012811 BUG_ON(!set);
12812 BUG_ON(!set->crtc);
12813 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012814
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012815 /* Enforce sane interface api - has been abused by the fb helper. */
12816 BUG_ON(!set->mode && set->fb);
12817 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012818
Daniel Vetter2e431052012-07-04 22:42:15 +020012819 if (set->fb) {
12820 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12821 set->crtc->base.id, set->fb->base.id,
12822 (int)set->num_connectors, set->x, set->y);
12823 } else {
12824 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012825 }
12826
12827 dev = set->crtc->dev;
12828
12829 ret = -ENOMEM;
12830 config = kzalloc(sizeof(*config), GFP_KERNEL);
12831 if (!config)
12832 goto out_config;
12833
12834 ret = intel_set_config_save_state(dev, config);
12835 if (ret)
12836 goto out_config;
12837
12838 save_set.crtc = set->crtc;
12839 save_set.mode = &set->crtc->mode;
12840 save_set.x = set->crtc->x;
12841 save_set.y = set->crtc->y;
Matt Roperf4510a22014-04-01 15:22:40 -070012842 save_set.fb = set->crtc->primary->fb;
Daniel Vetter2e431052012-07-04 22:42:15 +020012843
12844 /* Compute whether we need a full modeset, only an fb base update or no
12845 * change at all. In the future we might also check whether only the
12846 * mode changed, e.g. for LVDS where we only change the panel fitter in
12847 * such cases. */
12848 intel_set_config_compute_mode_changes(set, config);
12849
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012850 state = drm_atomic_state_alloc(dev);
12851 if (!state) {
12852 ret = -ENOMEM;
12853 goto out_config;
12854 }
12855
12856 state->acquire_ctx = dev->mode_config.acquire_ctx;
12857
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012858 ret = intel_modeset_stage_output_state(dev, set, config, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012859 if (ret)
12860 goto fail;
12861
Jesse Barnes50f52752014-11-07 13:11:00 -080012862 pipe_config = intel_modeset_compute_config(set->crtc, set->mode,
Daniel Vetter8805aa72015-04-10 16:22:38 +020012863 state,
Jesse Barnes50f52752014-11-07 13:11:00 -080012864 &modeset_pipes,
12865 &prepare_pipes,
12866 &disable_pipes);
Jesse Barnes20664592014-11-05 14:26:09 -080012867 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012868 ret = PTR_ERR(pipe_config);
Jesse Barnes50f52752014-11-07 13:11:00 -080012869 goto fail;
Jesse Barnes20664592014-11-05 14:26:09 -080012870 } else if (pipe_config) {
Ville Syrjäläb9950a12014-11-21 21:00:36 +020012871 if (pipe_config->has_audio !=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012872 to_intel_crtc(set->crtc)->config->has_audio)
Jesse Barnes20664592014-11-05 14:26:09 -080012873 config->mode_changed = true;
12874
Jesse Barnesaf15d2c2014-12-01 09:54:28 -080012875 /*
12876 * Note we have an issue here with infoframes: current code
12877 * only updates them on the full mode set path per hw
12878 * requirements. So here we should be checking for any
12879 * required changes and forcing a mode set.
12880 */
Jesse Barnes20664592014-11-05 14:26:09 -080012881 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012882
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012883 intel_update_pipe_size(to_intel_crtc(set->crtc));
12884
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012885 if (config->mode_changed) {
Jesse Barnes50f52752014-11-07 13:11:00 -080012886 ret = intel_set_mode_pipes(set->crtc, set->mode,
12887 set->x, set->y, set->fb, pipe_config,
12888 modeset_pipes, prepare_pipes,
12889 disable_pipes);
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012890 } else if (config->fb_changed) {
Matt Roper3b150f02014-05-29 08:06:53 -070012891 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080012892 struct drm_plane *primary = set->crtc->primary;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030012893 struct intel_plane_state *plane_state =
12894 to_intel_plane_state(primary->state);
12895 bool was_visible = plane_state->visible;
Gustavo Padovan455a6802014-12-01 15:40:11 -080012896 int vdisplay, hdisplay;
Matt Roper3b150f02014-05-29 08:06:53 -070012897
Gustavo Padovan455a6802014-12-01 15:40:11 -080012898 drm_crtc_get_hv_timing(set->mode, &hdisplay, &vdisplay);
Matt Roper70a101f2015-04-08 18:56:53 -070012899 ret = drm_plane_helper_update(primary, set->crtc, set->fb,
12900 0, 0, hdisplay, vdisplay,
12901 set->x << 16, set->y << 16,
12902 hdisplay << 16, vdisplay << 16);
Matt Roper3b150f02014-05-29 08:06:53 -070012903
12904 /*
12905 * We need to make sure the primary plane is re-enabled if it
12906 * has previously been turned off.
12907 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030012908 plane_state = to_intel_plane_state(primary->state);
12909 if (ret == 0 && !was_visible && plane_state->visible) {
Matt Roper3b150f02014-05-29 08:06:53 -070012910 WARN_ON(!intel_crtc->active);
Ville Syrjäläfdd508a2014-08-08 21:51:11 +030012911 intel_enable_primary_hw_plane(set->crtc->primary, set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012912 }
12913
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012914 /*
12915 * In the fastboot case this may be our only check of the
12916 * state after boot. It would be better to only do it on
12917 * the first update, but we don't have a nice way of doing that
12918 * (and really, set_config isn't used much for high freq page
12919 * flipping, so increasing its cost here shouldn't be a big
12920 * deal).
12921 */
Jani Nikulad330a952014-01-21 11:24:25 +020012922 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012923 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012924 }
12925
Chris Wilson2d05eae2013-05-03 17:36:25 +010012926 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012927 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12928 set->crtc->base.id, ret);
Daniel Vetter50f56112012-07-02 09:35:43 +020012929fail:
Chris Wilson2d05eae2013-05-03 17:36:25 +010012930 intel_set_config_restore_state(dev, config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012931
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012932 drm_atomic_state_clear(state);
12933
Ville Syrjälä7d00a1f2014-01-10 11:28:09 +020012934 /*
12935 * HACK: if the pipe was on, but we didn't have a framebuffer,
12936 * force the pipe off to avoid oopsing in the modeset code
12937 * due to fb==NULL. This should only happen during boot since
12938 * we don't yet reconstruct the FB from the hardware state.
12939 */
12940 if (to_intel_crtc(save_set.crtc)->new_enabled && !save_set.fb)
12941 disable_crtc_nofb(to_intel_crtc(save_set.crtc));
12942
Chris Wilson2d05eae2013-05-03 17:36:25 +010012943 /* Try to restore the config */
12944 if (config->mode_changed &&
12945 intel_set_mode(save_set.crtc, save_set.mode,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012946 save_set.x, save_set.y, save_set.fb,
12947 state))
Chris Wilson2d05eae2013-05-03 17:36:25 +010012948 DRM_ERROR("failed to restore config after modeset failure\n");
12949 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012950
Daniel Vetterd9e55602012-07-04 22:16:09 +020012951out_config:
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012952 if (state)
12953 drm_atomic_state_free(state);
12954
Daniel Vetterd9e55602012-07-04 22:16:09 +020012955 intel_set_config_free(config);
Daniel Vetter50f56112012-07-02 09:35:43 +020012956 return ret;
12957}
12958
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012959static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012960 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012961 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012962 .destroy = intel_crtc_destroy,
12963 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012964 .atomic_duplicate_state = intel_crtc_duplicate_state,
12965 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012966};
12967
Daniel Vetter53589012013-06-05 13:34:16 +020012968static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
12969 struct intel_shared_dpll *pll,
12970 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010012971{
Daniel Vetter53589012013-06-05 13:34:16 +020012972 uint32_t val;
12973
Daniel Vetterf458ebb2014-09-30 10:56:39 +020012974 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030012975 return false;
12976
Daniel Vetter53589012013-06-05 13:34:16 +020012977 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020012978 hw_state->dpll = val;
12979 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
12980 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020012981
12982 return val & DPLL_VCO_ENABLE;
12983}
12984
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012985static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
12986 struct intel_shared_dpll *pll)
12987{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012988 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
12989 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012990}
12991
Daniel Vettere7b903d2013-06-05 13:34:14 +020012992static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
12993 struct intel_shared_dpll *pll)
12994{
Daniel Vettere7b903d2013-06-05 13:34:14 +020012995 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020012996 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020012997
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012998 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020012999
13000 /* Wait for the clocks to stabilize. */
13001 POSTING_READ(PCH_DPLL(pll->id));
13002 udelay(150);
13003
13004 /* The pixel multiplier can only be updated once the
13005 * DPLL is enabled and the clocks are stable.
13006 *
13007 * So write it again.
13008 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013009 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013010 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013011 udelay(200);
13012}
13013
13014static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13015 struct intel_shared_dpll *pll)
13016{
13017 struct drm_device *dev = dev_priv->dev;
13018 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013019
13020 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013021 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013022 if (intel_crtc_to_shared_dpll(crtc) == pll)
13023 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13024 }
13025
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013026 I915_WRITE(PCH_DPLL(pll->id), 0);
13027 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013028 udelay(200);
13029}
13030
Daniel Vetter46edb022013-06-05 13:34:12 +020013031static char *ibx_pch_dpll_names[] = {
13032 "PCH DPLL A",
13033 "PCH DPLL B",
13034};
13035
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013036static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013037{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013038 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013039 int i;
13040
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013041 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013042
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013043 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013044 dev_priv->shared_dplls[i].id = i;
13045 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013046 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013047 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13048 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013049 dev_priv->shared_dplls[i].get_hw_state =
13050 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013051 }
13052}
13053
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013054static void intel_shared_dpll_init(struct drm_device *dev)
13055{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013056 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013057
Daniel Vetter9cd86932014-06-25 22:01:57 +030013058 if (HAS_DDI(dev))
13059 intel_ddi_pll_init(dev);
13060 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013061 ibx_pch_dpll_init(dev);
13062 else
13063 dev_priv->num_shared_dpll = 0;
13064
13065 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013066}
13067
Matt Roper6beb8c232014-12-01 15:40:14 -080013068/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013069 * intel_wm_need_update - Check whether watermarks need updating
13070 * @plane: drm plane
13071 * @state: new plane state
13072 *
13073 * Check current plane state versus the new one to determine whether
13074 * watermarks need to be recalculated.
13075 *
13076 * Returns true or false.
13077 */
13078bool intel_wm_need_update(struct drm_plane *plane,
13079 struct drm_plane_state *state)
13080{
13081 /* Update watermarks on tiling changes. */
13082 if (!plane->state->fb || !state->fb ||
13083 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13084 plane->state->rotation != state->rotation)
13085 return true;
13086
13087 return false;
13088}
13089
13090/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013091 * intel_prepare_plane_fb - Prepare fb for usage on plane
13092 * @plane: drm plane to prepare for
13093 * @fb: framebuffer to prepare for presentation
13094 *
13095 * Prepares a framebuffer for usage on a display plane. Generally this
13096 * involves pinning the underlying object and updating the frontbuffer tracking
13097 * bits. Some older platforms need special physical address handling for
13098 * cursor planes.
13099 *
13100 * Returns 0 on success, negative error code on failure.
13101 */
13102int
13103intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013104 struct drm_framebuffer *fb,
13105 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013106{
13107 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013108 struct intel_plane *intel_plane = to_intel_plane(plane);
13109 enum pipe pipe = intel_plane->pipe;
13110 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13111 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13112 unsigned frontbuffer_bits = 0;
13113 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013114
Matt Roperea2c67b2014-12-23 10:41:52 -080013115 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013116 return 0;
13117
Matt Roper6beb8c232014-12-01 15:40:14 -080013118 switch (plane->type) {
13119 case DRM_PLANE_TYPE_PRIMARY:
13120 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13121 break;
13122 case DRM_PLANE_TYPE_CURSOR:
13123 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13124 break;
13125 case DRM_PLANE_TYPE_OVERLAY:
13126 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13127 break;
13128 }
Matt Roper465c1202014-05-29 08:06:54 -070013129
Matt Roper4c345742014-07-09 16:22:10 -070013130 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013131
Matt Roper6beb8c232014-12-01 15:40:14 -080013132 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13133 INTEL_INFO(dev)->cursor_needs_physical) {
13134 int align = IS_I830(dev) ? 16 * 1024 : 256;
13135 ret = i915_gem_object_attach_phys(obj, align);
13136 if (ret)
13137 DRM_DEBUG_KMS("failed to attach phys object\n");
13138 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013139 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013140 }
13141
13142 if (ret == 0)
13143 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13144
13145 mutex_unlock(&dev->struct_mutex);
13146
13147 return ret;
13148}
13149
Matt Roper38f3ce32014-12-02 07:45:25 -080013150/**
13151 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13152 * @plane: drm plane to clean up for
13153 * @fb: old framebuffer that was on plane
13154 *
13155 * Cleans up a framebuffer that has just been removed from a plane.
13156 */
13157void
13158intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013159 struct drm_framebuffer *fb,
13160 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013161{
13162 struct drm_device *dev = plane->dev;
13163 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13164
13165 if (WARN_ON(!obj))
13166 return;
13167
13168 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13169 !INTEL_INFO(dev)->cursor_needs_physical) {
13170 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013171 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013172 mutex_unlock(&dev->struct_mutex);
13173 }
Matt Roper465c1202014-05-29 08:06:54 -070013174}
13175
Chandra Konduru6156a452015-04-27 13:48:39 -070013176int
13177skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13178{
13179 int max_scale;
13180 struct drm_device *dev;
13181 struct drm_i915_private *dev_priv;
13182 int crtc_clock, cdclk;
13183
13184 if (!intel_crtc || !crtc_state)
13185 return DRM_PLANE_HELPER_NO_SCALING;
13186
13187 dev = intel_crtc->base.dev;
13188 dev_priv = dev->dev_private;
13189 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13190 cdclk = dev_priv->display.get_display_clock_speed(dev);
13191
13192 if (!crtc_clock || !cdclk)
13193 return DRM_PLANE_HELPER_NO_SCALING;
13194
13195 /*
13196 * skl max scale is lower of:
13197 * close to 3 but not 3, -1 is for that purpose
13198 * or
13199 * cdclk/crtc_clock
13200 */
13201 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13202
13203 return max_scale;
13204}
13205
Matt Roper465c1202014-05-29 08:06:54 -070013206static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013207intel_check_primary_plane(struct drm_plane *plane,
13208 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013209{
Matt Roper32b7eee2014-12-24 07:59:06 -080013210 struct drm_device *dev = plane->dev;
13211 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013212 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013213 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013214 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013215 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013216 struct drm_rect *dest = &state->dst;
13217 struct drm_rect *src = &state->src;
13218 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013219 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013220 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13221 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013222 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013223
Matt Roperea2c67b2014-12-23 10:41:52 -080013224 crtc = crtc ? crtc : plane->crtc;
13225 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013226 crtc_state = state->base.state ?
13227 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013228
Chandra Konduru6156a452015-04-27 13:48:39 -070013229 if (INTEL_INFO(dev)->gen >= 9) {
13230 min_scale = 1;
13231 max_scale = skl_max_scale(intel_crtc, crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013232 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013233 }
Sonika Jindald8106362015-04-10 14:37:28 +053013234
Matt Roperc59cb172014-12-01 15:40:16 -080013235 ret = drm_plane_helper_check_update(plane, crtc, fb,
13236 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013237 min_scale,
13238 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013239 can_position, true,
13240 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013241 if (ret)
13242 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013243
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013244 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013245 struct intel_plane_state *old_state =
13246 to_intel_plane_state(plane->state);
13247
Matt Roper32b7eee2014-12-24 07:59:06 -080013248 intel_crtc->atomic.wait_for_flips = true;
13249
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013250 /*
13251 * FBC does not work on some platforms for rotated
13252 * planes, so disable it when rotation is not 0 and
13253 * update it when rotation is set back to 0.
13254 *
13255 * FIXME: This is redundant with the fbc update done in
13256 * the primary plane enable function except that that
13257 * one is done too late. We eventually need to unify
13258 * this.
13259 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013260 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013261 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013262 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013263 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013264 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013265 }
13266
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013267 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013268 /*
13269 * BDW signals flip done immediately if the plane
13270 * is disabled, even if the plane enable is already
13271 * armed to occur at the next vblank :(
13272 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013273 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013274 intel_crtc->atomic.wait_vblank = true;
13275 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013276
Matt Roper32b7eee2014-12-24 07:59:06 -080013277 intel_crtc->atomic.fb_bits |=
13278 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13279
13280 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013281
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013282 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013283 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013284 }
13285
Chandra Konduru6156a452015-04-27 13:48:39 -070013286 if (INTEL_INFO(dev)->gen >= 9) {
13287 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13288 to_intel_plane(plane), state, 0);
13289 if (ret)
13290 return ret;
13291 }
13292
Matt Roperc59cb172014-12-01 15:40:16 -080013293 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013294}
13295
Sonika Jindal48404c12014-08-22 14:06:04 +053013296static void
13297intel_commit_primary_plane(struct drm_plane *plane,
13298 struct intel_plane_state *state)
13299{
Matt Roper2b875c22014-12-01 15:40:13 -080013300 struct drm_crtc *crtc = state->base.crtc;
13301 struct drm_framebuffer *fb = state->base.fb;
13302 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013303 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013304 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013305 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013306
Matt Roperea2c67b2014-12-23 10:41:52 -080013307 crtc = crtc ? crtc : plane->crtc;
13308 intel_crtc = to_intel_crtc(crtc);
13309
Matt Ropercf4c7c12014-12-04 10:27:42 -080013310 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013311 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013312 crtc->y = src->y1 >> 16;
13313
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013314 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013315 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013316 /* FIXME: kill this fastboot hack */
13317 intel_update_pipe_size(intel_crtc);
13318
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013319 dev_priv->display.update_primary_plane(crtc, plane->fb,
13320 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013321 }
13322}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013323
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013324static void
13325intel_disable_primary_plane(struct drm_plane *plane,
13326 struct drm_crtc *crtc,
13327 bool force)
13328{
13329 struct drm_device *dev = plane->dev;
13330 struct drm_i915_private *dev_priv = dev->dev_private;
13331
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013332 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13333}
13334
Matt Roper32b7eee2014-12-24 07:59:06 -080013335static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13336{
13337 struct drm_device *dev = crtc->dev;
13338 struct drm_i915_private *dev_priv = dev->dev_private;
13339 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013340 struct intel_plane *intel_plane;
13341 struct drm_plane *p;
13342 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013343
Matt Roperea2c67b2014-12-23 10:41:52 -080013344 /* Track fb's for any planes being disabled */
13345 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13346 intel_plane = to_intel_plane(p);
13347
13348 if (intel_crtc->atomic.disabled_planes &
13349 (1 << drm_plane_index(p))) {
13350 switch (p->type) {
13351 case DRM_PLANE_TYPE_PRIMARY:
13352 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13353 break;
13354 case DRM_PLANE_TYPE_CURSOR:
13355 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13356 break;
13357 case DRM_PLANE_TYPE_OVERLAY:
13358 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13359 break;
13360 }
13361
13362 mutex_lock(&dev->struct_mutex);
13363 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13364 mutex_unlock(&dev->struct_mutex);
13365 }
13366 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013367
Matt Roper32b7eee2014-12-24 07:59:06 -080013368 if (intel_crtc->atomic.wait_for_flips)
13369 intel_crtc_wait_for_pending_flips(crtc);
13370
13371 if (intel_crtc->atomic.disable_fbc)
13372 intel_fbc_disable(dev);
13373
13374 if (intel_crtc->atomic.pre_disable_primary)
13375 intel_pre_disable_primary(crtc);
13376
13377 if (intel_crtc->atomic.update_wm)
13378 intel_update_watermarks(crtc);
13379
13380 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013381
13382 /* Perform vblank evasion around commit operation */
13383 if (intel_crtc->active)
13384 intel_crtc->atomic.evade =
13385 intel_pipe_update_start(intel_crtc,
13386 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013387}
13388
13389static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13390{
13391 struct drm_device *dev = crtc->dev;
13392 struct drm_i915_private *dev_priv = dev->dev_private;
13393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13394 struct drm_plane *p;
13395
Matt Roperc34c9ee2014-12-23 10:41:50 -080013396 if (intel_crtc->atomic.evade)
13397 intel_pipe_update_end(intel_crtc,
13398 intel_crtc->atomic.start_vbl_count);
13399
Matt Roper32b7eee2014-12-24 07:59:06 -080013400 intel_runtime_pm_put(dev_priv);
13401
13402 if (intel_crtc->atomic.wait_vblank)
13403 intel_wait_for_vblank(dev, intel_crtc->pipe);
13404
13405 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13406
13407 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013408 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013409 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013410 mutex_unlock(&dev->struct_mutex);
13411 }
Matt Roper465c1202014-05-29 08:06:54 -070013412
Matt Roper32b7eee2014-12-24 07:59:06 -080013413 if (intel_crtc->atomic.post_enable_primary)
13414 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013415
Matt Roper32b7eee2014-12-24 07:59:06 -080013416 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13417 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13418 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13419 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013420
Matt Roper32b7eee2014-12-24 07:59:06 -080013421 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013422}
13423
Matt Ropercf4c7c12014-12-04 10:27:42 -080013424/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013425 * intel_plane_destroy - destroy a plane
13426 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013427 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013428 * Common destruction function for all types of planes (primary, cursor,
13429 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013430 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013431void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013432{
13433 struct intel_plane *intel_plane = to_intel_plane(plane);
13434 drm_plane_cleanup(plane);
13435 kfree(intel_plane);
13436}
13437
Matt Roper65a3fea2015-01-21 16:35:42 -080013438const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013439 .update_plane = drm_atomic_helper_update_plane,
13440 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013441 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013442 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013443 .atomic_get_property = intel_plane_atomic_get_property,
13444 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013445 .atomic_duplicate_state = intel_plane_duplicate_state,
13446 .atomic_destroy_state = intel_plane_destroy_state,
13447
Matt Roper465c1202014-05-29 08:06:54 -070013448};
13449
13450static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13451 int pipe)
13452{
13453 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013454 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013455 const uint32_t *intel_primary_formats;
13456 int num_formats;
13457
13458 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13459 if (primary == NULL)
13460 return NULL;
13461
Matt Roper8e7d6882015-01-21 16:35:41 -080013462 state = intel_create_plane_state(&primary->base);
13463 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013464 kfree(primary);
13465 return NULL;
13466 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013467 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013468
Matt Roper465c1202014-05-29 08:06:54 -070013469 primary->can_scale = false;
13470 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013471 if (INTEL_INFO(dev)->gen >= 9) {
13472 primary->can_scale = true;
13473 }
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013474 state->scaler_id = -1;
Matt Roper465c1202014-05-29 08:06:54 -070013475 primary->pipe = pipe;
13476 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013477 primary->check_plane = intel_check_primary_plane;
13478 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013479 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013480 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013481 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13482 primary->plane = !pipe;
13483
13484 if (INTEL_INFO(dev)->gen <= 3) {
13485 intel_primary_formats = intel_primary_formats_gen2;
13486 num_formats = ARRAY_SIZE(intel_primary_formats_gen2);
13487 } else {
13488 intel_primary_formats = intel_primary_formats_gen4;
13489 num_formats = ARRAY_SIZE(intel_primary_formats_gen4);
13490 }
13491
13492 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013493 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013494 intel_primary_formats, num_formats,
13495 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013496
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013497 if (INTEL_INFO(dev)->gen >= 4)
13498 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013499
Matt Roperea2c67b2014-12-23 10:41:52 -080013500 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13501
Matt Roper465c1202014-05-29 08:06:54 -070013502 return &primary->base;
13503}
13504
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013505void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13506{
13507 if (!dev->mode_config.rotation_property) {
13508 unsigned long flags = BIT(DRM_ROTATE_0) |
13509 BIT(DRM_ROTATE_180);
13510
13511 if (INTEL_INFO(dev)->gen >= 9)
13512 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13513
13514 dev->mode_config.rotation_property =
13515 drm_mode_create_rotation_property(dev, flags);
13516 }
13517 if (dev->mode_config.rotation_property)
13518 drm_object_attach_property(&plane->base.base,
13519 dev->mode_config.rotation_property,
13520 plane->base.state->rotation);
13521}
13522
Matt Roper3d7d6512014-06-10 08:28:13 -070013523static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013524intel_check_cursor_plane(struct drm_plane *plane,
13525 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013526{
Matt Roper2b875c22014-12-01 15:40:13 -080013527 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013528 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013529 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013530 struct drm_rect *dest = &state->dst;
13531 struct drm_rect *src = &state->src;
13532 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013533 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013534 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013535 unsigned stride;
13536 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013537
Matt Roperea2c67b2014-12-23 10:41:52 -080013538 crtc = crtc ? crtc : plane->crtc;
13539 intel_crtc = to_intel_crtc(crtc);
13540
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013541 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013542 src, dest, clip,
13543 DRM_PLANE_HELPER_NO_SCALING,
13544 DRM_PLANE_HELPER_NO_SCALING,
13545 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013546 if (ret)
13547 return ret;
13548
13549
13550 /* if we want to turn off the cursor ignore width and height */
13551 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013552 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013553
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013554 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013555 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13556 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13557 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013558 return -EINVAL;
13559 }
13560
Matt Roperea2c67b2014-12-23 10:41:52 -080013561 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13562 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013563 DRM_DEBUG_KMS("buffer is too small\n");
13564 return -ENOMEM;
13565 }
13566
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013567 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013568 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13569 ret = -EINVAL;
13570 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013571
Matt Roper32b7eee2014-12-24 07:59:06 -080013572finish:
13573 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013574 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013575 intel_crtc->atomic.update_wm = true;
13576
13577 intel_crtc->atomic.fb_bits |=
13578 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13579 }
13580
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013581 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013582}
13583
Matt Roperf4a2cf22014-12-01 15:40:12 -080013584static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013585intel_disable_cursor_plane(struct drm_plane *plane,
13586 struct drm_crtc *crtc,
13587 bool force)
13588{
13589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13590
13591 if (!force) {
13592 plane->fb = NULL;
13593 intel_crtc->cursor_bo = NULL;
13594 intel_crtc->cursor_addr = 0;
13595 }
13596
13597 intel_crtc_update_cursor(crtc, false);
13598}
13599
13600static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013601intel_commit_cursor_plane(struct drm_plane *plane,
13602 struct intel_plane_state *state)
13603{
Matt Roper2b875c22014-12-01 15:40:13 -080013604 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013605 struct drm_device *dev = plane->dev;
13606 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013607 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013608 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013609
Matt Roperea2c67b2014-12-23 10:41:52 -080013610 crtc = crtc ? crtc : plane->crtc;
13611 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013612
Matt Roperea2c67b2014-12-23 10:41:52 -080013613 plane->fb = state->base.fb;
13614 crtc->cursor_x = state->base.crtc_x;
13615 crtc->cursor_y = state->base.crtc_y;
13616
Gustavo Padovana912f122014-12-01 15:40:10 -080013617 if (intel_crtc->cursor_bo == obj)
13618 goto update;
13619
Matt Roperf4a2cf22014-12-01 15:40:12 -080013620 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013621 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013622 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013623 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013624 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013625 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013626
Gustavo Padovana912f122014-12-01 15:40:10 -080013627 intel_crtc->cursor_addr = addr;
13628 intel_crtc->cursor_bo = obj;
13629update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013630
Matt Roper32b7eee2014-12-24 07:59:06 -080013631 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013632 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013633}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013634
Matt Roper3d7d6512014-06-10 08:28:13 -070013635static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13636 int pipe)
13637{
13638 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013639 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013640
13641 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13642 if (cursor == NULL)
13643 return NULL;
13644
Matt Roper8e7d6882015-01-21 16:35:41 -080013645 state = intel_create_plane_state(&cursor->base);
13646 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013647 kfree(cursor);
13648 return NULL;
13649 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013650 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013651
Matt Roper3d7d6512014-06-10 08:28:13 -070013652 cursor->can_scale = false;
13653 cursor->max_downscale = 1;
13654 cursor->pipe = pipe;
13655 cursor->plane = pipe;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013656 state->scaler_id = -1;
Matt Roperc59cb172014-12-01 15:40:16 -080013657 cursor->check_plane = intel_check_cursor_plane;
13658 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013659 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013660
13661 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013662 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013663 intel_cursor_formats,
13664 ARRAY_SIZE(intel_cursor_formats),
13665 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013666
13667 if (INTEL_INFO(dev)->gen >= 4) {
13668 if (!dev->mode_config.rotation_property)
13669 dev->mode_config.rotation_property =
13670 drm_mode_create_rotation_property(dev,
13671 BIT(DRM_ROTATE_0) |
13672 BIT(DRM_ROTATE_180));
13673 if (dev->mode_config.rotation_property)
13674 drm_object_attach_property(&cursor->base.base,
13675 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013676 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013677 }
13678
Matt Roperea2c67b2014-12-23 10:41:52 -080013679 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13680
Matt Roper3d7d6512014-06-10 08:28:13 -070013681 return &cursor->base;
13682}
13683
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013684static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13685 struct intel_crtc_state *crtc_state)
13686{
13687 int i;
13688 struct intel_scaler *intel_scaler;
13689 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13690
13691 for (i = 0; i < intel_crtc->num_scalers; i++) {
13692 intel_scaler = &scaler_state->scalers[i];
13693 intel_scaler->in_use = 0;
13694 intel_scaler->id = i;
13695
13696 intel_scaler->mode = PS_SCALER_MODE_DYN;
13697 }
13698
13699 scaler_state->scaler_id = -1;
13700}
13701
Hannes Ederb358d0a2008-12-18 21:18:47 +010013702static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013703{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013704 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013705 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013706 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013707 struct drm_plane *primary = NULL;
13708 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013709 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013710
Daniel Vetter955382f2013-09-19 14:05:45 +020013711 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013712 if (intel_crtc == NULL)
13713 return;
13714
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013715 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13716 if (!crtc_state)
13717 goto fail;
13718 intel_crtc_set_state(intel_crtc, crtc_state);
Matt Roper07878242015-02-25 11:43:26 -080013719 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013720
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013721 /* initialize shared scalers */
13722 if (INTEL_INFO(dev)->gen >= 9) {
13723 if (pipe == PIPE_C)
13724 intel_crtc->num_scalers = 1;
13725 else
13726 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13727
13728 skl_init_scalers(dev, intel_crtc, crtc_state);
13729 }
13730
Matt Roper465c1202014-05-29 08:06:54 -070013731 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013732 if (!primary)
13733 goto fail;
13734
13735 cursor = intel_cursor_plane_create(dev, pipe);
13736 if (!cursor)
13737 goto fail;
13738
Matt Roper465c1202014-05-29 08:06:54 -070013739 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013740 cursor, &intel_crtc_funcs);
13741 if (ret)
13742 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013743
13744 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013745 for (i = 0; i < 256; i++) {
13746 intel_crtc->lut_r[i] = i;
13747 intel_crtc->lut_g[i] = i;
13748 intel_crtc->lut_b[i] = i;
13749 }
13750
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013751 /*
13752 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013753 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013754 */
Jesse Barnes80824002009-09-10 15:28:06 -070013755 intel_crtc->pipe = pipe;
13756 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013757 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013758 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013759 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013760 }
13761
Chris Wilson4b0e3332014-05-30 16:35:26 +030013762 intel_crtc->cursor_base = ~0;
13763 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013764 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013765
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013766 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13767 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13768 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13769 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13770
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020013771 INIT_WORK(&intel_crtc->mmio_flip.work, intel_mmio_flip_work_func);
13772
Jesse Barnes79e53942008-11-07 14:24:08 -080013773 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013774
13775 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013776 return;
13777
13778fail:
13779 if (primary)
13780 drm_plane_cleanup(primary);
13781 if (cursor)
13782 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013783 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013784 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013785}
13786
Jesse Barnes752aa882013-10-31 18:55:49 +020013787enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13788{
13789 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013790 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013791
Rob Clark51fd3712013-11-19 12:10:12 -050013792 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013793
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013794 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013795 return INVALID_PIPE;
13796
13797 return to_intel_crtc(encoder->crtc)->pipe;
13798}
13799
Carl Worth08d7b3d2009-04-29 14:43:54 -070013800int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013801 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013802{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013803 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013804 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013805 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013806
Rob Clark7707e652014-07-17 23:30:04 -040013807 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013808
Rob Clark7707e652014-07-17 23:30:04 -040013809 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013810 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013811 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013812 }
13813
Rob Clark7707e652014-07-17 23:30:04 -040013814 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013815 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013816
Daniel Vetterc05422d2009-08-11 16:05:30 +020013817 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013818}
13819
Daniel Vetter66a92782012-07-12 20:08:18 +020013820static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013821{
Daniel Vetter66a92782012-07-12 20:08:18 +020013822 struct drm_device *dev = encoder->base.dev;
13823 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013824 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013825 int entry = 0;
13826
Damien Lespiaub2784e12014-08-05 11:29:37 +010013827 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013828 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013829 index_mask |= (1 << entry);
13830
Jesse Barnes79e53942008-11-07 14:24:08 -080013831 entry++;
13832 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013833
Jesse Barnes79e53942008-11-07 14:24:08 -080013834 return index_mask;
13835}
13836
Chris Wilson4d302442010-12-14 19:21:29 +000013837static bool has_edp_a(struct drm_device *dev)
13838{
13839 struct drm_i915_private *dev_priv = dev->dev_private;
13840
13841 if (!IS_MOBILE(dev))
13842 return false;
13843
13844 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13845 return false;
13846
Damien Lespiaue3589902014-02-07 19:12:50 +000013847 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013848 return false;
13849
13850 return true;
13851}
13852
Jesse Barnes84b4e042014-06-25 08:24:29 -070013853static bool intel_crt_present(struct drm_device *dev)
13854{
13855 struct drm_i915_private *dev_priv = dev->dev_private;
13856
Damien Lespiau884497e2013-12-03 13:56:23 +000013857 if (INTEL_INFO(dev)->gen >= 9)
13858 return false;
13859
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013860 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013861 return false;
13862
13863 if (IS_CHERRYVIEW(dev))
13864 return false;
13865
13866 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13867 return false;
13868
13869 return true;
13870}
13871
Jesse Barnes79e53942008-11-07 14:24:08 -080013872static void intel_setup_outputs(struct drm_device *dev)
13873{
Eric Anholt725e30a2009-01-22 13:01:02 -080013874 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013875 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013876 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013877
Daniel Vetterc9093352013-06-06 22:22:47 +020013878 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013879
Jesse Barnes84b4e042014-06-25 08:24:29 -070013880 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013881 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013882
Vandana Kannanc776eb22014-08-19 12:05:01 +053013883 if (IS_BROXTON(dev)) {
13884 /*
13885 * FIXME: Broxton doesn't support port detection via the
13886 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13887 * detect the ports.
13888 */
13889 intel_ddi_init(dev, PORT_A);
13890 intel_ddi_init(dev, PORT_B);
13891 intel_ddi_init(dev, PORT_C);
13892 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013893 int found;
13894
Jesse Barnesde31fac2015-03-06 15:53:32 -080013895 /*
13896 * Haswell uses DDI functions to detect digital outputs.
13897 * On SKL pre-D0 the strap isn't connected, so we assume
13898 * it's there.
13899 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013900 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013901 /* WaIgnoreDDIAStrap: skl */
13902 if (found ||
13903 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013904 intel_ddi_init(dev, PORT_A);
13905
13906 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13907 * register */
13908 found = I915_READ(SFUSE_STRAP);
13909
13910 if (found & SFUSE_STRAP_DDIB_DETECTED)
13911 intel_ddi_init(dev, PORT_B);
13912 if (found & SFUSE_STRAP_DDIC_DETECTED)
13913 intel_ddi_init(dev, PORT_C);
13914 if (found & SFUSE_STRAP_DDID_DETECTED)
13915 intel_ddi_init(dev, PORT_D);
13916 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013917 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013918 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013919
13920 if (has_edp_a(dev))
13921 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013922
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013923 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013924 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013925 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013926 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013927 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013928 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013929 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013930 }
13931
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013932 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013933 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013934
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013935 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013936 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013937
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013938 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013939 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013940
Daniel Vetter270b3042012-10-27 15:52:05 +020013941 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013942 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013943 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013944 /*
13945 * The DP_DETECTED bit is the latched state of the DDC
13946 * SDA pin at boot. However since eDP doesn't require DDC
13947 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13948 * eDP ports may have been muxed to an alternate function.
13949 * Thus we can't rely on the DP_DETECTED bit alone to detect
13950 * eDP ports. Consult the VBT as well as DP_DETECTED to
13951 * detect eDP ports.
13952 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013953 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13954 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013955 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13956 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013957 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
13958 intel_dp_is_edp(dev, PORT_B))
13959 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013960
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013961 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
13962 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070013963 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
13964 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013965 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
13966 intel_dp_is_edp(dev, PORT_C))
13967 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053013968
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013969 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013970 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013971 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
13972 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013973 /* eDP not supported on port D, so don't check VBT */
13974 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
13975 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030013976 }
13977
Jani Nikula3cfca972013-08-27 15:12:26 +030013978 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080013979 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080013980 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080013981
Paulo Zanonie2debe92013-02-18 19:00:27 -030013982 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013983 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013984 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013985 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
13986 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013987 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013988 }
Ma Ling27185ae2009-08-24 13:50:23 +080013989
Imre Deake7281ea2013-05-08 13:14:08 +030013990 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013991 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080013992 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013993
13994 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040013995
Paulo Zanonie2debe92013-02-18 19:00:27 -030013996 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013997 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030013998 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080013999 }
Ma Ling27185ae2009-08-24 13:50:23 +080014000
Paulo Zanonie2debe92013-02-18 19:00:27 -030014001 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014002
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014003 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14004 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014005 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014006 }
Imre Deake7281ea2013-05-08 13:14:08 +030014007 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014008 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014009 }
Ma Ling27185ae2009-08-24 13:50:23 +080014010
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014011 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014012 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014013 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014014 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014015 intel_dvo_init(dev);
14016
Zhenyu Wang103a1962009-11-27 11:44:36 +080014017 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014018 intel_tv_init(dev);
14019
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014020 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014021
Damien Lespiaub2784e12014-08-05 11:29:37 +010014022 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014023 encoder->base.possible_crtcs = encoder->crtc_mask;
14024 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014025 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014026 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014027
Paulo Zanonidde86e22012-12-01 12:04:25 -020014028 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014029
14030 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014031}
14032
14033static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14034{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014035 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014036 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014037
Daniel Vetteref2d6332014-02-10 18:00:38 +010014038 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014039 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014040 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014041 drm_gem_object_unreference(&intel_fb->obj->base);
14042 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014043 kfree(intel_fb);
14044}
14045
14046static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014047 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014048 unsigned int *handle)
14049{
14050 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014051 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014052
Chris Wilson05394f32010-11-08 19:18:58 +000014053 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014054}
14055
14056static const struct drm_framebuffer_funcs intel_fb_funcs = {
14057 .destroy = intel_user_framebuffer_destroy,
14058 .create_handle = intel_user_framebuffer_create_handle,
14059};
14060
Damien Lespiaub3218032015-02-27 11:15:18 +000014061static
14062u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14063 uint32_t pixel_format)
14064{
14065 u32 gen = INTEL_INFO(dev)->gen;
14066
14067 if (gen >= 9) {
14068 /* "The stride in bytes must not exceed the of the size of 8K
14069 * pixels and 32K bytes."
14070 */
14071 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14072 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14073 return 32*1024;
14074 } else if (gen >= 4) {
14075 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14076 return 16*1024;
14077 else
14078 return 32*1024;
14079 } else if (gen >= 3) {
14080 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14081 return 8*1024;
14082 else
14083 return 16*1024;
14084 } else {
14085 /* XXX DSPC is limited to 4k tiled */
14086 return 8*1024;
14087 }
14088}
14089
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014090static int intel_framebuffer_init(struct drm_device *dev,
14091 struct intel_framebuffer *intel_fb,
14092 struct drm_mode_fb_cmd2 *mode_cmd,
14093 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014094{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014095 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014096 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014097 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014098
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014099 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14100
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014101 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14102 /* Enforce that fb modifier and tiling mode match, but only for
14103 * X-tiled. This is needed for FBC. */
14104 if (!!(obj->tiling_mode == I915_TILING_X) !=
14105 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14106 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14107 return -EINVAL;
14108 }
14109 } else {
14110 if (obj->tiling_mode == I915_TILING_X)
14111 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14112 else if (obj->tiling_mode == I915_TILING_Y) {
14113 DRM_DEBUG("No Y tiling for legacy addfb\n");
14114 return -EINVAL;
14115 }
14116 }
14117
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014118 /* Passed in modifier sanity checking. */
14119 switch (mode_cmd->modifier[0]) {
14120 case I915_FORMAT_MOD_Y_TILED:
14121 case I915_FORMAT_MOD_Yf_TILED:
14122 if (INTEL_INFO(dev)->gen < 9) {
14123 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14124 mode_cmd->modifier[0]);
14125 return -EINVAL;
14126 }
14127 case DRM_FORMAT_MOD_NONE:
14128 case I915_FORMAT_MOD_X_TILED:
14129 break;
14130 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014131 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14132 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014133 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014134 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014135
Damien Lespiaub3218032015-02-27 11:15:18 +000014136 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14137 mode_cmd->pixel_format);
14138 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14139 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14140 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014141 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014142 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014143
Damien Lespiaub3218032015-02-27 11:15:18 +000014144 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14145 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014146 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014147 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14148 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014149 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014150 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014151 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014152 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014153
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014154 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014155 mode_cmd->pitches[0] != obj->stride) {
14156 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14157 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014158 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014159 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014160
Ville Syrjälä57779d02012-10-31 17:50:14 +020014161 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014162 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014163 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014164 case DRM_FORMAT_RGB565:
14165 case DRM_FORMAT_XRGB8888:
14166 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014167 break;
14168 case DRM_FORMAT_XRGB1555:
14169 case DRM_FORMAT_ARGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014170 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014171 DRM_DEBUG("unsupported pixel format: %s\n",
14172 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014173 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014174 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014175 break;
14176 case DRM_FORMAT_XBGR8888:
14177 case DRM_FORMAT_ABGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014178 case DRM_FORMAT_XRGB2101010:
14179 case DRM_FORMAT_ARGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014180 case DRM_FORMAT_XBGR2101010:
14181 case DRM_FORMAT_ABGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014182 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014183 DRM_DEBUG("unsupported pixel format: %s\n",
14184 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014185 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014186 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014187 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014188 case DRM_FORMAT_YUYV:
14189 case DRM_FORMAT_UYVY:
14190 case DRM_FORMAT_YVYU:
14191 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014192 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014193 DRM_DEBUG("unsupported pixel format: %s\n",
14194 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014195 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014196 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014197 break;
14198 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014199 DRM_DEBUG("unsupported pixel format: %s\n",
14200 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014201 return -EINVAL;
14202 }
14203
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014204 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14205 if (mode_cmd->offsets[0] != 0)
14206 return -EINVAL;
14207
Damien Lespiauec2c9812015-01-20 12:51:45 +000014208 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014209 mode_cmd->pixel_format,
14210 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014211 /* FIXME drm helper for size checks (especially planar formats)? */
14212 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14213 return -EINVAL;
14214
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014215 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14216 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014217 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014218
Jesse Barnes79e53942008-11-07 14:24:08 -080014219 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14220 if (ret) {
14221 DRM_ERROR("framebuffer init failed %d\n", ret);
14222 return ret;
14223 }
14224
Jesse Barnes79e53942008-11-07 14:24:08 -080014225 return 0;
14226}
14227
Jesse Barnes79e53942008-11-07 14:24:08 -080014228static struct drm_framebuffer *
14229intel_user_framebuffer_create(struct drm_device *dev,
14230 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014231 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014232{
Chris Wilson05394f32010-11-08 19:18:58 +000014233 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014234
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014235 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14236 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014237 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014238 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014239
Chris Wilsond2dff872011-04-19 08:36:26 +010014240 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014241}
14242
Daniel Vetter4520f532013-10-09 09:18:51 +020014243#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014244static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014245{
14246}
14247#endif
14248
Jesse Barnes79e53942008-11-07 14:24:08 -080014249static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014250 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014251 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014252 .atomic_check = intel_atomic_check,
14253 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014254};
14255
Jesse Barnese70236a2009-09-21 10:42:27 -070014256/* Set up chip specific display functions */
14257static void intel_init_display(struct drm_device *dev)
14258{
14259 struct drm_i915_private *dev_priv = dev->dev_private;
14260
Daniel Vetteree9300b2013-06-03 22:40:22 +020014261 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14262 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014263 else if (IS_CHERRYVIEW(dev))
14264 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014265 else if (IS_VALLEYVIEW(dev))
14266 dev_priv->display.find_dpll = vlv_find_best_dpll;
14267 else if (IS_PINEVIEW(dev))
14268 dev_priv->display.find_dpll = pnv_find_best_dpll;
14269 else
14270 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14271
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014272 if (INTEL_INFO(dev)->gen >= 9) {
14273 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014274 dev_priv->display.get_initial_plane_config =
14275 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014276 dev_priv->display.crtc_compute_clock =
14277 haswell_crtc_compute_clock;
14278 dev_priv->display.crtc_enable = haswell_crtc_enable;
14279 dev_priv->display.crtc_disable = haswell_crtc_disable;
14280 dev_priv->display.off = ironlake_crtc_off;
14281 dev_priv->display.update_primary_plane =
14282 skylake_update_primary_plane;
14283 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014284 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014285 dev_priv->display.get_initial_plane_config =
14286 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014287 dev_priv->display.crtc_compute_clock =
14288 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014289 dev_priv->display.crtc_enable = haswell_crtc_enable;
14290 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014291 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014292 dev_priv->display.update_primary_plane =
14293 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014294 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014295 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014296 dev_priv->display.get_initial_plane_config =
14297 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014298 dev_priv->display.crtc_compute_clock =
14299 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014300 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14301 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014302 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014303 dev_priv->display.update_primary_plane =
14304 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014305 } else if (IS_VALLEYVIEW(dev)) {
14306 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014307 dev_priv->display.get_initial_plane_config =
14308 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014309 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014310 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14311 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14312 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014313 dev_priv->display.update_primary_plane =
14314 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014315 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014316 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014317 dev_priv->display.get_initial_plane_config =
14318 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014319 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014320 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14321 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014322 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014323 dev_priv->display.update_primary_plane =
14324 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014325 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014326
Jesse Barnese70236a2009-09-21 10:42:27 -070014327 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014328 if (IS_SKYLAKE(dev))
14329 dev_priv->display.get_display_clock_speed =
14330 skylake_get_display_clock_speed;
14331 else if (IS_BROADWELL(dev))
14332 dev_priv->display.get_display_clock_speed =
14333 broadwell_get_display_clock_speed;
14334 else if (IS_HASWELL(dev))
14335 dev_priv->display.get_display_clock_speed =
14336 haswell_get_display_clock_speed;
14337 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014338 dev_priv->display.get_display_clock_speed =
14339 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014340 else if (IS_GEN5(dev))
14341 dev_priv->display.get_display_clock_speed =
14342 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014343 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14344 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014345 dev_priv->display.get_display_clock_speed =
14346 i945_get_display_clock_speed;
14347 else if (IS_I915G(dev))
14348 dev_priv->display.get_display_clock_speed =
14349 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014350 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014351 dev_priv->display.get_display_clock_speed =
14352 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014353 else if (IS_PINEVIEW(dev))
14354 dev_priv->display.get_display_clock_speed =
14355 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014356 else if (IS_I915GM(dev))
14357 dev_priv->display.get_display_clock_speed =
14358 i915gm_get_display_clock_speed;
14359 else if (IS_I865G(dev))
14360 dev_priv->display.get_display_clock_speed =
14361 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014362 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014363 dev_priv->display.get_display_clock_speed =
14364 i855_get_display_clock_speed;
14365 else /* 852, 830 */
14366 dev_priv->display.get_display_clock_speed =
14367 i830_get_display_clock_speed;
14368
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014369 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014370 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014371 } else if (IS_GEN6(dev)) {
14372 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014373 } else if (IS_IVYBRIDGE(dev)) {
14374 /* FIXME: detect B0+ stepping and use auto training */
14375 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014376 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014377 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014378 } else if (IS_VALLEYVIEW(dev)) {
14379 dev_priv->display.modeset_global_resources =
14380 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014381 } else if (IS_BROXTON(dev)) {
14382 dev_priv->display.modeset_global_resources =
14383 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014384 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014385
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014386 switch (INTEL_INFO(dev)->gen) {
14387 case 2:
14388 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14389 break;
14390
14391 case 3:
14392 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14393 break;
14394
14395 case 4:
14396 case 5:
14397 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14398 break;
14399
14400 case 6:
14401 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14402 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014403 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014404 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014405 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14406 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014407 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014408 /* Drop through - unsupported since execlist only. */
14409 default:
14410 /* Default just returns -ENODEV to indicate unsupported */
14411 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014412 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014413
14414 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014415
14416 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014417}
14418
Jesse Barnesb690e962010-07-19 13:53:12 -070014419/*
14420 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14421 * resume, or other times. This quirk makes sure that's the case for
14422 * affected systems.
14423 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014424static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014425{
14426 struct drm_i915_private *dev_priv = dev->dev_private;
14427
14428 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014429 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014430}
14431
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014432static void quirk_pipeb_force(struct drm_device *dev)
14433{
14434 struct drm_i915_private *dev_priv = dev->dev_private;
14435
14436 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14437 DRM_INFO("applying pipe b force quirk\n");
14438}
14439
Keith Packard435793d2011-07-12 14:56:22 -070014440/*
14441 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14442 */
14443static void quirk_ssc_force_disable(struct drm_device *dev)
14444{
14445 struct drm_i915_private *dev_priv = dev->dev_private;
14446 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014447 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014448}
14449
Carsten Emde4dca20e2012-03-15 15:56:26 +010014450/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014451 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14452 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014453 */
14454static void quirk_invert_brightness(struct drm_device *dev)
14455{
14456 struct drm_i915_private *dev_priv = dev->dev_private;
14457 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014458 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014459}
14460
Scot Doyle9c72cc62014-07-03 23:27:50 +000014461/* Some VBT's incorrectly indicate no backlight is present */
14462static void quirk_backlight_present(struct drm_device *dev)
14463{
14464 struct drm_i915_private *dev_priv = dev->dev_private;
14465 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14466 DRM_INFO("applying backlight present quirk\n");
14467}
14468
Jesse Barnesb690e962010-07-19 13:53:12 -070014469struct intel_quirk {
14470 int device;
14471 int subsystem_vendor;
14472 int subsystem_device;
14473 void (*hook)(struct drm_device *dev);
14474};
14475
Egbert Eich5f85f172012-10-14 15:46:38 +020014476/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14477struct intel_dmi_quirk {
14478 void (*hook)(struct drm_device *dev);
14479 const struct dmi_system_id (*dmi_id_list)[];
14480};
14481
14482static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14483{
14484 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14485 return 1;
14486}
14487
14488static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14489 {
14490 .dmi_id_list = &(const struct dmi_system_id[]) {
14491 {
14492 .callback = intel_dmi_reverse_brightness,
14493 .ident = "NCR Corporation",
14494 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14495 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14496 },
14497 },
14498 { } /* terminating entry */
14499 },
14500 .hook = quirk_invert_brightness,
14501 },
14502};
14503
Ben Widawskyc43b5632012-04-16 14:07:40 -070014504static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014505 /* HP Mini needs pipe A force quirk (LP: #322104) */
Akshay Joshi0206e352011-08-16 15:34:10 -040014506 { 0x27ae, 0x103c, 0x361a, quirk_pipea_force },
Jesse Barnesb690e962010-07-19 13:53:12 -070014507
Jesse Barnesb690e962010-07-19 13:53:12 -070014508 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14509 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14510
Jesse Barnesb690e962010-07-19 13:53:12 -070014511 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14512 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14513
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014514 /* 830 needs to leave pipe A & dpll A up */
14515 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14516
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014517 /* 830 needs to leave pipe B & dpll B up */
14518 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14519
Keith Packard435793d2011-07-12 14:56:22 -070014520 /* Lenovo U160 cannot use SSC on LVDS */
14521 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014522
14523 /* Sony Vaio Y cannot use SSC on LVDS */
14524 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014525
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014526 /* Acer Aspire 5734Z must invert backlight brightness */
14527 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14528
14529 /* Acer/eMachines G725 */
14530 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14531
14532 /* Acer/eMachines e725 */
14533 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14534
14535 /* Acer/Packard Bell NCL20 */
14536 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14537
14538 /* Acer Aspire 4736Z */
14539 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014540
14541 /* Acer Aspire 5336 */
14542 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014543
14544 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14545 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014546
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014547 /* Acer C720 Chromebook (Core i3 4005U) */
14548 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14549
jens steinb2a96012014-10-28 20:25:53 +010014550 /* Apple Macbook 2,1 (Core 2 T7400) */
14551 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14552
Scot Doyled4967d82014-07-03 23:27:52 +000014553 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14554 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014555
14556 /* HP Chromebook 14 (Celeron 2955U) */
14557 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014558
14559 /* Dell Chromebook 11 */
14560 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014561};
14562
14563static void intel_init_quirks(struct drm_device *dev)
14564{
14565 struct pci_dev *d = dev->pdev;
14566 int i;
14567
14568 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14569 struct intel_quirk *q = &intel_quirks[i];
14570
14571 if (d->device == q->device &&
14572 (d->subsystem_vendor == q->subsystem_vendor ||
14573 q->subsystem_vendor == PCI_ANY_ID) &&
14574 (d->subsystem_device == q->subsystem_device ||
14575 q->subsystem_device == PCI_ANY_ID))
14576 q->hook(dev);
14577 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014578 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14579 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14580 intel_dmi_quirks[i].hook(dev);
14581 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014582}
14583
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014584/* Disable the VGA plane that we never use */
14585static void i915_disable_vga(struct drm_device *dev)
14586{
14587 struct drm_i915_private *dev_priv = dev->dev_private;
14588 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014589 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014590
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014591 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014592 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014593 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014594 sr1 = inb(VGA_SR_DATA);
14595 outb(sr1 | 1<<5, VGA_SR_DATA);
14596 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14597 udelay(300);
14598
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014599 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014600 POSTING_READ(vga_reg);
14601}
14602
Daniel Vetterf8175862012-04-10 15:50:11 +020014603void intel_modeset_init_hw(struct drm_device *dev)
14604{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014605 intel_prepare_ddi(dev);
14606
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014607 if (IS_VALLEYVIEW(dev))
14608 vlv_update_cdclk(dev);
14609
Daniel Vetterf8175862012-04-10 15:50:11 +020014610 intel_init_clock_gating(dev);
14611
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014612 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014613}
14614
Jesse Barnes79e53942008-11-07 14:24:08 -080014615void intel_modeset_init(struct drm_device *dev)
14616{
Jesse Barnes652c3932009-08-17 13:31:43 -070014617 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014618 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014619 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014620 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014621
14622 drm_mode_config_init(dev);
14623
14624 dev->mode_config.min_width = 0;
14625 dev->mode_config.min_height = 0;
14626
Dave Airlie019d96c2011-09-29 16:20:42 +010014627 dev->mode_config.preferred_depth = 24;
14628 dev->mode_config.prefer_shadow = 1;
14629
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014630 dev->mode_config.allow_fb_modifiers = true;
14631
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014632 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014633
Jesse Barnesb690e962010-07-19 13:53:12 -070014634 intel_init_quirks(dev);
14635
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014636 intel_init_pm(dev);
14637
Ben Widawskye3c74752013-04-05 13:12:39 -070014638 if (INTEL_INFO(dev)->num_pipes == 0)
14639 return;
14640
Jesse Barnese70236a2009-09-21 10:42:27 -070014641 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014642 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014643
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014644 if (IS_GEN2(dev)) {
14645 dev->mode_config.max_width = 2048;
14646 dev->mode_config.max_height = 2048;
14647 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014648 dev->mode_config.max_width = 4096;
14649 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014650 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014651 dev->mode_config.max_width = 8192;
14652 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014653 }
Damien Lespiau068be562014-03-28 14:17:49 +000014654
Ville Syrjälädc41c152014-08-13 11:57:05 +030014655 if (IS_845G(dev) || IS_I865G(dev)) {
14656 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14657 dev->mode_config.cursor_height = 1023;
14658 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014659 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14660 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14661 } else {
14662 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14663 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14664 }
14665
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014666 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014667
Zhao Yakui28c97732009-10-09 11:39:41 +080014668 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014669 INTEL_INFO(dev)->num_pipes,
14670 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014671
Damien Lespiau055e3932014-08-18 13:49:10 +010014672 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014673 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014674 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014675 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014676 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014677 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014678 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014679 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014680 }
14681
Jesse Barnesf42bb702013-12-16 16:34:23 -080014682 intel_init_dpio(dev);
14683
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014684 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014685
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014686 /* Just disable it once at startup */
14687 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014688 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014689
14690 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014691 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014692
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014693 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014694 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014695 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014696
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014697 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014698 if (!crtc->active)
14699 continue;
14700
Jesse Barnes46f297f2014-03-07 08:57:48 -080014701 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014702 * Note that reserving the BIOS fb up front prevents us
14703 * from stuffing other stolen allocations like the ring
14704 * on top. This prevents some ugliness at boot time, and
14705 * can even allow for smooth boot transitions if the BIOS
14706 * fb is large enough for the active pipe configuration.
14707 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014708 if (dev_priv->display.get_initial_plane_config) {
14709 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014710 &crtc->plane_config);
14711 /*
14712 * If the fb is shared between multiple heads, we'll
14713 * just get the first one.
14714 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014715 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014716 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014717 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014718}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014719
Daniel Vetter7fad7982012-07-04 17:51:47 +020014720static void intel_enable_pipe_a(struct drm_device *dev)
14721{
14722 struct intel_connector *connector;
14723 struct drm_connector *crt = NULL;
14724 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014725 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014726
14727 /* We can't just switch on the pipe A, we need to set things up with a
14728 * proper mode and output configuration. As a gross hack, enable pipe A
14729 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014730 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014731 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14732 crt = &connector->base;
14733 break;
14734 }
14735 }
14736
14737 if (!crt)
14738 return;
14739
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014740 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014741 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014742}
14743
Daniel Vetterfa555832012-10-10 23:14:00 +020014744static bool
14745intel_check_plane_mapping(struct intel_crtc *crtc)
14746{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014747 struct drm_device *dev = crtc->base.dev;
14748 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014749 u32 reg, val;
14750
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014751 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014752 return true;
14753
14754 reg = DSPCNTR(!crtc->plane);
14755 val = I915_READ(reg);
14756
14757 if ((val & DISPLAY_PLANE_ENABLE) &&
14758 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14759 return false;
14760
14761 return true;
14762}
14763
Daniel Vetter24929352012-07-02 20:28:59 +020014764static void intel_sanitize_crtc(struct intel_crtc *crtc)
14765{
14766 struct drm_device *dev = crtc->base.dev;
14767 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014768 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014769
Daniel Vetter24929352012-07-02 20:28:59 +020014770 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014771 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014772 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14773
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014774 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014775 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014776 if (crtc->active) {
14777 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014778 drm_crtc_vblank_on(&crtc->base);
14779 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014780
Daniel Vetter24929352012-07-02 20:28:59 +020014781 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014782 * disable the crtc (and hence change the state) if it is wrong. Note
14783 * that gen4+ has a fixed plane -> pipe mapping. */
14784 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014785 struct intel_connector *connector;
14786 bool plane;
14787
Daniel Vetter24929352012-07-02 20:28:59 +020014788 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14789 crtc->base.base.id);
14790
14791 /* Pipe has the wrong plane attached and the plane is active.
14792 * Temporarily change the plane mapping and disable everything
14793 * ... */
14794 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014795 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014796 crtc->plane = !plane;
14797 dev_priv->display.crtc_disable(&crtc->base);
14798 crtc->plane = plane;
14799
14800 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014801 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014802 if (connector->encoder->base.crtc != &crtc->base)
14803 continue;
14804
Egbert Eich7f1950f2014-04-25 10:56:22 +020014805 connector->base.dpms = DRM_MODE_DPMS_OFF;
14806 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014807 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014808 /* multiple connectors may have the same encoder:
14809 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014810 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014811 if (connector->encoder->base.crtc == &crtc->base) {
14812 connector->encoder->base.crtc = NULL;
14813 connector->encoder->connectors_active = false;
14814 }
Daniel Vetter24929352012-07-02 20:28:59 +020014815
14816 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014817 crtc->base.state->enable = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014818 crtc->base.enabled = false;
14819 }
Daniel Vetter24929352012-07-02 20:28:59 +020014820
Daniel Vetter7fad7982012-07-04 17:51:47 +020014821 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14822 crtc->pipe == PIPE_A && !crtc->active) {
14823 /* BIOS forgot to enable pipe A, this mostly happens after
14824 * resume. Force-enable the pipe to fix this, the update_dpms
14825 * call below we restore the pipe to the right state, but leave
14826 * the required bits on. */
14827 intel_enable_pipe_a(dev);
14828 }
14829
Daniel Vetter24929352012-07-02 20:28:59 +020014830 /* Adjust the state of the output pipe according to whether we
14831 * have active connectors/encoders. */
14832 intel_crtc_update_dpms(&crtc->base);
14833
Matt Roper83d65732015-02-25 13:12:16 -080014834 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014835 struct intel_encoder *encoder;
14836
14837 /* This can happen either due to bugs in the get_hw_state
14838 * functions or because the pipe is force-enabled due to the
14839 * pipe A quirk. */
14840 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14841 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014842 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014843 crtc->active ? "enabled" : "disabled");
14844
Matt Roper83d65732015-02-25 13:12:16 -080014845 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014846 crtc->base.enabled = crtc->active;
14847
14848 /* Because we only establish the connector -> encoder ->
14849 * crtc links if something is active, this means the
14850 * crtc is now deactivated. Break the links. connector
14851 * -> encoder links are only establish when things are
14852 * actually up, hence no need to break them. */
14853 WARN_ON(crtc->active);
14854
14855 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14856 WARN_ON(encoder->connectors_active);
14857 encoder->base.crtc = NULL;
14858 }
14859 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014860
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014861 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014862 /*
14863 * We start out with underrun reporting disabled to avoid races.
14864 * For correct bookkeeping mark this on active crtcs.
14865 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014866 * Also on gmch platforms we dont have any hardware bits to
14867 * disable the underrun reporting. Which means we need to start
14868 * out with underrun reporting disabled also on inactive pipes,
14869 * since otherwise we'll complain about the garbage we read when
14870 * e.g. coming up after runtime pm.
14871 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014872 * No protection against concurrent access is required - at
14873 * worst a fifo underrun happens which also sets this to false.
14874 */
14875 crtc->cpu_fifo_underrun_disabled = true;
14876 crtc->pch_fifo_underrun_disabled = true;
14877 }
Daniel Vetter24929352012-07-02 20:28:59 +020014878}
14879
14880static void intel_sanitize_encoder(struct intel_encoder *encoder)
14881{
14882 struct intel_connector *connector;
14883 struct drm_device *dev = encoder->base.dev;
14884
14885 /* We need to check both for a crtc link (meaning that the
14886 * encoder is active and trying to read from a pipe) and the
14887 * pipe itself being active. */
14888 bool has_active_crtc = encoder->base.crtc &&
14889 to_intel_crtc(encoder->base.crtc)->active;
14890
14891 if (encoder->connectors_active && !has_active_crtc) {
14892 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14893 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014894 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014895
14896 /* Connector is active, but has no active pipe. This is
14897 * fallout from our resume register restoring. Disable
14898 * the encoder manually again. */
14899 if (encoder->base.crtc) {
14900 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14901 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014902 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014903 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014904 if (encoder->post_disable)
14905 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014906 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014907 encoder->base.crtc = NULL;
14908 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014909
14910 /* Inconsistent output/port/pipe state happens presumably due to
14911 * a bug in one of the get_hw_state functions. Or someplace else
14912 * in our code, like the register restore mess on resume. Clamp
14913 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014914 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014915 if (connector->encoder != encoder)
14916 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014917 connector->base.dpms = DRM_MODE_DPMS_OFF;
14918 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014919 }
14920 }
14921 /* Enabled encoders without active connectors will be fixed in
14922 * the crtc fixup. */
14923}
14924
Imre Deak04098752014-02-18 00:02:16 +020014925void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014926{
14927 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014928 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014929
Imre Deak04098752014-02-18 00:02:16 +020014930 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14931 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14932 i915_disable_vga(dev);
14933 }
14934}
14935
14936void i915_redisable_vga(struct drm_device *dev)
14937{
14938 struct drm_i915_private *dev_priv = dev->dev_private;
14939
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014940 /* This function can be called both from intel_modeset_setup_hw_state or
14941 * at a very early point in our resume sequence, where the power well
14942 * structures are not yet restored. Since this function is at a very
14943 * paranoid "someone might have enabled VGA while we were not looking"
14944 * level, just check if the power well is enabled instead of trying to
14945 * follow the "don't touch the power well if we don't need it" policy
14946 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014947 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014948 return;
14949
Imre Deak04098752014-02-18 00:02:16 +020014950 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014951}
14952
Ville Syrjälä98ec7732014-04-30 17:43:01 +030014953static bool primary_get_hw_state(struct intel_crtc *crtc)
14954{
14955 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
14956
14957 if (!crtc->active)
14958 return false;
14959
14960 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
14961}
14962
Daniel Vetter30e984d2013-06-05 13:34:17 +020014963static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020014964{
14965 struct drm_i915_private *dev_priv = dev->dev_private;
14966 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020014967 struct intel_crtc *crtc;
14968 struct intel_encoder *encoder;
14969 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020014970 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020014971
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014972 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014973 struct drm_plane *primary = crtc->base.primary;
14974 struct intel_plane_state *plane_state;
14975
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014976 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020014977
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014978 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020014979
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014980 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014981 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020014982
Matt Roper83d65732015-02-25 13:12:16 -080014983 crtc->base.state->enable = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014984 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014985
14986 plane_state = to_intel_plane_state(primary->state);
14987 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020014988
14989 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
14990 crtc->base.base.id,
14991 crtc->active ? "enabled" : "disabled");
14992 }
14993
Daniel Vetter53589012013-06-05 13:34:16 +020014994 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
14995 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
14996
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020014997 pll->on = pll->get_hw_state(dev_priv, pll,
14998 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020014999 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015000 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015001 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015002 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015003 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015004 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015005 }
Daniel Vetter53589012013-06-05 13:34:16 +020015006 }
Daniel Vetter53589012013-06-05 13:34:16 +020015007
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015008 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015009 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015010
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015011 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015012 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015013 }
15014
Damien Lespiaub2784e12014-08-05 11:29:37 +010015015 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015016 pipe = 0;
15017
15018 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015019 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15020 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015021 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015022 } else {
15023 encoder->base.crtc = NULL;
15024 }
15025
15026 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015027 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015028 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015029 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015030 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015031 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015032 }
15033
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015034 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015035 if (connector->get_hw_state(connector)) {
15036 connector->base.dpms = DRM_MODE_DPMS_ON;
15037 connector->encoder->connectors_active = true;
15038 connector->base.encoder = &connector->encoder->base;
15039 } else {
15040 connector->base.dpms = DRM_MODE_DPMS_OFF;
15041 connector->base.encoder = NULL;
15042 }
15043 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15044 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015045 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015046 connector->base.encoder ? "enabled" : "disabled");
15047 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015048}
15049
15050/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15051 * and i915 state tracking structures. */
15052void intel_modeset_setup_hw_state(struct drm_device *dev,
15053 bool force_restore)
15054{
15055 struct drm_i915_private *dev_priv = dev->dev_private;
15056 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015057 struct intel_crtc *crtc;
15058 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015059 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015060
15061 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015062
Jesse Barnesbabea612013-06-26 18:57:38 +030015063 /*
15064 * Now that we have the config, copy it to each CRTC struct
15065 * Note that this could go away if we move to using crtc_config
15066 * checking everywhere.
15067 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015068 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015069 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015070 intel_mode_from_pipe_config(&crtc->base.mode,
15071 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015072 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15073 crtc->base.base.id);
15074 drm_mode_debug_printmodeline(&crtc->base.mode);
15075 }
15076 }
15077
Daniel Vetter24929352012-07-02 20:28:59 +020015078 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015079 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015080 intel_sanitize_encoder(encoder);
15081 }
15082
Damien Lespiau055e3932014-08-18 13:49:10 +010015083 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015084 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15085 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015086 intel_dump_pipe_config(crtc, crtc->config,
15087 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015088 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015089
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015090 intel_modeset_update_connector_atomic_state(dev);
15091
Daniel Vetter35c95372013-07-17 06:55:04 +020015092 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15093 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15094
15095 if (!pll->on || pll->active)
15096 continue;
15097
15098 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15099
15100 pll->disable(dev_priv, pll);
15101 pll->on = false;
15102 }
15103
Pradeep Bhat30789992014-11-04 17:06:45 +000015104 if (IS_GEN9(dev))
15105 skl_wm_get_hw_state(dev);
15106 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015107 ilk_wm_get_hw_state(dev);
15108
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015109 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015110 i915_redisable_vga(dev);
15111
Daniel Vetterf30da182013-04-11 20:22:50 +020015112 /*
15113 * We need to use raw interfaces for restoring state to avoid
15114 * checking (bogus) intermediate states.
15115 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015116 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015117 struct drm_crtc *crtc =
15118 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015119
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015120 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015121 }
15122 } else {
15123 intel_modeset_update_staged_output_state(dev);
15124 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015125
15126 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015127}
15128
15129void intel_modeset_gem_init(struct drm_device *dev)
15130{
Jesse Barnes92122782014-10-09 12:57:42 -070015131 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015132 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015133 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015134 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015135
Imre Deakae484342014-03-31 15:10:44 +030015136 mutex_lock(&dev->struct_mutex);
15137 intel_init_gt_powersave(dev);
15138 mutex_unlock(&dev->struct_mutex);
15139
Jesse Barnes92122782014-10-09 12:57:42 -070015140 /*
15141 * There may be no VBT; and if the BIOS enabled SSC we can
15142 * just keep using it to avoid unnecessary flicker. Whereas if the
15143 * BIOS isn't using it, don't assume it will work even if the VBT
15144 * indicates as much.
15145 */
15146 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15147 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15148 DREF_SSC1_ENABLE);
15149
Chris Wilson1833b132012-05-09 11:56:28 +010015150 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015151
15152 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015153
15154 /*
15155 * Make sure any fbs we allocated at startup are properly
15156 * pinned & fenced. When we do the allocation it's too early
15157 * for this.
15158 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015159 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015160 obj = intel_fb_obj(c->primary->fb);
15161 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015162 continue;
15163
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015164 mutex_lock(&dev->struct_mutex);
15165 ret = intel_pin_and_fence_fb_obj(c->primary,
15166 c->primary->fb,
15167 c->primary->state,
15168 NULL);
15169 mutex_unlock(&dev->struct_mutex);
15170 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015171 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15172 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015173 drm_framebuffer_unreference(c->primary->fb);
15174 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015175 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015176 }
15177 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015178
15179 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015180}
15181
Imre Deak4932e2c2014-02-11 17:12:48 +020015182void intel_connector_unregister(struct intel_connector *intel_connector)
15183{
15184 struct drm_connector *connector = &intel_connector->base;
15185
15186 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015187 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015188}
15189
Jesse Barnes79e53942008-11-07 14:24:08 -080015190void intel_modeset_cleanup(struct drm_device *dev)
15191{
Jesse Barnes652c3932009-08-17 13:31:43 -070015192 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015193 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015194
Imre Deak2eb52522014-11-19 15:30:05 +020015195 intel_disable_gt_powersave(dev);
15196
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015197 intel_backlight_unregister(dev);
15198
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015199 /*
15200 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015201 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015202 * experience fancy races otherwise.
15203 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015204 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015205
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015206 /*
15207 * Due to the hpd irq storm handling the hotplug work can re-arm the
15208 * poll handlers. Hence disable polling after hpd handling is shut down.
15209 */
Keith Packardf87ea762010-10-03 19:36:26 -070015210 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015211
Jesse Barnes652c3932009-08-17 13:31:43 -070015212 mutex_lock(&dev->struct_mutex);
15213
Jesse Barnes723bfd72010-10-07 16:01:13 -070015214 intel_unregister_dsm_handler();
15215
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015216 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015217
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015218 mutex_unlock(&dev->struct_mutex);
15219
Chris Wilson1630fe72011-07-08 12:22:42 +010015220 /* flush any delayed tasks or pending work */
15221 flush_scheduled_work();
15222
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015223 /* destroy the backlight and sysfs files before encoders/connectors */
15224 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015225 struct intel_connector *intel_connector;
15226
15227 intel_connector = to_intel_connector(connector);
15228 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015229 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015230
Jesse Barnes79e53942008-11-07 14:24:08 -080015231 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015232
15233 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015234
15235 mutex_lock(&dev->struct_mutex);
15236 intel_cleanup_gt_powersave(dev);
15237 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015238}
15239
Dave Airlie28d52042009-09-21 14:33:58 +100015240/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015241 * Return which encoder is currently attached for connector.
15242 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015243struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015244{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015245 return &intel_attached_encoder(connector)->base;
15246}
Jesse Barnes79e53942008-11-07 14:24:08 -080015247
Chris Wilsondf0e9242010-09-09 16:20:55 +010015248void intel_connector_attach_encoder(struct intel_connector *connector,
15249 struct intel_encoder *encoder)
15250{
15251 connector->encoder = encoder;
15252 drm_mode_connector_attach_encoder(&connector->base,
15253 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015254}
Dave Airlie28d52042009-09-21 14:33:58 +100015255
15256/*
15257 * set vga decode state - true == enable VGA decode
15258 */
15259int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15260{
15261 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015262 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015263 u16 gmch_ctrl;
15264
Chris Wilson75fa0412014-02-07 18:37:02 -020015265 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15266 DRM_ERROR("failed to read control word\n");
15267 return -EIO;
15268 }
15269
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015270 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15271 return 0;
15272
Dave Airlie28d52042009-09-21 14:33:58 +100015273 if (state)
15274 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15275 else
15276 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015277
15278 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15279 DRM_ERROR("failed to write control word\n");
15280 return -EIO;
15281 }
15282
Dave Airlie28d52042009-09-21 14:33:58 +100015283 return 0;
15284}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015285
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015286struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015287
15288 u32 power_well_driver;
15289
Chris Wilson63b66e52013-08-08 15:12:06 +020015290 int num_transcoders;
15291
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015292 struct intel_cursor_error_state {
15293 u32 control;
15294 u32 position;
15295 u32 base;
15296 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015297 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015298
15299 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015300 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015301 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015302 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015303 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015304
15305 struct intel_plane_error_state {
15306 u32 control;
15307 u32 stride;
15308 u32 size;
15309 u32 pos;
15310 u32 addr;
15311 u32 surface;
15312 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015313 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015314
15315 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015316 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015317 enum transcoder cpu_transcoder;
15318
15319 u32 conf;
15320
15321 u32 htotal;
15322 u32 hblank;
15323 u32 hsync;
15324 u32 vtotal;
15325 u32 vblank;
15326 u32 vsync;
15327 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015328};
15329
15330struct intel_display_error_state *
15331intel_display_capture_error_state(struct drm_device *dev)
15332{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015333 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015334 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015335 int transcoders[] = {
15336 TRANSCODER_A,
15337 TRANSCODER_B,
15338 TRANSCODER_C,
15339 TRANSCODER_EDP,
15340 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015341 int i;
15342
Chris Wilson63b66e52013-08-08 15:12:06 +020015343 if (INTEL_INFO(dev)->num_pipes == 0)
15344 return NULL;
15345
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015346 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015347 if (error == NULL)
15348 return NULL;
15349
Imre Deak190be112013-11-25 17:15:31 +020015350 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015351 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15352
Damien Lespiau055e3932014-08-18 13:49:10 +010015353 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015354 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015355 __intel_display_power_is_enabled(dev_priv,
15356 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015357 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015358 continue;
15359
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015360 error->cursor[i].control = I915_READ(CURCNTR(i));
15361 error->cursor[i].position = I915_READ(CURPOS(i));
15362 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015363
15364 error->plane[i].control = I915_READ(DSPCNTR(i));
15365 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015366 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015367 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015368 error->plane[i].pos = I915_READ(DSPPOS(i));
15369 }
Paulo Zanonica291362013-03-06 20:03:14 -030015370 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15371 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015372 if (INTEL_INFO(dev)->gen >= 4) {
15373 error->plane[i].surface = I915_READ(DSPSURF(i));
15374 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15375 }
15376
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015377 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015378
Sonika Jindal3abfce72014-07-21 15:23:43 +053015379 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015380 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015381 }
15382
15383 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15384 if (HAS_DDI(dev_priv->dev))
15385 error->num_transcoders++; /* Account for eDP. */
15386
15387 for (i = 0; i < error->num_transcoders; i++) {
15388 enum transcoder cpu_transcoder = transcoders[i];
15389
Imre Deakddf9c532013-11-27 22:02:02 +020015390 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015391 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015392 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015393 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015394 continue;
15395
Chris Wilson63b66e52013-08-08 15:12:06 +020015396 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15397
15398 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15399 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15400 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15401 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15402 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15403 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15404 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015405 }
15406
15407 return error;
15408}
15409
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015410#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15411
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015412void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015413intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015414 struct drm_device *dev,
15415 struct intel_display_error_state *error)
15416{
Damien Lespiau055e3932014-08-18 13:49:10 +010015417 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015418 int i;
15419
Chris Wilson63b66e52013-08-08 15:12:06 +020015420 if (!error)
15421 return;
15422
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015423 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015424 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015425 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015426 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015427 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015428 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015429 err_printf(m, " Power: %s\n",
15430 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015431 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015432 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015433
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015434 err_printf(m, "Plane [%d]:\n", i);
15435 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15436 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015437 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015438 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15439 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015440 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015441 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015442 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015443 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015444 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15445 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015446 }
15447
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015448 err_printf(m, "Cursor [%d]:\n", i);
15449 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15450 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15451 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015452 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015453
15454 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015455 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015456 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015457 err_printf(m, " Power: %s\n",
15458 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015459 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15460 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15461 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15462 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15463 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15464 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15465 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15466 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015467}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015468
15469void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15470{
15471 struct intel_crtc *crtc;
15472
15473 for_each_intel_crtc(dev, crtc) {
15474 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015475
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015476 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015477
15478 work = crtc->unpin_work;
15479
15480 if (work && work->event &&
15481 work->event->base.file_priv == file) {
15482 kfree(work->event);
15483 work->event = NULL;
15484 }
15485
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015486 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015487 }
15488}