blob: 4325d00a4598967e560afdc49a23cf0ae88af1d3 [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 for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Matt Roper465c1202014-05-29 08:06:54 -070075};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Chris Wilson6b383a72010-09-13 13:54:26 +010082static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080083
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020085 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030086static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020089static int intel_set_mode(struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080090static int intel_framebuffer_init(struct drm_device *dev,
91 struct intel_framebuffer *ifb,
92 struct drm_mode_fb_cmd2 *mode_cmd,
93 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020094static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
95static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020096static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070097 struct intel_link_m_n *m_n,
98 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +020099static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200100static void haswell_set_pipeconf(struct drm_crtc *crtc);
101static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200102static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200103 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200104static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200105 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800106static void intel_begin_crtc_commit(struct drm_crtc *crtc);
107static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700108static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
109 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200110static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
111 int num_connectors);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100112
Dave Airlie0e32b392014-05-02 14:02:48 +1000113static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
114{
115 if (!connector->mst_port)
116 return connector->encoder;
117 else
118 return &connector->mst_port->mst_encoders[pipe]->base;
119}
120
Jesse Barnes79e53942008-11-07 14:24:08 -0800121typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400122 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800123} intel_range_t;
124
125typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400126 int dot_limit;
127 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800128} intel_p2_t;
129
Ma Lingd4906092009-03-18 20:13:27 +0800130typedef struct intel_limit intel_limit_t;
131struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400132 intel_range_t dot, vco, n, m, m1, m2, p, p1;
133 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800134};
Jesse Barnes79e53942008-11-07 14:24:08 -0800135
Daniel Vetterd2acd212012-10-20 20:57:43 +0200136int
137intel_pch_rawclk(struct drm_device *dev)
138{
139 struct drm_i915_private *dev_priv = dev->dev_private;
140
141 WARN_ON(!HAS_PCH_SPLIT(dev));
142
143 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
144}
145
Chris Wilson021357a2010-09-07 20:54:59 +0100146static inline u32 /* units of 100MHz */
147intel_fdi_link_freq(struct drm_device *dev)
148{
Chris Wilson8b99e682010-10-13 09:59:17 +0100149 if (IS_GEN5(dev)) {
150 struct drm_i915_private *dev_priv = dev->dev_private;
151 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
152 } else
153 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100154}
155
Daniel Vetter5d536e22013-07-06 12:52:06 +0200156static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400157 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200158 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200159 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .m = { .min = 96, .max = 140 },
161 .m1 = { .min = 18, .max = 26 },
162 .m2 = { .min = 6, .max = 16 },
163 .p = { .min = 4, .max = 128 },
164 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700165 .p2 = { .dot_limit = 165000,
166 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700167};
168
Daniel Vetter5d536e22013-07-06 12:52:06 +0200169static const intel_limit_t intel_limits_i8xx_dvo = {
170 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200171 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200172 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200173 .m = { .min = 96, .max = 140 },
174 .m1 = { .min = 18, .max = 26 },
175 .m2 = { .min = 6, .max = 16 },
176 .p = { .min = 4, .max = 128 },
177 .p1 = { .min = 2, .max = 33 },
178 .p2 = { .dot_limit = 165000,
179 .p2_slow = 4, .p2_fast = 4 },
180};
181
Keith Packarde4b36692009-06-05 19:22:17 -0700182static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400183 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200184 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200185 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .m = { .min = 96, .max = 140 },
187 .m1 = { .min = 18, .max = 26 },
188 .m2 = { .min = 6, .max = 16 },
189 .p = { .min = 4, .max = 128 },
190 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700191 .p2 = { .dot_limit = 165000,
192 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700193};
Eric Anholt273e27c2011-03-30 13:01:10 -0700194
Keith Packarde4b36692009-06-05 19:22:17 -0700195static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400196 .dot = { .min = 20000, .max = 400000 },
197 .vco = { .min = 1400000, .max = 2800000 },
198 .n = { .min = 1, .max = 6 },
199 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100200 .m1 = { .min = 8, .max = 18 },
201 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400202 .p = { .min = 5, .max = 80 },
203 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700204 .p2 = { .dot_limit = 200000,
205 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700206};
207
208static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400209 .dot = { .min = 20000, .max = 400000 },
210 .vco = { .min = 1400000, .max = 2800000 },
211 .n = { .min = 1, .max = 6 },
212 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100213 .m1 = { .min = 8, .max = 18 },
214 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400215 .p = { .min = 7, .max = 98 },
216 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700217 .p2 = { .dot_limit = 112000,
218 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700219};
220
Eric Anholt273e27c2011-03-30 13:01:10 -0700221
Keith Packarde4b36692009-06-05 19:22:17 -0700222static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700223 .dot = { .min = 25000, .max = 270000 },
224 .vco = { .min = 1750000, .max = 3500000},
225 .n = { .min = 1, .max = 4 },
226 .m = { .min = 104, .max = 138 },
227 .m1 = { .min = 17, .max = 23 },
228 .m2 = { .min = 5, .max = 11 },
229 .p = { .min = 10, .max = 30 },
230 .p1 = { .min = 1, .max = 3},
231 .p2 = { .dot_limit = 270000,
232 .p2_slow = 10,
233 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800234 },
Keith Packarde4b36692009-06-05 19:22:17 -0700235};
236
237static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700238 .dot = { .min = 22000, .max = 400000 },
239 .vco = { .min = 1750000, .max = 3500000},
240 .n = { .min = 1, .max = 4 },
241 .m = { .min = 104, .max = 138 },
242 .m1 = { .min = 16, .max = 23 },
243 .m2 = { .min = 5, .max = 11 },
244 .p = { .min = 5, .max = 80 },
245 .p1 = { .min = 1, .max = 8},
246 .p2 = { .dot_limit = 165000,
247 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700248};
249
250static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700251 .dot = { .min = 20000, .max = 115000 },
252 .vco = { .min = 1750000, .max = 3500000 },
253 .n = { .min = 1, .max = 3 },
254 .m = { .min = 104, .max = 138 },
255 .m1 = { .min = 17, .max = 23 },
256 .m2 = { .min = 5, .max = 11 },
257 .p = { .min = 28, .max = 112 },
258 .p1 = { .min = 2, .max = 8 },
259 .p2 = { .dot_limit = 0,
260 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800261 },
Keith Packarde4b36692009-06-05 19:22:17 -0700262};
263
264static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700265 .dot = { .min = 80000, .max = 224000 },
266 .vco = { .min = 1750000, .max = 3500000 },
267 .n = { .min = 1, .max = 3 },
268 .m = { .min = 104, .max = 138 },
269 .m1 = { .min = 17, .max = 23 },
270 .m2 = { .min = 5, .max = 11 },
271 .p = { .min = 14, .max = 42 },
272 .p1 = { .min = 2, .max = 6 },
273 .p2 = { .dot_limit = 0,
274 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800275 },
Keith Packarde4b36692009-06-05 19:22:17 -0700276};
277
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500278static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400279 .dot = { .min = 20000, .max = 400000},
280 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700281 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .n = { .min = 3, .max = 6 },
283 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .m1 = { .min = 0, .max = 0 },
286 .m2 = { .min = 0, .max = 254 },
287 .p = { .min = 5, .max = 80 },
288 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700289 .p2 = { .dot_limit = 200000,
290 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700291};
292
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500293static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400294 .dot = { .min = 20000, .max = 400000 },
295 .vco = { .min = 1700000, .max = 3500000 },
296 .n = { .min = 3, .max = 6 },
297 .m = { .min = 2, .max = 256 },
298 .m1 = { .min = 0, .max = 0 },
299 .m2 = { .min = 0, .max = 254 },
300 .p = { .min = 7, .max = 112 },
301 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700302 .p2 = { .dot_limit = 112000,
303 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700304};
305
Eric Anholt273e27c2011-03-30 13:01:10 -0700306/* Ironlake / Sandybridge
307 *
308 * We calculate clock using (register_value + 2) for N/M1/M2, so here
309 * the range value for them is (actual_value - 2).
310 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800311static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700312 .dot = { .min = 25000, .max = 350000 },
313 .vco = { .min = 1760000, .max = 3510000 },
314 .n = { .min = 1, .max = 5 },
315 .m = { .min = 79, .max = 127 },
316 .m1 = { .min = 12, .max = 22 },
317 .m2 = { .min = 5, .max = 9 },
318 .p = { .min = 5, .max = 80 },
319 .p1 = { .min = 1, .max = 8 },
320 .p2 = { .dot_limit = 225000,
321 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700322};
323
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800324static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700325 .dot = { .min = 25000, .max = 350000 },
326 .vco = { .min = 1760000, .max = 3510000 },
327 .n = { .min = 1, .max = 3 },
328 .m = { .min = 79, .max = 118 },
329 .m1 = { .min = 12, .max = 22 },
330 .m2 = { .min = 5, .max = 9 },
331 .p = { .min = 28, .max = 112 },
332 .p1 = { .min = 2, .max = 8 },
333 .p2 = { .dot_limit = 225000,
334 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800335};
336
337static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700338 .dot = { .min = 25000, .max = 350000 },
339 .vco = { .min = 1760000, .max = 3510000 },
340 .n = { .min = 1, .max = 3 },
341 .m = { .min = 79, .max = 127 },
342 .m1 = { .min = 12, .max = 22 },
343 .m2 = { .min = 5, .max = 9 },
344 .p = { .min = 14, .max = 56 },
345 .p1 = { .min = 2, .max = 8 },
346 .p2 = { .dot_limit = 225000,
347 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800348};
349
Eric Anholt273e27c2011-03-30 13:01:10 -0700350/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700352 .dot = { .min = 25000, .max = 350000 },
353 .vco = { .min = 1760000, .max = 3510000 },
354 .n = { .min = 1, .max = 2 },
355 .m = { .min = 79, .max = 126 },
356 .m1 = { .min = 12, .max = 22 },
357 .m2 = { .min = 5, .max = 9 },
358 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400359 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700360 .p2 = { .dot_limit = 225000,
361 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800362};
363
364static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700365 .dot = { .min = 25000, .max = 350000 },
366 .vco = { .min = 1760000, .max = 3510000 },
367 .n = { .min = 1, .max = 3 },
368 .m = { .min = 79, .max = 126 },
369 .m1 = { .min = 12, .max = 22 },
370 .m2 = { .min = 5, .max = 9 },
371 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400372 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700373 .p2 = { .dot_limit = 225000,
374 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800375};
376
Ville Syrjälädc730512013-09-24 21:26:30 +0300377static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300378 /*
379 * These are the data rate limits (measured in fast clocks)
380 * since those are the strictest limits we have. The fast
381 * clock and actual rate limits are more relaxed, so checking
382 * them would make no difference.
383 */
384 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200385 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700386 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700387 .m1 = { .min = 2, .max = 3 },
388 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300389 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300390 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700391};
392
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300393static const intel_limit_t intel_limits_chv = {
394 /*
395 * These are the data rate limits (measured in fast clocks)
396 * since those are the strictest limits we have. The fast
397 * clock and actual rate limits are more relaxed, so checking
398 * them would make no difference.
399 */
400 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200401 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300402 .n = { .min = 1, .max = 1 },
403 .m1 = { .min = 2, .max = 2 },
404 .m2 = { .min = 24 << 22, .max = 175 << 22 },
405 .p1 = { .min = 2, .max = 4 },
406 .p2 = { .p2_slow = 1, .p2_fast = 14 },
407};
408
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200409static const intel_limit_t intel_limits_bxt = {
410 /* FIXME: find real dot limits */
411 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530412 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200413 .n = { .min = 1, .max = 1 },
414 .m1 = { .min = 2, .max = 2 },
415 /* FIXME: find real m2 limits */
416 .m2 = { .min = 2 << 22, .max = 255 << 22 },
417 .p1 = { .min = 2, .max = 4 },
418 .p2 = { .p2_slow = 1, .p2_fast = 20 },
419};
420
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200421static bool
422needs_modeset(struct drm_crtc_state *state)
423{
424 return state->mode_changed || state->active_changed;
425}
426
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300427/**
428 * Returns whether any output on the specified pipe is of the specified type
429 */
Damien Lespiau40935612014-10-29 11:16:59 +0000430bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300431{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300432 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300433 struct intel_encoder *encoder;
434
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300435 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300436 if (encoder->type == type)
437 return true;
438
439 return false;
440}
441
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200442/**
443 * Returns whether any output on the specified pipe will have the specified
444 * type after a staged modeset is complete, i.e., the same as
445 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
446 * encoder->crtc.
447 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200448static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
449 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200450{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200451 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300452 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200453 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200454 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200456
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300457 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 if (connector_state->crtc != crtc_state->base.crtc)
459 continue;
460
461 num_connectors++;
462
463 encoder = to_intel_encoder(connector_state->best_encoder);
464 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200465 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200466 }
467
468 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200469
470 return false;
471}
472
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200473static const intel_limit_t *
474intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800475{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200476 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800477 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800478
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200479 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100480 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000481 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800482 limit = &intel_limits_ironlake_dual_lvds_100m;
483 else
484 limit = &intel_limits_ironlake_dual_lvds;
485 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000486 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800487 limit = &intel_limits_ironlake_single_lvds_100m;
488 else
489 limit = &intel_limits_ironlake_single_lvds;
490 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200491 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800492 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800493
494 return limit;
495}
496
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200497static const intel_limit_t *
498intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800499{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200500 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800501 const intel_limit_t *limit;
502
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200503 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100504 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700505 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800506 else
Keith Packarde4b36692009-06-05 19:22:17 -0700507 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200508 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
509 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700510 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200511 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800515
516 return limit;
517}
518
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200519static const intel_limit_t *
520intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800521{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200522 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800523 const intel_limit_t *limit;
524
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200525 if (IS_BROXTON(dev))
526 limit = &intel_limits_bxt;
527 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200528 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800529 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200530 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500531 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500533 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800534 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500535 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300536 } else if (IS_CHERRYVIEW(dev)) {
537 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700538 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300539 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100540 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200541 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100542 limit = &intel_limits_i9xx_lvds;
543 else
544 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800545 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200546 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700547 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700549 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200550 else
551 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 }
553 return limit;
554}
555
Imre Deakdccbea32015-06-22 23:35:51 +0300556/*
557 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
558 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
559 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
560 * The helpers' return value is the rate of the clock that is fed to the
561 * display engine's pipe which can be the above fast dot clock rate or a
562 * divided-down version of it.
563 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500564/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300565static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800566{
Shaohua Li21778322009-02-23 15:19:16 +0800567 clock->m = clock->m2 + 2;
568 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200569 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300570 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300571 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
572 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300573
574 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800575}
576
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200577static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
578{
579 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
580}
581
Imre Deakdccbea32015-06-22 23:35:51 +0300582static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800583{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200584 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800585 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200586 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300587 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300588 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
589 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300590
591 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800592}
593
Imre Deakdccbea32015-06-22 23:35:51 +0300594static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300595{
596 clock->m = clock->m1 * clock->m2;
597 clock->p = clock->p1 * clock->p2;
598 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300599 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300600 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
601 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300602
603 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300604}
605
Imre Deakdccbea32015-06-22 23:35:51 +0300606int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300607{
608 clock->m = clock->m1 * clock->m2;
609 clock->p = clock->p1 * clock->p2;
610 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300611 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300612 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
613 clock->n << 22);
614 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300615
616 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300617}
618
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800619#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800620/**
621 * Returns whether the given set of divisors are valid for a given refclk with
622 * the given connectors.
623 */
624
Chris Wilson1b894b52010-12-14 20:04:54 +0000625static bool intel_PLL_is_valid(struct drm_device *dev,
626 const intel_limit_t *limit,
627 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800628{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300629 if (clock->n < limit->n.min || limit->n.max < clock->n)
630 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800631 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400632 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800633 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400634 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800635 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300637
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200638 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300639 if (clock->m1 <= clock->m2)
640 INTELPllInvalid("m1 <= m2\n");
641
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200642 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300643 if (clock->p < limit->p.min || limit->p.max < clock->p)
644 INTELPllInvalid("p out of range\n");
645 if (clock->m < limit->m.min || limit->m.max < clock->m)
646 INTELPllInvalid("m out of range\n");
647 }
648
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400650 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800651 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
652 * connector, etc., rather than just a single range.
653 */
654 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400655 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800656
657 return true;
658}
659
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300660static int
661i9xx_select_p2_div(const intel_limit_t *limit,
662 const struct intel_crtc_state *crtc_state,
663 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800664{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300665 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800666
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200667 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800668 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100669 * For LVDS just rely on its current settings for dual-channel.
670 * We haven't figured out how to reliably set up different
671 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800672 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100673 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300674 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800675 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300676 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800677 } else {
678 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300679 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800680 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300681 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800682 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300683}
684
685static bool
686i9xx_find_best_dpll(const intel_limit_t *limit,
687 struct intel_crtc_state *crtc_state,
688 int target, int refclk, intel_clock_t *match_clock,
689 intel_clock_t *best_clock)
690{
691 struct drm_device *dev = crtc_state->base.crtc->dev;
692 intel_clock_t clock;
693 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800694
Akshay Joshi0206e352011-08-16 15:34:10 -0400695 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800696
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300697 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
698
Zhao Yakui42158662009-11-20 11:24:18 +0800699 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
700 clock.m1++) {
701 for (clock.m2 = limit->m2.min;
702 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200703 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800704 break;
705 for (clock.n = limit->n.min;
706 clock.n <= limit->n.max; clock.n++) {
707 for (clock.p1 = limit->p1.min;
708 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800709 int this_err;
710
Imre Deakdccbea32015-06-22 23:35:51 +0300711 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000712 if (!intel_PLL_is_valid(dev, limit,
713 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800714 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800715 if (match_clock &&
716 clock.p != match_clock->p)
717 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800718
719 this_err = abs(clock.dot - target);
720 if (this_err < err) {
721 *best_clock = clock;
722 err = this_err;
723 }
724 }
725 }
726 }
727 }
728
729 return (err != target);
730}
731
Ma Lingd4906092009-03-18 20:13:27 +0800732static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200733pnv_find_best_dpll(const intel_limit_t *limit,
734 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200735 int target, int refclk, intel_clock_t *match_clock,
736 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200737{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300738 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200739 intel_clock_t clock;
740 int err = target;
741
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200742 memset(best_clock, 0, sizeof(*best_clock));
743
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300744 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
745
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200746 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
747 clock.m1++) {
748 for (clock.m2 = limit->m2.min;
749 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200750 for (clock.n = limit->n.min;
751 clock.n <= limit->n.max; clock.n++) {
752 for (clock.p1 = limit->p1.min;
753 clock.p1 <= limit->p1.max; clock.p1++) {
754 int this_err;
755
Imre Deakdccbea32015-06-22 23:35:51 +0300756 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800757 if (!intel_PLL_is_valid(dev, limit,
758 &clock))
759 continue;
760 if (match_clock &&
761 clock.p != match_clock->p)
762 continue;
763
764 this_err = abs(clock.dot - target);
765 if (this_err < err) {
766 *best_clock = clock;
767 err = this_err;
768 }
769 }
770 }
771 }
772 }
773
774 return (err != target);
775}
776
Ma Lingd4906092009-03-18 20:13:27 +0800777static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200778g4x_find_best_dpll(const intel_limit_t *limit,
779 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200780 int target, int refclk, intel_clock_t *match_clock,
781 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800782{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300783 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800784 intel_clock_t clock;
785 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300786 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400787 /* approximately equals target * 0.00585 */
788 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800789
790 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300791
792 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
793
Ma Lingd4906092009-03-18 20:13:27 +0800794 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200795 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800796 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200797 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800798 for (clock.m1 = limit->m1.max;
799 clock.m1 >= limit->m1.min; clock.m1--) {
800 for (clock.m2 = limit->m2.max;
801 clock.m2 >= limit->m2.min; clock.m2--) {
802 for (clock.p1 = limit->p1.max;
803 clock.p1 >= limit->p1.min; clock.p1--) {
804 int this_err;
805
Imre Deakdccbea32015-06-22 23:35:51 +0300806 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000807 if (!intel_PLL_is_valid(dev, limit,
808 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800809 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000810
811 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800812 if (this_err < err_most) {
813 *best_clock = clock;
814 err_most = this_err;
815 max_n = clock.n;
816 found = true;
817 }
818 }
819 }
820 }
821 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800822 return found;
823}
Ma Lingd4906092009-03-18 20:13:27 +0800824
Imre Deakd5dd62b2015-03-17 11:40:03 +0200825/*
826 * Check if the calculated PLL configuration is more optimal compared to the
827 * best configuration and error found so far. Return the calculated error.
828 */
829static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
830 const intel_clock_t *calculated_clock,
831 const intel_clock_t *best_clock,
832 unsigned int best_error_ppm,
833 unsigned int *error_ppm)
834{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200835 /*
836 * For CHV ignore the error and consider only the P value.
837 * Prefer a bigger P value based on HW requirements.
838 */
839 if (IS_CHERRYVIEW(dev)) {
840 *error_ppm = 0;
841
842 return calculated_clock->p > best_clock->p;
843 }
844
Imre Deak24be4e42015-03-17 11:40:04 +0200845 if (WARN_ON_ONCE(!target_freq))
846 return false;
847
Imre Deakd5dd62b2015-03-17 11:40:03 +0200848 *error_ppm = div_u64(1000000ULL *
849 abs(target_freq - calculated_clock->dot),
850 target_freq);
851 /*
852 * Prefer a better P value over a better (smaller) error if the error
853 * is small. Ensure this preference for future configurations too by
854 * setting the error to 0.
855 */
856 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
857 *error_ppm = 0;
858
859 return true;
860 }
861
862 return *error_ppm + 10 < best_error_ppm;
863}
864
Zhenyu Wang2c072452009-06-05 15:38:42 +0800865static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200866vlv_find_best_dpll(const intel_limit_t *limit,
867 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200868 int target, int refclk, intel_clock_t *match_clock,
869 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700870{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200871 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300872 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300873 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300874 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300875 /* min update 19.2 MHz */
876 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300877 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700878
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300879 target *= 5; /* fast clock */
880
881 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700882
883 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300884 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300885 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300886 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300887 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300888 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700889 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300890 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200891 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300892
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300893 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
894 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300895
Imre Deakdccbea32015-06-22 23:35:51 +0300896 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300897
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300898 if (!intel_PLL_is_valid(dev, limit,
899 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300900 continue;
901
Imre Deakd5dd62b2015-03-17 11:40:03 +0200902 if (!vlv_PLL_is_optimal(dev, target,
903 &clock,
904 best_clock,
905 bestppm, &ppm))
906 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300907
Imre Deakd5dd62b2015-03-17 11:40:03 +0200908 *best_clock = clock;
909 bestppm = ppm;
910 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700911 }
912 }
913 }
914 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700915
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300916 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700917}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700918
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300919static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200920chv_find_best_dpll(const intel_limit_t *limit,
921 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300922 int target, int refclk, intel_clock_t *match_clock,
923 intel_clock_t *best_clock)
924{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200925 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300926 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200927 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300928 intel_clock_t clock;
929 uint64_t m2;
930 int found = false;
931
932 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200933 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300934
935 /*
936 * Based on hardware doc, the n always set to 1, and m1 always
937 * set to 2. If requires to support 200Mhz refclk, we need to
938 * revisit this because n may not 1 anymore.
939 */
940 clock.n = 1, clock.m1 = 2;
941 target *= 5; /* fast clock */
942
943 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
944 for (clock.p2 = limit->p2.p2_fast;
945 clock.p2 >= limit->p2.p2_slow;
946 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200947 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300948
949 clock.p = clock.p1 * clock.p2;
950
951 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
952 clock.n) << 22, refclk * clock.m1);
953
954 if (m2 > INT_MAX/clock.m1)
955 continue;
956
957 clock.m2 = m2;
958
Imre Deakdccbea32015-06-22 23:35:51 +0300959 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300960
961 if (!intel_PLL_is_valid(dev, limit, &clock))
962 continue;
963
Imre Deak9ca3ba02015-03-17 11:40:05 +0200964 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
965 best_error_ppm, &error_ppm))
966 continue;
967
968 *best_clock = clock;
969 best_error_ppm = error_ppm;
970 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300971 }
972 }
973
974 return found;
975}
976
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200977bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
978 intel_clock_t *best_clock)
979{
980 int refclk = i9xx_get_refclk(crtc_state, 0);
981
982 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
983 target_clock, refclk, NULL, best_clock);
984}
985
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300986bool intel_crtc_active(struct drm_crtc *crtc)
987{
988 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
989
990 /* Be paranoid as we can arrive here with only partial
991 * state retrieved from the hardware during setup.
992 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100993 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300994 * as Haswell has gained clock readout/fastboot support.
995 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000996 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300997 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700998 *
999 * FIXME: The intel_crtc->active here should be switched to
1000 * crtc->state->active once we have proper CRTC states wired up
1001 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001002 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001003 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001004 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001005}
1006
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001007enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1008 enum pipe pipe)
1009{
1010 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1012
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001013 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001014}
1015
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001016static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1017{
1018 struct drm_i915_private *dev_priv = dev->dev_private;
1019 u32 reg = PIPEDSL(pipe);
1020 u32 line1, line2;
1021 u32 line_mask;
1022
1023 if (IS_GEN2(dev))
1024 line_mask = DSL_LINEMASK_GEN2;
1025 else
1026 line_mask = DSL_LINEMASK_GEN3;
1027
1028 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001029 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001030 line2 = I915_READ(reg) & line_mask;
1031
1032 return line1 == line2;
1033}
1034
Keith Packardab7ad7f2010-10-03 00:33:06 -07001035/*
1036 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001037 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001038 *
1039 * After disabling a pipe, we can't wait for vblank in the usual way,
1040 * spinning on the vblank interrupt status bit, since we won't actually
1041 * see an interrupt when the pipe is disabled.
1042 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001043 * On Gen4 and above:
1044 * wait for the pipe register state bit to turn off
1045 *
1046 * Otherwise:
1047 * wait for the display line value to settle (it usually
1048 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001049 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001050 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001051static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001052{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001053 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001054 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001055 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001056 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057
Keith Packardab7ad7f2010-10-03 00:33:06 -07001058 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001059 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001060
Keith Packardab7ad7f2010-10-03 00:33:06 -07001061 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001062 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1063 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001064 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001066 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001067 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001068 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001069 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001070}
1071
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001072/*
1073 * ibx_digital_port_connected - is the specified port connected?
1074 * @dev_priv: i915 private structure
1075 * @port: the port to test
1076 *
1077 * Returns true if @port is connected, false otherwise.
1078 */
1079bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1080 struct intel_digital_port *port)
1081{
1082 u32 bit;
1083
Damien Lespiauc36346e2012-12-13 16:09:03 +00001084 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001085 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001086 case PORT_B:
1087 bit = SDE_PORTB_HOTPLUG;
1088 break;
1089 case PORT_C:
1090 bit = SDE_PORTC_HOTPLUG;
1091 break;
1092 case PORT_D:
1093 bit = SDE_PORTD_HOTPLUG;
1094 break;
1095 default:
1096 return true;
1097 }
1098 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001099 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001100 case PORT_B:
1101 bit = SDE_PORTB_HOTPLUG_CPT;
1102 break;
1103 case PORT_C:
1104 bit = SDE_PORTC_HOTPLUG_CPT;
1105 break;
1106 case PORT_D:
1107 bit = SDE_PORTD_HOTPLUG_CPT;
1108 break;
1109 default:
1110 return true;
1111 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001112 }
1113
1114 return I915_READ(SDEISR) & bit;
1115}
1116
Jesse Barnesb24e7172011-01-04 15:09:30 -08001117static const char *state_string(bool enabled)
1118{
1119 return enabled ? "on" : "off";
1120}
1121
1122/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001123void assert_pll(struct drm_i915_private *dev_priv,
1124 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001125{
1126 int reg;
1127 u32 val;
1128 bool cur_state;
1129
1130 reg = DPLL(pipe);
1131 val = I915_READ(reg);
1132 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001133 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001134 "PLL state assertion failure (expected %s, current %s)\n",
1135 state_string(state), state_string(cur_state));
1136}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001137
Jani Nikula23538ef2013-08-27 15:12:22 +03001138/* XXX: the dsi pll is shared between MIPI DSI ports */
1139static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1140{
1141 u32 val;
1142 bool cur_state;
1143
Ville Syrjäläa5805162015-05-26 20:42:30 +03001144 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001145 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001146 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001147
1148 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001149 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001150 "DSI PLL state assertion failure (expected %s, current %s)\n",
1151 state_string(state), state_string(cur_state));
1152}
1153#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1154#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1155
Daniel Vetter55607e82013-06-16 21:42:39 +02001156struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001157intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001158{
Daniel Vettere2b78262013-06-07 23:10:03 +02001159 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1160
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001161 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001162 return NULL;
1163
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001164 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001165}
1166
Jesse Barnesb24e7172011-01-04 15:09:30 -08001167/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001168void assert_shared_dpll(struct drm_i915_private *dev_priv,
1169 struct intel_shared_dpll *pll,
1170 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001171{
Jesse Barnes040484a2011-01-03 12:14:26 -08001172 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001173 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001174
Chris Wilson92b27b02012-05-20 18:10:50 +01001175 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001176 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001177 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001178
Daniel Vetter53589012013-06-05 13:34:16 +02001179 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001180 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001181 "%s assertion failure (expected %s, current %s)\n",
1182 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001183}
Jesse Barnes040484a2011-01-03 12:14:26 -08001184
1185static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1186 enum pipe pipe, bool state)
1187{
1188 int reg;
1189 u32 val;
1190 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001191 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1192 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001193
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001194 if (HAS_DDI(dev_priv->dev)) {
1195 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001196 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001197 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001198 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001199 } else {
1200 reg = FDI_TX_CTL(pipe);
1201 val = I915_READ(reg);
1202 cur_state = !!(val & FDI_TX_ENABLE);
1203 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001204 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001205 "FDI TX state assertion failure (expected %s, current %s)\n",
1206 state_string(state), state_string(cur_state));
1207}
1208#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1209#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1210
1211static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1212 enum pipe pipe, bool state)
1213{
1214 int reg;
1215 u32 val;
1216 bool cur_state;
1217
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001218 reg = FDI_RX_CTL(pipe);
1219 val = I915_READ(reg);
1220 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001221 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001222 "FDI RX state assertion failure (expected %s, current %s)\n",
1223 state_string(state), state_string(cur_state));
1224}
1225#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1226#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1227
1228static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1229 enum pipe pipe)
1230{
1231 int reg;
1232 u32 val;
1233
1234 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001235 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001236 return;
1237
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001238 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001239 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001240 return;
1241
Jesse Barnes040484a2011-01-03 12:14:26 -08001242 reg = FDI_TX_CTL(pipe);
1243 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001244 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 -08001245}
1246
Daniel Vetter55607e82013-06-16 21:42:39 +02001247void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1248 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001249{
1250 int reg;
1251 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001252 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001253
1254 reg = FDI_RX_CTL(pipe);
1255 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001256 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001257 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001258 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1259 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001260}
1261
Daniel Vetterb680c372014-09-19 18:27:27 +02001262void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1263 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001264{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001265 struct drm_device *dev = dev_priv->dev;
1266 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001267 u32 val;
1268 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001269 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001270
Jani Nikulabedd4db2014-08-22 15:04:13 +03001271 if (WARN_ON(HAS_DDI(dev)))
1272 return;
1273
1274 if (HAS_PCH_SPLIT(dev)) {
1275 u32 port_sel;
1276
Jesse Barnesea0760c2011-01-04 15:09:32 -08001277 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001278 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1279
1280 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1281 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1282 panel_pipe = PIPE_B;
1283 /* XXX: else fix for eDP */
1284 } else if (IS_VALLEYVIEW(dev)) {
1285 /* presumably write lock depends on pipe, not port select */
1286 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1287 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001288 } else {
1289 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001290 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1291 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001292 }
1293
1294 val = I915_READ(pp_reg);
1295 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001296 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001297 locked = false;
1298
Rob Clarke2c719b2014-12-15 13:56:32 -05001299 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001300 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001301 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001302}
1303
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001304static void assert_cursor(struct drm_i915_private *dev_priv,
1305 enum pipe pipe, bool state)
1306{
1307 struct drm_device *dev = dev_priv->dev;
1308 bool cur_state;
1309
Paulo Zanonid9d82082014-02-27 16:30:56 -03001310 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001311 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001312 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001313 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001314
Rob Clarke2c719b2014-12-15 13:56:32 -05001315 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001316 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1317 pipe_name(pipe), state_string(state), state_string(cur_state));
1318}
1319#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1320#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1321
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001322void assert_pipe(struct drm_i915_private *dev_priv,
1323 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001324{
1325 int reg;
1326 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001327 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001328 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1329 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001330
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001331 /* if we need the pipe quirk it must be always on */
1332 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1333 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001334 state = true;
1335
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001336 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001337 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001338 cur_state = false;
1339 } else {
1340 reg = PIPECONF(cpu_transcoder);
1341 val = I915_READ(reg);
1342 cur_state = !!(val & PIPECONF_ENABLE);
1343 }
1344
Rob Clarke2c719b2014-12-15 13:56:32 -05001345 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001346 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001347 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001348}
1349
Chris Wilson931872f2012-01-16 23:01:13 +00001350static void assert_plane(struct drm_i915_private *dev_priv,
1351 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001352{
1353 int reg;
1354 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001355 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001356
1357 reg = DSPCNTR(plane);
1358 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001359 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001360 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001361 "plane %c assertion failure (expected %s, current %s)\n",
1362 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001363}
1364
Chris Wilson931872f2012-01-16 23:01:13 +00001365#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1366#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1367
Jesse Barnesb24e7172011-01-04 15:09:30 -08001368static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1369 enum pipe pipe)
1370{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001371 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001372 int reg, i;
1373 u32 val;
1374 int cur_pipe;
1375
Ville Syrjälä653e1022013-06-04 13:49:05 +03001376 /* Primary planes are fixed to pipes on gen4+ */
1377 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001378 reg = DSPCNTR(pipe);
1379 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001380 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001381 "plane %c assertion failure, should be disabled but not\n",
1382 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001383 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001384 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001385
Jesse Barnesb24e7172011-01-04 15:09:30 -08001386 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001387 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001388 reg = DSPCNTR(i);
1389 val = I915_READ(reg);
1390 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1391 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001392 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001393 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1394 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001395 }
1396}
1397
Jesse Barnes19332d72013-03-28 09:55:38 -07001398static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1399 enum pipe pipe)
1400{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001401 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001402 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001403 u32 val;
1404
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001405 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001406 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001407 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001408 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001409 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1410 sprite, pipe_name(pipe));
1411 }
1412 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001413 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001414 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001415 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001416 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001417 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001418 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001419 }
1420 } else if (INTEL_INFO(dev)->gen >= 7) {
1421 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001422 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001423 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001424 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001425 plane_name(pipe), pipe_name(pipe));
1426 } else if (INTEL_INFO(dev)->gen >= 5) {
1427 reg = DVSCNTR(pipe);
1428 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001429 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001430 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1431 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001432 }
1433}
1434
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001435static void assert_vblank_disabled(struct drm_crtc *crtc)
1436{
Rob Clarke2c719b2014-12-15 13:56:32 -05001437 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001438 drm_crtc_vblank_put(crtc);
1439}
1440
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001441static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001442{
1443 u32 val;
1444 bool enabled;
1445
Rob Clarke2c719b2014-12-15 13:56:32 -05001446 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001447
Jesse Barnes92f25842011-01-04 15:09:34 -08001448 val = I915_READ(PCH_DREF_CONTROL);
1449 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1450 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001451 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001452}
1453
Daniel Vetterab9412b2013-05-03 11:49:46 +02001454static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1455 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001456{
1457 int reg;
1458 u32 val;
1459 bool enabled;
1460
Daniel Vetterab9412b2013-05-03 11:49:46 +02001461 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001462 val = I915_READ(reg);
1463 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001464 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001465 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1466 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001467}
1468
Keith Packard4e634382011-08-06 10:39:45 -07001469static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1470 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001471{
1472 if ((val & DP_PORT_EN) == 0)
1473 return false;
1474
1475 if (HAS_PCH_CPT(dev_priv->dev)) {
1476 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1477 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1478 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1479 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001480 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1481 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1482 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001483 } else {
1484 if ((val & DP_PIPE_MASK) != (pipe << 30))
1485 return false;
1486 }
1487 return true;
1488}
1489
Keith Packard1519b992011-08-06 10:35:34 -07001490static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1491 enum pipe pipe, u32 val)
1492{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001493 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001494 return false;
1495
1496 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001497 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001498 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001499 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1500 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1501 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001502 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001503 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001504 return false;
1505 }
1506 return true;
1507}
1508
1509static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1510 enum pipe pipe, u32 val)
1511{
1512 if ((val & LVDS_PORT_EN) == 0)
1513 return false;
1514
1515 if (HAS_PCH_CPT(dev_priv->dev)) {
1516 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1517 return false;
1518 } else {
1519 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1520 return false;
1521 }
1522 return true;
1523}
1524
1525static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1526 enum pipe pipe, u32 val)
1527{
1528 if ((val & ADPA_DAC_ENABLE) == 0)
1529 return false;
1530 if (HAS_PCH_CPT(dev_priv->dev)) {
1531 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1532 return false;
1533 } else {
1534 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1535 return false;
1536 }
1537 return true;
1538}
1539
Jesse Barnes291906f2011-02-02 12:28:03 -08001540static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001541 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001542{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001543 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001544 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001545 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001546 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001547
Rob Clarke2c719b2014-12-15 13:56:32 -05001548 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001549 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001550 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001551}
1552
1553static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1554 enum pipe pipe, int reg)
1555{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001556 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001557 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001558 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001559 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001560
Rob Clarke2c719b2014-12-15 13:56:32 -05001561 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001562 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001563 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001564}
1565
1566static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1567 enum pipe pipe)
1568{
1569 int reg;
1570 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001571
Keith Packardf0575e92011-07-25 22:12:43 -07001572 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1574 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001575
1576 reg = PCH_ADPA;
1577 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001578 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001579 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001580 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001581
1582 reg = PCH_LVDS;
1583 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001584 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001585 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001586 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001587
Paulo Zanonie2debe92013-02-18 19:00:27 -03001588 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1590 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001591}
1592
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001593static void intel_init_dpio(struct drm_device *dev)
1594{
1595 struct drm_i915_private *dev_priv = dev->dev_private;
1596
1597 if (!IS_VALLEYVIEW(dev))
1598 return;
1599
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001600 /*
1601 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1602 * CHV x1 PHY (DP/HDMI D)
1603 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1604 */
1605 if (IS_CHERRYVIEW(dev)) {
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1607 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1608 } else {
1609 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1610 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001611}
1612
Ville Syrjäläd288f652014-10-28 13:20:22 +02001613static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001614 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001615{
Daniel Vetter426115c2013-07-11 22:13:42 +02001616 struct drm_device *dev = crtc->base.dev;
1617 struct drm_i915_private *dev_priv = dev->dev_private;
1618 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001619 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620
Daniel Vetter426115c2013-07-11 22:13:42 +02001621 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001622
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001623 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001624 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1625
1626 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001627 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001628 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001629
Daniel Vetter426115c2013-07-11 22:13:42 +02001630 I915_WRITE(reg, dpll);
1631 POSTING_READ(reg);
1632 udelay(150);
1633
1634 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1635 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1636
Ville Syrjäläd288f652014-10-28 13:20:22 +02001637 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001638 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001639
1640 /* We do this three times for luck */
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 */
Daniel Vetter426115c2013-07-11 22:13:42 +02001647 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001648 POSTING_READ(reg);
1649 udelay(150); /* wait for warmup */
1650}
1651
Ville Syrjäläd288f652014-10-28 13:20:22 +02001652static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001653 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001654{
1655 struct drm_device *dev = crtc->base.dev;
1656 struct drm_i915_private *dev_priv = dev->dev_private;
1657 int pipe = crtc->pipe;
1658 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001659 u32 tmp;
1660
1661 assert_pipe_disabled(dev_priv, crtc->pipe);
1662
1663 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1664
Ville Syrjäläa5805162015-05-26 20:42:30 +03001665 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001666
1667 /* Enable back the 10bit clock to display controller */
1668 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1669 tmp |= DPIO_DCLKP_EN;
1670 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1671
Ville Syrjälä54433e92015-05-26 20:42:31 +03001672 mutex_unlock(&dev_priv->sb_lock);
1673
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001674 /*
1675 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1676 */
1677 udelay(1);
1678
1679 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001680 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001681
1682 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001683 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001684 DRM_ERROR("PLL %d failed to lock\n", pipe);
1685
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001686 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001687 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001688 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001689}
1690
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001691static int intel_num_dvo_pipes(struct drm_device *dev)
1692{
1693 struct intel_crtc *crtc;
1694 int count = 0;
1695
1696 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001697 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001698 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001699
1700 return count;
1701}
1702
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001703static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001704{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001705 struct drm_device *dev = crtc->base.dev;
1706 struct drm_i915_private *dev_priv = dev->dev_private;
1707 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001708 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001709
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001710 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001711
1712 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001713 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001714
1715 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001716 if (IS_MOBILE(dev) && !IS_I830(dev))
1717 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001718
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001719 /* Enable DVO 2x clock on both PLLs if necessary */
1720 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1721 /*
1722 * It appears to be important that we don't enable this
1723 * for the current pipe before otherwise configuring the
1724 * PLL. No idea how this should be handled if multiple
1725 * DVO outputs are enabled simultaneosly.
1726 */
1727 dpll |= DPLL_DVO_2X_MODE;
1728 I915_WRITE(DPLL(!crtc->pipe),
1729 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1730 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001731
1732 /* Wait for the clocks to stabilize. */
1733 POSTING_READ(reg);
1734 udelay(150);
1735
1736 if (INTEL_INFO(dev)->gen >= 4) {
1737 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001738 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001739 } else {
1740 /* The pixel multiplier can only be updated once the
1741 * DPLL is enabled and the clocks are stable.
1742 *
1743 * So write it again.
1744 */
1745 I915_WRITE(reg, dpll);
1746 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001747
1748 /* We do this three times for luck */
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 */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001755 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001756 POSTING_READ(reg);
1757 udelay(150); /* wait for warmup */
1758}
1759
1760/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001761 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001762 * @dev_priv: i915 private structure
1763 * @pipe: pipe PLL to disable
1764 *
1765 * Disable the PLL for @pipe, making sure the pipe is off first.
1766 *
1767 * Note! This is for pre-ILK only.
1768 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001769static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001770{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001771 struct drm_device *dev = crtc->base.dev;
1772 struct drm_i915_private *dev_priv = dev->dev_private;
1773 enum pipe pipe = crtc->pipe;
1774
1775 /* Disable DVO 2x clock on both PLLs if necessary */
1776 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001777 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001778 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001779 I915_WRITE(DPLL(PIPE_B),
1780 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1781 I915_WRITE(DPLL(PIPE_A),
1782 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1783 }
1784
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001785 /* Don't disable pipe or pipe PLLs if needed */
1786 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1787 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001788 return;
1789
1790 /* Make sure the pipe isn't still relying on us */
1791 assert_pipe_disabled(dev_priv, pipe);
1792
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001793 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001794 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001795}
1796
Jesse Barnesf6071162013-10-01 10:41:38 -07001797static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1798{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001799 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001800
1801 /* Make sure the pipe isn't still relying on us */
1802 assert_pipe_disabled(dev_priv, pipe);
1803
Imre Deake5cbfbf2014-01-09 17:08:16 +02001804 /*
1805 * Leave integrated clock source and reference clock enabled for pipe B.
1806 * The latter is needed for VGA hotplug / manual detection.
1807 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001808 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001809 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001810 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001811 I915_WRITE(DPLL(pipe), val);
1812 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001813
1814}
1815
1816static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1817{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001818 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001819 u32 val;
1820
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001821 /* Make sure the pipe isn't still relying on us */
1822 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001823
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001824 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001825 val = DPLL_SSC_REF_CLK_CHV |
1826 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001827 if (pipe != PIPE_A)
1828 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1829 I915_WRITE(DPLL(pipe), val);
1830 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001831
Ville Syrjäläa5805162015-05-26 20:42:30 +03001832 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001833
1834 /* Disable 10bit clock to display controller */
1835 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1836 val &= ~DPIO_DCLKP_EN;
1837 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1838
Ville Syrjälä61407f62014-05-27 16:32:55 +03001839 /* disable left/right clock distribution */
1840 if (pipe != PIPE_B) {
1841 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1842 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1843 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1844 } else {
1845 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1846 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1847 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1848 }
1849
Ville Syrjäläa5805162015-05-26 20:42:30 +03001850 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001851}
1852
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001853void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001854 struct intel_digital_port *dport,
1855 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856{
1857 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001858 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001860 switch (dport->port) {
1861 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001862 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001863 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001864 break;
1865 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001866 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001867 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001868 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001869 break;
1870 case PORT_D:
1871 port_mask = DPLL_PORTD_READY_MASK;
1872 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001873 break;
1874 default:
1875 BUG();
1876 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001877
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001878 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1879 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1880 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001881}
1882
Daniel Vetterb14b1052014-04-24 23:55:13 +02001883static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1884{
1885 struct drm_device *dev = crtc->base.dev;
1886 struct drm_i915_private *dev_priv = dev->dev_private;
1887 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1888
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001889 if (WARN_ON(pll == NULL))
1890 return;
1891
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001892 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001893 if (pll->active == 0) {
1894 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1895 WARN_ON(pll->on);
1896 assert_shared_dpll_disabled(dev_priv, pll);
1897
1898 pll->mode_set(dev_priv, pll);
1899 }
1900}
1901
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001902/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001903 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001904 * @dev_priv: i915 private structure
1905 * @pipe: pipe PLL to enable
1906 *
1907 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1908 * drives the transcoder clock.
1909 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001910static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001911{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001912 struct drm_device *dev = crtc->base.dev;
1913 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001914 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001915
Daniel Vetter87a875b2013-06-05 13:34:19 +02001916 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001917 return;
1918
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001919 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001920 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001921
Damien Lespiau74dd6922014-07-29 18:06:17 +01001922 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001923 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001924 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001925
Daniel Vettercdbd2312013-06-05 13:34:03 +02001926 if (pll->active++) {
1927 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001928 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001929 return;
1930 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001931 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001932
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001933 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1934
Daniel Vetter46edb022013-06-05 13:34:12 +02001935 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001936 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001937 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001938}
1939
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001940static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001941{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001942 struct drm_device *dev = crtc->base.dev;
1943 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001944 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001945
Jesse Barnes92f25842011-01-04 15:09:34 -08001946 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001947 BUG_ON(INTEL_INFO(dev)->gen < 5);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001948 if (pll == NULL)
1949 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001951 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001952 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001953
Daniel Vetter46edb022013-06-05 13:34:12 +02001954 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1955 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001956 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001957
Chris Wilson48da64a2012-05-13 20:16:12 +01001958 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001959 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001960 return;
1961 }
1962
Daniel Vettere9d69442013-06-05 13:34:15 +02001963 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001964 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001965 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001966 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001967
Daniel Vetter46edb022013-06-05 13:34:12 +02001968 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001969 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001970 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001971
1972 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001973}
1974
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001975static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1976 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001977{
Daniel Vetter23670b322012-11-01 09:15:30 +01001978 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001979 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001980 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001981 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001984 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001985
1986 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001987 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001988 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001989
1990 /* FDI must be feeding us bits for PCH ports */
1991 assert_fdi_tx_enabled(dev_priv, pipe);
1992 assert_fdi_rx_enabled(dev_priv, pipe);
1993
Daniel Vetter23670b322012-11-01 09:15:30 +01001994 if (HAS_PCH_CPT(dev)) {
1995 /* Workaround: Set the timing override bit before enabling the
1996 * pch transcoder. */
1997 reg = TRANS_CHICKEN2(pipe);
1998 val = I915_READ(reg);
1999 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2000 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03002001 }
Daniel Vetter23670b322012-11-01 09:15:30 +01002002
Daniel Vetterab9412b2013-05-03 11:49:46 +02002003 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002004 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002005 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002006
2007 if (HAS_PCH_IBX(dev_priv->dev)) {
2008 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002009 * Make the BPC in transcoder be consistent with
2010 * that in pipeconf reg. For HDMI we must use 8bpc
2011 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07002012 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002013 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002014 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2015 val |= PIPECONF_8BPC;
2016 else
2017 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002018 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002019
2020 val &= ~TRANS_INTERLACE_MASK;
2021 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002022 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002023 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002024 val |= TRANS_LEGACY_INTERLACED_ILK;
2025 else
2026 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002027 else
2028 val |= TRANS_PROGRESSIVE;
2029
Jesse Barnes040484a2011-01-03 12:14:26 -08002030 I915_WRITE(reg, val | TRANS_ENABLE);
2031 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002032 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002033}
2034
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002036 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002037{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002038 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002039
2040 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002041 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002042
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002044 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002045 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002046
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002047 /* Workaround: set timing override bit. */
2048 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002049 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002050 I915_WRITE(_TRANSA_CHICKEN2, val);
2051
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002052 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002053 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002054
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002055 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2056 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002057 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002058 else
2059 val |= TRANS_PROGRESSIVE;
2060
Daniel Vetterab9412b2013-05-03 11:49:46 +02002061 I915_WRITE(LPT_TRANSCONF, val);
2062 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002063 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002064}
2065
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002066static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2067 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002068{
Daniel Vetter23670b322012-11-01 09:15:30 +01002069 struct drm_device *dev = dev_priv->dev;
2070 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002071
2072 /* FDI relies on the transcoder */
2073 assert_fdi_tx_disabled(dev_priv, pipe);
2074 assert_fdi_rx_disabled(dev_priv, pipe);
2075
Jesse Barnes291906f2011-02-02 12:28:03 -08002076 /* Ports must be off as well */
2077 assert_pch_ports_disabled(dev_priv, pipe);
2078
Daniel Vetterab9412b2013-05-03 11:49:46 +02002079 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002080 val = I915_READ(reg);
2081 val &= ~TRANS_ENABLE;
2082 I915_WRITE(reg, val);
2083 /* wait for PCH transcoder off, transcoder state */
2084 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002085 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002086
2087 if (!HAS_PCH_IBX(dev)) {
2088 /* Workaround: Clear the timing override chicken bit again. */
2089 reg = TRANS_CHICKEN2(pipe);
2090 val = I915_READ(reg);
2091 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2092 I915_WRITE(reg, val);
2093 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002094}
2095
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002096static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002097{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002098 u32 val;
2099
Daniel Vetterab9412b2013-05-03 11:49:46 +02002100 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002101 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002102 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002103 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002104 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002105 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002106
2107 /* Workaround: clear timing override bit. */
2108 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002109 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002110 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002111}
2112
2113/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002114 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002115 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002117 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002119 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002120static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121{
Paulo Zanoni03722642014-01-17 13:51:09 -02002122 struct drm_device *dev = crtc->base.dev;
2123 struct drm_i915_private *dev_priv = dev->dev_private;
2124 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002125 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2126 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002127 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002128 int reg;
2129 u32 val;
2130
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002131 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2132
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002133 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002134 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002135 assert_sprites_disabled(dev_priv, pipe);
2136
Paulo Zanoni681e5812012-12-06 11:12:38 -02002137 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002138 pch_transcoder = TRANSCODER_A;
2139 else
2140 pch_transcoder = pipe;
2141
Jesse Barnesb24e7172011-01-04 15:09:30 -08002142 /*
2143 * A pipe without a PLL won't actually be able to drive bits from
2144 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2145 * need the check.
2146 */
Imre Deak50360402015-01-16 00:55:16 -08002147 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002148 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002149 assert_dsi_pll_enabled(dev_priv);
2150 else
2151 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002152 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002153 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002154 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002155 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002156 assert_fdi_tx_pll_enabled(dev_priv,
2157 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002158 }
2159 /* FIXME: assert CPU port conditions for SNB+ */
2160 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002161
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002162 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002164 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002165 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2166 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002167 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002168 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002169
2170 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002171 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172}
2173
2174/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002175 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002176 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002177 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002178 * Disable the pipe of @crtc, making sure that various hardware
2179 * specific requirements are met, if applicable, e.g. plane
2180 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002181 *
2182 * Will wait until the pipe has shut down before returning.
2183 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002184static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002185{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002186 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002187 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002188 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002189 int reg;
2190 u32 val;
2191
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002192 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2193
Jesse Barnesb24e7172011-01-04 15:09:30 -08002194 /*
2195 * Make sure planes won't keep trying to pump pixels to us,
2196 * or we might hang the display.
2197 */
2198 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002199 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002200 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002201
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002202 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002203 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002204 if ((val & PIPECONF_ENABLE) == 0)
2205 return;
2206
Ville Syrjälä67adc642014-08-15 01:21:57 +03002207 /*
2208 * Double wide has implications for planes
2209 * so best keep it disabled when not needed.
2210 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002211 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002212 val &= ~PIPECONF_DOUBLE_WIDE;
2213
2214 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002215 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2216 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002217 val &= ~PIPECONF_ENABLE;
2218
2219 I915_WRITE(reg, val);
2220 if ((val & PIPECONF_ENABLE) == 0)
2221 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002222}
2223
Chris Wilson693db182013-03-05 14:52:39 +00002224static bool need_vtd_wa(struct drm_device *dev)
2225{
2226#ifdef CONFIG_INTEL_IOMMU
2227 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2228 return true;
2229#endif
2230 return false;
2231}
2232
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002233unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002234intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2235 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002236{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002237 unsigned int tile_height;
2238 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002239
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002240 switch (fb_format_modifier) {
2241 case DRM_FORMAT_MOD_NONE:
2242 tile_height = 1;
2243 break;
2244 case I915_FORMAT_MOD_X_TILED:
2245 tile_height = IS_GEN2(dev) ? 16 : 8;
2246 break;
2247 case I915_FORMAT_MOD_Y_TILED:
2248 tile_height = 32;
2249 break;
2250 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002251 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2252 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002253 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002254 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002255 tile_height = 64;
2256 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002257 case 2:
2258 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002259 tile_height = 32;
2260 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002261 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002262 tile_height = 16;
2263 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002264 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002265 WARN_ONCE(1,
2266 "128-bit pixels are not supported for display!");
2267 tile_height = 16;
2268 break;
2269 }
2270 break;
2271 default:
2272 MISSING_CASE(fb_format_modifier);
2273 tile_height = 1;
2274 break;
2275 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002276
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002277 return tile_height;
2278}
2279
2280unsigned int
2281intel_fb_align_height(struct drm_device *dev, unsigned int height,
2282 uint32_t pixel_format, uint64_t fb_format_modifier)
2283{
2284 return ALIGN(height, intel_tile_height(dev, pixel_format,
2285 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002286}
2287
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002288static int
2289intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2290 const struct drm_plane_state *plane_state)
2291{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002292 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002293 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002294
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002295 *view = i915_ggtt_view_normal;
2296
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002297 if (!plane_state)
2298 return 0;
2299
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002300 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002301 return 0;
2302
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002303 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002304
2305 info->height = fb->height;
2306 info->pixel_format = fb->pixel_format;
2307 info->pitch = fb->pitches[0];
2308 info->fb_modifier = fb->modifier[0];
2309
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002310 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2311 fb->modifier[0]);
2312 tile_pitch = PAGE_SIZE / tile_height;
2313 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2314 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2315 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2316
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002317 return 0;
2318}
2319
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002320static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2321{
2322 if (INTEL_INFO(dev_priv)->gen >= 9)
2323 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002324 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2325 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002326 return 128 * 1024;
2327 else if (INTEL_INFO(dev_priv)->gen >= 4)
2328 return 4 * 1024;
2329 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002330 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002331}
2332
Chris Wilson127bd2a2010-07-23 23:32:05 +01002333int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002334intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2335 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002336 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002337 struct intel_engine_cs *pipelined,
2338 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002339{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002340 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002341 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002342 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002343 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002344 u32 alignment;
2345 int ret;
2346
Matt Roperebcdd392014-07-09 16:22:11 -07002347 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2348
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002349 switch (fb->modifier[0]) {
2350 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002351 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002352 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002353 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002354 if (INTEL_INFO(dev)->gen >= 9)
2355 alignment = 256 * 1024;
2356 else {
2357 /* pin() will align the object as required by fence */
2358 alignment = 0;
2359 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002360 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002361 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002362 case I915_FORMAT_MOD_Yf_TILED:
2363 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2364 "Y tiling bo slipped through, driver bug!\n"))
2365 return -EINVAL;
2366 alignment = 1 * 1024 * 1024;
2367 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002368 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002369 MISSING_CASE(fb->modifier[0]);
2370 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002371 }
2372
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002373 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2374 if (ret)
2375 return ret;
2376
Chris Wilson693db182013-03-05 14:52:39 +00002377 /* Note that the w/a also requires 64 PTE of padding following the
2378 * bo. We currently fill all unused PTE with the shadow page and so
2379 * we should always have valid PTE following the scanout preventing
2380 * the VT-d warning.
2381 */
2382 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2383 alignment = 256 * 1024;
2384
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002385 /*
2386 * Global gtt pte registers are special registers which actually forward
2387 * writes to a chunk of system memory. Which means that there is no risk
2388 * that the register values disappear as soon as we call
2389 * intel_runtime_pm_put(), so it is correct to wrap only the
2390 * pin/unpin/fence and not more.
2391 */
2392 intel_runtime_pm_get(dev_priv);
2393
Chris Wilsonce453d82011-02-21 14:43:56 +00002394 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002395 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002396 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002397 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002398 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002399
2400 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2401 * fence, whereas 965+ only requires a fence if using
2402 * framebuffer compression. For simplicity, we always install
2403 * a fence as the cost is not that onerous.
2404 */
Chris Wilson06d98132012-04-17 15:31:24 +01002405 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002406 if (ret)
2407 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002408
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002409 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002410
Chris Wilsonce453d82011-02-21 14:43:56 +00002411 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002412 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002413 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002414
2415err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002416 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002417err_interruptible:
2418 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002419 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002420 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002421}
2422
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002423static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2424 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002425{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002426 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002427 struct i915_ggtt_view view;
2428 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002429
Matt Roperebcdd392014-07-09 16:22:11 -07002430 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2431
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002432 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2433 WARN_ONCE(ret, "Couldn't get view from plane state!");
2434
Chris Wilson1690e1e2011-12-14 13:57:08 +01002435 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002436 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002437}
2438
Daniel Vetterc2c75132012-07-05 12:17:30 +02002439/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2440 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002441unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2442 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002443 unsigned int tiling_mode,
2444 unsigned int cpp,
2445 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002446{
Chris Wilsonbc752862013-02-21 20:04:31 +00002447 if (tiling_mode != I915_TILING_NONE) {
2448 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002449
Chris Wilsonbc752862013-02-21 20:04:31 +00002450 tile_rows = *y / 8;
2451 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002452
Chris Wilsonbc752862013-02-21 20:04:31 +00002453 tiles = *x / (512/cpp);
2454 *x %= 512/cpp;
2455
2456 return tile_rows * pitch * 8 + tiles * 4096;
2457 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002458 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002459 unsigned int offset;
2460
2461 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002462 *y = (offset & alignment) / pitch;
2463 *x = ((offset & alignment) - *y * pitch) / cpp;
2464 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002465 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002466}
2467
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002468static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002469{
2470 switch (format) {
2471 case DISPPLANE_8BPP:
2472 return DRM_FORMAT_C8;
2473 case DISPPLANE_BGRX555:
2474 return DRM_FORMAT_XRGB1555;
2475 case DISPPLANE_BGRX565:
2476 return DRM_FORMAT_RGB565;
2477 default:
2478 case DISPPLANE_BGRX888:
2479 return DRM_FORMAT_XRGB8888;
2480 case DISPPLANE_RGBX888:
2481 return DRM_FORMAT_XBGR8888;
2482 case DISPPLANE_BGRX101010:
2483 return DRM_FORMAT_XRGB2101010;
2484 case DISPPLANE_RGBX101010:
2485 return DRM_FORMAT_XBGR2101010;
2486 }
2487}
2488
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002489static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2490{
2491 switch (format) {
2492 case PLANE_CTL_FORMAT_RGB_565:
2493 return DRM_FORMAT_RGB565;
2494 default:
2495 case PLANE_CTL_FORMAT_XRGB_8888:
2496 if (rgb_order) {
2497 if (alpha)
2498 return DRM_FORMAT_ABGR8888;
2499 else
2500 return DRM_FORMAT_XBGR8888;
2501 } else {
2502 if (alpha)
2503 return DRM_FORMAT_ARGB8888;
2504 else
2505 return DRM_FORMAT_XRGB8888;
2506 }
2507 case PLANE_CTL_FORMAT_XRGB_2101010:
2508 if (rgb_order)
2509 return DRM_FORMAT_XBGR2101010;
2510 else
2511 return DRM_FORMAT_XRGB2101010;
2512 }
2513}
2514
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002515static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002516intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2517 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002518{
2519 struct drm_device *dev = crtc->base.dev;
2520 struct drm_i915_gem_object *obj = NULL;
2521 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002522 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002523 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2524 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2525 PAGE_SIZE);
2526
2527 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002528
Chris Wilsonff2652e2014-03-10 08:07:02 +00002529 if (plane_config->size == 0)
2530 return false;
2531
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002532 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2533 base_aligned,
2534 base_aligned,
2535 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002537 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002538
Damien Lespiau49af4492015-01-20 12:51:44 +00002539 obj->tiling_mode = plane_config->tiling;
2540 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002541 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002542
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002543 mode_cmd.pixel_format = fb->pixel_format;
2544 mode_cmd.width = fb->width;
2545 mode_cmd.height = fb->height;
2546 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002547 mode_cmd.modifier[0] = fb->modifier[0];
2548 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002549
2550 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002551 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002552 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002553 DRM_DEBUG_KMS("intel fb init failed\n");
2554 goto out_unref_obj;
2555 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002556 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557
Daniel Vetterf6936e22015-03-26 12:17:05 +01002558 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560
2561out_unref_obj:
2562 drm_gem_object_unreference(&obj->base);
2563 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002564 return false;
2565}
2566
Matt Roperafd65eb2015-02-03 13:10:04 -08002567/* Update plane->state->fb to match plane->fb after driver-internal updates */
2568static void
2569update_state_fb(struct drm_plane *plane)
2570{
2571 if (plane->fb == plane->state->fb)
2572 return;
2573
2574 if (plane->state->fb)
2575 drm_framebuffer_unreference(plane->state->fb);
2576 plane->state->fb = plane->fb;
2577 if (plane->state->fb)
2578 drm_framebuffer_reference(plane->state->fb);
2579}
2580
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002581static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002582intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2583 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002584{
2585 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002586 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587 struct drm_crtc *c;
2588 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002589 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002590 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002591 struct drm_plane_state *plane_state = primary->state;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002592 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002593
Damien Lespiau2d140302015-02-05 17:22:18 +00002594 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595 return;
2596
Daniel Vetterf6936e22015-03-26 12:17:05 +01002597 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002598 fb = &plane_config->fb->base;
2599 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002600 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002601
Damien Lespiau2d140302015-02-05 17:22:18 +00002602 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002603
2604 /*
2605 * Failed to alloc the obj, check to see if we should share
2606 * an fb with another CRTC instead
2607 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002608 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002609 i = to_intel_crtc(c);
2610
2611 if (c == &intel_crtc->base)
2612 continue;
2613
Matt Roper2ff8fde2014-07-08 07:50:07 -07002614 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002615 continue;
2616
Daniel Vetter88595ac2015-03-26 12:42:24 +01002617 fb = c->primary->fb;
2618 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002619 continue;
2620
Daniel Vetter88595ac2015-03-26 12:42:24 +01002621 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002622 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002623 drm_framebuffer_reference(fb);
2624 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002625 }
2626 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002627
2628 return;
2629
2630valid_fb:
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002631 plane_state->src_x = plane_state->src_y = 0;
2632 plane_state->src_w = fb->width << 16;
2633 plane_state->src_h = fb->height << 16;
2634
2635 plane_state->crtc_x = plane_state->src_y = 0;
2636 plane_state->crtc_w = fb->width;
2637 plane_state->crtc_h = fb->height;
2638
Daniel Vetter88595ac2015-03-26 12:42:24 +01002639 obj = intel_fb_obj(fb);
2640 if (obj->tiling_mode != I915_TILING_NONE)
2641 dev_priv->preserve_bios_swizzle = true;
2642
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002643 drm_framebuffer_reference(fb);
2644 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002645 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002646 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002647 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002648}
2649
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002650static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2651 struct drm_framebuffer *fb,
2652 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002653{
2654 struct drm_device *dev = crtc->dev;
2655 struct drm_i915_private *dev_priv = dev->dev_private;
2656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002657 struct drm_plane *primary = crtc->primary;
2658 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002659 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002660 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002661 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002662 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002663 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302664 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002665
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002666 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002667 I915_WRITE(reg, 0);
2668 if (INTEL_INFO(dev)->gen >= 4)
2669 I915_WRITE(DSPSURF(plane), 0);
2670 else
2671 I915_WRITE(DSPADDR(plane), 0);
2672 POSTING_READ(reg);
2673 return;
2674 }
2675
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002676 obj = intel_fb_obj(fb);
2677 if (WARN_ON(obj == NULL))
2678 return;
2679
2680 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2681
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002682 dspcntr = DISPPLANE_GAMMA_ENABLE;
2683
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002684 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002685
2686 if (INTEL_INFO(dev)->gen < 4) {
2687 if (intel_crtc->pipe == PIPE_B)
2688 dspcntr |= DISPPLANE_SEL_PIPE_B;
2689
2690 /* pipesrc and dspsize control the size that is scaled from,
2691 * which should always be the user's requested size.
2692 */
2693 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002694 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2695 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002696 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002697 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2698 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002699 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2700 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002701 I915_WRITE(PRIMPOS(plane), 0);
2702 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002703 }
2704
Ville Syrjälä57779d02012-10-31 17:50:14 +02002705 switch (fb->pixel_format) {
2706 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002707 dspcntr |= DISPPLANE_8BPP;
2708 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002709 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002710 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002711 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002712 case DRM_FORMAT_RGB565:
2713 dspcntr |= DISPPLANE_BGRX565;
2714 break;
2715 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002716 dspcntr |= DISPPLANE_BGRX888;
2717 break;
2718 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002719 dspcntr |= DISPPLANE_RGBX888;
2720 break;
2721 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002722 dspcntr |= DISPPLANE_BGRX101010;
2723 break;
2724 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002725 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002726 break;
2727 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002728 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002729 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002730
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002731 if (INTEL_INFO(dev)->gen >= 4 &&
2732 obj->tiling_mode != I915_TILING_NONE)
2733 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002734
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002735 if (IS_G4X(dev))
2736 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2737
Ville Syrjäläb98971272014-08-27 16:51:22 +03002738 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002739
Daniel Vetterc2c75132012-07-05 12:17:30 +02002740 if (INTEL_INFO(dev)->gen >= 4) {
2741 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002742 intel_gen4_compute_page_offset(dev_priv,
2743 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002744 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002745 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002746 linear_offset -= intel_crtc->dspaddr_offset;
2747 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002748 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002749 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002750
Matt Roper8e7d6882015-01-21 16:35:41 -08002751 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302752 dspcntr |= DISPPLANE_ROTATE_180;
2753
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002754 x += (intel_crtc->config->pipe_src_w - 1);
2755 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302756
2757 /* Finding the last pixel of the last line of the display
2758 data and adding to linear_offset*/
2759 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002760 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2761 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302762 }
2763
2764 I915_WRITE(reg, dspcntr);
2765
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002766 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002767 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002768 I915_WRITE(DSPSURF(plane),
2769 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002770 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002771 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002772 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002773 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002774 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002775}
2776
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002777static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2778 struct drm_framebuffer *fb,
2779 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780{
2781 struct drm_device *dev = crtc->dev;
2782 struct drm_i915_private *dev_priv = dev->dev_private;
2783 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002784 struct drm_plane *primary = crtc->primary;
2785 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002786 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002787 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002788 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002789 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002790 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302791 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002792
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002793 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002794 I915_WRITE(reg, 0);
2795 I915_WRITE(DSPSURF(plane), 0);
2796 POSTING_READ(reg);
2797 return;
2798 }
2799
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002800 obj = intel_fb_obj(fb);
2801 if (WARN_ON(obj == NULL))
2802 return;
2803
2804 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2805
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002806 dspcntr = DISPPLANE_GAMMA_ENABLE;
2807
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002808 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002809
2810 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2811 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2812
Ville Syrjälä57779d02012-10-31 17:50:14 +02002813 switch (fb->pixel_format) {
2814 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002815 dspcntr |= DISPPLANE_8BPP;
2816 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002817 case DRM_FORMAT_RGB565:
2818 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002819 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002820 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002821 dspcntr |= DISPPLANE_BGRX888;
2822 break;
2823 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002824 dspcntr |= DISPPLANE_RGBX888;
2825 break;
2826 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002827 dspcntr |= DISPPLANE_BGRX101010;
2828 break;
2829 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002830 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002831 break;
2832 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002833 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002834 }
2835
2836 if (obj->tiling_mode != I915_TILING_NONE)
2837 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002838
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002839 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002840 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002841
Ville Syrjäläb98971272014-08-27 16:51:22 +03002842 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002843 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002844 intel_gen4_compute_page_offset(dev_priv,
2845 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002846 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002847 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002848 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002849 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302850 dspcntr |= DISPPLANE_ROTATE_180;
2851
2852 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002853 x += (intel_crtc->config->pipe_src_w - 1);
2854 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302855
2856 /* Finding the last pixel of the last line of the display
2857 data and adding to linear_offset*/
2858 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002859 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2860 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302861 }
2862 }
2863
2864 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002865
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002866 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002867 I915_WRITE(DSPSURF(plane),
2868 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002869 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002870 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2871 } else {
2872 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2873 I915_WRITE(DSPLINOFF(plane), linear_offset);
2874 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002875 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002876}
2877
Damien Lespiaub3218032015-02-27 11:15:18 +00002878u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2879 uint32_t pixel_format)
2880{
2881 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2882
2883 /*
2884 * The stride is either expressed as a multiple of 64 bytes
2885 * chunks for linear buffers or in number of tiles for tiled
2886 * buffers.
2887 */
2888 switch (fb_modifier) {
2889 case DRM_FORMAT_MOD_NONE:
2890 return 64;
2891 case I915_FORMAT_MOD_X_TILED:
2892 if (INTEL_INFO(dev)->gen == 2)
2893 return 128;
2894 return 512;
2895 case I915_FORMAT_MOD_Y_TILED:
2896 /* No need to check for old gens and Y tiling since this is
2897 * about the display engine and those will be blocked before
2898 * we get here.
2899 */
2900 return 128;
2901 case I915_FORMAT_MOD_Yf_TILED:
2902 if (bits_per_pixel == 8)
2903 return 64;
2904 else
2905 return 128;
2906 default:
2907 MISSING_CASE(fb_modifier);
2908 return 64;
2909 }
2910}
2911
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002912unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2913 struct drm_i915_gem_object *obj)
2914{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002915 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002916
2917 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002918 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002919
2920 return i915_gem_obj_ggtt_offset_view(obj, view);
2921}
2922
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002923static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2924{
2925 struct drm_device *dev = intel_crtc->base.dev;
2926 struct drm_i915_private *dev_priv = dev->dev_private;
2927
2928 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2929 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2930 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
2931 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2932 intel_crtc->base.base.id, intel_crtc->pipe, id);
2933}
2934
Chandra Kondurua1b22782015-04-07 15:28:45 -07002935/*
2936 * This function detaches (aka. unbinds) unused scalers in hardware
2937 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002938static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002939{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002940 struct intel_crtc_scaler_state *scaler_state;
2941 int i;
2942
Chandra Kondurua1b22782015-04-07 15:28:45 -07002943 scaler_state = &intel_crtc->config->scaler_state;
2944
2945 /* loop through and disable scalers that aren't in use */
2946 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002947 if (!scaler_state->scalers[i].in_use)
2948 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002949 }
2950}
2951
Chandra Konduru6156a452015-04-27 13:48:39 -07002952u32 skl_plane_ctl_format(uint32_t pixel_format)
2953{
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002955 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 /*
2964 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2965 * to be already pre-multiplied. We need to add a knob (or a different
2966 * DRM_FORMAT) for user-space to configure that.
2967 */
2968 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002969 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002975 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002976 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002979 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002981 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002982 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002983 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002984 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002985 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002986 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002987 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002988 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002989
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002990 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002991}
2992
2993u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2994{
Chandra Konduru6156a452015-04-27 13:48:39 -07002995 switch (fb_modifier) {
2996 case DRM_FORMAT_MOD_NONE:
2997 break;
2998 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002999 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003001 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003002 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003003 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003004 default:
3005 MISSING_CASE(fb_modifier);
3006 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003007
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003008 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003009}
3010
3011u32 skl_plane_ctl_rotation(unsigned int rotation)
3012{
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 switch (rotation) {
3014 case BIT(DRM_ROTATE_0):
3015 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303016 /*
3017 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3018 * while i915 HW rotation is clockwise, thats why this swapping.
3019 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003020 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303021 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003022 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003023 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003024 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303025 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003026 default:
3027 MISSING_CASE(rotation);
3028 }
3029
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003030 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003031}
3032
Damien Lespiau70d21f02013-07-03 21:06:04 +01003033static void skylake_update_primary_plane(struct drm_crtc *crtc,
3034 struct drm_framebuffer *fb,
3035 int x, int y)
3036{
3037 struct drm_device *dev = crtc->dev;
3038 struct drm_i915_private *dev_priv = dev->dev_private;
3039 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003040 struct drm_plane *plane = crtc->primary;
3041 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003042 struct drm_i915_gem_object *obj;
3043 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303044 u32 plane_ctl, stride_div, stride;
3045 u32 tile_height, plane_offset, plane_size;
3046 unsigned int rotation;
3047 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003048 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003049 struct intel_crtc_state *crtc_state = intel_crtc->config;
3050 struct intel_plane_state *plane_state;
3051 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3052 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3053 int scaler_id = -1;
3054
Chandra Konduru6156a452015-04-27 13:48:39 -07003055 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003056
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003057 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003058 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3059 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3060 POSTING_READ(PLANE_CTL(pipe, 0));
3061 return;
3062 }
3063
3064 plane_ctl = PLANE_CTL_ENABLE |
3065 PLANE_CTL_PIPE_GAMMA_ENABLE |
3066 PLANE_CTL_PIPE_CSC_ENABLE;
3067
Chandra Konduru6156a452015-04-27 13:48:39 -07003068 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3069 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003070 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303071
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303072 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003073 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003074
Damien Lespiaub3218032015-02-27 11:15:18 +00003075 obj = intel_fb_obj(fb);
3076 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3077 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303078 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3079
Chandra Konduru6156a452015-04-27 13:48:39 -07003080 /*
3081 * FIXME: intel_plane_state->src, dst aren't set when transitional
3082 * update_plane helpers are called from legacy paths.
3083 * Once full atomic crtc is available, below check can be avoided.
3084 */
3085 if (drm_rect_width(&plane_state->src)) {
3086 scaler_id = plane_state->scaler_id;
3087 src_x = plane_state->src.x1 >> 16;
3088 src_y = plane_state->src.y1 >> 16;
3089 src_w = drm_rect_width(&plane_state->src) >> 16;
3090 src_h = drm_rect_height(&plane_state->src) >> 16;
3091 dst_x = plane_state->dst.x1;
3092 dst_y = plane_state->dst.y1;
3093 dst_w = drm_rect_width(&plane_state->dst);
3094 dst_h = drm_rect_height(&plane_state->dst);
3095
3096 WARN_ON(x != src_x || y != src_y);
3097 } else {
3098 src_w = intel_crtc->config->pipe_src_w;
3099 src_h = intel_crtc->config->pipe_src_h;
3100 }
3101
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303102 if (intel_rotation_90_or_270(rotation)) {
3103 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003104 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303105 fb->modifier[0]);
3106 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003107 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303108 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003109 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303110 } else {
3111 stride = fb->pitches[0] / stride_div;
3112 x_offset = x;
3113 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003114 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303115 }
3116 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003117
Damien Lespiau70d21f02013-07-03 21:06:04 +01003118 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303119 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3120 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3121 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003122
3123 if (scaler_id >= 0) {
3124 uint32_t ps_ctrl = 0;
3125
3126 WARN_ON(!dst_w || !dst_h);
3127 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3128 crtc_state->scaler_state.scalers[scaler_id].mode;
3129 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3130 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3131 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3132 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3133 I915_WRITE(PLANE_POS(pipe, 0), 0);
3134 } else {
3135 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3136 }
3137
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003138 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003139
3140 POSTING_READ(PLANE_SURF(pipe, 0));
3141}
3142
Jesse Barnes17638cd2011-06-24 12:19:23 -07003143/* Assume fb object is pinned & idle & fenced and just update base pointers */
3144static int
3145intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3146 int x, int y, enum mode_set_atomic state)
3147{
3148 struct drm_device *dev = crtc->dev;
3149 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003150
Paulo Zanoniff2a3112015-07-07 15:26:03 -03003151 if (dev_priv->fbc.disable_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03003152 dev_priv->fbc.disable_fbc(dev_priv);
Jesse Barnes81255562010-08-02 12:07:50 -07003153
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003154 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3155
3156 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003157}
3158
Ville Syrjälä75147472014-11-24 18:28:11 +02003159static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003160{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003161 struct drm_crtc *crtc;
3162
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003163 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003164 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3165 enum plane plane = intel_crtc->plane;
3166
3167 intel_prepare_page_flip(dev, plane);
3168 intel_finish_page_flip_plane(dev, plane);
3169 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003170}
3171
3172static void intel_update_primary_planes(struct drm_device *dev)
3173{
3174 struct drm_i915_private *dev_priv = dev->dev_private;
3175 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003176
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003177 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003178 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3179
Rob Clark51fd3712013-11-19 12:10:12 -05003180 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003181 /*
3182 * FIXME: Once we have proper support for primary planes (and
3183 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003184 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003185 */
Matt Roperf4510a22014-04-01 15:22:40 -07003186 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003187 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003188 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003189 crtc->x,
3190 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003191 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003192 }
3193}
3194
Ville Syrjälä75147472014-11-24 18:28:11 +02003195void intel_prepare_reset(struct drm_device *dev)
3196{
3197 /* no reset support for gen2 */
3198 if (IS_GEN2(dev))
3199 return;
3200
3201 /* reset doesn't touch the display */
3202 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3203 return;
3204
3205 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003206 /*
3207 * Disabling the crtcs gracefully seems nicer. Also the
3208 * g33 docs say we should at least disable all the planes.
3209 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003210 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003211}
3212
3213void intel_finish_reset(struct drm_device *dev)
3214{
3215 struct drm_i915_private *dev_priv = to_i915(dev);
3216
3217 /*
3218 * Flips in the rings will be nuked by the reset,
3219 * so complete all pending flips so that user space
3220 * will get its events and not get stuck.
3221 */
3222 intel_complete_page_flips(dev);
3223
3224 /* no reset support for gen2 */
3225 if (IS_GEN2(dev))
3226 return;
3227
3228 /* reset doesn't touch the display */
3229 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3230 /*
3231 * Flips in the rings have been nuked by the reset,
3232 * so update the base address of all primary
3233 * planes to the the last fb to make sure we're
3234 * showing the correct fb after a reset.
3235 */
3236 intel_update_primary_planes(dev);
3237 return;
3238 }
3239
3240 /*
3241 * The display has been reset as well,
3242 * so need a full re-initialization.
3243 */
3244 intel_runtime_pm_disable_interrupts(dev_priv);
3245 intel_runtime_pm_enable_interrupts(dev_priv);
3246
3247 intel_modeset_init_hw(dev);
3248
3249 spin_lock_irq(&dev_priv->irq_lock);
3250 if (dev_priv->display.hpd_irq_setup)
3251 dev_priv->display.hpd_irq_setup(dev);
3252 spin_unlock_irq(&dev_priv->irq_lock);
3253
3254 intel_modeset_setup_hw_state(dev, true);
3255
3256 intel_hpd_init(dev_priv);
3257
3258 drm_modeset_unlock_all(dev);
3259}
3260
Chris Wilson2e2f3512015-04-27 13:41:14 +01003261static void
Chris Wilson14667a42012-04-03 17:58:35 +01003262intel_finish_fb(struct drm_framebuffer *old_fb)
3263{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003264 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003265 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003266 bool was_interruptible = dev_priv->mm.interruptible;
3267 int ret;
3268
Chris Wilson14667a42012-04-03 17:58:35 +01003269 /* Big Hammer, we also need to ensure that any pending
3270 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3271 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003272 * framebuffer. Note that we rely on userspace rendering
3273 * into the buffer attached to the pipe they are waiting
3274 * on. If not, userspace generates a GPU hang with IPEHR
3275 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003276 *
3277 * This should only fail upon a hung GPU, in which case we
3278 * can safely continue.
3279 */
3280 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003281 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003282 dev_priv->mm.interruptible = was_interruptible;
3283
Chris Wilson2e2f3512015-04-27 13:41:14 +01003284 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003285}
3286
Chris Wilson7d5e3792014-03-04 13:15:08 +00003287static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3288{
3289 struct drm_device *dev = crtc->dev;
3290 struct drm_i915_private *dev_priv = dev->dev_private;
3291 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003292 bool pending;
3293
3294 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3295 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3296 return false;
3297
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003298 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003299 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003300 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003301
3302 return pending;
3303}
3304
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003305static void intel_update_pipe_size(struct intel_crtc *crtc)
3306{
3307 struct drm_device *dev = crtc->base.dev;
3308 struct drm_i915_private *dev_priv = dev->dev_private;
3309 const struct drm_display_mode *adjusted_mode;
3310
3311 if (!i915.fastboot)
3312 return;
3313
3314 /*
3315 * Update pipe size and adjust fitter if needed: the reason for this is
3316 * that in compute_mode_changes we check the native mode (not the pfit
3317 * mode) to see if we can flip rather than do a full mode set. In the
3318 * fastboot case, we'll flip, but if we don't update the pipesrc and
3319 * pfit state, we'll end up with a big fb scanned out into the wrong
3320 * sized surface.
3321 *
3322 * To fix this properly, we need to hoist the checks up into
3323 * compute_mode_changes (or above), check the actual pfit state and
3324 * whether the platform allows pfit disable with pipe active, and only
3325 * then update the pipesrc and pfit state, even on the flip path.
3326 */
3327
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003328 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003329
3330 I915_WRITE(PIPESRC(crtc->pipe),
3331 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3332 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003333 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003334 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3335 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003336 I915_WRITE(PF_CTL(crtc->pipe), 0);
3337 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3338 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3339 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003340 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3341 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003342}
3343
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003344static void intel_fdi_normal_train(struct drm_crtc *crtc)
3345{
3346 struct drm_device *dev = crtc->dev;
3347 struct drm_i915_private *dev_priv = dev->dev_private;
3348 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3349 int pipe = intel_crtc->pipe;
3350 u32 reg, temp;
3351
3352 /* enable normal train */
3353 reg = FDI_TX_CTL(pipe);
3354 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003355 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003356 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3357 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003358 } else {
3359 temp &= ~FDI_LINK_TRAIN_NONE;
3360 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003361 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003362 I915_WRITE(reg, temp);
3363
3364 reg = FDI_RX_CTL(pipe);
3365 temp = I915_READ(reg);
3366 if (HAS_PCH_CPT(dev)) {
3367 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3368 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3369 } else {
3370 temp &= ~FDI_LINK_TRAIN_NONE;
3371 temp |= FDI_LINK_TRAIN_NONE;
3372 }
3373 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3374
3375 /* wait one idle pattern time */
3376 POSTING_READ(reg);
3377 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003378
3379 /* IVB wants error correction enabled */
3380 if (IS_IVYBRIDGE(dev))
3381 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3382 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003383}
3384
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003385/* The FDI link training functions for ILK/Ibexpeak. */
3386static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3387{
3388 struct drm_device *dev = crtc->dev;
3389 struct drm_i915_private *dev_priv = dev->dev_private;
3390 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3391 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003393
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003394 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003395 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003396
Adam Jacksone1a44742010-06-25 15:32:14 -04003397 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3398 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003399 reg = FDI_RX_IMR(pipe);
3400 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003401 temp &= ~FDI_RX_SYMBOL_LOCK;
3402 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003403 I915_WRITE(reg, temp);
3404 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003405 udelay(150);
3406
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003408 reg = FDI_TX_CTL(pipe);
3409 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003410 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003411 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 temp &= ~FDI_LINK_TRAIN_NONE;
3413 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003414 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415
Chris Wilson5eddb702010-09-11 13:48:45 +01003416 reg = FDI_RX_CTL(pipe);
3417 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418 temp &= ~FDI_LINK_TRAIN_NONE;
3419 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3421
3422 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003423 udelay(150);
3424
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003425 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003426 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3427 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3428 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003429
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003431 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003432 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3434
3435 if ((temp & FDI_RX_BIT_LOCK)) {
3436 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003437 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 break;
3439 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003441 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443
3444 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003445 reg = FDI_TX_CTL(pipe);
3446 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 temp &= ~FDI_LINK_TRAIN_NONE;
3448 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 reg = FDI_RX_CTL(pipe);
3452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 temp &= ~FDI_LINK_TRAIN_NONE;
3454 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 I915_WRITE(reg, temp);
3456
3457 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003458 udelay(150);
3459
Chris Wilson5eddb702010-09-11 13:48:45 +01003460 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003461 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3464
3465 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 DRM_DEBUG_KMS("FDI train 2 done.\n");
3468 break;
3469 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003471 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473
3474 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003475
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476}
3477
Akshay Joshi0206e352011-08-16 15:34:10 -04003478static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3480 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3481 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3482 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3483};
3484
3485/* The FDI link training functions for SNB/Cougarpoint. */
3486static void gen6_fdi_link_train(struct drm_crtc *crtc)
3487{
3488 struct drm_device *dev = crtc->dev;
3489 struct drm_i915_private *dev_priv = dev->dev_private;
3490 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3491 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003492 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493
Adam Jacksone1a44742010-06-25 15:32:14 -04003494 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3495 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003496 reg = FDI_RX_IMR(pipe);
3497 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003498 temp &= ~FDI_RX_SYMBOL_LOCK;
3499 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003500 I915_WRITE(reg, temp);
3501
3502 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003503 udelay(150);
3504
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 reg = FDI_TX_CTL(pipe);
3507 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003508 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003509 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003510 temp &= ~FDI_LINK_TRAIN_NONE;
3511 temp |= FDI_LINK_TRAIN_PATTERN_1;
3512 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3513 /* SNB-B */
3514 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003515 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003516
Daniel Vetterd74cf322012-10-26 10:58:13 +02003517 I915_WRITE(FDI_RX_MISC(pipe),
3518 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3519
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 reg = FDI_RX_CTL(pipe);
3521 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522 if (HAS_PCH_CPT(dev)) {
3523 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3524 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3525 } else {
3526 temp &= ~FDI_LINK_TRAIN_NONE;
3527 temp |= FDI_LINK_TRAIN_PATTERN_1;
3528 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003529 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3530
3531 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532 udelay(150);
3533
Akshay Joshi0206e352011-08-16 15:34:10 -04003534 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 reg = FDI_TX_CTL(pipe);
3536 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003537 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3538 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003539 I915_WRITE(reg, temp);
3540
3541 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 udelay(500);
3543
Sean Paulfa37d392012-03-02 12:53:39 -05003544 for (retry = 0; retry < 5; retry++) {
3545 reg = FDI_RX_IIR(pipe);
3546 temp = I915_READ(reg);
3547 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3548 if (temp & FDI_RX_BIT_LOCK) {
3549 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3550 DRM_DEBUG_KMS("FDI train 1 done.\n");
3551 break;
3552 }
3553 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003554 }
Sean Paulfa37d392012-03-02 12:53:39 -05003555 if (retry < 5)
3556 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003557 }
3558 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003559 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560
3561 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 reg = FDI_TX_CTL(pipe);
3563 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 temp &= ~FDI_LINK_TRAIN_NONE;
3565 temp |= FDI_LINK_TRAIN_PATTERN_2;
3566 if (IS_GEN6(dev)) {
3567 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3568 /* SNB-B */
3569 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3570 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003571 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003572
Chris Wilson5eddb702010-09-11 13:48:45 +01003573 reg = FDI_RX_CTL(pipe);
3574 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003575 if (HAS_PCH_CPT(dev)) {
3576 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3577 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3578 } else {
3579 temp &= ~FDI_LINK_TRAIN_NONE;
3580 temp |= FDI_LINK_TRAIN_PATTERN_2;
3581 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003582 I915_WRITE(reg, temp);
3583
3584 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003585 udelay(150);
3586
Akshay Joshi0206e352011-08-16 15:34:10 -04003587 for (i = 0; i < 4; i++) {
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_VOL_EMP_MASK;
3591 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003592 I915_WRITE(reg, temp);
3593
3594 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003595 udelay(500);
3596
Sean Paulfa37d392012-03-02 12:53:39 -05003597 for (retry = 0; retry < 5; retry++) {
3598 reg = FDI_RX_IIR(pipe);
3599 temp = I915_READ(reg);
3600 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3601 if (temp & FDI_RX_SYMBOL_LOCK) {
3602 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3603 DRM_DEBUG_KMS("FDI train 2 done.\n");
3604 break;
3605 }
3606 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003607 }
Sean Paulfa37d392012-03-02 12:53:39 -05003608 if (retry < 5)
3609 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003610 }
3611 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003612 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003613
3614 DRM_DEBUG_KMS("FDI train done.\n");
3615}
3616
Jesse Barnes357555c2011-04-28 15:09:55 -07003617/* Manual link training for Ivy Bridge A0 parts */
3618static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3619{
3620 struct drm_device *dev = crtc->dev;
3621 struct drm_i915_private *dev_priv = dev->dev_private;
3622 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3623 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003624 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003625
3626 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3627 for train result */
3628 reg = FDI_RX_IMR(pipe);
3629 temp = I915_READ(reg);
3630 temp &= ~FDI_RX_SYMBOL_LOCK;
3631 temp &= ~FDI_RX_BIT_LOCK;
3632 I915_WRITE(reg, temp);
3633
3634 POSTING_READ(reg);
3635 udelay(150);
3636
Daniel Vetter01a415f2012-10-27 15:58:40 +02003637 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3638 I915_READ(FDI_RX_IIR(pipe)));
3639
Jesse Barnes139ccd32013-08-19 11:04:55 -07003640 /* Try each vswing and preemphasis setting twice before moving on */
3641 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3642 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003643 reg = FDI_TX_CTL(pipe);
3644 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003645 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3646 temp &= ~FDI_TX_ENABLE;
3647 I915_WRITE(reg, temp);
3648
3649 reg = FDI_RX_CTL(pipe);
3650 temp = I915_READ(reg);
3651 temp &= ~FDI_LINK_TRAIN_AUTO;
3652 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3653 temp &= ~FDI_RX_ENABLE;
3654 I915_WRITE(reg, temp);
3655
3656 /* enable CPU FDI TX and PCH FDI RX */
3657 reg = FDI_TX_CTL(pipe);
3658 temp = I915_READ(reg);
3659 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003660 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003661 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003662 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003663 temp |= snb_b_fdi_train_param[j/2];
3664 temp |= FDI_COMPOSITE_SYNC;
3665 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3666
3667 I915_WRITE(FDI_RX_MISC(pipe),
3668 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3669
3670 reg = FDI_RX_CTL(pipe);
3671 temp = I915_READ(reg);
3672 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3673 temp |= FDI_COMPOSITE_SYNC;
3674 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3675
3676 POSTING_READ(reg);
3677 udelay(1); /* should be 0.5us */
3678
3679 for (i = 0; i < 4; i++) {
3680 reg = FDI_RX_IIR(pipe);
3681 temp = I915_READ(reg);
3682 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3683
3684 if (temp & FDI_RX_BIT_LOCK ||
3685 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3686 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3687 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3688 i);
3689 break;
3690 }
3691 udelay(1); /* should be 0.5us */
3692 }
3693 if (i == 4) {
3694 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3695 continue;
3696 }
3697
3698 /* Train 2 */
3699 reg = FDI_TX_CTL(pipe);
3700 temp = I915_READ(reg);
3701 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3702 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3703 I915_WRITE(reg, temp);
3704
3705 reg = FDI_RX_CTL(pipe);
3706 temp = I915_READ(reg);
3707 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3708 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003709 I915_WRITE(reg, temp);
3710
3711 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003712 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003713
Jesse Barnes139ccd32013-08-19 11:04:55 -07003714 for (i = 0; i < 4; i++) {
3715 reg = FDI_RX_IIR(pipe);
3716 temp = I915_READ(reg);
3717 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003718
Jesse Barnes139ccd32013-08-19 11:04:55 -07003719 if (temp & FDI_RX_SYMBOL_LOCK ||
3720 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3721 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3722 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3723 i);
3724 goto train_done;
3725 }
3726 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003727 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003728 if (i == 4)
3729 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003730 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003731
Jesse Barnes139ccd32013-08-19 11:04:55 -07003732train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003733 DRM_DEBUG_KMS("FDI train done.\n");
3734}
3735
Daniel Vetter88cefb62012-08-12 19:27:14 +02003736static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003737{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003738 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003739 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003740 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003741 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003742
Jesse Barnesc64e3112010-09-10 11:27:03 -07003743
Jesse Barnes0e23b992010-09-10 11:10:00 -07003744 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003745 reg = FDI_RX_CTL(pipe);
3746 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003747 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003748 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003749 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003750 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3751
3752 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003753 udelay(200);
3754
3755 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003756 temp = I915_READ(reg);
3757 I915_WRITE(reg, temp | FDI_PCDCLK);
3758
3759 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003760 udelay(200);
3761
Paulo Zanoni20749732012-11-23 15:30:38 -02003762 /* Enable CPU FDI TX PLL, always on for Ironlake */
3763 reg = FDI_TX_CTL(pipe);
3764 temp = I915_READ(reg);
3765 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3766 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003767
Paulo Zanoni20749732012-11-23 15:30:38 -02003768 POSTING_READ(reg);
3769 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003770 }
3771}
3772
Daniel Vetter88cefb62012-08-12 19:27:14 +02003773static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3774{
3775 struct drm_device *dev = intel_crtc->base.dev;
3776 struct drm_i915_private *dev_priv = dev->dev_private;
3777 int pipe = intel_crtc->pipe;
3778 u32 reg, temp;
3779
3780 /* Switch from PCDclk to Rawclk */
3781 reg = FDI_RX_CTL(pipe);
3782 temp = I915_READ(reg);
3783 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3784
3785 /* Disable CPU FDI TX PLL */
3786 reg = FDI_TX_CTL(pipe);
3787 temp = I915_READ(reg);
3788 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3789
3790 POSTING_READ(reg);
3791 udelay(100);
3792
3793 reg = FDI_RX_CTL(pipe);
3794 temp = I915_READ(reg);
3795 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3796
3797 /* Wait for the clocks to turn off. */
3798 POSTING_READ(reg);
3799 udelay(100);
3800}
3801
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003802static void ironlake_fdi_disable(struct drm_crtc *crtc)
3803{
3804 struct drm_device *dev = crtc->dev;
3805 struct drm_i915_private *dev_priv = dev->dev_private;
3806 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3807 int pipe = intel_crtc->pipe;
3808 u32 reg, temp;
3809
3810 /* disable CPU FDI tx and PCH FDI rx */
3811 reg = FDI_TX_CTL(pipe);
3812 temp = I915_READ(reg);
3813 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3814 POSTING_READ(reg);
3815
3816 reg = FDI_RX_CTL(pipe);
3817 temp = I915_READ(reg);
3818 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003819 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003820 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3821
3822 POSTING_READ(reg);
3823 udelay(100);
3824
3825 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003826 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003827 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003828
3829 /* still set train pattern 1 */
3830 reg = FDI_TX_CTL(pipe);
3831 temp = I915_READ(reg);
3832 temp &= ~FDI_LINK_TRAIN_NONE;
3833 temp |= FDI_LINK_TRAIN_PATTERN_1;
3834 I915_WRITE(reg, temp);
3835
3836 reg = FDI_RX_CTL(pipe);
3837 temp = I915_READ(reg);
3838 if (HAS_PCH_CPT(dev)) {
3839 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3840 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3841 } else {
3842 temp &= ~FDI_LINK_TRAIN_NONE;
3843 temp |= FDI_LINK_TRAIN_PATTERN_1;
3844 }
3845 /* BPC in FDI rx is consistent with that in PIPECONF */
3846 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003847 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003848 I915_WRITE(reg, temp);
3849
3850 POSTING_READ(reg);
3851 udelay(100);
3852}
3853
Chris Wilson5dce5b932014-01-20 10:17:36 +00003854bool intel_has_pending_fb_unpin(struct drm_device *dev)
3855{
3856 struct intel_crtc *crtc;
3857
3858 /* Note that we don't need to be called with mode_config.lock here
3859 * as our list of CRTC objects is static for the lifetime of the
3860 * device and so cannot disappear as we iterate. Similarly, we can
3861 * happily treat the predicates as racy, atomic checks as userspace
3862 * cannot claim and pin a new fb without at least acquring the
3863 * struct_mutex and so serialising with us.
3864 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003865 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003866 if (atomic_read(&crtc->unpin_work_count) == 0)
3867 continue;
3868
3869 if (crtc->unpin_work)
3870 intel_wait_for_vblank(dev, crtc->pipe);
3871
3872 return true;
3873 }
3874
3875 return false;
3876}
3877
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003878static void page_flip_completed(struct intel_crtc *intel_crtc)
3879{
3880 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3881 struct intel_unpin_work *work = intel_crtc->unpin_work;
3882
3883 /* ensure that the unpin work is consistent wrt ->pending. */
3884 smp_rmb();
3885 intel_crtc->unpin_work = NULL;
3886
3887 if (work->event)
3888 drm_send_vblank_event(intel_crtc->base.dev,
3889 intel_crtc->pipe,
3890 work->event);
3891
3892 drm_crtc_vblank_put(&intel_crtc->base);
3893
3894 wake_up_all(&dev_priv->pending_flip_queue);
3895 queue_work(dev_priv->wq, &work->work);
3896
3897 trace_i915_flip_complete(intel_crtc->plane,
3898 work->pending_flip_obj);
3899}
3900
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003901void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003902{
Chris Wilson0f911282012-04-17 10:05:38 +01003903 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003904 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003905
Daniel Vetter2c10d572012-12-20 21:24:07 +01003906 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003907 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3908 !intel_crtc_has_pending_flip(crtc),
3909 60*HZ) == 0)) {
3910 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003911
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003912 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003913 if (intel_crtc->unpin_work) {
3914 WARN_ONCE(1, "Removing stuck page flip\n");
3915 page_flip_completed(intel_crtc);
3916 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003917 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003918 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003919
Chris Wilson975d5682014-08-20 13:13:34 +01003920 if (crtc->primary->fb) {
3921 mutex_lock(&dev->struct_mutex);
3922 intel_finish_fb(crtc->primary->fb);
3923 mutex_unlock(&dev->struct_mutex);
3924 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003925}
3926
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927/* Program iCLKIP clock to the desired frequency */
3928static void lpt_program_iclkip(struct drm_crtc *crtc)
3929{
3930 struct drm_device *dev = crtc->dev;
3931 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003932 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003933 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3934 u32 temp;
3935
Ville Syrjäläa5805162015-05-26 20:42:30 +03003936 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003937
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003938 /* It is necessary to ungate the pixclk gate prior to programming
3939 * the divisors, and gate it back when it is done.
3940 */
3941 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3942
3943 /* Disable SSCCTL */
3944 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003945 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3946 SBI_SSCCTL_DISABLE,
3947 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948
3949 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003950 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003951 auxdiv = 1;
3952 divsel = 0x41;
3953 phaseinc = 0x20;
3954 } else {
3955 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003956 * but the adjusted_mode->crtc_clock in in KHz. To get the
3957 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958 * convert the virtual clock precision to KHz here for higher
3959 * precision.
3960 */
3961 u32 iclk_virtual_root_freq = 172800 * 1000;
3962 u32 iclk_pi_range = 64;
3963 u32 desired_divisor, msb_divisor_value, pi_value;
3964
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003965 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003966 msb_divisor_value = desired_divisor / iclk_pi_range;
3967 pi_value = desired_divisor % iclk_pi_range;
3968
3969 auxdiv = 0;
3970 divsel = msb_divisor_value - 2;
3971 phaseinc = pi_value;
3972 }
3973
3974 /* This should not happen with any sane values */
3975 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3976 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3977 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3978 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3979
3980 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 +03003981 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003982 auxdiv,
3983 divsel,
3984 phasedir,
3985 phaseinc);
3986
3987 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003988 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003989 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3990 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3991 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3992 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3993 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3994 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003995 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003996
3997 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003998 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003999 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4000 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004001 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004002
4003 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004004 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004005 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004006 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004007
4008 /* Wait for initialization time */
4009 udelay(24);
4010
4011 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004012
Ville Syrjäläa5805162015-05-26 20:42:30 +03004013 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004014}
4015
Daniel Vetter275f01b22013-05-03 11:49:47 +02004016static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4017 enum pipe pch_transcoder)
4018{
4019 struct drm_device *dev = crtc->base.dev;
4020 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004021 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004022
4023 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4024 I915_READ(HTOTAL(cpu_transcoder)));
4025 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4026 I915_READ(HBLANK(cpu_transcoder)));
4027 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4028 I915_READ(HSYNC(cpu_transcoder)));
4029
4030 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4031 I915_READ(VTOTAL(cpu_transcoder)));
4032 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4033 I915_READ(VBLANK(cpu_transcoder)));
4034 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4035 I915_READ(VSYNC(cpu_transcoder)));
4036 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4037 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4038}
4039
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004040static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004041{
4042 struct drm_i915_private *dev_priv = dev->dev_private;
4043 uint32_t temp;
4044
4045 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004046 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004047 return;
4048
4049 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4050 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4051
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004052 temp &= ~FDI_BC_BIFURCATION_SELECT;
4053 if (enable)
4054 temp |= FDI_BC_BIFURCATION_SELECT;
4055
4056 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004057 I915_WRITE(SOUTH_CHICKEN1, temp);
4058 POSTING_READ(SOUTH_CHICKEN1);
4059}
4060
4061static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4062{
4063 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004064
4065 switch (intel_crtc->pipe) {
4066 case PIPE_A:
4067 break;
4068 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004069 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004070 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004071 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004072 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004073
4074 break;
4075 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004076 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004077
4078 break;
4079 default:
4080 BUG();
4081 }
4082}
4083
Jesse Barnesf67a5592011-01-05 10:31:48 -08004084/*
4085 * Enable PCH resources required for PCH ports:
4086 * - PCH PLLs
4087 * - FDI training & RX/TX
4088 * - update transcoder timings
4089 * - DP transcoding bits
4090 * - transcoder
4091 */
4092static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004093{
4094 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004095 struct drm_i915_private *dev_priv = dev->dev_private;
4096 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4097 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004098 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004099
Daniel Vetterab9412b2013-05-03 11:49:46 +02004100 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004101
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004102 if (IS_IVYBRIDGE(dev))
4103 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4104
Daniel Vettercd986ab2012-10-26 10:58:12 +02004105 /* Write the TU size bits before fdi link training, so that error
4106 * detection works. */
4107 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4108 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4109
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004110 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004111 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004112
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004113 /* We need to program the right clock selection before writing the pixel
4114 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004115 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004116 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004117
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004118 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004119 temp |= TRANS_DPLL_ENABLE(pipe);
4120 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004121 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004122 temp |= sel;
4123 else
4124 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004125 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004127
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004128 /* XXX: pch pll's can be enabled any time before we enable the PCH
4129 * transcoder, and we actually should do this to not upset any PCH
4130 * transcoder that already use the clock when we share it.
4131 *
4132 * Note that enable_shared_dpll tries to do the right thing, but
4133 * get_shared_dpll unconditionally resets the pll - we need that to have
4134 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004135 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004136
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004137 /* set transcoder timing, panel must allow it */
4138 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004139 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004141 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004142
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004143 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004144 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004145 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004146 reg = TRANS_DP_CTL(pipe);
4147 temp = I915_READ(reg);
4148 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004149 TRANS_DP_SYNC_MASK |
4150 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004151 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004152 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153
4154 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004155 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004156 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004157 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004158
4159 switch (intel_trans_dp_port_sel(crtc)) {
4160 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 break;
4163 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004164 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165 break;
4166 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004167 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168 break;
4169 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004170 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004171 }
4172
Chris Wilson5eddb702010-09-11 13:48:45 +01004173 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004174 }
4175
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004176 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004177}
4178
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004179static void lpt_pch_enable(struct drm_crtc *crtc)
4180{
4181 struct drm_device *dev = crtc->dev;
4182 struct drm_i915_private *dev_priv = dev->dev_private;
4183 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004184 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004185
Daniel Vetterab9412b2013-05-03 11:49:46 +02004186 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004187
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004188 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004189
Paulo Zanoni0540e482012-10-31 18:12:40 -02004190 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004191 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004192
Paulo Zanoni937bb612012-10-31 18:12:47 -02004193 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004194}
4195
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004196struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4197 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004198{
Daniel Vettere2b78262013-06-07 23:10:03 +02004199 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004200 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004201 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004202 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004203
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004204 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4205
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004206 if (HAS_PCH_IBX(dev_priv->dev)) {
4207 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004208 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004209 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004210
Daniel Vetter46edb022013-06-05 13:34:12 +02004211 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4212 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004213
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004214 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004215
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004216 goto found;
4217 }
4218
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304219 if (IS_BROXTON(dev_priv->dev)) {
4220 /* PLL is attached to port in bxt */
4221 struct intel_encoder *encoder;
4222 struct intel_digital_port *intel_dig_port;
4223
4224 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4225 if (WARN_ON(!encoder))
4226 return NULL;
4227
4228 intel_dig_port = enc_to_dig_port(&encoder->base);
4229 /* 1:1 mapping between ports and PLLs */
4230 i = (enum intel_dpll_id)intel_dig_port->port;
4231 pll = &dev_priv->shared_dplls[i];
4232 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4233 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004234 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304235
4236 goto found;
4237 }
4238
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004239 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4240 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004241
4242 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004243 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004244 continue;
4245
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004246 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004247 &shared_dpll[i].hw_state,
4248 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004249 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004250 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004251 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004252 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004253 goto found;
4254 }
4255 }
4256
4257 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004258 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4259 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004260 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004261 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4262 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004263 goto found;
4264 }
4265 }
4266
4267 return NULL;
4268
4269found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004270 if (shared_dpll[i].crtc_mask == 0)
4271 shared_dpll[i].hw_state =
4272 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004273
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004274 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004275 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4276 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004277
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004278 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004279
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004280 return pll;
4281}
4282
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004283static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004284{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004285 struct drm_i915_private *dev_priv = to_i915(state->dev);
4286 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004287 struct intel_shared_dpll *pll;
4288 enum intel_dpll_id i;
4289
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004290 if (!to_intel_atomic_state(state)->dpll_set)
4291 return;
4292
4293 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004294 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4295 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004296 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004297 }
4298}
4299
Daniel Vettera1520312013-05-03 11:49:50 +02004300static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004301{
4302 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004303 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004304 u32 temp;
4305
4306 temp = I915_READ(dslreg);
4307 udelay(500);
4308 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004309 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004310 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004311 }
4312}
4313
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004314static int
4315skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4316 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4317 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004318{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004319 struct intel_crtc_scaler_state *scaler_state =
4320 &crtc_state->scaler_state;
4321 struct intel_crtc *intel_crtc =
4322 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004323 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004324
4325 need_scaling = intel_rotation_90_or_270(rotation) ?
4326 (src_h != dst_w || src_w != dst_h):
4327 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004328
4329 /*
4330 * if plane is being disabled or scaler is no more required or force detach
4331 * - free scaler binded to this plane/crtc
4332 * - in order to do this, update crtc->scaler_usage
4333 *
4334 * Here scaler state in crtc_state is set free so that
4335 * scaler can be assigned to other user. Actual register
4336 * update to free the scaler is done in plane/panel-fit programming.
4337 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4338 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004339 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004340 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004341 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004342 scaler_state->scalers[*scaler_id].in_use = 0;
4343
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004344 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4345 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4346 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004347 scaler_state->scaler_users);
4348 *scaler_id = -1;
4349 }
4350 return 0;
4351 }
4352
4353 /* range checks */
4354 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4355 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4356
4357 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4358 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004359 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004360 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004361 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004362 return -EINVAL;
4363 }
4364
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004365 /* mark this plane as a scaler user in crtc_state */
4366 scaler_state->scaler_users |= (1 << scaler_user);
4367 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4368 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4369 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4370 scaler_state->scaler_users);
4371
4372 return 0;
4373}
4374
4375/**
4376 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4377 *
4378 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004379 *
4380 * Return
4381 * 0 - scaler_usage updated successfully
4382 * error - requested scaling cannot be supported or other error condition
4383 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004384int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004385{
4386 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4387 struct drm_display_mode *adjusted_mode =
4388 &state->base.adjusted_mode;
4389
4390 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4391 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4392
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004393 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004394 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4395 state->pipe_src_w, state->pipe_src_h,
Imre Deak8c6cda22015-06-23 20:40:27 +03004396 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004397}
4398
4399/**
4400 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4401 *
4402 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004403 * @plane_state: atomic plane state to update
4404 *
4405 * Return
4406 * 0 - scaler_usage updated successfully
4407 * error - requested scaling cannot be supported or other error condition
4408 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004409static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4410 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004411{
4412
4413 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004414 struct intel_plane *intel_plane =
4415 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004416 struct drm_framebuffer *fb = plane_state->base.fb;
4417 int ret;
4418
4419 bool force_detach = !fb || !plane_state->visible;
4420
4421 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4422 intel_plane->base.base.id, intel_crtc->pipe,
4423 drm_plane_index(&intel_plane->base));
4424
4425 ret = skl_update_scaler(crtc_state, force_detach,
4426 drm_plane_index(&intel_plane->base),
4427 &plane_state->scaler_id,
4428 plane_state->base.rotation,
4429 drm_rect_width(&plane_state->src) >> 16,
4430 drm_rect_height(&plane_state->src) >> 16,
4431 drm_rect_width(&plane_state->dst),
4432 drm_rect_height(&plane_state->dst));
4433
4434 if (ret || plane_state->scaler_id < 0)
4435 return ret;
4436
Chandra Kondurua1b22782015-04-07 15:28:45 -07004437 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004438 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004439 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004440 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004441 return -EINVAL;
4442 }
4443
4444 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004445 switch (fb->pixel_format) {
4446 case DRM_FORMAT_RGB565:
4447 case DRM_FORMAT_XBGR8888:
4448 case DRM_FORMAT_XRGB8888:
4449 case DRM_FORMAT_ABGR8888:
4450 case DRM_FORMAT_ARGB8888:
4451 case DRM_FORMAT_XRGB2101010:
4452 case DRM_FORMAT_XBGR2101010:
4453 case DRM_FORMAT_YUYV:
4454 case DRM_FORMAT_YVYU:
4455 case DRM_FORMAT_UYVY:
4456 case DRM_FORMAT_VYUY:
4457 break;
4458 default:
4459 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4460 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4461 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004462 }
4463
Chandra Kondurua1b22782015-04-07 15:28:45 -07004464 return 0;
4465}
4466
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004467static void skylake_scaler_disable(struct intel_crtc *crtc)
4468{
4469 int i;
4470
4471 for (i = 0; i < crtc->num_scalers; i++)
4472 skl_detach_scaler(crtc, i);
4473}
4474
4475static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004476{
4477 struct drm_device *dev = crtc->base.dev;
4478 struct drm_i915_private *dev_priv = dev->dev_private;
4479 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004480 struct intel_crtc_scaler_state *scaler_state =
4481 &crtc->config->scaler_state;
4482
4483 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4484
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004485 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004486 int id;
4487
4488 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4489 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4490 return;
4491 }
4492
4493 id = scaler_state->scaler_id;
4494 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4495 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4496 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4497 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4498
4499 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004500 }
4501}
4502
Jesse Barnesb074cec2013-04-25 12:55:02 -07004503static void ironlake_pfit_enable(struct intel_crtc *crtc)
4504{
4505 struct drm_device *dev = crtc->base.dev;
4506 struct drm_i915_private *dev_priv = dev->dev_private;
4507 int pipe = crtc->pipe;
4508
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004509 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004510 /* Force use of hard-coded filter coefficients
4511 * as some pre-programmed values are broken,
4512 * e.g. x201.
4513 */
4514 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4515 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4516 PF_PIPE_SEL_IVB(pipe));
4517 else
4518 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004519 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4520 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004521 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004522}
4523
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004524void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004525{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004526 struct drm_device *dev = crtc->base.dev;
4527 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004528
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004529 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004530 return;
4531
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004532 /* We can only enable IPS after we enable a plane and wait for a vblank */
4533 intel_wait_for_vblank(dev, crtc->pipe);
4534
Paulo Zanonid77e4532013-09-24 13:52:55 -03004535 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004536 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004537 mutex_lock(&dev_priv->rps.hw_lock);
4538 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4539 mutex_unlock(&dev_priv->rps.hw_lock);
4540 /* Quoting Art Runyan: "its not safe to expect any particular
4541 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004542 * mailbox." Moreover, the mailbox may return a bogus state,
4543 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004544 */
4545 } else {
4546 I915_WRITE(IPS_CTL, IPS_ENABLE);
4547 /* The bit only becomes 1 in the next vblank, so this wait here
4548 * is essentially intel_wait_for_vblank. If we don't have this
4549 * and don't wait for vblanks until the end of crtc_enable, then
4550 * the HW state readout code will complain that the expected
4551 * IPS_CTL value is not the one we read. */
4552 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4553 DRM_ERROR("Timed out waiting for IPS enable\n");
4554 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004555}
4556
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004557void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004558{
4559 struct drm_device *dev = crtc->base.dev;
4560 struct drm_i915_private *dev_priv = dev->dev_private;
4561
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004562 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004563 return;
4564
4565 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004566 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004567 mutex_lock(&dev_priv->rps.hw_lock);
4568 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4569 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004570 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4571 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4572 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004573 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004574 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004575 POSTING_READ(IPS_CTL);
4576 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004577
4578 /* We need to wait for a vblank before we can disable the plane. */
4579 intel_wait_for_vblank(dev, crtc->pipe);
4580}
4581
4582/** Loads the palette/gamma unit for the CRTC with the prepared values */
4583static void intel_crtc_load_lut(struct drm_crtc *crtc)
4584{
4585 struct drm_device *dev = crtc->dev;
4586 struct drm_i915_private *dev_priv = dev->dev_private;
4587 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4588 enum pipe pipe = intel_crtc->pipe;
4589 int palreg = PALETTE(pipe);
4590 int i;
4591 bool reenable_ips = false;
4592
4593 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004594 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004595 return;
4596
Imre Deak50360402015-01-16 00:55:16 -08004597 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004598 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004599 assert_dsi_pll_enabled(dev_priv);
4600 else
4601 assert_pll_enabled(dev_priv, pipe);
4602 }
4603
4604 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304605 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004606 palreg = LGC_PALETTE(pipe);
4607
4608 /* Workaround : Do not read or write the pipe palette/gamma data while
4609 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4610 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004611 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004612 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4613 GAMMA_MODE_MODE_SPLIT)) {
4614 hsw_disable_ips(intel_crtc);
4615 reenable_ips = true;
4616 }
4617
4618 for (i = 0; i < 256; i++) {
4619 I915_WRITE(palreg + 4 * i,
4620 (intel_crtc->lut_r[i] << 16) |
4621 (intel_crtc->lut_g[i] << 8) |
4622 intel_crtc->lut_b[i]);
4623 }
4624
4625 if (reenable_ips)
4626 hsw_enable_ips(intel_crtc);
4627}
4628
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004629static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004630{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004631 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004632 struct drm_device *dev = intel_crtc->base.dev;
4633 struct drm_i915_private *dev_priv = dev->dev_private;
4634
4635 mutex_lock(&dev->struct_mutex);
4636 dev_priv->mm.interruptible = false;
4637 (void) intel_overlay_switch_off(intel_crtc->overlay);
4638 dev_priv->mm.interruptible = true;
4639 mutex_unlock(&dev->struct_mutex);
4640 }
4641
4642 /* Let userspace switch the overlay on again. In most cases userspace
4643 * has to recompute where to put it anyway.
4644 */
4645}
4646
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004647/**
4648 * intel_post_enable_primary - Perform operations after enabling primary plane
4649 * @crtc: the CRTC whose primary plane was just enabled
4650 *
4651 * Performs potentially sleeping operations that must be done after the primary
4652 * plane is enabled, such as updating FBC and IPS. Note that this may be
4653 * called due to an explicit primary plane update, or due to an implicit
4654 * re-enable that is caused when a sprite plane is updated to no longer
4655 * completely hide the primary plane.
4656 */
4657static void
4658intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004659{
4660 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004661 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004662 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4663 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004664
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004665 /*
4666 * BDW signals flip done immediately if the plane
4667 * is disabled, even if the plane enable is already
4668 * armed to occur at the next vblank :(
4669 */
4670 if (IS_BROADWELL(dev))
4671 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004672
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004673 /*
4674 * FIXME IPS should be fine as long as one plane is
4675 * enabled, but in practice it seems to have problems
4676 * when going from primary only to sprite only and vice
4677 * versa.
4678 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004679 hsw_enable_ips(intel_crtc);
4680
Daniel Vetterf99d7062014-06-19 16:01:59 +02004681 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004682 * Gen2 reports pipe underruns whenever all planes are disabled.
4683 * So don't enable underrun reporting before at least some planes
4684 * are enabled.
4685 * FIXME: Need to fix the logic to work when we turn off all planes
4686 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004687 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004688 if (IS_GEN2(dev))
4689 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4690
4691 /* Underruns don't raise interrupts, so check manually. */
4692 if (HAS_GMCH_DISPLAY(dev))
4693 i9xx_check_fifo_underruns(dev_priv);
4694}
4695
4696/**
4697 * intel_pre_disable_primary - Perform operations before disabling primary plane
4698 * @crtc: the CRTC whose primary plane is to be disabled
4699 *
4700 * Performs potentially sleeping operations that must be done before the
4701 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4702 * be called due to an explicit primary plane update, or due to an implicit
4703 * disable that is caused when a sprite plane completely hides the primary
4704 * plane.
4705 */
4706static void
4707intel_pre_disable_primary(struct drm_crtc *crtc)
4708{
4709 struct drm_device *dev = crtc->dev;
4710 struct drm_i915_private *dev_priv = dev->dev_private;
4711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4712 int pipe = intel_crtc->pipe;
4713
4714 /*
4715 * Gen2 reports pipe underruns whenever all planes are disabled.
4716 * So diasble underrun reporting before all the planes get disabled.
4717 * FIXME: Need to fix the logic to work when we turn off all planes
4718 * but leave the pipe running.
4719 */
4720 if (IS_GEN2(dev))
4721 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4722
4723 /*
4724 * Vblank time updates from the shadow to live plane control register
4725 * are blocked if the memory self-refresh mode is active at that
4726 * moment. So to make sure the plane gets truly disabled, disable
4727 * first the self-refresh mode. The self-refresh enable bit in turn
4728 * will be checked/applied by the HW only at the next frame start
4729 * event which is after the vblank start event, so we need to have a
4730 * wait-for-vblank between disabling the plane and the pipe.
4731 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004732 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004733 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004734 dev_priv->wm.vlv.cxsr = false;
4735 intel_wait_for_vblank(dev, pipe);
4736 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004737
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004738 /*
4739 * FIXME IPS should be fine as long as one plane is
4740 * enabled, but in practice it seems to have problems
4741 * when going from primary only to sprite only and vice
4742 * versa.
4743 */
4744 hsw_disable_ips(intel_crtc);
4745}
4746
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004747static void intel_post_plane_update(struct intel_crtc *crtc)
4748{
4749 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4750 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -03004751 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004752 struct drm_plane *plane;
4753
4754 if (atomic->wait_vblank)
4755 intel_wait_for_vblank(dev, crtc->pipe);
4756
4757 intel_frontbuffer_flip(dev, atomic->fb_bits);
4758
Ville Syrjälä852eb002015-06-24 22:00:07 +03004759 if (atomic->disable_cxsr)
4760 crtc->wm.cxsr_allowed = true;
4761
Ville Syrjäläf015c552015-06-24 22:00:02 +03004762 if (crtc->atomic.update_wm_post)
4763 intel_update_watermarks(&crtc->base);
4764
Paulo Zanonic80ac852015-07-02 19:25:13 -03004765 if (atomic->update_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03004766 intel_fbc_update(dev_priv);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004767
4768 if (atomic->post_enable_primary)
4769 intel_post_enable_primary(&crtc->base);
4770
4771 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4772 intel_update_sprite_watermarks(plane, &crtc->base,
4773 0, 0, 0, false, false);
4774
4775 memset(atomic, 0, sizeof(*atomic));
4776}
4777
4778static void intel_pre_plane_update(struct intel_crtc *crtc)
4779{
4780 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004781 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004782 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4783 struct drm_plane *p;
4784
4785 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004786 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4787 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004788
4789 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004790 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4791 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004792 mutex_unlock(&dev->struct_mutex);
4793 }
4794
4795 if (atomic->wait_for_flips)
4796 intel_crtc_wait_for_pending_flips(&crtc->base);
4797
Paulo Zanonic80ac852015-07-02 19:25:13 -03004798 if (atomic->disable_fbc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03004799 intel_fbc_disable_crtc(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004800
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -07004801 if (crtc->atomic.disable_ips)
4802 hsw_disable_ips(crtc);
4803
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004804 if (atomic->pre_disable_primary)
4805 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004806
4807 if (atomic->disable_cxsr) {
4808 crtc->wm.cxsr_allowed = false;
4809 intel_set_memory_cxsr(dev_priv, false);
4810 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004811}
4812
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004813static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004814{
4815 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004817 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004818 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004819
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004820 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004821
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004822 drm_for_each_plane_mask(p, dev, plane_mask)
4823 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004824
Daniel Vetterf99d7062014-06-19 16:01:59 +02004825 /*
4826 * FIXME: Once we grow proper nuclear flip support out of this we need
4827 * to compute the mask of flip planes precisely. For the time being
4828 * consider this a flip to a NULL plane.
4829 */
4830 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004831}
4832
Jesse Barnesf67a5592011-01-05 10:31:48 -08004833static void ironlake_crtc_enable(struct drm_crtc *crtc)
4834{
4835 struct drm_device *dev = crtc->dev;
4836 struct drm_i915_private *dev_priv = dev->dev_private;
4837 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004838 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004839 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004840
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004841 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004842 return;
4843
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004844 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004845 intel_prepare_shared_dpll(intel_crtc);
4846
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004847 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304848 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004849
4850 intel_set_pipe_timings(intel_crtc);
4851
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004852 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004853 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004854 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004855 }
4856
4857 ironlake_set_pipeconf(crtc);
4858
Jesse Barnesf67a5592011-01-05 10:31:48 -08004859 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004860
Daniel Vettera72e4c92014-09-30 10:56:47 +02004861 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4862 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004863
Daniel Vetterf6736a12013-06-05 13:34:30 +02004864 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004865 if (encoder->pre_enable)
4866 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004867
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004868 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004869 /* Note: FDI PLL enabling _must_ be done before we enable the
4870 * cpu pipes, hence this is separate from all the other fdi/pch
4871 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004872 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004873 } else {
4874 assert_fdi_tx_disabled(dev_priv, pipe);
4875 assert_fdi_rx_disabled(dev_priv, pipe);
4876 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004877
Jesse Barnesb074cec2013-04-25 12:55:02 -07004878 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004879
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004880 /*
4881 * On ILK+ LUT must be loaded before the pipe is running but with
4882 * clocks enabled
4883 */
4884 intel_crtc_load_lut(crtc);
4885
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004886 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004887 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004888
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004889 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004890 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004891
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004892 assert_vblank_disabled(crtc);
4893 drm_crtc_vblank_on(crtc);
4894
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004895 for_each_encoder_on_crtc(dev, crtc, encoder)
4896 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004897
4898 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004899 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004900}
4901
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004902/* IPS only exists on ULT machines and is tied to pipe A. */
4903static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4904{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004905 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004906}
4907
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004908static void haswell_crtc_enable(struct drm_crtc *crtc)
4909{
4910 struct drm_device *dev = crtc->dev;
4911 struct drm_i915_private *dev_priv = dev->dev_private;
4912 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4913 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004914 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4915 struct intel_crtc_state *pipe_config =
4916 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004917
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004918 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004919 return;
4920
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004921 if (intel_crtc_to_shared_dpll(intel_crtc))
4922 intel_enable_shared_dpll(intel_crtc);
4923
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004924 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304925 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004926
4927 intel_set_pipe_timings(intel_crtc);
4928
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004929 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4930 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4931 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004932 }
4933
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004934 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004935 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004936 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004937 }
4938
4939 haswell_set_pipeconf(crtc);
4940
4941 intel_set_pipe_csc(crtc);
4942
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004943 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004944
Daniel Vettera72e4c92014-09-30 10:56:47 +02004945 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004946 for_each_encoder_on_crtc(dev, crtc, encoder)
4947 if (encoder->pre_enable)
4948 encoder->pre_enable(encoder);
4949
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004950 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004951 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4952 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004953 dev_priv->display.fdi_link_train(crtc);
4954 }
4955
Paulo Zanoni1f544382012-10-24 11:32:00 -02004956 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004957
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004958 if (INTEL_INFO(dev)->gen == 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004959 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004960 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004961 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004962 else
4963 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004964
4965 /*
4966 * On ILK+ LUT must be loaded before the pipe is running but with
4967 * clocks enabled
4968 */
4969 intel_crtc_load_lut(crtc);
4970
Paulo Zanoni1f544382012-10-24 11:32:00 -02004971 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004972 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004973
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004974 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004975 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004976
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004977 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004978 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004980 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004981 intel_ddi_set_vc_payload_alloc(crtc, true);
4982
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004983 assert_vblank_disabled(crtc);
4984 drm_crtc_vblank_on(crtc);
4985
Jani Nikula8807e552013-08-30 19:40:32 +03004986 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004987 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004988 intel_opregion_notify_encoder(encoder, true);
4989 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004990
Paulo Zanonie4916942013-09-20 16:21:19 -03004991 /* If we change the relative order between pipe/planes enabling, we need
4992 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004993 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4994 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4995 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4996 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4997 }
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
Daniel Vetterea9d7582012-07-10 10:42:52 +02005024 for_each_encoder_on_crtc(dev, crtc, encoder)
5025 encoder->disable(encoder);
5026
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005027 drm_crtc_vblank_off(crtc);
5028 assert_vblank_disabled(crtc);
5029
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005030 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005031 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005032
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005033 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005034
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005035 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005036
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005037 if (intel_crtc->config->has_pch_encoder)
5038 ironlake_fdi_disable(crtc);
5039
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005040 for_each_encoder_on_crtc(dev, crtc, encoder)
5041 if (encoder->post_disable)
5042 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005043
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005044 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005045 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005046
Daniel Vetterd925c592013-06-05 13:34:04 +02005047 if (HAS_PCH_CPT(dev)) {
5048 /* disable TRANS_DP_CTL */
5049 reg = TRANS_DP_CTL(pipe);
5050 temp = I915_READ(reg);
5051 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5052 TRANS_DP_PORT_SEL_MASK);
5053 temp |= TRANS_DP_PORT_SEL_NONE;
5054 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005055
Daniel Vetterd925c592013-06-05 13:34:04 +02005056 /* disable DPLL_SEL */
5057 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005058 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005059 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005060 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005061
Daniel Vetterd925c592013-06-05 13:34:04 +02005062 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005063 }
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005064
5065 intel_crtc->active = false;
5066 intel_update_watermarks(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005067}
5068
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005069static void haswell_crtc_disable(struct drm_crtc *crtc)
5070{
5071 struct drm_device *dev = crtc->dev;
5072 struct drm_i915_private *dev_priv = dev->dev_private;
5073 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5074 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005075 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005076
Jani Nikula8807e552013-08-30 19:40:32 +03005077 for_each_encoder_on_crtc(dev, crtc, encoder) {
5078 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005079 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005080 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005081
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005082 drm_crtc_vblank_off(crtc);
5083 assert_vblank_disabled(crtc);
5084
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005085 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005086 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5087 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005088 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005089
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005090 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005091 intel_ddi_set_vc_payload_alloc(crtc, false);
5092
Paulo Zanoniad80a812012-10-24 16:06:19 -02005093 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005094
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005095 if (INTEL_INFO(dev)->gen == 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005096 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005097 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005098 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005099 else
5100 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005101
Paulo Zanoni1f544382012-10-24 11:32:00 -02005102 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005103
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005104 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005105 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005106 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005107 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005108
Imre Deak97b040a2014-06-25 22:01:50 +03005109 for_each_encoder_on_crtc(dev, crtc, encoder)
5110 if (encoder->post_disable)
5111 encoder->post_disable(encoder);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005112
5113 intel_crtc->active = false;
5114 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005115}
5116
Jesse Barnes2dd24552013-04-25 12:55:01 -07005117static void i9xx_pfit_enable(struct intel_crtc *crtc)
5118{
5119 struct drm_device *dev = crtc->base.dev;
5120 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005121 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005122
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005123 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005124 return;
5125
Daniel Vetterc0b03412013-05-28 12:05:54 +02005126 /*
5127 * The panel fitter should only be adjusted whilst the pipe is disabled,
5128 * according to register description and PRM.
5129 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005130 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5131 assert_pipe_disabled(dev_priv, crtc->pipe);
5132
Jesse Barnesb074cec2013-04-25 12:55:02 -07005133 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5134 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005135
5136 /* Border color in case we don't scale up to the full screen. Black by
5137 * default, change to something else for debugging. */
5138 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005139}
5140
Dave Airlied05410f2014-06-05 13:22:59 +10005141static enum intel_display_power_domain port_to_power_domain(enum port port)
5142{
5143 switch (port) {
5144 case PORT_A:
5145 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5146 case PORT_B:
5147 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5148 case PORT_C:
5149 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5150 case PORT_D:
5151 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5152 default:
5153 WARN_ON_ONCE(1);
5154 return POWER_DOMAIN_PORT_OTHER;
5155 }
5156}
5157
Imre Deak77d22dc2014-03-05 16:20:52 +02005158#define for_each_power_domain(domain, mask) \
5159 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5160 if ((1 << (domain)) & (mask))
5161
Imre Deak319be8a2014-03-04 19:22:57 +02005162enum intel_display_power_domain
5163intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005164{
Imre Deak319be8a2014-03-04 19:22:57 +02005165 struct drm_device *dev = intel_encoder->base.dev;
5166 struct intel_digital_port *intel_dig_port;
5167
5168 switch (intel_encoder->type) {
5169 case INTEL_OUTPUT_UNKNOWN:
5170 /* Only DDI platforms should ever use this output type */
5171 WARN_ON_ONCE(!HAS_DDI(dev));
5172 case INTEL_OUTPUT_DISPLAYPORT:
5173 case INTEL_OUTPUT_HDMI:
5174 case INTEL_OUTPUT_EDP:
5175 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005176 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005177 case INTEL_OUTPUT_DP_MST:
5178 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5179 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005180 case INTEL_OUTPUT_ANALOG:
5181 return POWER_DOMAIN_PORT_CRT;
5182 case INTEL_OUTPUT_DSI:
5183 return POWER_DOMAIN_PORT_DSI;
5184 default:
5185 return POWER_DOMAIN_PORT_OTHER;
5186 }
5187}
5188
5189static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5190{
5191 struct drm_device *dev = crtc->dev;
5192 struct intel_encoder *intel_encoder;
5193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5194 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005195 unsigned long mask;
5196 enum transcoder transcoder;
5197
5198 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5199
5200 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5201 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005202 if (intel_crtc->config->pch_pfit.enabled ||
5203 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005204 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5205
Imre Deak319be8a2014-03-04 19:22:57 +02005206 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5207 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5208
Imre Deak77d22dc2014-03-05 16:20:52 +02005209 return mask;
5210}
5211
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005212static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005213{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005214 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005215 struct drm_i915_private *dev_priv = dev->dev_private;
5216 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5217 struct intel_crtc *crtc;
5218
5219 /*
5220 * First get all needed power domains, then put all unneeded, to avoid
5221 * any unnecessary toggling of the power wells.
5222 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005223 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005224 enum intel_display_power_domain domain;
5225
Matt Roper83d65732015-02-25 13:12:16 -08005226 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005227 continue;
5228
Imre Deak319be8a2014-03-04 19:22:57 +02005229 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005230
5231 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5232 intel_display_power_get(dev_priv, domain);
5233 }
5234
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005235 if (dev_priv->display.modeset_commit_cdclk) {
5236 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5237
5238 if (cdclk != dev_priv->cdclk_freq &&
5239 !WARN_ON(!state->allow_modeset))
5240 dev_priv->display.modeset_commit_cdclk(state);
5241 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005242
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005243 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005244 enum intel_display_power_domain domain;
5245
5246 for_each_power_domain(domain, crtc->enabled_power_domains)
5247 intel_display_power_put(dev_priv, domain);
5248
5249 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5250 }
5251
5252 intel_display_set_init_power(dev_priv, false);
5253}
5254
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005255static void intel_update_max_cdclk(struct drm_device *dev)
5256{
5257 struct drm_i915_private *dev_priv = dev->dev_private;
5258
5259 if (IS_SKYLAKE(dev)) {
5260 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5261
5262 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5263 dev_priv->max_cdclk_freq = 675000;
5264 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5265 dev_priv->max_cdclk_freq = 540000;
5266 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5267 dev_priv->max_cdclk_freq = 450000;
5268 else
5269 dev_priv->max_cdclk_freq = 337500;
5270 } else if (IS_BROADWELL(dev)) {
5271 /*
5272 * FIXME with extra cooling we can allow
5273 * 540 MHz for ULX and 675 Mhz for ULT.
5274 * How can we know if extra cooling is
5275 * available? PCI ID, VTB, something else?
5276 */
5277 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5278 dev_priv->max_cdclk_freq = 450000;
5279 else if (IS_BDW_ULX(dev))
5280 dev_priv->max_cdclk_freq = 450000;
5281 else if (IS_BDW_ULT(dev))
5282 dev_priv->max_cdclk_freq = 540000;
5283 else
5284 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005285 } else if (IS_CHERRYVIEW(dev)) {
5286 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005287 } else if (IS_VALLEYVIEW(dev)) {
5288 dev_priv->max_cdclk_freq = 400000;
5289 } else {
5290 /* otherwise assume cdclk is fixed */
5291 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5292 }
5293
5294 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5295 dev_priv->max_cdclk_freq);
5296}
5297
5298static void intel_update_cdclk(struct drm_device *dev)
5299{
5300 struct drm_i915_private *dev_priv = dev->dev_private;
5301
5302 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5303 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5304 dev_priv->cdclk_freq);
5305
5306 /*
5307 * Program the gmbus_freq based on the cdclk frequency.
5308 * BSpec erroneously claims we should aim for 4MHz, but
5309 * in fact 1MHz is the correct frequency.
5310 */
5311 if (IS_VALLEYVIEW(dev)) {
5312 /*
5313 * Program the gmbus_freq based on the cdclk frequency.
5314 * BSpec erroneously claims we should aim for 4MHz, but
5315 * in fact 1MHz is the correct frequency.
5316 */
5317 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5318 }
5319
5320 if (dev_priv->max_cdclk_freq == 0)
5321 intel_update_max_cdclk(dev);
5322}
5323
Damien Lespiau70d0c572015-06-04 18:21:29 +01005324static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305325{
5326 struct drm_i915_private *dev_priv = dev->dev_private;
5327 uint32_t divider;
5328 uint32_t ratio;
5329 uint32_t current_freq;
5330 int ret;
5331
5332 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5333 switch (frequency) {
5334 case 144000:
5335 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5336 ratio = BXT_DE_PLL_RATIO(60);
5337 break;
5338 case 288000:
5339 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5340 ratio = BXT_DE_PLL_RATIO(60);
5341 break;
5342 case 384000:
5343 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5344 ratio = BXT_DE_PLL_RATIO(60);
5345 break;
5346 case 576000:
5347 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5348 ratio = BXT_DE_PLL_RATIO(60);
5349 break;
5350 case 624000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5352 ratio = BXT_DE_PLL_RATIO(65);
5353 break;
5354 case 19200:
5355 /*
5356 * Bypass frequency with DE PLL disabled. Init ratio, divider
5357 * to suppress GCC warning.
5358 */
5359 ratio = 0;
5360 divider = 0;
5361 break;
5362 default:
5363 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5364
5365 return;
5366 }
5367
5368 mutex_lock(&dev_priv->rps.hw_lock);
5369 /* Inform power controller of upcoming frequency change */
5370 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5371 0x80000000);
5372 mutex_unlock(&dev_priv->rps.hw_lock);
5373
5374 if (ret) {
5375 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5376 ret, frequency);
5377 return;
5378 }
5379
5380 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5381 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5382 current_freq = current_freq * 500 + 1000;
5383
5384 /*
5385 * DE PLL has to be disabled when
5386 * - setting to 19.2MHz (bypass, PLL isn't used)
5387 * - before setting to 624MHz (PLL needs toggling)
5388 * - before setting to any frequency from 624MHz (PLL needs toggling)
5389 */
5390 if (frequency == 19200 || frequency == 624000 ||
5391 current_freq == 624000) {
5392 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5393 /* Timeout 200us */
5394 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5395 1))
5396 DRM_ERROR("timout waiting for DE PLL unlock\n");
5397 }
5398
5399 if (frequency != 19200) {
5400 uint32_t val;
5401
5402 val = I915_READ(BXT_DE_PLL_CTL);
5403 val &= ~BXT_DE_PLL_RATIO_MASK;
5404 val |= ratio;
5405 I915_WRITE(BXT_DE_PLL_CTL, val);
5406
5407 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5408 /* Timeout 200us */
5409 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5410 DRM_ERROR("timeout waiting for DE PLL lock\n");
5411
5412 val = I915_READ(CDCLK_CTL);
5413 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5414 val |= divider;
5415 /*
5416 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5417 * enable otherwise.
5418 */
5419 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5420 if (frequency >= 500000)
5421 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5422
5423 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5424 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5425 val |= (frequency - 1000) / 500;
5426 I915_WRITE(CDCLK_CTL, val);
5427 }
5428
5429 mutex_lock(&dev_priv->rps.hw_lock);
5430 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5431 DIV_ROUND_UP(frequency, 25000));
5432 mutex_unlock(&dev_priv->rps.hw_lock);
5433
5434 if (ret) {
5435 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5436 ret, frequency);
5437 return;
5438 }
5439
Damien Lespiaua47871b2015-06-04 18:21:34 +01005440 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305441}
5442
5443void broxton_init_cdclk(struct drm_device *dev)
5444{
5445 struct drm_i915_private *dev_priv = dev->dev_private;
5446 uint32_t val;
5447
5448 /*
5449 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5450 * or else the reset will hang because there is no PCH to respond.
5451 * Move the handshake programming to initialization sequence.
5452 * Previously was left up to BIOS.
5453 */
5454 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5455 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5456 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5457
5458 /* Enable PG1 for cdclk */
5459 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5460
5461 /* check if cd clock is enabled */
5462 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5463 DRM_DEBUG_KMS("Display already initialized\n");
5464 return;
5465 }
5466
5467 /*
5468 * FIXME:
5469 * - The initial CDCLK needs to be read from VBT.
5470 * Need to make this change after VBT has changes for BXT.
5471 * - check if setting the max (or any) cdclk freq is really necessary
5472 * here, it belongs to modeset time
5473 */
5474 broxton_set_cdclk(dev, 624000);
5475
5476 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005477 POSTING_READ(DBUF_CTL);
5478
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305479 udelay(10);
5480
5481 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5482 DRM_ERROR("DBuf power enable timeout!\n");
5483}
5484
5485void broxton_uninit_cdclk(struct drm_device *dev)
5486{
5487 struct drm_i915_private *dev_priv = dev->dev_private;
5488
5489 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005490 POSTING_READ(DBUF_CTL);
5491
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305492 udelay(10);
5493
5494 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5495 DRM_ERROR("DBuf power disable timeout!\n");
5496
5497 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5498 broxton_set_cdclk(dev, 19200);
5499
5500 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5501}
5502
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005503static const struct skl_cdclk_entry {
5504 unsigned int freq;
5505 unsigned int vco;
5506} skl_cdclk_frequencies[] = {
5507 { .freq = 308570, .vco = 8640 },
5508 { .freq = 337500, .vco = 8100 },
5509 { .freq = 432000, .vco = 8640 },
5510 { .freq = 450000, .vco = 8100 },
5511 { .freq = 540000, .vco = 8100 },
5512 { .freq = 617140, .vco = 8640 },
5513 { .freq = 675000, .vco = 8100 },
5514};
5515
5516static unsigned int skl_cdclk_decimal(unsigned int freq)
5517{
5518 return (freq - 1000) / 500;
5519}
5520
5521static unsigned int skl_cdclk_get_vco(unsigned int freq)
5522{
5523 unsigned int i;
5524
5525 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5526 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5527
5528 if (e->freq == freq)
5529 return e->vco;
5530 }
5531
5532 return 8100;
5533}
5534
5535static void
5536skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5537{
5538 unsigned int min_freq;
5539 u32 val;
5540
5541 /* select the minimum CDCLK before enabling DPLL 0 */
5542 val = I915_READ(CDCLK_CTL);
5543 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5544 val |= CDCLK_FREQ_337_308;
5545
5546 if (required_vco == 8640)
5547 min_freq = 308570;
5548 else
5549 min_freq = 337500;
5550
5551 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5552
5553 I915_WRITE(CDCLK_CTL, val);
5554 POSTING_READ(CDCLK_CTL);
5555
5556 /*
5557 * We always enable DPLL0 with the lowest link rate possible, but still
5558 * taking into account the VCO required to operate the eDP panel at the
5559 * desired frequency. The usual DP link rates operate with a VCO of
5560 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5561 * The modeset code is responsible for the selection of the exact link
5562 * rate later on, with the constraint of choosing a frequency that
5563 * works with required_vco.
5564 */
5565 val = I915_READ(DPLL_CTRL1);
5566
5567 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5568 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5569 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5570 if (required_vco == 8640)
5571 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5572 SKL_DPLL0);
5573 else
5574 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5575 SKL_DPLL0);
5576
5577 I915_WRITE(DPLL_CTRL1, val);
5578 POSTING_READ(DPLL_CTRL1);
5579
5580 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5581
5582 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5583 DRM_ERROR("DPLL0 not locked\n");
5584}
5585
5586static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5587{
5588 int ret;
5589 u32 val;
5590
5591 /* inform PCU we want to change CDCLK */
5592 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5593 mutex_lock(&dev_priv->rps.hw_lock);
5594 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5595 mutex_unlock(&dev_priv->rps.hw_lock);
5596
5597 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5598}
5599
5600static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5601{
5602 unsigned int i;
5603
5604 for (i = 0; i < 15; i++) {
5605 if (skl_cdclk_pcu_ready(dev_priv))
5606 return true;
5607 udelay(10);
5608 }
5609
5610 return false;
5611}
5612
5613static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5614{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005615 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005616 u32 freq_select, pcu_ack;
5617
5618 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5619
5620 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5621 DRM_ERROR("failed to inform PCU about cdclk change\n");
5622 return;
5623 }
5624
5625 /* set CDCLK_CTL */
5626 switch(freq) {
5627 case 450000:
5628 case 432000:
5629 freq_select = CDCLK_FREQ_450_432;
5630 pcu_ack = 1;
5631 break;
5632 case 540000:
5633 freq_select = CDCLK_FREQ_540;
5634 pcu_ack = 2;
5635 break;
5636 case 308570:
5637 case 337500:
5638 default:
5639 freq_select = CDCLK_FREQ_337_308;
5640 pcu_ack = 0;
5641 break;
5642 case 617140:
5643 case 675000:
5644 freq_select = CDCLK_FREQ_675_617;
5645 pcu_ack = 3;
5646 break;
5647 }
5648
5649 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5650 POSTING_READ(CDCLK_CTL);
5651
5652 /* inform PCU of the change */
5653 mutex_lock(&dev_priv->rps.hw_lock);
5654 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5655 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005656
5657 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005658}
5659
5660void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5661{
5662 /* disable DBUF power */
5663 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5664 POSTING_READ(DBUF_CTL);
5665
5666 udelay(10);
5667
5668 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5669 DRM_ERROR("DBuf power disable timeout\n");
5670
5671 /* disable DPLL0 */
5672 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5673 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5674 DRM_ERROR("Couldn't disable DPLL0\n");
5675
5676 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5677}
5678
5679void skl_init_cdclk(struct drm_i915_private *dev_priv)
5680{
5681 u32 val;
5682 unsigned int required_vco;
5683
5684 /* enable PCH reset handshake */
5685 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5686 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5687
5688 /* enable PG1 and Misc I/O */
5689 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5690
5691 /* DPLL0 already enabed !? */
5692 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5693 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5694 return;
5695 }
5696
5697 /* enable DPLL0 */
5698 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5699 skl_dpll0_enable(dev_priv, required_vco);
5700
5701 /* set CDCLK to the frequency the BIOS chose */
5702 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5703
5704 /* enable DBUF power */
5705 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5706 POSTING_READ(DBUF_CTL);
5707
5708 udelay(10);
5709
5710 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5711 DRM_ERROR("DBuf power enable timeout\n");
5712}
5713
Ville Syrjälädfcab172014-06-13 13:37:47 +03005714/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005715static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005716{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005717 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005718
Jesse Barnes586f49d2013-11-04 16:06:59 -08005719 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005720 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005721 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5722 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005723 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005724
Ville Syrjälädfcab172014-06-13 13:37:47 +03005725 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005726}
5727
5728/* Adjust CDclk dividers to allow high res or save power if possible */
5729static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5730{
5731 struct drm_i915_private *dev_priv = dev->dev_private;
5732 u32 val, cmd;
5733
Vandana Kannan164dfd22014-11-24 13:37:41 +05305734 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5735 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005736
Ville Syrjälädfcab172014-06-13 13:37:47 +03005737 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005738 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005739 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005740 cmd = 1;
5741 else
5742 cmd = 0;
5743
5744 mutex_lock(&dev_priv->rps.hw_lock);
5745 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5746 val &= ~DSPFREQGUAR_MASK;
5747 val |= (cmd << DSPFREQGUAR_SHIFT);
5748 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5749 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5750 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5751 50)) {
5752 DRM_ERROR("timed out waiting for CDclk change\n");
5753 }
5754 mutex_unlock(&dev_priv->rps.hw_lock);
5755
Ville Syrjälä54433e92015-05-26 20:42:31 +03005756 mutex_lock(&dev_priv->sb_lock);
5757
Ville Syrjälädfcab172014-06-13 13:37:47 +03005758 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005759 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005760
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005761 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005762
Jesse Barnes30a970c2013-11-04 13:48:12 -08005763 /* adjust cdclk divider */
5764 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005765 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005766 val |= divider;
5767 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005768
5769 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5770 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5771 50))
5772 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773 }
5774
Jesse Barnes30a970c2013-11-04 13:48:12 -08005775 /* adjust self-refresh exit latency value */
5776 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5777 val &= ~0x7f;
5778
5779 /*
5780 * For high bandwidth configs, we set a higher latency in the bunit
5781 * so that the core display fetch happens in time to avoid underruns.
5782 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005783 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005784 val |= 4500 / 250; /* 4.5 usec */
5785 else
5786 val |= 3000 / 250; /* 3.0 usec */
5787 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005788
Ville Syrjäläa5805162015-05-26 20:42:30 +03005789 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790
Ville Syrjäläb6283052015-06-03 15:45:07 +03005791 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005792}
5793
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005794static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5795{
5796 struct drm_i915_private *dev_priv = dev->dev_private;
5797 u32 val, cmd;
5798
Vandana Kannan164dfd22014-11-24 13:37:41 +05305799 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5800 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005801
5802 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005803 case 333333:
5804 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005805 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005806 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005807 break;
5808 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005809 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005810 return;
5811 }
5812
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005813 /*
5814 * Specs are full of misinformation, but testing on actual
5815 * hardware has shown that we just need to write the desired
5816 * CCK divider into the Punit register.
5817 */
5818 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5819
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005820 mutex_lock(&dev_priv->rps.hw_lock);
5821 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5822 val &= ~DSPFREQGUAR_MASK_CHV;
5823 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5824 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5825 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5826 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5827 50)) {
5828 DRM_ERROR("timed out waiting for CDclk change\n");
5829 }
5830 mutex_unlock(&dev_priv->rps.hw_lock);
5831
Ville Syrjäläb6283052015-06-03 15:45:07 +03005832 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005833}
5834
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5836 int max_pixclk)
5837{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005838 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005839 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005840
Jesse Barnes30a970c2013-11-04 13:48:12 -08005841 /*
5842 * Really only a few cases to deal with, as only 4 CDclks are supported:
5843 * 200MHz
5844 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005845 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005846 * 400MHz (VLV only)
5847 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5848 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005849 *
5850 * We seem to get an unstable or solid color picture at 200MHz.
5851 * Not sure what's wrong. For now use 200MHz only when all pipes
5852 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005853 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005854 if (!IS_CHERRYVIEW(dev_priv) &&
5855 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005856 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005857 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005858 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005859 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005860 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005861 else
5862 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005863}
5864
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305865static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5866 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005867{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305868 /*
5869 * FIXME:
5870 * - remove the guardband, it's not needed on BXT
5871 * - set 19.2MHz bypass frequency if there are no active pipes
5872 */
5873 if (max_pixclk > 576000*9/10)
5874 return 624000;
5875 else if (max_pixclk > 384000*9/10)
5876 return 576000;
5877 else if (max_pixclk > 288000*9/10)
5878 return 384000;
5879 else if (max_pixclk > 144000*9/10)
5880 return 288000;
5881 else
5882 return 144000;
5883}
5884
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005885/* Compute the max pixel clock for new configuration. Uses atomic state if
5886 * that's non-NULL, look at current state otherwise. */
5887static int intel_mode_max_pixclk(struct drm_device *dev,
5888 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005889{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005890 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005891 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005892 int max_pixclk = 0;
5893
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005894 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005895 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005896 if (IS_ERR(crtc_state))
5897 return PTR_ERR(crtc_state);
5898
5899 if (!crtc_state->base.enable)
5900 continue;
5901
5902 max_pixclk = max(max_pixclk,
5903 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005904 }
5905
5906 return max_pixclk;
5907}
5908
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005909static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005910{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005911 struct drm_device *dev = state->dev;
5912 struct drm_i915_private *dev_priv = dev->dev_private;
5913 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005914
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005915 if (max_pixclk < 0)
5916 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005917
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005918 to_intel_atomic_state(state)->cdclk =
5919 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305920
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005921 return 0;
5922}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005923
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005924static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5925{
5926 struct drm_device *dev = state->dev;
5927 struct drm_i915_private *dev_priv = dev->dev_private;
5928 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005929
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005930 if (max_pixclk < 0)
5931 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005932
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005933 to_intel_atomic_state(state)->cdclk =
5934 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005935
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005936 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005937}
5938
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005939static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5940{
5941 unsigned int credits, default_credits;
5942
5943 if (IS_CHERRYVIEW(dev_priv))
5944 default_credits = PFI_CREDIT(12);
5945 else
5946 default_credits = PFI_CREDIT(8);
5947
Vandana Kannan164dfd22014-11-24 13:37:41 +05305948 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005949 /* CHV suggested value is 31 or 63 */
5950 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005951 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005952 else
5953 credits = PFI_CREDIT(15);
5954 } else {
5955 credits = default_credits;
5956 }
5957
5958 /*
5959 * WA - write default credits before re-programming
5960 * FIXME: should we also set the resend bit here?
5961 */
5962 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5963 default_credits);
5964
5965 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5966 credits | PFI_CREDIT_RESEND);
5967
5968 /*
5969 * FIXME is this guaranteed to clear
5970 * immediately or should we poll for it?
5971 */
5972 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5973}
5974
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005975static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005976{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005977 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005978 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005979 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005980
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005981 /*
5982 * FIXME: We can end up here with all power domains off, yet
5983 * with a CDCLK frequency other than the minimum. To account
5984 * for this take the PIPE-A power domain, which covers the HW
5985 * blocks needed for the following programming. This can be
5986 * removed once it's guaranteed that we get here either with
5987 * the minimum CDCLK set, or the required power domains
5988 * enabled.
5989 */
5990 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005991
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005992 if (IS_CHERRYVIEW(dev))
5993 cherryview_set_cdclk(dev, req_cdclk);
5994 else
5995 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005996
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005997 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02005998
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005999 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006000}
6001
Jesse Barnes89b667f2013-04-18 14:51:36 -07006002static void valleyview_crtc_enable(struct drm_crtc *crtc)
6003{
6004 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006005 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006006 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6007 struct intel_encoder *encoder;
6008 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006009 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006010
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006011 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006012 return;
6013
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006014 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306015
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006016 if (!is_dsi) {
6017 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006018 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006019 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006020 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006021 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006022
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006023 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306024 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006025
6026 intel_set_pipe_timings(intel_crtc);
6027
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006028 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6029 struct drm_i915_private *dev_priv = dev->dev_private;
6030
6031 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6032 I915_WRITE(CHV_CANVAS(pipe), 0);
6033 }
6034
Daniel Vetter5b18e572014-04-24 23:55:06 +02006035 i9xx_set_pipeconf(intel_crtc);
6036
Jesse Barnes89b667f2013-04-18 14:51:36 -07006037 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038
Daniel Vettera72e4c92014-09-30 10:56:47 +02006039 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006040
Jesse Barnes89b667f2013-04-18 14:51:36 -07006041 for_each_encoder_on_crtc(dev, crtc, encoder)
6042 if (encoder->pre_pll_enable)
6043 encoder->pre_pll_enable(encoder);
6044
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006045 if (!is_dsi) {
6046 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006047 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006048 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006049 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006050 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006051
6052 for_each_encoder_on_crtc(dev, crtc, encoder)
6053 if (encoder->pre_enable)
6054 encoder->pre_enable(encoder);
6055
Jesse Barnes2dd24552013-04-25 12:55:01 -07006056 i9xx_pfit_enable(intel_crtc);
6057
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006058 intel_crtc_load_lut(crtc);
6059
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006060 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006061
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006062 assert_vblank_disabled(crtc);
6063 drm_crtc_vblank_on(crtc);
6064
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006065 for_each_encoder_on_crtc(dev, crtc, encoder)
6066 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006067}
6068
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006069static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6070{
6071 struct drm_device *dev = crtc->base.dev;
6072 struct drm_i915_private *dev_priv = dev->dev_private;
6073
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006074 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6075 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006076}
6077
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006078static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006079{
6080 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006081 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006083 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006084 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006085
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006086 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006087 return;
6088
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006089 i9xx_set_pll_dividers(intel_crtc);
6090
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006091 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306092 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006093
6094 intel_set_pipe_timings(intel_crtc);
6095
Daniel Vetter5b18e572014-04-24 23:55:06 +02006096 i9xx_set_pipeconf(intel_crtc);
6097
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006098 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006099
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006100 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006101 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006102
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006103 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006104 if (encoder->pre_enable)
6105 encoder->pre_enable(encoder);
6106
Daniel Vetterf6736a12013-06-05 13:34:30 +02006107 i9xx_enable_pll(intel_crtc);
6108
Jesse Barnes2dd24552013-04-25 12:55:01 -07006109 i9xx_pfit_enable(intel_crtc);
6110
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006111 intel_crtc_load_lut(crtc);
6112
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006113 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006114 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006115
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006116 assert_vblank_disabled(crtc);
6117 drm_crtc_vblank_on(crtc);
6118
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006119 for_each_encoder_on_crtc(dev, crtc, encoder)
6120 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006121}
6122
Daniel Vetter87476d62013-04-11 16:29:06 +02006123static void i9xx_pfit_disable(struct intel_crtc *crtc)
6124{
6125 struct drm_device *dev = crtc->base.dev;
6126 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006127
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006128 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006129 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006130
6131 assert_pipe_disabled(dev_priv, crtc->pipe);
6132
Daniel Vetter328d8e82013-05-08 10:36:31 +02006133 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6134 I915_READ(PFIT_CONTROL));
6135 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006136}
6137
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006138static void i9xx_crtc_disable(struct drm_crtc *crtc)
6139{
6140 struct drm_device *dev = crtc->dev;
6141 struct drm_i915_private *dev_priv = dev->dev_private;
6142 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006143 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006144 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006145
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006146 /*
6147 * On gen2 planes are double buffered but the pipe isn't, so we must
6148 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006149 * We also need to wait on all gmch platforms because of the
6150 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006151 */
Imre Deak564ed192014-06-13 14:54:21 +03006152 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006153
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006154 for_each_encoder_on_crtc(dev, crtc, encoder)
6155 encoder->disable(encoder);
6156
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006157 drm_crtc_vblank_off(crtc);
6158 assert_vblank_disabled(crtc);
6159
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006160 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006161
Daniel Vetter87476d62013-04-11 16:29:06 +02006162 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006163
Jesse Barnes89b667f2013-04-18 14:51:36 -07006164 for_each_encoder_on_crtc(dev, crtc, encoder)
6165 if (encoder->post_disable)
6166 encoder->post_disable(encoder);
6167
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006168 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006169 if (IS_CHERRYVIEW(dev))
6170 chv_disable_pll(dev_priv, pipe);
6171 else if (IS_VALLEYVIEW(dev))
6172 vlv_disable_pll(dev_priv, pipe);
6173 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006174 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006175 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006176
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006177 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006178 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02006179
6180 intel_crtc->active = false;
6181 intel_update_watermarks(crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006182}
6183
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006184static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006185{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006187 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006188 enum intel_display_power_domain domain;
6189 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006190
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006191 if (!intel_crtc->active)
6192 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006193
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006194 if (to_intel_plane_state(crtc->primary->state)->visible) {
6195 intel_crtc_wait_for_pending_flips(crtc);
6196 intel_pre_disable_primary(crtc);
6197 }
6198
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006199 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006200 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006201
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006202 domains = intel_crtc->enabled_power_domains;
6203 for_each_power_domain(domain, domains)
6204 intel_display_power_put(dev_priv, domain);
6205 intel_crtc->enabled_power_domains = 0;
6206}
6207
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006208/*
6209 * turn all crtc's off, but do not adjust state
6210 * This has to be paired with a call to intel_modeset_setup_hw_state.
6211 */
Maarten Lankhorst9716c692015-06-10 10:24:19 +02006212void intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006213{
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006214 struct drm_crtc *crtc;
6215
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006216 for_each_crtc(dev, crtc)
6217 intel_crtc_disable_noatomic(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006218}
6219
Chris Wilsoncdd59982010-09-08 16:30:16 +01006220/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006221int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006222{
6223 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006224 struct drm_mode_config *config = &dev->mode_config;
6225 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006226 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006227 struct intel_crtc_state *pipe_config;
6228 struct drm_atomic_state *state;
6229 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006230
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006231 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006232 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006233
6234 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006235 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006236
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006237 /* this function should be called with drm_modeset_lock_all for now */
6238 if (WARN_ON(!ctx))
6239 return -EIO;
6240 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006241
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006242 state = drm_atomic_state_alloc(dev);
6243 if (WARN_ON(!state))
6244 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006245
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006246 state->acquire_ctx = ctx;
6247 state->allow_modeset = true;
6248
6249 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6250 if (IS_ERR(pipe_config)) {
6251 ret = PTR_ERR(pipe_config);
6252 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006253 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006254 pipe_config->base.active = enable;
6255
6256 ret = intel_set_mode(state);
6257 if (!ret)
6258 return ret;
6259
6260err:
6261 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6262 drm_atomic_state_free(state);
6263 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306264}
6265
6266/**
6267 * Sets the power management mode of the pipe and plane.
6268 */
6269void intel_crtc_update_dpms(struct drm_crtc *crtc)
6270{
6271 struct drm_device *dev = crtc->dev;
6272 struct intel_encoder *intel_encoder;
6273 bool enable = false;
6274
6275 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6276 enable |= intel_encoder->connectors_active;
6277
6278 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006279}
6280
Chris Wilsonea5b2132010-08-04 13:50:23 +01006281void intel_encoder_destroy(struct drm_encoder *encoder)
6282{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006283 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006284
Chris Wilsonea5b2132010-08-04 13:50:23 +01006285 drm_encoder_cleanup(encoder);
6286 kfree(intel_encoder);
6287}
6288
Damien Lespiau92373292013-08-08 22:28:57 +01006289/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006290 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6291 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006292static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006293{
6294 if (mode == DRM_MODE_DPMS_ON) {
6295 encoder->connectors_active = true;
6296
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006297 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006298 } else {
6299 encoder->connectors_active = false;
6300
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006301 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006302 }
6303}
6304
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006305/* Cross check the actual hw state with our own modeset state tracking (and it's
6306 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006307static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006308{
6309 if (connector->get_hw_state(connector)) {
6310 struct intel_encoder *encoder = connector->encoder;
6311 struct drm_crtc *crtc;
6312 bool encoder_enabled;
6313 enum pipe pipe;
6314
6315 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6316 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006317 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006318
Dave Airlie0e32b392014-05-02 14:02:48 +10006319 /* there is no real hw state for MST connectors */
6320 if (connector->mst_port)
6321 return;
6322
Rob Clarke2c719b2014-12-15 13:56:32 -05006323 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006324 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006325 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006326 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006327
Dave Airlie36cd7442014-05-02 13:44:18 +10006328 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006329 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006330 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006331
Dave Airlie36cd7442014-05-02 13:44:18 +10006332 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006333 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6334 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006335 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006336
Dave Airlie36cd7442014-05-02 13:44:18 +10006337 crtc = encoder->base.crtc;
6338
Matt Roper83d65732015-02-25 13:12:16 -08006339 I915_STATE_WARN(!crtc->state->enable,
6340 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006341 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6342 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006343 "encoder active on the wrong pipe\n");
6344 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006345 }
6346}
6347
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006348int intel_connector_init(struct intel_connector *connector)
6349{
6350 struct drm_connector_state *connector_state;
6351
6352 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6353 if (!connector_state)
6354 return -ENOMEM;
6355
6356 connector->base.state = connector_state;
6357 return 0;
6358}
6359
6360struct intel_connector *intel_connector_alloc(void)
6361{
6362 struct intel_connector *connector;
6363
6364 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6365 if (!connector)
6366 return NULL;
6367
6368 if (intel_connector_init(connector) < 0) {
6369 kfree(connector);
6370 return NULL;
6371 }
6372
6373 return connector;
6374}
6375
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006376/* Even simpler default implementation, if there's really no special case to
6377 * consider. */
6378void intel_connector_dpms(struct drm_connector *connector, int mode)
6379{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006380 /* All the simple cases only support two dpms states. */
6381 if (mode != DRM_MODE_DPMS_ON)
6382 mode = DRM_MODE_DPMS_OFF;
6383
6384 if (mode == connector->dpms)
6385 return;
6386
6387 connector->dpms = mode;
6388
6389 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006390 if (connector->encoder)
6391 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006392
Daniel Vetterb9805142012-08-31 17:37:33 +02006393 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006394}
6395
Daniel Vetterf0947c32012-07-02 13:10:34 +02006396/* Simple connector->get_hw_state implementation for encoders that support only
6397 * one connector and no cloning and hence the encoder state determines the state
6398 * of the connector. */
6399bool intel_connector_get_hw_state(struct intel_connector *connector)
6400{
Daniel Vetter24929352012-07-02 20:28:59 +02006401 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006402 struct intel_encoder *encoder = connector->encoder;
6403
6404 return encoder->get_hw_state(encoder, &pipe);
6405}
6406
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006407static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006408{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006409 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6410 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006411
6412 return 0;
6413}
6414
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006415static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006416 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006417{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006418 struct drm_atomic_state *state = pipe_config->base.state;
6419 struct intel_crtc *other_crtc;
6420 struct intel_crtc_state *other_crtc_state;
6421
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006422 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6423 pipe_name(pipe), pipe_config->fdi_lanes);
6424 if (pipe_config->fdi_lanes > 4) {
6425 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6426 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006427 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006428 }
6429
Paulo Zanonibafb6552013-11-02 21:07:44 -07006430 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006431 if (pipe_config->fdi_lanes > 2) {
6432 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6433 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006434 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006435 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006436 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006437 }
6438 }
6439
6440 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006441 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006442
6443 /* Ivybridge 3 pipe is really complicated */
6444 switch (pipe) {
6445 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006446 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006447 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006448 if (pipe_config->fdi_lanes <= 2)
6449 return 0;
6450
6451 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6452 other_crtc_state =
6453 intel_atomic_get_crtc_state(state, other_crtc);
6454 if (IS_ERR(other_crtc_state))
6455 return PTR_ERR(other_crtc_state);
6456
6457 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006458 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6459 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006460 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006461 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006462 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006463 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006464 if (pipe_config->fdi_lanes > 2) {
6465 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6466 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006467 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006468 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006469
6470 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6471 other_crtc_state =
6472 intel_atomic_get_crtc_state(state, other_crtc);
6473 if (IS_ERR(other_crtc_state))
6474 return PTR_ERR(other_crtc_state);
6475
6476 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006477 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006478 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006479 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006480 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006481 default:
6482 BUG();
6483 }
6484}
6485
Daniel Vettere29c22c2013-02-21 00:00:16 +01006486#define RETRY 1
6487static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006488 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006489{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006490 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006491 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006492 int lane, link_bw, fdi_dotclock, ret;
6493 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006494
Daniel Vettere29c22c2013-02-21 00:00:16 +01006495retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006496 /* FDI is a binary signal running at ~2.7GHz, encoding
6497 * each output octet as 10 bits. The actual frequency
6498 * is stored as a divider into a 100MHz clock, and the
6499 * mode pixel clock is stored in units of 1KHz.
6500 * Hence the bw of each lane in terms of the mode signal
6501 * is:
6502 */
6503 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6504
Damien Lespiau241bfc32013-09-25 16:45:37 +01006505 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006506
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006507 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006508 pipe_config->pipe_bpp);
6509
6510 pipe_config->fdi_lanes = lane;
6511
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006512 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006513 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006514
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006515 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6516 intel_crtc->pipe, pipe_config);
6517 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006518 pipe_config->pipe_bpp -= 2*3;
6519 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6520 pipe_config->pipe_bpp);
6521 needs_recompute = true;
6522 pipe_config->bw_constrained = true;
6523
6524 goto retry;
6525 }
6526
6527 if (needs_recompute)
6528 return RETRY;
6529
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006530 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006531}
6532
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006533static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6534 struct intel_crtc_state *pipe_config)
6535{
6536 if (pipe_config->pipe_bpp > 24)
6537 return false;
6538
6539 /* HSW can handle pixel rate up to cdclk? */
6540 if (IS_HASWELL(dev_priv->dev))
6541 return true;
6542
6543 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006544 * We compare against max which means we must take
6545 * the increased cdclk requirement into account when
6546 * calculating the new cdclk.
6547 *
6548 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006549 */
6550 return ilk_pipe_pixel_rate(pipe_config) <=
6551 dev_priv->max_cdclk_freq * 95 / 100;
6552}
6553
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006554static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006555 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006556{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006557 struct drm_device *dev = crtc->base.dev;
6558 struct drm_i915_private *dev_priv = dev->dev_private;
6559
Jani Nikulad330a952014-01-21 11:24:25 +02006560 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006561 hsw_crtc_supports_ips(crtc) &&
6562 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006563}
6564
Daniel Vettera43f6e02013-06-07 23:10:32 +02006565static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006566 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006567{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006568 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006569 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006570 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006571
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006572 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006573 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006574 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006575
6576 /*
6577 * Enable pixel doubling when the dot clock
6578 * is > 90% of the (display) core speed.
6579 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006580 * GDG double wide on either pipe,
6581 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006582 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006583 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006584 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006585 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006586 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006587 }
6588
Damien Lespiau241bfc32013-09-25 16:45:37 +01006589 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006590 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006591 }
Chris Wilson89749352010-09-12 18:25:19 +01006592
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006593 /*
6594 * Pipe horizontal size must be even in:
6595 * - DVO ganged mode
6596 * - LVDS dual channel mode
6597 * - Double wide pipe
6598 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006599 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006600 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6601 pipe_config->pipe_src_w &= ~1;
6602
Damien Lespiau8693a822013-05-03 18:48:11 +01006603 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6604 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006605 */
6606 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6607 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006608 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006609
Damien Lespiauf5adf942013-06-24 18:29:34 +01006610 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006611 hsw_compute_ips_config(crtc, pipe_config);
6612
Daniel Vetter877d48d2013-04-19 11:24:43 +02006613 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006614 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006615
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006616 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006617}
6618
Ville Syrjälä1652d192015-03-31 14:12:01 +03006619static int skylake_get_display_clock_speed(struct drm_device *dev)
6620{
6621 struct drm_i915_private *dev_priv = to_i915(dev);
6622 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6623 uint32_t cdctl = I915_READ(CDCLK_CTL);
6624 uint32_t linkrate;
6625
Damien Lespiau414355a2015-06-04 18:21:31 +01006626 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006627 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006628
6629 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6630 return 540000;
6631
6632 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006633 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006634
Damien Lespiau71cd8422015-04-30 16:39:17 +01006635 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6636 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006637 /* vco 8640 */
6638 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6639 case CDCLK_FREQ_450_432:
6640 return 432000;
6641 case CDCLK_FREQ_337_308:
6642 return 308570;
6643 case CDCLK_FREQ_675_617:
6644 return 617140;
6645 default:
6646 WARN(1, "Unknown cd freq selection\n");
6647 }
6648 } else {
6649 /* vco 8100 */
6650 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6651 case CDCLK_FREQ_450_432:
6652 return 450000;
6653 case CDCLK_FREQ_337_308:
6654 return 337500;
6655 case CDCLK_FREQ_675_617:
6656 return 675000;
6657 default:
6658 WARN(1, "Unknown cd freq selection\n");
6659 }
6660 }
6661
6662 /* error case, do as if DPLL0 isn't enabled */
6663 return 24000;
6664}
6665
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006666static int broxton_get_display_clock_speed(struct drm_device *dev)
6667{
6668 struct drm_i915_private *dev_priv = to_i915(dev);
6669 uint32_t cdctl = I915_READ(CDCLK_CTL);
6670 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6671 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6672 int cdclk;
6673
6674 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6675 return 19200;
6676
6677 cdclk = 19200 * pll_ratio / 2;
6678
6679 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6680 case BXT_CDCLK_CD2X_DIV_SEL_1:
6681 return cdclk; /* 576MHz or 624MHz */
6682 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6683 return cdclk * 2 / 3; /* 384MHz */
6684 case BXT_CDCLK_CD2X_DIV_SEL_2:
6685 return cdclk / 2; /* 288MHz */
6686 case BXT_CDCLK_CD2X_DIV_SEL_4:
6687 return cdclk / 4; /* 144MHz */
6688 }
6689
6690 /* error case, do as if DE PLL isn't enabled */
6691 return 19200;
6692}
6693
Ville Syrjälä1652d192015-03-31 14:12:01 +03006694static int broadwell_get_display_clock_speed(struct drm_device *dev)
6695{
6696 struct drm_i915_private *dev_priv = dev->dev_private;
6697 uint32_t lcpll = I915_READ(LCPLL_CTL);
6698 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6699
6700 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6701 return 800000;
6702 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6703 return 450000;
6704 else if (freq == LCPLL_CLK_FREQ_450)
6705 return 450000;
6706 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6707 return 540000;
6708 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6709 return 337500;
6710 else
6711 return 675000;
6712}
6713
6714static int haswell_get_display_clock_speed(struct drm_device *dev)
6715{
6716 struct drm_i915_private *dev_priv = dev->dev_private;
6717 uint32_t lcpll = I915_READ(LCPLL_CTL);
6718 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6719
6720 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6721 return 800000;
6722 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6723 return 450000;
6724 else if (freq == LCPLL_CLK_FREQ_450)
6725 return 450000;
6726 else if (IS_HSW_ULT(dev))
6727 return 337500;
6728 else
6729 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006730}
6731
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006732static int valleyview_get_display_clock_speed(struct drm_device *dev)
6733{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006734 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006735 u32 val;
6736 int divider;
6737
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006738 if (dev_priv->hpll_freq == 0)
6739 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6740
Ville Syrjäläa5805162015-05-26 20:42:30 +03006741 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006742 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006743 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006744
6745 divider = val & DISPLAY_FREQUENCY_VALUES;
6746
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006747 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6748 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6749 "cdclk change in progress\n");
6750
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006751 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006752}
6753
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006754static int ilk_get_display_clock_speed(struct drm_device *dev)
6755{
6756 return 450000;
6757}
6758
Jesse Barnese70236a2009-09-21 10:42:27 -07006759static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006760{
Jesse Barnese70236a2009-09-21 10:42:27 -07006761 return 400000;
6762}
Jesse Barnes79e53942008-11-07 14:24:08 -08006763
Jesse Barnese70236a2009-09-21 10:42:27 -07006764static int i915_get_display_clock_speed(struct drm_device *dev)
6765{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006766 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006767}
Jesse Barnes79e53942008-11-07 14:24:08 -08006768
Jesse Barnese70236a2009-09-21 10:42:27 -07006769static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6770{
6771 return 200000;
6772}
Jesse Barnes79e53942008-11-07 14:24:08 -08006773
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006774static int pnv_get_display_clock_speed(struct drm_device *dev)
6775{
6776 u16 gcfgc = 0;
6777
6778 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6779
6780 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6781 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006782 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006783 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006784 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006785 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006786 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006787 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6788 return 200000;
6789 default:
6790 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6791 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006792 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006793 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006794 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006795 }
6796}
6797
Jesse Barnese70236a2009-09-21 10:42:27 -07006798static int i915gm_get_display_clock_speed(struct drm_device *dev)
6799{
6800 u16 gcfgc = 0;
6801
6802 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6803
6804 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006805 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006806 else {
6807 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6808 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006809 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006810 default:
6811 case GC_DISPLAY_CLOCK_190_200_MHZ:
6812 return 190000;
6813 }
6814 }
6815}
Jesse Barnes79e53942008-11-07 14:24:08 -08006816
Jesse Barnese70236a2009-09-21 10:42:27 -07006817static int i865_get_display_clock_speed(struct drm_device *dev)
6818{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006819 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006820}
6821
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006822static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006823{
6824 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006825
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006826 /*
6827 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6828 * encoding is different :(
6829 * FIXME is this the right way to detect 852GM/852GMV?
6830 */
6831 if (dev->pdev->revision == 0x1)
6832 return 133333;
6833
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006834 pci_bus_read_config_word(dev->pdev->bus,
6835 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6836
Jesse Barnese70236a2009-09-21 10:42:27 -07006837 /* Assume that the hardware is in the high speed state. This
6838 * should be the default.
6839 */
6840 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6841 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006842 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006843 case GC_CLOCK_100_200:
6844 return 200000;
6845 case GC_CLOCK_166_250:
6846 return 250000;
6847 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006848 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006849 case GC_CLOCK_133_266:
6850 case GC_CLOCK_133_266_2:
6851 case GC_CLOCK_166_266:
6852 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006853 }
6854
6855 /* Shouldn't happen */
6856 return 0;
6857}
6858
6859static int i830_get_display_clock_speed(struct drm_device *dev)
6860{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006861 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006862}
6863
Ville Syrjälä34edce22015-05-22 11:22:33 +03006864static unsigned int intel_hpll_vco(struct drm_device *dev)
6865{
6866 struct drm_i915_private *dev_priv = dev->dev_private;
6867 static const unsigned int blb_vco[8] = {
6868 [0] = 3200000,
6869 [1] = 4000000,
6870 [2] = 5333333,
6871 [3] = 4800000,
6872 [4] = 6400000,
6873 };
6874 static const unsigned int pnv_vco[8] = {
6875 [0] = 3200000,
6876 [1] = 4000000,
6877 [2] = 5333333,
6878 [3] = 4800000,
6879 [4] = 2666667,
6880 };
6881 static const unsigned int cl_vco[8] = {
6882 [0] = 3200000,
6883 [1] = 4000000,
6884 [2] = 5333333,
6885 [3] = 6400000,
6886 [4] = 3333333,
6887 [5] = 3566667,
6888 [6] = 4266667,
6889 };
6890 static const unsigned int elk_vco[8] = {
6891 [0] = 3200000,
6892 [1] = 4000000,
6893 [2] = 5333333,
6894 [3] = 4800000,
6895 };
6896 static const unsigned int ctg_vco[8] = {
6897 [0] = 3200000,
6898 [1] = 4000000,
6899 [2] = 5333333,
6900 [3] = 6400000,
6901 [4] = 2666667,
6902 [5] = 4266667,
6903 };
6904 const unsigned int *vco_table;
6905 unsigned int vco;
6906 uint8_t tmp = 0;
6907
6908 /* FIXME other chipsets? */
6909 if (IS_GM45(dev))
6910 vco_table = ctg_vco;
6911 else if (IS_G4X(dev))
6912 vco_table = elk_vco;
6913 else if (IS_CRESTLINE(dev))
6914 vco_table = cl_vco;
6915 else if (IS_PINEVIEW(dev))
6916 vco_table = pnv_vco;
6917 else if (IS_G33(dev))
6918 vco_table = blb_vco;
6919 else
6920 return 0;
6921
6922 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6923
6924 vco = vco_table[tmp & 0x7];
6925 if (vco == 0)
6926 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6927 else
6928 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6929
6930 return vco;
6931}
6932
6933static int gm45_get_display_clock_speed(struct drm_device *dev)
6934{
6935 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6936 uint16_t tmp = 0;
6937
6938 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6939
6940 cdclk_sel = (tmp >> 12) & 0x1;
6941
6942 switch (vco) {
6943 case 2666667:
6944 case 4000000:
6945 case 5333333:
6946 return cdclk_sel ? 333333 : 222222;
6947 case 3200000:
6948 return cdclk_sel ? 320000 : 228571;
6949 default:
6950 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6951 return 222222;
6952 }
6953}
6954
6955static int i965gm_get_display_clock_speed(struct drm_device *dev)
6956{
6957 static const uint8_t div_3200[] = { 16, 10, 8 };
6958 static const uint8_t div_4000[] = { 20, 12, 10 };
6959 static const uint8_t div_5333[] = { 24, 16, 14 };
6960 const uint8_t *div_table;
6961 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6962 uint16_t tmp = 0;
6963
6964 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6965
6966 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6967
6968 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6969 goto fail;
6970
6971 switch (vco) {
6972 case 3200000:
6973 div_table = div_3200;
6974 break;
6975 case 4000000:
6976 div_table = div_4000;
6977 break;
6978 case 5333333:
6979 div_table = div_5333;
6980 break;
6981 default:
6982 goto fail;
6983 }
6984
6985 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6986
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006987fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006988 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6989 return 200000;
6990}
6991
6992static int g33_get_display_clock_speed(struct drm_device *dev)
6993{
6994 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6995 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6996 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6997 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6998 const uint8_t *div_table;
6999 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7000 uint16_t tmp = 0;
7001
7002 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7003
7004 cdclk_sel = (tmp >> 4) & 0x7;
7005
7006 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7007 goto fail;
7008
7009 switch (vco) {
7010 case 3200000:
7011 div_table = div_3200;
7012 break;
7013 case 4000000:
7014 div_table = div_4000;
7015 break;
7016 case 4800000:
7017 div_table = div_4800;
7018 break;
7019 case 5333333:
7020 div_table = div_5333;
7021 break;
7022 default:
7023 goto fail;
7024 }
7025
7026 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7027
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007028fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007029 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7030 return 190476;
7031}
7032
Zhenyu Wang2c072452009-06-05 15:38:42 +08007033static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007034intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007035{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007036 while (*num > DATA_LINK_M_N_MASK ||
7037 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007038 *num >>= 1;
7039 *den >>= 1;
7040 }
7041}
7042
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007043static void compute_m_n(unsigned int m, unsigned int n,
7044 uint32_t *ret_m, uint32_t *ret_n)
7045{
7046 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7047 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7048 intel_reduce_m_n_ratio(ret_m, ret_n);
7049}
7050
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007051void
7052intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7053 int pixel_clock, int link_clock,
7054 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007055{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007056 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007057
7058 compute_m_n(bits_per_pixel * pixel_clock,
7059 link_clock * nlanes * 8,
7060 &m_n->gmch_m, &m_n->gmch_n);
7061
7062 compute_m_n(pixel_clock, link_clock,
7063 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007064}
7065
Chris Wilsona7615032011-01-12 17:04:08 +00007066static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7067{
Jani Nikulad330a952014-01-21 11:24:25 +02007068 if (i915.panel_use_ssc >= 0)
7069 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007070 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007071 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007072}
7073
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007074static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7075 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007076{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007077 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007078 struct drm_i915_private *dev_priv = dev->dev_private;
7079 int refclk;
7080
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007081 WARN_ON(!crtc_state->base.state);
7082
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007083 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007084 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007085 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007086 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007087 refclk = dev_priv->vbt.lvds_ssc_freq;
7088 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007089 } else if (!IS_GEN2(dev)) {
7090 refclk = 96000;
7091 } else {
7092 refclk = 48000;
7093 }
7094
7095 return refclk;
7096}
7097
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007098static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007099{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007100 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007101}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007102
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007103static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7104{
7105 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007106}
7107
Daniel Vetterf47709a2013-03-28 10:42:02 +01007108static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007109 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007110 intel_clock_t *reduced_clock)
7111{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007112 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007113 u32 fp, fp2 = 0;
7114
7115 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007116 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007117 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007118 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007119 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007120 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007121 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007122 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007123 }
7124
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007125 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007126
Daniel Vetterf47709a2013-03-28 10:42:02 +01007127 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007128 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007129 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007130 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007131 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007132 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007133 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007134 }
7135}
7136
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007137static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7138 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007139{
7140 u32 reg_val;
7141
7142 /*
7143 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7144 * and set it to a reasonable value instead.
7145 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007146 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007147 reg_val &= 0xffffff00;
7148 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007149 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007150
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007151 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007152 reg_val &= 0x8cffffff;
7153 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007154 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007155
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007156 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007157 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007158 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007159
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007160 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007161 reg_val &= 0x00ffffff;
7162 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007163 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007164}
7165
Daniel Vetterb5518422013-05-03 11:49:48 +02007166static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7167 struct intel_link_m_n *m_n)
7168{
7169 struct drm_device *dev = crtc->base.dev;
7170 struct drm_i915_private *dev_priv = dev->dev_private;
7171 int pipe = crtc->pipe;
7172
Daniel Vettere3b95f12013-05-03 11:49:49 +02007173 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7174 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7175 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7176 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007177}
7178
7179static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007180 struct intel_link_m_n *m_n,
7181 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007182{
7183 struct drm_device *dev = crtc->base.dev;
7184 struct drm_i915_private *dev_priv = dev->dev_private;
7185 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007186 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007187
7188 if (INTEL_INFO(dev)->gen >= 5) {
7189 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7190 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7191 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7192 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007193 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7194 * for gen < 8) and if DRRS is supported (to make sure the
7195 * registers are not unnecessarily accessed).
7196 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307197 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007198 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007199 I915_WRITE(PIPE_DATA_M2(transcoder),
7200 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7201 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7202 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7203 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7204 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007205 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007206 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7207 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7208 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7209 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007210 }
7211}
7212
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307213void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007214{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307215 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7216
7217 if (m_n == M1_N1) {
7218 dp_m_n = &crtc->config->dp_m_n;
7219 dp_m2_n2 = &crtc->config->dp_m2_n2;
7220 } else if (m_n == M2_N2) {
7221
7222 /*
7223 * M2_N2 registers are not supported. Hence m2_n2 divider value
7224 * needs to be programmed into M1_N1.
7225 */
7226 dp_m_n = &crtc->config->dp_m2_n2;
7227 } else {
7228 DRM_ERROR("Unsupported divider value\n");
7229 return;
7230 }
7231
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007232 if (crtc->config->has_pch_encoder)
7233 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007234 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307235 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007236}
7237
Daniel Vetter251ac862015-06-18 10:30:24 +02007238static void vlv_compute_dpll(struct intel_crtc *crtc,
7239 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007240{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007241 u32 dpll, dpll_md;
7242
7243 /*
7244 * Enable DPIO clock input. We should never disable the reference
7245 * clock for pipe B, since VGA hotplug / manual detection depends
7246 * on it.
7247 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007248 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7249 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007250 /* We should never disable this, set it here for state tracking */
7251 if (crtc->pipe == PIPE_B)
7252 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7253 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007254 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007255
Ville Syrjäläd288f652014-10-28 13:20:22 +02007256 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007257 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007258 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007259}
7260
Ville Syrjäläd288f652014-10-28 13:20:22 +02007261static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007262 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007263{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007264 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007265 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007266 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007267 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007268 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007269 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007270
Ville Syrjäläa5805162015-05-26 20:42:30 +03007271 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007272
Ville Syrjäläd288f652014-10-28 13:20:22 +02007273 bestn = pipe_config->dpll.n;
7274 bestm1 = pipe_config->dpll.m1;
7275 bestm2 = pipe_config->dpll.m2;
7276 bestp1 = pipe_config->dpll.p1;
7277 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007278
Jesse Barnes89b667f2013-04-18 14:51:36 -07007279 /* See eDP HDMI DPIO driver vbios notes doc */
7280
7281 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007282 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007283 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007284
7285 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007286 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007287
7288 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007289 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007290 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007291 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007292
7293 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007294 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007295
7296 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007297 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7298 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7299 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007300 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007301
7302 /*
7303 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7304 * but we don't support that).
7305 * Note: don't use the DAC post divider as it seems unstable.
7306 */
7307 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007308 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007309
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007310 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007311 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007312
Jesse Barnes89b667f2013-04-18 14:51:36 -07007313 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007314 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007315 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7316 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007317 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007318 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007319 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007320 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007321 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007322
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007323 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007324 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007325 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007326 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007327 0x0df40000);
7328 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007329 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007330 0x0df70000);
7331 } else { /* HDMI or VGA */
7332 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007333 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007334 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007335 0x0df70000);
7336 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007337 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007338 0x0df40000);
7339 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007340
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007341 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007342 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007343 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7344 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007345 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007346 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007347
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007348 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007349 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007350}
7351
Daniel Vetter251ac862015-06-18 10:30:24 +02007352static void chv_compute_dpll(struct intel_crtc *crtc,
7353 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007354{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007355 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7356 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007357 DPLL_VCO_ENABLE;
7358 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007359 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007360
Ville Syrjäläd288f652014-10-28 13:20:22 +02007361 pipe_config->dpll_hw_state.dpll_md =
7362 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007363}
7364
Ville Syrjäläd288f652014-10-28 13:20:22 +02007365static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007366 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007367{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007368 struct drm_device *dev = crtc->base.dev;
7369 struct drm_i915_private *dev_priv = dev->dev_private;
7370 int pipe = crtc->pipe;
7371 int dpll_reg = DPLL(crtc->pipe);
7372 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307373 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007374 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307375 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307376 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007377
Ville Syrjäläd288f652014-10-28 13:20:22 +02007378 bestn = pipe_config->dpll.n;
7379 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7380 bestm1 = pipe_config->dpll.m1;
7381 bestm2 = pipe_config->dpll.m2 >> 22;
7382 bestp1 = pipe_config->dpll.p1;
7383 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307384 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307385 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307386 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007387
7388 /*
7389 * Enable Refclk and SSC
7390 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007391 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007392 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007393
Ville Syrjäläa5805162015-05-26 20:42:30 +03007394 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007395
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007396 /* p1 and p2 divider */
7397 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7398 5 << DPIO_CHV_S1_DIV_SHIFT |
7399 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7400 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7401 1 << DPIO_CHV_K_DIV_SHIFT);
7402
7403 /* Feedback post-divider - m2 */
7404 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7405
7406 /* Feedback refclk divider - n and m1 */
7407 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7408 DPIO_CHV_M1_DIV_BY_2 |
7409 1 << DPIO_CHV_N_DIV_SHIFT);
7410
7411 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307412 if (bestm2_frac)
7413 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007414
7415 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307416 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7417 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7418 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7419 if (bestm2_frac)
7420 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7421 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007422
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307423 /* Program digital lock detect threshold */
7424 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7425 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7426 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7427 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7428 if (!bestm2_frac)
7429 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7430 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7431
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007432 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307433 if (vco == 5400000) {
7434 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7435 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7436 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7437 tribuf_calcntr = 0x9;
7438 } else if (vco <= 6200000) {
7439 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7440 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7441 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7442 tribuf_calcntr = 0x9;
7443 } else if (vco <= 6480000) {
7444 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7445 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7446 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7447 tribuf_calcntr = 0x8;
7448 } else {
7449 /* Not supported. Apply the same limits as in the max case */
7450 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7451 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7452 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7453 tribuf_calcntr = 0;
7454 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007455 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7456
Ville Syrjälä968040b2015-03-11 22:52:08 +02007457 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307458 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7459 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7460 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7461
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007462 /* AFC Recal */
7463 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7464 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7465 DPIO_AFC_RECAL);
7466
Ville Syrjäläa5805162015-05-26 20:42:30 +03007467 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007468}
7469
Ville Syrjäläd288f652014-10-28 13:20:22 +02007470/**
7471 * vlv_force_pll_on - forcibly enable just the PLL
7472 * @dev_priv: i915 private structure
7473 * @pipe: pipe PLL to enable
7474 * @dpll: PLL configuration
7475 *
7476 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7477 * in cases where we need the PLL enabled even when @pipe is not going to
7478 * be enabled.
7479 */
7480void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7481 const struct dpll *dpll)
7482{
7483 struct intel_crtc *crtc =
7484 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007485 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007486 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007487 .pixel_multiplier = 1,
7488 .dpll = *dpll,
7489 };
7490
7491 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007492 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007493 chv_prepare_pll(crtc, &pipe_config);
7494 chv_enable_pll(crtc, &pipe_config);
7495 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007496 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007497 vlv_prepare_pll(crtc, &pipe_config);
7498 vlv_enable_pll(crtc, &pipe_config);
7499 }
7500}
7501
7502/**
7503 * vlv_force_pll_off - forcibly disable just the PLL
7504 * @dev_priv: i915 private structure
7505 * @pipe: pipe PLL to disable
7506 *
7507 * Disable the PLL for @pipe. To be used in cases where we need
7508 * the PLL enabled even when @pipe is not going to be enabled.
7509 */
7510void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7511{
7512 if (IS_CHERRYVIEW(dev))
7513 chv_disable_pll(to_i915(dev), pipe);
7514 else
7515 vlv_disable_pll(to_i915(dev), pipe);
7516}
7517
Daniel Vetter251ac862015-06-18 10:30:24 +02007518static void i9xx_compute_dpll(struct intel_crtc *crtc,
7519 struct intel_crtc_state *crtc_state,
7520 intel_clock_t *reduced_clock,
7521 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007522{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007523 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007524 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007525 u32 dpll;
7526 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007527 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007528
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007529 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307530
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007531 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7532 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007533
7534 dpll = DPLL_VGA_MODE_DIS;
7535
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007536 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007537 dpll |= DPLLB_MODE_LVDS;
7538 else
7539 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007540
Daniel Vetteref1b4602013-06-01 17:17:04 +02007541 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007542 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007543 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007544 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007545
7546 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007547 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007548
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007549 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007550 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007551
7552 /* compute bitmask from p1 value */
7553 if (IS_PINEVIEW(dev))
7554 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7555 else {
7556 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7557 if (IS_G4X(dev) && reduced_clock)
7558 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7559 }
7560 switch (clock->p2) {
7561 case 5:
7562 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7563 break;
7564 case 7:
7565 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7566 break;
7567 case 10:
7568 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7569 break;
7570 case 14:
7571 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7572 break;
7573 }
7574 if (INTEL_INFO(dev)->gen >= 4)
7575 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7576
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007577 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007578 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007579 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007580 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7581 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7582 else
7583 dpll |= PLL_REF_INPUT_DREFCLK;
7584
7585 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007586 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007587
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007588 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007589 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007590 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007591 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007592 }
7593}
7594
Daniel Vetter251ac862015-06-18 10:30:24 +02007595static void i8xx_compute_dpll(struct intel_crtc *crtc,
7596 struct intel_crtc_state *crtc_state,
7597 intel_clock_t *reduced_clock,
7598 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007599{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007600 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007601 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007602 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007603 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007604
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007605 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307606
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007607 dpll = DPLL_VGA_MODE_DIS;
7608
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007609 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007610 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7611 } else {
7612 if (clock->p1 == 2)
7613 dpll |= PLL_P1_DIVIDE_BY_TWO;
7614 else
7615 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7616 if (clock->p2 == 4)
7617 dpll |= PLL_P2_DIVIDE_BY_4;
7618 }
7619
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007620 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007621 dpll |= DPLL_DVO_2X_MODE;
7622
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007623 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007624 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7625 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7626 else
7627 dpll |= PLL_REF_INPUT_DREFCLK;
7628
7629 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007630 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007631}
7632
Daniel Vetter8a654f32013-06-01 17:16:22 +02007633static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007634{
7635 struct drm_device *dev = intel_crtc->base.dev;
7636 struct drm_i915_private *dev_priv = dev->dev_private;
7637 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007638 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007639 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007640 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007641 uint32_t crtc_vtotal, crtc_vblank_end;
7642 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007643
7644 /* We need to be careful not to changed the adjusted mode, for otherwise
7645 * the hw state checker will get angry at the mismatch. */
7646 crtc_vtotal = adjusted_mode->crtc_vtotal;
7647 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007648
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007649 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007650 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007651 crtc_vtotal -= 1;
7652 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007653
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007654 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007655 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7656 else
7657 vsyncshift = adjusted_mode->crtc_hsync_start -
7658 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007659 if (vsyncshift < 0)
7660 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007661 }
7662
7663 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007664 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007665
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007666 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007667 (adjusted_mode->crtc_hdisplay - 1) |
7668 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007669 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007670 (adjusted_mode->crtc_hblank_start - 1) |
7671 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007672 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007673 (adjusted_mode->crtc_hsync_start - 1) |
7674 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7675
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007676 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007677 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007678 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007679 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007680 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007681 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007682 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007683 (adjusted_mode->crtc_vsync_start - 1) |
7684 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7685
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007686 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7687 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7688 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7689 * bits. */
7690 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7691 (pipe == PIPE_B || pipe == PIPE_C))
7692 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7693
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007694 /* pipesrc controls the size that is scaled from, which should
7695 * always be the user's requested size.
7696 */
7697 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007698 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7699 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007700}
7701
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007702static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007703 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007704{
7705 struct drm_device *dev = crtc->base.dev;
7706 struct drm_i915_private *dev_priv = dev->dev_private;
7707 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7708 uint32_t tmp;
7709
7710 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007711 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7712 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007713 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007714 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7715 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007716 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007717 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7718 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007719
7720 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007721 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7722 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007723 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007724 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7725 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007726 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007727 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7728 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007729
7730 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007731 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7732 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7733 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007734 }
7735
7736 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007737 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7738 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7739
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007740 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7741 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007742}
7743
Daniel Vetterf6a83282014-02-11 15:28:57 -08007744void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007745 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007746{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007747 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7748 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7749 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7750 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007751
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007752 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7753 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7754 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7755 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007756
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007757 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007758
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007759 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7760 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007761}
7762
Daniel Vetter84b046f2013-02-19 18:48:54 +01007763static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7764{
7765 struct drm_device *dev = intel_crtc->base.dev;
7766 struct drm_i915_private *dev_priv = dev->dev_private;
7767 uint32_t pipeconf;
7768
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007769 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007770
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007771 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7772 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7773 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007774
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007775 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007776 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007777
Daniel Vetterff9ce462013-04-24 14:57:17 +02007778 /* only g4x and later have fancy bpc/dither controls */
7779 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007780 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007781 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007782 pipeconf |= PIPECONF_DITHER_EN |
7783 PIPECONF_DITHER_TYPE_SP;
7784
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007785 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007786 case 18:
7787 pipeconf |= PIPECONF_6BPC;
7788 break;
7789 case 24:
7790 pipeconf |= PIPECONF_8BPC;
7791 break;
7792 case 30:
7793 pipeconf |= PIPECONF_10BPC;
7794 break;
7795 default:
7796 /* Case prevented by intel_choose_pipe_bpp_dither. */
7797 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007798 }
7799 }
7800
7801 if (HAS_PIPE_CXSR(dev)) {
7802 if (intel_crtc->lowfreq_avail) {
7803 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7804 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7805 } else {
7806 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007807 }
7808 }
7809
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007810 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007811 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007812 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007813 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7814 else
7815 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7816 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007817 pipeconf |= PIPECONF_PROGRESSIVE;
7818
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007819 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007820 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007821
Daniel Vetter84b046f2013-02-19 18:48:54 +01007822 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7823 POSTING_READ(PIPECONF(intel_crtc->pipe));
7824}
7825
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007826static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7827 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007828{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007829 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007830 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007831 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007832 intel_clock_t clock;
7833 bool ok;
7834 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007835 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007836 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007837 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007838 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007839 struct drm_connector_state *connector_state;
7840 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007841
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007842 memset(&crtc_state->dpll_hw_state, 0,
7843 sizeof(crtc_state->dpll_hw_state));
7844
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007845 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007846 if (connector_state->crtc != &crtc->base)
7847 continue;
7848
7849 encoder = to_intel_encoder(connector_state->best_encoder);
7850
Chris Wilson5eddb702010-09-11 13:48:45 +01007851 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007852 case INTEL_OUTPUT_DSI:
7853 is_dsi = true;
7854 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007855 default:
7856 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007857 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007858
Eric Anholtc751ce42010-03-25 11:48:48 -07007859 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007860 }
7861
Jani Nikulaf2335332013-09-13 11:03:09 +03007862 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007863 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007864
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007865 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007866 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007867
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007868 /*
7869 * Returns a set of divisors for the desired target clock with
7870 * the given refclk, or FALSE. The returned values represent
7871 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7872 * 2) / p1 / p2.
7873 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007874 limit = intel_limit(crtc_state, refclk);
7875 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007876 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007877 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007878 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007879 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7880 return -EINVAL;
7881 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007882
Jani Nikulaf2335332013-09-13 11:03:09 +03007883 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007884 crtc_state->dpll.n = clock.n;
7885 crtc_state->dpll.m1 = clock.m1;
7886 crtc_state->dpll.m2 = clock.m2;
7887 crtc_state->dpll.p1 = clock.p1;
7888 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007889 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007890
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007891 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007892 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007893 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007894 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007895 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007896 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007897 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007898 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007899 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007900 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007901 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007902
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007903 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007904}
7905
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007906static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007907 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007908{
7909 struct drm_device *dev = crtc->base.dev;
7910 struct drm_i915_private *dev_priv = dev->dev_private;
7911 uint32_t tmp;
7912
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007913 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7914 return;
7915
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007916 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007917 if (!(tmp & PFIT_ENABLE))
7918 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007919
Daniel Vetter06922822013-07-11 13:35:40 +02007920 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007921 if (INTEL_INFO(dev)->gen < 4) {
7922 if (crtc->pipe != PIPE_B)
7923 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007924 } else {
7925 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7926 return;
7927 }
7928
Daniel Vetter06922822013-07-11 13:35:40 +02007929 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007930 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7931 if (INTEL_INFO(dev)->gen < 5)
7932 pipe_config->gmch_pfit.lvds_border_bits =
7933 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7934}
7935
Jesse Barnesacbec812013-09-20 11:29:32 -07007936static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007937 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007938{
7939 struct drm_device *dev = crtc->base.dev;
7940 struct drm_i915_private *dev_priv = dev->dev_private;
7941 int pipe = pipe_config->cpu_transcoder;
7942 intel_clock_t clock;
7943 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007944 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007945
Shobhit Kumarf573de52014-07-30 20:32:37 +05307946 /* In case of MIPI DPLL will not even be used */
7947 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7948 return;
7949
Ville Syrjäläa5805162015-05-26 20:42:30 +03007950 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007951 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007952 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007953
7954 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7955 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7956 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7957 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7958 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7959
Imre Deakdccbea32015-06-22 23:35:51 +03007960 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007961}
7962
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007963static void
7964i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7965 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007966{
7967 struct drm_device *dev = crtc->base.dev;
7968 struct drm_i915_private *dev_priv = dev->dev_private;
7969 u32 val, base, offset;
7970 int pipe = crtc->pipe, plane = crtc->plane;
7971 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007972 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007973 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007974 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007975
Damien Lespiau42a7b082015-02-05 19:35:13 +00007976 val = I915_READ(DSPCNTR(plane));
7977 if (!(val & DISPLAY_PLANE_ENABLE))
7978 return;
7979
Damien Lespiaud9806c92015-01-21 14:07:19 +00007980 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007981 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007982 DRM_DEBUG_KMS("failed to alloc fb\n");
7983 return;
7984 }
7985
Damien Lespiau1b842c82015-01-21 13:50:54 +00007986 fb = &intel_fb->base;
7987
Daniel Vetter18c52472015-02-10 17:16:09 +00007988 if (INTEL_INFO(dev)->gen >= 4) {
7989 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007990 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007991 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7992 }
7993 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007994
7995 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007996 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007997 fb->pixel_format = fourcc;
7998 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007999
8000 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008001 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008002 offset = I915_READ(DSPTILEOFF(plane));
8003 else
8004 offset = I915_READ(DSPLINOFF(plane));
8005 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8006 } else {
8007 base = I915_READ(DSPADDR(plane));
8008 }
8009 plane_config->base = base;
8010
8011 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008012 fb->width = ((val >> 16) & 0xfff) + 1;
8013 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008014
8015 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008016 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008017
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008018 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008019 fb->pixel_format,
8020 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008021
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008022 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008023
Damien Lespiau2844a922015-01-20 12:51:48 +00008024 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8025 pipe_name(pipe), plane, fb->width, fb->height,
8026 fb->bits_per_pixel, base, fb->pitches[0],
8027 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008028
Damien Lespiau2d140302015-02-05 17:22:18 +00008029 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008030}
8031
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008032static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008033 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008034{
8035 struct drm_device *dev = crtc->base.dev;
8036 struct drm_i915_private *dev_priv = dev->dev_private;
8037 int pipe = pipe_config->cpu_transcoder;
8038 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8039 intel_clock_t clock;
8040 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8041 int refclk = 100000;
8042
Ville Syrjäläa5805162015-05-26 20:42:30 +03008043 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008044 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8045 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8046 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8047 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008048 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008049
8050 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8051 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8052 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8053 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8054 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8055
Imre Deakdccbea32015-06-22 23:35:51 +03008056 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008057}
8058
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008059static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008060 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008061{
8062 struct drm_device *dev = crtc->base.dev;
8063 struct drm_i915_private *dev_priv = dev->dev_private;
8064 uint32_t tmp;
8065
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008066 if (!intel_display_power_is_enabled(dev_priv,
8067 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008068 return false;
8069
Daniel Vettere143a212013-07-04 12:01:15 +02008070 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008071 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008072
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008073 tmp = I915_READ(PIPECONF(crtc->pipe));
8074 if (!(tmp & PIPECONF_ENABLE))
8075 return false;
8076
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008077 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8078 switch (tmp & PIPECONF_BPC_MASK) {
8079 case PIPECONF_6BPC:
8080 pipe_config->pipe_bpp = 18;
8081 break;
8082 case PIPECONF_8BPC:
8083 pipe_config->pipe_bpp = 24;
8084 break;
8085 case PIPECONF_10BPC:
8086 pipe_config->pipe_bpp = 30;
8087 break;
8088 default:
8089 break;
8090 }
8091 }
8092
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008093 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8094 pipe_config->limited_color_range = true;
8095
Ville Syrjälä282740f2013-09-04 18:30:03 +03008096 if (INTEL_INFO(dev)->gen < 4)
8097 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8098
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008099 intel_get_pipe_timings(crtc, pipe_config);
8100
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008101 i9xx_get_pfit_config(crtc, pipe_config);
8102
Daniel Vetter6c49f242013-06-06 12:45:25 +02008103 if (INTEL_INFO(dev)->gen >= 4) {
8104 tmp = I915_READ(DPLL_MD(crtc->pipe));
8105 pipe_config->pixel_multiplier =
8106 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8107 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008108 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008109 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8110 tmp = I915_READ(DPLL(crtc->pipe));
8111 pipe_config->pixel_multiplier =
8112 ((tmp & SDVO_MULTIPLIER_MASK)
8113 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8114 } else {
8115 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8116 * port and will be fixed up in the encoder->get_config
8117 * function. */
8118 pipe_config->pixel_multiplier = 1;
8119 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008120 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8121 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008122 /*
8123 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8124 * on 830. Filter it out here so that we don't
8125 * report errors due to that.
8126 */
8127 if (IS_I830(dev))
8128 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8129
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008130 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8131 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008132 } else {
8133 /* Mask out read-only status bits. */
8134 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8135 DPLL_PORTC_READY_MASK |
8136 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008137 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008138
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008139 if (IS_CHERRYVIEW(dev))
8140 chv_crtc_clock_get(crtc, pipe_config);
8141 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008142 vlv_crtc_clock_get(crtc, pipe_config);
8143 else
8144 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008145
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008146 return true;
8147}
8148
Paulo Zanonidde86e22012-12-01 12:04:25 -02008149static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008150{
8151 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008152 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008153 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008154 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008155 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008156 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008157 bool has_ck505 = false;
8158 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008159
8160 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008161 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008162 switch (encoder->type) {
8163 case INTEL_OUTPUT_LVDS:
8164 has_panel = true;
8165 has_lvds = true;
8166 break;
8167 case INTEL_OUTPUT_EDP:
8168 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008169 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008170 has_cpu_edp = true;
8171 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008172 default:
8173 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008174 }
8175 }
8176
Keith Packard99eb6a02011-09-26 14:29:12 -07008177 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008178 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008179 can_ssc = has_ck505;
8180 } else {
8181 has_ck505 = false;
8182 can_ssc = true;
8183 }
8184
Imre Deak2de69052013-05-08 13:14:04 +03008185 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8186 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008187
8188 /* Ironlake: try to setup display ref clock before DPLL
8189 * enabling. This is only under driver's control after
8190 * PCH B stepping, previous chipset stepping should be
8191 * ignoring this setting.
8192 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008193 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008194
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008195 /* As we must carefully and slowly disable/enable each source in turn,
8196 * compute the final state we want first and check if we need to
8197 * make any changes at all.
8198 */
8199 final = val;
8200 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008201 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008202 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008203 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008204 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8205
8206 final &= ~DREF_SSC_SOURCE_MASK;
8207 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8208 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008209
Keith Packard199e5d72011-09-22 12:01:57 -07008210 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008211 final |= DREF_SSC_SOURCE_ENABLE;
8212
8213 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8214 final |= DREF_SSC1_ENABLE;
8215
8216 if (has_cpu_edp) {
8217 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8218 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8219 else
8220 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8221 } else
8222 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8223 } else {
8224 final |= DREF_SSC_SOURCE_DISABLE;
8225 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8226 }
8227
8228 if (final == val)
8229 return;
8230
8231 /* Always enable nonspread source */
8232 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8233
8234 if (has_ck505)
8235 val |= DREF_NONSPREAD_CK505_ENABLE;
8236 else
8237 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8238
8239 if (has_panel) {
8240 val &= ~DREF_SSC_SOURCE_MASK;
8241 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008242
Keith Packard199e5d72011-09-22 12:01:57 -07008243 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008244 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008245 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008246 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008247 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008248 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008249
8250 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008251 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008252 POSTING_READ(PCH_DREF_CONTROL);
8253 udelay(200);
8254
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008255 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008256
8257 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008258 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008259 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008260 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008261 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008262 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008263 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008264 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008265 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008266
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008267 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008268 POSTING_READ(PCH_DREF_CONTROL);
8269 udelay(200);
8270 } else {
8271 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8272
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008273 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008274
8275 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008276 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008277
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008278 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008279 POSTING_READ(PCH_DREF_CONTROL);
8280 udelay(200);
8281
8282 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008283 val &= ~DREF_SSC_SOURCE_MASK;
8284 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008285
8286 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008287 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008288
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008289 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008290 POSTING_READ(PCH_DREF_CONTROL);
8291 udelay(200);
8292 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008293
8294 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008295}
8296
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008297static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008298{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008299 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008300
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008301 tmp = I915_READ(SOUTH_CHICKEN2);
8302 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8303 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008304
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008305 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8306 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8307 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008308
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008309 tmp = I915_READ(SOUTH_CHICKEN2);
8310 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8311 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008312
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008313 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8314 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8315 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008316}
8317
8318/* WaMPhyProgramming:hsw */
8319static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8320{
8321 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008322
8323 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8324 tmp &= ~(0xFF << 24);
8325 tmp |= (0x12 << 24);
8326 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8327
Paulo Zanonidde86e22012-12-01 12:04:25 -02008328 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8329 tmp |= (1 << 11);
8330 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8331
8332 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8333 tmp |= (1 << 11);
8334 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8335
Paulo Zanonidde86e22012-12-01 12:04:25 -02008336 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8337 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8338 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8339
8340 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8341 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8342 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8343
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008344 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8345 tmp &= ~(7 << 13);
8346 tmp |= (5 << 13);
8347 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008348
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008349 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8350 tmp &= ~(7 << 13);
8351 tmp |= (5 << 13);
8352 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008353
8354 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8355 tmp &= ~0xFF;
8356 tmp |= 0x1C;
8357 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8358
8359 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8360 tmp &= ~0xFF;
8361 tmp |= 0x1C;
8362 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8363
8364 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8365 tmp &= ~(0xFF << 16);
8366 tmp |= (0x1C << 16);
8367 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8368
8369 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8370 tmp &= ~(0xFF << 16);
8371 tmp |= (0x1C << 16);
8372 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8373
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008374 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8375 tmp |= (1 << 27);
8376 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008377
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008378 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8379 tmp |= (1 << 27);
8380 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008381
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008382 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8383 tmp &= ~(0xF << 28);
8384 tmp |= (4 << 28);
8385 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008386
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008387 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8388 tmp &= ~(0xF << 28);
8389 tmp |= (4 << 28);
8390 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008391}
8392
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008393/* Implements 3 different sequences from BSpec chapter "Display iCLK
8394 * Programming" based on the parameters passed:
8395 * - Sequence to enable CLKOUT_DP
8396 * - Sequence to enable CLKOUT_DP without spread
8397 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8398 */
8399static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8400 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008401{
8402 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008403 uint32_t reg, tmp;
8404
8405 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8406 with_spread = true;
8407 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8408 with_fdi, "LP PCH doesn't have FDI\n"))
8409 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008410
Ville Syrjäläa5805162015-05-26 20:42:30 +03008411 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008412
8413 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8414 tmp &= ~SBI_SSCCTL_DISABLE;
8415 tmp |= SBI_SSCCTL_PATHALT;
8416 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8417
8418 udelay(24);
8419
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008420 if (with_spread) {
8421 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8422 tmp &= ~SBI_SSCCTL_PATHALT;
8423 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008424
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008425 if (with_fdi) {
8426 lpt_reset_fdi_mphy(dev_priv);
8427 lpt_program_fdi_mphy(dev_priv);
8428 }
8429 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008430
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008431 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8432 SBI_GEN0 : SBI_DBUFF0;
8433 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8434 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8435 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008436
Ville Syrjäläa5805162015-05-26 20:42:30 +03008437 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008438}
8439
Paulo Zanoni47701c32013-07-23 11:19:25 -03008440/* Sequence to disable CLKOUT_DP */
8441static void lpt_disable_clkout_dp(struct drm_device *dev)
8442{
8443 struct drm_i915_private *dev_priv = dev->dev_private;
8444 uint32_t reg, tmp;
8445
Ville Syrjäläa5805162015-05-26 20:42:30 +03008446 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008447
8448 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8449 SBI_GEN0 : SBI_DBUFF0;
8450 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8451 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8452 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8453
8454 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8455 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8456 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8457 tmp |= SBI_SSCCTL_PATHALT;
8458 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8459 udelay(32);
8460 }
8461 tmp |= SBI_SSCCTL_DISABLE;
8462 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8463 }
8464
Ville Syrjäläa5805162015-05-26 20:42:30 +03008465 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008466}
8467
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008468static void lpt_init_pch_refclk(struct drm_device *dev)
8469{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008470 struct intel_encoder *encoder;
8471 bool has_vga = false;
8472
Damien Lespiaub2784e12014-08-05 11:29:37 +01008473 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008474 switch (encoder->type) {
8475 case INTEL_OUTPUT_ANALOG:
8476 has_vga = true;
8477 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008478 default:
8479 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008480 }
8481 }
8482
Paulo Zanoni47701c32013-07-23 11:19:25 -03008483 if (has_vga)
8484 lpt_enable_clkout_dp(dev, true, true);
8485 else
8486 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008487}
8488
Paulo Zanonidde86e22012-12-01 12:04:25 -02008489/*
8490 * Initialize reference clocks when the driver loads
8491 */
8492void intel_init_pch_refclk(struct drm_device *dev)
8493{
8494 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8495 ironlake_init_pch_refclk(dev);
8496 else if (HAS_PCH_LPT(dev))
8497 lpt_init_pch_refclk(dev);
8498}
8499
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008500static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008501{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008502 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008503 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008504 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008505 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008506 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008507 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008508 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008509 bool is_lvds = false;
8510
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008511 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008512 if (connector_state->crtc != crtc_state->base.crtc)
8513 continue;
8514
8515 encoder = to_intel_encoder(connector_state->best_encoder);
8516
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008517 switch (encoder->type) {
8518 case INTEL_OUTPUT_LVDS:
8519 is_lvds = true;
8520 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008521 default:
8522 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008523 }
8524 num_connectors++;
8525 }
8526
8527 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008528 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008529 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008530 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008531 }
8532
8533 return 120000;
8534}
8535
Daniel Vetter6ff93602013-04-19 11:24:36 +02008536static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008537{
8538 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8539 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8540 int pipe = intel_crtc->pipe;
8541 uint32_t val;
8542
Daniel Vetter78114072013-06-13 00:54:57 +02008543 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008544
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008545 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008546 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008547 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008548 break;
8549 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008550 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008551 break;
8552 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008553 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008554 break;
8555 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008556 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008557 break;
8558 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008559 /* Case prevented by intel_choose_pipe_bpp_dither. */
8560 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008561 }
8562
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008563 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008564 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8565
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008566 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008567 val |= PIPECONF_INTERLACED_ILK;
8568 else
8569 val |= PIPECONF_PROGRESSIVE;
8570
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008571 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008572 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008573
Paulo Zanonic8203562012-09-12 10:06:29 -03008574 I915_WRITE(PIPECONF(pipe), val);
8575 POSTING_READ(PIPECONF(pipe));
8576}
8577
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008578/*
8579 * Set up the pipe CSC unit.
8580 *
8581 * Currently only full range RGB to limited range RGB conversion
8582 * is supported, but eventually this should handle various
8583 * RGB<->YCbCr scenarios as well.
8584 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008585static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008586{
8587 struct drm_device *dev = crtc->dev;
8588 struct drm_i915_private *dev_priv = dev->dev_private;
8589 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8590 int pipe = intel_crtc->pipe;
8591 uint16_t coeff = 0x7800; /* 1.0 */
8592
8593 /*
8594 * TODO: Check what kind of values actually come out of the pipe
8595 * with these coeff/postoff values and adjust to get the best
8596 * accuracy. Perhaps we even need to take the bpc value into
8597 * consideration.
8598 */
8599
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008600 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008601 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8602
8603 /*
8604 * GY/GU and RY/RU should be the other way around according
8605 * to BSpec, but reality doesn't agree. Just set them up in
8606 * a way that results in the correct picture.
8607 */
8608 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8609 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8610
8611 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8612 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8613
8614 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8615 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8616
8617 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8618 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8619 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8620
8621 if (INTEL_INFO(dev)->gen > 6) {
8622 uint16_t postoff = 0;
8623
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008624 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008625 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008626
8627 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8628 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8629 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8630
8631 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8632 } else {
8633 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8634
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008635 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008636 mode |= CSC_BLACK_SCREEN_OFFSET;
8637
8638 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8639 }
8640}
8641
Daniel Vetter6ff93602013-04-19 11:24:36 +02008642static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008643{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008644 struct drm_device *dev = crtc->dev;
8645 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008646 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008647 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008648 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008649 uint32_t val;
8650
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008651 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008652
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008653 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008654 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8655
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008656 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008657 val |= PIPECONF_INTERLACED_ILK;
8658 else
8659 val |= PIPECONF_PROGRESSIVE;
8660
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008661 I915_WRITE(PIPECONF(cpu_transcoder), val);
8662 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008663
8664 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8665 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008666
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308667 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008668 val = 0;
8669
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008670 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008671 case 18:
8672 val |= PIPEMISC_DITHER_6_BPC;
8673 break;
8674 case 24:
8675 val |= PIPEMISC_DITHER_8_BPC;
8676 break;
8677 case 30:
8678 val |= PIPEMISC_DITHER_10_BPC;
8679 break;
8680 case 36:
8681 val |= PIPEMISC_DITHER_12_BPC;
8682 break;
8683 default:
8684 /* Case prevented by pipe_config_set_bpp. */
8685 BUG();
8686 }
8687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008688 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008689 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8690
8691 I915_WRITE(PIPEMISC(pipe), val);
8692 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008693}
8694
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008695static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008696 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008697 intel_clock_t *clock,
8698 bool *has_reduced_clock,
8699 intel_clock_t *reduced_clock)
8700{
8701 struct drm_device *dev = crtc->dev;
8702 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008703 int refclk;
8704 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008705 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008706
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008707 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008708
8709 /*
8710 * Returns a set of divisors for the desired target clock with the given
8711 * refclk, or FALSE. The returned values represent the clock equation:
8712 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8713 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008714 limit = intel_limit(crtc_state, refclk);
8715 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008716 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008717 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008718 if (!ret)
8719 return false;
8720
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008721 return true;
8722}
8723
Paulo Zanonid4b19312012-11-29 11:29:32 -02008724int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8725{
8726 /*
8727 * Account for spread spectrum to avoid
8728 * oversubscribing the link. Max center spread
8729 * is 2.5%; use 5% for safety's sake.
8730 */
8731 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008732 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008733}
8734
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008735static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008736{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008737 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008738}
8739
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008740static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008741 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008742 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008743 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008744{
8745 struct drm_crtc *crtc = &intel_crtc->base;
8746 struct drm_device *dev = crtc->dev;
8747 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008748 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008749 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008750 struct drm_connector_state *connector_state;
8751 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008752 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008753 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008754 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008755
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008756 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008757 if (connector_state->crtc != crtc_state->base.crtc)
8758 continue;
8759
8760 encoder = to_intel_encoder(connector_state->best_encoder);
8761
8762 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008763 case INTEL_OUTPUT_LVDS:
8764 is_lvds = true;
8765 break;
8766 case INTEL_OUTPUT_SDVO:
8767 case INTEL_OUTPUT_HDMI:
8768 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008769 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008770 default:
8771 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008772 }
8773
8774 num_connectors++;
8775 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008776
Chris Wilsonc1858122010-12-03 21:35:48 +00008777 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008778 factor = 21;
8779 if (is_lvds) {
8780 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008781 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008782 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008783 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008784 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008785 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008786
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008787 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008788 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008789
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008790 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8791 *fp2 |= FP_CB_TUNE;
8792
Chris Wilson5eddb702010-09-11 13:48:45 +01008793 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008794
Eric Anholta07d6782011-03-30 13:01:08 -07008795 if (is_lvds)
8796 dpll |= DPLLB_MODE_LVDS;
8797 else
8798 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008799
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008800 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008801 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008802
8803 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008804 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008805 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008806 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008807
Eric Anholta07d6782011-03-30 13:01:08 -07008808 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008809 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008810 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008811 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008812
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008813 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008814 case 5:
8815 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8816 break;
8817 case 7:
8818 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8819 break;
8820 case 10:
8821 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8822 break;
8823 case 14:
8824 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8825 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008826 }
8827
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008828 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008829 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008830 else
8831 dpll |= PLL_REF_INPUT_DREFCLK;
8832
Daniel Vetter959e16d2013-06-05 13:34:21 +02008833 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008834}
8835
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008836static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8837 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008838{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008839 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008840 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008841 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008842 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008843 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008844 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008845
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008846 memset(&crtc_state->dpll_hw_state, 0,
8847 sizeof(crtc_state->dpll_hw_state));
8848
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008849 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008850
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008851 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8852 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8853
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008854 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008855 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008856 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008857 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8858 return -EINVAL;
8859 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008860 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008861 if (!crtc_state->clock_set) {
8862 crtc_state->dpll.n = clock.n;
8863 crtc_state->dpll.m1 = clock.m1;
8864 crtc_state->dpll.m2 = clock.m2;
8865 crtc_state->dpll.p1 = clock.p1;
8866 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008867 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008868
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008869 /* 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 +02008870 if (crtc_state->has_pch_encoder) {
8871 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008872 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008873 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008874
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008875 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008876 &fp, &reduced_clock,
8877 has_reduced_clock ? &fp2 : NULL);
8878
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008879 crtc_state->dpll_hw_state.dpll = dpll;
8880 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008881 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008882 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008883 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008884 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008885
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008886 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008887 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008888 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008889 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008890 return -EINVAL;
8891 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008892 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008893
Rodrigo Viviab585de2015-03-24 12:40:09 -07008894 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008895 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008896 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008897 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008898
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008899 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008900}
8901
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008902static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8903 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008904{
8905 struct drm_device *dev = crtc->base.dev;
8906 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008907 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008908
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008909 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8910 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8911 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8912 & ~TU_SIZE_MASK;
8913 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8914 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8915 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8916}
8917
8918static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8919 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008920 struct intel_link_m_n *m_n,
8921 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008922{
8923 struct drm_device *dev = crtc->base.dev;
8924 struct drm_i915_private *dev_priv = dev->dev_private;
8925 enum pipe pipe = crtc->pipe;
8926
8927 if (INTEL_INFO(dev)->gen >= 5) {
8928 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8929 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8930 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8931 & ~TU_SIZE_MASK;
8932 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8933 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8934 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008935 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8936 * gen < 8) and if DRRS is supported (to make sure the
8937 * registers are not unnecessarily read).
8938 */
8939 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008940 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008941 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8942 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8943 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8944 & ~TU_SIZE_MASK;
8945 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8946 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8947 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8948 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008949 } else {
8950 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8951 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8952 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8953 & ~TU_SIZE_MASK;
8954 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8955 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8956 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8957 }
8958}
8959
8960void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008961 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008962{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008963 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008964 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8965 else
8966 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008967 &pipe_config->dp_m_n,
8968 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008969}
8970
Daniel Vetter72419202013-04-04 13:28:53 +02008971static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008972 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008973{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008974 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008975 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008976}
8977
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008978static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008979 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008980{
8981 struct drm_device *dev = crtc->base.dev;
8982 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008983 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8984 uint32_t ps_ctrl = 0;
8985 int id = -1;
8986 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008987
Chandra Kondurua1b22782015-04-07 15:28:45 -07008988 /* find scaler attached to this pipe */
8989 for (i = 0; i < crtc->num_scalers; i++) {
8990 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8991 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8992 id = i;
8993 pipe_config->pch_pfit.enabled = true;
8994 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8995 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8996 break;
8997 }
8998 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008999
Chandra Kondurua1b22782015-04-07 15:28:45 -07009000 scaler_state->scaler_id = id;
9001 if (id >= 0) {
9002 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9003 } else {
9004 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009005 }
9006}
9007
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009008static void
9009skylake_get_initial_plane_config(struct intel_crtc *crtc,
9010 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009011{
9012 struct drm_device *dev = crtc->base.dev;
9013 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009014 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009015 int pipe = crtc->pipe;
9016 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009017 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009018 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009019 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009020
Damien Lespiaud9806c92015-01-21 14:07:19 +00009021 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009022 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009023 DRM_DEBUG_KMS("failed to alloc fb\n");
9024 return;
9025 }
9026
Damien Lespiau1b842c82015-01-21 13:50:54 +00009027 fb = &intel_fb->base;
9028
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009029 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009030 if (!(val & PLANE_CTL_ENABLE))
9031 goto error;
9032
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009033 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9034 fourcc = skl_format_to_fourcc(pixel_format,
9035 val & PLANE_CTL_ORDER_RGBX,
9036 val & PLANE_CTL_ALPHA_MASK);
9037 fb->pixel_format = fourcc;
9038 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9039
Damien Lespiau40f46282015-02-27 11:15:21 +00009040 tiling = val & PLANE_CTL_TILED_MASK;
9041 switch (tiling) {
9042 case PLANE_CTL_TILED_LINEAR:
9043 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9044 break;
9045 case PLANE_CTL_TILED_X:
9046 plane_config->tiling = I915_TILING_X;
9047 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9048 break;
9049 case PLANE_CTL_TILED_Y:
9050 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9051 break;
9052 case PLANE_CTL_TILED_YF:
9053 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9054 break;
9055 default:
9056 MISSING_CASE(tiling);
9057 goto error;
9058 }
9059
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009060 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9061 plane_config->base = base;
9062
9063 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9064
9065 val = I915_READ(PLANE_SIZE(pipe, 0));
9066 fb->height = ((val >> 16) & 0xfff) + 1;
9067 fb->width = ((val >> 0) & 0x1fff) + 1;
9068
9069 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009070 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9071 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009072 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9073
9074 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009075 fb->pixel_format,
9076 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009077
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009078 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009079
9080 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9081 pipe_name(pipe), fb->width, fb->height,
9082 fb->bits_per_pixel, base, fb->pitches[0],
9083 plane_config->size);
9084
Damien Lespiau2d140302015-02-05 17:22:18 +00009085 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009086 return;
9087
9088error:
9089 kfree(fb);
9090}
9091
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009092static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009093 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009094{
9095 struct drm_device *dev = crtc->base.dev;
9096 struct drm_i915_private *dev_priv = dev->dev_private;
9097 uint32_t tmp;
9098
9099 tmp = I915_READ(PF_CTL(crtc->pipe));
9100
9101 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009102 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009103 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9104 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009105
9106 /* We currently do not free assignements of panel fitters on
9107 * ivb/hsw (since we don't use the higher upscaling modes which
9108 * differentiates them) so just WARN about this case for now. */
9109 if (IS_GEN7(dev)) {
9110 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9111 PF_PIPE_SEL_IVB(crtc->pipe));
9112 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009113 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009114}
9115
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009116static void
9117ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9118 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009119{
9120 struct drm_device *dev = crtc->base.dev;
9121 struct drm_i915_private *dev_priv = dev->dev_private;
9122 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009123 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009124 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009125 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009126 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009127 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009128
Damien Lespiau42a7b082015-02-05 19:35:13 +00009129 val = I915_READ(DSPCNTR(pipe));
9130 if (!(val & DISPLAY_PLANE_ENABLE))
9131 return;
9132
Damien Lespiaud9806c92015-01-21 14:07:19 +00009133 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009134 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009135 DRM_DEBUG_KMS("failed to alloc fb\n");
9136 return;
9137 }
9138
Damien Lespiau1b842c82015-01-21 13:50:54 +00009139 fb = &intel_fb->base;
9140
Daniel Vetter18c52472015-02-10 17:16:09 +00009141 if (INTEL_INFO(dev)->gen >= 4) {
9142 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009143 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009144 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9145 }
9146 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009147
9148 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009149 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009150 fb->pixel_format = fourcc;
9151 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009152
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009153 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009154 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009155 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009156 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009157 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009158 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009159 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009160 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009161 }
9162 plane_config->base = base;
9163
9164 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009165 fb->width = ((val >> 16) & 0xfff) + 1;
9166 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009167
9168 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009169 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009170
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009171 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009172 fb->pixel_format,
9173 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009174
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009175 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009176
Damien Lespiau2844a922015-01-20 12:51:48 +00009177 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9178 pipe_name(pipe), fb->width, fb->height,
9179 fb->bits_per_pixel, base, fb->pitches[0],
9180 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009181
Damien Lespiau2d140302015-02-05 17:22:18 +00009182 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009183}
9184
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009185static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009186 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009187{
9188 struct drm_device *dev = crtc->base.dev;
9189 struct drm_i915_private *dev_priv = dev->dev_private;
9190 uint32_t tmp;
9191
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009192 if (!intel_display_power_is_enabled(dev_priv,
9193 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009194 return false;
9195
Daniel Vettere143a212013-07-04 12:01:15 +02009196 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009197 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009198
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009199 tmp = I915_READ(PIPECONF(crtc->pipe));
9200 if (!(tmp & PIPECONF_ENABLE))
9201 return false;
9202
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009203 switch (tmp & PIPECONF_BPC_MASK) {
9204 case PIPECONF_6BPC:
9205 pipe_config->pipe_bpp = 18;
9206 break;
9207 case PIPECONF_8BPC:
9208 pipe_config->pipe_bpp = 24;
9209 break;
9210 case PIPECONF_10BPC:
9211 pipe_config->pipe_bpp = 30;
9212 break;
9213 case PIPECONF_12BPC:
9214 pipe_config->pipe_bpp = 36;
9215 break;
9216 default:
9217 break;
9218 }
9219
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009220 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9221 pipe_config->limited_color_range = true;
9222
Daniel Vetterab9412b2013-05-03 11:49:46 +02009223 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009224 struct intel_shared_dpll *pll;
9225
Daniel Vetter88adfff2013-03-28 10:42:01 +01009226 pipe_config->has_pch_encoder = true;
9227
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009228 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9229 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9230 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009231
9232 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009233
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009234 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009235 pipe_config->shared_dpll =
9236 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009237 } else {
9238 tmp = I915_READ(PCH_DPLL_SEL);
9239 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9240 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9241 else
9242 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9243 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009244
9245 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9246
9247 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9248 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009249
9250 tmp = pipe_config->dpll_hw_state.dpll;
9251 pipe_config->pixel_multiplier =
9252 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9253 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009254
9255 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009256 } else {
9257 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009258 }
9259
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009260 intel_get_pipe_timings(crtc, pipe_config);
9261
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009262 ironlake_get_pfit_config(crtc, pipe_config);
9263
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009264 return true;
9265}
9266
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009267static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9268{
9269 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009270 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009271
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009272 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009273 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009274 pipe_name(crtc->pipe));
9275
Rob Clarke2c719b2014-12-15 13:56:32 -05009276 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9277 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9278 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9279 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9280 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9281 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009282 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009283 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009284 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009285 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009286 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009287 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009288 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009289 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009290 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009291
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009292 /*
9293 * In theory we can still leave IRQs enabled, as long as only the HPD
9294 * interrupts remain enabled. We used to check for that, but since it's
9295 * gen-specific and since we only disable LCPLL after we fully disable
9296 * the interrupts, the check below should be enough.
9297 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009298 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009299}
9300
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009301static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9302{
9303 struct drm_device *dev = dev_priv->dev;
9304
9305 if (IS_HASWELL(dev))
9306 return I915_READ(D_COMP_HSW);
9307 else
9308 return I915_READ(D_COMP_BDW);
9309}
9310
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009311static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9312{
9313 struct drm_device *dev = dev_priv->dev;
9314
9315 if (IS_HASWELL(dev)) {
9316 mutex_lock(&dev_priv->rps.hw_lock);
9317 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9318 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009319 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009320 mutex_unlock(&dev_priv->rps.hw_lock);
9321 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009322 I915_WRITE(D_COMP_BDW, val);
9323 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009324 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009325}
9326
9327/*
9328 * This function implements pieces of two sequences from BSpec:
9329 * - Sequence for display software to disable LCPLL
9330 * - Sequence for display software to allow package C8+
9331 * The steps implemented here are just the steps that actually touch the LCPLL
9332 * register. Callers should take care of disabling all the display engine
9333 * functions, doing the mode unset, fixing interrupts, etc.
9334 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009335static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9336 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009337{
9338 uint32_t val;
9339
9340 assert_can_disable_lcpll(dev_priv);
9341
9342 val = I915_READ(LCPLL_CTL);
9343
9344 if (switch_to_fclk) {
9345 val |= LCPLL_CD_SOURCE_FCLK;
9346 I915_WRITE(LCPLL_CTL, val);
9347
9348 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9349 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9350 DRM_ERROR("Switching to FCLK failed\n");
9351
9352 val = I915_READ(LCPLL_CTL);
9353 }
9354
9355 val |= LCPLL_PLL_DISABLE;
9356 I915_WRITE(LCPLL_CTL, val);
9357 POSTING_READ(LCPLL_CTL);
9358
9359 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9360 DRM_ERROR("LCPLL still locked\n");
9361
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009362 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009363 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009364 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009365 ndelay(100);
9366
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009367 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9368 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009369 DRM_ERROR("D_COMP RCOMP still in progress\n");
9370
9371 if (allow_power_down) {
9372 val = I915_READ(LCPLL_CTL);
9373 val |= LCPLL_POWER_DOWN_ALLOW;
9374 I915_WRITE(LCPLL_CTL, val);
9375 POSTING_READ(LCPLL_CTL);
9376 }
9377}
9378
9379/*
9380 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9381 * source.
9382 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009383static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009384{
9385 uint32_t val;
9386
9387 val = I915_READ(LCPLL_CTL);
9388
9389 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9390 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9391 return;
9392
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009393 /*
9394 * Make sure we're not on PC8 state before disabling PC8, otherwise
9395 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009396 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009397 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009398
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009399 if (val & LCPLL_POWER_DOWN_ALLOW) {
9400 val &= ~LCPLL_POWER_DOWN_ALLOW;
9401 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009402 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009403 }
9404
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009405 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009406 val |= D_COMP_COMP_FORCE;
9407 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009408 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009409
9410 val = I915_READ(LCPLL_CTL);
9411 val &= ~LCPLL_PLL_DISABLE;
9412 I915_WRITE(LCPLL_CTL, val);
9413
9414 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9415 DRM_ERROR("LCPLL not locked yet\n");
9416
9417 if (val & LCPLL_CD_SOURCE_FCLK) {
9418 val = I915_READ(LCPLL_CTL);
9419 val &= ~LCPLL_CD_SOURCE_FCLK;
9420 I915_WRITE(LCPLL_CTL, val);
9421
9422 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9423 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9424 DRM_ERROR("Switching back to LCPLL failed\n");
9425 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009426
Mika Kuoppala59bad942015-01-16 11:34:40 +02009427 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009428 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009429}
9430
Paulo Zanoni765dab672014-03-07 20:08:18 -03009431/*
9432 * Package states C8 and deeper are really deep PC states that can only be
9433 * reached when all the devices on the system allow it, so even if the graphics
9434 * device allows PC8+, it doesn't mean the system will actually get to these
9435 * states. Our driver only allows PC8+ when going into runtime PM.
9436 *
9437 * The requirements for PC8+ are that all the outputs are disabled, the power
9438 * well is disabled and most interrupts are disabled, and these are also
9439 * requirements for runtime PM. When these conditions are met, we manually do
9440 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9441 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9442 * hang the machine.
9443 *
9444 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9445 * the state of some registers, so when we come back from PC8+ we need to
9446 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9447 * need to take care of the registers kept by RC6. Notice that this happens even
9448 * if we don't put the device in PCI D3 state (which is what currently happens
9449 * because of the runtime PM support).
9450 *
9451 * For more, read "Display Sequences for Package C8" on the hardware
9452 * documentation.
9453 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009454void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009455{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009456 struct drm_device *dev = dev_priv->dev;
9457 uint32_t val;
9458
Paulo Zanonic67a4702013-08-19 13:18:09 -03009459 DRM_DEBUG_KMS("Enabling package C8+\n");
9460
Paulo Zanonic67a4702013-08-19 13:18:09 -03009461 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9462 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9463 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9464 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9465 }
9466
9467 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009468 hsw_disable_lcpll(dev_priv, true, true);
9469}
9470
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009471void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009472{
9473 struct drm_device *dev = dev_priv->dev;
9474 uint32_t val;
9475
Paulo Zanonic67a4702013-08-19 13:18:09 -03009476 DRM_DEBUG_KMS("Disabling package C8+\n");
9477
9478 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009479 lpt_init_pch_refclk(dev);
9480
9481 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9482 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9483 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9484 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9485 }
9486
9487 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009488}
9489
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009490static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309491{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009492 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009493 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309494
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009495 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309496}
9497
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009498/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009499static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009500{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009501 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009502 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009503 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009504
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009505 for_each_intel_crtc(state->dev, intel_crtc) {
9506 int pixel_rate;
9507
9508 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9509 if (IS_ERR(crtc_state))
9510 return PTR_ERR(crtc_state);
9511
9512 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009513 continue;
9514
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009515 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009516
9517 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009518 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009519 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9520
9521 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9522 }
9523
9524 return max_pixel_rate;
9525}
9526
9527static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9528{
9529 struct drm_i915_private *dev_priv = dev->dev_private;
9530 uint32_t val, data;
9531 int ret;
9532
9533 if (WARN((I915_READ(LCPLL_CTL) &
9534 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9535 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9536 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9537 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9538 "trying to change cdclk frequency with cdclk not enabled\n"))
9539 return;
9540
9541 mutex_lock(&dev_priv->rps.hw_lock);
9542 ret = sandybridge_pcode_write(dev_priv,
9543 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9544 mutex_unlock(&dev_priv->rps.hw_lock);
9545 if (ret) {
9546 DRM_ERROR("failed to inform pcode about cdclk change\n");
9547 return;
9548 }
9549
9550 val = I915_READ(LCPLL_CTL);
9551 val |= LCPLL_CD_SOURCE_FCLK;
9552 I915_WRITE(LCPLL_CTL, val);
9553
9554 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9555 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9556 DRM_ERROR("Switching to FCLK failed\n");
9557
9558 val = I915_READ(LCPLL_CTL);
9559 val &= ~LCPLL_CLK_FREQ_MASK;
9560
9561 switch (cdclk) {
9562 case 450000:
9563 val |= LCPLL_CLK_FREQ_450;
9564 data = 0;
9565 break;
9566 case 540000:
9567 val |= LCPLL_CLK_FREQ_54O_BDW;
9568 data = 1;
9569 break;
9570 case 337500:
9571 val |= LCPLL_CLK_FREQ_337_5_BDW;
9572 data = 2;
9573 break;
9574 case 675000:
9575 val |= LCPLL_CLK_FREQ_675_BDW;
9576 data = 3;
9577 break;
9578 default:
9579 WARN(1, "invalid cdclk frequency\n");
9580 return;
9581 }
9582
9583 I915_WRITE(LCPLL_CTL, val);
9584
9585 val = I915_READ(LCPLL_CTL);
9586 val &= ~LCPLL_CD_SOURCE_FCLK;
9587 I915_WRITE(LCPLL_CTL, val);
9588
9589 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9590 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9591 DRM_ERROR("Switching back to LCPLL failed\n");
9592
9593 mutex_lock(&dev_priv->rps.hw_lock);
9594 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9595 mutex_unlock(&dev_priv->rps.hw_lock);
9596
9597 intel_update_cdclk(dev);
9598
9599 WARN(cdclk != dev_priv->cdclk_freq,
9600 "cdclk requested %d kHz but got %d kHz\n",
9601 cdclk, dev_priv->cdclk_freq);
9602}
9603
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009604static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009605{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009606 struct drm_i915_private *dev_priv = to_i915(state->dev);
9607 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009608 int cdclk;
9609
9610 /*
9611 * FIXME should also account for plane ratio
9612 * once 64bpp pixel formats are supported.
9613 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009614 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009615 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009616 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009617 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009618 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009619 cdclk = 450000;
9620 else
9621 cdclk = 337500;
9622
9623 /*
9624 * FIXME move the cdclk caclulation to
9625 * compute_config() so we can fail gracegully.
9626 */
9627 if (cdclk > dev_priv->max_cdclk_freq) {
9628 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9629 cdclk, dev_priv->max_cdclk_freq);
9630 cdclk = dev_priv->max_cdclk_freq;
9631 }
9632
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009633 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009634
9635 return 0;
9636}
9637
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009638static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009639{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009640 struct drm_device *dev = old_state->dev;
9641 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009642
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009643 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009644}
9645
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009646static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9647 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009648{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009649 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009650 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009651
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009652 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009653
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009654 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009655}
9656
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309657static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9658 enum port port,
9659 struct intel_crtc_state *pipe_config)
9660{
9661 switch (port) {
9662 case PORT_A:
9663 pipe_config->ddi_pll_sel = SKL_DPLL0;
9664 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9665 break;
9666 case PORT_B:
9667 pipe_config->ddi_pll_sel = SKL_DPLL1;
9668 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9669 break;
9670 case PORT_C:
9671 pipe_config->ddi_pll_sel = SKL_DPLL2;
9672 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9673 break;
9674 default:
9675 DRM_ERROR("Incorrect port type\n");
9676 }
9677}
9678
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009679static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9680 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009681 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009682{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009683 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009684
9685 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9686 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9687
9688 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009689 case SKL_DPLL0:
9690 /*
9691 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9692 * of the shared DPLL framework and thus needs to be read out
9693 * separately
9694 */
9695 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9696 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9697 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009698 case SKL_DPLL1:
9699 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9700 break;
9701 case SKL_DPLL2:
9702 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9703 break;
9704 case SKL_DPLL3:
9705 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9706 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009707 }
9708}
9709
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009710static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9711 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009712 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009713{
9714 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9715
9716 switch (pipe_config->ddi_pll_sel) {
9717 case PORT_CLK_SEL_WRPLL1:
9718 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9719 break;
9720 case PORT_CLK_SEL_WRPLL2:
9721 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9722 break;
9723 }
9724}
9725
Daniel Vetter26804af2014-06-25 22:01:55 +03009726static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009727 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009728{
9729 struct drm_device *dev = crtc->base.dev;
9730 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009731 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009732 enum port port;
9733 uint32_t tmp;
9734
9735 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9736
9737 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9738
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009739 if (IS_SKYLAKE(dev))
9740 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309741 else if (IS_BROXTON(dev))
9742 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009743 else
9744 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009745
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009746 if (pipe_config->shared_dpll >= 0) {
9747 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9748
9749 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9750 &pipe_config->dpll_hw_state));
9751 }
9752
Daniel Vetter26804af2014-06-25 22:01:55 +03009753 /*
9754 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9755 * DDI E. So just check whether this pipe is wired to DDI E and whether
9756 * the PCH transcoder is on.
9757 */
Damien Lespiauca370452013-12-03 13:56:24 +00009758 if (INTEL_INFO(dev)->gen < 9 &&
9759 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009760 pipe_config->has_pch_encoder = true;
9761
9762 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9763 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9764 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9765
9766 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9767 }
9768}
9769
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009770static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009771 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009772{
9773 struct drm_device *dev = crtc->base.dev;
9774 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009775 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009776 uint32_t tmp;
9777
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009778 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009779 POWER_DOMAIN_PIPE(crtc->pipe)))
9780 return false;
9781
Daniel Vettere143a212013-07-04 12:01:15 +02009782 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009783 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9784
Daniel Vettereccb1402013-05-22 00:50:22 +02009785 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9786 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9787 enum pipe trans_edp_pipe;
9788 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9789 default:
9790 WARN(1, "unknown pipe linked to edp transcoder\n");
9791 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9792 case TRANS_DDI_EDP_INPUT_A_ON:
9793 trans_edp_pipe = PIPE_A;
9794 break;
9795 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9796 trans_edp_pipe = PIPE_B;
9797 break;
9798 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9799 trans_edp_pipe = PIPE_C;
9800 break;
9801 }
9802
9803 if (trans_edp_pipe == crtc->pipe)
9804 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9805 }
9806
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009807 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009808 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009809 return false;
9810
Daniel Vettereccb1402013-05-22 00:50:22 +02009811 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009812 if (!(tmp & PIPECONF_ENABLE))
9813 return false;
9814
Daniel Vetter26804af2014-06-25 22:01:55 +03009815 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009816
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009817 intel_get_pipe_timings(crtc, pipe_config);
9818
Chandra Kondurua1b22782015-04-07 15:28:45 -07009819 if (INTEL_INFO(dev)->gen >= 9) {
9820 skl_init_scalers(dev, crtc, pipe_config);
9821 }
9822
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009823 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009824
9825 if (INTEL_INFO(dev)->gen >= 9) {
9826 pipe_config->scaler_state.scaler_id = -1;
9827 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9828 }
9829
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009830 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009831 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009832 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009833 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009834 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009835 else
9836 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009837 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009838
Jesse Barnese59150d2014-01-07 13:30:45 -08009839 if (IS_HASWELL(dev))
9840 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9841 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009842
Clint Taylorebb69c92014-09-30 10:30:22 -07009843 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9844 pipe_config->pixel_multiplier =
9845 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9846 } else {
9847 pipe_config->pixel_multiplier = 1;
9848 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009849
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009850 return true;
9851}
9852
Chris Wilson560b85b2010-08-07 11:01:38 +01009853static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9854{
9855 struct drm_device *dev = crtc->dev;
9856 struct drm_i915_private *dev_priv = dev->dev_private;
9857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009858 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009859
Ville Syrjälädc41c152014-08-13 11:57:05 +03009860 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009861 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9862 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009863 unsigned int stride = roundup_pow_of_two(width) * 4;
9864
9865 switch (stride) {
9866 default:
9867 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9868 width, stride);
9869 stride = 256;
9870 /* fallthrough */
9871 case 256:
9872 case 512:
9873 case 1024:
9874 case 2048:
9875 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009876 }
9877
Ville Syrjälädc41c152014-08-13 11:57:05 +03009878 cntl |= CURSOR_ENABLE |
9879 CURSOR_GAMMA_ENABLE |
9880 CURSOR_FORMAT_ARGB |
9881 CURSOR_STRIDE(stride);
9882
9883 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009884 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009885
Ville Syrjälädc41c152014-08-13 11:57:05 +03009886 if (intel_crtc->cursor_cntl != 0 &&
9887 (intel_crtc->cursor_base != base ||
9888 intel_crtc->cursor_size != size ||
9889 intel_crtc->cursor_cntl != cntl)) {
9890 /* On these chipsets we can only modify the base/size/stride
9891 * whilst the cursor is disabled.
9892 */
9893 I915_WRITE(_CURACNTR, 0);
9894 POSTING_READ(_CURACNTR);
9895 intel_crtc->cursor_cntl = 0;
9896 }
9897
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009898 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009899 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009900 intel_crtc->cursor_base = base;
9901 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009902
9903 if (intel_crtc->cursor_size != size) {
9904 I915_WRITE(CURSIZE, size);
9905 intel_crtc->cursor_size = size;
9906 }
9907
Chris Wilson4b0e3332014-05-30 16:35:26 +03009908 if (intel_crtc->cursor_cntl != cntl) {
9909 I915_WRITE(_CURACNTR, cntl);
9910 POSTING_READ(_CURACNTR);
9911 intel_crtc->cursor_cntl = cntl;
9912 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009913}
9914
9915static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9916{
9917 struct drm_device *dev = crtc->dev;
9918 struct drm_i915_private *dev_priv = dev->dev_private;
9919 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9920 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009921 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009922
Chris Wilson4b0e3332014-05-30 16:35:26 +03009923 cntl = 0;
9924 if (base) {
9925 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009926 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309927 case 64:
9928 cntl |= CURSOR_MODE_64_ARGB_AX;
9929 break;
9930 case 128:
9931 cntl |= CURSOR_MODE_128_ARGB_AX;
9932 break;
9933 case 256:
9934 cntl |= CURSOR_MODE_256_ARGB_AX;
9935 break;
9936 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009937 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309938 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009939 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009940 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009941
9942 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9943 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009944 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009945
Matt Roper8e7d6882015-01-21 16:35:41 -08009946 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009947 cntl |= CURSOR_ROTATE_180;
9948
Chris Wilson4b0e3332014-05-30 16:35:26 +03009949 if (intel_crtc->cursor_cntl != cntl) {
9950 I915_WRITE(CURCNTR(pipe), cntl);
9951 POSTING_READ(CURCNTR(pipe));
9952 intel_crtc->cursor_cntl = cntl;
9953 }
9954
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009955 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009956 I915_WRITE(CURBASE(pipe), base);
9957 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009958
9959 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009960}
9961
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009962/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009963static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9964 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009965{
9966 struct drm_device *dev = crtc->dev;
9967 struct drm_i915_private *dev_priv = dev->dev_private;
9968 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9969 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009970 int x = crtc->cursor_x;
9971 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009972 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009973
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009974 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009975 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009976
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009977 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009978 base = 0;
9979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009980 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009981 base = 0;
9982
9983 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009984 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009985 base = 0;
9986
9987 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9988 x = -x;
9989 }
9990 pos |= x << CURSOR_X_SHIFT;
9991
9992 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009993 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009994 base = 0;
9995
9996 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9997 y = -y;
9998 }
9999 pos |= y << CURSOR_Y_SHIFT;
10000
Chris Wilson4b0e3332014-05-30 16:35:26 +030010001 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010002 return;
10003
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010004 I915_WRITE(CURPOS(pipe), pos);
10005
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010006 /* ILK+ do this automagically */
10007 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010008 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010009 base += (intel_crtc->base.cursor->state->crtc_h *
10010 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010011 }
10012
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010013 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010014 i845_update_cursor(crtc, base);
10015 else
10016 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010017}
10018
Ville Syrjälädc41c152014-08-13 11:57:05 +030010019static bool cursor_size_ok(struct drm_device *dev,
10020 uint32_t width, uint32_t height)
10021{
10022 if (width == 0 || height == 0)
10023 return false;
10024
10025 /*
10026 * 845g/865g are special in that they are only limited by
10027 * the width of their cursors, the height is arbitrary up to
10028 * the precision of the register. Everything else requires
10029 * square cursors, limited to a few power-of-two sizes.
10030 */
10031 if (IS_845G(dev) || IS_I865G(dev)) {
10032 if ((width & 63) != 0)
10033 return false;
10034
10035 if (width > (IS_845G(dev) ? 64 : 512))
10036 return false;
10037
10038 if (height > 1023)
10039 return false;
10040 } else {
10041 switch (width | height) {
10042 case 256:
10043 case 128:
10044 if (IS_GEN2(dev))
10045 return false;
10046 case 64:
10047 break;
10048 default:
10049 return false;
10050 }
10051 }
10052
10053 return true;
10054}
10055
Jesse Barnes79e53942008-11-07 14:24:08 -080010056static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010057 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010058{
James Simmons72034252010-08-03 01:33:19 +010010059 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010060 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010061
James Simmons72034252010-08-03 01:33:19 +010010062 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010063 intel_crtc->lut_r[i] = red[i] >> 8;
10064 intel_crtc->lut_g[i] = green[i] >> 8;
10065 intel_crtc->lut_b[i] = blue[i] >> 8;
10066 }
10067
10068 intel_crtc_load_lut(crtc);
10069}
10070
Jesse Barnes79e53942008-11-07 14:24:08 -080010071/* VESA 640x480x72Hz mode to set on the pipe */
10072static struct drm_display_mode load_detect_mode = {
10073 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10074 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10075};
10076
Daniel Vettera8bb6812014-02-10 18:00:39 +010010077struct drm_framebuffer *
10078__intel_framebuffer_create(struct drm_device *dev,
10079 struct drm_mode_fb_cmd2 *mode_cmd,
10080 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010081{
10082 struct intel_framebuffer *intel_fb;
10083 int ret;
10084
10085 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10086 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010087 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010088 return ERR_PTR(-ENOMEM);
10089 }
10090
10091 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010092 if (ret)
10093 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010094
10095 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010096err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010097 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010098 kfree(intel_fb);
10099
10100 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010101}
10102
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010103static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010104intel_framebuffer_create(struct drm_device *dev,
10105 struct drm_mode_fb_cmd2 *mode_cmd,
10106 struct drm_i915_gem_object *obj)
10107{
10108 struct drm_framebuffer *fb;
10109 int ret;
10110
10111 ret = i915_mutex_lock_interruptible(dev);
10112 if (ret)
10113 return ERR_PTR(ret);
10114 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10115 mutex_unlock(&dev->struct_mutex);
10116
10117 return fb;
10118}
10119
Chris Wilsond2dff872011-04-19 08:36:26 +010010120static u32
10121intel_framebuffer_pitch_for_width(int width, int bpp)
10122{
10123 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10124 return ALIGN(pitch, 64);
10125}
10126
10127static u32
10128intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10129{
10130 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010131 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010132}
10133
10134static struct drm_framebuffer *
10135intel_framebuffer_create_for_mode(struct drm_device *dev,
10136 struct drm_display_mode *mode,
10137 int depth, int bpp)
10138{
10139 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010140 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010141
10142 obj = i915_gem_alloc_object(dev,
10143 intel_framebuffer_size_for_mode(mode, bpp));
10144 if (obj == NULL)
10145 return ERR_PTR(-ENOMEM);
10146
10147 mode_cmd.width = mode->hdisplay;
10148 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010149 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10150 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010151 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010152
10153 return intel_framebuffer_create(dev, &mode_cmd, obj);
10154}
10155
10156static struct drm_framebuffer *
10157mode_fits_in_fbdev(struct drm_device *dev,
10158 struct drm_display_mode *mode)
10159{
Daniel Vetter4520f532013-10-09 09:18:51 +020010160#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010161 struct drm_i915_private *dev_priv = dev->dev_private;
10162 struct drm_i915_gem_object *obj;
10163 struct drm_framebuffer *fb;
10164
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010165 if (!dev_priv->fbdev)
10166 return NULL;
10167
10168 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010169 return NULL;
10170
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010171 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010172 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010173
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010174 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010175 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10176 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010177 return NULL;
10178
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010179 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010180 return NULL;
10181
10182 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010183#else
10184 return NULL;
10185#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010186}
10187
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010188static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10189 struct drm_crtc *crtc,
10190 struct drm_display_mode *mode,
10191 struct drm_framebuffer *fb,
10192 int x, int y)
10193{
10194 struct drm_plane_state *plane_state;
10195 int hdisplay, vdisplay;
10196 int ret;
10197
10198 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10199 if (IS_ERR(plane_state))
10200 return PTR_ERR(plane_state);
10201
10202 if (mode)
10203 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10204 else
10205 hdisplay = vdisplay = 0;
10206
10207 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10208 if (ret)
10209 return ret;
10210 drm_atomic_set_fb_for_plane(plane_state, fb);
10211 plane_state->crtc_x = 0;
10212 plane_state->crtc_y = 0;
10213 plane_state->crtc_w = hdisplay;
10214 plane_state->crtc_h = vdisplay;
10215 plane_state->src_x = x << 16;
10216 plane_state->src_y = y << 16;
10217 plane_state->src_w = hdisplay << 16;
10218 plane_state->src_h = vdisplay << 16;
10219
10220 return 0;
10221}
10222
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010223bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010224 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010225 struct intel_load_detect_pipe *old,
10226 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010227{
10228 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010229 struct intel_encoder *intel_encoder =
10230 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010231 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010232 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010233 struct drm_crtc *crtc = NULL;
10234 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010235 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010236 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010237 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010238 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010239 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010240 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010241
Chris Wilsond2dff872011-04-19 08:36:26 +010010242 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010243 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010244 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010245
Rob Clark51fd3712013-11-19 12:10:12 -050010246retry:
10247 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10248 if (ret)
10249 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010250
Jesse Barnes79e53942008-11-07 14:24:08 -080010251 /*
10252 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010253 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010254 * - if the connector already has an assigned crtc, use it (but make
10255 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010256 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010257 * - try to find the first unused crtc that can drive this connector,
10258 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010259 */
10260
10261 /* See if we already have a CRTC for this connector */
10262 if (encoder->crtc) {
10263 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010264
Rob Clark51fd3712013-11-19 12:10:12 -050010265 ret = drm_modeset_lock(&crtc->mutex, ctx);
10266 if (ret)
10267 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010268 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10269 if (ret)
10270 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010271
Daniel Vetter24218aa2012-08-12 19:27:11 +020010272 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010273 old->load_detect_temp = false;
10274
10275 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010276 if (connector->dpms != DRM_MODE_DPMS_ON)
10277 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010278
Chris Wilson71731882011-04-19 23:10:58 +010010279 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010280 }
10281
10282 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010283 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010284 i++;
10285 if (!(encoder->possible_crtcs & (1 << i)))
10286 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010287 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010288 continue;
10289 /* This can occur when applying the pipe A quirk on resume. */
10290 if (to_intel_crtc(possible_crtc)->new_enabled)
10291 continue;
10292
10293 crtc = possible_crtc;
10294 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010295 }
10296
10297 /*
10298 * If we didn't find an unused CRTC, don't use any.
10299 */
10300 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010301 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010302 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010303 }
10304
Rob Clark51fd3712013-11-19 12:10:12 -050010305 ret = drm_modeset_lock(&crtc->mutex, ctx);
10306 if (ret)
10307 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010308 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10309 if (ret)
10310 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010311 intel_encoder->new_crtc = to_intel_crtc(crtc);
10312 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010313
10314 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010315 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010316 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010317 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010318 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010319
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010320 state = drm_atomic_state_alloc(dev);
10321 if (!state)
10322 return false;
10323
10324 state->acquire_ctx = ctx;
10325
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010326 connector_state = drm_atomic_get_connector_state(state, connector);
10327 if (IS_ERR(connector_state)) {
10328 ret = PTR_ERR(connector_state);
10329 goto fail;
10330 }
10331
10332 connector_state->crtc = crtc;
10333 connector_state->best_encoder = &intel_encoder->base;
10334
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010335 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10336 if (IS_ERR(crtc_state)) {
10337 ret = PTR_ERR(crtc_state);
10338 goto fail;
10339 }
10340
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010341 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010342
Chris Wilson64927112011-04-20 07:25:26 +010010343 if (!mode)
10344 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010345
Chris Wilsond2dff872011-04-19 08:36:26 +010010346 /* We need a framebuffer large enough to accommodate all accesses
10347 * that the plane may generate whilst we perform load detection.
10348 * We can not rely on the fbcon either being present (we get called
10349 * during its initialisation to detect all boot displays, or it may
10350 * not even exist) or that it is large enough to satisfy the
10351 * requested mode.
10352 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010353 fb = mode_fits_in_fbdev(dev, mode);
10354 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010355 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010356 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10357 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010358 } else
10359 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010360 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010361 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010362 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010363 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010364
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010365 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10366 if (ret)
10367 goto fail;
10368
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010369 drm_mode_copy(&crtc_state->base.mode, mode);
10370
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010371 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010372 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010373 if (old->release_fb)
10374 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010375 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010376 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010377 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010378
Jesse Barnes79e53942008-11-07 14:24:08 -080010379 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010380 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010381 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010382
10383 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010384 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010385fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010386 drm_atomic_state_free(state);
10387 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010388
Rob Clark51fd3712013-11-19 12:10:12 -050010389 if (ret == -EDEADLK) {
10390 drm_modeset_backoff(ctx);
10391 goto retry;
10392 }
10393
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010394 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010395}
10396
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010397void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010398 struct intel_load_detect_pipe *old,
10399 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010400{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010401 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010402 struct intel_encoder *intel_encoder =
10403 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010404 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010405 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010406 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010407 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010408 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010409 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010410 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010411
Chris Wilsond2dff872011-04-19 08:36:26 +010010412 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010413 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010414 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010415
Chris Wilson8261b192011-04-19 23:18:09 +010010416 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010417 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010418 if (!state)
10419 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010420
10421 state->acquire_ctx = ctx;
10422
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010423 connector_state = drm_atomic_get_connector_state(state, connector);
10424 if (IS_ERR(connector_state))
10425 goto fail;
10426
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010427 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10428 if (IS_ERR(crtc_state))
10429 goto fail;
10430
Daniel Vetterfc303102012-07-09 10:40:58 +020010431 to_intel_connector(connector)->new_encoder = NULL;
10432 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010433 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010434
10435 connector_state->best_encoder = NULL;
10436 connector_state->crtc = NULL;
10437
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010438 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010439
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010440 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10441 0, 0);
10442 if (ret)
10443 goto fail;
10444
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010445 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010446 if (ret)
10447 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010448
Daniel Vetter36206362012-12-10 20:42:17 +010010449 if (old->release_fb) {
10450 drm_framebuffer_unregister_private(old->release_fb);
10451 drm_framebuffer_unreference(old->release_fb);
10452 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010453
Chris Wilson0622a532011-04-21 09:32:11 +010010454 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010455 }
10456
Eric Anholtc751ce42010-03-25 11:48:48 -070010457 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010458 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10459 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010460
10461 return;
10462fail:
10463 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10464 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010465}
10466
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010467static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010468 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010469{
10470 struct drm_i915_private *dev_priv = dev->dev_private;
10471 u32 dpll = pipe_config->dpll_hw_state.dpll;
10472
10473 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010474 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010475 else if (HAS_PCH_SPLIT(dev))
10476 return 120000;
10477 else if (!IS_GEN2(dev))
10478 return 96000;
10479 else
10480 return 48000;
10481}
10482
Jesse Barnes79e53942008-11-07 14:24:08 -080010483/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010484static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010485 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010486{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010487 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010488 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010489 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010490 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010491 u32 fp;
10492 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010493 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010494 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010495
10496 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010497 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010498 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010499 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010500
10501 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010502 if (IS_PINEVIEW(dev)) {
10503 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10504 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010505 } else {
10506 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10507 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10508 }
10509
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010510 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010511 if (IS_PINEVIEW(dev))
10512 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10513 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010514 else
10515 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010516 DPLL_FPA01_P1_POST_DIV_SHIFT);
10517
10518 switch (dpll & DPLL_MODE_MASK) {
10519 case DPLLB_MODE_DAC_SERIAL:
10520 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10521 5 : 10;
10522 break;
10523 case DPLLB_MODE_LVDS:
10524 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10525 7 : 14;
10526 break;
10527 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010528 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010529 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010530 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010531 }
10532
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010533 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010534 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010535 else
Imre Deakdccbea32015-06-22 23:35:51 +030010536 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010537 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010538 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010539 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010540
10541 if (is_lvds) {
10542 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10543 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010544
10545 if (lvds & LVDS_CLKB_POWER_UP)
10546 clock.p2 = 7;
10547 else
10548 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010549 } else {
10550 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10551 clock.p1 = 2;
10552 else {
10553 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10554 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10555 }
10556 if (dpll & PLL_P2_DIVIDE_BY_4)
10557 clock.p2 = 4;
10558 else
10559 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010560 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010561
Imre Deakdccbea32015-06-22 23:35:51 +030010562 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010563 }
10564
Ville Syrjälä18442d02013-09-13 16:00:08 +030010565 /*
10566 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010567 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010568 * encoder's get_config() function.
10569 */
Imre Deakdccbea32015-06-22 23:35:51 +030010570 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010571}
10572
Ville Syrjälä6878da02013-09-13 15:59:11 +030010573int intel_dotclock_calculate(int link_freq,
10574 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010575{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010576 /*
10577 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010578 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010579 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010580 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010581 *
10582 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010583 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010584 */
10585
Ville Syrjälä6878da02013-09-13 15:59:11 +030010586 if (!m_n->link_n)
10587 return 0;
10588
10589 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10590}
10591
Ville Syrjälä18442d02013-09-13 16:00:08 +030010592static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010593 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010594{
10595 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010596
10597 /* read out port_clock from the DPLL */
10598 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010599
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010600 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010601 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010602 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010603 * agree once we know their relationship in the encoder's
10604 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010605 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010606 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010607 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10608 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010609}
10610
10611/** Returns the currently programmed mode of the given pipe. */
10612struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10613 struct drm_crtc *crtc)
10614{
Jesse Barnes548f2452011-02-17 10:40:53 -080010615 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010616 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010617 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010618 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010619 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010620 int htot = I915_READ(HTOTAL(cpu_transcoder));
10621 int hsync = I915_READ(HSYNC(cpu_transcoder));
10622 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10623 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010624 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010625
10626 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10627 if (!mode)
10628 return NULL;
10629
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010630 /*
10631 * Construct a pipe_config sufficient for getting the clock info
10632 * back out of crtc_clock_get.
10633 *
10634 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10635 * to use a real value here instead.
10636 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010637 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010638 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010639 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10640 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10641 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010642 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10643
Ville Syrjälä773ae032013-09-23 17:48:20 +030010644 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010645 mode->hdisplay = (htot & 0xffff) + 1;
10646 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10647 mode->hsync_start = (hsync & 0xffff) + 1;
10648 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10649 mode->vdisplay = (vtot & 0xffff) + 1;
10650 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10651 mode->vsync_start = (vsync & 0xffff) + 1;
10652 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10653
10654 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010655
10656 return mode;
10657}
10658
Chris Wilsonf047e392012-07-21 12:31:41 +010010659void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010660{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010661 struct drm_i915_private *dev_priv = dev->dev_private;
10662
Chris Wilsonf62a0072014-02-21 17:55:39 +000010663 if (dev_priv->mm.busy)
10664 return;
10665
Paulo Zanoni43694d62014-03-07 20:08:08 -030010666 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010667 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010668 if (INTEL_INFO(dev)->gen >= 6)
10669 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010670 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010671}
10672
10673void intel_mark_idle(struct drm_device *dev)
10674{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010675 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010676
Chris Wilsonf62a0072014-02-21 17:55:39 +000010677 if (!dev_priv->mm.busy)
10678 return;
10679
10680 dev_priv->mm.busy = false;
10681
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010682 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010683 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010684
Paulo Zanoni43694d62014-03-07 20:08:08 -030010685 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010686}
10687
Jesse Barnes79e53942008-11-07 14:24:08 -080010688static void intel_crtc_destroy(struct drm_crtc *crtc)
10689{
10690 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010691 struct drm_device *dev = crtc->dev;
10692 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010693
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010694 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010695 work = intel_crtc->unpin_work;
10696 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010697 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010698
10699 if (work) {
10700 cancel_work_sync(&work->work);
10701 kfree(work);
10702 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010703
10704 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010705
Jesse Barnes79e53942008-11-07 14:24:08 -080010706 kfree(intel_crtc);
10707}
10708
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010709static void intel_unpin_work_fn(struct work_struct *__work)
10710{
10711 struct intel_unpin_work *work =
10712 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010713 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10714 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -030010715 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010716 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010717
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010718 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010719 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010720 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010721
Paulo Zanoni7733b492015-07-07 15:26:04 -030010722 intel_fbc_update(dev_priv);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010723
10724 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010725 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010726 mutex_unlock(&dev->struct_mutex);
10727
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010728 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010729 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010730
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010731 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10732 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010733
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010734 kfree(work);
10735}
10736
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010737static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010738 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010739{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010740 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10741 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010742 unsigned long flags;
10743
10744 /* Ignore early vblank irqs */
10745 if (intel_crtc == NULL)
10746 return;
10747
Daniel Vetterf3260382014-09-15 14:55:23 +020010748 /*
10749 * This is called both by irq handlers and the reset code (to complete
10750 * lost pageflips) so needs the full irqsave spinlocks.
10751 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010752 spin_lock_irqsave(&dev->event_lock, flags);
10753 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010754
10755 /* Ensure we don't miss a work->pending update ... */
10756 smp_rmb();
10757
10758 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010759 spin_unlock_irqrestore(&dev->event_lock, flags);
10760 return;
10761 }
10762
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010763 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010764
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010765 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010766}
10767
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010768void intel_finish_page_flip(struct drm_device *dev, int pipe)
10769{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010770 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010771 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10772
Mario Kleiner49b14a52010-12-09 07:00:07 +010010773 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010774}
10775
10776void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10777{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010778 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010779 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10780
Mario Kleiner49b14a52010-12-09 07:00:07 +010010781 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010782}
10783
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010784/* Is 'a' after or equal to 'b'? */
10785static bool g4x_flip_count_after_eq(u32 a, u32 b)
10786{
10787 return !((a - b) & 0x80000000);
10788}
10789
10790static bool page_flip_finished(struct intel_crtc *crtc)
10791{
10792 struct drm_device *dev = crtc->base.dev;
10793 struct drm_i915_private *dev_priv = dev->dev_private;
10794
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010795 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10796 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10797 return true;
10798
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010799 /*
10800 * The relevant registers doen't exist on pre-ctg.
10801 * As the flip done interrupt doesn't trigger for mmio
10802 * flips on gmch platforms, a flip count check isn't
10803 * really needed there. But since ctg has the registers,
10804 * include it in the check anyway.
10805 */
10806 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10807 return true;
10808
10809 /*
10810 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10811 * used the same base address. In that case the mmio flip might
10812 * have completed, but the CS hasn't even executed the flip yet.
10813 *
10814 * A flip count check isn't enough as the CS might have updated
10815 * the base address just after start of vblank, but before we
10816 * managed to process the interrupt. This means we'd complete the
10817 * CS flip too soon.
10818 *
10819 * Combining both checks should get us a good enough result. It may
10820 * still happen that the CS flip has been executed, but has not
10821 * yet actually completed. But in case the base address is the same
10822 * anyway, we don't really care.
10823 */
10824 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10825 crtc->unpin_work->gtt_offset &&
10826 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10827 crtc->unpin_work->flip_count);
10828}
10829
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010830void intel_prepare_page_flip(struct drm_device *dev, int plane)
10831{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010832 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010833 struct intel_crtc *intel_crtc =
10834 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10835 unsigned long flags;
10836
Daniel Vetterf3260382014-09-15 14:55:23 +020010837
10838 /*
10839 * This is called both by irq handlers and the reset code (to complete
10840 * lost pageflips) so needs the full irqsave spinlocks.
10841 *
10842 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010843 * generate a page-flip completion irq, i.e. every modeset
10844 * is also accompanied by a spurious intel_prepare_page_flip().
10845 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010846 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010847 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010848 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010849 spin_unlock_irqrestore(&dev->event_lock, flags);
10850}
10851
Robin Schroereba905b2014-05-18 02:24:50 +020010852static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010853{
10854 /* Ensure that the work item is consistent when activating it ... */
10855 smp_wmb();
10856 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10857 /* and that it is marked active as soon as the irq could fire. */
10858 smp_wmb();
10859}
10860
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010861static int intel_gen2_queue_flip(struct drm_device *dev,
10862 struct drm_crtc *crtc,
10863 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010864 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010865 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010866 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010867{
John Harrison6258fbe2015-05-29 17:43:48 +010010868 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010869 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010870 u32 flip_mask;
10871 int ret;
10872
John Harrison5fb9de12015-05-29 17:44:07 +010010873 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010874 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010875 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010876
10877 /* Can't queue multiple flips, so wait for the previous
10878 * one to finish before executing the next.
10879 */
10880 if (intel_crtc->plane)
10881 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10882 else
10883 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010884 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10885 intel_ring_emit(ring, MI_NOOP);
10886 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10887 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10888 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010889 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010890 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010891
10892 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010893 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010894}
10895
10896static int intel_gen3_queue_flip(struct drm_device *dev,
10897 struct drm_crtc *crtc,
10898 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010899 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010900 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010901 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010902{
John Harrison6258fbe2015-05-29 17:43:48 +010010903 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010904 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010905 u32 flip_mask;
10906 int ret;
10907
John Harrison5fb9de12015-05-29 17:44:07 +010010908 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010909 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010910 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010911
10912 if (intel_crtc->plane)
10913 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10914 else
10915 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010916 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10917 intel_ring_emit(ring, MI_NOOP);
10918 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10919 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10920 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010921 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010922 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010923
Chris Wilsone7d841c2012-12-03 11:36:30 +000010924 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010925 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010926}
10927
10928static int intel_gen4_queue_flip(struct drm_device *dev,
10929 struct drm_crtc *crtc,
10930 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010931 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010932 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010933 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010934{
John Harrison6258fbe2015-05-29 17:43:48 +010010935 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010936 struct drm_i915_private *dev_priv = dev->dev_private;
10937 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10938 uint32_t pf, pipesrc;
10939 int ret;
10940
John Harrison5fb9de12015-05-29 17:44:07 +010010941 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010942 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010943 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010944
10945 /* i965+ uses the linear or tiled offsets from the
10946 * Display Registers (which do not change across a page-flip)
10947 * so we need only reprogram the base address.
10948 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010949 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10950 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10951 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010952 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010953 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010954
10955 /* XXX Enabling the panel-fitter across page-flip is so far
10956 * untested on non-native modes, so ignore it for now.
10957 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10958 */
10959 pf = 0;
10960 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010961 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010962
10963 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010964 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010965}
10966
10967static int intel_gen6_queue_flip(struct drm_device *dev,
10968 struct drm_crtc *crtc,
10969 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010970 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010971 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010972 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010973{
John Harrison6258fbe2015-05-29 17:43:48 +010010974 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010975 struct drm_i915_private *dev_priv = dev->dev_private;
10976 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10977 uint32_t pf, pipesrc;
10978 int ret;
10979
John Harrison5fb9de12015-05-29 17:44:07 +010010980 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010981 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010982 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010983
Daniel Vetter6d90c952012-04-26 23:28:05 +020010984 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10985 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10986 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010987 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010988
Chris Wilson99d9acd2012-04-17 20:37:00 +010010989 /* Contrary to the suggestions in the documentation,
10990 * "Enable Panel Fitter" does not seem to be required when page
10991 * flipping with a non-native mode, and worse causes a normal
10992 * modeset to fail.
10993 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10994 */
10995 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010996 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010997 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010998
10999 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011000 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011001}
11002
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011003static int intel_gen7_queue_flip(struct drm_device *dev,
11004 struct drm_crtc *crtc,
11005 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011006 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011007 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011008 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011009{
John Harrison6258fbe2015-05-29 17:43:48 +010011010 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011011 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011012 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011013 int len, ret;
11014
Robin Schroereba905b2014-05-18 02:24:50 +020011015 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011016 case PLANE_A:
11017 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11018 break;
11019 case PLANE_B:
11020 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11021 break;
11022 case PLANE_C:
11023 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11024 break;
11025 default:
11026 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011027 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011028 }
11029
Chris Wilsonffe74d72013-08-26 20:58:12 +010011030 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011031 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011032 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011033 /*
11034 * On Gen 8, SRM is now taking an extra dword to accommodate
11035 * 48bits addresses, and we need a NOOP for the batch size to
11036 * stay even.
11037 */
11038 if (IS_GEN8(dev))
11039 len += 2;
11040 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011041
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011042 /*
11043 * BSpec MI_DISPLAY_FLIP for IVB:
11044 * "The full packet must be contained within the same cache line."
11045 *
11046 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11047 * cacheline, if we ever start emitting more commands before
11048 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11049 * then do the cacheline alignment, and finally emit the
11050 * MI_DISPLAY_FLIP.
11051 */
John Harrisonbba09b12015-05-29 17:44:06 +010011052 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011053 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011054 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011055
John Harrison5fb9de12015-05-29 17:44:07 +010011056 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011057 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011058 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011059
Chris Wilsonffe74d72013-08-26 20:58:12 +010011060 /* Unmask the flip-done completion message. Note that the bspec says that
11061 * we should do this for both the BCS and RCS, and that we must not unmask
11062 * more than one flip event at any time (or ensure that one flip message
11063 * can be sent by waiting for flip-done prior to queueing new flips).
11064 * Experimentation says that BCS works despite DERRMR masking all
11065 * flip-done completion events and that unmasking all planes at once
11066 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11067 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11068 */
11069 if (ring->id == RCS) {
11070 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11071 intel_ring_emit(ring, DERRMR);
11072 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11073 DERRMR_PIPEB_PRI_FLIP_DONE |
11074 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011075 if (IS_GEN8(dev))
11076 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11077 MI_SRM_LRM_GLOBAL_GTT);
11078 else
11079 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11080 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011081 intel_ring_emit(ring, DERRMR);
11082 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011083 if (IS_GEN8(dev)) {
11084 intel_ring_emit(ring, 0);
11085 intel_ring_emit(ring, MI_NOOP);
11086 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011087 }
11088
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011089 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011090 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011091 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011092 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011093
11094 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011095 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011096}
11097
Sourab Gupta84c33a62014-06-02 16:47:17 +053011098static bool use_mmio_flip(struct intel_engine_cs *ring,
11099 struct drm_i915_gem_object *obj)
11100{
11101 /*
11102 * This is not being used for older platforms, because
11103 * non-availability of flip done interrupt forces us to use
11104 * CS flips. Older platforms derive flip done using some clever
11105 * tricks involving the flip_pending status bits and vblank irqs.
11106 * So using MMIO flips there would disrupt this mechanism.
11107 */
11108
Chris Wilson8e09bf82014-07-08 10:40:30 +010011109 if (ring == NULL)
11110 return true;
11111
Sourab Gupta84c33a62014-06-02 16:47:17 +053011112 if (INTEL_INFO(ring->dev)->gen < 5)
11113 return false;
11114
11115 if (i915.use_mmio_flip < 0)
11116 return false;
11117 else if (i915.use_mmio_flip > 0)
11118 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011119 else if (i915.enable_execlists)
11120 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011121 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011122 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011123}
11124
Damien Lespiauff944562014-11-20 14:58:16 +000011125static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11126{
11127 struct drm_device *dev = intel_crtc->base.dev;
11128 struct drm_i915_private *dev_priv = dev->dev_private;
11129 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011130 const enum pipe pipe = intel_crtc->pipe;
11131 u32 ctl, stride;
11132
11133 ctl = I915_READ(PLANE_CTL(pipe, 0));
11134 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011135 switch (fb->modifier[0]) {
11136 case DRM_FORMAT_MOD_NONE:
11137 break;
11138 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011139 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011140 break;
11141 case I915_FORMAT_MOD_Y_TILED:
11142 ctl |= PLANE_CTL_TILED_Y;
11143 break;
11144 case I915_FORMAT_MOD_Yf_TILED:
11145 ctl |= PLANE_CTL_TILED_YF;
11146 break;
11147 default:
11148 MISSING_CASE(fb->modifier[0]);
11149 }
Damien Lespiauff944562014-11-20 14:58:16 +000011150
11151 /*
11152 * The stride is either expressed as a multiple of 64 bytes chunks for
11153 * linear buffers or in number of tiles for tiled buffers.
11154 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011155 stride = fb->pitches[0] /
11156 intel_fb_stride_alignment(dev, fb->modifier[0],
11157 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011158
11159 /*
11160 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11161 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11162 */
11163 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11164 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11165
11166 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11167 POSTING_READ(PLANE_SURF(pipe, 0));
11168}
11169
11170static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011171{
11172 struct drm_device *dev = intel_crtc->base.dev;
11173 struct drm_i915_private *dev_priv = dev->dev_private;
11174 struct intel_framebuffer *intel_fb =
11175 to_intel_framebuffer(intel_crtc->base.primary->fb);
11176 struct drm_i915_gem_object *obj = intel_fb->obj;
11177 u32 dspcntr;
11178 u32 reg;
11179
Sourab Gupta84c33a62014-06-02 16:47:17 +053011180 reg = DSPCNTR(intel_crtc->plane);
11181 dspcntr = I915_READ(reg);
11182
Damien Lespiauc5d97472014-10-25 00:11:11 +010011183 if (obj->tiling_mode != I915_TILING_NONE)
11184 dspcntr |= DISPPLANE_TILED;
11185 else
11186 dspcntr &= ~DISPPLANE_TILED;
11187
Sourab Gupta84c33a62014-06-02 16:47:17 +053011188 I915_WRITE(reg, dspcntr);
11189
11190 I915_WRITE(DSPSURF(intel_crtc->plane),
11191 intel_crtc->unpin_work->gtt_offset);
11192 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011193
Damien Lespiauff944562014-11-20 14:58:16 +000011194}
11195
11196/*
11197 * XXX: This is the temporary way to update the plane registers until we get
11198 * around to using the usual plane update functions for MMIO flips
11199 */
11200static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11201{
11202 struct drm_device *dev = intel_crtc->base.dev;
11203 bool atomic_update;
11204 u32 start_vbl_count;
11205
11206 intel_mark_page_flip_active(intel_crtc);
11207
11208 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11209
11210 if (INTEL_INFO(dev)->gen >= 9)
11211 skl_do_mmio_flip(intel_crtc);
11212 else
11213 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11214 ilk_do_mmio_flip(intel_crtc);
11215
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011216 if (atomic_update)
11217 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011218}
11219
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011220static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011221{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011222 struct intel_mmio_flip *mmio_flip =
11223 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011224
Daniel Vettereed29a52015-05-21 14:21:25 +020011225 if (mmio_flip->req)
11226 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011227 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011228 false, NULL,
11229 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011230
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011231 intel_do_mmio_flip(mmio_flip->crtc);
11232
Daniel Vettereed29a52015-05-21 14:21:25 +020011233 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011234 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011235}
11236
11237static int intel_queue_mmio_flip(struct drm_device *dev,
11238 struct drm_crtc *crtc,
11239 struct drm_framebuffer *fb,
11240 struct drm_i915_gem_object *obj,
11241 struct intel_engine_cs *ring,
11242 uint32_t flags)
11243{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011244 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011245
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011246 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11247 if (mmio_flip == NULL)
11248 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011249
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011250 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011251 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011252 mmio_flip->crtc = to_intel_crtc(crtc);
11253
11254 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11255 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011256
Sourab Gupta84c33a62014-06-02 16:47:17 +053011257 return 0;
11258}
11259
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011260static int intel_default_queue_flip(struct drm_device *dev,
11261 struct drm_crtc *crtc,
11262 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011263 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011264 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011265 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011266{
11267 return -ENODEV;
11268}
11269
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011270static bool __intel_pageflip_stall_check(struct drm_device *dev,
11271 struct drm_crtc *crtc)
11272{
11273 struct drm_i915_private *dev_priv = dev->dev_private;
11274 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11275 struct intel_unpin_work *work = intel_crtc->unpin_work;
11276 u32 addr;
11277
11278 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11279 return true;
11280
11281 if (!work->enable_stall_check)
11282 return false;
11283
11284 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011285 if (work->flip_queued_req &&
11286 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011287 return false;
11288
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011289 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011290 }
11291
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011292 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011293 return false;
11294
11295 /* Potential stall - if we see that the flip has happened,
11296 * assume a missed interrupt. */
11297 if (INTEL_INFO(dev)->gen >= 4)
11298 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11299 else
11300 addr = I915_READ(DSPADDR(intel_crtc->plane));
11301
11302 /* There is a potential issue here with a false positive after a flip
11303 * to the same address. We could address this by checking for a
11304 * non-incrementing frame counter.
11305 */
11306 return addr == work->gtt_offset;
11307}
11308
11309void intel_check_page_flip(struct drm_device *dev, int pipe)
11310{
11311 struct drm_i915_private *dev_priv = dev->dev_private;
11312 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11313 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011314 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011315
Dave Gordon6c51d462015-03-06 15:34:26 +000011316 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011317
11318 if (crtc == NULL)
11319 return;
11320
Daniel Vetterf3260382014-09-15 14:55:23 +020011321 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011322 work = intel_crtc->unpin_work;
11323 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011324 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011325 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011326 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011327 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011328 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011329 if (work != NULL &&
11330 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11331 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011332 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011333}
11334
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011335static int intel_crtc_page_flip(struct drm_crtc *crtc,
11336 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011337 struct drm_pending_vblank_event *event,
11338 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011339{
11340 struct drm_device *dev = crtc->dev;
11341 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011342 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011343 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011344 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011345 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011346 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011347 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011348 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011349 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011350 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011351 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011352
Matt Roper2ff8fde2014-07-08 07:50:07 -070011353 /*
11354 * drm_mode_page_flip_ioctl() should already catch this, but double
11355 * check to be safe. In the future we may enable pageflipping from
11356 * a disabled primary plane.
11357 */
11358 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11359 return -EBUSY;
11360
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011361 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011362 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011363 return -EINVAL;
11364
11365 /*
11366 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11367 * Note that pitch changes could also affect these register.
11368 */
11369 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011370 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11371 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011372 return -EINVAL;
11373
Chris Wilsonf900db42014-02-20 09:26:13 +000011374 if (i915_terminally_wedged(&dev_priv->gpu_error))
11375 goto out_hang;
11376
Daniel Vetterb14c5672013-09-19 12:18:32 +020011377 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011378 if (work == NULL)
11379 return -ENOMEM;
11380
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011381 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011382 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011383 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011384 INIT_WORK(&work->work, intel_unpin_work_fn);
11385
Daniel Vetter87b6b102014-05-15 15:33:46 +020011386 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011387 if (ret)
11388 goto free_work;
11389
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011390 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011391 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011392 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011393 /* Before declaring the flip queue wedged, check if
11394 * the hardware completed the operation behind our backs.
11395 */
11396 if (__intel_pageflip_stall_check(dev, crtc)) {
11397 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11398 page_flip_completed(intel_crtc);
11399 } else {
11400 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011401 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011402
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011403 drm_crtc_vblank_put(crtc);
11404 kfree(work);
11405 return -EBUSY;
11406 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011407 }
11408 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011409 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011410
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011411 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11412 flush_workqueue(dev_priv->wq);
11413
Jesse Barnes75dfca82010-02-10 15:09:44 -080011414 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011415 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011416 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011417
Matt Roperf4510a22014-04-01 15:22:40 -070011418 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011419 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011420
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011421 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011422
Chris Wilson89ed88b2015-02-16 14:31:49 +000011423 ret = i915_mutex_lock_interruptible(dev);
11424 if (ret)
11425 goto cleanup;
11426
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011427 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011428 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011429
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011430 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011431 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011432
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011433 if (IS_VALLEYVIEW(dev)) {
11434 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011435 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011436 /* vlv: DISPLAY_FLIP fails to change tiling */
11437 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011438 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011439 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011440 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011441 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011442 if (ring == NULL || ring->id != RCS)
11443 ring = &dev_priv->ring[BCS];
11444 } else {
11445 ring = &dev_priv->ring[RCS];
11446 }
11447
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011448 mmio_flip = use_mmio_flip(ring, obj);
11449
11450 /* When using CS flips, we want to emit semaphores between rings.
11451 * However, when using mmio flips we will create a task to do the
11452 * synchronisation, so all we want here is to pin the framebuffer
11453 * into the display plane and skip any waits.
11454 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011455 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011456 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011457 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011458 if (ret)
11459 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011460
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011461 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11462 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011463
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011464 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011465 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11466 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011467 if (ret)
11468 goto cleanup_unpin;
11469
John Harrisonf06cc1b2014-11-24 18:49:37 +000011470 i915_gem_request_assign(&work->flip_queued_req,
11471 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011472 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011473 if (!request) {
11474 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11475 if (ret)
11476 goto cleanup_unpin;
11477 }
11478
11479 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011480 page_flip_flags);
11481 if (ret)
11482 goto cleanup_unpin;
11483
John Harrison6258fbe2015-05-29 17:43:48 +010011484 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011485 }
11486
John Harrison91af1272015-06-18 13:14:56 +010011487 if (request)
John Harrison75289872015-05-29 17:43:49 +010011488 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011489
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011490 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011491 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011492
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011493 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011494 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011495 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011496
Paulo Zanoni7733b492015-07-07 15:26:04 -030011497 intel_fbc_disable(dev_priv);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011498 intel_frontbuffer_flip_prepare(dev,
11499 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011500
Jesse Barnese5510fa2010-07-01 16:48:37 -070011501 trace_i915_flip_request(intel_crtc->plane, obj);
11502
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011503 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011504
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011505cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011506 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011507cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011508 if (request)
11509 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011510 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011511 mutex_unlock(&dev->struct_mutex);
11512cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011513 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011514 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011515
Chris Wilson89ed88b2015-02-16 14:31:49 +000011516 drm_gem_object_unreference_unlocked(&obj->base);
11517 drm_framebuffer_unreference(work->old_fb);
11518
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011519 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011520 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011521 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011522
Daniel Vetter87b6b102014-05-15 15:33:46 +020011523 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011524free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011525 kfree(work);
11526
Chris Wilsonf900db42014-02-20 09:26:13 +000011527 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011528 struct drm_atomic_state *state;
11529 struct drm_plane_state *plane_state;
11530
Chris Wilsonf900db42014-02-20 09:26:13 +000011531out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011532 state = drm_atomic_state_alloc(dev);
11533 if (!state)
11534 return -ENOMEM;
11535 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11536
11537retry:
11538 plane_state = drm_atomic_get_plane_state(state, primary);
11539 ret = PTR_ERR_OR_ZERO(plane_state);
11540 if (!ret) {
11541 drm_atomic_set_fb_for_plane(plane_state, fb);
11542
11543 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11544 if (!ret)
11545 ret = drm_atomic_commit(state);
11546 }
11547
11548 if (ret == -EDEADLK) {
11549 drm_modeset_backoff(state->acquire_ctx);
11550 drm_atomic_state_clear(state);
11551 goto retry;
11552 }
11553
11554 if (ret)
11555 drm_atomic_state_free(state);
11556
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011557 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011558 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011559 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011560 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011561 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011562 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011563 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011564}
11565
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011566
11567/**
11568 * intel_wm_need_update - Check whether watermarks need updating
11569 * @plane: drm plane
11570 * @state: new plane state
11571 *
11572 * Check current plane state versus the new one to determine whether
11573 * watermarks need to be recalculated.
11574 *
11575 * Returns true or false.
11576 */
11577static bool intel_wm_need_update(struct drm_plane *plane,
11578 struct drm_plane_state *state)
11579{
11580 /* Update watermarks on tiling changes. */
11581 if (!plane->state->fb || !state->fb ||
11582 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11583 plane->state->rotation != state->rotation)
11584 return true;
11585
11586 if (plane->state->crtc_w != state->crtc_w)
11587 return true;
11588
11589 return false;
11590}
11591
11592int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11593 struct drm_plane_state *plane_state)
11594{
11595 struct drm_crtc *crtc = crtc_state->crtc;
11596 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11597 struct drm_plane *plane = plane_state->plane;
11598 struct drm_device *dev = crtc->dev;
11599 struct drm_i915_private *dev_priv = dev->dev_private;
11600 struct intel_plane_state *old_plane_state =
11601 to_intel_plane_state(plane->state);
11602 int idx = intel_crtc->base.base.id, ret;
11603 int i = drm_plane_index(plane);
11604 bool mode_changed = needs_modeset(crtc_state);
11605 bool was_crtc_enabled = crtc->state->active;
11606 bool is_crtc_enabled = crtc_state->active;
11607
11608 bool turn_off, turn_on, visible, was_visible;
11609 struct drm_framebuffer *fb = plane_state->fb;
11610
11611 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11612 plane->type != DRM_PLANE_TYPE_CURSOR) {
11613 ret = skl_update_scaler_plane(
11614 to_intel_crtc_state(crtc_state),
11615 to_intel_plane_state(plane_state));
11616 if (ret)
11617 return ret;
11618 }
11619
11620 /*
11621 * Disabling a plane is always okay; we just need to update
11622 * fb tracking in a special way since cleanup_fb() won't
11623 * get called by the plane helpers.
11624 */
11625 if (old_plane_state->base.fb && !fb)
11626 intel_crtc->atomic.disabled_planes |= 1 << i;
11627
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011628 was_visible = old_plane_state->visible;
11629 visible = to_intel_plane_state(plane_state)->visible;
11630
11631 if (!was_crtc_enabled && WARN_ON(was_visible))
11632 was_visible = false;
11633
11634 if (!is_crtc_enabled && WARN_ON(visible))
11635 visible = false;
11636
11637 if (!was_visible && !visible)
11638 return 0;
11639
11640 turn_off = was_visible && (!visible || mode_changed);
11641 turn_on = visible && (!was_visible || mode_changed);
11642
11643 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11644 plane->base.id, fb ? fb->base.id : -1);
11645
11646 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11647 plane->base.id, was_visible, visible,
11648 turn_off, turn_on, mode_changed);
11649
Ville Syrjälä852eb002015-06-24 22:00:07 +030011650 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011651 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011652 /* must disable cxsr around plane enable/disable */
11653 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11654 intel_crtc->atomic.disable_cxsr = true;
11655 /* to potentially re-enable cxsr */
11656 intel_crtc->atomic.wait_vblank = true;
11657 intel_crtc->atomic.update_wm_post = true;
11658 }
11659 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011660 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011661 /* must disable cxsr around plane enable/disable */
11662 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11663 if (is_crtc_enabled)
11664 intel_crtc->atomic.wait_vblank = true;
11665 intel_crtc->atomic.disable_cxsr = true;
11666 }
11667 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011668 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011669 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011670
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011671 if (visible)
11672 intel_crtc->atomic.fb_bits |=
11673 to_intel_plane(plane)->frontbuffer_bit;
11674
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011675 switch (plane->type) {
11676 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011677 intel_crtc->atomic.wait_for_flips = true;
11678 intel_crtc->atomic.pre_disable_primary = turn_off;
11679 intel_crtc->atomic.post_enable_primary = turn_on;
11680
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011681 if (turn_off) {
11682 /*
11683 * FIXME: Actually if we will still have any other
11684 * plane enabled on the pipe we could let IPS enabled
11685 * still, but for now lets consider that when we make
11686 * primary invisible by setting DSPCNTR to 0 on
11687 * update_primary_plane function IPS needs to be
11688 * disable.
11689 */
11690 intel_crtc->atomic.disable_ips = true;
11691
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011692 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011693 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011694
11695 /*
11696 * FBC does not work on some platforms for rotated
11697 * planes, so disable it when rotation is not 0 and
11698 * update it when rotation is set back to 0.
11699 *
11700 * FIXME: This is redundant with the fbc update done in
11701 * the primary plane enable function except that that
11702 * one is done too late. We eventually need to unify
11703 * this.
11704 */
11705
11706 if (visible &&
11707 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11708 dev_priv->fbc.crtc == intel_crtc &&
11709 plane_state->rotation != BIT(DRM_ROTATE_0))
11710 intel_crtc->atomic.disable_fbc = true;
11711
11712 /*
11713 * BDW signals flip done immediately if the plane
11714 * is disabled, even if the plane enable is already
11715 * armed to occur at the next vblank :(
11716 */
11717 if (turn_on && IS_BROADWELL(dev))
11718 intel_crtc->atomic.wait_vblank = true;
11719
11720 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11721 break;
11722 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011723 break;
11724 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011725 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011726 intel_crtc->atomic.wait_vblank = true;
11727 intel_crtc->atomic.update_sprite_watermarks |=
11728 1 << i;
11729 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011730 }
11731 return 0;
11732}
11733
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011734static bool encoders_cloneable(const struct intel_encoder *a,
11735 const struct intel_encoder *b)
11736{
11737 /* masks could be asymmetric, so check both ways */
11738 return a == b || (a->cloneable & (1 << b->type) &&
11739 b->cloneable & (1 << a->type));
11740}
11741
11742static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11743 struct intel_crtc *crtc,
11744 struct intel_encoder *encoder)
11745{
11746 struct intel_encoder *source_encoder;
11747 struct drm_connector *connector;
11748 struct drm_connector_state *connector_state;
11749 int i;
11750
11751 for_each_connector_in_state(state, connector, connector_state, i) {
11752 if (connector_state->crtc != &crtc->base)
11753 continue;
11754
11755 source_encoder =
11756 to_intel_encoder(connector_state->best_encoder);
11757 if (!encoders_cloneable(encoder, source_encoder))
11758 return false;
11759 }
11760
11761 return true;
11762}
11763
11764static bool check_encoder_cloning(struct drm_atomic_state *state,
11765 struct intel_crtc *crtc)
11766{
11767 struct intel_encoder *encoder;
11768 struct drm_connector *connector;
11769 struct drm_connector_state *connector_state;
11770 int i;
11771
11772 for_each_connector_in_state(state, connector, connector_state, i) {
11773 if (connector_state->crtc != &crtc->base)
11774 continue;
11775
11776 encoder = to_intel_encoder(connector_state->best_encoder);
11777 if (!check_single_encoder_cloning(state, crtc, encoder))
11778 return false;
11779 }
11780
11781 return true;
11782}
11783
11784static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11785 struct drm_crtc_state *crtc_state)
11786{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011787 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011788 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011789 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011790 struct intel_crtc_state *pipe_config =
11791 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011792 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011793 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011794 bool mode_changed = needs_modeset(crtc_state);
11795
11796 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11797 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11798 return -EINVAL;
11799 }
11800
11801 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11802 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11803 idx, crtc->state->active, intel_crtc->active);
11804
Ville Syrjälä852eb002015-06-24 22:00:07 +030011805 if (mode_changed && !crtc_state->active)
11806 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011807
Maarten Lankhorstad421372015-06-15 12:33:42 +020011808 if (mode_changed && crtc_state->enable &&
11809 dev_priv->display.crtc_compute_clock &&
11810 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11811 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11812 pipe_config);
11813 if (ret)
11814 return ret;
11815 }
11816
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011817 ret = 0;
11818 if (INTEL_INFO(dev)->gen >= 9) {
11819 if (mode_changed)
11820 ret = skl_update_scaler_crtc(pipe_config);
11821
11822 if (!ret)
11823 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11824 pipe_config);
11825 }
11826
11827 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011828}
11829
Jani Nikula65b38e02015-04-13 11:26:56 +030011830static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011831 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11832 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011833 .atomic_begin = intel_begin_crtc_commit,
11834 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011835 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011836};
11837
Daniel Vetter9a935852012-07-05 22:34:27 +020011838/**
11839 * intel_modeset_update_staged_output_state
11840 *
11841 * Updates the staged output configuration state, e.g. after we've read out the
11842 * current hw state.
11843 */
11844static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11845{
Ville Syrjälä76688512014-01-10 11:28:06 +020011846 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011847 struct intel_encoder *encoder;
11848 struct intel_connector *connector;
11849
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011850 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011851 connector->new_encoder =
11852 to_intel_encoder(connector->base.encoder);
11853 }
11854
Damien Lespiaub2784e12014-08-05 11:29:37 +010011855 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011856 encoder->new_crtc =
11857 to_intel_crtc(encoder->base.crtc);
11858 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011859
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011860 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011861 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011862 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011863}
11864
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011865/* Transitional helper to copy current connector/encoder state to
11866 * connector->state. This is needed so that code that is partially
11867 * converted to atomic does the right thing.
11868 */
11869static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11870{
11871 struct intel_connector *connector;
11872
11873 for_each_intel_connector(dev, connector) {
11874 if (connector->base.encoder) {
11875 connector->base.state->best_encoder =
11876 connector->base.encoder;
11877 connector->base.state->crtc =
11878 connector->base.encoder->crtc;
11879 } else {
11880 connector->base.state->best_encoder = NULL;
11881 connector->base.state->crtc = NULL;
11882 }
11883 }
11884}
11885
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011886static void
Robin Schroereba905b2014-05-18 02:24:50 +020011887connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011888 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011889{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011890 int bpp = pipe_config->pipe_bpp;
11891
11892 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11893 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011894 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011895
11896 /* Don't use an invalid EDID bpc value */
11897 if (connector->base.display_info.bpc &&
11898 connector->base.display_info.bpc * 3 < bpp) {
11899 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11900 bpp, connector->base.display_info.bpc*3);
11901 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11902 }
11903
11904 /* Clamp bpp to 8 on screens without EDID 1.4 */
11905 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11906 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11907 bpp);
11908 pipe_config->pipe_bpp = 24;
11909 }
11910}
11911
11912static int
11913compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011914 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011915{
11916 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011917 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011918 struct drm_connector *connector;
11919 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011920 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011921
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011922 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011923 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011924 else if (INTEL_INFO(dev)->gen >= 5)
11925 bpp = 12*3;
11926 else
11927 bpp = 8*3;
11928
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011929
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011930 pipe_config->pipe_bpp = bpp;
11931
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011932 state = pipe_config->base.state;
11933
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011934 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011935 for_each_connector_in_state(state, connector, connector_state, i) {
11936 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011937 continue;
11938
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011939 connected_sink_compute_bpp(to_intel_connector(connector),
11940 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011941 }
11942
11943 return bpp;
11944}
11945
Daniel Vetter644db712013-09-19 14:53:58 +020011946static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11947{
11948 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11949 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011950 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011951 mode->crtc_hdisplay, mode->crtc_hsync_start,
11952 mode->crtc_hsync_end, mode->crtc_htotal,
11953 mode->crtc_vdisplay, mode->crtc_vsync_start,
11954 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11955}
11956
Daniel Vetterc0b03412013-05-28 12:05:54 +020011957static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011958 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011959 const char *context)
11960{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011961 struct drm_device *dev = crtc->base.dev;
11962 struct drm_plane *plane;
11963 struct intel_plane *intel_plane;
11964 struct intel_plane_state *state;
11965 struct drm_framebuffer *fb;
11966
11967 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11968 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011969
11970 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11971 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11972 pipe_config->pipe_bpp, pipe_config->dither);
11973 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11974 pipe_config->has_pch_encoder,
11975 pipe_config->fdi_lanes,
11976 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11977 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11978 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011979 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11980 pipe_config->has_dp_encoder,
11981 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11982 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11983 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011984
11985 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11986 pipe_config->has_dp_encoder,
11987 pipe_config->dp_m2_n2.gmch_m,
11988 pipe_config->dp_m2_n2.gmch_n,
11989 pipe_config->dp_m2_n2.link_m,
11990 pipe_config->dp_m2_n2.link_n,
11991 pipe_config->dp_m2_n2.tu);
11992
Daniel Vetter55072d12014-11-20 16:10:28 +010011993 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11994 pipe_config->has_audio,
11995 pipe_config->has_infoframe);
11996
Daniel Vetterc0b03412013-05-28 12:05:54 +020011997 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011998 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011999 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012000 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12001 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012002 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012003 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12004 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012005 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12006 crtc->num_scalers,
12007 pipe_config->scaler_state.scaler_users,
12008 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012009 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12010 pipe_config->gmch_pfit.control,
12011 pipe_config->gmch_pfit.pgm_ratios,
12012 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012013 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012014 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012015 pipe_config->pch_pfit.size,
12016 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012017 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012018 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012019
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012020 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012021 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012022 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012023 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012024 pipe_config->ddi_pll_sel,
12025 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012026 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012027 pipe_config->dpll_hw_state.pll0,
12028 pipe_config->dpll_hw_state.pll1,
12029 pipe_config->dpll_hw_state.pll2,
12030 pipe_config->dpll_hw_state.pll3,
12031 pipe_config->dpll_hw_state.pll6,
12032 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012033 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012034 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012035 pipe_config->dpll_hw_state.pcsdw12);
12036 } else if (IS_SKYLAKE(dev)) {
12037 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12038 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12039 pipe_config->ddi_pll_sel,
12040 pipe_config->dpll_hw_state.ctrl1,
12041 pipe_config->dpll_hw_state.cfgcr1,
12042 pipe_config->dpll_hw_state.cfgcr2);
12043 } else if (HAS_DDI(dev)) {
12044 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12045 pipe_config->ddi_pll_sel,
12046 pipe_config->dpll_hw_state.wrpll);
12047 } else {
12048 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12049 "fp0: 0x%x, fp1: 0x%x\n",
12050 pipe_config->dpll_hw_state.dpll,
12051 pipe_config->dpll_hw_state.dpll_md,
12052 pipe_config->dpll_hw_state.fp0,
12053 pipe_config->dpll_hw_state.fp1);
12054 }
12055
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012056 DRM_DEBUG_KMS("planes on this crtc\n");
12057 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12058 intel_plane = to_intel_plane(plane);
12059 if (intel_plane->pipe != crtc->pipe)
12060 continue;
12061
12062 state = to_intel_plane_state(plane->state);
12063 fb = state->base.fb;
12064 if (!fb) {
12065 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12066 "disabled, scaler_id = %d\n",
12067 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12068 plane->base.id, intel_plane->pipe,
12069 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12070 drm_plane_index(plane), state->scaler_id);
12071 continue;
12072 }
12073
12074 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12075 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12076 plane->base.id, intel_plane->pipe,
12077 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12078 drm_plane_index(plane));
12079 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12080 fb->base.id, fb->width, fb->height, fb->pixel_format);
12081 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12082 state->scaler_id,
12083 state->src.x1 >> 16, state->src.y1 >> 16,
12084 drm_rect_width(&state->src) >> 16,
12085 drm_rect_height(&state->src) >> 16,
12086 state->dst.x1, state->dst.y1,
12087 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12088 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012089}
12090
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012091static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012092{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012093 struct drm_device *dev = state->dev;
12094 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012095 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012096 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012097 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012098 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012099
12100 /*
12101 * Walk the connector list instead of the encoder
12102 * list to detect the problem on ddi platforms
12103 * where there's just one encoder per digital port.
12104 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012105 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012106 if (!connector_state->best_encoder)
12107 continue;
12108
12109 encoder = to_intel_encoder(connector_state->best_encoder);
12110
12111 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012112
12113 switch (encoder->type) {
12114 unsigned int port_mask;
12115 case INTEL_OUTPUT_UNKNOWN:
12116 if (WARN_ON(!HAS_DDI(dev)))
12117 break;
12118 case INTEL_OUTPUT_DISPLAYPORT:
12119 case INTEL_OUTPUT_HDMI:
12120 case INTEL_OUTPUT_EDP:
12121 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12122
12123 /* the same port mustn't appear more than once */
12124 if (used_ports & port_mask)
12125 return false;
12126
12127 used_ports |= port_mask;
12128 default:
12129 break;
12130 }
12131 }
12132
12133 return true;
12134}
12135
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012136static void
12137clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12138{
12139 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012140 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012141 struct intel_dpll_hw_state dpll_hw_state;
12142 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012143 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012144
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012145 /* FIXME: before the switch to atomic started, a new pipe_config was
12146 * kzalloc'd. Code that depends on any field being zero should be
12147 * fixed, so that the crtc_state can be safely duplicated. For now,
12148 * only fields that are know to not cause problems are preserved. */
12149
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012150 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012151 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012152 shared_dpll = crtc_state->shared_dpll;
12153 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012154 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012155
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012156 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012157
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012158 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012159 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012160 crtc_state->shared_dpll = shared_dpll;
12161 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012162 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012163}
12164
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012165static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012166intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012167 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012168{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012169 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012170 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012171 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012172 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012173 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012174 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012175 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012176
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012177 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012178
Daniel Vettere143a212013-07-04 12:01:15 +020012179 pipe_config->cpu_transcoder =
12180 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012181
Imre Deak2960bc92013-07-30 13:36:32 +030012182 /*
12183 * Sanitize sync polarity flags based on requested ones. If neither
12184 * positive or negative polarity is requested, treat this as meaning
12185 * negative polarity.
12186 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012187 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012188 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012189 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012190
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012191 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012192 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012193 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012194
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012195 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12196 * plane pixel format and any sink constraints into account. Returns the
12197 * source plane bpp so that dithering can be selected on mismatches
12198 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012199 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12200 pipe_config);
12201 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012202 goto fail;
12203
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012204 /*
12205 * Determine the real pipe dimensions. Note that stereo modes can
12206 * increase the actual pipe size due to the frame doubling and
12207 * insertion of additional space for blanks between the frame. This
12208 * is stored in the crtc timings. We use the requested mode to do this
12209 * computation to clearly distinguish it from the adjusted mode, which
12210 * can be changed by the connectors in the below retry loop.
12211 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012212 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012213 &pipe_config->pipe_src_w,
12214 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012215
Daniel Vettere29c22c2013-02-21 00:00:16 +010012216encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012217 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012218 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012219 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012220
Daniel Vetter135c81b2013-07-21 21:37:09 +020012221 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012222 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12223 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012224
Daniel Vetter7758a112012-07-08 19:40:39 +020012225 /* Pass our mode to the connectors and the CRTC to give them a chance to
12226 * adjust it according to limitations or connector properties, and also
12227 * a chance to reject the mode entirely.
12228 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012229 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012230 if (connector_state->crtc != crtc)
12231 continue;
12232
12233 encoder = to_intel_encoder(connector_state->best_encoder);
12234
Daniel Vetterefea6e82013-07-21 21:36:59 +020012235 if (!(encoder->compute_config(encoder, pipe_config))) {
12236 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012237 goto fail;
12238 }
12239 }
12240
Daniel Vetterff9a6752013-06-01 17:16:21 +020012241 /* Set default port clock if not overwritten by the encoder. Needs to be
12242 * done afterwards in case the encoder adjusts the mode. */
12243 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012244 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012245 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012246
Daniel Vettera43f6e02013-06-07 23:10:32 +020012247 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012248 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012249 DRM_DEBUG_KMS("CRTC fixup failed\n");
12250 goto fail;
12251 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012252
12253 if (ret == RETRY) {
12254 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12255 ret = -EINVAL;
12256 goto fail;
12257 }
12258
12259 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12260 retry = false;
12261 goto encoder_retry;
12262 }
12263
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012264 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012265 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012266 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012267
Daniel Vetter7758a112012-07-08 19:40:39 +020012268fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012269 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012270}
12271
Daniel Vetterea9d7582012-07-10 10:42:52 +020012272static bool intel_crtc_in_use(struct drm_crtc *crtc)
12273{
12274 struct drm_encoder *encoder;
12275 struct drm_device *dev = crtc->dev;
12276
12277 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12278 if (encoder->crtc == crtc)
12279 return true;
12280
12281 return false;
12282}
12283
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012284static void
12285intel_modeset_update_state(struct drm_atomic_state *state)
12286{
12287 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012288 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012289 struct drm_crtc *crtc;
12290 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012291 struct drm_connector *connector;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012292 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012293
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012294 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012295
Damien Lespiaub2784e12014-08-05 11:29:37 +010012296 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012297 if (!intel_encoder->base.crtc)
12298 continue;
12299
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012300 crtc = intel_encoder->base.crtc;
12301 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12302 if (!crtc_state || !needs_modeset(crtc->state))
12303 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012304
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012305 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012306 }
12307
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012308 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorstf7217902015-06-10 10:24:20 +020012309 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012310
Ville Syrjälä76688512014-01-10 11:28:06 +020012311 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012312 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012313 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012314
12315 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012316
12317 /* Update hwmode for vblank functions */
12318 if (crtc->state->active)
12319 crtc->hwmode = crtc->state->adjusted_mode;
12320 else
12321 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012322 }
12323
12324 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12325 if (!connector->encoder || !connector->encoder->crtc)
12326 continue;
12327
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012328 crtc = connector->encoder->crtc;
12329 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12330 if (!crtc_state || !needs_modeset(crtc->state))
12331 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012332
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012333 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012334 struct drm_property *dpms_property =
12335 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012336
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012337 connector->dpms = DRM_MODE_DPMS_ON;
12338 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012339
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012340 intel_encoder = to_intel_encoder(connector->encoder);
12341 intel_encoder->connectors_active = true;
12342 } else
12343 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012344 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012345}
12346
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012347static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012348{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012349 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012350
12351 if (clock1 == clock2)
12352 return true;
12353
12354 if (!clock1 || !clock2)
12355 return false;
12356
12357 diff = abs(clock1 - clock2);
12358
12359 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12360 return true;
12361
12362 return false;
12363}
12364
Daniel Vetter25c5b262012-07-08 22:08:04 +020012365#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12366 list_for_each_entry((intel_crtc), \
12367 &(dev)->mode_config.crtc_list, \
12368 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012369 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012370
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012371
12372static bool
12373intel_compare_m_n(unsigned int m, unsigned int n,
12374 unsigned int m2, unsigned int n2,
12375 bool exact)
12376{
12377 if (m == m2 && n == n2)
12378 return true;
12379
12380 if (exact || !m || !n || !m2 || !n2)
12381 return false;
12382
12383 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12384
12385 if (m > m2) {
12386 while (m > m2) {
12387 m2 <<= 1;
12388 n2 <<= 1;
12389 }
12390 } else if (m < m2) {
12391 while (m < m2) {
12392 m <<= 1;
12393 n <<= 1;
12394 }
12395 }
12396
12397 return m == m2 && n == n2;
12398}
12399
12400static bool
12401intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12402 struct intel_link_m_n *m2_n2,
12403 bool adjust)
12404{
12405 if (m_n->tu == m2_n2->tu &&
12406 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12407 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12408 intel_compare_m_n(m_n->link_m, m_n->link_n,
12409 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12410 if (adjust)
12411 *m2_n2 = *m_n;
12412
12413 return true;
12414 }
12415
12416 return false;
12417}
12418
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012419static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012420intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012421 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012422 struct intel_crtc_state *pipe_config,
12423 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012424{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012425 bool ret = true;
12426
12427#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12428 do { \
12429 if (!adjust) \
12430 DRM_ERROR(fmt, ##__VA_ARGS__); \
12431 else \
12432 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12433 } while (0)
12434
Daniel Vetter66e985c2013-06-05 13:34:20 +020012435#define PIPE_CONF_CHECK_X(name) \
12436 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012437 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012438 "(expected 0x%08x, found 0x%08x)\n", \
12439 current_config->name, \
12440 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012441 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012442 }
12443
Daniel Vetter08a24032013-04-19 11:25:34 +020012444#define PIPE_CONF_CHECK_I(name) \
12445 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012446 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012447 "(expected %i, found %i)\n", \
12448 current_config->name, \
12449 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012450 ret = false; \
12451 }
12452
12453#define PIPE_CONF_CHECK_M_N(name) \
12454 if (!intel_compare_link_m_n(&current_config->name, \
12455 &pipe_config->name,\
12456 adjust)) { \
12457 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12458 "(expected tu %i gmch %i/%i link %i/%i, " \
12459 "found tu %i, gmch %i/%i link %i/%i)\n", \
12460 current_config->name.tu, \
12461 current_config->name.gmch_m, \
12462 current_config->name.gmch_n, \
12463 current_config->name.link_m, \
12464 current_config->name.link_n, \
12465 pipe_config->name.tu, \
12466 pipe_config->name.gmch_m, \
12467 pipe_config->name.gmch_n, \
12468 pipe_config->name.link_m, \
12469 pipe_config->name.link_n); \
12470 ret = false; \
12471 }
12472
12473#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12474 if (!intel_compare_link_m_n(&current_config->name, \
12475 &pipe_config->name, adjust) && \
12476 !intel_compare_link_m_n(&current_config->alt_name, \
12477 &pipe_config->name, adjust)) { \
12478 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12479 "(expected tu %i gmch %i/%i link %i/%i, " \
12480 "or tu %i gmch %i/%i link %i/%i, " \
12481 "found tu %i, gmch %i/%i link %i/%i)\n", \
12482 current_config->name.tu, \
12483 current_config->name.gmch_m, \
12484 current_config->name.gmch_n, \
12485 current_config->name.link_m, \
12486 current_config->name.link_n, \
12487 current_config->alt_name.tu, \
12488 current_config->alt_name.gmch_m, \
12489 current_config->alt_name.gmch_n, \
12490 current_config->alt_name.link_m, \
12491 current_config->alt_name.link_n, \
12492 pipe_config->name.tu, \
12493 pipe_config->name.gmch_m, \
12494 pipe_config->name.gmch_n, \
12495 pipe_config->name.link_m, \
12496 pipe_config->name.link_n); \
12497 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012498 }
12499
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012500/* This is required for BDW+ where there is only one set of registers for
12501 * switching between high and low RR.
12502 * This macro can be used whenever a comparison has to be made between one
12503 * hw state and multiple sw state variables.
12504 */
12505#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12506 if ((current_config->name != pipe_config->name) && \
12507 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012508 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012509 "(expected %i or %i, found %i)\n", \
12510 current_config->name, \
12511 current_config->alt_name, \
12512 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012513 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012514 }
12515
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012516#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12517 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012518 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012519 "(expected %i, found %i)\n", \
12520 current_config->name & (mask), \
12521 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012522 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012523 }
12524
Ville Syrjälä5e550652013-09-06 23:29:07 +030012525#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12526 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012527 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012528 "(expected %i, found %i)\n", \
12529 current_config->name, \
12530 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012531 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012532 }
12533
Daniel Vetterbb760062013-06-06 14:55:52 +020012534#define PIPE_CONF_QUIRK(quirk) \
12535 ((current_config->quirks | pipe_config->quirks) & (quirk))
12536
Daniel Vettereccb1402013-05-22 00:50:22 +020012537 PIPE_CONF_CHECK_I(cpu_transcoder);
12538
Daniel Vetter08a24032013-04-19 11:25:34 +020012539 PIPE_CONF_CHECK_I(has_pch_encoder);
12540 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012541 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012542
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012543 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012544
12545 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012546 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012547
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012548 PIPE_CONF_CHECK_I(has_drrs);
12549 if (current_config->has_drrs)
12550 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12551 } else
12552 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012553
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012554 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12555 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12556 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12557 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12558 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12559 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012560
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012561 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12562 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12563 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12564 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12565 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12566 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012567
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012568 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012569 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012570 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12571 IS_VALLEYVIEW(dev))
12572 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012573 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012574
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012575 PIPE_CONF_CHECK_I(has_audio);
12576
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012577 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012578 DRM_MODE_FLAG_INTERLACE);
12579
Daniel Vetterbb760062013-06-06 14:55:52 +020012580 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012581 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012582 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012583 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012584 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012585 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012586 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012587 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012588 DRM_MODE_FLAG_NVSYNC);
12589 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012590
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012591 PIPE_CONF_CHECK_I(pipe_src_w);
12592 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012593
Daniel Vetter99535992014-04-13 12:00:33 +020012594 /*
12595 * FIXME: BIOS likes to set up a cloned config with lvds+external
12596 * screen. Since we don't yet re-compute the pipe config when moving
12597 * just the lvds port away to another pipe the sw tracking won't match.
12598 *
12599 * Proper atomic modesets with recomputed global state will fix this.
12600 * Until then just don't check gmch state for inherited modes.
12601 */
12602 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12603 PIPE_CONF_CHECK_I(gmch_pfit.control);
12604 /* pfit ratios are autocomputed by the hw on gen4+ */
12605 if (INTEL_INFO(dev)->gen < 4)
12606 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12607 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12608 }
12609
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012610 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12611 if (current_config->pch_pfit.enabled) {
12612 PIPE_CONF_CHECK_I(pch_pfit.pos);
12613 PIPE_CONF_CHECK_I(pch_pfit.size);
12614 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012615
Chandra Kondurua1b22782015-04-07 15:28:45 -070012616 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12617
Jesse Barnese59150d2014-01-07 13:30:45 -080012618 /* BDW+ don't expose a synchronous way to read the state */
12619 if (IS_HASWELL(dev))
12620 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012621
Ville Syrjälä282740f2013-09-04 18:30:03 +030012622 PIPE_CONF_CHECK_I(double_wide);
12623
Daniel Vetter26804af2014-06-25 22:01:55 +030012624 PIPE_CONF_CHECK_X(ddi_pll_sel);
12625
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012626 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012627 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012628 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012629 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12630 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012631 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012632 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12633 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12634 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012635
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012636 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12637 PIPE_CONF_CHECK_I(pipe_bpp);
12638
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012639 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012640 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012641
Daniel Vetter66e985c2013-06-05 13:34:20 +020012642#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012643#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012644#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012645#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012646#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012647#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012648#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012649
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012650 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012651}
12652
Damien Lespiau08db6652014-11-04 17:06:52 +000012653static void check_wm_state(struct drm_device *dev)
12654{
12655 struct drm_i915_private *dev_priv = dev->dev_private;
12656 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12657 struct intel_crtc *intel_crtc;
12658 int plane;
12659
12660 if (INTEL_INFO(dev)->gen < 9)
12661 return;
12662
12663 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12664 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12665
12666 for_each_intel_crtc(dev, intel_crtc) {
12667 struct skl_ddb_entry *hw_entry, *sw_entry;
12668 const enum pipe pipe = intel_crtc->pipe;
12669
12670 if (!intel_crtc->active)
12671 continue;
12672
12673 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012674 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012675 hw_entry = &hw_ddb.plane[pipe][plane];
12676 sw_entry = &sw_ddb->plane[pipe][plane];
12677
12678 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12679 continue;
12680
12681 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12682 "(expected (%u,%u), found (%u,%u))\n",
12683 pipe_name(pipe), plane + 1,
12684 sw_entry->start, sw_entry->end,
12685 hw_entry->start, hw_entry->end);
12686 }
12687
12688 /* cursor */
12689 hw_entry = &hw_ddb.cursor[pipe];
12690 sw_entry = &sw_ddb->cursor[pipe];
12691
12692 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12693 continue;
12694
12695 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12696 "(expected (%u,%u), found (%u,%u))\n",
12697 pipe_name(pipe),
12698 sw_entry->start, sw_entry->end,
12699 hw_entry->start, hw_entry->end);
12700 }
12701}
12702
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012703static void
12704check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012705{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012706 struct intel_connector *connector;
12707
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012708 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012709 /* This also checks the encoder/connector hw state with the
12710 * ->get_hw_state callbacks. */
12711 intel_connector_check_state(connector);
12712
Rob Clarke2c719b2014-12-15 13:56:32 -050012713 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012714 "connector's staged encoder doesn't match current encoder\n");
12715 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012716}
12717
12718static void
12719check_encoder_state(struct drm_device *dev)
12720{
12721 struct intel_encoder *encoder;
12722 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012723
Damien Lespiaub2784e12014-08-05 11:29:37 +010012724 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012725 bool enabled = false;
12726 bool active = false;
12727 enum pipe pipe, tracked_pipe;
12728
12729 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12730 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012731 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012732
Rob Clarke2c719b2014-12-15 13:56:32 -050012733 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012734 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012735 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012736 "encoder's active_connectors set, but no crtc\n");
12737
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012738 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012739 if (connector->base.encoder != &encoder->base)
12740 continue;
12741 enabled = true;
12742 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12743 active = true;
12744 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012745 /*
12746 * for MST connectors if we unplug the connector is gone
12747 * away but the encoder is still connected to a crtc
12748 * until a modeset happens in response to the hotplug.
12749 */
12750 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12751 continue;
12752
Rob Clarke2c719b2014-12-15 13:56:32 -050012753 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012754 "encoder's enabled state mismatch "
12755 "(expected %i, found %i)\n",
12756 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012757 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012758 "active encoder with no crtc\n");
12759
Rob Clarke2c719b2014-12-15 13:56:32 -050012760 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012761 "encoder's computed active state doesn't match tracked active state "
12762 "(expected %i, found %i)\n", active, encoder->connectors_active);
12763
12764 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012765 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012766 "encoder's hw state doesn't match sw tracking "
12767 "(expected %i, found %i)\n",
12768 encoder->connectors_active, active);
12769
12770 if (!encoder->base.crtc)
12771 continue;
12772
12773 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012774 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012775 "active encoder's pipe doesn't match"
12776 "(expected %i, found %i)\n",
12777 tracked_pipe, pipe);
12778
12779 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012780}
12781
12782static void
12783check_crtc_state(struct drm_device *dev)
12784{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012785 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012786 struct intel_crtc *crtc;
12787 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012788 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012789
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012790 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012791 bool enabled = false;
12792 bool active = false;
12793
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012794 memset(&pipe_config, 0, sizeof(pipe_config));
12795
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012796 DRM_DEBUG_KMS("[CRTC:%d]\n",
12797 crtc->base.base.id);
12798
Matt Roper83d65732015-02-25 13:12:16 -080012799 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012800 "active crtc, but not enabled in sw tracking\n");
12801
Damien Lespiaub2784e12014-08-05 11:29:37 +010012802 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012803 if (encoder->base.crtc != &crtc->base)
12804 continue;
12805 enabled = true;
12806 if (encoder->connectors_active)
12807 active = true;
12808 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012809
Rob Clarke2c719b2014-12-15 13:56:32 -050012810 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012811 "crtc's computed active state doesn't match tracked active state "
12812 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012813 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012814 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012815 "(expected %i, found %i)\n", enabled,
12816 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012817
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012818 active = dev_priv->display.get_pipe_config(crtc,
12819 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012820
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012821 /* hw state is inconsistent with the pipe quirk */
12822 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12823 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012824 active = crtc->active;
12825
Damien Lespiaub2784e12014-08-05 11:29:37 +010012826 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012827 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012828 if (encoder->base.crtc != &crtc->base)
12829 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012830 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012831 encoder->get_config(encoder, &pipe_config);
12832 }
12833
Rob Clarke2c719b2014-12-15 13:56:32 -050012834 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012835 "crtc active state doesn't match with hw state "
12836 "(expected %i, found %i)\n", crtc->active, active);
12837
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012838 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12839 "transitional active state does not match atomic hw state "
12840 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12841
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012842 if (!active)
12843 continue;
12844
12845 if (!intel_pipe_config_compare(dev, crtc->config,
12846 &pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012847 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012848 intel_dump_pipe_config(crtc, &pipe_config,
12849 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012850 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012851 "[sw state]");
12852 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012853 }
12854}
12855
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012856static void
12857check_shared_dpll_state(struct drm_device *dev)
12858{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012859 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012860 struct intel_crtc *crtc;
12861 struct intel_dpll_hw_state dpll_hw_state;
12862 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012863
12864 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12865 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12866 int enabled_crtcs = 0, active_crtcs = 0;
12867 bool active;
12868
12869 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12870
12871 DRM_DEBUG_KMS("%s\n", pll->name);
12872
12873 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12874
Rob Clarke2c719b2014-12-15 13:56:32 -050012875 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012876 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012877 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012878 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012879 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012880 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012881 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012882 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012883 "pll on state mismatch (expected %i, found %i)\n",
12884 pll->on, active);
12885
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012886 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012887 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012888 enabled_crtcs++;
12889 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12890 active_crtcs++;
12891 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012892 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012893 "pll active crtcs mismatch (expected %i, found %i)\n",
12894 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012895 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012896 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012897 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012898
Rob Clarke2c719b2014-12-15 13:56:32 -050012899 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012900 sizeof(dpll_hw_state)),
12901 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012902 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012903}
12904
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012905void
12906intel_modeset_check_state(struct drm_device *dev)
12907{
Damien Lespiau08db6652014-11-04 17:06:52 +000012908 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012909 check_connector_state(dev);
12910 check_encoder_state(dev);
12911 check_crtc_state(dev);
12912 check_shared_dpll_state(dev);
12913}
12914
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012915void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012916 int dotclock)
12917{
12918 /*
12919 * FDI already provided one idea for the dotclock.
12920 * Yell if the encoder disagrees.
12921 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012922 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012923 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012924 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012925}
12926
Ville Syrjälä80715b22014-05-15 20:23:23 +030012927static void update_scanline_offset(struct intel_crtc *crtc)
12928{
12929 struct drm_device *dev = crtc->base.dev;
12930
12931 /*
12932 * The scanline counter increments at the leading edge of hsync.
12933 *
12934 * On most platforms it starts counting from vtotal-1 on the
12935 * first active line. That means the scanline counter value is
12936 * always one less than what we would expect. Ie. just after
12937 * start of vblank, which also occurs at start of hsync (on the
12938 * last active line), the scanline counter will read vblank_start-1.
12939 *
12940 * On gen2 the scanline counter starts counting from 1 instead
12941 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12942 * to keep the value positive), instead of adding one.
12943 *
12944 * On HSW+ the behaviour of the scanline counter depends on the output
12945 * type. For DP ports it behaves like most other platforms, but on HDMI
12946 * there's an extra 1 line difference. So we need to add two instead of
12947 * one to the value.
12948 */
12949 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012950 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012951 int vtotal;
12952
12953 vtotal = mode->crtc_vtotal;
12954 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12955 vtotal /= 2;
12956
12957 crtc->scanline_offset = vtotal - 1;
12958 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012959 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012960 crtc->scanline_offset = 2;
12961 } else
12962 crtc->scanline_offset = 1;
12963}
12964
Maarten Lankhorstad421372015-06-15 12:33:42 +020012965static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012966{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012967 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012968 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012969 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012970 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012971 struct intel_crtc_state *intel_crtc_state;
12972 struct drm_crtc *crtc;
12973 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012974 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012975
12976 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012977 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012978
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012979 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012980 int dpll;
12981
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012982 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012983 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012984 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012985
Maarten Lankhorstad421372015-06-15 12:33:42 +020012986 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012987 continue;
12988
Maarten Lankhorstad421372015-06-15 12:33:42 +020012989 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012990
Maarten Lankhorstad421372015-06-15 12:33:42 +020012991 if (!shared_dpll)
12992 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12993
12994 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012995 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012996}
12997
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012998/*
12999 * This implements the workaround described in the "notes" section of the mode
13000 * set sequence documentation. When going from no pipes or single pipe to
13001 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13002 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13003 */
13004static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13005{
13006 struct drm_crtc_state *crtc_state;
13007 struct intel_crtc *intel_crtc;
13008 struct drm_crtc *crtc;
13009 struct intel_crtc_state *first_crtc_state = NULL;
13010 struct intel_crtc_state *other_crtc_state = NULL;
13011 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13012 int i;
13013
13014 /* look at all crtc's that are going to be enabled in during modeset */
13015 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13016 intel_crtc = to_intel_crtc(crtc);
13017
13018 if (!crtc_state->active || !needs_modeset(crtc_state))
13019 continue;
13020
13021 if (first_crtc_state) {
13022 other_crtc_state = to_intel_crtc_state(crtc_state);
13023 break;
13024 } else {
13025 first_crtc_state = to_intel_crtc_state(crtc_state);
13026 first_pipe = intel_crtc->pipe;
13027 }
13028 }
13029
13030 /* No workaround needed? */
13031 if (!first_crtc_state)
13032 return 0;
13033
13034 /* w/a possibly needed, check how many crtc's are already enabled. */
13035 for_each_intel_crtc(state->dev, intel_crtc) {
13036 struct intel_crtc_state *pipe_config;
13037
13038 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13039 if (IS_ERR(pipe_config))
13040 return PTR_ERR(pipe_config);
13041
13042 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13043
13044 if (!pipe_config->base.active ||
13045 needs_modeset(&pipe_config->base))
13046 continue;
13047
13048 /* 2 or more enabled crtcs means no need for w/a */
13049 if (enabled_pipe != INVALID_PIPE)
13050 return 0;
13051
13052 enabled_pipe = intel_crtc->pipe;
13053 }
13054
13055 if (enabled_pipe != INVALID_PIPE)
13056 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13057 else if (other_crtc_state)
13058 other_crtc_state->hsw_workaround_pipe = first_pipe;
13059
13060 return 0;
13061}
13062
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013063static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13064{
13065 struct drm_crtc *crtc;
13066 struct drm_crtc_state *crtc_state;
13067 int ret = 0;
13068
13069 /* add all active pipes to the state */
13070 for_each_crtc(state->dev, crtc) {
13071 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13072 if (IS_ERR(crtc_state))
13073 return PTR_ERR(crtc_state);
13074
13075 if (!crtc_state->active || needs_modeset(crtc_state))
13076 continue;
13077
13078 crtc_state->mode_changed = true;
13079
13080 ret = drm_atomic_add_affected_connectors(state, crtc);
13081 if (ret)
13082 break;
13083
13084 ret = drm_atomic_add_affected_planes(state, crtc);
13085 if (ret)
13086 break;
13087 }
13088
13089 return ret;
13090}
13091
13092
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013093/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013094static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013095{
13096 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013097 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013098 int ret;
13099
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013100 if (!check_digital_port_conflicts(state)) {
13101 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13102 return -EINVAL;
13103 }
13104
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013105 /*
13106 * See if the config requires any additional preparation, e.g.
13107 * to adjust global state with pipes off. We need to do this
13108 * here so we can get the modeset_pipe updated config for the new
13109 * mode set on this crtc. For other crtcs we need to use the
13110 * adjusted_mode bits in the crtc directly.
13111 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013112 if (dev_priv->display.modeset_calc_cdclk) {
13113 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030013114
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013115 ret = dev_priv->display.modeset_calc_cdclk(state);
13116
13117 cdclk = to_intel_atomic_state(state)->cdclk;
13118 if (!ret && cdclk != dev_priv->cdclk_freq)
13119 ret = intel_modeset_all_pipes(state);
13120
13121 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013122 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013123 } else
13124 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013125
Maarten Lankhorstad421372015-06-15 12:33:42 +020013126 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013127
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013128 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013129 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013130
Maarten Lankhorstad421372015-06-15 12:33:42 +020013131 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013132}
13133
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013134static int
13135intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013136{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013137 struct drm_crtc *crtc;
13138 struct drm_crtc_state *crtc_state;
13139 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013140 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013141
13142 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013143 if (ret)
13144 return ret;
13145
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013146 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013147 struct intel_crtc_state *pipe_config =
13148 to_intel_crtc_state(crtc_state);
13149 bool modeset, recalc;
13150
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013151 if (!crtc_state->enable) {
13152 if (needs_modeset(crtc_state))
13153 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013154 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013155 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013156
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013157 modeset = needs_modeset(crtc_state);
13158 recalc = pipe_config->quirks & PIPE_CONFIG_QUIRK_INHERITED_MODE;
13159
13160 if (!modeset && !recalc)
13161 continue;
13162
13163 if (recalc) {
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013164 ret = drm_atomic_add_affected_connectors(state, crtc);
13165 if (ret)
13166 return ret;
13167 }
13168
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013169 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013170 if (ret)
13171 return ret;
13172
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013173 if (recalc && !intel_pipe_config_compare(state->dev,
13174 to_intel_crtc_state(crtc->state),
13175 pipe_config, true)) {
13176 modeset = crtc_state->mode_changed = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013177
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013178 ret = drm_atomic_add_affected_planes(state, crtc);
13179 if (ret)
13180 return ret;
13181 }
13182
13183 any_ms = modeset;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013184 intel_dump_pipe_config(to_intel_crtc(crtc),
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013185 pipe_config,
13186 modeset ? "[modeset]" : "[fastboot]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013187 }
13188
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013189 if (any_ms) {
13190 ret = intel_modeset_checks(state);
13191
13192 if (ret)
13193 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013194 } else
13195 to_intel_atomic_state(state)->cdclk =
13196 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013197
13198 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013199}
13200
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013201static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013202{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013203 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013204 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013205 struct drm_crtc *crtc;
13206 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013207 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013208 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013209 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013210
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013211 ret = drm_atomic_helper_prepare_planes(dev, state);
13212 if (ret)
13213 return ret;
13214
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013215 drm_atomic_helper_swap_state(dev, state);
13216
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013217 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013218 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13219
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013220 if (!needs_modeset(crtc->state))
13221 continue;
13222
13223 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013224 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013225
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013226 if (crtc_state->active) {
13227 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13228 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013229 intel_crtc->active = false;
13230 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013231 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013232 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013233
Daniel Vetterea9d7582012-07-10 10:42:52 +020013234 /* Only after disabling all output pipelines that will be changed can we
13235 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013236 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013237
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013238 /* The state has been swaped above, so state actually contains the
13239 * old state now. */
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013240 if (any_ms)
13241 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013242
Daniel Vettera6778b32012-07-02 09:56:42 +020013243 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013244 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013245 if (needs_modeset(crtc->state) && crtc->state->active) {
13246 update_scanline_offset(to_intel_crtc(crtc));
13247 dev_priv->display.crtc_enable(crtc);
13248 }
13249
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013250 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013251 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013252
Daniel Vettera6778b32012-07-02 09:56:42 +020013253 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013254
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013255 drm_atomic_helper_cleanup_planes(dev, state);
13256
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013257 drm_atomic_state_free(state);
13258
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013259 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013260}
13261
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013262static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013263{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013264 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013265 int ret;
13266
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013267 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013268 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013269 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013270
13271 return ret;
13272}
13273
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013274static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013275{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013276 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013277
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013278 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013279 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013280 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013281
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013282 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013283}
13284
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013285void intel_crtc_restore_mode(struct drm_crtc *crtc)
13286{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013287 struct drm_device *dev = crtc->dev;
13288 struct drm_atomic_state *state;
13289 struct intel_encoder *encoder;
13290 struct intel_connector *connector;
13291 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013292 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013293 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013294
13295 state = drm_atomic_state_alloc(dev);
13296 if (!state) {
13297 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13298 crtc->base.id);
13299 return;
13300 }
13301
13302 state->acquire_ctx = dev->mode_config.acquire_ctx;
13303
13304 /* The force restore path in the HW readout code relies on the staged
13305 * config still keeping the user requested config while the actual
13306 * state has been overwritten by the configuration read from HW. We
13307 * need to copy the staged config to the atomic state, otherwise the
13308 * mode set will just reapply the state the HW is already in. */
13309 for_each_intel_encoder(dev, encoder) {
13310 if (&encoder->new_crtc->base != crtc)
13311 continue;
13312
13313 for_each_intel_connector(dev, connector) {
13314 if (connector->new_encoder != encoder)
13315 continue;
13316
13317 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13318 if (IS_ERR(connector_state)) {
13319 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13320 connector->base.base.id,
13321 connector->base.name,
13322 PTR_ERR(connector_state));
13323 continue;
13324 }
13325
13326 connector_state->crtc = crtc;
13327 connector_state->best_encoder = &encoder->base;
13328 }
13329 }
13330
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013331 crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
13332 if (IS_ERR(crtc_state)) {
13333 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13334 crtc->base.id, PTR_ERR(crtc_state));
13335 drm_atomic_state_free(state);
13336 return;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013337 }
13338
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013339 crtc_state->base.active = crtc_state->base.enable =
13340 to_intel_crtc(crtc)->new_enabled;
13341
13342 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
13343
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013344 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13345 crtc->primary->fb, crtc->x, crtc->y);
13346
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013347 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013348 if (ret)
13349 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013350}
13351
Daniel Vetter25c5b262012-07-08 22:08:04 +020013352#undef for_each_intel_crtc_masked
13353
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013354static bool intel_connector_in_mode_set(struct intel_connector *connector,
13355 struct drm_mode_set *set)
13356{
13357 int ro;
13358
13359 for (ro = 0; ro < set->num_connectors; ro++)
13360 if (set->connectors[ro] == &connector->base)
13361 return true;
13362
13363 return false;
13364}
13365
Daniel Vetter2e431052012-07-04 22:42:15 +020013366static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013367intel_modeset_stage_output_state(struct drm_device *dev,
13368 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013369 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013370{
Daniel Vetter9a935852012-07-05 22:34:27 +020013371 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013372 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013373 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013374 struct drm_crtc *crtc;
13375 struct drm_crtc_state *crtc_state;
13376 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013377
Damien Lespiau9abdda72013-02-13 13:29:23 +000013378 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013379 * of connectors. For paranoia, double-check this. */
13380 WARN_ON(!set->fb && (set->num_connectors != 0));
13381 WARN_ON(set->fb && (set->num_connectors == 0));
13382
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013383 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013384 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13385
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013386 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13387 continue;
13388
13389 connector_state =
13390 drm_atomic_get_connector_state(state, &connector->base);
13391 if (IS_ERR(connector_state))
13392 return PTR_ERR(connector_state);
13393
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013394 if (in_mode_set) {
13395 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013396 connector_state->best_encoder =
13397 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013398 }
13399
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013400 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013401 continue;
13402
Daniel Vetter9a935852012-07-05 22:34:27 +020013403 /* If we disable the crtc, disable all its connectors. Also, if
13404 * the connector is on the changing crtc but not on the new
13405 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013406 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013407 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013408
13409 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13410 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013411 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013412 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013413 }
13414 /* connector->new_encoder is now updated for all connectors. */
13415
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013416 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13417 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013418
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013419 if (!connector_state->best_encoder) {
13420 ret = drm_atomic_set_crtc_for_connector(connector_state,
13421 NULL);
13422 if (ret)
13423 return ret;
13424
Daniel Vetter50f56112012-07-02 09:35:43 +020013425 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013426 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013427
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013428 if (intel_connector_in_mode_set(connector, set)) {
13429 struct drm_crtc *crtc = connector->base.state->crtc;
13430
13431 /* If this connector was in a previous crtc, add it
13432 * to the state. We might need to disable it. */
13433 if (crtc) {
13434 crtc_state =
13435 drm_atomic_get_crtc_state(state, crtc);
13436 if (IS_ERR(crtc_state))
13437 return PTR_ERR(crtc_state);
13438 }
13439
13440 ret = drm_atomic_set_crtc_for_connector(connector_state,
13441 set->crtc);
13442 if (ret)
13443 return ret;
13444 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013445
13446 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013447 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13448 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013449 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013450 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013451
Daniel Vetter9a935852012-07-05 22:34:27 +020013452 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13453 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013454 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013455 connector_state->crtc->base.id);
13456
13457 if (connector_state->best_encoder != &connector->encoder->base)
13458 connector->encoder =
13459 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013460 }
13461
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013462 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013463 bool has_connectors;
13464
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013465 ret = drm_atomic_add_affected_connectors(state, crtc);
13466 if (ret)
13467 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013468
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013469 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13470 if (has_connectors != crtc_state->enable)
13471 crtc_state->enable =
13472 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013473 }
13474
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013475 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13476 set->fb, set->x, set->y);
13477 if (ret)
13478 return ret;
13479
13480 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13481 if (IS_ERR(crtc_state))
13482 return PTR_ERR(crtc_state);
13483
Matt Roperce522992015-06-05 15:08:24 -070013484 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13485 if (ret)
13486 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013487
13488 if (set->num_connectors)
13489 crtc_state->active = true;
13490
Daniel Vetter2e431052012-07-04 22:42:15 +020013491 return 0;
13492}
13493
13494static int intel_crtc_set_config(struct drm_mode_set *set)
13495{
13496 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013497 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013498 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013499
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013500 BUG_ON(!set);
13501 BUG_ON(!set->crtc);
13502 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013503
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013504 /* Enforce sane interface api - has been abused by the fb helper. */
13505 BUG_ON(!set->mode && set->fb);
13506 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013507
Daniel Vetter2e431052012-07-04 22:42:15 +020013508 if (set->fb) {
13509 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13510 set->crtc->base.id, set->fb->base.id,
13511 (int)set->num_connectors, set->x, set->y);
13512 } else {
13513 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013514 }
13515
13516 dev = set->crtc->dev;
13517
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013518 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013519 if (!state)
13520 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013521
13522 state->acquire_ctx = dev->mode_config.acquire_ctx;
13523
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013524 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013525 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013526 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013527
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013528 ret = intel_modeset_compute_config(state);
13529 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013530 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013531
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013532 intel_update_pipe_size(to_intel_crtc(set->crtc));
13533
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013534 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013535 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013536 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13537 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013538 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013539
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013540out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013541 if (ret)
13542 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013543 return ret;
13544}
13545
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013546static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013547 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013548 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013549 .destroy = intel_crtc_destroy,
13550 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013551 .atomic_duplicate_state = intel_crtc_duplicate_state,
13552 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013553};
13554
Daniel Vetter53589012013-06-05 13:34:16 +020013555static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13556 struct intel_shared_dpll *pll,
13557 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013558{
Daniel Vetter53589012013-06-05 13:34:16 +020013559 uint32_t val;
13560
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013561 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013562 return false;
13563
Daniel Vetter53589012013-06-05 13:34:16 +020013564 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013565 hw_state->dpll = val;
13566 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13567 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013568
13569 return val & DPLL_VCO_ENABLE;
13570}
13571
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013572static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13573 struct intel_shared_dpll *pll)
13574{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013575 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13576 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013577}
13578
Daniel Vettere7b903d2013-06-05 13:34:14 +020013579static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13580 struct intel_shared_dpll *pll)
13581{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013582 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013583 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013584
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013585 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013586
13587 /* Wait for the clocks to stabilize. */
13588 POSTING_READ(PCH_DPLL(pll->id));
13589 udelay(150);
13590
13591 /* The pixel multiplier can only be updated once the
13592 * DPLL is enabled and the clocks are stable.
13593 *
13594 * So write it again.
13595 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013596 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013597 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013598 udelay(200);
13599}
13600
13601static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13602 struct intel_shared_dpll *pll)
13603{
13604 struct drm_device *dev = dev_priv->dev;
13605 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013606
13607 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013608 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013609 if (intel_crtc_to_shared_dpll(crtc) == pll)
13610 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13611 }
13612
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013613 I915_WRITE(PCH_DPLL(pll->id), 0);
13614 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013615 udelay(200);
13616}
13617
Daniel Vetter46edb022013-06-05 13:34:12 +020013618static char *ibx_pch_dpll_names[] = {
13619 "PCH DPLL A",
13620 "PCH DPLL B",
13621};
13622
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013623static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013624{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013625 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013626 int i;
13627
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013628 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013629
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013630 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013631 dev_priv->shared_dplls[i].id = i;
13632 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013633 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013634 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13635 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013636 dev_priv->shared_dplls[i].get_hw_state =
13637 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013638 }
13639}
13640
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013641static void intel_shared_dpll_init(struct drm_device *dev)
13642{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013643 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013644
Ville Syrjäläb6283052015-06-03 15:45:07 +030013645 intel_update_cdclk(dev);
13646
Daniel Vetter9cd86932014-06-25 22:01:57 +030013647 if (HAS_DDI(dev))
13648 intel_ddi_pll_init(dev);
13649 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013650 ibx_pch_dpll_init(dev);
13651 else
13652 dev_priv->num_shared_dpll = 0;
13653
13654 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013655}
13656
Matt Roper6beb8c232014-12-01 15:40:14 -080013657/**
13658 * intel_prepare_plane_fb - Prepare fb for usage on plane
13659 * @plane: drm plane to prepare for
13660 * @fb: framebuffer to prepare for presentation
13661 *
13662 * Prepares a framebuffer for usage on a display plane. Generally this
13663 * involves pinning the underlying object and updating the frontbuffer tracking
13664 * bits. Some older platforms need special physical address handling for
13665 * cursor planes.
13666 *
13667 * Returns 0 on success, negative error code on failure.
13668 */
13669int
13670intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013671 struct drm_framebuffer *fb,
13672 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013673{
13674 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013675 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013676 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13677 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013678 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013679
Matt Roperea2c67b2014-12-23 10:41:52 -080013680 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013681 return 0;
13682
Matt Roper4c345742014-07-09 16:22:10 -070013683 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013684
Matt Roper6beb8c232014-12-01 15:40:14 -080013685 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13686 INTEL_INFO(dev)->cursor_needs_physical) {
13687 int align = IS_I830(dev) ? 16 * 1024 : 256;
13688 ret = i915_gem_object_attach_phys(obj, align);
13689 if (ret)
13690 DRM_DEBUG_KMS("failed to attach phys object\n");
13691 } else {
John Harrison91af1272015-06-18 13:14:56 +010013692 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013693 }
13694
13695 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013696 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013697
13698 mutex_unlock(&dev->struct_mutex);
13699
13700 return ret;
13701}
13702
Matt Roper38f3ce32014-12-02 07:45:25 -080013703/**
13704 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13705 * @plane: drm plane to clean up for
13706 * @fb: old framebuffer that was on plane
13707 *
13708 * Cleans up a framebuffer that has just been removed from a plane.
13709 */
13710void
13711intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013712 struct drm_framebuffer *fb,
13713 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013714{
13715 struct drm_device *dev = plane->dev;
13716 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13717
13718 if (WARN_ON(!obj))
13719 return;
13720
13721 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13722 !INTEL_INFO(dev)->cursor_needs_physical) {
13723 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013724 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013725 mutex_unlock(&dev->struct_mutex);
13726 }
Matt Roper465c1202014-05-29 08:06:54 -070013727}
13728
Chandra Konduru6156a452015-04-27 13:48:39 -070013729int
13730skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13731{
13732 int max_scale;
13733 struct drm_device *dev;
13734 struct drm_i915_private *dev_priv;
13735 int crtc_clock, cdclk;
13736
13737 if (!intel_crtc || !crtc_state)
13738 return DRM_PLANE_HELPER_NO_SCALING;
13739
13740 dev = intel_crtc->base.dev;
13741 dev_priv = dev->dev_private;
13742 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013743 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013744
13745 if (!crtc_clock || !cdclk)
13746 return DRM_PLANE_HELPER_NO_SCALING;
13747
13748 /*
13749 * skl max scale is lower of:
13750 * close to 3 but not 3, -1 is for that purpose
13751 * or
13752 * cdclk/crtc_clock
13753 */
13754 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13755
13756 return max_scale;
13757}
13758
Matt Roper465c1202014-05-29 08:06:54 -070013759static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013760intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013761 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013762 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013763{
Matt Roper2b875c22014-12-01 15:40:13 -080013764 struct drm_crtc *crtc = state->base.crtc;
13765 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013766 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013767 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13768 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013769
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013770 /* use scaler when colorkey is not required */
13771 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013772 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013773 min_scale = 1;
13774 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013775 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013776 }
Sonika Jindald8106362015-04-10 14:37:28 +053013777
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013778 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13779 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013780 min_scale, max_scale,
13781 can_position, true,
13782 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013783}
13784
Gustavo Padovan14af2932014-10-24 14:51:31 +010013785static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013786intel_commit_primary_plane(struct drm_plane *plane,
13787 struct intel_plane_state *state)
13788{
Matt Roper2b875c22014-12-01 15:40:13 -080013789 struct drm_crtc *crtc = state->base.crtc;
13790 struct drm_framebuffer *fb = state->base.fb;
13791 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013792 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013793 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013794 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013795
Matt Roperea2c67b2014-12-23 10:41:52 -080013796 crtc = crtc ? crtc : plane->crtc;
13797 intel_crtc = to_intel_crtc(crtc);
13798
Matt Ropercf4c7c12014-12-04 10:27:42 -080013799 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013800 crtc->x = src->x1 >> 16;
13801 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013802
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013803 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013804 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013805
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013806 if (state->visible)
13807 /* FIXME: kill this fastboot hack */
13808 intel_update_pipe_size(intel_crtc);
13809
13810 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013811}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013812
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013813static void
13814intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013815 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013816{
13817 struct drm_device *dev = plane->dev;
13818 struct drm_i915_private *dev_priv = dev->dev_private;
13819
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013820 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13821}
13822
Matt Roper32b7eee2014-12-24 07:59:06 -080013823static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13824{
13825 struct drm_device *dev = crtc->dev;
13826 struct drm_i915_private *dev_priv = dev->dev_private;
13827 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013828
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013829 if (!needs_modeset(crtc->state))
13830 intel_pre_plane_update(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013831
Ville Syrjäläf015c552015-06-24 22:00:02 +030013832 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013833 intel_update_watermarks(crtc);
13834
13835 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013836
13837 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013838 if (crtc->state->active)
Matt Roperc34c9ee2014-12-23 10:41:50 -080013839 intel_crtc->atomic.evade =
13840 intel_pipe_update_start(intel_crtc,
13841 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013842
13843 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13844 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013845}
13846
13847static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13848{
13849 struct drm_device *dev = crtc->dev;
13850 struct drm_i915_private *dev_priv = dev->dev_private;
13851 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013852
Matt Roperc34c9ee2014-12-23 10:41:50 -080013853 if (intel_crtc->atomic.evade)
13854 intel_pipe_update_end(intel_crtc,
13855 intel_crtc->atomic.start_vbl_count);
13856
Matt Roper32b7eee2014-12-24 07:59:06 -080013857 intel_runtime_pm_put(dev_priv);
13858
Maarten Lankhorstac21b222015-06-15 12:33:49 +020013859 intel_post_plane_update(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013860}
13861
Matt Ropercf4c7c12014-12-04 10:27:42 -080013862/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013863 * intel_plane_destroy - destroy a plane
13864 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013865 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013866 * Common destruction function for all types of planes (primary, cursor,
13867 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013868 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013869void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013870{
13871 struct intel_plane *intel_plane = to_intel_plane(plane);
13872 drm_plane_cleanup(plane);
13873 kfree(intel_plane);
13874}
13875
Matt Roper65a3fea2015-01-21 16:35:42 -080013876const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013877 .update_plane = drm_atomic_helper_update_plane,
13878 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013879 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013880 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013881 .atomic_get_property = intel_plane_atomic_get_property,
13882 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013883 .atomic_duplicate_state = intel_plane_duplicate_state,
13884 .atomic_destroy_state = intel_plane_destroy_state,
13885
Matt Roper465c1202014-05-29 08:06:54 -070013886};
13887
13888static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13889 int pipe)
13890{
13891 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013892 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013893 const uint32_t *intel_primary_formats;
13894 int num_formats;
13895
13896 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13897 if (primary == NULL)
13898 return NULL;
13899
Matt Roper8e7d6882015-01-21 16:35:41 -080013900 state = intel_create_plane_state(&primary->base);
13901 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013902 kfree(primary);
13903 return NULL;
13904 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013905 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013906
Matt Roper465c1202014-05-29 08:06:54 -070013907 primary->can_scale = false;
13908 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013909 if (INTEL_INFO(dev)->gen >= 9) {
13910 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013911 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013912 }
Matt Roper465c1202014-05-29 08:06:54 -070013913 primary->pipe = pipe;
13914 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013915 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013916 primary->check_plane = intel_check_primary_plane;
13917 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013918 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013919 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13920 primary->plane = !pipe;
13921
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013922 if (INTEL_INFO(dev)->gen >= 9) {
13923 intel_primary_formats = skl_primary_formats;
13924 num_formats = ARRAY_SIZE(skl_primary_formats);
13925 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013926 intel_primary_formats = i965_primary_formats;
13927 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013928 } else {
13929 intel_primary_formats = i8xx_primary_formats;
13930 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013931 }
13932
13933 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013934 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013935 intel_primary_formats, num_formats,
13936 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013937
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013938 if (INTEL_INFO(dev)->gen >= 4)
13939 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013940
Matt Roperea2c67b2014-12-23 10:41:52 -080013941 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13942
Matt Roper465c1202014-05-29 08:06:54 -070013943 return &primary->base;
13944}
13945
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013946void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13947{
13948 if (!dev->mode_config.rotation_property) {
13949 unsigned long flags = BIT(DRM_ROTATE_0) |
13950 BIT(DRM_ROTATE_180);
13951
13952 if (INTEL_INFO(dev)->gen >= 9)
13953 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13954
13955 dev->mode_config.rotation_property =
13956 drm_mode_create_rotation_property(dev, flags);
13957 }
13958 if (dev->mode_config.rotation_property)
13959 drm_object_attach_property(&plane->base.base,
13960 dev->mode_config.rotation_property,
13961 plane->base.state->rotation);
13962}
13963
Matt Roper3d7d6512014-06-10 08:28:13 -070013964static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013965intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013966 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013967 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013968{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013969 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013970 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013971 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013972 unsigned stride;
13973 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013974
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013975 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13976 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013977 DRM_PLANE_HELPER_NO_SCALING,
13978 DRM_PLANE_HELPER_NO_SCALING,
13979 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013980 if (ret)
13981 return ret;
13982
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013983 /* if we want to turn off the cursor ignore width and height */
13984 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013985 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013986
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013987 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013988 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013989 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13990 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013991 return -EINVAL;
13992 }
13993
Matt Roperea2c67b2014-12-23 10:41:52 -080013994 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13995 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013996 DRM_DEBUG_KMS("buffer is too small\n");
13997 return -ENOMEM;
13998 }
13999
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014000 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014001 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014002 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014003 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014004
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014005 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014006}
14007
Matt Roperf4a2cf22014-12-01 15:40:12 -080014008static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014009intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014010 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014011{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014012 intel_crtc_update_cursor(crtc, false);
14013}
14014
14015static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030014016intel_commit_cursor_plane(struct drm_plane *plane,
14017 struct intel_plane_state *state)
14018{
Matt Roper2b875c22014-12-01 15:40:13 -080014019 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014020 struct drm_device *dev = plane->dev;
14021 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014022 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014023 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014024
Matt Roperea2c67b2014-12-23 10:41:52 -080014025 crtc = crtc ? crtc : plane->crtc;
14026 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014027
Matt Roperea2c67b2014-12-23 10:41:52 -080014028 plane->fb = state->base.fb;
14029 crtc->cursor_x = state->base.crtc_x;
14030 crtc->cursor_y = state->base.crtc_y;
14031
Gustavo Padovana912f122014-12-01 15:40:10 -080014032 if (intel_crtc->cursor_bo == obj)
14033 goto update;
14034
Matt Roperf4a2cf22014-12-01 15:40:12 -080014035 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014036 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014037 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014038 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014039 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014040 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014041
Gustavo Padovana912f122014-12-01 15:40:10 -080014042 intel_crtc->cursor_addr = addr;
14043 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080014044
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020014045update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014046 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014047 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014048}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014049
Matt Roper3d7d6512014-06-10 08:28:13 -070014050static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14051 int pipe)
14052{
14053 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014054 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014055
14056 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14057 if (cursor == NULL)
14058 return NULL;
14059
Matt Roper8e7d6882015-01-21 16:35:41 -080014060 state = intel_create_plane_state(&cursor->base);
14061 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014062 kfree(cursor);
14063 return NULL;
14064 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014065 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014066
Matt Roper3d7d6512014-06-10 08:28:13 -070014067 cursor->can_scale = false;
14068 cursor->max_downscale = 1;
14069 cursor->pipe = pipe;
14070 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014071 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014072 cursor->check_plane = intel_check_cursor_plane;
14073 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014074 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014075
14076 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014077 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014078 intel_cursor_formats,
14079 ARRAY_SIZE(intel_cursor_formats),
14080 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014081
14082 if (INTEL_INFO(dev)->gen >= 4) {
14083 if (!dev->mode_config.rotation_property)
14084 dev->mode_config.rotation_property =
14085 drm_mode_create_rotation_property(dev,
14086 BIT(DRM_ROTATE_0) |
14087 BIT(DRM_ROTATE_180));
14088 if (dev->mode_config.rotation_property)
14089 drm_object_attach_property(&cursor->base.base,
14090 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014091 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014092 }
14093
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014094 if (INTEL_INFO(dev)->gen >=9)
14095 state->scaler_id = -1;
14096
Matt Roperea2c67b2014-12-23 10:41:52 -080014097 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14098
Matt Roper3d7d6512014-06-10 08:28:13 -070014099 return &cursor->base;
14100}
14101
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014102static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14103 struct intel_crtc_state *crtc_state)
14104{
14105 int i;
14106 struct intel_scaler *intel_scaler;
14107 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14108
14109 for (i = 0; i < intel_crtc->num_scalers; i++) {
14110 intel_scaler = &scaler_state->scalers[i];
14111 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014112 intel_scaler->mode = PS_SCALER_MODE_DYN;
14113 }
14114
14115 scaler_state->scaler_id = -1;
14116}
14117
Hannes Ederb358d0a2008-12-18 21:18:47 +010014118static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014119{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014120 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014121 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014122 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014123 struct drm_plane *primary = NULL;
14124 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014125 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014126
Daniel Vetter955382f2013-09-19 14:05:45 +020014127 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014128 if (intel_crtc == NULL)
14129 return;
14130
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014131 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14132 if (!crtc_state)
14133 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014134 intel_crtc->config = crtc_state;
14135 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014136 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014137
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014138 /* initialize shared scalers */
14139 if (INTEL_INFO(dev)->gen >= 9) {
14140 if (pipe == PIPE_C)
14141 intel_crtc->num_scalers = 1;
14142 else
14143 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14144
14145 skl_init_scalers(dev, intel_crtc, crtc_state);
14146 }
14147
Matt Roper465c1202014-05-29 08:06:54 -070014148 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014149 if (!primary)
14150 goto fail;
14151
14152 cursor = intel_cursor_plane_create(dev, pipe);
14153 if (!cursor)
14154 goto fail;
14155
Matt Roper465c1202014-05-29 08:06:54 -070014156 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014157 cursor, &intel_crtc_funcs);
14158 if (ret)
14159 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014160
14161 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014162 for (i = 0; i < 256; i++) {
14163 intel_crtc->lut_r[i] = i;
14164 intel_crtc->lut_g[i] = i;
14165 intel_crtc->lut_b[i] = i;
14166 }
14167
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014168 /*
14169 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014170 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014171 */
Jesse Barnes80824002009-09-10 15:28:06 -070014172 intel_crtc->pipe = pipe;
14173 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014174 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014175 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014176 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014177 }
14178
Chris Wilson4b0e3332014-05-30 16:35:26 +030014179 intel_crtc->cursor_base = ~0;
14180 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014181 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014182
Ville Syrjälä852eb002015-06-24 22:00:07 +030014183 intel_crtc->wm.cxsr_allowed = true;
14184
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014185 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14186 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14187 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14188 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14189
Jesse Barnes79e53942008-11-07 14:24:08 -080014190 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014191
14192 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014193 return;
14194
14195fail:
14196 if (primary)
14197 drm_plane_cleanup(primary);
14198 if (cursor)
14199 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014200 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014201 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014202}
14203
Jesse Barnes752aa882013-10-31 18:55:49 +020014204enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14205{
14206 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014207 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014208
Rob Clark51fd3712013-11-19 12:10:12 -050014209 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014210
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014211 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014212 return INVALID_PIPE;
14213
14214 return to_intel_crtc(encoder->crtc)->pipe;
14215}
14216
Carl Worth08d7b3d2009-04-29 14:43:54 -070014217int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014218 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014219{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014220 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014221 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014222 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014223
Rob Clark7707e652014-07-17 23:30:04 -040014224 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014225
Rob Clark7707e652014-07-17 23:30:04 -040014226 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014227 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014228 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014229 }
14230
Rob Clark7707e652014-07-17 23:30:04 -040014231 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014232 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014233
Daniel Vetterc05422d2009-08-11 16:05:30 +020014234 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014235}
14236
Daniel Vetter66a92782012-07-12 20:08:18 +020014237static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014238{
Daniel Vetter66a92782012-07-12 20:08:18 +020014239 struct drm_device *dev = encoder->base.dev;
14240 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014241 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014242 int entry = 0;
14243
Damien Lespiaub2784e12014-08-05 11:29:37 +010014244 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014245 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014246 index_mask |= (1 << entry);
14247
Jesse Barnes79e53942008-11-07 14:24:08 -080014248 entry++;
14249 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014250
Jesse Barnes79e53942008-11-07 14:24:08 -080014251 return index_mask;
14252}
14253
Chris Wilson4d302442010-12-14 19:21:29 +000014254static bool has_edp_a(struct drm_device *dev)
14255{
14256 struct drm_i915_private *dev_priv = dev->dev_private;
14257
14258 if (!IS_MOBILE(dev))
14259 return false;
14260
14261 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14262 return false;
14263
Damien Lespiaue3589902014-02-07 19:12:50 +000014264 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014265 return false;
14266
14267 return true;
14268}
14269
Jesse Barnes84b4e042014-06-25 08:24:29 -070014270static bool intel_crt_present(struct drm_device *dev)
14271{
14272 struct drm_i915_private *dev_priv = dev->dev_private;
14273
Damien Lespiau884497e2013-12-03 13:56:23 +000014274 if (INTEL_INFO(dev)->gen >= 9)
14275 return false;
14276
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014277 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014278 return false;
14279
14280 if (IS_CHERRYVIEW(dev))
14281 return false;
14282
14283 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14284 return false;
14285
14286 return true;
14287}
14288
Jesse Barnes79e53942008-11-07 14:24:08 -080014289static void intel_setup_outputs(struct drm_device *dev)
14290{
Eric Anholt725e30a2009-01-22 13:01:02 -080014291 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014292 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014293 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014294
Daniel Vetterc9093352013-06-06 22:22:47 +020014295 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014296
Jesse Barnes84b4e042014-06-25 08:24:29 -070014297 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014298 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014299
Vandana Kannanc776eb22014-08-19 12:05:01 +053014300 if (IS_BROXTON(dev)) {
14301 /*
14302 * FIXME: Broxton doesn't support port detection via the
14303 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14304 * detect the ports.
14305 */
14306 intel_ddi_init(dev, PORT_A);
14307 intel_ddi_init(dev, PORT_B);
14308 intel_ddi_init(dev, PORT_C);
14309 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014310 int found;
14311
Jesse Barnesde31fac2015-03-06 15:53:32 -080014312 /*
14313 * Haswell uses DDI functions to detect digital outputs.
14314 * On SKL pre-D0 the strap isn't connected, so we assume
14315 * it's there.
14316 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014317 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014318 /* WaIgnoreDDIAStrap: skl */
14319 if (found ||
14320 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014321 intel_ddi_init(dev, PORT_A);
14322
14323 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14324 * register */
14325 found = I915_READ(SFUSE_STRAP);
14326
14327 if (found & SFUSE_STRAP_DDIB_DETECTED)
14328 intel_ddi_init(dev, PORT_B);
14329 if (found & SFUSE_STRAP_DDIC_DETECTED)
14330 intel_ddi_init(dev, PORT_C);
14331 if (found & SFUSE_STRAP_DDID_DETECTED)
14332 intel_ddi_init(dev, PORT_D);
14333 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014334 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014335 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014336
14337 if (has_edp_a(dev))
14338 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014339
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014340 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014341 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014342 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014343 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014344 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014345 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014346 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014347 }
14348
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014349 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014350 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014351
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014352 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014353 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014354
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014355 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014356 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014357
Daniel Vetter270b3042012-10-27 15:52:05 +020014358 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014359 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014360 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014361 /*
14362 * The DP_DETECTED bit is the latched state of the DDC
14363 * SDA pin at boot. However since eDP doesn't require DDC
14364 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14365 * eDP ports may have been muxed to an alternate function.
14366 * Thus we can't rely on the DP_DETECTED bit alone to detect
14367 * eDP ports. Consult the VBT as well as DP_DETECTED to
14368 * detect eDP ports.
14369 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014370 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14371 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014372 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14373 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014374 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14375 intel_dp_is_edp(dev, PORT_B))
14376 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014377
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014378 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14379 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014380 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14381 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014382 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14383 intel_dp_is_edp(dev, PORT_C))
14384 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014385
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014386 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014387 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014388 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14389 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014390 /* eDP not supported on port D, so don't check VBT */
14391 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14392 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014393 }
14394
Jani Nikula3cfca972013-08-27 15:12:26 +030014395 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014396 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014397 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014398
Paulo Zanonie2debe92013-02-18 19:00:27 -030014399 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014400 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014401 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014402 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014403 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014404 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014405 }
Ma Ling27185ae2009-08-24 13:50:23 +080014406
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014407 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014408 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014409 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014410
14411 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014412
Paulo Zanonie2debe92013-02-18 19:00:27 -030014413 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014414 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014415 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014416 }
Ma Ling27185ae2009-08-24 13:50:23 +080014417
Paulo Zanonie2debe92013-02-18 19:00:27 -030014418 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014419
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014420 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014421 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014422 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014423 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014424 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014425 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014426 }
Ma Ling27185ae2009-08-24 13:50:23 +080014427
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014428 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014429 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014430 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014431 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014432 intel_dvo_init(dev);
14433
Zhenyu Wang103a1962009-11-27 11:44:36 +080014434 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014435 intel_tv_init(dev);
14436
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014437 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014438
Damien Lespiaub2784e12014-08-05 11:29:37 +010014439 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014440 encoder->base.possible_crtcs = encoder->crtc_mask;
14441 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014442 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014443 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014444
Paulo Zanonidde86e22012-12-01 12:04:25 -020014445 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014446
14447 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014448}
14449
14450static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14451{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014452 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014453 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014454
Daniel Vetteref2d6332014-02-10 18:00:38 +010014455 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014456 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014457 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014458 drm_gem_object_unreference(&intel_fb->obj->base);
14459 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014460 kfree(intel_fb);
14461}
14462
14463static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014464 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014465 unsigned int *handle)
14466{
14467 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014468 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014469
Chris Wilson05394f32010-11-08 19:18:58 +000014470 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014471}
14472
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014473static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14474 struct drm_file *file,
14475 unsigned flags, unsigned color,
14476 struct drm_clip_rect *clips,
14477 unsigned num_clips)
14478{
14479 struct drm_device *dev = fb->dev;
14480 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14481 struct drm_i915_gem_object *obj = intel_fb->obj;
14482
14483 mutex_lock(&dev->struct_mutex);
14484 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
14485 mutex_unlock(&dev->struct_mutex);
14486
14487 return 0;
14488}
14489
Jesse Barnes79e53942008-11-07 14:24:08 -080014490static const struct drm_framebuffer_funcs intel_fb_funcs = {
14491 .destroy = intel_user_framebuffer_destroy,
14492 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014493 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014494};
14495
Damien Lespiaub3218032015-02-27 11:15:18 +000014496static
14497u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14498 uint32_t pixel_format)
14499{
14500 u32 gen = INTEL_INFO(dev)->gen;
14501
14502 if (gen >= 9) {
14503 /* "The stride in bytes must not exceed the of the size of 8K
14504 * pixels and 32K bytes."
14505 */
14506 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14507 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14508 return 32*1024;
14509 } else if (gen >= 4) {
14510 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14511 return 16*1024;
14512 else
14513 return 32*1024;
14514 } else if (gen >= 3) {
14515 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14516 return 8*1024;
14517 else
14518 return 16*1024;
14519 } else {
14520 /* XXX DSPC is limited to 4k tiled */
14521 return 8*1024;
14522 }
14523}
14524
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014525static int intel_framebuffer_init(struct drm_device *dev,
14526 struct intel_framebuffer *intel_fb,
14527 struct drm_mode_fb_cmd2 *mode_cmd,
14528 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014529{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014530 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014531 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014532 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014533
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014534 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14535
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014536 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14537 /* Enforce that fb modifier and tiling mode match, but only for
14538 * X-tiled. This is needed for FBC. */
14539 if (!!(obj->tiling_mode == I915_TILING_X) !=
14540 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14541 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14542 return -EINVAL;
14543 }
14544 } else {
14545 if (obj->tiling_mode == I915_TILING_X)
14546 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14547 else if (obj->tiling_mode == I915_TILING_Y) {
14548 DRM_DEBUG("No Y tiling for legacy addfb\n");
14549 return -EINVAL;
14550 }
14551 }
14552
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014553 /* Passed in modifier sanity checking. */
14554 switch (mode_cmd->modifier[0]) {
14555 case I915_FORMAT_MOD_Y_TILED:
14556 case I915_FORMAT_MOD_Yf_TILED:
14557 if (INTEL_INFO(dev)->gen < 9) {
14558 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14559 mode_cmd->modifier[0]);
14560 return -EINVAL;
14561 }
14562 case DRM_FORMAT_MOD_NONE:
14563 case I915_FORMAT_MOD_X_TILED:
14564 break;
14565 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014566 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14567 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014568 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014569 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014570
Damien Lespiaub3218032015-02-27 11:15:18 +000014571 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14572 mode_cmd->pixel_format);
14573 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14574 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14575 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014576 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014577 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014578
Damien Lespiaub3218032015-02-27 11:15:18 +000014579 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14580 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014581 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014582 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14583 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014584 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014585 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014586 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014587 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014588
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014589 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014590 mode_cmd->pitches[0] != obj->stride) {
14591 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14592 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014593 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014594 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014595
Ville Syrjälä57779d02012-10-31 17:50:14 +020014596 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014597 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014598 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014599 case DRM_FORMAT_RGB565:
14600 case DRM_FORMAT_XRGB8888:
14601 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014602 break;
14603 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014604 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014605 DRM_DEBUG("unsupported pixel format: %s\n",
14606 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014607 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014608 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014609 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014610 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014611 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14612 DRM_DEBUG("unsupported pixel format: %s\n",
14613 drm_get_format_name(mode_cmd->pixel_format));
14614 return -EINVAL;
14615 }
14616 break;
14617 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014618 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014619 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014620 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014621 DRM_DEBUG("unsupported pixel format: %s\n",
14622 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014623 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014624 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014625 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014626 case DRM_FORMAT_ABGR2101010:
14627 if (!IS_VALLEYVIEW(dev)) {
14628 DRM_DEBUG("unsupported pixel format: %s\n",
14629 drm_get_format_name(mode_cmd->pixel_format));
14630 return -EINVAL;
14631 }
14632 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014633 case DRM_FORMAT_YUYV:
14634 case DRM_FORMAT_UYVY:
14635 case DRM_FORMAT_YVYU:
14636 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014637 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014638 DRM_DEBUG("unsupported pixel format: %s\n",
14639 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014640 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014641 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014642 break;
14643 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014644 DRM_DEBUG("unsupported pixel format: %s\n",
14645 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014646 return -EINVAL;
14647 }
14648
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014649 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14650 if (mode_cmd->offsets[0] != 0)
14651 return -EINVAL;
14652
Damien Lespiauec2c9812015-01-20 12:51:45 +000014653 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014654 mode_cmd->pixel_format,
14655 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014656 /* FIXME drm helper for size checks (especially planar formats)? */
14657 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14658 return -EINVAL;
14659
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014660 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14661 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014662 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014663
Jesse Barnes79e53942008-11-07 14:24:08 -080014664 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14665 if (ret) {
14666 DRM_ERROR("framebuffer init failed %d\n", ret);
14667 return ret;
14668 }
14669
Jesse Barnes79e53942008-11-07 14:24:08 -080014670 return 0;
14671}
14672
Jesse Barnes79e53942008-11-07 14:24:08 -080014673static struct drm_framebuffer *
14674intel_user_framebuffer_create(struct drm_device *dev,
14675 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014676 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014677{
Chris Wilson05394f32010-11-08 19:18:58 +000014678 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014679
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014680 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14681 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014682 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014683 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014684
Chris Wilsond2dff872011-04-19 08:36:26 +010014685 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014686}
14687
Daniel Vetter4520f532013-10-09 09:18:51 +020014688#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014689static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014690{
14691}
14692#endif
14693
Jesse Barnes79e53942008-11-07 14:24:08 -080014694static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014695 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014696 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014697 .atomic_check = intel_atomic_check,
14698 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014699 .atomic_state_alloc = intel_atomic_state_alloc,
14700 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014701};
14702
Jesse Barnese70236a2009-09-21 10:42:27 -070014703/* Set up chip specific display functions */
14704static void intel_init_display(struct drm_device *dev)
14705{
14706 struct drm_i915_private *dev_priv = dev->dev_private;
14707
Daniel Vetteree9300b2013-06-03 22:40:22 +020014708 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14709 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014710 else if (IS_CHERRYVIEW(dev))
14711 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014712 else if (IS_VALLEYVIEW(dev))
14713 dev_priv->display.find_dpll = vlv_find_best_dpll;
14714 else if (IS_PINEVIEW(dev))
14715 dev_priv->display.find_dpll = pnv_find_best_dpll;
14716 else
14717 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14718
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014719 if (INTEL_INFO(dev)->gen >= 9) {
14720 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014721 dev_priv->display.get_initial_plane_config =
14722 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014723 dev_priv->display.crtc_compute_clock =
14724 haswell_crtc_compute_clock;
14725 dev_priv->display.crtc_enable = haswell_crtc_enable;
14726 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014727 dev_priv->display.update_primary_plane =
14728 skylake_update_primary_plane;
14729 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014730 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014731 dev_priv->display.get_initial_plane_config =
14732 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014733 dev_priv->display.crtc_compute_clock =
14734 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014735 dev_priv->display.crtc_enable = haswell_crtc_enable;
14736 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014737 dev_priv->display.update_primary_plane =
14738 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014739 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014740 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014741 dev_priv->display.get_initial_plane_config =
14742 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014743 dev_priv->display.crtc_compute_clock =
14744 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014745 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14746 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014747 dev_priv->display.update_primary_plane =
14748 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014749 } else if (IS_VALLEYVIEW(dev)) {
14750 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014751 dev_priv->display.get_initial_plane_config =
14752 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014753 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014754 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14755 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014756 dev_priv->display.update_primary_plane =
14757 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014758 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014759 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014760 dev_priv->display.get_initial_plane_config =
14761 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014762 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014763 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14764 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014765 dev_priv->display.update_primary_plane =
14766 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014767 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014768
Jesse Barnese70236a2009-09-21 10:42:27 -070014769 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014770 if (IS_SKYLAKE(dev))
14771 dev_priv->display.get_display_clock_speed =
14772 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014773 else if (IS_BROXTON(dev))
14774 dev_priv->display.get_display_clock_speed =
14775 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014776 else if (IS_BROADWELL(dev))
14777 dev_priv->display.get_display_clock_speed =
14778 broadwell_get_display_clock_speed;
14779 else if (IS_HASWELL(dev))
14780 dev_priv->display.get_display_clock_speed =
14781 haswell_get_display_clock_speed;
14782 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014783 dev_priv->display.get_display_clock_speed =
14784 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014785 else if (IS_GEN5(dev))
14786 dev_priv->display.get_display_clock_speed =
14787 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014788 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014789 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014790 dev_priv->display.get_display_clock_speed =
14791 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014792 else if (IS_GM45(dev))
14793 dev_priv->display.get_display_clock_speed =
14794 gm45_get_display_clock_speed;
14795 else if (IS_CRESTLINE(dev))
14796 dev_priv->display.get_display_clock_speed =
14797 i965gm_get_display_clock_speed;
14798 else if (IS_PINEVIEW(dev))
14799 dev_priv->display.get_display_clock_speed =
14800 pnv_get_display_clock_speed;
14801 else if (IS_G33(dev) || IS_G4X(dev))
14802 dev_priv->display.get_display_clock_speed =
14803 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014804 else if (IS_I915G(dev))
14805 dev_priv->display.get_display_clock_speed =
14806 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014807 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014808 dev_priv->display.get_display_clock_speed =
14809 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014810 else if (IS_PINEVIEW(dev))
14811 dev_priv->display.get_display_clock_speed =
14812 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014813 else if (IS_I915GM(dev))
14814 dev_priv->display.get_display_clock_speed =
14815 i915gm_get_display_clock_speed;
14816 else if (IS_I865G(dev))
14817 dev_priv->display.get_display_clock_speed =
14818 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014819 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014820 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014821 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014822 else { /* 830 */
14823 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014824 dev_priv->display.get_display_clock_speed =
14825 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014826 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014827
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014828 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014829 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014830 } else if (IS_GEN6(dev)) {
14831 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014832 } else if (IS_IVYBRIDGE(dev)) {
14833 /* FIXME: detect B0+ stepping and use auto training */
14834 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014835 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014836 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014837 if (IS_BROADWELL(dev)) {
14838 dev_priv->display.modeset_commit_cdclk =
14839 broadwell_modeset_commit_cdclk;
14840 dev_priv->display.modeset_calc_cdclk =
14841 broadwell_modeset_calc_cdclk;
14842 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014843 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014844 dev_priv->display.modeset_commit_cdclk =
14845 valleyview_modeset_commit_cdclk;
14846 dev_priv->display.modeset_calc_cdclk =
14847 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014848 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014849 dev_priv->display.modeset_commit_cdclk =
14850 broxton_modeset_commit_cdclk;
14851 dev_priv->display.modeset_calc_cdclk =
14852 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014853 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014854
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014855 switch (INTEL_INFO(dev)->gen) {
14856 case 2:
14857 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14858 break;
14859
14860 case 3:
14861 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14862 break;
14863
14864 case 4:
14865 case 5:
14866 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14867 break;
14868
14869 case 6:
14870 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14871 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014872 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014873 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014874 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14875 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014876 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014877 /* Drop through - unsupported since execlist only. */
14878 default:
14879 /* Default just returns -ENODEV to indicate unsupported */
14880 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014881 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014882
14883 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014884
14885 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014886}
14887
Jesse Barnesb690e962010-07-19 13:53:12 -070014888/*
14889 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14890 * resume, or other times. This quirk makes sure that's the case for
14891 * affected systems.
14892 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014893static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014894{
14895 struct drm_i915_private *dev_priv = dev->dev_private;
14896
14897 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014898 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014899}
14900
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014901static void quirk_pipeb_force(struct drm_device *dev)
14902{
14903 struct drm_i915_private *dev_priv = dev->dev_private;
14904
14905 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14906 DRM_INFO("applying pipe b force quirk\n");
14907}
14908
Keith Packard435793d2011-07-12 14:56:22 -070014909/*
14910 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14911 */
14912static void quirk_ssc_force_disable(struct drm_device *dev)
14913{
14914 struct drm_i915_private *dev_priv = dev->dev_private;
14915 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014916 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014917}
14918
Carsten Emde4dca20e2012-03-15 15:56:26 +010014919/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014920 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14921 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014922 */
14923static void quirk_invert_brightness(struct drm_device *dev)
14924{
14925 struct drm_i915_private *dev_priv = dev->dev_private;
14926 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014927 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014928}
14929
Scot Doyle9c72cc62014-07-03 23:27:50 +000014930/* Some VBT's incorrectly indicate no backlight is present */
14931static void quirk_backlight_present(struct drm_device *dev)
14932{
14933 struct drm_i915_private *dev_priv = dev->dev_private;
14934 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14935 DRM_INFO("applying backlight present quirk\n");
14936}
14937
Jesse Barnesb690e962010-07-19 13:53:12 -070014938struct intel_quirk {
14939 int device;
14940 int subsystem_vendor;
14941 int subsystem_device;
14942 void (*hook)(struct drm_device *dev);
14943};
14944
Egbert Eich5f85f172012-10-14 15:46:38 +020014945/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14946struct intel_dmi_quirk {
14947 void (*hook)(struct drm_device *dev);
14948 const struct dmi_system_id (*dmi_id_list)[];
14949};
14950
14951static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14952{
14953 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14954 return 1;
14955}
14956
14957static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14958 {
14959 .dmi_id_list = &(const struct dmi_system_id[]) {
14960 {
14961 .callback = intel_dmi_reverse_brightness,
14962 .ident = "NCR Corporation",
14963 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14964 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14965 },
14966 },
14967 { } /* terminating entry */
14968 },
14969 .hook = quirk_invert_brightness,
14970 },
14971};
14972
Ben Widawskyc43b5632012-04-16 14:07:40 -070014973static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014974 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14975 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14976
Jesse Barnesb690e962010-07-19 13:53:12 -070014977 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14978 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14979
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014980 /* 830 needs to leave pipe A & dpll A up */
14981 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14982
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014983 /* 830 needs to leave pipe B & dpll B up */
14984 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14985
Keith Packard435793d2011-07-12 14:56:22 -070014986 /* Lenovo U160 cannot use SSC on LVDS */
14987 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014988
14989 /* Sony Vaio Y cannot use SSC on LVDS */
14990 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014991
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014992 /* Acer Aspire 5734Z must invert backlight brightness */
14993 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14994
14995 /* Acer/eMachines G725 */
14996 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14997
14998 /* Acer/eMachines e725 */
14999 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15000
15001 /* Acer/Packard Bell NCL20 */
15002 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15003
15004 /* Acer Aspire 4736Z */
15005 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015006
15007 /* Acer Aspire 5336 */
15008 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015009
15010 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15011 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015012
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015013 /* Acer C720 Chromebook (Core i3 4005U) */
15014 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15015
jens steinb2a96012014-10-28 20:25:53 +010015016 /* Apple Macbook 2,1 (Core 2 T7400) */
15017 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15018
Scot Doyled4967d82014-07-03 23:27:52 +000015019 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15020 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015021
15022 /* HP Chromebook 14 (Celeron 2955U) */
15023 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015024
15025 /* Dell Chromebook 11 */
15026 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015027};
15028
15029static void intel_init_quirks(struct drm_device *dev)
15030{
15031 struct pci_dev *d = dev->pdev;
15032 int i;
15033
15034 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15035 struct intel_quirk *q = &intel_quirks[i];
15036
15037 if (d->device == q->device &&
15038 (d->subsystem_vendor == q->subsystem_vendor ||
15039 q->subsystem_vendor == PCI_ANY_ID) &&
15040 (d->subsystem_device == q->subsystem_device ||
15041 q->subsystem_device == PCI_ANY_ID))
15042 q->hook(dev);
15043 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015044 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15045 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15046 intel_dmi_quirks[i].hook(dev);
15047 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015048}
15049
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015050/* Disable the VGA plane that we never use */
15051static void i915_disable_vga(struct drm_device *dev)
15052{
15053 struct drm_i915_private *dev_priv = dev->dev_private;
15054 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015055 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015056
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015057 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015058 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015059 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015060 sr1 = inb(VGA_SR_DATA);
15061 outb(sr1 | 1<<5, VGA_SR_DATA);
15062 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15063 udelay(300);
15064
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015065 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015066 POSTING_READ(vga_reg);
15067}
15068
Daniel Vetterf8175862012-04-10 15:50:11 +020015069void intel_modeset_init_hw(struct drm_device *dev)
15070{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015071 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015072 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015073 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015074 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015075}
15076
Jesse Barnes79e53942008-11-07 14:24:08 -080015077void intel_modeset_init(struct drm_device *dev)
15078{
Jesse Barnes652c3932009-08-17 13:31:43 -070015079 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015080 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015081 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015082 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015083
15084 drm_mode_config_init(dev);
15085
15086 dev->mode_config.min_width = 0;
15087 dev->mode_config.min_height = 0;
15088
Dave Airlie019d96c2011-09-29 16:20:42 +010015089 dev->mode_config.preferred_depth = 24;
15090 dev->mode_config.prefer_shadow = 1;
15091
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015092 dev->mode_config.allow_fb_modifiers = true;
15093
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015094 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015095
Jesse Barnesb690e962010-07-19 13:53:12 -070015096 intel_init_quirks(dev);
15097
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015098 intel_init_pm(dev);
15099
Ben Widawskye3c74752013-04-05 13:12:39 -070015100 if (INTEL_INFO(dev)->num_pipes == 0)
15101 return;
15102
Jesse Barnese70236a2009-09-21 10:42:27 -070015103 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015104 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015105
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015106 if (IS_GEN2(dev)) {
15107 dev->mode_config.max_width = 2048;
15108 dev->mode_config.max_height = 2048;
15109 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015110 dev->mode_config.max_width = 4096;
15111 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015112 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015113 dev->mode_config.max_width = 8192;
15114 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015115 }
Damien Lespiau068be562014-03-28 14:17:49 +000015116
Ville Syrjälädc41c152014-08-13 11:57:05 +030015117 if (IS_845G(dev) || IS_I865G(dev)) {
15118 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15119 dev->mode_config.cursor_height = 1023;
15120 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015121 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15122 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15123 } else {
15124 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15125 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15126 }
15127
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015128 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015129
Zhao Yakui28c97732009-10-09 11:39:41 +080015130 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015131 INTEL_INFO(dev)->num_pipes,
15132 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015133
Damien Lespiau055e3932014-08-18 13:49:10 +010015134 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015135 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015136 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015137 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015138 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015139 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015140 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015141 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015142 }
15143
Jesse Barnesf42bb702013-12-16 16:34:23 -080015144 intel_init_dpio(dev);
15145
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015146 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015147
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015148 /* Just disable it once at startup */
15149 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015150 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015151
15152 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030015153 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015154
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015155 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015156 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015157 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015158
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015159 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015160 struct intel_initial_plane_config plane_config = {};
15161
Jesse Barnes46f297f2014-03-07 08:57:48 -080015162 if (!crtc->active)
15163 continue;
15164
Jesse Barnes46f297f2014-03-07 08:57:48 -080015165 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015166 * Note that reserving the BIOS fb up front prevents us
15167 * from stuffing other stolen allocations like the ring
15168 * on top. This prevents some ugliness at boot time, and
15169 * can even allow for smooth boot transitions if the BIOS
15170 * fb is large enough for the active pipe configuration.
15171 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015172 dev_priv->display.get_initial_plane_config(crtc,
15173 &plane_config);
15174
15175 /*
15176 * If the fb is shared between multiple heads, we'll
15177 * just get the first one.
15178 */
15179 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015180 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015181}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015182
Daniel Vetter7fad7982012-07-04 17:51:47 +020015183static void intel_enable_pipe_a(struct drm_device *dev)
15184{
15185 struct intel_connector *connector;
15186 struct drm_connector *crt = NULL;
15187 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015188 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015189
15190 /* We can't just switch on the pipe A, we need to set things up with a
15191 * proper mode and output configuration. As a gross hack, enable pipe A
15192 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015193 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015194 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15195 crt = &connector->base;
15196 break;
15197 }
15198 }
15199
15200 if (!crt)
15201 return;
15202
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015203 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015204 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015205}
15206
Daniel Vetterfa555832012-10-10 23:14:00 +020015207static bool
15208intel_check_plane_mapping(struct intel_crtc *crtc)
15209{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015210 struct drm_device *dev = crtc->base.dev;
15211 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015212 u32 reg, val;
15213
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015214 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015215 return true;
15216
15217 reg = DSPCNTR(!crtc->plane);
15218 val = I915_READ(reg);
15219
15220 if ((val & DISPLAY_PLANE_ENABLE) &&
15221 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15222 return false;
15223
15224 return true;
15225}
15226
Daniel Vetter24929352012-07-02 20:28:59 +020015227static void intel_sanitize_crtc(struct intel_crtc *crtc)
15228{
15229 struct drm_device *dev = crtc->base.dev;
15230 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015231 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015232 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015233 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015234
Daniel Vetter24929352012-07-02 20:28:59 +020015235 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015236 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015237 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15238
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015239 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015240 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015241 if (crtc->active) {
15242 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015243 drm_crtc_vblank_on(&crtc->base);
15244 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015245
Daniel Vetter24929352012-07-02 20:28:59 +020015246 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015247 * disable the crtc (and hence change the state) if it is wrong. Note
15248 * that gen4+ has a fixed plane -> pipe mapping. */
15249 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015250 bool plane;
15251
Daniel Vetter24929352012-07-02 20:28:59 +020015252 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15253 crtc->base.base.id);
15254
15255 /* Pipe has the wrong plane attached and the plane is active.
15256 * Temporarily change the plane mapping and disable everything
15257 * ... */
15258 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015259 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015260 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015261 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015262 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015263 }
Daniel Vetter24929352012-07-02 20:28:59 +020015264
Daniel Vetter7fad7982012-07-04 17:51:47 +020015265 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15266 crtc->pipe == PIPE_A && !crtc->active) {
15267 /* BIOS forgot to enable pipe A, this mostly happens after
15268 * resume. Force-enable the pipe to fix this, the update_dpms
15269 * call below we restore the pipe to the right state, but leave
15270 * the required bits on. */
15271 intel_enable_pipe_a(dev);
15272 }
15273
Daniel Vetter24929352012-07-02 20:28:59 +020015274 /* Adjust the state of the output pipe according to whether we
15275 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015276 enable = false;
15277 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15278 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015279
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015280 if (!enable)
15281 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015282
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015283 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015284
15285 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015286 * functions or because of calls to intel_crtc_disable_noatomic,
15287 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015288 * pipe A quirk. */
15289 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15290 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015291 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015292 crtc->active ? "enabled" : "disabled");
15293
Matt Roper83d65732015-02-25 13:12:16 -080015294 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015295 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015296 crtc->base.enabled = crtc->active;
15297
15298 /* Because we only establish the connector -> encoder ->
15299 * crtc links if something is active, this means the
15300 * crtc is now deactivated. Break the links. connector
15301 * -> encoder links are only establish when things are
15302 * actually up, hence no need to break them. */
15303 WARN_ON(crtc->active);
15304
15305 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15306 WARN_ON(encoder->connectors_active);
15307 encoder->base.crtc = NULL;
15308 }
15309 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015310
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015311 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015312 /*
15313 * We start out with underrun reporting disabled to avoid races.
15314 * For correct bookkeeping mark this on active crtcs.
15315 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015316 * Also on gmch platforms we dont have any hardware bits to
15317 * disable the underrun reporting. Which means we need to start
15318 * out with underrun reporting disabled also on inactive pipes,
15319 * since otherwise we'll complain about the garbage we read when
15320 * e.g. coming up after runtime pm.
15321 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015322 * No protection against concurrent access is required - at
15323 * worst a fifo underrun happens which also sets this to false.
15324 */
15325 crtc->cpu_fifo_underrun_disabled = true;
15326 crtc->pch_fifo_underrun_disabled = true;
15327 }
Daniel Vetter24929352012-07-02 20:28:59 +020015328}
15329
15330static void intel_sanitize_encoder(struct intel_encoder *encoder)
15331{
15332 struct intel_connector *connector;
15333 struct drm_device *dev = encoder->base.dev;
15334
15335 /* We need to check both for a crtc link (meaning that the
15336 * encoder is active and trying to read from a pipe) and the
15337 * pipe itself being active. */
15338 bool has_active_crtc = encoder->base.crtc &&
15339 to_intel_crtc(encoder->base.crtc)->active;
15340
15341 if (encoder->connectors_active && !has_active_crtc) {
15342 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15343 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015344 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015345
15346 /* Connector is active, but has no active pipe. This is
15347 * fallout from our resume register restoring. Disable
15348 * the encoder manually again. */
15349 if (encoder->base.crtc) {
15350 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15351 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015352 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015353 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015354 if (encoder->post_disable)
15355 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015356 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015357 encoder->base.crtc = NULL;
15358 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015359
15360 /* Inconsistent output/port/pipe state happens presumably due to
15361 * a bug in one of the get_hw_state functions. Or someplace else
15362 * in our code, like the register restore mess on resume. Clamp
15363 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015364 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015365 if (connector->encoder != encoder)
15366 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015367 connector->base.dpms = DRM_MODE_DPMS_OFF;
15368 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015369 }
15370 }
15371 /* Enabled encoders without active connectors will be fixed in
15372 * the crtc fixup. */
15373}
15374
Imre Deak04098752014-02-18 00:02:16 +020015375void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015376{
15377 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015378 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015379
Imre Deak04098752014-02-18 00:02:16 +020015380 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15381 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15382 i915_disable_vga(dev);
15383 }
15384}
15385
15386void i915_redisable_vga(struct drm_device *dev)
15387{
15388 struct drm_i915_private *dev_priv = dev->dev_private;
15389
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015390 /* This function can be called both from intel_modeset_setup_hw_state or
15391 * at a very early point in our resume sequence, where the power well
15392 * structures are not yet restored. Since this function is at a very
15393 * paranoid "someone might have enabled VGA while we were not looking"
15394 * level, just check if the power well is enabled instead of trying to
15395 * follow the "don't touch the power well if we don't need it" policy
15396 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015397 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015398 return;
15399
Imre Deak04098752014-02-18 00:02:16 +020015400 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015401}
15402
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015403static bool primary_get_hw_state(struct intel_crtc *crtc)
15404{
15405 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15406
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015407 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15408}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015409
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015410static void readout_plane_state(struct intel_crtc *crtc,
15411 struct intel_crtc_state *crtc_state)
15412{
15413 struct intel_plane *p;
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015414 struct intel_plane_state *plane_state;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015415 bool active = crtc_state->base.active;
15416
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015417 for_each_intel_plane(crtc->base.dev, p) {
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015418 if (crtc->pipe != p->pipe)
15419 continue;
15420
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015421 plane_state = to_intel_plane_state(p->base.state);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015422
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015423 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15424 plane_state->visible = primary_get_hw_state(crtc);
15425 else {
15426 if (active)
15427 p->disable_plane(&p->base, &crtc->base);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015428
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015429 plane_state->visible = false;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015430 }
15431 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015432}
15433
Daniel Vetter30e984d2013-06-05 13:34:17 +020015434static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015435{
15436 struct drm_i915_private *dev_priv = dev->dev_private;
15437 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015438 struct intel_crtc *crtc;
15439 struct intel_encoder *encoder;
15440 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015441 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015442
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015443 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015444 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015445 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015446 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015447
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015448 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015449
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015450 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015451 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015452
Matt Roper83d65732015-02-25 13:12:16 -080015453 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015454 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015455 crtc->base.enabled = crtc->active;
Maarten Lankhorstb8b7fad2015-06-12 11:15:41 +020015456 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015457
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015458 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015459
15460 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15461 crtc->base.base.id,
15462 crtc->active ? "enabled" : "disabled");
15463 }
15464
Daniel Vetter53589012013-06-05 13:34:16 +020015465 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15466 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15467
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015468 pll->on = pll->get_hw_state(dev_priv, pll,
15469 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015470 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015471 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015472 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015473 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015474 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015475 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015476 }
Daniel Vetter53589012013-06-05 13:34:16 +020015477 }
Daniel Vetter53589012013-06-05 13:34:16 +020015478
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015479 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015480 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015481
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015482 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015483 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015484 }
15485
Damien Lespiaub2784e12014-08-05 11:29:37 +010015486 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015487 pipe = 0;
15488
15489 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015490 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15491 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015492 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015493 } else {
15494 encoder->base.crtc = NULL;
15495 }
15496
15497 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015498 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015499 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015500 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015501 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015502 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015503 }
15504
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015505 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015506 if (connector->get_hw_state(connector)) {
15507 connector->base.dpms = DRM_MODE_DPMS_ON;
15508 connector->encoder->connectors_active = true;
15509 connector->base.encoder = &connector->encoder->base;
15510 } else {
15511 connector->base.dpms = DRM_MODE_DPMS_OFF;
15512 connector->base.encoder = NULL;
15513 }
15514 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15515 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015516 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015517 connector->base.encoder ? "enabled" : "disabled");
15518 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015519}
15520
15521/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15522 * and i915 state tracking structures. */
15523void intel_modeset_setup_hw_state(struct drm_device *dev,
15524 bool force_restore)
15525{
15526 struct drm_i915_private *dev_priv = dev->dev_private;
15527 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015528 struct intel_crtc *crtc;
15529 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015530 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015531
15532 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015533
Jesse Barnesbabea612013-06-26 18:57:38 +030015534 /*
15535 * Now that we have the config, copy it to each CRTC struct
15536 * Note that this could go away if we move to using crtc_config
15537 * checking everywhere.
15538 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015539 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015540 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015541 intel_mode_from_pipe_config(&crtc->base.mode,
15542 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015543 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15544 crtc->base.base.id);
15545 drm_mode_debug_printmodeline(&crtc->base.mode);
15546 }
15547 }
15548
Daniel Vetter24929352012-07-02 20:28:59 +020015549 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015550 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015551 intel_sanitize_encoder(encoder);
15552 }
15553
Damien Lespiau055e3932014-08-18 13:49:10 +010015554 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015555 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15556 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015557 intel_dump_pipe_config(crtc, crtc->config,
15558 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015559 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015560
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015561 intel_modeset_update_connector_atomic_state(dev);
15562
Daniel Vetter35c95372013-07-17 06:55:04 +020015563 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15564 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15565
15566 if (!pll->on || pll->active)
15567 continue;
15568
15569 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15570
15571 pll->disable(dev_priv, pll);
15572 pll->on = false;
15573 }
15574
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015575 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015576 vlv_wm_get_hw_state(dev);
15577 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015578 skl_wm_get_hw_state(dev);
15579 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015580 ilk_wm_get_hw_state(dev);
15581
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015582 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015583 i915_redisable_vga(dev);
15584
Daniel Vetterf30da182013-04-11 20:22:50 +020015585 /*
15586 * We need to use raw interfaces for restoring state to avoid
15587 * checking (bogus) intermediate states.
15588 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015589 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015590 struct drm_crtc *crtc =
15591 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015592
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015593 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015594 }
15595 } else {
15596 intel_modeset_update_staged_output_state(dev);
15597 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015598
15599 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015600}
15601
15602void intel_modeset_gem_init(struct drm_device *dev)
15603{
Jesse Barnes92122782014-10-09 12:57:42 -070015604 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015605 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015606 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015607 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015608
Imre Deakae484342014-03-31 15:10:44 +030015609 mutex_lock(&dev->struct_mutex);
15610 intel_init_gt_powersave(dev);
15611 mutex_unlock(&dev->struct_mutex);
15612
Jesse Barnes92122782014-10-09 12:57:42 -070015613 /*
15614 * There may be no VBT; and if the BIOS enabled SSC we can
15615 * just keep using it to avoid unnecessary flicker. Whereas if the
15616 * BIOS isn't using it, don't assume it will work even if the VBT
15617 * indicates as much.
15618 */
15619 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15620 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15621 DREF_SSC1_ENABLE);
15622
Chris Wilson1833b132012-05-09 11:56:28 +010015623 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015624
15625 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015626
15627 /*
15628 * Make sure any fbs we allocated at startup are properly
15629 * pinned & fenced. When we do the allocation it's too early
15630 * for this.
15631 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015632 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015633 obj = intel_fb_obj(c->primary->fb);
15634 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015635 continue;
15636
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015637 mutex_lock(&dev->struct_mutex);
15638 ret = intel_pin_and_fence_fb_obj(c->primary,
15639 c->primary->fb,
15640 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015641 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015642 mutex_unlock(&dev->struct_mutex);
15643 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015644 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15645 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015646 drm_framebuffer_unreference(c->primary->fb);
15647 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015648 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015649 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015650 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015651 }
15652 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015653
15654 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015655}
15656
Imre Deak4932e2c2014-02-11 17:12:48 +020015657void intel_connector_unregister(struct intel_connector *intel_connector)
15658{
15659 struct drm_connector *connector = &intel_connector->base;
15660
15661 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015662 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015663}
15664
Jesse Barnes79e53942008-11-07 14:24:08 -080015665void intel_modeset_cleanup(struct drm_device *dev)
15666{
Jesse Barnes652c3932009-08-17 13:31:43 -070015667 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015668 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015669
Imre Deak2eb52522014-11-19 15:30:05 +020015670 intel_disable_gt_powersave(dev);
15671
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015672 intel_backlight_unregister(dev);
15673
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015674 /*
15675 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015676 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015677 * experience fancy races otherwise.
15678 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015679 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015680
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015681 /*
15682 * Due to the hpd irq storm handling the hotplug work can re-arm the
15683 * poll handlers. Hence disable polling after hpd handling is shut down.
15684 */
Keith Packardf87ea762010-10-03 19:36:26 -070015685 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015686
Jesse Barnes723bfd72010-10-07 16:01:13 -070015687 intel_unregister_dsm_handler();
15688
Paulo Zanoni7733b492015-07-07 15:26:04 -030015689 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015690
Chris Wilson1630fe72011-07-08 12:22:42 +010015691 /* flush any delayed tasks or pending work */
15692 flush_scheduled_work();
15693
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015694 /* destroy the backlight and sysfs files before encoders/connectors */
15695 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015696 struct intel_connector *intel_connector;
15697
15698 intel_connector = to_intel_connector(connector);
15699 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015700 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015701
Jesse Barnes79e53942008-11-07 14:24:08 -080015702 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015703
15704 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015705
15706 mutex_lock(&dev->struct_mutex);
15707 intel_cleanup_gt_powersave(dev);
15708 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015709}
15710
Dave Airlie28d52042009-09-21 14:33:58 +100015711/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015712 * Return which encoder is currently attached for connector.
15713 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015714struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015715{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015716 return &intel_attached_encoder(connector)->base;
15717}
Jesse Barnes79e53942008-11-07 14:24:08 -080015718
Chris Wilsondf0e9242010-09-09 16:20:55 +010015719void intel_connector_attach_encoder(struct intel_connector *connector,
15720 struct intel_encoder *encoder)
15721{
15722 connector->encoder = encoder;
15723 drm_mode_connector_attach_encoder(&connector->base,
15724 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015725}
Dave Airlie28d52042009-09-21 14:33:58 +100015726
15727/*
15728 * set vga decode state - true == enable VGA decode
15729 */
15730int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15731{
15732 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015733 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015734 u16 gmch_ctrl;
15735
Chris Wilson75fa0412014-02-07 18:37:02 -020015736 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15737 DRM_ERROR("failed to read control word\n");
15738 return -EIO;
15739 }
15740
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015741 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15742 return 0;
15743
Dave Airlie28d52042009-09-21 14:33:58 +100015744 if (state)
15745 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15746 else
15747 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015748
15749 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15750 DRM_ERROR("failed to write control word\n");
15751 return -EIO;
15752 }
15753
Dave Airlie28d52042009-09-21 14:33:58 +100015754 return 0;
15755}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015756
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015757struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015758
15759 u32 power_well_driver;
15760
Chris Wilson63b66e52013-08-08 15:12:06 +020015761 int num_transcoders;
15762
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015763 struct intel_cursor_error_state {
15764 u32 control;
15765 u32 position;
15766 u32 base;
15767 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015768 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015769
15770 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015771 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015772 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015773 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015774 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015775
15776 struct intel_plane_error_state {
15777 u32 control;
15778 u32 stride;
15779 u32 size;
15780 u32 pos;
15781 u32 addr;
15782 u32 surface;
15783 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015784 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015785
15786 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015787 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015788 enum transcoder cpu_transcoder;
15789
15790 u32 conf;
15791
15792 u32 htotal;
15793 u32 hblank;
15794 u32 hsync;
15795 u32 vtotal;
15796 u32 vblank;
15797 u32 vsync;
15798 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015799};
15800
15801struct intel_display_error_state *
15802intel_display_capture_error_state(struct drm_device *dev)
15803{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015804 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015805 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015806 int transcoders[] = {
15807 TRANSCODER_A,
15808 TRANSCODER_B,
15809 TRANSCODER_C,
15810 TRANSCODER_EDP,
15811 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015812 int i;
15813
Chris Wilson63b66e52013-08-08 15:12:06 +020015814 if (INTEL_INFO(dev)->num_pipes == 0)
15815 return NULL;
15816
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015817 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015818 if (error == NULL)
15819 return NULL;
15820
Imre Deak190be112013-11-25 17:15:31 +020015821 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015822 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15823
Damien Lespiau055e3932014-08-18 13:49:10 +010015824 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015825 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015826 __intel_display_power_is_enabled(dev_priv,
15827 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015828 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015829 continue;
15830
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015831 error->cursor[i].control = I915_READ(CURCNTR(i));
15832 error->cursor[i].position = I915_READ(CURPOS(i));
15833 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015834
15835 error->plane[i].control = I915_READ(DSPCNTR(i));
15836 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015837 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015838 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015839 error->plane[i].pos = I915_READ(DSPPOS(i));
15840 }
Paulo Zanonica291362013-03-06 20:03:14 -030015841 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15842 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015843 if (INTEL_INFO(dev)->gen >= 4) {
15844 error->plane[i].surface = I915_READ(DSPSURF(i));
15845 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15846 }
15847
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015848 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015849
Sonika Jindal3abfce72014-07-21 15:23:43 +053015850 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015851 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015852 }
15853
15854 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15855 if (HAS_DDI(dev_priv->dev))
15856 error->num_transcoders++; /* Account for eDP. */
15857
15858 for (i = 0; i < error->num_transcoders; i++) {
15859 enum transcoder cpu_transcoder = transcoders[i];
15860
Imre Deakddf9c532013-11-27 22:02:02 +020015861 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015862 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015863 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015864 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015865 continue;
15866
Chris Wilson63b66e52013-08-08 15:12:06 +020015867 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15868
15869 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15870 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15871 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15872 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15873 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15874 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15875 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015876 }
15877
15878 return error;
15879}
15880
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015881#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15882
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015883void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015884intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015885 struct drm_device *dev,
15886 struct intel_display_error_state *error)
15887{
Damien Lespiau055e3932014-08-18 13:49:10 +010015888 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015889 int i;
15890
Chris Wilson63b66e52013-08-08 15:12:06 +020015891 if (!error)
15892 return;
15893
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015894 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015895 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015896 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015897 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015898 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015899 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015900 err_printf(m, " Power: %s\n",
15901 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015902 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015903 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015904
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015905 err_printf(m, "Plane [%d]:\n", i);
15906 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15907 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015908 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015909 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15910 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015911 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015912 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015913 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015914 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015915 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15916 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015917 }
15918
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015919 err_printf(m, "Cursor [%d]:\n", i);
15920 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15921 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15922 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015923 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015924
15925 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015926 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015927 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015928 err_printf(m, " Power: %s\n",
15929 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015930 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15931 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15932 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15933 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15934 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15935 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15936 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15937 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015938}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015939
15940void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15941{
15942 struct intel_crtc *crtc;
15943
15944 for_each_intel_crtc(dev, crtc) {
15945 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015946
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015947 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015948
15949 work = crtc->unpin_work;
15950
15951 if (work && work->event &&
15952 work->event->base.file_priv == file) {
15953 kfree(work->event);
15954 work->event = NULL;
15955 }
15956
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015957 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015958 }
15959}