blob: 10a17268113844a7a42583af8d79dcb32c8d11d6 [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);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +0200112static void intel_modeset_setup_hw_state(struct drm_device *dev);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100113
Dave Airlie0e32b392014-05-02 14:02:48 +1000114static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
115{
116 if (!connector->mst_port)
117 return connector->encoder;
118 else
119 return &connector->mst_port->mst_encoders[pipe]->base;
120}
121
Jesse Barnes79e53942008-11-07 14:24:08 -0800122typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400123 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800124} intel_range_t;
125
126typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400127 int dot_limit;
128 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800129} intel_p2_t;
130
Ma Lingd4906092009-03-18 20:13:27 +0800131typedef struct intel_limit intel_limit_t;
132struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400133 intel_range_t dot, vco, n, m, m1, m2, p, p1;
134 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800135};
Jesse Barnes79e53942008-11-07 14:24:08 -0800136
Daniel Vetterd2acd212012-10-20 20:57:43 +0200137int
138intel_pch_rawclk(struct drm_device *dev)
139{
140 struct drm_i915_private *dev_priv = dev->dev_private;
141
142 WARN_ON(!HAS_PCH_SPLIT(dev));
143
144 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
145}
146
Chris Wilson021357a2010-09-07 20:54:59 +0100147static inline u32 /* units of 100MHz */
148intel_fdi_link_freq(struct drm_device *dev)
149{
Chris Wilson8b99e682010-10-13 09:59:17 +0100150 if (IS_GEN5(dev)) {
151 struct drm_i915_private *dev_priv = dev->dev_private;
152 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
153 } else
154 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100155}
156
Daniel Vetter5d536e22013-07-06 12:52:06 +0200157static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400158 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200159 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200160 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400161 .m = { .min = 96, .max = 140 },
162 .m1 = { .min = 18, .max = 26 },
163 .m2 = { .min = 6, .max = 16 },
164 .p = { .min = 4, .max = 128 },
165 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700166 .p2 = { .dot_limit = 165000,
167 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700168};
169
Daniel Vetter5d536e22013-07-06 12:52:06 +0200170static const intel_limit_t intel_limits_i8xx_dvo = {
171 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200172 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200173 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200174 .m = { .min = 96, .max = 140 },
175 .m1 = { .min = 18, .max = 26 },
176 .m2 = { .min = 6, .max = 16 },
177 .p = { .min = 4, .max = 128 },
178 .p1 = { .min = 2, .max = 33 },
179 .p2 = { .dot_limit = 165000,
180 .p2_slow = 4, .p2_fast = 4 },
181};
182
Keith Packarde4b36692009-06-05 19:22:17 -0700183static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400184 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200185 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200186 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400187 .m = { .min = 96, .max = 140 },
188 .m1 = { .min = 18, .max = 26 },
189 .m2 = { .min = 6, .max = 16 },
190 .p = { .min = 4, .max = 128 },
191 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700192 .p2 = { .dot_limit = 165000,
193 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700194};
Eric Anholt273e27c2011-03-30 13:01:10 -0700195
Keith Packarde4b36692009-06-05 19:22:17 -0700196static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400197 .dot = { .min = 20000, .max = 400000 },
198 .vco = { .min = 1400000, .max = 2800000 },
199 .n = { .min = 1, .max = 6 },
200 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100201 .m1 = { .min = 8, .max = 18 },
202 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400203 .p = { .min = 5, .max = 80 },
204 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700205 .p2 = { .dot_limit = 200000,
206 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700207};
208
209static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400210 .dot = { .min = 20000, .max = 400000 },
211 .vco = { .min = 1400000, .max = 2800000 },
212 .n = { .min = 1, .max = 6 },
213 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100214 .m1 = { .min = 8, .max = 18 },
215 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400216 .p = { .min = 7, .max = 98 },
217 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700218 .p2 = { .dot_limit = 112000,
219 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700220};
221
Eric Anholt273e27c2011-03-30 13:01:10 -0700222
Keith Packarde4b36692009-06-05 19:22:17 -0700223static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700224 .dot = { .min = 25000, .max = 270000 },
225 .vco = { .min = 1750000, .max = 3500000},
226 .n = { .min = 1, .max = 4 },
227 .m = { .min = 104, .max = 138 },
228 .m1 = { .min = 17, .max = 23 },
229 .m2 = { .min = 5, .max = 11 },
230 .p = { .min = 10, .max = 30 },
231 .p1 = { .min = 1, .max = 3},
232 .p2 = { .dot_limit = 270000,
233 .p2_slow = 10,
234 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800235 },
Keith Packarde4b36692009-06-05 19:22:17 -0700236};
237
238static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700239 .dot = { .min = 22000, .max = 400000 },
240 .vco = { .min = 1750000, .max = 3500000},
241 .n = { .min = 1, .max = 4 },
242 .m = { .min = 104, .max = 138 },
243 .m1 = { .min = 16, .max = 23 },
244 .m2 = { .min = 5, .max = 11 },
245 .p = { .min = 5, .max = 80 },
246 .p1 = { .min = 1, .max = 8},
247 .p2 = { .dot_limit = 165000,
248 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700249};
250
251static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700252 .dot = { .min = 20000, .max = 115000 },
253 .vco = { .min = 1750000, .max = 3500000 },
254 .n = { .min = 1, .max = 3 },
255 .m = { .min = 104, .max = 138 },
256 .m1 = { .min = 17, .max = 23 },
257 .m2 = { .min = 5, .max = 11 },
258 .p = { .min = 28, .max = 112 },
259 .p1 = { .min = 2, .max = 8 },
260 .p2 = { .dot_limit = 0,
261 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800262 },
Keith Packarde4b36692009-06-05 19:22:17 -0700263};
264
265static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700266 .dot = { .min = 80000, .max = 224000 },
267 .vco = { .min = 1750000, .max = 3500000 },
268 .n = { .min = 1, .max = 3 },
269 .m = { .min = 104, .max = 138 },
270 .m1 = { .min = 17, .max = 23 },
271 .m2 = { .min = 5, .max = 11 },
272 .p = { .min = 14, .max = 42 },
273 .p1 = { .min = 2, .max = 6 },
274 .p2 = { .dot_limit = 0,
275 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800276 },
Keith Packarde4b36692009-06-05 19:22:17 -0700277};
278
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500279static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400280 .dot = { .min = 20000, .max = 400000},
281 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700282 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400283 .n = { .min = 3, .max = 6 },
284 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700285 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400286 .m1 = { .min = 0, .max = 0 },
287 .m2 = { .min = 0, .max = 254 },
288 .p = { .min = 5, .max = 80 },
289 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700290 .p2 = { .dot_limit = 200000,
291 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700292};
293
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500294static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400295 .dot = { .min = 20000, .max = 400000 },
296 .vco = { .min = 1700000, .max = 3500000 },
297 .n = { .min = 3, .max = 6 },
298 .m = { .min = 2, .max = 256 },
299 .m1 = { .min = 0, .max = 0 },
300 .m2 = { .min = 0, .max = 254 },
301 .p = { .min = 7, .max = 112 },
302 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700303 .p2 = { .dot_limit = 112000,
304 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700305};
306
Eric Anholt273e27c2011-03-30 13:01:10 -0700307/* Ironlake / Sandybridge
308 *
309 * We calculate clock using (register_value + 2) for N/M1/M2, so here
310 * the range value for them is (actual_value - 2).
311 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800312static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700313 .dot = { .min = 25000, .max = 350000 },
314 .vco = { .min = 1760000, .max = 3510000 },
315 .n = { .min = 1, .max = 5 },
316 .m = { .min = 79, .max = 127 },
317 .m1 = { .min = 12, .max = 22 },
318 .m2 = { .min = 5, .max = 9 },
319 .p = { .min = 5, .max = 80 },
320 .p1 = { .min = 1, .max = 8 },
321 .p2 = { .dot_limit = 225000,
322 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700323};
324
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800325static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700326 .dot = { .min = 25000, .max = 350000 },
327 .vco = { .min = 1760000, .max = 3510000 },
328 .n = { .min = 1, .max = 3 },
329 .m = { .min = 79, .max = 118 },
330 .m1 = { .min = 12, .max = 22 },
331 .m2 = { .min = 5, .max = 9 },
332 .p = { .min = 28, .max = 112 },
333 .p1 = { .min = 2, .max = 8 },
334 .p2 = { .dot_limit = 225000,
335 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800336};
337
338static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700339 .dot = { .min = 25000, .max = 350000 },
340 .vco = { .min = 1760000, .max = 3510000 },
341 .n = { .min = 1, .max = 3 },
342 .m = { .min = 79, .max = 127 },
343 .m1 = { .min = 12, .max = 22 },
344 .m2 = { .min = 5, .max = 9 },
345 .p = { .min = 14, .max = 56 },
346 .p1 = { .min = 2, .max = 8 },
347 .p2 = { .dot_limit = 225000,
348 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800349};
350
Eric Anholt273e27c2011-03-30 13:01:10 -0700351/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800352static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700353 .dot = { .min = 25000, .max = 350000 },
354 .vco = { .min = 1760000, .max = 3510000 },
355 .n = { .min = 1, .max = 2 },
356 .m = { .min = 79, .max = 126 },
357 .m1 = { .min = 12, .max = 22 },
358 .m2 = { .min = 5, .max = 9 },
359 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400360 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700361 .p2 = { .dot_limit = 225000,
362 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800363};
364
365static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700366 .dot = { .min = 25000, .max = 350000 },
367 .vco = { .min = 1760000, .max = 3510000 },
368 .n = { .min = 1, .max = 3 },
369 .m = { .min = 79, .max = 126 },
370 .m1 = { .min = 12, .max = 22 },
371 .m2 = { .min = 5, .max = 9 },
372 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400373 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700374 .p2 = { .dot_limit = 225000,
375 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800376};
377
Ville Syrjälädc730512013-09-24 21:26:30 +0300378static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300379 /*
380 * These are the data rate limits (measured in fast clocks)
381 * since those are the strictest limits we have. The fast
382 * clock and actual rate limits are more relaxed, so checking
383 * them would make no difference.
384 */
385 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200386 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700387 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700388 .m1 = { .min = 2, .max = 3 },
389 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300390 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300391 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700392};
393
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300394static const intel_limit_t intel_limits_chv = {
395 /*
396 * These are the data rate limits (measured in fast clocks)
397 * since those are the strictest limits we have. The fast
398 * clock and actual rate limits are more relaxed, so checking
399 * them would make no difference.
400 */
401 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200402 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300403 .n = { .min = 1, .max = 1 },
404 .m1 = { .min = 2, .max = 2 },
405 .m2 = { .min = 24 << 22, .max = 175 << 22 },
406 .p1 = { .min = 2, .max = 4 },
407 .p2 = { .p2_slow = 1, .p2_fast = 14 },
408};
409
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200410static const intel_limit_t intel_limits_bxt = {
411 /* FIXME: find real dot limits */
412 .dot = { .min = 0, .max = INT_MAX },
Vandana Kannane6292552015-07-01 17:02:57 +0530413 .vco = { .min = 4800000, .max = 6700000 },
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200414 .n = { .min = 1, .max = 1 },
415 .m1 = { .min = 2, .max = 2 },
416 /* FIXME: find real m2 limits */
417 .m2 = { .min = 2 << 22, .max = 255 << 22 },
418 .p1 = { .min = 2, .max = 4 },
419 .p2 = { .p2_slow = 1, .p2_fast = 20 },
420};
421
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200422static bool
423needs_modeset(struct drm_crtc_state *state)
424{
425 return state->mode_changed || state->active_changed;
426}
427
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300428/**
429 * Returns whether any output on the specified pipe is of the specified type
430 */
Damien Lespiau40935612014-10-29 11:16:59 +0000431bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300432{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300433 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300434 struct intel_encoder *encoder;
435
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300436 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300437 if (encoder->type == type)
438 return true;
439
440 return false;
441}
442
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200443/**
444 * Returns whether any output on the specified pipe will have the specified
445 * type after a staged modeset is complete, i.e., the same as
446 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
447 * encoder->crtc.
448 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200449static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
450 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200451{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200452 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300453 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200454 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200455 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200456 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300458 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200459 if (connector_state->crtc != crtc_state->base.crtc)
460 continue;
461
462 num_connectors++;
463
464 encoder = to_intel_encoder(connector_state->best_encoder);
465 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200466 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200467 }
468
469 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200470
471 return false;
472}
473
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200474static const intel_limit_t *
475intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800476{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200477 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800478 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800479
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200480 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100481 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000482 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800483 limit = &intel_limits_ironlake_dual_lvds_100m;
484 else
485 limit = &intel_limits_ironlake_dual_lvds;
486 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000487 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800488 limit = &intel_limits_ironlake_single_lvds_100m;
489 else
490 limit = &intel_limits_ironlake_single_lvds;
491 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200492 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800493 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800494
495 return limit;
496}
497
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200498static const intel_limit_t *
499intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800500{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200501 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800502 const intel_limit_t *limit;
503
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200504 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100505 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700506 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800507 else
Keith Packarde4b36692009-06-05 19:22:17 -0700508 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200509 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
510 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700511 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200512 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700513 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800514 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700515 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800516
517 return limit;
518}
519
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200520static const intel_limit_t *
521intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800522{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200523 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800524 const intel_limit_t *limit;
525
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200526 if (IS_BROXTON(dev))
527 limit = &intel_limits_bxt;
528 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200529 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800530 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200531 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500532 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200533 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500534 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800535 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500536 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300537 } else if (IS_CHERRYVIEW(dev)) {
538 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700539 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300540 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100541 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200542 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100543 limit = &intel_limits_i9xx_lvds;
544 else
545 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800546 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200547 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700548 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200549 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700550 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200551 else
552 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800553 }
554 return limit;
555}
556
Imre Deakdccbea32015-06-22 23:35:51 +0300557/*
558 * Platform specific helpers to calculate the port PLL loopback- (clock.m),
559 * and post-divider (clock.p) values, pre- (clock.vco) and post-divided fast
560 * (clock.dot) clock rates. This fast dot clock is fed to the port's IO logic.
561 * The helpers' return value is the rate of the clock that is fed to the
562 * display engine's pipe which can be the above fast dot clock rate or a
563 * divided-down version of it.
564 */
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500565/* m1 is reserved as 0 in Pineview, n is a ring counter */
Imre Deakdccbea32015-06-22 23:35:51 +0300566static int pnv_calc_dpll_params(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800567{
Shaohua Li21778322009-02-23 15:19:16 +0800568 clock->m = clock->m2 + 2;
569 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200570 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300571 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300572 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
573 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300574
575 return clock->dot;
Shaohua Li21778322009-02-23 15:19:16 +0800576}
577
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200578static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
579{
580 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
581}
582
Imre Deakdccbea32015-06-22 23:35:51 +0300583static int i9xx_calc_dpll_params(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800584{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200585 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800586 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200587 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300588 return 0;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300589 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
590 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300591
592 return clock->dot;
Jesse Barnes79e53942008-11-07 14:24:08 -0800593}
594
Imre Deakdccbea32015-06-22 23:35:51 +0300595static int vlv_calc_dpll_params(int refclk, intel_clock_t *clock)
Imre Deak589eca62015-06-22 23:35:50 +0300596{
597 clock->m = clock->m1 * clock->m2;
598 clock->p = clock->p1 * clock->p2;
599 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300600 return 0;
Imre Deak589eca62015-06-22 23:35:50 +0300601 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
602 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300603
604 return clock->dot / 5;
Imre Deak589eca62015-06-22 23:35:50 +0300605}
606
Imre Deakdccbea32015-06-22 23:35:51 +0300607int chv_calc_dpll_params(int refclk, intel_clock_t *clock)
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300608{
609 clock->m = clock->m1 * clock->m2;
610 clock->p = clock->p1 * clock->p2;
611 if (WARN_ON(clock->n == 0 || clock->p == 0))
Imre Deakdccbea32015-06-22 23:35:51 +0300612 return 0;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300613 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
614 clock->n << 22);
615 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Imre Deakdccbea32015-06-22 23:35:51 +0300616
617 return clock->dot / 5;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300618}
619
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800620#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800621/**
622 * Returns whether the given set of divisors are valid for a given refclk with
623 * the given connectors.
624 */
625
Chris Wilson1b894b52010-12-14 20:04:54 +0000626static bool intel_PLL_is_valid(struct drm_device *dev,
627 const intel_limit_t *limit,
628 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800629{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300630 if (clock->n < limit->n.min || limit->n.max < clock->n)
631 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400633 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800634 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400635 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400637 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300638
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200639 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300640 if (clock->m1 <= clock->m2)
641 INTELPllInvalid("m1 <= m2\n");
642
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200643 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300644 if (clock->p < limit->p.min || limit->p.max < clock->p)
645 INTELPllInvalid("p out of range\n");
646 if (clock->m < limit->m.min || limit->m.max < clock->m)
647 INTELPllInvalid("m out of range\n");
648 }
649
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400651 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800652 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
653 * connector, etc., rather than just a single range.
654 */
655 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400656 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800657
658 return true;
659}
660
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300661static int
662i9xx_select_p2_div(const intel_limit_t *limit,
663 const struct intel_crtc_state *crtc_state,
664 int target)
Jesse Barnes79e53942008-11-07 14:24:08 -0800665{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300666 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800667
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200668 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800669 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100670 * For LVDS just rely on its current settings for dual-channel.
671 * We haven't figured out how to reliably set up different
672 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800673 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100674 if (intel_is_dual_link_lvds(dev))
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300675 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800676 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300677 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800678 } else {
679 if (target < limit->p2.dot_limit)
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300680 return limit->p2.p2_slow;
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 else
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300682 return limit->p2.p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800683 }
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300684}
685
686static bool
687i9xx_find_best_dpll(const intel_limit_t *limit,
688 struct intel_crtc_state *crtc_state,
689 int target, int refclk, intel_clock_t *match_clock,
690 intel_clock_t *best_clock)
691{
692 struct drm_device *dev = crtc_state->base.crtc->dev;
693 intel_clock_t clock;
694 int err = target;
Jesse Barnes79e53942008-11-07 14:24:08 -0800695
Akshay Joshi0206e352011-08-16 15:34:10 -0400696 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800697
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300698 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
699
Zhao Yakui42158662009-11-20 11:24:18 +0800700 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
701 clock.m1++) {
702 for (clock.m2 = limit->m2.min;
703 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200704 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800705 break;
706 for (clock.n = limit->n.min;
707 clock.n <= limit->n.max; clock.n++) {
708 for (clock.p1 = limit->p1.min;
709 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800710 int this_err;
711
Imre Deakdccbea32015-06-22 23:35:51 +0300712 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000713 if (!intel_PLL_is_valid(dev, limit,
714 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800715 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800716 if (match_clock &&
717 clock.p != match_clock->p)
718 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800719
720 this_err = abs(clock.dot - target);
721 if (this_err < err) {
722 *best_clock = clock;
723 err = this_err;
724 }
725 }
726 }
727 }
728 }
729
730 return (err != target);
731}
732
Ma Lingd4906092009-03-18 20:13:27 +0800733static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200734pnv_find_best_dpll(const intel_limit_t *limit,
735 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200736 int target, int refclk, intel_clock_t *match_clock,
737 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300739 struct drm_device *dev = crtc_state->base.crtc->dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200740 intel_clock_t clock;
741 int err = target;
742
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200743 memset(best_clock, 0, sizeof(*best_clock));
744
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300745 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
746
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200747 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
748 clock.m1++) {
749 for (clock.m2 = limit->m2.min;
750 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200751 for (clock.n = limit->n.min;
752 clock.n <= limit->n.max; clock.n++) {
753 for (clock.p1 = limit->p1.min;
754 clock.p1 <= limit->p1.max; clock.p1++) {
755 int this_err;
756
Imre Deakdccbea32015-06-22 23:35:51 +0300757 pnv_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800758 if (!intel_PLL_is_valid(dev, limit,
759 &clock))
760 continue;
761 if (match_clock &&
762 clock.p != match_clock->p)
763 continue;
764
765 this_err = abs(clock.dot - target);
766 if (this_err < err) {
767 *best_clock = clock;
768 err = this_err;
769 }
770 }
771 }
772 }
773 }
774
775 return (err != target);
776}
777
Ma Lingd4906092009-03-18 20:13:27 +0800778static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200779g4x_find_best_dpll(const intel_limit_t *limit,
780 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200781 int target, int refclk, intel_clock_t *match_clock,
782 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800783{
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300784 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Lingd4906092009-03-18 20:13:27 +0800785 intel_clock_t clock;
786 int max_n;
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300787 bool found = false;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400788 /* approximately equals target * 0.00585 */
789 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800790
791 memset(best_clock, 0, sizeof(*best_clock));
Ville Syrjälä3b1429d2015-06-18 13:47:22 +0300792
793 clock.p2 = i9xx_select_p2_div(limit, crtc_state, target);
794
Ma Lingd4906092009-03-18 20:13:27 +0800795 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200796 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800797 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200798 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800799 for (clock.m1 = limit->m1.max;
800 clock.m1 >= limit->m1.min; clock.m1--) {
801 for (clock.m2 = limit->m2.max;
802 clock.m2 >= limit->m2.min; clock.m2--) {
803 for (clock.p1 = limit->p1.max;
804 clock.p1 >= limit->p1.min; clock.p1--) {
805 int this_err;
806
Imre Deakdccbea32015-06-22 23:35:51 +0300807 i9xx_calc_dpll_params(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000808 if (!intel_PLL_is_valid(dev, limit,
809 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800810 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000811
812 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800813 if (this_err < err_most) {
814 *best_clock = clock;
815 err_most = this_err;
816 max_n = clock.n;
817 found = true;
818 }
819 }
820 }
821 }
822 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800823 return found;
824}
Ma Lingd4906092009-03-18 20:13:27 +0800825
Imre Deakd5dd62b2015-03-17 11:40:03 +0200826/*
827 * Check if the calculated PLL configuration is more optimal compared to the
828 * best configuration and error found so far. Return the calculated error.
829 */
830static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
831 const intel_clock_t *calculated_clock,
832 const intel_clock_t *best_clock,
833 unsigned int best_error_ppm,
834 unsigned int *error_ppm)
835{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200836 /*
837 * For CHV ignore the error and consider only the P value.
838 * Prefer a bigger P value based on HW requirements.
839 */
840 if (IS_CHERRYVIEW(dev)) {
841 *error_ppm = 0;
842
843 return calculated_clock->p > best_clock->p;
844 }
845
Imre Deak24be4e42015-03-17 11:40:04 +0200846 if (WARN_ON_ONCE(!target_freq))
847 return false;
848
Imre Deakd5dd62b2015-03-17 11:40:03 +0200849 *error_ppm = div_u64(1000000ULL *
850 abs(target_freq - calculated_clock->dot),
851 target_freq);
852 /*
853 * Prefer a better P value over a better (smaller) error if the error
854 * is small. Ensure this preference for future configurations too by
855 * setting the error to 0.
856 */
857 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
858 *error_ppm = 0;
859
860 return true;
861 }
862
863 return *error_ppm + 10 < best_error_ppm;
864}
865
Zhenyu Wang2c072452009-06-05 15:38:42 +0800866static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200867vlv_find_best_dpll(const intel_limit_t *limit,
868 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200869 int target, int refclk, intel_clock_t *match_clock,
870 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700871{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200872 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300873 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300874 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300875 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300876 /* min update 19.2 MHz */
877 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300878 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700879
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300880 target *= 5; /* fast clock */
881
882 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700883
884 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300885 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300886 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300887 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300888 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300889 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700890 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300891 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200892 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300893
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300894 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
895 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300896
Imre Deakdccbea32015-06-22 23:35:51 +0300897 vlv_calc_dpll_params(refclk, &clock);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300898
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300899 if (!intel_PLL_is_valid(dev, limit,
900 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300901 continue;
902
Imre Deakd5dd62b2015-03-17 11:40:03 +0200903 if (!vlv_PLL_is_optimal(dev, target,
904 &clock,
905 best_clock,
906 bestppm, &ppm))
907 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300908
Imre Deakd5dd62b2015-03-17 11:40:03 +0200909 *best_clock = clock;
910 bestppm = ppm;
911 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700912 }
913 }
914 }
915 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700916
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300917 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700918}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700919
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300920static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200921chv_find_best_dpll(const intel_limit_t *limit,
922 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300923 int target, int refclk, intel_clock_t *match_clock,
924 intel_clock_t *best_clock)
925{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200926 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300927 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200928 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300929 intel_clock_t clock;
930 uint64_t m2;
931 int found = false;
932
933 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200934 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300935
936 /*
937 * Based on hardware doc, the n always set to 1, and m1 always
938 * set to 2. If requires to support 200Mhz refclk, we need to
939 * revisit this because n may not 1 anymore.
940 */
941 clock.n = 1, clock.m1 = 2;
942 target *= 5; /* fast clock */
943
944 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
945 for (clock.p2 = limit->p2.p2_fast;
946 clock.p2 >= limit->p2.p2_slow;
947 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200948 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300949
950 clock.p = clock.p1 * clock.p2;
951
952 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
953 clock.n) << 22, refclk * clock.m1);
954
955 if (m2 > INT_MAX/clock.m1)
956 continue;
957
958 clock.m2 = m2;
959
Imre Deakdccbea32015-06-22 23:35:51 +0300960 chv_calc_dpll_params(refclk, &clock);
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300961
962 if (!intel_PLL_is_valid(dev, limit, &clock))
963 continue;
964
Imre Deak9ca3ba02015-03-17 11:40:05 +0200965 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
966 best_error_ppm, &error_ppm))
967 continue;
968
969 *best_clock = clock;
970 best_error_ppm = error_ppm;
971 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300972 }
973 }
974
975 return found;
976}
977
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200978bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
979 intel_clock_t *best_clock)
980{
981 int refclk = i9xx_get_refclk(crtc_state, 0);
982
983 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
984 target_clock, refclk, NULL, best_clock);
985}
986
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300987bool intel_crtc_active(struct drm_crtc *crtc)
988{
989 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
990
991 /* Be paranoid as we can arrive here with only partial
992 * state retrieved from the hardware during setup.
993 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100994 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300995 * as Haswell has gained clock readout/fastboot support.
996 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000997 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300998 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700999 *
1000 * FIXME: The intel_crtc->active here should be switched to
1001 * crtc->state->active once we have proper CRTC states wired up
1002 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001003 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001004 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001005 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001006}
1007
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001008enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1009 enum pipe pipe)
1010{
1011 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1013
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001014 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001015}
1016
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001017static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1018{
1019 struct drm_i915_private *dev_priv = dev->dev_private;
1020 u32 reg = PIPEDSL(pipe);
1021 u32 line1, line2;
1022 u32 line_mask;
1023
1024 if (IS_GEN2(dev))
1025 line_mask = DSL_LINEMASK_GEN2;
1026 else
1027 line_mask = DSL_LINEMASK_GEN3;
1028
1029 line1 = I915_READ(reg) & line_mask;
Daniel Vetter6adfb1e2015-07-07 09:10:40 +02001030 msleep(5);
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001031 line2 = I915_READ(reg) & line_mask;
1032
1033 return line1 == line2;
1034}
1035
Keith Packardab7ad7f2010-10-03 00:33:06 -07001036/*
1037 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001038 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001039 *
1040 * After disabling a pipe, we can't wait for vblank in the usual way,
1041 * spinning on the vblank interrupt status bit, since we won't actually
1042 * see an interrupt when the pipe is disabled.
1043 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001044 * On Gen4 and above:
1045 * wait for the pipe register state bit to turn off
1046 *
1047 * Otherwise:
1048 * wait for the display line value to settle (it usually
1049 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001050 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001051 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001052static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001053{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001054 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001055 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001056 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001057 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001058
Keith Packardab7ad7f2010-10-03 00:33:06 -07001059 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001060 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001061
Keith Packardab7ad7f2010-10-03 00:33:06 -07001062 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001063 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1064 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001065 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001066 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001067 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001068 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001069 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001070 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001071}
1072
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001073/*
1074 * ibx_digital_port_connected - is the specified port connected?
1075 * @dev_priv: i915 private structure
1076 * @port: the port to test
1077 *
1078 * Returns true if @port is connected, false otherwise.
1079 */
1080bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1081 struct intel_digital_port *port)
1082{
1083 u32 bit;
1084
Damien Lespiauc36346e2012-12-13 16:09:03 +00001085 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001086 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001087 case PORT_B:
1088 bit = SDE_PORTB_HOTPLUG;
1089 break;
1090 case PORT_C:
1091 bit = SDE_PORTC_HOTPLUG;
1092 break;
1093 case PORT_D:
1094 bit = SDE_PORTD_HOTPLUG;
1095 break;
1096 default:
1097 return true;
1098 }
1099 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001100 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001101 case PORT_B:
1102 bit = SDE_PORTB_HOTPLUG_CPT;
1103 break;
1104 case PORT_C:
1105 bit = SDE_PORTC_HOTPLUG_CPT;
1106 break;
1107 case PORT_D:
1108 bit = SDE_PORTD_HOTPLUG_CPT;
1109 break;
1110 default:
1111 return true;
1112 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001113 }
1114
1115 return I915_READ(SDEISR) & bit;
1116}
1117
Jesse Barnesb24e7172011-01-04 15:09:30 -08001118static const char *state_string(bool enabled)
1119{
1120 return enabled ? "on" : "off";
1121}
1122
1123/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001124void assert_pll(struct drm_i915_private *dev_priv,
1125 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001126{
1127 int reg;
1128 u32 val;
1129 bool cur_state;
1130
1131 reg = DPLL(pipe);
1132 val = I915_READ(reg);
1133 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001134 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001135 "PLL state assertion failure (expected %s, current %s)\n",
1136 state_string(state), state_string(cur_state));
1137}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001138
Jani Nikula23538ef2013-08-27 15:12:22 +03001139/* XXX: the dsi pll is shared between MIPI DSI ports */
1140static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1141{
1142 u32 val;
1143 bool cur_state;
1144
Ville Syrjäläa5805162015-05-26 20:42:30 +03001145 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001146 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001147 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001148
1149 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001150 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001151 "DSI PLL state assertion failure (expected %s, current %s)\n",
1152 state_string(state), state_string(cur_state));
1153}
1154#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1155#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1156
Daniel Vetter55607e82013-06-16 21:42:39 +02001157struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001158intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001159{
Daniel Vettere2b78262013-06-07 23:10:03 +02001160 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1161
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001162 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001163 return NULL;
1164
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001165 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001166}
1167
Jesse Barnesb24e7172011-01-04 15:09:30 -08001168/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001169void assert_shared_dpll(struct drm_i915_private *dev_priv,
1170 struct intel_shared_dpll *pll,
1171 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001172{
Jesse Barnes040484a2011-01-03 12:14:26 -08001173 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001174 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001175
Chris Wilson92b27b02012-05-20 18:10:50 +01001176 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001177 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001178 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001179
Daniel Vetter53589012013-06-05 13:34:16 +02001180 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001181 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001182 "%s assertion failure (expected %s, current %s)\n",
1183 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001184}
Jesse Barnes040484a2011-01-03 12:14:26 -08001185
1186static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1187 enum pipe pipe, bool state)
1188{
1189 int reg;
1190 u32 val;
1191 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001192 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1193 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001194
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001195 if (HAS_DDI(dev_priv->dev)) {
1196 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001197 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001198 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001199 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001200 } else {
1201 reg = FDI_TX_CTL(pipe);
1202 val = I915_READ(reg);
1203 cur_state = !!(val & FDI_TX_ENABLE);
1204 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001205 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001206 "FDI TX state assertion failure (expected %s, current %s)\n",
1207 state_string(state), state_string(cur_state));
1208}
1209#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1210#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1211
1212static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1213 enum pipe pipe, bool state)
1214{
1215 int reg;
1216 u32 val;
1217 bool cur_state;
1218
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001219 reg = FDI_RX_CTL(pipe);
1220 val = I915_READ(reg);
1221 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001222 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001223 "FDI RX state assertion failure (expected %s, current %s)\n",
1224 state_string(state), state_string(cur_state));
1225}
1226#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1227#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1228
1229static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1230 enum pipe pipe)
1231{
1232 int reg;
1233 u32 val;
1234
1235 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001236 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001237 return;
1238
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001239 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001240 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001241 return;
1242
Jesse Barnes040484a2011-01-03 12:14:26 -08001243 reg = FDI_TX_CTL(pipe);
1244 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001245 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 -08001246}
1247
Daniel Vetter55607e82013-06-16 21:42:39 +02001248void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1249 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001250{
1251 int reg;
1252 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001253 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001254
1255 reg = FDI_RX_CTL(pipe);
1256 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001257 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001258 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001259 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1260 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001261}
1262
Daniel Vetterb680c372014-09-19 18:27:27 +02001263void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1264 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001265{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001266 struct drm_device *dev = dev_priv->dev;
1267 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001268 u32 val;
1269 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001270 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001271
Jani Nikulabedd4db2014-08-22 15:04:13 +03001272 if (WARN_ON(HAS_DDI(dev)))
1273 return;
1274
1275 if (HAS_PCH_SPLIT(dev)) {
1276 u32 port_sel;
1277
Jesse Barnesea0760c2011-01-04 15:09:32 -08001278 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001279 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1280
1281 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1282 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1283 panel_pipe = PIPE_B;
1284 /* XXX: else fix for eDP */
1285 } else if (IS_VALLEYVIEW(dev)) {
1286 /* presumably write lock depends on pipe, not port select */
1287 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1288 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001289 } else {
1290 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001291 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1292 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001293 }
1294
1295 val = I915_READ(pp_reg);
1296 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001297 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001298 locked = false;
1299
Rob Clarke2c719b2014-12-15 13:56:32 -05001300 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001302 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001303}
1304
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001305static void assert_cursor(struct drm_i915_private *dev_priv,
1306 enum pipe pipe, bool state)
1307{
1308 struct drm_device *dev = dev_priv->dev;
1309 bool cur_state;
1310
Paulo Zanonid9d82082014-02-27 16:30:56 -03001311 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001312 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001313 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001314 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001315
Rob Clarke2c719b2014-12-15 13:56:32 -05001316 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001317 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1318 pipe_name(pipe), state_string(state), state_string(cur_state));
1319}
1320#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1321#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1322
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001323void assert_pipe(struct drm_i915_private *dev_priv,
1324 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001325{
1326 int reg;
1327 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001328 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001329 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1330 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001331
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001332 /* if we need the pipe quirk it must be always on */
1333 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1334 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001335 state = true;
1336
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001337 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001338 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001339 cur_state = false;
1340 } else {
1341 reg = PIPECONF(cpu_transcoder);
1342 val = I915_READ(reg);
1343 cur_state = !!(val & PIPECONF_ENABLE);
1344 }
1345
Rob Clarke2c719b2014-12-15 13:56:32 -05001346 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001347 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001348 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001349}
1350
Chris Wilson931872f2012-01-16 23:01:13 +00001351static void assert_plane(struct drm_i915_private *dev_priv,
1352 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353{
1354 int reg;
1355 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001356 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357
1358 reg = DSPCNTR(plane);
1359 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001360 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001361 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001362 "plane %c assertion failure (expected %s, current %s)\n",
1363 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001364}
1365
Chris Wilson931872f2012-01-16 23:01:13 +00001366#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1367#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1368
Jesse Barnesb24e7172011-01-04 15:09:30 -08001369static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1370 enum pipe pipe)
1371{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001372 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001373 int reg, i;
1374 u32 val;
1375 int cur_pipe;
1376
Ville Syrjälä653e1022013-06-04 13:49:05 +03001377 /* Primary planes are fixed to pipes on gen4+ */
1378 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001379 reg = DSPCNTR(pipe);
1380 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001381 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001382 "plane %c assertion failure, should be disabled but not\n",
1383 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001384 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001385 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001386
Jesse Barnesb24e7172011-01-04 15:09:30 -08001387 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001388 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001389 reg = DSPCNTR(i);
1390 val = I915_READ(reg);
1391 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1392 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001393 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001394 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1395 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001396 }
1397}
1398
Jesse Barnes19332d72013-03-28 09:55:38 -07001399static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1400 enum pipe pipe)
1401{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001402 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001403 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001404 u32 val;
1405
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001407 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001408 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001409 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001410 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1411 sprite, pipe_name(pipe));
1412 }
1413 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001414 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001415 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001416 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001417 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001418 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001419 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001420 }
1421 } else if (INTEL_INFO(dev)->gen >= 7) {
1422 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001423 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001424 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001425 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001426 plane_name(pipe), pipe_name(pipe));
1427 } else if (INTEL_INFO(dev)->gen >= 5) {
1428 reg = DVSCNTR(pipe);
1429 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001430 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001431 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1432 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001433 }
1434}
1435
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001436static void assert_vblank_disabled(struct drm_crtc *crtc)
1437{
Rob Clarke2c719b2014-12-15 13:56:32 -05001438 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001439 drm_crtc_vblank_put(crtc);
1440}
1441
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001442static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001443{
1444 u32 val;
1445 bool enabled;
1446
Rob Clarke2c719b2014-12-15 13:56:32 -05001447 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001448
Jesse Barnes92f25842011-01-04 15:09:34 -08001449 val = I915_READ(PCH_DREF_CONTROL);
1450 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1451 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001452 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001453}
1454
Daniel Vetterab9412b2013-05-03 11:49:46 +02001455static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1456 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001457{
1458 int reg;
1459 u32 val;
1460 bool enabled;
1461
Daniel Vetterab9412b2013-05-03 11:49:46 +02001462 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001463 val = I915_READ(reg);
1464 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001465 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001466 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1467 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001468}
1469
Keith Packard4e634382011-08-06 10:39:45 -07001470static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1471 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001472{
1473 if ((val & DP_PORT_EN) == 0)
1474 return false;
1475
1476 if (HAS_PCH_CPT(dev_priv->dev)) {
1477 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1478 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1479 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1480 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001481 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1482 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1483 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001484 } else {
1485 if ((val & DP_PIPE_MASK) != (pipe << 30))
1486 return false;
1487 }
1488 return true;
1489}
1490
Keith Packard1519b992011-08-06 10:35:34 -07001491static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1492 enum pipe pipe, u32 val)
1493{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001494 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001495 return false;
1496
1497 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001498 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001499 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001500 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1501 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1502 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001503 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001504 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001505 return false;
1506 }
1507 return true;
1508}
1509
1510static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1511 enum pipe pipe, u32 val)
1512{
1513 if ((val & LVDS_PORT_EN) == 0)
1514 return false;
1515
1516 if (HAS_PCH_CPT(dev_priv->dev)) {
1517 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1518 return false;
1519 } else {
1520 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1521 return false;
1522 }
1523 return true;
1524}
1525
1526static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1527 enum pipe pipe, u32 val)
1528{
1529 if ((val & ADPA_DAC_ENABLE) == 0)
1530 return false;
1531 if (HAS_PCH_CPT(dev_priv->dev)) {
1532 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1533 return false;
1534 } else {
1535 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1536 return false;
1537 }
1538 return true;
1539}
1540
Jesse Barnes291906f2011-02-02 12:28:03 -08001541static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001542 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001543{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001544 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001545 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001546 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001547 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001548
Rob Clarke2c719b2014-12-15 13:56:32 -05001549 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001550 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001551 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001552}
1553
1554static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1555 enum pipe pipe, int reg)
1556{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001557 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001558 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001559 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001560 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001561
Rob Clarke2c719b2014-12-15 13:56:32 -05001562 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001563 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001564 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001565}
1566
1567static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1568 enum pipe pipe)
1569{
1570 int reg;
1571 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001572
Keith Packardf0575e92011-07-25 22:12:43 -07001573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1574 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1575 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001576
1577 reg = PCH_ADPA;
1578 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001579 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001580 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001581 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001582
1583 reg = PCH_LVDS;
1584 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001585 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001586 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001587 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001588
Paulo Zanonie2debe92013-02-18 19:00:27 -03001589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1590 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1591 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001592}
1593
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001594static void intel_init_dpio(struct drm_device *dev)
1595{
1596 struct drm_i915_private *dev_priv = dev->dev_private;
1597
1598 if (!IS_VALLEYVIEW(dev))
1599 return;
1600
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001601 /*
1602 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1603 * CHV x1 PHY (DP/HDMI D)
1604 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1605 */
1606 if (IS_CHERRYVIEW(dev)) {
1607 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1609 } else {
1610 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1611 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001612}
1613
Ville Syrjäläd288f652014-10-28 13:20:22 +02001614static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001615 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001616{
Daniel Vetter426115c2013-07-11 22:13:42 +02001617 struct drm_device *dev = crtc->base.dev;
1618 struct drm_i915_private *dev_priv = dev->dev_private;
1619 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001620 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001621
Daniel Vetter426115c2013-07-11 22:13:42 +02001622 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001623
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001624 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001625 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1626
1627 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001628 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001629 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001630
Daniel Vetter426115c2013-07-11 22:13:42 +02001631 I915_WRITE(reg, dpll);
1632 POSTING_READ(reg);
1633 udelay(150);
1634
1635 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1636 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1637
Ville Syrjäläd288f652014-10-28 13:20:22 +02001638 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001639 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001640
1641 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001642 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001643 POSTING_READ(reg);
1644 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001645 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001646 POSTING_READ(reg);
1647 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001648 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001649 POSTING_READ(reg);
1650 udelay(150); /* wait for warmup */
1651}
1652
Ville Syrjäläd288f652014-10-28 13:20:22 +02001653static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001654 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001655{
1656 struct drm_device *dev = crtc->base.dev;
1657 struct drm_i915_private *dev_priv = dev->dev_private;
1658 int pipe = crtc->pipe;
1659 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001660 u32 tmp;
1661
1662 assert_pipe_disabled(dev_priv, crtc->pipe);
1663
1664 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1665
Ville Syrjäläa5805162015-05-26 20:42:30 +03001666 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001667
1668 /* Enable back the 10bit clock to display controller */
1669 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1670 tmp |= DPIO_DCLKP_EN;
1671 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1672
Ville Syrjälä54433e92015-05-26 20:42:31 +03001673 mutex_unlock(&dev_priv->sb_lock);
1674
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001675 /*
1676 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1677 */
1678 udelay(1);
1679
1680 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001681 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001682
1683 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001684 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001685 DRM_ERROR("PLL %d failed to lock\n", pipe);
1686
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001687 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001688 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001689 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001690}
1691
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001692static int intel_num_dvo_pipes(struct drm_device *dev)
1693{
1694 struct intel_crtc *crtc;
1695 int count = 0;
1696
1697 for_each_intel_crtc(dev, crtc)
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001698 count += crtc->base.state->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001699 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001700
1701 return count;
1702}
1703
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001704static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001705{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001706 struct drm_device *dev = crtc->base.dev;
1707 struct drm_i915_private *dev_priv = dev->dev_private;
1708 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001709 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001710
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001711 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001712
1713 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001714 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001715
1716 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001717 if (IS_MOBILE(dev) && !IS_I830(dev))
1718 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001719
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001720 /* Enable DVO 2x clock on both PLLs if necessary */
1721 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1722 /*
1723 * It appears to be important that we don't enable this
1724 * for the current pipe before otherwise configuring the
1725 * PLL. No idea how this should be handled if multiple
1726 * DVO outputs are enabled simultaneosly.
1727 */
1728 dpll |= DPLL_DVO_2X_MODE;
1729 I915_WRITE(DPLL(!crtc->pipe),
1730 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1731 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001732
1733 /* Wait for the clocks to stabilize. */
1734 POSTING_READ(reg);
1735 udelay(150);
1736
1737 if (INTEL_INFO(dev)->gen >= 4) {
1738 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001739 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001740 } else {
1741 /* The pixel multiplier can only be updated once the
1742 * DPLL is enabled and the clocks are stable.
1743 *
1744 * So write it again.
1745 */
1746 I915_WRITE(reg, dpll);
1747 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001748
1749 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001750 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001751 POSTING_READ(reg);
1752 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001753 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001754 POSTING_READ(reg);
1755 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001756 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001757 POSTING_READ(reg);
1758 udelay(150); /* wait for warmup */
1759}
1760
1761/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001762 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001763 * @dev_priv: i915 private structure
1764 * @pipe: pipe PLL to disable
1765 *
1766 * Disable the PLL for @pipe, making sure the pipe is off first.
1767 *
1768 * Note! This is for pre-ILK only.
1769 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001770static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001771{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001772 struct drm_device *dev = crtc->base.dev;
1773 struct drm_i915_private *dev_priv = dev->dev_private;
1774 enum pipe pipe = crtc->pipe;
1775
1776 /* Disable DVO 2x clock on both PLLs if necessary */
1777 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001778 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Maarten Lankhorst3538b9d2015-06-01 12:50:10 +02001779 !intel_num_dvo_pipes(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001780 I915_WRITE(DPLL(PIPE_B),
1781 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1782 I915_WRITE(DPLL(PIPE_A),
1783 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1784 }
1785
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001786 /* Don't disable pipe or pipe PLLs if needed */
1787 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1788 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001789 return;
1790
1791 /* Make sure the pipe isn't still relying on us */
1792 assert_pipe_disabled(dev_priv, pipe);
1793
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001794 I915_WRITE(DPLL(pipe), DPLL_VGA_MODE_DIS);
Daniel Vetter50b44a42013-06-05 13:34:33 +02001795 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001796}
1797
Jesse Barnesf6071162013-10-01 10:41:38 -07001798static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1799{
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001800 u32 val;
Jesse Barnesf6071162013-10-01 10:41:38 -07001801
1802 /* Make sure the pipe isn't still relying on us */
1803 assert_pipe_disabled(dev_priv, pipe);
1804
Imre Deake5cbfbf2014-01-09 17:08:16 +02001805 /*
1806 * Leave integrated clock source and reference clock enabled for pipe B.
1807 * The latter is needed for VGA hotplug / manual detection.
1808 */
Ville Syrjäläb8afb912015-06-29 15:25:48 +03001809 val = DPLL_VGA_MODE_DIS;
Jesse Barnesf6071162013-10-01 10:41:38 -07001810 if (pipe == PIPE_B)
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001811 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REF_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001812 I915_WRITE(DPLL(pipe), val);
1813 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001814
1815}
1816
1817static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1818{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001819 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001820 u32 val;
1821
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001822 /* Make sure the pipe isn't still relying on us */
1823 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001824
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001825 /* Set PLL en = 0 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03001826 val = DPLL_SSC_REF_CLK_CHV |
1827 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001828 if (pipe != PIPE_A)
1829 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1830 I915_WRITE(DPLL(pipe), val);
1831 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001832
Ville Syrjäläa5805162015-05-26 20:42:30 +03001833 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001834
1835 /* Disable 10bit clock to display controller */
1836 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1837 val &= ~DPIO_DCLKP_EN;
1838 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1839
Ville Syrjälä61407f62014-05-27 16:32:55 +03001840 /* disable left/right clock distribution */
1841 if (pipe != PIPE_B) {
1842 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1843 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1844 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1845 } else {
1846 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1847 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1848 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1849 }
1850
Ville Syrjäläa5805162015-05-26 20:42:30 +03001851 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001852}
1853
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001854void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001855 struct intel_digital_port *dport,
1856 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001857{
1858 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001859 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001860
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001861 switch (dport->port) {
1862 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001863 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001864 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001865 break;
1866 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001867 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001868 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001869 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001870 break;
1871 case PORT_D:
1872 port_mask = DPLL_PORTD_READY_MASK;
1873 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001874 break;
1875 default:
1876 BUG();
1877 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001878
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001879 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1880 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1881 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001882}
1883
Daniel Vetterb14b1052014-04-24 23:55:13 +02001884static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1885{
1886 struct drm_device *dev = crtc->base.dev;
1887 struct drm_i915_private *dev_priv = dev->dev_private;
1888 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1889
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001890 if (WARN_ON(pll == NULL))
1891 return;
1892
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001893 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001894 if (pll->active == 0) {
1895 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1896 WARN_ON(pll->on);
1897 assert_shared_dpll_disabled(dev_priv, pll);
1898
1899 pll->mode_set(dev_priv, pll);
1900 }
1901}
1902
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001903/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001904 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001905 * @dev_priv: i915 private structure
1906 * @pipe: pipe PLL to enable
1907 *
1908 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1909 * drives the transcoder clock.
1910 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001911static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001912{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001913 struct drm_device *dev = crtc->base.dev;
1914 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001915 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001916
Daniel Vetter87a875b2013-06-05 13:34:19 +02001917 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001918 return;
1919
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001920 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001921 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001922
Damien Lespiau74dd6922014-07-29 18:06:17 +01001923 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001924 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001925 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001926
Daniel Vettercdbd2312013-06-05 13:34:03 +02001927 if (pll->active++) {
1928 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001929 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001930 return;
1931 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001932 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001933
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001934 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1935
Daniel Vetter46edb022013-06-05 13:34:12 +02001936 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001937 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001938 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001939}
1940
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001941static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001942{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001943 struct drm_device *dev = crtc->base.dev;
1944 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001945 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001946
Jesse Barnes92f25842011-01-04 15:09:34 -08001947 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001948 BUG_ON(INTEL_INFO(dev)->gen < 5);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001949 if (pll == NULL)
1950 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001951
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02001952 if (WARN_ON(!(pll->config.crtc_mask & (1 << drm_crtc_index(&crtc->base)))))
Chris Wilson48da64a2012-05-13 20:16:12 +01001953 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001954
Daniel Vetter46edb022013-06-05 13:34:12 +02001955 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1956 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001957 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001958
Chris Wilson48da64a2012-05-13 20:16:12 +01001959 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001960 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001961 return;
1962 }
1963
Daniel Vettere9d69442013-06-05 13:34:15 +02001964 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001965 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001966 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001967 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001968
Daniel Vetter46edb022013-06-05 13:34:12 +02001969 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001970 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001971 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001972
1973 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001974}
1975
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001976static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1977 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001978{
Daniel Vetter23670b322012-11-01 09:15:30 +01001979 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001980 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001982 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001983
1984 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001985 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001986
1987 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001988 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001989 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001990
1991 /* FDI must be feeding us bits for PCH ports */
1992 assert_fdi_tx_enabled(dev_priv, pipe);
1993 assert_fdi_rx_enabled(dev_priv, pipe);
1994
Daniel Vetter23670b322012-11-01 09:15:30 +01001995 if (HAS_PCH_CPT(dev)) {
1996 /* Workaround: Set the timing override bit before enabling the
1997 * pch transcoder. */
1998 reg = TRANS_CHICKEN2(pipe);
1999 val = I915_READ(reg);
2000 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2001 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03002002 }
Daniel Vetter23670b322012-11-01 09:15:30 +01002003
Daniel Vetterab9412b2013-05-03 11:49:46 +02002004 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002005 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002006 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002007
2008 if (HAS_PCH_IBX(dev_priv->dev)) {
2009 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002010 * Make the BPC in transcoder be consistent with
2011 * that in pipeconf reg. For HDMI we must use 8bpc
2012 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07002013 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002014 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002015 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2016 val |= PIPECONF_8BPC;
2017 else
2018 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002019 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002020
2021 val &= ~TRANS_INTERLACE_MASK;
2022 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002023 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002024 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002025 val |= TRANS_LEGACY_INTERLACED_ILK;
2026 else
2027 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002028 else
2029 val |= TRANS_PROGRESSIVE;
2030
Jesse Barnes040484a2011-01-03 12:14:26 -08002031 I915_WRITE(reg, val | TRANS_ENABLE);
2032 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002033 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002034}
2035
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002037 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002038{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002039 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040
2041 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002042 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002044 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002045 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002046 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002047
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002048 /* Workaround: set timing override bit. */
2049 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002050 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002051 I915_WRITE(_TRANSA_CHICKEN2, val);
2052
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002053 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002054 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002055
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002056 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2057 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002058 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002059 else
2060 val |= TRANS_PROGRESSIVE;
2061
Daniel Vetterab9412b2013-05-03 11:49:46 +02002062 I915_WRITE(LPT_TRANSCONF, val);
2063 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002064 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002065}
2066
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002067static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2068 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002069{
Daniel Vetter23670b322012-11-01 09:15:30 +01002070 struct drm_device *dev = dev_priv->dev;
2071 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002072
2073 /* FDI relies on the transcoder */
2074 assert_fdi_tx_disabled(dev_priv, pipe);
2075 assert_fdi_rx_disabled(dev_priv, pipe);
2076
Jesse Barnes291906f2011-02-02 12:28:03 -08002077 /* Ports must be off as well */
2078 assert_pch_ports_disabled(dev_priv, pipe);
2079
Daniel Vetterab9412b2013-05-03 11:49:46 +02002080 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002081 val = I915_READ(reg);
2082 val &= ~TRANS_ENABLE;
2083 I915_WRITE(reg, val);
2084 /* wait for PCH transcoder off, transcoder state */
2085 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002086 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002087
2088 if (!HAS_PCH_IBX(dev)) {
2089 /* Workaround: Clear the timing override chicken bit again. */
2090 reg = TRANS_CHICKEN2(pipe);
2091 val = I915_READ(reg);
2092 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2093 I915_WRITE(reg, val);
2094 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002095}
2096
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002097static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002098{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002099 u32 val;
2100
Daniel Vetterab9412b2013-05-03 11:49:46 +02002101 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002102 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002103 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002104 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002105 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002106 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002107
2108 /* Workaround: clear timing override bit. */
2109 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002110 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002111 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002112}
2113
2114/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002115 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002116 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002117 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002118 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002119 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002120 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002121static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002122{
Paulo Zanoni03722642014-01-17 13:51:09 -02002123 struct drm_device *dev = crtc->base.dev;
2124 struct drm_i915_private *dev_priv = dev->dev_private;
2125 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002126 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2127 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002128 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002129 int reg;
2130 u32 val;
2131
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002132 DRM_DEBUG_KMS("enabling pipe %c\n", pipe_name(pipe));
2133
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002134 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002135 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002136 assert_sprites_disabled(dev_priv, pipe);
2137
Paulo Zanoni681e5812012-12-06 11:12:38 -02002138 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002139 pch_transcoder = TRANSCODER_A;
2140 else
2141 pch_transcoder = pipe;
2142
Jesse Barnesb24e7172011-01-04 15:09:30 -08002143 /*
2144 * A pipe without a PLL won't actually be able to drive bits from
2145 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2146 * need the check.
2147 */
Imre Deak50360402015-01-16 00:55:16 -08002148 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002149 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002150 assert_dsi_pll_enabled(dev_priv);
2151 else
2152 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002153 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002154 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002155 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002156 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002157 assert_fdi_tx_pll_enabled(dev_priv,
2158 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002159 }
2160 /* FIXME: assert CPU port conditions for SNB+ */
2161 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002162
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002163 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002164 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002165 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002166 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2167 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002168 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002169 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002170
2171 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002172 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002173}
2174
2175/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002176 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002177 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002178 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002179 * Disable the pipe of @crtc, making sure that various hardware
2180 * specific requirements are met, if applicable, e.g. plane
2181 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002182 *
2183 * Will wait until the pipe has shut down before returning.
2184 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002185static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002187 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002188 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002189 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002190 int reg;
2191 u32 val;
2192
Ville Syrjälä9e2ee2d2015-06-24 21:59:35 +03002193 DRM_DEBUG_KMS("disabling pipe %c\n", pipe_name(pipe));
2194
Jesse Barnesb24e7172011-01-04 15:09:30 -08002195 /*
2196 * Make sure planes won't keep trying to pump pixels to us,
2197 * or we might hang the display.
2198 */
2199 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002200 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002201 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002202
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002203 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002204 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002205 if ((val & PIPECONF_ENABLE) == 0)
2206 return;
2207
Ville Syrjälä67adc642014-08-15 01:21:57 +03002208 /*
2209 * Double wide has implications for planes
2210 * so best keep it disabled when not needed.
2211 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002212 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002213 val &= ~PIPECONF_DOUBLE_WIDE;
2214
2215 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002216 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2217 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002218 val &= ~PIPECONF_ENABLE;
2219
2220 I915_WRITE(reg, val);
2221 if ((val & PIPECONF_ENABLE) == 0)
2222 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002223}
2224
Chris Wilson693db182013-03-05 14:52:39 +00002225static bool need_vtd_wa(struct drm_device *dev)
2226{
2227#ifdef CONFIG_INTEL_IOMMU
2228 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2229 return true;
2230#endif
2231 return false;
2232}
2233
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002234unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002235intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2236 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002237{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002238 unsigned int tile_height;
2239 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002240
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002241 switch (fb_format_modifier) {
2242 case DRM_FORMAT_MOD_NONE:
2243 tile_height = 1;
2244 break;
2245 case I915_FORMAT_MOD_X_TILED:
2246 tile_height = IS_GEN2(dev) ? 16 : 8;
2247 break;
2248 case I915_FORMAT_MOD_Y_TILED:
2249 tile_height = 32;
2250 break;
2251 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002252 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2253 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002254 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002255 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002256 tile_height = 64;
2257 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002258 case 2:
2259 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002260 tile_height = 32;
2261 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002262 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002263 tile_height = 16;
2264 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002265 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002266 WARN_ONCE(1,
2267 "128-bit pixels are not supported for display!");
2268 tile_height = 16;
2269 break;
2270 }
2271 break;
2272 default:
2273 MISSING_CASE(fb_format_modifier);
2274 tile_height = 1;
2275 break;
2276 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002277
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002278 return tile_height;
2279}
2280
2281unsigned int
2282intel_fb_align_height(struct drm_device *dev, unsigned int height,
2283 uint32_t pixel_format, uint64_t fb_format_modifier)
2284{
2285 return ALIGN(height, intel_tile_height(dev, pixel_format,
2286 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002287}
2288
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002289static int
2290intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2291 const struct drm_plane_state *plane_state)
2292{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002293 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002294 unsigned int tile_height, tile_pitch;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002295
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002296 *view = i915_ggtt_view_normal;
2297
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002298 if (!plane_state)
2299 return 0;
2300
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002301 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002302 return 0;
2303
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002304 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002305
2306 info->height = fb->height;
2307 info->pixel_format = fb->pixel_format;
2308 info->pitch = fb->pitches[0];
2309 info->fb_modifier = fb->modifier[0];
2310
Tvrtko Ursulin84fe03f2015-06-23 14:26:46 +01002311 tile_height = intel_tile_height(fb->dev, fb->pixel_format,
2312 fb->modifier[0]);
2313 tile_pitch = PAGE_SIZE / tile_height;
2314 info->width_pages = DIV_ROUND_UP(fb->pitches[0], tile_pitch);
2315 info->height_pages = DIV_ROUND_UP(fb->height, tile_height);
2316 info->size = info->width_pages * info->height_pages * PAGE_SIZE;
2317
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002318 return 0;
2319}
2320
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002321static unsigned int intel_linear_alignment(struct drm_i915_private *dev_priv)
2322{
2323 if (INTEL_INFO(dev_priv)->gen >= 9)
2324 return 256 * 1024;
Ville Syrjälä985b8bb2015-06-11 16:31:15 +03002325 else if (IS_BROADWATER(dev_priv) || IS_CRESTLINE(dev_priv) ||
2326 IS_VALLEYVIEW(dev_priv))
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002327 return 128 * 1024;
2328 else if (INTEL_INFO(dev_priv)->gen >= 4)
2329 return 4 * 1024;
2330 else
Ville Syrjälä44c59052015-06-11 16:31:16 +03002331 return 0;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002332}
2333
Chris Wilson127bd2a2010-07-23 23:32:05 +01002334int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002335intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2336 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002337 const struct drm_plane_state *plane_state,
John Harrison91af1272015-06-18 13:14:56 +01002338 struct intel_engine_cs *pipelined,
2339 struct drm_i915_gem_request **pipelined_request)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002340{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002341 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002342 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002343 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002344 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002345 u32 alignment;
2346 int ret;
2347
Matt Roperebcdd392014-07-09 16:22:11 -07002348 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2349
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002350 switch (fb->modifier[0]) {
2351 case DRM_FORMAT_MOD_NONE:
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002352 alignment = intel_linear_alignment(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002353 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002354 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002355 if (INTEL_INFO(dev)->gen >= 9)
2356 alignment = 256 * 1024;
2357 else {
2358 /* pin() will align the object as required by fence */
2359 alignment = 0;
2360 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002361 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002362 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002363 case I915_FORMAT_MOD_Yf_TILED:
2364 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2365 "Y tiling bo slipped through, driver bug!\n"))
2366 return -EINVAL;
2367 alignment = 1 * 1024 * 1024;
2368 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002369 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002370 MISSING_CASE(fb->modifier[0]);
2371 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002372 }
2373
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002374 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2375 if (ret)
2376 return ret;
2377
Chris Wilson693db182013-03-05 14:52:39 +00002378 /* Note that the w/a also requires 64 PTE of padding following the
2379 * bo. We currently fill all unused PTE with the shadow page and so
2380 * we should always have valid PTE following the scanout preventing
2381 * the VT-d warning.
2382 */
2383 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2384 alignment = 256 * 1024;
2385
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002386 /*
2387 * Global gtt pte registers are special registers which actually forward
2388 * writes to a chunk of system memory. Which means that there is no risk
2389 * that the register values disappear as soon as we call
2390 * intel_runtime_pm_put(), so it is correct to wrap only the
2391 * pin/unpin/fence and not more.
2392 */
2393 intel_runtime_pm_get(dev_priv);
2394
Chris Wilsonce453d82011-02-21 14:43:56 +00002395 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002396 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
John Harrison91af1272015-06-18 13:14:56 +01002397 pipelined_request, &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002398 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002399 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002400
2401 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2402 * fence, whereas 965+ only requires a fence if using
2403 * framebuffer compression. For simplicity, we always install
2404 * a fence as the cost is not that onerous.
2405 */
Chris Wilson06d98132012-04-17 15:31:24 +01002406 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002407 if (ret)
2408 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002409
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002410 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002411
Chris Wilsonce453d82011-02-21 14:43:56 +00002412 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002413 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002414 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002415
2416err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002417 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002418err_interruptible:
2419 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002420 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002421 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002422}
2423
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002424static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2425 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002426{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002427 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002428 struct i915_ggtt_view view;
2429 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002430
Matt Roperebcdd392014-07-09 16:22:11 -07002431 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2432
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002433 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2434 WARN_ONCE(ret, "Couldn't get view from plane state!");
2435
Chris Wilson1690e1e2011-12-14 13:57:08 +01002436 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002437 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002438}
2439
Daniel Vetterc2c75132012-07-05 12:17:30 +02002440/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2441 * is assumed to be a power-of-two. */
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002442unsigned long intel_gen4_compute_page_offset(struct drm_i915_private *dev_priv,
2443 int *x, int *y,
Chris Wilsonbc752862013-02-21 20:04:31 +00002444 unsigned int tiling_mode,
2445 unsigned int cpp,
2446 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002447{
Chris Wilsonbc752862013-02-21 20:04:31 +00002448 if (tiling_mode != I915_TILING_NONE) {
2449 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002450
Chris Wilsonbc752862013-02-21 20:04:31 +00002451 tile_rows = *y / 8;
2452 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453
Chris Wilsonbc752862013-02-21 20:04:31 +00002454 tiles = *x / (512/cpp);
2455 *x %= 512/cpp;
2456
2457 return tile_rows * pitch * 8 + tiles * 4096;
2458 } else {
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002459 unsigned int alignment = intel_linear_alignment(dev_priv) - 1;
Chris Wilsonbc752862013-02-21 20:04:31 +00002460 unsigned int offset;
2461
2462 offset = *y * pitch + *x * cpp;
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002463 *y = (offset & alignment) / pitch;
2464 *x = ((offset & alignment) - *y * pitch) / cpp;
2465 return offset & ~alignment;
Chris Wilsonbc752862013-02-21 20:04:31 +00002466 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002467}
2468
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002469static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002470{
2471 switch (format) {
2472 case DISPPLANE_8BPP:
2473 return DRM_FORMAT_C8;
2474 case DISPPLANE_BGRX555:
2475 return DRM_FORMAT_XRGB1555;
2476 case DISPPLANE_BGRX565:
2477 return DRM_FORMAT_RGB565;
2478 default:
2479 case DISPPLANE_BGRX888:
2480 return DRM_FORMAT_XRGB8888;
2481 case DISPPLANE_RGBX888:
2482 return DRM_FORMAT_XBGR8888;
2483 case DISPPLANE_BGRX101010:
2484 return DRM_FORMAT_XRGB2101010;
2485 case DISPPLANE_RGBX101010:
2486 return DRM_FORMAT_XBGR2101010;
2487 }
2488}
2489
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002490static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2491{
2492 switch (format) {
2493 case PLANE_CTL_FORMAT_RGB_565:
2494 return DRM_FORMAT_RGB565;
2495 default:
2496 case PLANE_CTL_FORMAT_XRGB_8888:
2497 if (rgb_order) {
2498 if (alpha)
2499 return DRM_FORMAT_ABGR8888;
2500 else
2501 return DRM_FORMAT_XBGR8888;
2502 } else {
2503 if (alpha)
2504 return DRM_FORMAT_ARGB8888;
2505 else
2506 return DRM_FORMAT_XRGB8888;
2507 }
2508 case PLANE_CTL_FORMAT_XRGB_2101010:
2509 if (rgb_order)
2510 return DRM_FORMAT_XBGR2101010;
2511 else
2512 return DRM_FORMAT_XRGB2101010;
2513 }
2514}
2515
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002516static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002517intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2518 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002519{
2520 struct drm_device *dev = crtc->base.dev;
2521 struct drm_i915_gem_object *obj = NULL;
2522 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002523 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002524 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2525 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2526 PAGE_SIZE);
2527
2528 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002529
Chris Wilsonff2652e2014-03-10 08:07:02 +00002530 if (plane_config->size == 0)
2531 return false;
2532
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002533 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2534 base_aligned,
2535 base_aligned,
2536 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002537 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002538 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002539
Damien Lespiau49af4492015-01-20 12:51:44 +00002540 obj->tiling_mode = plane_config->tiling;
2541 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002542 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002543
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002544 mode_cmd.pixel_format = fb->pixel_format;
2545 mode_cmd.width = fb->width;
2546 mode_cmd.height = fb->height;
2547 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002548 mode_cmd.modifier[0] = fb->modifier[0];
2549 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002550
2551 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002552 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002553 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554 DRM_DEBUG_KMS("intel fb init failed\n");
2555 goto out_unref_obj;
2556 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002557 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558
Daniel Vetterf6936e22015-03-26 12:17:05 +01002559 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002560 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002561
2562out_unref_obj:
2563 drm_gem_object_unreference(&obj->base);
2564 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565 return false;
2566}
2567
Matt Roperafd65eb2015-02-03 13:10:04 -08002568/* Update plane->state->fb to match plane->fb after driver-internal updates */
2569static void
2570update_state_fb(struct drm_plane *plane)
2571{
2572 if (plane->fb == plane->state->fb)
2573 return;
2574
2575 if (plane->state->fb)
2576 drm_framebuffer_unreference(plane->state->fb);
2577 plane->state->fb = plane->fb;
2578 if (plane->state->fb)
2579 drm_framebuffer_reference(plane->state->fb);
2580}
2581
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002582static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002583intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2584 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002585{
2586 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002587 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002588 struct drm_crtc *c;
2589 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002590 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591 struct drm_plane *primary = intel_crtc->base.primary;
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002592 struct drm_plane_state *plane_state = primary->state;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002593 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594
Damien Lespiau2d140302015-02-05 17:22:18 +00002595 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596 return;
2597
Daniel Vetterf6936e22015-03-26 12:17:05 +01002598 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002599 fb = &plane_config->fb->base;
2600 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002601 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602
Damien Lespiau2d140302015-02-05 17:22:18 +00002603 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002604
2605 /*
2606 * Failed to alloc the obj, check to see if we should share
2607 * an fb with another CRTC instead
2608 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002609 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002610 i = to_intel_crtc(c);
2611
2612 if (c == &intel_crtc->base)
2613 continue;
2614
Matt Roper2ff8fde2014-07-08 07:50:07 -07002615 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002616 continue;
2617
Daniel Vetter88595ac2015-03-26 12:42:24 +01002618 fb = c->primary->fb;
2619 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002620 continue;
2621
Daniel Vetter88595ac2015-03-26 12:42:24 +01002622 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002623 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624 drm_framebuffer_reference(fb);
2625 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002626 }
2627 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002628
2629 return;
2630
2631valid_fb:
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002632 plane_state->src_x = plane_state->src_y = 0;
2633 plane_state->src_w = fb->width << 16;
2634 plane_state->src_h = fb->height << 16;
2635
2636 plane_state->crtc_x = plane_state->src_y = 0;
2637 plane_state->crtc_w = fb->width;
2638 plane_state->crtc_h = fb->height;
2639
Daniel Vetter88595ac2015-03-26 12:42:24 +01002640 obj = intel_fb_obj(fb);
2641 if (obj->tiling_mode != I915_TILING_NONE)
2642 dev_priv->preserve_bios_swizzle = true;
2643
Maarten Lankhorstbe5651f2015-07-13 16:30:18 +02002644 drm_framebuffer_reference(fb);
2645 primary->fb = primary->state->fb = fb;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002646 primary->crtc = primary->state->crtc = &intel_crtc->base;
Maarten Lankhorst36750f22015-06-01 12:49:54 +02002647 intel_crtc->base.state->plane_mask |= (1 << drm_plane_index(primary));
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03002648 obj->frontbuffer_bits |= to_intel_plane(primary)->frontbuffer_bit;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002649}
2650
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002651static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2652 struct drm_framebuffer *fb,
2653 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002654{
2655 struct drm_device *dev = crtc->dev;
2656 struct drm_i915_private *dev_priv = dev->dev_private;
2657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002658 struct drm_plane *primary = crtc->primary;
2659 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002660 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002661 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002662 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002663 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002664 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302665 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002666
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002667 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002668 I915_WRITE(reg, 0);
2669 if (INTEL_INFO(dev)->gen >= 4)
2670 I915_WRITE(DSPSURF(plane), 0);
2671 else
2672 I915_WRITE(DSPADDR(plane), 0);
2673 POSTING_READ(reg);
2674 return;
2675 }
2676
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002677 obj = intel_fb_obj(fb);
2678 if (WARN_ON(obj == NULL))
2679 return;
2680
2681 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2682
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002683 dspcntr = DISPPLANE_GAMMA_ENABLE;
2684
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002685 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002686
2687 if (INTEL_INFO(dev)->gen < 4) {
2688 if (intel_crtc->pipe == PIPE_B)
2689 dspcntr |= DISPPLANE_SEL_PIPE_B;
2690
2691 /* pipesrc and dspsize control the size that is scaled from,
2692 * which should always be the user's requested size.
2693 */
2694 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002695 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2696 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002697 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002698 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2699 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002700 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2701 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002702 I915_WRITE(PRIMPOS(plane), 0);
2703 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002704 }
2705
Ville Syrjälä57779d02012-10-31 17:50:14 +02002706 switch (fb->pixel_format) {
2707 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002708 dspcntr |= DISPPLANE_8BPP;
2709 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002710 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002711 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002712 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002713 case DRM_FORMAT_RGB565:
2714 dspcntr |= DISPPLANE_BGRX565;
2715 break;
2716 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002717 dspcntr |= DISPPLANE_BGRX888;
2718 break;
2719 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002720 dspcntr |= DISPPLANE_RGBX888;
2721 break;
2722 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002723 dspcntr |= DISPPLANE_BGRX101010;
2724 break;
2725 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002726 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002727 break;
2728 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002729 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002730 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002731
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002732 if (INTEL_INFO(dev)->gen >= 4 &&
2733 obj->tiling_mode != I915_TILING_NONE)
2734 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002735
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002736 if (IS_G4X(dev))
2737 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2738
Ville Syrjäläb98971272014-08-27 16:51:22 +03002739 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002740
Daniel Vetterc2c75132012-07-05 12:17:30 +02002741 if (INTEL_INFO(dev)->gen >= 4) {
2742 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002743 intel_gen4_compute_page_offset(dev_priv,
2744 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002745 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002746 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002747 linear_offset -= intel_crtc->dspaddr_offset;
2748 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002749 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002750 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002751
Matt Roper8e7d6882015-01-21 16:35:41 -08002752 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302753 dspcntr |= DISPPLANE_ROTATE_180;
2754
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002755 x += (intel_crtc->config->pipe_src_w - 1);
2756 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302757
2758 /* Finding the last pixel of the last line of the display
2759 data and adding to linear_offset*/
2760 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002761 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2762 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302763 }
2764
2765 I915_WRITE(reg, dspcntr);
2766
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002767 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002768 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002769 I915_WRITE(DSPSURF(plane),
2770 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002771 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002772 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002773 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002774 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002775 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776}
2777
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002778static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2779 struct drm_framebuffer *fb,
2780 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002781{
2782 struct drm_device *dev = crtc->dev;
2783 struct drm_i915_private *dev_priv = dev->dev_private;
2784 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002785 struct drm_plane *primary = crtc->primary;
2786 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002787 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002788 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002789 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002790 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002791 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302792 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002793
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002794 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002795 I915_WRITE(reg, 0);
2796 I915_WRITE(DSPSURF(plane), 0);
2797 POSTING_READ(reg);
2798 return;
2799 }
2800
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002801 obj = intel_fb_obj(fb);
2802 if (WARN_ON(obj == NULL))
2803 return;
2804
2805 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2806
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002807 dspcntr = DISPPLANE_GAMMA_ENABLE;
2808
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002809 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002810
2811 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2812 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2813
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 switch (fb->pixel_format) {
2815 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002816 dspcntr |= DISPPLANE_8BPP;
2817 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002818 case DRM_FORMAT_RGB565:
2819 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002820 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002821 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 dspcntr |= DISPPLANE_BGRX888;
2823 break;
2824 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002825 dspcntr |= DISPPLANE_RGBX888;
2826 break;
2827 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002828 dspcntr |= DISPPLANE_BGRX101010;
2829 break;
2830 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002831 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832 break;
2833 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002834 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002835 }
2836
2837 if (obj->tiling_mode != I915_TILING_NONE)
2838 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002840 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002841 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002842
Ville Syrjäläb98971272014-08-27 16:51:22 +03002843 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002844 intel_crtc->dspaddr_offset =
Ville Syrjälä4e9a86b2015-06-11 16:31:14 +03002845 intel_gen4_compute_page_offset(dev_priv,
2846 &x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002847 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002848 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002849 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002850 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302851 dspcntr |= DISPPLANE_ROTATE_180;
2852
2853 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002854 x += (intel_crtc->config->pipe_src_w - 1);
2855 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302856
2857 /* Finding the last pixel of the last line of the display
2858 data and adding to linear_offset*/
2859 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002860 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2861 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302862 }
2863 }
2864
2865 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002866
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002867 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002868 I915_WRITE(DSPSURF(plane),
2869 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002870 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002871 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2872 } else {
2873 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2874 I915_WRITE(DSPLINOFF(plane), linear_offset);
2875 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002876 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002877}
2878
Damien Lespiaub3218032015-02-27 11:15:18 +00002879u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2880 uint32_t pixel_format)
2881{
2882 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2883
2884 /*
2885 * The stride is either expressed as a multiple of 64 bytes
2886 * chunks for linear buffers or in number of tiles for tiled
2887 * buffers.
2888 */
2889 switch (fb_modifier) {
2890 case DRM_FORMAT_MOD_NONE:
2891 return 64;
2892 case I915_FORMAT_MOD_X_TILED:
2893 if (INTEL_INFO(dev)->gen == 2)
2894 return 128;
2895 return 512;
2896 case I915_FORMAT_MOD_Y_TILED:
2897 /* No need to check for old gens and Y tiling since this is
2898 * about the display engine and those will be blocked before
2899 * we get here.
2900 */
2901 return 128;
2902 case I915_FORMAT_MOD_Yf_TILED:
2903 if (bits_per_pixel == 8)
2904 return 64;
2905 else
2906 return 128;
2907 default:
2908 MISSING_CASE(fb_modifier);
2909 return 64;
2910 }
2911}
2912
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002913unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2914 struct drm_i915_gem_object *obj)
2915{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002916 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002917
2918 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002919 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002920
2921 return i915_gem_obj_ggtt_offset_view(obj, view);
2922}
2923
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002924static void skl_detach_scaler(struct intel_crtc *intel_crtc, int id)
2925{
2926 struct drm_device *dev = intel_crtc->base.dev;
2927 struct drm_i915_private *dev_priv = dev->dev_private;
2928
2929 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, id), 0);
2930 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, id), 0);
2931 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, id), 0);
2932 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2933 intel_crtc->base.base.id, intel_crtc->pipe, id);
2934}
2935
Chandra Kondurua1b22782015-04-07 15:28:45 -07002936/*
2937 * This function detaches (aka. unbinds) unused scalers in hardware
2938 */
Maarten Lankhorst05832362015-06-15 12:33:48 +02002939static void skl_detach_scalers(struct intel_crtc *intel_crtc)
Chandra Kondurua1b22782015-04-07 15:28:45 -07002940{
Chandra Kondurua1b22782015-04-07 15:28:45 -07002941 struct intel_crtc_scaler_state *scaler_state;
2942 int i;
2943
Chandra Kondurua1b22782015-04-07 15:28:45 -07002944 scaler_state = &intel_crtc->config->scaler_state;
2945
2946 /* loop through and disable scalers that aren't in use */
2947 for (i = 0; i < intel_crtc->num_scalers; i++) {
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02002948 if (!scaler_state->scalers[i].in_use)
2949 skl_detach_scaler(intel_crtc, i);
Chandra Kondurua1b22782015-04-07 15:28:45 -07002950 }
2951}
2952
Chandra Konduru6156a452015-04-27 13:48:39 -07002953u32 skl_plane_ctl_format(uint32_t pixel_format)
2954{
Chandra Konduru6156a452015-04-27 13:48:39 -07002955 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002956 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002963 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002964 /*
2965 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2966 * to be already pre-multiplied. We need to add a knob (or a different
2967 * DRM_FORMAT) for user-space to configure that.
2968 */
2969 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002972 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002974 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002984 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002986 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002988 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002990
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002991 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992}
2993
2994u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2995{
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 switch (fb_modifier) {
2997 case DRM_FORMAT_MOD_NONE:
2998 break;
2999 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003002 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003004 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003005 default:
3006 MISSING_CASE(fb_modifier);
3007 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003008
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003009 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003010}
3011
3012u32 skl_plane_ctl_rotation(unsigned int rotation)
3013{
Chandra Konduru6156a452015-04-27 13:48:39 -07003014 switch (rotation) {
3015 case BIT(DRM_ROTATE_0):
3016 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303017 /*
3018 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3019 * while i915 HW rotation is clockwise, thats why this swapping.
3020 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003021 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303022 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003023 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003024 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003025 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303026 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003027 default:
3028 MISSING_CASE(rotation);
3029 }
3030
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003031 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003032}
3033
Damien Lespiau70d21f02013-07-03 21:06:04 +01003034static void skylake_update_primary_plane(struct drm_crtc *crtc,
3035 struct drm_framebuffer *fb,
3036 int x, int y)
3037{
3038 struct drm_device *dev = crtc->dev;
3039 struct drm_i915_private *dev_priv = dev->dev_private;
3040 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003041 struct drm_plane *plane = crtc->primary;
3042 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003043 struct drm_i915_gem_object *obj;
3044 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303045 u32 plane_ctl, stride_div, stride;
3046 u32 tile_height, plane_offset, plane_size;
3047 unsigned int rotation;
3048 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003049 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003050 struct intel_crtc_state *crtc_state = intel_crtc->config;
3051 struct intel_plane_state *plane_state;
3052 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3053 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3054 int scaler_id = -1;
3055
Chandra Konduru6156a452015-04-27 13:48:39 -07003056 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003057
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003058 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003059 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3060 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3061 POSTING_READ(PLANE_CTL(pipe, 0));
3062 return;
3063 }
3064
3065 plane_ctl = PLANE_CTL_ENABLE |
3066 PLANE_CTL_PIPE_GAMMA_ENABLE |
3067 PLANE_CTL_PIPE_CSC_ENABLE;
3068
Chandra Konduru6156a452015-04-27 13:48:39 -07003069 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3070 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003071 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303072
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303073 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003074 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003075
Damien Lespiaub3218032015-02-27 11:15:18 +00003076 obj = intel_fb_obj(fb);
3077 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3078 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303079 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3080
Chandra Konduru6156a452015-04-27 13:48:39 -07003081 /*
3082 * FIXME: intel_plane_state->src, dst aren't set when transitional
3083 * update_plane helpers are called from legacy paths.
3084 * Once full atomic crtc is available, below check can be avoided.
3085 */
3086 if (drm_rect_width(&plane_state->src)) {
3087 scaler_id = plane_state->scaler_id;
3088 src_x = plane_state->src.x1 >> 16;
3089 src_y = plane_state->src.y1 >> 16;
3090 src_w = drm_rect_width(&plane_state->src) >> 16;
3091 src_h = drm_rect_height(&plane_state->src) >> 16;
3092 dst_x = plane_state->dst.x1;
3093 dst_y = plane_state->dst.y1;
3094 dst_w = drm_rect_width(&plane_state->dst);
3095 dst_h = drm_rect_height(&plane_state->dst);
3096
3097 WARN_ON(x != src_x || y != src_y);
3098 } else {
3099 src_w = intel_crtc->config->pipe_src_w;
3100 src_h = intel_crtc->config->pipe_src_h;
3101 }
3102
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303103 if (intel_rotation_90_or_270(rotation)) {
3104 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003105 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303106 fb->modifier[0]);
3107 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003108 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303109 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003110 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303111 } else {
3112 stride = fb->pitches[0] / stride_div;
3113 x_offset = x;
3114 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003115 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303116 }
3117 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003118
Damien Lespiau70d21f02013-07-03 21:06:04 +01003119 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303120 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3121 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3122 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003123
3124 if (scaler_id >= 0) {
3125 uint32_t ps_ctrl = 0;
3126
3127 WARN_ON(!dst_w || !dst_h);
3128 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3129 crtc_state->scaler_state.scalers[scaler_id].mode;
3130 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3131 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3132 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3133 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3134 I915_WRITE(PLANE_POS(pipe, 0), 0);
3135 } else {
3136 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3137 }
3138
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003139 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003140
3141 POSTING_READ(PLANE_SURF(pipe, 0));
3142}
3143
Jesse Barnes17638cd2011-06-24 12:19:23 -07003144/* Assume fb object is pinned & idle & fenced and just update base pointers */
3145static int
3146intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3147 int x, int y, enum mode_set_atomic state)
3148{
3149 struct drm_device *dev = crtc->dev;
3150 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003151
Paulo Zanoniff2a3112015-07-07 15:26:03 -03003152 if (dev_priv->fbc.disable_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03003153 dev_priv->fbc.disable_fbc(dev_priv);
Jesse Barnes81255562010-08-02 12:07:50 -07003154
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003155 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3156
3157 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003158}
3159
Ville Syrjälä75147472014-11-24 18:28:11 +02003160static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003161{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003162 struct drm_crtc *crtc;
3163
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003164 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003165 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3166 enum plane plane = intel_crtc->plane;
3167
3168 intel_prepare_page_flip(dev, plane);
3169 intel_finish_page_flip_plane(dev, plane);
3170 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003171}
3172
3173static void intel_update_primary_planes(struct drm_device *dev)
3174{
3175 struct drm_i915_private *dev_priv = dev->dev_private;
3176 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003177
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003178 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003179 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3180
Rob Clark51fd3712013-11-19 12:10:12 -05003181 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003182 /*
3183 * FIXME: Once we have proper support for primary planes (and
3184 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003185 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003186 */
Matt Roperf4510a22014-04-01 15:22:40 -07003187 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003188 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003189 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003190 crtc->x,
3191 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003192 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003193 }
3194}
3195
Ville Syrjälä75147472014-11-24 18:28:11 +02003196void intel_prepare_reset(struct drm_device *dev)
3197{
3198 /* no reset support for gen2 */
3199 if (IS_GEN2(dev))
3200 return;
3201
3202 /* reset doesn't touch the display */
3203 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3204 return;
3205
3206 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003207 /*
3208 * Disabling the crtcs gracefully seems nicer. Also the
3209 * g33 docs say we should at least disable all the planes.
3210 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003211 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003212}
3213
3214void intel_finish_reset(struct drm_device *dev)
3215{
3216 struct drm_i915_private *dev_priv = to_i915(dev);
3217
3218 /*
3219 * Flips in the rings will be nuked by the reset,
3220 * so complete all pending flips so that user space
3221 * will get its events and not get stuck.
3222 */
3223 intel_complete_page_flips(dev);
3224
3225 /* no reset support for gen2 */
3226 if (IS_GEN2(dev))
3227 return;
3228
3229 /* reset doesn't touch the display */
3230 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3231 /*
3232 * Flips in the rings have been nuked by the reset,
3233 * so update the base address of all primary
3234 * planes to the the last fb to make sure we're
3235 * showing the correct fb after a reset.
3236 */
3237 intel_update_primary_planes(dev);
3238 return;
3239 }
3240
3241 /*
3242 * The display has been reset as well,
3243 * so need a full re-initialization.
3244 */
3245 intel_runtime_pm_disable_interrupts(dev_priv);
3246 intel_runtime_pm_enable_interrupts(dev_priv);
3247
3248 intel_modeset_init_hw(dev);
3249
3250 spin_lock_irq(&dev_priv->irq_lock);
3251 if (dev_priv->display.hpd_irq_setup)
3252 dev_priv->display.hpd_irq_setup(dev);
3253 spin_unlock_irq(&dev_priv->irq_lock);
3254
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +02003255 intel_display_resume(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003256
3257 intel_hpd_init(dev_priv);
3258
3259 drm_modeset_unlock_all(dev);
3260}
3261
Chris Wilson2e2f3512015-04-27 13:41:14 +01003262static void
Chris Wilson14667a42012-04-03 17:58:35 +01003263intel_finish_fb(struct drm_framebuffer *old_fb)
3264{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003265 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003266 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003267 bool was_interruptible = dev_priv->mm.interruptible;
3268 int ret;
3269
Chris Wilson14667a42012-04-03 17:58:35 +01003270 /* Big Hammer, we also need to ensure that any pending
3271 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3272 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003273 * framebuffer. Note that we rely on userspace rendering
3274 * into the buffer attached to the pipe they are waiting
3275 * on. If not, userspace generates a GPU hang with IPEHR
3276 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003277 *
3278 * This should only fail upon a hung GPU, in which case we
3279 * can safely continue.
3280 */
3281 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003282 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003283 dev_priv->mm.interruptible = was_interruptible;
3284
Chris Wilson2e2f3512015-04-27 13:41:14 +01003285 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003286}
3287
Chris Wilson7d5e3792014-03-04 13:15:08 +00003288static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3289{
3290 struct drm_device *dev = crtc->dev;
3291 struct drm_i915_private *dev_priv = dev->dev_private;
3292 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003293 bool pending;
3294
3295 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3296 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3297 return false;
3298
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003299 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003300 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003301 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003302
3303 return pending;
3304}
3305
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003306static void intel_update_pipe_size(struct intel_crtc *crtc)
3307{
3308 struct drm_device *dev = crtc->base.dev;
3309 struct drm_i915_private *dev_priv = dev->dev_private;
3310 const struct drm_display_mode *adjusted_mode;
3311
3312 if (!i915.fastboot)
3313 return;
3314
3315 /*
3316 * Update pipe size and adjust fitter if needed: the reason for this is
3317 * that in compute_mode_changes we check the native mode (not the pfit
3318 * mode) to see if we can flip rather than do a full mode set. In the
3319 * fastboot case, we'll flip, but if we don't update the pipesrc and
3320 * pfit state, we'll end up with a big fb scanned out into the wrong
3321 * sized surface.
3322 *
3323 * To fix this properly, we need to hoist the checks up into
3324 * compute_mode_changes (or above), check the actual pfit state and
3325 * whether the platform allows pfit disable with pipe active, and only
3326 * then update the pipesrc and pfit state, even on the flip path.
3327 */
3328
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003329 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003330
3331 I915_WRITE(PIPESRC(crtc->pipe),
3332 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3333 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003334 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003335 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3336 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003337 I915_WRITE(PF_CTL(crtc->pipe), 0);
3338 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3339 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3340 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003341 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3342 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003343}
3344
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003345static void intel_fdi_normal_train(struct drm_crtc *crtc)
3346{
3347 struct drm_device *dev = crtc->dev;
3348 struct drm_i915_private *dev_priv = dev->dev_private;
3349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3350 int pipe = intel_crtc->pipe;
3351 u32 reg, temp;
3352
3353 /* enable normal train */
3354 reg = FDI_TX_CTL(pipe);
3355 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003356 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003357 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3358 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003359 } else {
3360 temp &= ~FDI_LINK_TRAIN_NONE;
3361 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003362 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003363 I915_WRITE(reg, temp);
3364
3365 reg = FDI_RX_CTL(pipe);
3366 temp = I915_READ(reg);
3367 if (HAS_PCH_CPT(dev)) {
3368 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3369 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3370 } else {
3371 temp &= ~FDI_LINK_TRAIN_NONE;
3372 temp |= FDI_LINK_TRAIN_NONE;
3373 }
3374 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3375
3376 /* wait one idle pattern time */
3377 POSTING_READ(reg);
3378 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003379
3380 /* IVB wants error correction enabled */
3381 if (IS_IVYBRIDGE(dev))
3382 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3383 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003384}
3385
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003386/* The FDI link training functions for ILK/Ibexpeak. */
3387static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3388{
3389 struct drm_device *dev = crtc->dev;
3390 struct drm_i915_private *dev_priv = dev->dev_private;
3391 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3392 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003393 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003394
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003395 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003396 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003397
Adam Jacksone1a44742010-06-25 15:32:14 -04003398 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3399 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 reg = FDI_RX_IMR(pipe);
3401 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003402 temp &= ~FDI_RX_SYMBOL_LOCK;
3403 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003404 I915_WRITE(reg, temp);
3405 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003406 udelay(150);
3407
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003408 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 reg = FDI_TX_CTL(pipe);
3410 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003411 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003412 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413 temp &= ~FDI_LINK_TRAIN_NONE;
3414 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003415 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003416
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 reg = FDI_RX_CTL(pipe);
3418 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003419 temp &= ~FDI_LINK_TRAIN_NONE;
3420 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3422
3423 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424 udelay(150);
3425
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003426 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003427 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3428 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3429 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003430
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003432 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003433 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003434 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3435
3436 if ((temp & FDI_RX_BIT_LOCK)) {
3437 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 break;
3440 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003441 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003442 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003443 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444
3445 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 reg = FDI_TX_CTL(pipe);
3447 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448 temp &= ~FDI_LINK_TRAIN_NONE;
3449 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003451
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 reg = FDI_RX_CTL(pipe);
3453 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003454 temp &= ~FDI_LINK_TRAIN_NONE;
3455 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003456 I915_WRITE(reg, temp);
3457
3458 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 udelay(150);
3460
Chris Wilson5eddb702010-09-11 13:48:45 +01003461 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003462 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3465
3466 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003467 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 DRM_DEBUG_KMS("FDI train 2 done.\n");
3469 break;
3470 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003471 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003472 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003473 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003474
3475 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003476
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477}
3478
Akshay Joshi0206e352011-08-16 15:34:10 -04003479static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3481 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3482 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3483 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3484};
3485
3486/* The FDI link training functions for SNB/Cougarpoint. */
3487static void gen6_fdi_link_train(struct drm_crtc *crtc)
3488{
3489 struct drm_device *dev = crtc->dev;
3490 struct drm_i915_private *dev_priv = dev->dev_private;
3491 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3492 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003493 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494
Adam Jacksone1a44742010-06-25 15:32:14 -04003495 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3496 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 reg = FDI_RX_IMR(pipe);
3498 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003499 temp &= ~FDI_RX_SYMBOL_LOCK;
3500 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003501 I915_WRITE(reg, temp);
3502
3503 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003504 udelay(150);
3505
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003506 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003507 reg = FDI_TX_CTL(pipe);
3508 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003509 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003510 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 temp &= ~FDI_LINK_TRAIN_NONE;
3512 temp |= FDI_LINK_TRAIN_PATTERN_1;
3513 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3514 /* SNB-B */
3515 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003516 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003517
Daniel Vetterd74cf322012-10-26 10:58:13 +02003518 I915_WRITE(FDI_RX_MISC(pipe),
3519 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3520
Chris Wilson5eddb702010-09-11 13:48:45 +01003521 reg = FDI_RX_CTL(pipe);
3522 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003523 if (HAS_PCH_CPT(dev)) {
3524 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3525 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3526 } else {
3527 temp &= ~FDI_LINK_TRAIN_NONE;
3528 temp |= FDI_LINK_TRAIN_PATTERN_1;
3529 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3531
3532 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003533 udelay(150);
3534
Akshay Joshi0206e352011-08-16 15:34:10 -04003535 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 reg = FDI_TX_CTL(pipe);
3537 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3539 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003540 I915_WRITE(reg, temp);
3541
3542 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 udelay(500);
3544
Sean Paulfa37d392012-03-02 12:53:39 -05003545 for (retry = 0; retry < 5; retry++) {
3546 reg = FDI_RX_IIR(pipe);
3547 temp = I915_READ(reg);
3548 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3549 if (temp & FDI_RX_BIT_LOCK) {
3550 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3551 DRM_DEBUG_KMS("FDI train 1 done.\n");
3552 break;
3553 }
3554 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003555 }
Sean Paulfa37d392012-03-02 12:53:39 -05003556 if (retry < 5)
3557 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003558 }
3559 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003560 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003561
3562 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003563 reg = FDI_TX_CTL(pipe);
3564 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003565 temp &= ~FDI_LINK_TRAIN_NONE;
3566 temp |= FDI_LINK_TRAIN_PATTERN_2;
3567 if (IS_GEN6(dev)) {
3568 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3569 /* SNB-B */
3570 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3571 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003572 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003573
Chris Wilson5eddb702010-09-11 13:48:45 +01003574 reg = FDI_RX_CTL(pipe);
3575 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003576 if (HAS_PCH_CPT(dev)) {
3577 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3578 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3579 } else {
3580 temp &= ~FDI_LINK_TRAIN_NONE;
3581 temp |= FDI_LINK_TRAIN_PATTERN_2;
3582 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003583 I915_WRITE(reg, temp);
3584
3585 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003586 udelay(150);
3587
Akshay Joshi0206e352011-08-16 15:34:10 -04003588 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003589 reg = FDI_TX_CTL(pipe);
3590 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003591 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3592 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003593 I915_WRITE(reg, temp);
3594
3595 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 udelay(500);
3597
Sean Paulfa37d392012-03-02 12:53:39 -05003598 for (retry = 0; retry < 5; retry++) {
3599 reg = FDI_RX_IIR(pipe);
3600 temp = I915_READ(reg);
3601 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3602 if (temp & FDI_RX_SYMBOL_LOCK) {
3603 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3604 DRM_DEBUG_KMS("FDI train 2 done.\n");
3605 break;
3606 }
3607 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003608 }
Sean Paulfa37d392012-03-02 12:53:39 -05003609 if (retry < 5)
3610 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003611 }
3612 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003613 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003614
3615 DRM_DEBUG_KMS("FDI train done.\n");
3616}
3617
Jesse Barnes357555c2011-04-28 15:09:55 -07003618/* Manual link training for Ivy Bridge A0 parts */
3619static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3620{
3621 struct drm_device *dev = crtc->dev;
3622 struct drm_i915_private *dev_priv = dev->dev_private;
3623 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3624 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003625 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003626
3627 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3628 for train result */
3629 reg = FDI_RX_IMR(pipe);
3630 temp = I915_READ(reg);
3631 temp &= ~FDI_RX_SYMBOL_LOCK;
3632 temp &= ~FDI_RX_BIT_LOCK;
3633 I915_WRITE(reg, temp);
3634
3635 POSTING_READ(reg);
3636 udelay(150);
3637
Daniel Vetter01a415f2012-10-27 15:58:40 +02003638 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3639 I915_READ(FDI_RX_IIR(pipe)));
3640
Jesse Barnes139ccd32013-08-19 11:04:55 -07003641 /* Try each vswing and preemphasis setting twice before moving on */
3642 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3643 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003644 reg = FDI_TX_CTL(pipe);
3645 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3647 temp &= ~FDI_TX_ENABLE;
3648 I915_WRITE(reg, temp);
3649
3650 reg = FDI_RX_CTL(pipe);
3651 temp = I915_READ(reg);
3652 temp &= ~FDI_LINK_TRAIN_AUTO;
3653 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3654 temp &= ~FDI_RX_ENABLE;
3655 I915_WRITE(reg, temp);
3656
3657 /* enable CPU FDI TX and PCH FDI RX */
3658 reg = FDI_TX_CTL(pipe);
3659 temp = I915_READ(reg);
3660 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003661 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003662 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003663 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003664 temp |= snb_b_fdi_train_param[j/2];
3665 temp |= FDI_COMPOSITE_SYNC;
3666 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3667
3668 I915_WRITE(FDI_RX_MISC(pipe),
3669 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3670
3671 reg = FDI_RX_CTL(pipe);
3672 temp = I915_READ(reg);
3673 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3674 temp |= FDI_COMPOSITE_SYNC;
3675 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3676
3677 POSTING_READ(reg);
3678 udelay(1); /* should be 0.5us */
3679
3680 for (i = 0; i < 4; i++) {
3681 reg = FDI_RX_IIR(pipe);
3682 temp = I915_READ(reg);
3683 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3684
3685 if (temp & FDI_RX_BIT_LOCK ||
3686 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3687 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3688 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3689 i);
3690 break;
3691 }
3692 udelay(1); /* should be 0.5us */
3693 }
3694 if (i == 4) {
3695 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3696 continue;
3697 }
3698
3699 /* Train 2 */
3700 reg = FDI_TX_CTL(pipe);
3701 temp = I915_READ(reg);
3702 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3703 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3704 I915_WRITE(reg, temp);
3705
3706 reg = FDI_RX_CTL(pipe);
3707 temp = I915_READ(reg);
3708 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3709 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003710 I915_WRITE(reg, temp);
3711
3712 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003713 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003714
Jesse Barnes139ccd32013-08-19 11:04:55 -07003715 for (i = 0; i < 4; i++) {
3716 reg = FDI_RX_IIR(pipe);
3717 temp = I915_READ(reg);
3718 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003719
Jesse Barnes139ccd32013-08-19 11:04:55 -07003720 if (temp & FDI_RX_SYMBOL_LOCK ||
3721 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3722 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3723 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3724 i);
3725 goto train_done;
3726 }
3727 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003728 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003729 if (i == 4)
3730 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003731 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003732
Jesse Barnes139ccd32013-08-19 11:04:55 -07003733train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003734 DRM_DEBUG_KMS("FDI train done.\n");
3735}
3736
Daniel Vetter88cefb62012-08-12 19:27:14 +02003737static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003738{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003739 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003740 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003741 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003742 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003743
Jesse Barnesc64e3112010-09-10 11:27:03 -07003744
Jesse Barnes0e23b992010-09-10 11:10:00 -07003745 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003746 reg = FDI_RX_CTL(pipe);
3747 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003748 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003749 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003750 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003751 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3752
3753 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003754 udelay(200);
3755
3756 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003757 temp = I915_READ(reg);
3758 I915_WRITE(reg, temp | FDI_PCDCLK);
3759
3760 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003761 udelay(200);
3762
Paulo Zanoni20749732012-11-23 15:30:38 -02003763 /* Enable CPU FDI TX PLL, always on for Ironlake */
3764 reg = FDI_TX_CTL(pipe);
3765 temp = I915_READ(reg);
3766 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3767 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003768
Paulo Zanoni20749732012-11-23 15:30:38 -02003769 POSTING_READ(reg);
3770 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003771 }
3772}
3773
Daniel Vetter88cefb62012-08-12 19:27:14 +02003774static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3775{
3776 struct drm_device *dev = intel_crtc->base.dev;
3777 struct drm_i915_private *dev_priv = dev->dev_private;
3778 int pipe = intel_crtc->pipe;
3779 u32 reg, temp;
3780
3781 /* Switch from PCDclk to Rawclk */
3782 reg = FDI_RX_CTL(pipe);
3783 temp = I915_READ(reg);
3784 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3785
3786 /* Disable CPU FDI TX PLL */
3787 reg = FDI_TX_CTL(pipe);
3788 temp = I915_READ(reg);
3789 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3790
3791 POSTING_READ(reg);
3792 udelay(100);
3793
3794 reg = FDI_RX_CTL(pipe);
3795 temp = I915_READ(reg);
3796 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3797
3798 /* Wait for the clocks to turn off. */
3799 POSTING_READ(reg);
3800 udelay(100);
3801}
3802
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003803static void ironlake_fdi_disable(struct drm_crtc *crtc)
3804{
3805 struct drm_device *dev = crtc->dev;
3806 struct drm_i915_private *dev_priv = dev->dev_private;
3807 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3808 int pipe = intel_crtc->pipe;
3809 u32 reg, temp;
3810
3811 /* disable CPU FDI tx and PCH FDI rx */
3812 reg = FDI_TX_CTL(pipe);
3813 temp = I915_READ(reg);
3814 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3815 POSTING_READ(reg);
3816
3817 reg = FDI_RX_CTL(pipe);
3818 temp = I915_READ(reg);
3819 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003820 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003821 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3822
3823 POSTING_READ(reg);
3824 udelay(100);
3825
3826 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003827 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003828 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003829
3830 /* still set train pattern 1 */
3831 reg = FDI_TX_CTL(pipe);
3832 temp = I915_READ(reg);
3833 temp &= ~FDI_LINK_TRAIN_NONE;
3834 temp |= FDI_LINK_TRAIN_PATTERN_1;
3835 I915_WRITE(reg, temp);
3836
3837 reg = FDI_RX_CTL(pipe);
3838 temp = I915_READ(reg);
3839 if (HAS_PCH_CPT(dev)) {
3840 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3841 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3842 } else {
3843 temp &= ~FDI_LINK_TRAIN_NONE;
3844 temp |= FDI_LINK_TRAIN_PATTERN_1;
3845 }
3846 /* BPC in FDI rx is consistent with that in PIPECONF */
3847 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003848 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003849 I915_WRITE(reg, temp);
3850
3851 POSTING_READ(reg);
3852 udelay(100);
3853}
3854
Chris Wilson5dce5b932014-01-20 10:17:36 +00003855bool intel_has_pending_fb_unpin(struct drm_device *dev)
3856{
3857 struct intel_crtc *crtc;
3858
3859 /* Note that we don't need to be called with mode_config.lock here
3860 * as our list of CRTC objects is static for the lifetime of the
3861 * device and so cannot disappear as we iterate. Similarly, we can
3862 * happily treat the predicates as racy, atomic checks as userspace
3863 * cannot claim and pin a new fb without at least acquring the
3864 * struct_mutex and so serialising with us.
3865 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003866 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003867 if (atomic_read(&crtc->unpin_work_count) == 0)
3868 continue;
3869
3870 if (crtc->unpin_work)
3871 intel_wait_for_vblank(dev, crtc->pipe);
3872
3873 return true;
3874 }
3875
3876 return false;
3877}
3878
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003879static void page_flip_completed(struct intel_crtc *intel_crtc)
3880{
3881 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3882 struct intel_unpin_work *work = intel_crtc->unpin_work;
3883
3884 /* ensure that the unpin work is consistent wrt ->pending. */
3885 smp_rmb();
3886 intel_crtc->unpin_work = NULL;
3887
3888 if (work->event)
3889 drm_send_vblank_event(intel_crtc->base.dev,
3890 intel_crtc->pipe,
3891 work->event);
3892
3893 drm_crtc_vblank_put(&intel_crtc->base);
3894
3895 wake_up_all(&dev_priv->pending_flip_queue);
3896 queue_work(dev_priv->wq, &work->work);
3897
3898 trace_i915_flip_complete(intel_crtc->plane,
3899 work->pending_flip_obj);
3900}
3901
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003902void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003903{
Chris Wilson0f911282012-04-17 10:05:38 +01003904 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003905 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003906
Daniel Vetter2c10d572012-12-20 21:24:07 +01003907 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003908 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3909 !intel_crtc_has_pending_flip(crtc),
3910 60*HZ) == 0)) {
3911 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003912
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003913 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003914 if (intel_crtc->unpin_work) {
3915 WARN_ONCE(1, "Removing stuck page flip\n");
3916 page_flip_completed(intel_crtc);
3917 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003918 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003919 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003920
Chris Wilson975d5682014-08-20 13:13:34 +01003921 if (crtc->primary->fb) {
3922 mutex_lock(&dev->struct_mutex);
3923 intel_finish_fb(crtc->primary->fb);
3924 mutex_unlock(&dev->struct_mutex);
3925 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003926}
3927
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003928/* Program iCLKIP clock to the desired frequency */
3929static void lpt_program_iclkip(struct drm_crtc *crtc)
3930{
3931 struct drm_device *dev = crtc->dev;
3932 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003933 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003934 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3935 u32 temp;
3936
Ville Syrjäläa5805162015-05-26 20:42:30 +03003937 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003938
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939 /* It is necessary to ungate the pixclk gate prior to programming
3940 * the divisors, and gate it back when it is done.
3941 */
3942 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3943
3944 /* Disable SSCCTL */
3945 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003946 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3947 SBI_SSCCTL_DISABLE,
3948 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003949
3950 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003951 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003952 auxdiv = 1;
3953 divsel = 0x41;
3954 phaseinc = 0x20;
3955 } else {
3956 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003957 * but the adjusted_mode->crtc_clock in in KHz. To get the
3958 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003959 * convert the virtual clock precision to KHz here for higher
3960 * precision.
3961 */
3962 u32 iclk_virtual_root_freq = 172800 * 1000;
3963 u32 iclk_pi_range = 64;
3964 u32 desired_divisor, msb_divisor_value, pi_value;
3965
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003966 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003967 msb_divisor_value = desired_divisor / iclk_pi_range;
3968 pi_value = desired_divisor % iclk_pi_range;
3969
3970 auxdiv = 0;
3971 divsel = msb_divisor_value - 2;
3972 phaseinc = pi_value;
3973 }
3974
3975 /* This should not happen with any sane values */
3976 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3977 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3978 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3979 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3980
3981 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 +03003982 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003983 auxdiv,
3984 divsel,
3985 phasedir,
3986 phaseinc);
3987
3988 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003989 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003990 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3991 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3992 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3993 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3994 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3995 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003996 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003997
3998 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003999 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004000 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4001 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004002 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004003
4004 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004005 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004006 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004007 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004008
4009 /* Wait for initialization time */
4010 udelay(24);
4011
4012 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004013
Ville Syrjäläa5805162015-05-26 20:42:30 +03004014 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004015}
4016
Daniel Vetter275f01b22013-05-03 11:49:47 +02004017static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4018 enum pipe pch_transcoder)
4019{
4020 struct drm_device *dev = crtc->base.dev;
4021 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004022 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004023
4024 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4025 I915_READ(HTOTAL(cpu_transcoder)));
4026 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4027 I915_READ(HBLANK(cpu_transcoder)));
4028 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4029 I915_READ(HSYNC(cpu_transcoder)));
4030
4031 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4032 I915_READ(VTOTAL(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4034 I915_READ(VBLANK(cpu_transcoder)));
4035 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4036 I915_READ(VSYNC(cpu_transcoder)));
4037 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4038 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4039}
4040
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004041static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004042{
4043 struct drm_i915_private *dev_priv = dev->dev_private;
4044 uint32_t temp;
4045
4046 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004047 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004048 return;
4049
4050 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4051 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4052
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004053 temp &= ~FDI_BC_BIFURCATION_SELECT;
4054 if (enable)
4055 temp |= FDI_BC_BIFURCATION_SELECT;
4056
4057 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004058 I915_WRITE(SOUTH_CHICKEN1, temp);
4059 POSTING_READ(SOUTH_CHICKEN1);
4060}
4061
4062static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4063{
4064 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004065
4066 switch (intel_crtc->pipe) {
4067 case PIPE_A:
4068 break;
4069 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004070 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004071 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004072 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004073 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004074
4075 break;
4076 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004077 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004078
4079 break;
4080 default:
4081 BUG();
4082 }
4083}
4084
Jesse Barnesf67a5592011-01-05 10:31:48 -08004085/*
4086 * Enable PCH resources required for PCH ports:
4087 * - PCH PLLs
4088 * - FDI training & RX/TX
4089 * - update transcoder timings
4090 * - DP transcoding bits
4091 * - transcoder
4092 */
4093static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004094{
4095 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004096 struct drm_i915_private *dev_priv = dev->dev_private;
4097 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4098 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004099 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004100
Daniel Vetterab9412b2013-05-03 11:49:46 +02004101 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004102
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004103 if (IS_IVYBRIDGE(dev))
4104 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4105
Daniel Vettercd986ab2012-10-26 10:58:12 +02004106 /* Write the TU size bits before fdi link training, so that error
4107 * detection works. */
4108 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4109 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4110
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004111 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004112 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004113
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004114 /* We need to program the right clock selection before writing the pixel
4115 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004116 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004117 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004118
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004119 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004120 temp |= TRANS_DPLL_ENABLE(pipe);
4121 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004122 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004123 temp |= sel;
4124 else
4125 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004126 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004127 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004129 /* XXX: pch pll's can be enabled any time before we enable the PCH
4130 * transcoder, and we actually should do this to not upset any PCH
4131 * transcoder that already use the clock when we share it.
4132 *
4133 * Note that enable_shared_dpll tries to do the right thing, but
4134 * get_shared_dpll unconditionally resets the pll - we need that to have
4135 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004136 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004137
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004138 /* set transcoder timing, panel must allow it */
4139 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004140 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004141
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004142 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004143
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004144 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004145 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004146 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004147 reg = TRANS_DP_CTL(pipe);
4148 temp = I915_READ(reg);
4149 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004150 TRANS_DP_SYNC_MASK |
4151 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004152 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004153 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004154
4155 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004156 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004158 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159
4160 switch (intel_trans_dp_port_sel(crtc)) {
4161 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163 break;
4164 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004165 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166 break;
4167 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004168 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004169 break;
4170 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004171 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172 }
4173
Chris Wilson5eddb702010-09-11 13:48:45 +01004174 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004175 }
4176
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004177 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004178}
4179
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004180static void lpt_pch_enable(struct drm_crtc *crtc)
4181{
4182 struct drm_device *dev = crtc->dev;
4183 struct drm_i915_private *dev_priv = dev->dev_private;
4184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004185 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186
Daniel Vetterab9412b2013-05-03 11:49:46 +02004187 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004188
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004189 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004190
Paulo Zanoni0540e482012-10-31 18:12:40 -02004191 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004192 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004193
Paulo Zanoni937bb612012-10-31 18:12:47 -02004194 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004195}
4196
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004197struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4198 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004199{
Daniel Vettere2b78262013-06-07 23:10:03 +02004200 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004201 struct intel_shared_dpll *pll;
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004202 struct intel_shared_dpll_config *shared_dpll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004203 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004204
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004205 shared_dpll = intel_atomic_get_shared_dpll_state(crtc_state->base.state);
4206
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004207 if (HAS_PCH_IBX(dev_priv->dev)) {
4208 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004209 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004210 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004211
Daniel Vetter46edb022013-06-05 13:34:12 +02004212 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4213 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004214
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004215 WARN_ON(shared_dpll[i].crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004216
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004217 goto found;
4218 }
4219
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304220 if (IS_BROXTON(dev_priv->dev)) {
4221 /* PLL is attached to port in bxt */
4222 struct intel_encoder *encoder;
4223 struct intel_digital_port *intel_dig_port;
4224
4225 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4226 if (WARN_ON(!encoder))
4227 return NULL;
4228
4229 intel_dig_port = enc_to_dig_port(&encoder->base);
4230 /* 1:1 mapping between ports and PLLs */
4231 i = (enum intel_dpll_id)intel_dig_port->port;
4232 pll = &dev_priv->shared_dplls[i];
4233 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4234 crtc->base.base.id, pll->name);
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004235 WARN_ON(shared_dpll[i].crtc_mask);
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304236
4237 goto found;
4238 }
4239
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004240 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4241 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004242
4243 /* Only want to check enabled timings first */
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004244 if (shared_dpll[i].crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004245 continue;
4246
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004247 if (memcmp(&crtc_state->dpll_hw_state,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004248 &shared_dpll[i].hw_state,
4249 sizeof(crtc_state->dpll_hw_state)) == 0) {
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004250 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004251 crtc->base.base.id, pll->name,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004252 shared_dpll[i].crtc_mask,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004253 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004254 goto found;
4255 }
4256 }
4257
4258 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004259 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4260 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004261 if (shared_dpll[i].crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004262 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4263 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004264 goto found;
4265 }
4266 }
4267
4268 return NULL;
4269
4270found:
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004271 if (shared_dpll[i].crtc_mask == 0)
4272 shared_dpll[i].hw_state =
4273 crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004274
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004275 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004276 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4277 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004278
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004279 shared_dpll[i].crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004280
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004281 return pll;
4282}
4283
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004284static void intel_shared_dpll_commit(struct drm_atomic_state *state)
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004285{
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004286 struct drm_i915_private *dev_priv = to_i915(state->dev);
4287 struct intel_shared_dpll_config *shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004288 struct intel_shared_dpll *pll;
4289 enum intel_dpll_id i;
4290
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004291 if (!to_intel_atomic_state(state)->dpll_set)
4292 return;
4293
4294 shared_dpll = to_intel_atomic_state(state)->shared_dpll;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004295 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4296 pll = &dev_priv->shared_dplls[i];
Maarten Lankhorstde419ab2015-06-04 10:21:28 +02004297 pll->config = shared_dpll[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004298 }
4299}
4300
Daniel Vettera1520312013-05-03 11:49:50 +02004301static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004302{
4303 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004304 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004305 u32 temp;
4306
4307 temp = I915_READ(dslreg);
4308 udelay(500);
4309 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004310 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004311 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004312 }
4313}
4314
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004315static int
4316skl_update_scaler(struct intel_crtc_state *crtc_state, bool force_detach,
4317 unsigned scaler_user, int *scaler_id, unsigned int rotation,
4318 int src_w, int src_h, int dst_w, int dst_h)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004319{
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004320 struct intel_crtc_scaler_state *scaler_state =
4321 &crtc_state->scaler_state;
4322 struct intel_crtc *intel_crtc =
4323 to_intel_crtc(crtc_state->base.crtc);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004324 int need_scaling;
Chandra Konduru6156a452015-04-27 13:48:39 -07004325
4326 need_scaling = intel_rotation_90_or_270(rotation) ?
4327 (src_h != dst_w || src_w != dst_h):
4328 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004329
4330 /*
4331 * if plane is being disabled or scaler is no more required or force detach
4332 * - free scaler binded to this plane/crtc
4333 * - in order to do this, update crtc->scaler_usage
4334 *
4335 * Here scaler state in crtc_state is set free so that
4336 * scaler can be assigned to other user. Actual register
4337 * update to free the scaler is done in plane/panel-fit programming.
4338 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4339 */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004340 if (force_detach || !need_scaling) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004341 if (*scaler_id >= 0) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004342 scaler_state->scaler_users &= ~(1 << scaler_user);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004343 scaler_state->scalers[*scaler_id].in_use = 0;
4344
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004345 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4346 "Staged freeing scaler id %d scaler_users = 0x%x\n",
4347 intel_crtc->pipe, scaler_user, *scaler_id,
Chandra Kondurua1b22782015-04-07 15:28:45 -07004348 scaler_state->scaler_users);
4349 *scaler_id = -1;
4350 }
4351 return 0;
4352 }
4353
4354 /* range checks */
4355 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4356 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4357
4358 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4359 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004360 DRM_DEBUG_KMS("scaler_user index %u.%u: src %ux%u dst %ux%u "
Chandra Kondurua1b22782015-04-07 15:28:45 -07004361 "size is out of scaler range\n",
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004362 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004363 return -EINVAL;
4364 }
4365
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004366 /* mark this plane as a scaler user in crtc_state */
4367 scaler_state->scaler_users |= (1 << scaler_user);
4368 DRM_DEBUG_KMS("scaler_user index %u.%u: "
4369 "staged scaling request for %ux%u->%ux%u scaler_users = 0x%x\n",
4370 intel_crtc->pipe, scaler_user, src_w, src_h, dst_w, dst_h,
4371 scaler_state->scaler_users);
4372
4373 return 0;
4374}
4375
4376/**
4377 * skl_update_scaler_crtc - Stages update to scaler state for a given crtc.
4378 *
4379 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004380 *
4381 * Return
4382 * 0 - scaler_usage updated successfully
4383 * error - requested scaling cannot be supported or other error condition
4384 */
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004385int skl_update_scaler_crtc(struct intel_crtc_state *state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004386{
4387 struct intel_crtc *intel_crtc = to_intel_crtc(state->base.crtc);
4388 struct drm_display_mode *adjusted_mode =
4389 &state->base.adjusted_mode;
4390
4391 DRM_DEBUG_KMS("Updating scaler for [CRTC:%i] scaler_user index %u.%u\n",
4392 intel_crtc->base.base.id, intel_crtc->pipe, SKL_CRTC_INDEX);
4393
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004394 return skl_update_scaler(state, !state->base.active, SKL_CRTC_INDEX,
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004395 &state->scaler_state.scaler_id, DRM_ROTATE_0,
4396 state->pipe_src_w, state->pipe_src_h,
Imre Deak8c6cda22015-06-23 20:40:27 +03004397 adjusted_mode->hdisplay, adjusted_mode->vdisplay);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004398}
4399
4400/**
4401 * skl_update_scaler_plane - Stages update to scaler state for a given plane.
4402 *
4403 * @state: crtc's scaler state
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004404 * @plane_state: atomic plane state to update
4405 *
4406 * Return
4407 * 0 - scaler_usage updated successfully
4408 * error - requested scaling cannot be supported or other error condition
4409 */
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004410static int skl_update_scaler_plane(struct intel_crtc_state *crtc_state,
4411 struct intel_plane_state *plane_state)
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004412{
4413
4414 struct intel_crtc *intel_crtc = to_intel_crtc(crtc_state->base.crtc);
Maarten Lankhorstda20eab2015-06-15 12:33:44 +02004415 struct intel_plane *intel_plane =
4416 to_intel_plane(plane_state->base.plane);
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004417 struct drm_framebuffer *fb = plane_state->base.fb;
4418 int ret;
4419
4420 bool force_detach = !fb || !plane_state->visible;
4421
4422 DRM_DEBUG_KMS("Updating scaler for [PLANE:%d] scaler_user index %u.%u\n",
4423 intel_plane->base.base.id, intel_crtc->pipe,
4424 drm_plane_index(&intel_plane->base));
4425
4426 ret = skl_update_scaler(crtc_state, force_detach,
4427 drm_plane_index(&intel_plane->base),
4428 &plane_state->scaler_id,
4429 plane_state->base.rotation,
4430 drm_rect_width(&plane_state->src) >> 16,
4431 drm_rect_height(&plane_state->src) >> 16,
4432 drm_rect_width(&plane_state->dst),
4433 drm_rect_height(&plane_state->dst));
4434
4435 if (ret || plane_state->scaler_id < 0)
4436 return ret;
4437
Chandra Kondurua1b22782015-04-07 15:28:45 -07004438 /* check colorkey */
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004439 if (plane_state->ckey.flags != I915_SET_COLORKEY_NONE) {
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004440 DRM_DEBUG_KMS("[PLANE:%d] scaling with color key not allowed",
Maarten Lankhorst818ed962015-06-15 12:33:54 +02004441 intel_plane->base.base.id);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004442 return -EINVAL;
4443 }
4444
4445 /* Check src format */
Maarten Lankhorst86adf9d2015-06-22 09:50:32 +02004446 switch (fb->pixel_format) {
4447 case DRM_FORMAT_RGB565:
4448 case DRM_FORMAT_XBGR8888:
4449 case DRM_FORMAT_XRGB8888:
4450 case DRM_FORMAT_ABGR8888:
4451 case DRM_FORMAT_ARGB8888:
4452 case DRM_FORMAT_XRGB2101010:
4453 case DRM_FORMAT_XBGR2101010:
4454 case DRM_FORMAT_YUYV:
4455 case DRM_FORMAT_YVYU:
4456 case DRM_FORMAT_UYVY:
4457 case DRM_FORMAT_VYUY:
4458 break;
4459 default:
4460 DRM_DEBUG_KMS("[PLANE:%d] FB:%d unsupported scaling format 0x%x\n",
4461 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4462 return -EINVAL;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004463 }
4464
Chandra Kondurua1b22782015-04-07 15:28:45 -07004465 return 0;
4466}
4467
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004468static void skylake_scaler_disable(struct intel_crtc *crtc)
4469{
4470 int i;
4471
4472 for (i = 0; i < crtc->num_scalers; i++)
4473 skl_detach_scaler(crtc, i);
4474}
4475
4476static void skylake_pfit_enable(struct intel_crtc *crtc)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004477{
4478 struct drm_device *dev = crtc->base.dev;
4479 struct drm_i915_private *dev_priv = dev->dev_private;
4480 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004481 struct intel_crtc_scaler_state *scaler_state =
4482 &crtc->config->scaler_state;
4483
4484 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4485
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004486 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004487 int id;
4488
4489 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4490 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4491 return;
4492 }
4493
4494 id = scaler_state->scaler_id;
4495 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4496 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4497 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4498 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4499
4500 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004501 }
4502}
4503
Jesse Barnesb074cec2013-04-25 12:55:02 -07004504static void ironlake_pfit_enable(struct intel_crtc *crtc)
4505{
4506 struct drm_device *dev = crtc->base.dev;
4507 struct drm_i915_private *dev_priv = dev->dev_private;
4508 int pipe = crtc->pipe;
4509
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004510 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004511 /* Force use of hard-coded filter coefficients
4512 * as some pre-programmed values are broken,
4513 * e.g. x201.
4514 */
4515 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4516 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4517 PF_PIPE_SEL_IVB(pipe));
4518 else
4519 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004520 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4521 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004522 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004523}
4524
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004525void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004526{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004527 struct drm_device *dev = crtc->base.dev;
4528 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004529
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004530 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004531 return;
4532
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004533 /* We can only enable IPS after we enable a plane and wait for a vblank */
4534 intel_wait_for_vblank(dev, crtc->pipe);
4535
Paulo Zanonid77e4532013-09-24 13:52:55 -03004536 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004537 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004538 mutex_lock(&dev_priv->rps.hw_lock);
4539 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4540 mutex_unlock(&dev_priv->rps.hw_lock);
4541 /* Quoting Art Runyan: "its not safe to expect any particular
4542 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004543 * mailbox." Moreover, the mailbox may return a bogus state,
4544 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004545 */
4546 } else {
4547 I915_WRITE(IPS_CTL, IPS_ENABLE);
4548 /* The bit only becomes 1 in the next vblank, so this wait here
4549 * is essentially intel_wait_for_vblank. If we don't have this
4550 * and don't wait for vblanks until the end of crtc_enable, then
4551 * the HW state readout code will complain that the expected
4552 * IPS_CTL value is not the one we read. */
4553 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4554 DRM_ERROR("Timed out waiting for IPS enable\n");
4555 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004556}
4557
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004558void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004559{
4560 struct drm_device *dev = crtc->base.dev;
4561 struct drm_i915_private *dev_priv = dev->dev_private;
4562
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004563 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004564 return;
4565
4566 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004567 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004568 mutex_lock(&dev_priv->rps.hw_lock);
4569 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4570 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004571 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4572 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4573 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004574 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004575 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004576 POSTING_READ(IPS_CTL);
4577 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004578
4579 /* We need to wait for a vblank before we can disable the plane. */
4580 intel_wait_for_vblank(dev, crtc->pipe);
4581}
4582
4583/** Loads the palette/gamma unit for the CRTC with the prepared values */
4584static void intel_crtc_load_lut(struct drm_crtc *crtc)
4585{
4586 struct drm_device *dev = crtc->dev;
4587 struct drm_i915_private *dev_priv = dev->dev_private;
4588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4589 enum pipe pipe = intel_crtc->pipe;
4590 int palreg = PALETTE(pipe);
4591 int i;
4592 bool reenable_ips = false;
4593
4594 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004595 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004596 return;
4597
Imre Deak50360402015-01-16 00:55:16 -08004598 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004599 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004600 assert_dsi_pll_enabled(dev_priv);
4601 else
4602 assert_pll_enabled(dev_priv, pipe);
4603 }
4604
4605 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304606 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004607 palreg = LGC_PALETTE(pipe);
4608
4609 /* Workaround : Do not read or write the pipe palette/gamma data while
4610 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4611 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004612 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004613 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4614 GAMMA_MODE_MODE_SPLIT)) {
4615 hsw_disable_ips(intel_crtc);
4616 reenable_ips = true;
4617 }
4618
4619 for (i = 0; i < 256; i++) {
4620 I915_WRITE(palreg + 4 * i,
4621 (intel_crtc->lut_r[i] << 16) |
4622 (intel_crtc->lut_g[i] << 8) |
4623 intel_crtc->lut_b[i]);
4624 }
4625
4626 if (reenable_ips)
4627 hsw_enable_ips(intel_crtc);
4628}
4629
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004630static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004631{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004632 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004633 struct drm_device *dev = intel_crtc->base.dev;
4634 struct drm_i915_private *dev_priv = dev->dev_private;
4635
4636 mutex_lock(&dev->struct_mutex);
4637 dev_priv->mm.interruptible = false;
4638 (void) intel_overlay_switch_off(intel_crtc->overlay);
4639 dev_priv->mm.interruptible = true;
4640 mutex_unlock(&dev->struct_mutex);
4641 }
4642
4643 /* Let userspace switch the overlay on again. In most cases userspace
4644 * has to recompute where to put it anyway.
4645 */
4646}
4647
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004648/**
4649 * intel_post_enable_primary - Perform operations after enabling primary plane
4650 * @crtc: the CRTC whose primary plane was just enabled
4651 *
4652 * Performs potentially sleeping operations that must be done after the primary
4653 * plane is enabled, such as updating FBC and IPS. Note that this may be
4654 * called due to an explicit primary plane update, or due to an implicit
4655 * re-enable that is caused when a sprite plane is updated to no longer
4656 * completely hide the primary plane.
4657 */
4658static void
4659intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004660{
4661 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004662 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4664 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004665
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004666 /*
4667 * BDW signals flip done immediately if the plane
4668 * is disabled, even if the plane enable is already
4669 * armed to occur at the next vblank :(
4670 */
4671 if (IS_BROADWELL(dev))
4672 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004673
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004674 /*
4675 * FIXME IPS should be fine as long as one plane is
4676 * enabled, but in practice it seems to have problems
4677 * when going from primary only to sprite only and vice
4678 * versa.
4679 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004680 hsw_enable_ips(intel_crtc);
4681
Daniel Vetterf99d7062014-06-19 16:01:59 +02004682 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004683 * Gen2 reports pipe underruns whenever all planes are disabled.
4684 * So don't enable underrun reporting before at least some planes
4685 * are enabled.
4686 * FIXME: Need to fix the logic to work when we turn off all planes
4687 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004688 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004689 if (IS_GEN2(dev))
4690 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4691
4692 /* Underruns don't raise interrupts, so check manually. */
4693 if (HAS_GMCH_DISPLAY(dev))
4694 i9xx_check_fifo_underruns(dev_priv);
4695}
4696
4697/**
4698 * intel_pre_disable_primary - Perform operations before disabling primary plane
4699 * @crtc: the CRTC whose primary plane is to be disabled
4700 *
4701 * Performs potentially sleeping operations that must be done before the
4702 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4703 * be called due to an explicit primary plane update, or due to an implicit
4704 * disable that is caused when a sprite plane completely hides the primary
4705 * plane.
4706 */
4707static void
4708intel_pre_disable_primary(struct drm_crtc *crtc)
4709{
4710 struct drm_device *dev = crtc->dev;
4711 struct drm_i915_private *dev_priv = dev->dev_private;
4712 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4713 int pipe = intel_crtc->pipe;
4714
4715 /*
4716 * Gen2 reports pipe underruns whenever all planes are disabled.
4717 * So diasble underrun reporting before all the planes get disabled.
4718 * FIXME: Need to fix the logic to work when we turn off all planes
4719 * but leave the pipe running.
4720 */
4721 if (IS_GEN2(dev))
4722 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4723
4724 /*
4725 * Vblank time updates from the shadow to live plane control register
4726 * are blocked if the memory self-refresh mode is active at that
4727 * moment. So to make sure the plane gets truly disabled, disable
4728 * first the self-refresh mode. The self-refresh enable bit in turn
4729 * will be checked/applied by the HW only at the next frame start
4730 * event which is after the vblank start event, so we need to have a
4731 * wait-for-vblank between disabling the plane and the pipe.
4732 */
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004733 if (HAS_GMCH_DISPLAY(dev)) {
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004734 intel_set_memory_cxsr(dev_priv, false);
Ville Syrjälä262cd2e2015-06-24 22:00:04 +03004735 dev_priv->wm.vlv.cxsr = false;
4736 intel_wait_for_vblank(dev, pipe);
4737 }
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004738
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004739 /*
4740 * FIXME IPS should be fine as long as one plane is
4741 * enabled, but in practice it seems to have problems
4742 * when going from primary only to sprite only and vice
4743 * versa.
4744 */
4745 hsw_disable_ips(intel_crtc);
4746}
4747
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004748static void intel_post_plane_update(struct intel_crtc *crtc)
4749{
4750 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4751 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -03004752 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004753 struct drm_plane *plane;
4754
4755 if (atomic->wait_vblank)
4756 intel_wait_for_vblank(dev, crtc->pipe);
4757
4758 intel_frontbuffer_flip(dev, atomic->fb_bits);
4759
Ville Syrjälä852eb002015-06-24 22:00:07 +03004760 if (atomic->disable_cxsr)
4761 crtc->wm.cxsr_allowed = true;
4762
Ville Syrjäläf015c552015-06-24 22:00:02 +03004763 if (crtc->atomic.update_wm_post)
4764 intel_update_watermarks(&crtc->base);
4765
Paulo Zanonic80ac852015-07-02 19:25:13 -03004766 if (atomic->update_fbc)
Paulo Zanoni7733b492015-07-07 15:26:04 -03004767 intel_fbc_update(dev_priv);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004768
4769 if (atomic->post_enable_primary)
4770 intel_post_enable_primary(&crtc->base);
4771
4772 drm_for_each_plane_mask(plane, dev, atomic->update_sprite_watermarks)
4773 intel_update_sprite_watermarks(plane, &crtc->base,
4774 0, 0, 0, false, false);
4775
4776 memset(atomic, 0, sizeof(*atomic));
4777}
4778
4779static void intel_pre_plane_update(struct intel_crtc *crtc)
4780{
4781 struct drm_device *dev = crtc->base.dev;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +02004782 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004783 struct intel_crtc_atomic_commit *atomic = &crtc->atomic;
4784 struct drm_plane *p;
4785
4786 /* Track fb's for any planes being disabled */
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004787 drm_for_each_plane_mask(p, dev, atomic->disabled_planes) {
4788 struct intel_plane *plane = to_intel_plane(p);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004789
4790 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +03004791 i915_gem_track_fb(intel_fb_obj(plane->base.fb), NULL,
4792 plane->frontbuffer_bit);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004793 mutex_unlock(&dev->struct_mutex);
4794 }
4795
4796 if (atomic->wait_for_flips)
4797 intel_crtc_wait_for_pending_flips(&crtc->base);
4798
Paulo Zanonic80ac852015-07-02 19:25:13 -03004799 if (atomic->disable_fbc)
Paulo Zanoni25ad93f2015-07-02 19:25:10 -03004800 intel_fbc_disable_crtc(crtc);
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004801
Rodrigo Vivi066cf552015-06-26 13:55:54 -07004802 if (crtc->atomic.disable_ips)
4803 hsw_disable_ips(crtc);
4804
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004805 if (atomic->pre_disable_primary)
4806 intel_pre_disable_primary(&crtc->base);
Ville Syrjälä852eb002015-06-24 22:00:07 +03004807
4808 if (atomic->disable_cxsr) {
4809 crtc->wm.cxsr_allowed = false;
4810 intel_set_memory_cxsr(dev_priv, false);
4811 }
Maarten Lankhorstac21b222015-06-15 12:33:49 +02004812}
4813
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004814static void intel_crtc_disable_planes(struct drm_crtc *crtc, unsigned plane_mask)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004815{
4816 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004817 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004818 struct drm_plane *p;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004819 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004820
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004821 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004822
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02004823 drm_for_each_plane_mask(p, dev, plane_mask)
4824 to_intel_plane(p)->disable_plane(p, crtc);
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004825
Daniel Vetterf99d7062014-06-19 16:01:59 +02004826 /*
4827 * FIXME: Once we grow proper nuclear flip support out of this we need
4828 * to compute the mask of flip planes precisely. For the time being
4829 * consider this a flip to a NULL plane.
4830 */
4831 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004832}
4833
Jesse Barnesf67a5592011-01-05 10:31:48 -08004834static void ironlake_crtc_enable(struct drm_crtc *crtc)
4835{
4836 struct drm_device *dev = crtc->dev;
4837 struct drm_i915_private *dev_priv = dev->dev_private;
4838 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004839 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004840 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004841
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004842 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004843 return;
4844
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004845 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004846 intel_prepare_shared_dpll(intel_crtc);
4847
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004848 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304849 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004850
4851 intel_set_pipe_timings(intel_crtc);
4852
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004853 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004854 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004855 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004856 }
4857
4858 ironlake_set_pipeconf(crtc);
4859
Jesse Barnesf67a5592011-01-05 10:31:48 -08004860 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004861
Daniel Vettera72e4c92014-09-30 10:56:47 +02004862 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4863 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004864
Daniel Vetterf6736a12013-06-05 13:34:30 +02004865 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004866 if (encoder->pre_enable)
4867 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004868
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004869 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004870 /* Note: FDI PLL enabling _must_ be done before we enable the
4871 * cpu pipes, hence this is separate from all the other fdi/pch
4872 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004873 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004874 } else {
4875 assert_fdi_tx_disabled(dev_priv, pipe);
4876 assert_fdi_rx_disabled(dev_priv, pipe);
4877 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004878
Jesse Barnesb074cec2013-04-25 12:55:02 -07004879 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004880
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004881 /*
4882 * On ILK+ LUT must be loaded before the pipe is running but with
4883 * clocks enabled
4884 */
4885 intel_crtc_load_lut(crtc);
4886
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004887 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004888 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004889
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004890 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004891 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004892
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004893 assert_vblank_disabled(crtc);
4894 drm_crtc_vblank_on(crtc);
4895
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004896 for_each_encoder_on_crtc(dev, crtc, encoder)
4897 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004898
4899 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004900 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004901}
4902
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004903/* IPS only exists on ULT machines and is tied to pipe A. */
4904static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4905{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004906 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004907}
4908
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004909static void haswell_crtc_enable(struct drm_crtc *crtc)
4910{
4911 struct drm_device *dev = crtc->dev;
4912 struct drm_i915_private *dev_priv = dev->dev_private;
4913 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4914 struct intel_encoder *encoder;
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004915 int pipe = intel_crtc->pipe, hsw_workaround_pipe;
4916 struct intel_crtc_state *pipe_config =
4917 to_intel_crtc_state(crtc->state);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004918
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004919 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004920 return;
4921
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004922 if (intel_crtc_to_shared_dpll(intel_crtc))
4923 intel_enable_shared_dpll(intel_crtc);
4924
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004925 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304926 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004927
4928 intel_set_pipe_timings(intel_crtc);
4929
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004930 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4931 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4932 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004933 }
4934
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004935 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004936 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004937 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004938 }
4939
4940 haswell_set_pipeconf(crtc);
4941
4942 intel_set_pipe_csc(crtc);
4943
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004944 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004945
Daniel Vettera72e4c92014-09-30 10:56:47 +02004946 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004947 for_each_encoder_on_crtc(dev, crtc, encoder)
4948 if (encoder->pre_enable)
4949 encoder->pre_enable(encoder);
4950
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004951 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004952 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4953 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004954 dev_priv->display.fdi_link_train(crtc);
4955 }
4956
Paulo Zanoni1f544382012-10-24 11:32:00 -02004957 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004958
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004959 if (INTEL_INFO(dev)->gen == 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02004960 skylake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004961 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004962 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004963 else
4964 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004965
4966 /*
4967 * On ILK+ LUT must be loaded before the pipe is running but with
4968 * clocks enabled
4969 */
4970 intel_crtc_load_lut(crtc);
4971
Paulo Zanoni1f544382012-10-24 11:32:00 -02004972 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00004973 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004974
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004975 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004976 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004977
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004978 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004979 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004980
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004981 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10004982 intel_ddi_set_vc_payload_alloc(crtc, true);
4983
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004984 assert_vblank_disabled(crtc);
4985 drm_crtc_vblank_on(crtc);
4986
Jani Nikula8807e552013-08-30 19:40:32 +03004987 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004988 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03004989 intel_opregion_notify_encoder(encoder, true);
4990 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004991
Paulo Zanonie4916942013-09-20 16:21:19 -03004992 /* If we change the relative order between pipe/planes enabling, we need
4993 * to change the workaround. */
Maarten Lankhorst99d736a2015-06-01 12:50:09 +02004994 hsw_workaround_pipe = pipe_config->hsw_workaround_pipe;
4995 if (IS_HASWELL(dev) && hsw_workaround_pipe != INVALID_PIPE) {
4996 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4997 intel_wait_for_vblank(dev, hsw_workaround_pipe);
4998 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004999}
5000
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005001static void ironlake_pfit_disable(struct intel_crtc *crtc)
5002{
5003 struct drm_device *dev = crtc->base.dev;
5004 struct drm_i915_private *dev_priv = dev->dev_private;
5005 int pipe = crtc->pipe;
5006
5007 /* To avoid upsetting the power well on haswell only disable the pfit if
5008 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005009 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005010 I915_WRITE(PF_CTL(pipe), 0);
5011 I915_WRITE(PF_WIN_POS(pipe), 0);
5012 I915_WRITE(PF_WIN_SZ(pipe), 0);
5013 }
5014}
5015
Jesse Barnes6be4a602010-09-10 10:26:01 -07005016static void ironlake_crtc_disable(struct drm_crtc *crtc)
5017{
5018 struct drm_device *dev = crtc->dev;
5019 struct drm_i915_private *dev_priv = dev->dev_private;
5020 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005021 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005022 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005023 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005024
Daniel Vetterea9d7582012-07-10 10:42:52 +02005025 for_each_encoder_on_crtc(dev, crtc, encoder)
5026 encoder->disable(encoder);
5027
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005028 drm_crtc_vblank_off(crtc);
5029 assert_vblank_disabled(crtc);
5030
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005031 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005032 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005033
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005034 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005035
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005036 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005037
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005038 if (intel_crtc->config->has_pch_encoder)
5039 ironlake_fdi_disable(crtc);
5040
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005041 for_each_encoder_on_crtc(dev, crtc, encoder)
5042 if (encoder->post_disable)
5043 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005044
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005045 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005046 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005047
Daniel Vetterd925c592013-06-05 13:34:04 +02005048 if (HAS_PCH_CPT(dev)) {
5049 /* disable TRANS_DP_CTL */
5050 reg = TRANS_DP_CTL(pipe);
5051 temp = I915_READ(reg);
5052 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5053 TRANS_DP_PORT_SEL_MASK);
5054 temp |= TRANS_DP_PORT_SEL_NONE;
5055 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005056
Daniel Vetterd925c592013-06-05 13:34:04 +02005057 /* disable DPLL_SEL */
5058 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005059 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005060 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005061 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005062
Daniel Vetterd925c592013-06-05 13:34:04 +02005063 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005064 }
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005065
5066 intel_crtc->active = false;
5067 intel_update_watermarks(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005068}
5069
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005070static void haswell_crtc_disable(struct drm_crtc *crtc)
5071{
5072 struct drm_device *dev = crtc->dev;
5073 struct drm_i915_private *dev_priv = dev->dev_private;
5074 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5075 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005076 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005077
Jani Nikula8807e552013-08-30 19:40:32 +03005078 for_each_encoder_on_crtc(dev, crtc, encoder) {
5079 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005080 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005081 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005082
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005083 drm_crtc_vblank_off(crtc);
5084 assert_vblank_disabled(crtc);
5085
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005086 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005087 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5088 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005089 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005090
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005091 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005092 intel_ddi_set_vc_payload_alloc(crtc, false);
5093
Paulo Zanoniad80a812012-10-24 16:06:19 -02005094 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005095
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005096 if (INTEL_INFO(dev)->gen == 9)
Maarten Lankhorste435d6e2015-07-13 16:30:15 +02005097 skylake_scaler_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005098 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005099 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005100 else
5101 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005102
Paulo Zanoni1f544382012-10-24 11:32:00 -02005103 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005104
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005105 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005106 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005107 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005108 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005109
Imre Deak97b040a2014-06-25 22:01:50 +03005110 for_each_encoder_on_crtc(dev, crtc, encoder)
5111 if (encoder->post_disable)
5112 encoder->post_disable(encoder);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02005113
5114 intel_crtc->active = false;
5115 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005116}
5117
Jesse Barnes2dd24552013-04-25 12:55:01 -07005118static void i9xx_pfit_enable(struct intel_crtc *crtc)
5119{
5120 struct drm_device *dev = crtc->base.dev;
5121 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005122 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005123
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005124 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005125 return;
5126
Daniel Vetterc0b03412013-05-28 12:05:54 +02005127 /*
5128 * The panel fitter should only be adjusted whilst the pipe is disabled,
5129 * according to register description and PRM.
5130 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005131 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5132 assert_pipe_disabled(dev_priv, crtc->pipe);
5133
Jesse Barnesb074cec2013-04-25 12:55:02 -07005134 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5135 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005136
5137 /* Border color in case we don't scale up to the full screen. Black by
5138 * default, change to something else for debugging. */
5139 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005140}
5141
Dave Airlied05410f2014-06-05 13:22:59 +10005142static enum intel_display_power_domain port_to_power_domain(enum port port)
5143{
5144 switch (port) {
5145 case PORT_A:
5146 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5147 case PORT_B:
5148 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5149 case PORT_C:
5150 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5151 case PORT_D:
5152 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5153 default:
5154 WARN_ON_ONCE(1);
5155 return POWER_DOMAIN_PORT_OTHER;
5156 }
5157}
5158
Imre Deak77d22dc2014-03-05 16:20:52 +02005159#define for_each_power_domain(domain, mask) \
5160 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5161 if ((1 << (domain)) & (mask))
5162
Imre Deak319be8a2014-03-04 19:22:57 +02005163enum intel_display_power_domain
5164intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005165{
Imre Deak319be8a2014-03-04 19:22:57 +02005166 struct drm_device *dev = intel_encoder->base.dev;
5167 struct intel_digital_port *intel_dig_port;
5168
5169 switch (intel_encoder->type) {
5170 case INTEL_OUTPUT_UNKNOWN:
5171 /* Only DDI platforms should ever use this output type */
5172 WARN_ON_ONCE(!HAS_DDI(dev));
5173 case INTEL_OUTPUT_DISPLAYPORT:
5174 case INTEL_OUTPUT_HDMI:
5175 case INTEL_OUTPUT_EDP:
5176 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005177 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005178 case INTEL_OUTPUT_DP_MST:
5179 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5180 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005181 case INTEL_OUTPUT_ANALOG:
5182 return POWER_DOMAIN_PORT_CRT;
5183 case INTEL_OUTPUT_DSI:
5184 return POWER_DOMAIN_PORT_DSI;
5185 default:
5186 return POWER_DOMAIN_PORT_OTHER;
5187 }
5188}
5189
5190static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5191{
5192 struct drm_device *dev = crtc->dev;
5193 struct intel_encoder *intel_encoder;
5194 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5195 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005196 unsigned long mask;
5197 enum transcoder transcoder;
5198
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005199 if (!crtc->state->active)
5200 return 0;
5201
Imre Deak77d22dc2014-03-05 16:20:52 +02005202 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5203
5204 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5205 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005206 if (intel_crtc->config->pch_pfit.enabled ||
5207 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005208 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5209
Imre Deak319be8a2014-03-04 19:22:57 +02005210 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5211 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5212
Imre Deak77d22dc2014-03-05 16:20:52 +02005213 return mask;
5214}
5215
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005216static unsigned long modeset_get_crtc_power_domains(struct drm_crtc *crtc)
5217{
5218 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
5219 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5220 enum intel_display_power_domain domain;
5221 unsigned long domains, new_domains, old_domains;
5222
5223 old_domains = intel_crtc->enabled_power_domains;
5224 intel_crtc->enabled_power_domains = new_domains = get_crtc_power_domains(crtc);
5225
5226 domains = new_domains & ~old_domains;
5227
5228 for_each_power_domain(domain, domains)
5229 intel_display_power_get(dev_priv, domain);
5230
5231 return old_domains & ~new_domains;
5232}
5233
5234static void modeset_put_power_domains(struct drm_i915_private *dev_priv,
5235 unsigned long domains)
5236{
5237 enum intel_display_power_domain domain;
5238
5239 for_each_power_domain(domain, domains)
5240 intel_display_power_put(dev_priv, domain);
5241}
5242
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005243static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005244{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005245 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005246 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005247 unsigned long put_domains[I915_MAX_PIPES] = {};
5248 struct drm_crtc_state *crtc_state;
5249 struct drm_crtc *crtc;
5250 int i;
Imre Deak77d22dc2014-03-05 16:20:52 +02005251
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005252 for_each_crtc_in_state(state, crtc, crtc_state, i) {
5253 if (needs_modeset(crtc->state))
5254 put_domains[to_intel_crtc(crtc)->pipe] =
5255 modeset_get_crtc_power_domains(crtc);
Imre Deak77d22dc2014-03-05 16:20:52 +02005256 }
5257
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005258 if (dev_priv->display.modeset_commit_cdclk) {
5259 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5260
5261 if (cdclk != dev_priv->cdclk_freq &&
5262 !WARN_ON(!state->allow_modeset))
5263 dev_priv->display.modeset_commit_cdclk(state);
5264 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005265
Maarten Lankhorst292b9902015-07-13 16:30:27 +02005266 for (i = 0; i < I915_MAX_PIPES; i++)
5267 if (put_domains[i])
5268 modeset_put_power_domains(dev_priv, put_domains[i]);
Imre Deak77d22dc2014-03-05 16:20:52 +02005269}
5270
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005271static void intel_update_max_cdclk(struct drm_device *dev)
5272{
5273 struct drm_i915_private *dev_priv = dev->dev_private;
5274
5275 if (IS_SKYLAKE(dev)) {
5276 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5277
5278 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5279 dev_priv->max_cdclk_freq = 675000;
5280 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5281 dev_priv->max_cdclk_freq = 540000;
5282 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5283 dev_priv->max_cdclk_freq = 450000;
5284 else
5285 dev_priv->max_cdclk_freq = 337500;
5286 } else if (IS_BROADWELL(dev)) {
5287 /*
5288 * FIXME with extra cooling we can allow
5289 * 540 MHz for ULX and 675 Mhz for ULT.
5290 * How can we know if extra cooling is
5291 * available? PCI ID, VTB, something else?
5292 */
5293 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5294 dev_priv->max_cdclk_freq = 450000;
5295 else if (IS_BDW_ULX(dev))
5296 dev_priv->max_cdclk_freq = 450000;
5297 else if (IS_BDW_ULT(dev))
5298 dev_priv->max_cdclk_freq = 540000;
5299 else
5300 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005301 } else if (IS_CHERRYVIEW(dev)) {
5302 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005303 } else if (IS_VALLEYVIEW(dev)) {
5304 dev_priv->max_cdclk_freq = 400000;
5305 } else {
5306 /* otherwise assume cdclk is fixed */
5307 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5308 }
5309
5310 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5311 dev_priv->max_cdclk_freq);
5312}
5313
5314static void intel_update_cdclk(struct drm_device *dev)
5315{
5316 struct drm_i915_private *dev_priv = dev->dev_private;
5317
5318 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5319 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5320 dev_priv->cdclk_freq);
5321
5322 /*
5323 * Program the gmbus_freq based on the cdclk frequency.
5324 * BSpec erroneously claims we should aim for 4MHz, but
5325 * in fact 1MHz is the correct frequency.
5326 */
5327 if (IS_VALLEYVIEW(dev)) {
5328 /*
5329 * Program the gmbus_freq based on the cdclk frequency.
5330 * BSpec erroneously claims we should aim for 4MHz, but
5331 * in fact 1MHz is the correct frequency.
5332 */
5333 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5334 }
5335
5336 if (dev_priv->max_cdclk_freq == 0)
5337 intel_update_max_cdclk(dev);
5338}
5339
Damien Lespiau70d0c572015-06-04 18:21:29 +01005340static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305341{
5342 struct drm_i915_private *dev_priv = dev->dev_private;
5343 uint32_t divider;
5344 uint32_t ratio;
5345 uint32_t current_freq;
5346 int ret;
5347
5348 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5349 switch (frequency) {
5350 case 144000:
5351 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5352 ratio = BXT_DE_PLL_RATIO(60);
5353 break;
5354 case 288000:
5355 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5356 ratio = BXT_DE_PLL_RATIO(60);
5357 break;
5358 case 384000:
5359 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5360 ratio = BXT_DE_PLL_RATIO(60);
5361 break;
5362 case 576000:
5363 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5364 ratio = BXT_DE_PLL_RATIO(60);
5365 break;
5366 case 624000:
5367 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5368 ratio = BXT_DE_PLL_RATIO(65);
5369 break;
5370 case 19200:
5371 /*
5372 * Bypass frequency with DE PLL disabled. Init ratio, divider
5373 * to suppress GCC warning.
5374 */
5375 ratio = 0;
5376 divider = 0;
5377 break;
5378 default:
5379 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5380
5381 return;
5382 }
5383
5384 mutex_lock(&dev_priv->rps.hw_lock);
5385 /* Inform power controller of upcoming frequency change */
5386 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5387 0x80000000);
5388 mutex_unlock(&dev_priv->rps.hw_lock);
5389
5390 if (ret) {
5391 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5392 ret, frequency);
5393 return;
5394 }
5395
5396 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5397 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5398 current_freq = current_freq * 500 + 1000;
5399
5400 /*
5401 * DE PLL has to be disabled when
5402 * - setting to 19.2MHz (bypass, PLL isn't used)
5403 * - before setting to 624MHz (PLL needs toggling)
5404 * - before setting to any frequency from 624MHz (PLL needs toggling)
5405 */
5406 if (frequency == 19200 || frequency == 624000 ||
5407 current_freq == 624000) {
5408 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5409 /* Timeout 200us */
5410 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5411 1))
5412 DRM_ERROR("timout waiting for DE PLL unlock\n");
5413 }
5414
5415 if (frequency != 19200) {
5416 uint32_t val;
5417
5418 val = I915_READ(BXT_DE_PLL_CTL);
5419 val &= ~BXT_DE_PLL_RATIO_MASK;
5420 val |= ratio;
5421 I915_WRITE(BXT_DE_PLL_CTL, val);
5422
5423 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5424 /* Timeout 200us */
5425 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5426 DRM_ERROR("timeout waiting for DE PLL lock\n");
5427
5428 val = I915_READ(CDCLK_CTL);
5429 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5430 val |= divider;
5431 /*
5432 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5433 * enable otherwise.
5434 */
5435 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5436 if (frequency >= 500000)
5437 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5438
5439 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5440 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5441 val |= (frequency - 1000) / 500;
5442 I915_WRITE(CDCLK_CTL, val);
5443 }
5444
5445 mutex_lock(&dev_priv->rps.hw_lock);
5446 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5447 DIV_ROUND_UP(frequency, 25000));
5448 mutex_unlock(&dev_priv->rps.hw_lock);
5449
5450 if (ret) {
5451 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5452 ret, frequency);
5453 return;
5454 }
5455
Damien Lespiaua47871b2015-06-04 18:21:34 +01005456 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305457}
5458
5459void broxton_init_cdclk(struct drm_device *dev)
5460{
5461 struct drm_i915_private *dev_priv = dev->dev_private;
5462 uint32_t val;
5463
5464 /*
5465 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5466 * or else the reset will hang because there is no PCH to respond.
5467 * Move the handshake programming to initialization sequence.
5468 * Previously was left up to BIOS.
5469 */
5470 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5471 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5472 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5473
5474 /* Enable PG1 for cdclk */
5475 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5476
5477 /* check if cd clock is enabled */
5478 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5479 DRM_DEBUG_KMS("Display already initialized\n");
5480 return;
5481 }
5482
5483 /*
5484 * FIXME:
5485 * - The initial CDCLK needs to be read from VBT.
5486 * Need to make this change after VBT has changes for BXT.
5487 * - check if setting the max (or any) cdclk freq is really necessary
5488 * here, it belongs to modeset time
5489 */
5490 broxton_set_cdclk(dev, 624000);
5491
5492 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005493 POSTING_READ(DBUF_CTL);
5494
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305495 udelay(10);
5496
5497 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5498 DRM_ERROR("DBuf power enable timeout!\n");
5499}
5500
5501void broxton_uninit_cdclk(struct drm_device *dev)
5502{
5503 struct drm_i915_private *dev_priv = dev->dev_private;
5504
5505 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005506 POSTING_READ(DBUF_CTL);
5507
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305508 udelay(10);
5509
5510 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5511 DRM_ERROR("DBuf power disable timeout!\n");
5512
5513 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5514 broxton_set_cdclk(dev, 19200);
5515
5516 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5517}
5518
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005519static const struct skl_cdclk_entry {
5520 unsigned int freq;
5521 unsigned int vco;
5522} skl_cdclk_frequencies[] = {
5523 { .freq = 308570, .vco = 8640 },
5524 { .freq = 337500, .vco = 8100 },
5525 { .freq = 432000, .vco = 8640 },
5526 { .freq = 450000, .vco = 8100 },
5527 { .freq = 540000, .vco = 8100 },
5528 { .freq = 617140, .vco = 8640 },
5529 { .freq = 675000, .vco = 8100 },
5530};
5531
5532static unsigned int skl_cdclk_decimal(unsigned int freq)
5533{
5534 return (freq - 1000) / 500;
5535}
5536
5537static unsigned int skl_cdclk_get_vco(unsigned int freq)
5538{
5539 unsigned int i;
5540
5541 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5542 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5543
5544 if (e->freq == freq)
5545 return e->vco;
5546 }
5547
5548 return 8100;
5549}
5550
5551static void
5552skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5553{
5554 unsigned int min_freq;
5555 u32 val;
5556
5557 /* select the minimum CDCLK before enabling DPLL 0 */
5558 val = I915_READ(CDCLK_CTL);
5559 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5560 val |= CDCLK_FREQ_337_308;
5561
5562 if (required_vco == 8640)
5563 min_freq = 308570;
5564 else
5565 min_freq = 337500;
5566
5567 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5568
5569 I915_WRITE(CDCLK_CTL, val);
5570 POSTING_READ(CDCLK_CTL);
5571
5572 /*
5573 * We always enable DPLL0 with the lowest link rate possible, but still
5574 * taking into account the VCO required to operate the eDP panel at the
5575 * desired frequency. The usual DP link rates operate with a VCO of
5576 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5577 * The modeset code is responsible for the selection of the exact link
5578 * rate later on, with the constraint of choosing a frequency that
5579 * works with required_vco.
5580 */
5581 val = I915_READ(DPLL_CTRL1);
5582
5583 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5584 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5585 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5586 if (required_vco == 8640)
5587 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5588 SKL_DPLL0);
5589 else
5590 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5591 SKL_DPLL0);
5592
5593 I915_WRITE(DPLL_CTRL1, val);
5594 POSTING_READ(DPLL_CTRL1);
5595
5596 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5597
5598 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5599 DRM_ERROR("DPLL0 not locked\n");
5600}
5601
5602static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5603{
5604 int ret;
5605 u32 val;
5606
5607 /* inform PCU we want to change CDCLK */
5608 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5609 mutex_lock(&dev_priv->rps.hw_lock);
5610 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5611 mutex_unlock(&dev_priv->rps.hw_lock);
5612
5613 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5614}
5615
5616static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5617{
5618 unsigned int i;
5619
5620 for (i = 0; i < 15; i++) {
5621 if (skl_cdclk_pcu_ready(dev_priv))
5622 return true;
5623 udelay(10);
5624 }
5625
5626 return false;
5627}
5628
5629static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5630{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005631 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005632 u32 freq_select, pcu_ack;
5633
5634 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5635
5636 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5637 DRM_ERROR("failed to inform PCU about cdclk change\n");
5638 return;
5639 }
5640
5641 /* set CDCLK_CTL */
5642 switch(freq) {
5643 case 450000:
5644 case 432000:
5645 freq_select = CDCLK_FREQ_450_432;
5646 pcu_ack = 1;
5647 break;
5648 case 540000:
5649 freq_select = CDCLK_FREQ_540;
5650 pcu_ack = 2;
5651 break;
5652 case 308570:
5653 case 337500:
5654 default:
5655 freq_select = CDCLK_FREQ_337_308;
5656 pcu_ack = 0;
5657 break;
5658 case 617140:
5659 case 675000:
5660 freq_select = CDCLK_FREQ_675_617;
5661 pcu_ack = 3;
5662 break;
5663 }
5664
5665 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5666 POSTING_READ(CDCLK_CTL);
5667
5668 /* inform PCU of the change */
5669 mutex_lock(&dev_priv->rps.hw_lock);
5670 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5671 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005672
5673 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005674}
5675
5676void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5677{
5678 /* disable DBUF power */
5679 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5680 POSTING_READ(DBUF_CTL);
5681
5682 udelay(10);
5683
5684 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5685 DRM_ERROR("DBuf power disable timeout\n");
5686
5687 /* disable DPLL0 */
5688 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5689 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5690 DRM_ERROR("Couldn't disable DPLL0\n");
5691
5692 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5693}
5694
5695void skl_init_cdclk(struct drm_i915_private *dev_priv)
5696{
5697 u32 val;
5698 unsigned int required_vco;
5699
5700 /* enable PCH reset handshake */
5701 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5702 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5703
5704 /* enable PG1 and Misc I/O */
5705 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5706
5707 /* DPLL0 already enabed !? */
5708 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5709 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5710 return;
5711 }
5712
5713 /* enable DPLL0 */
5714 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5715 skl_dpll0_enable(dev_priv, required_vco);
5716
5717 /* set CDCLK to the frequency the BIOS chose */
5718 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5719
5720 /* enable DBUF power */
5721 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5722 POSTING_READ(DBUF_CTL);
5723
5724 udelay(10);
5725
5726 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5727 DRM_ERROR("DBuf power enable timeout\n");
5728}
5729
Ville Syrjälädfcab172014-06-13 13:37:47 +03005730/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005731static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005732{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005733 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005734
Jesse Barnes586f49d2013-11-04 16:06:59 -08005735 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005736 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005737 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5738 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005739 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005740
Ville Syrjälädfcab172014-06-13 13:37:47 +03005741 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005742}
5743
5744/* Adjust CDclk dividers to allow high res or save power if possible */
5745static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5746{
5747 struct drm_i915_private *dev_priv = dev->dev_private;
5748 u32 val, cmd;
5749
Vandana Kannan164dfd22014-11-24 13:37:41 +05305750 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5751 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005752
Ville Syrjälädfcab172014-06-13 13:37:47 +03005753 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005754 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005755 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005756 cmd = 1;
5757 else
5758 cmd = 0;
5759
5760 mutex_lock(&dev_priv->rps.hw_lock);
5761 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5762 val &= ~DSPFREQGUAR_MASK;
5763 val |= (cmd << DSPFREQGUAR_SHIFT);
5764 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5765 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5766 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5767 50)) {
5768 DRM_ERROR("timed out waiting for CDclk change\n");
5769 }
5770 mutex_unlock(&dev_priv->rps.hw_lock);
5771
Ville Syrjälä54433e92015-05-26 20:42:31 +03005772 mutex_lock(&dev_priv->sb_lock);
5773
Ville Syrjälädfcab172014-06-13 13:37:47 +03005774 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005775 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005776
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005777 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005778
Jesse Barnes30a970c2013-11-04 13:48:12 -08005779 /* adjust cdclk divider */
5780 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005781 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005782 val |= divider;
5783 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005784
5785 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5786 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5787 50))
5788 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005789 }
5790
Jesse Barnes30a970c2013-11-04 13:48:12 -08005791 /* adjust self-refresh exit latency value */
5792 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5793 val &= ~0x7f;
5794
5795 /*
5796 * For high bandwidth configs, we set a higher latency in the bunit
5797 * so that the core display fetch happens in time to avoid underruns.
5798 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005799 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005800 val |= 4500 / 250; /* 4.5 usec */
5801 else
5802 val |= 3000 / 250; /* 3.0 usec */
5803 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005804
Ville Syrjäläa5805162015-05-26 20:42:30 +03005805 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005806
Ville Syrjäläb6283052015-06-03 15:45:07 +03005807 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005808}
5809
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005810static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5811{
5812 struct drm_i915_private *dev_priv = dev->dev_private;
5813 u32 val, cmd;
5814
Vandana Kannan164dfd22014-11-24 13:37:41 +05305815 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5816 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005817
5818 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005819 case 333333:
5820 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005822 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005823 break;
5824 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005825 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005826 return;
5827 }
5828
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005829 /*
5830 * Specs are full of misinformation, but testing on actual
5831 * hardware has shown that we just need to write the desired
5832 * CCK divider into the Punit register.
5833 */
5834 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5835
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005836 mutex_lock(&dev_priv->rps.hw_lock);
5837 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5838 val &= ~DSPFREQGUAR_MASK_CHV;
5839 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5840 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5841 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5842 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5843 50)) {
5844 DRM_ERROR("timed out waiting for CDclk change\n");
5845 }
5846 mutex_unlock(&dev_priv->rps.hw_lock);
5847
Ville Syrjäläb6283052015-06-03 15:45:07 +03005848 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005849}
5850
Jesse Barnes30a970c2013-11-04 13:48:12 -08005851static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5852 int max_pixclk)
5853{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005854 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005855 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005856
Jesse Barnes30a970c2013-11-04 13:48:12 -08005857 /*
5858 * Really only a few cases to deal with, as only 4 CDclks are supported:
5859 * 200MHz
5860 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005861 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005862 * 400MHz (VLV only)
5863 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5864 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005865 *
5866 * We seem to get an unstable or solid color picture at 200MHz.
5867 * Not sure what's wrong. For now use 200MHz only when all pipes
5868 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005869 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005870 if (!IS_CHERRYVIEW(dev_priv) &&
5871 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005872 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005873 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005874 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005875 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005876 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005877 else
5878 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005879}
5880
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305881static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5882 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005883{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305884 /*
5885 * FIXME:
5886 * - remove the guardband, it's not needed on BXT
5887 * - set 19.2MHz bypass frequency if there are no active pipes
5888 */
5889 if (max_pixclk > 576000*9/10)
5890 return 624000;
5891 else if (max_pixclk > 384000*9/10)
5892 return 576000;
5893 else if (max_pixclk > 288000*9/10)
5894 return 384000;
5895 else if (max_pixclk > 144000*9/10)
5896 return 288000;
5897 else
5898 return 144000;
5899}
5900
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005901/* Compute the max pixel clock for new configuration. Uses atomic state if
5902 * that's non-NULL, look at current state otherwise. */
5903static int intel_mode_max_pixclk(struct drm_device *dev,
5904 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005905{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005907 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005908 int max_pixclk = 0;
5909
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005910 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005911 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005912 if (IS_ERR(crtc_state))
5913 return PTR_ERR(crtc_state);
5914
5915 if (!crtc_state->base.enable)
5916 continue;
5917
5918 max_pixclk = max(max_pixclk,
5919 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005920 }
5921
5922 return max_pixclk;
5923}
5924
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005925static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005926{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005927 struct drm_device *dev = state->dev;
5928 struct drm_i915_private *dev_priv = dev->dev_private;
5929 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005930
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005931 if (max_pixclk < 0)
5932 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005933
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005934 to_intel_atomic_state(state)->cdclk =
5935 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305936
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005937 return 0;
5938}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005939
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005940static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5941{
5942 struct drm_device *dev = state->dev;
5943 struct drm_i915_private *dev_priv = dev->dev_private;
5944 int max_pixclk = intel_mode_max_pixclk(dev, state);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005945
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005946 if (max_pixclk < 0)
5947 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005948
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005949 to_intel_atomic_state(state)->cdclk =
5950 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005951
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005952 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005953}
5954
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005955static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5956{
5957 unsigned int credits, default_credits;
5958
5959 if (IS_CHERRYVIEW(dev_priv))
5960 default_credits = PFI_CREDIT(12);
5961 else
5962 default_credits = PFI_CREDIT(8);
5963
Vandana Kannan164dfd22014-11-24 13:37:41 +05305964 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005965 /* CHV suggested value is 31 or 63 */
5966 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005967 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005968 else
5969 credits = PFI_CREDIT(15);
5970 } else {
5971 credits = default_credits;
5972 }
5973
5974 /*
5975 * WA - write default credits before re-programming
5976 * FIXME: should we also set the resend bit here?
5977 */
5978 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5979 default_credits);
5980
5981 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5982 credits | PFI_CREDIT_RESEND);
5983
5984 /*
5985 * FIXME is this guaranteed to clear
5986 * immediately or should we poll for it?
5987 */
5988 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5989}
5990
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005991static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005992{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005993 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005994 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005995 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005996
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005997 /*
5998 * FIXME: We can end up here with all power domains off, yet
5999 * with a CDCLK frequency other than the minimum. To account
6000 * for this take the PIPE-A power domain, which covers the HW
6001 * blocks needed for the following programming. This can be
6002 * removed once it's guaranteed that we get here either with
6003 * the minimum CDCLK set, or the required power domains
6004 * enabled.
6005 */
6006 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006007
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006008 if (IS_CHERRYVIEW(dev))
6009 cherryview_set_cdclk(dev, req_cdclk);
6010 else
6011 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006012
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006013 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02006014
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006015 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006016}
6017
Jesse Barnes89b667f2013-04-18 14:51:36 -07006018static void valleyview_crtc_enable(struct drm_crtc *crtc)
6019{
6020 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006021 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6023 struct intel_encoder *encoder;
6024 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006025 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006026
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006027 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006028 return;
6029
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006030 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306031
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006032 if (!is_dsi) {
6033 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006034 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006035 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006036 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006037 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006038
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006039 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306040 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006041
6042 intel_set_pipe_timings(intel_crtc);
6043
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006044 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6045 struct drm_i915_private *dev_priv = dev->dev_private;
6046
6047 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6048 I915_WRITE(CHV_CANVAS(pipe), 0);
6049 }
6050
Daniel Vetter5b18e572014-04-24 23:55:06 +02006051 i9xx_set_pipeconf(intel_crtc);
6052
Jesse Barnes89b667f2013-04-18 14:51:36 -07006053 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006054
Daniel Vettera72e4c92014-09-30 10:56:47 +02006055 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006056
Jesse Barnes89b667f2013-04-18 14:51:36 -07006057 for_each_encoder_on_crtc(dev, crtc, encoder)
6058 if (encoder->pre_pll_enable)
6059 encoder->pre_pll_enable(encoder);
6060
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006061 if (!is_dsi) {
6062 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006063 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006064 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006065 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006066 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006067
6068 for_each_encoder_on_crtc(dev, crtc, encoder)
6069 if (encoder->pre_enable)
6070 encoder->pre_enable(encoder);
6071
Jesse Barnes2dd24552013-04-25 12:55:01 -07006072 i9xx_pfit_enable(intel_crtc);
6073
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006074 intel_crtc_load_lut(crtc);
6075
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006076 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006077
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006078 assert_vblank_disabled(crtc);
6079 drm_crtc_vblank_on(crtc);
6080
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006081 for_each_encoder_on_crtc(dev, crtc, encoder)
6082 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006083}
6084
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006085static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6086{
6087 struct drm_device *dev = crtc->base.dev;
6088 struct drm_i915_private *dev_priv = dev->dev_private;
6089
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006090 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6091 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006092}
6093
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006094static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006095{
6096 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006097 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006098 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006099 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006100 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006101
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006102 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006103 return;
6104
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006105 i9xx_set_pll_dividers(intel_crtc);
6106
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006107 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306108 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006109
6110 intel_set_pipe_timings(intel_crtc);
6111
Daniel Vetter5b18e572014-04-24 23:55:06 +02006112 i9xx_set_pipeconf(intel_crtc);
6113
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006114 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006115
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006116 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006117 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006118
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006119 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006120 if (encoder->pre_enable)
6121 encoder->pre_enable(encoder);
6122
Daniel Vetterf6736a12013-06-05 13:34:30 +02006123 i9xx_enable_pll(intel_crtc);
6124
Jesse Barnes2dd24552013-04-25 12:55:01 -07006125 i9xx_pfit_enable(intel_crtc);
6126
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006127 intel_crtc_load_lut(crtc);
6128
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006129 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006130 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006131
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006132 assert_vblank_disabled(crtc);
6133 drm_crtc_vblank_on(crtc);
6134
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006135 for_each_encoder_on_crtc(dev, crtc, encoder)
6136 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006137}
6138
Daniel Vetter87476d62013-04-11 16:29:06 +02006139static void i9xx_pfit_disable(struct intel_crtc *crtc)
6140{
6141 struct drm_device *dev = crtc->base.dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006143
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006144 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006145 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006146
6147 assert_pipe_disabled(dev_priv, crtc->pipe);
6148
Daniel Vetter328d8e82013-05-08 10:36:31 +02006149 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6150 I915_READ(PFIT_CONTROL));
6151 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006152}
6153
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006154static void i9xx_crtc_disable(struct drm_crtc *crtc)
6155{
6156 struct drm_device *dev = crtc->dev;
6157 struct drm_i915_private *dev_priv = dev->dev_private;
6158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006159 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006160 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006161
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006162 /*
6163 * On gen2 planes are double buffered but the pipe isn't, so we must
6164 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006165 * We also need to wait on all gmch platforms because of the
6166 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006167 */
Imre Deak564ed192014-06-13 14:54:21 +03006168 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006169
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006170 for_each_encoder_on_crtc(dev, crtc, encoder)
6171 encoder->disable(encoder);
6172
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006173 drm_crtc_vblank_off(crtc);
6174 assert_vblank_disabled(crtc);
6175
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006176 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006177
Daniel Vetter87476d62013-04-11 16:29:06 +02006178 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006179
Jesse Barnes89b667f2013-04-18 14:51:36 -07006180 for_each_encoder_on_crtc(dev, crtc, encoder)
6181 if (encoder->post_disable)
6182 encoder->post_disable(encoder);
6183
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006184 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006185 if (IS_CHERRYVIEW(dev))
6186 chv_disable_pll(dev_priv, pipe);
6187 else if (IS_VALLEYVIEW(dev))
6188 vlv_disable_pll(dev_priv, pipe);
6189 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006190 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006191 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006192
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006193 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006194 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02006195
6196 intel_crtc->active = false;
6197 intel_update_watermarks(crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006198}
6199
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006200static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006201{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006203 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006204 enum intel_display_power_domain domain;
6205 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006206
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006207 if (!intel_crtc->active)
6208 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006209
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006210 if (to_intel_plane_state(crtc->primary->state)->visible) {
6211 intel_crtc_wait_for_pending_flips(crtc);
6212 intel_pre_disable_primary(crtc);
6213 }
6214
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006215 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006216 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorst1f7457b2015-07-13 11:55:05 +02006217 intel_disable_shared_dpll(intel_crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006218
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006219 domains = intel_crtc->enabled_power_domains;
6220 for_each_power_domain(domain, domains)
6221 intel_display_power_put(dev_priv, domain);
6222 intel_crtc->enabled_power_domains = 0;
6223}
6224
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006225/*
6226 * turn all crtc's off, but do not adjust state
6227 * This has to be paired with a call to intel_modeset_setup_hw_state.
6228 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006229int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006230{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006231 struct drm_mode_config *config = &dev->mode_config;
6232 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6233 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006234 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006235 unsigned crtc_mask = 0;
6236 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006237
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006238 if (WARN_ON(!ctx))
6239 return 0;
6240
6241 lockdep_assert_held(&ctx->ww_ctx);
6242 state = drm_atomic_state_alloc(dev);
6243 if (WARN_ON(!state))
6244 return -ENOMEM;
6245
6246 state->acquire_ctx = ctx;
6247 state->allow_modeset = true;
6248
6249 for_each_crtc(dev, crtc) {
6250 struct drm_crtc_state *crtc_state =
6251 drm_atomic_get_crtc_state(state, crtc);
6252
6253 ret = PTR_ERR_OR_ZERO(crtc_state);
6254 if (ret)
6255 goto free;
6256
6257 if (!crtc_state->active)
6258 continue;
6259
6260 crtc_state->active = false;
6261 crtc_mask |= 1 << drm_crtc_index(crtc);
6262 }
6263
6264 if (crtc_mask) {
6265 ret = intel_set_mode(state);
6266
6267 if (!ret) {
6268 for_each_crtc(dev, crtc)
6269 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6270 crtc->state->active = true;
6271
6272 return ret;
6273 }
6274 }
6275
6276free:
6277 if (ret)
6278 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6279 drm_atomic_state_free(state);
6280 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006281}
6282
Chris Wilsoncdd59982010-09-08 16:30:16 +01006283/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006284int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006285{
6286 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006287 struct drm_mode_config *config = &dev->mode_config;
6288 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006289 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006290 struct intel_crtc_state *pipe_config;
6291 struct drm_atomic_state *state;
6292 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006293
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006294 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006295 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006296
6297 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006298 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006299
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006300 /* this function should be called with drm_modeset_lock_all for now */
6301 if (WARN_ON(!ctx))
6302 return -EIO;
6303 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006304
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006305 state = drm_atomic_state_alloc(dev);
6306 if (WARN_ON(!state))
6307 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006308
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006309 state->acquire_ctx = ctx;
6310 state->allow_modeset = true;
6311
6312 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6313 if (IS_ERR(pipe_config)) {
6314 ret = PTR_ERR(pipe_config);
6315 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006316 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006317 pipe_config->base.active = enable;
6318
6319 ret = intel_set_mode(state);
6320 if (!ret)
6321 return ret;
6322
6323err:
6324 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6325 drm_atomic_state_free(state);
6326 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306327}
6328
6329/**
6330 * Sets the power management mode of the pipe and plane.
6331 */
6332void intel_crtc_update_dpms(struct drm_crtc *crtc)
6333{
6334 struct drm_device *dev = crtc->dev;
6335 struct intel_encoder *intel_encoder;
6336 bool enable = false;
6337
6338 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6339 enable |= intel_encoder->connectors_active;
6340
6341 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006342}
6343
Chris Wilsonea5b2132010-08-04 13:50:23 +01006344void intel_encoder_destroy(struct drm_encoder *encoder)
6345{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006346 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006347
Chris Wilsonea5b2132010-08-04 13:50:23 +01006348 drm_encoder_cleanup(encoder);
6349 kfree(intel_encoder);
6350}
6351
Damien Lespiau92373292013-08-08 22:28:57 +01006352/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006353 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6354 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006355static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006356{
6357 if (mode == DRM_MODE_DPMS_ON) {
6358 encoder->connectors_active = true;
6359
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006360 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006361 } else {
6362 encoder->connectors_active = false;
6363
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006364 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006365 }
6366}
6367
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006368/* Cross check the actual hw state with our own modeset state tracking (and it's
6369 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006370static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006371{
6372 if (connector->get_hw_state(connector)) {
6373 struct intel_encoder *encoder = connector->encoder;
6374 struct drm_crtc *crtc;
6375 bool encoder_enabled;
6376 enum pipe pipe;
6377
6378 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6379 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006380 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006381
Dave Airlie0e32b392014-05-02 14:02:48 +10006382 /* there is no real hw state for MST connectors */
6383 if (connector->mst_port)
6384 return;
6385
Rob Clarke2c719b2014-12-15 13:56:32 -05006386 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006387 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006388 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006389 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006390
Dave Airlie36cd7442014-05-02 13:44:18 +10006391 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006392 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006393 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006394
Dave Airlie36cd7442014-05-02 13:44:18 +10006395 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006396 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6397 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006398 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006399
Dave Airlie36cd7442014-05-02 13:44:18 +10006400 crtc = encoder->base.crtc;
6401
Matt Roper83d65732015-02-25 13:12:16 -08006402 I915_STATE_WARN(!crtc->state->enable,
6403 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006404 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6405 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006406 "encoder active on the wrong pipe\n");
6407 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006408 }
6409}
6410
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006411int intel_connector_init(struct intel_connector *connector)
6412{
6413 struct drm_connector_state *connector_state;
6414
6415 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6416 if (!connector_state)
6417 return -ENOMEM;
6418
6419 connector->base.state = connector_state;
6420 return 0;
6421}
6422
6423struct intel_connector *intel_connector_alloc(void)
6424{
6425 struct intel_connector *connector;
6426
6427 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6428 if (!connector)
6429 return NULL;
6430
6431 if (intel_connector_init(connector) < 0) {
6432 kfree(connector);
6433 return NULL;
6434 }
6435
6436 return connector;
6437}
6438
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006439/* Even simpler default implementation, if there's really no special case to
6440 * consider. */
6441void intel_connector_dpms(struct drm_connector *connector, int mode)
6442{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006443 /* All the simple cases only support two dpms states. */
6444 if (mode != DRM_MODE_DPMS_ON)
6445 mode = DRM_MODE_DPMS_OFF;
6446
6447 if (mode == connector->dpms)
6448 return;
6449
6450 connector->dpms = mode;
6451
6452 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006453 if (connector->encoder)
6454 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006455
Daniel Vetterb9805142012-08-31 17:37:33 +02006456 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006457}
6458
Daniel Vetterf0947c32012-07-02 13:10:34 +02006459/* Simple connector->get_hw_state implementation for encoders that support only
6460 * one connector and no cloning and hence the encoder state determines the state
6461 * of the connector. */
6462bool intel_connector_get_hw_state(struct intel_connector *connector)
6463{
Daniel Vetter24929352012-07-02 20:28:59 +02006464 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006465 struct intel_encoder *encoder = connector->encoder;
6466
6467 return encoder->get_hw_state(encoder, &pipe);
6468}
6469
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006471{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006472 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6473 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006474
6475 return 0;
6476}
6477
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006478static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006479 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006481 struct drm_atomic_state *state = pipe_config->base.state;
6482 struct intel_crtc *other_crtc;
6483 struct intel_crtc_state *other_crtc_state;
6484
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006485 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6486 pipe_name(pipe), pipe_config->fdi_lanes);
6487 if (pipe_config->fdi_lanes > 4) {
6488 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6489 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006490 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491 }
6492
Paulo Zanonibafb6552013-11-02 21:07:44 -07006493 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006494 if (pipe_config->fdi_lanes > 2) {
6495 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6496 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006497 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006498 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006499 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006500 }
6501 }
6502
6503 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006504 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006505
6506 /* Ivybridge 3 pipe is really complicated */
6507 switch (pipe) {
6508 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006509 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006510 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006511 if (pipe_config->fdi_lanes <= 2)
6512 return 0;
6513
6514 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6515 other_crtc_state =
6516 intel_atomic_get_crtc_state(state, other_crtc);
6517 if (IS_ERR(other_crtc_state))
6518 return PTR_ERR(other_crtc_state);
6519
6520 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006521 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6522 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006523 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006524 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006525 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006526 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006527 if (pipe_config->fdi_lanes > 2) {
6528 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6529 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006530 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006531 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006532
6533 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6534 other_crtc_state =
6535 intel_atomic_get_crtc_state(state, other_crtc);
6536 if (IS_ERR(other_crtc_state))
6537 return PTR_ERR(other_crtc_state);
6538
6539 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006540 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006541 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006542 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006543 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006544 default:
6545 BUG();
6546 }
6547}
6548
Daniel Vettere29c22c2013-02-21 00:00:16 +01006549#define RETRY 1
6550static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006551 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006552{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006553 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006554 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006555 int lane, link_bw, fdi_dotclock, ret;
6556 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006557
Daniel Vettere29c22c2013-02-21 00:00:16 +01006558retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006559 /* FDI is a binary signal running at ~2.7GHz, encoding
6560 * each output octet as 10 bits. The actual frequency
6561 * is stored as a divider into a 100MHz clock, and the
6562 * mode pixel clock is stored in units of 1KHz.
6563 * Hence the bw of each lane in terms of the mode signal
6564 * is:
6565 */
6566 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6567
Damien Lespiau241bfc32013-09-25 16:45:37 +01006568 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006569
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006570 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006571 pipe_config->pipe_bpp);
6572
6573 pipe_config->fdi_lanes = lane;
6574
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006575 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006576 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006577
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006578 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6579 intel_crtc->pipe, pipe_config);
6580 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006581 pipe_config->pipe_bpp -= 2*3;
6582 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6583 pipe_config->pipe_bpp);
6584 needs_recompute = true;
6585 pipe_config->bw_constrained = true;
6586
6587 goto retry;
6588 }
6589
6590 if (needs_recompute)
6591 return RETRY;
6592
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006593 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006594}
6595
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006596static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6597 struct intel_crtc_state *pipe_config)
6598{
6599 if (pipe_config->pipe_bpp > 24)
6600 return false;
6601
6602 /* HSW can handle pixel rate up to cdclk? */
6603 if (IS_HASWELL(dev_priv->dev))
6604 return true;
6605
6606 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006607 * We compare against max which means we must take
6608 * the increased cdclk requirement into account when
6609 * calculating the new cdclk.
6610 *
6611 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006612 */
6613 return ilk_pipe_pixel_rate(pipe_config) <=
6614 dev_priv->max_cdclk_freq * 95 / 100;
6615}
6616
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006617static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006618 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006619{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006620 struct drm_device *dev = crtc->base.dev;
6621 struct drm_i915_private *dev_priv = dev->dev_private;
6622
Jani Nikulad330a952014-01-21 11:24:25 +02006623 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006624 hsw_crtc_supports_ips(crtc) &&
6625 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006626}
6627
Daniel Vettera43f6e02013-06-07 23:10:32 +02006628static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006629 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006630{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006631 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006632 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006633 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006634
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006635 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006636 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006637 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006638
6639 /*
6640 * Enable pixel doubling when the dot clock
6641 * is > 90% of the (display) core speed.
6642 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006643 * GDG double wide on either pipe,
6644 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006645 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006646 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006647 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006648 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006649 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006650 }
6651
Damien Lespiau241bfc32013-09-25 16:45:37 +01006652 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006653 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006654 }
Chris Wilson89749352010-09-12 18:25:19 +01006655
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006656 /*
6657 * Pipe horizontal size must be even in:
6658 * - DVO ganged mode
6659 * - LVDS dual channel mode
6660 * - Double wide pipe
6661 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006662 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006663 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6664 pipe_config->pipe_src_w &= ~1;
6665
Damien Lespiau8693a822013-05-03 18:48:11 +01006666 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6667 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006668 */
6669 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6670 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006671 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006672
Damien Lespiauf5adf942013-06-24 18:29:34 +01006673 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006674 hsw_compute_ips_config(crtc, pipe_config);
6675
Daniel Vetter877d48d2013-04-19 11:24:43 +02006676 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006677 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006678
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006679 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006680}
6681
Ville Syrjälä1652d192015-03-31 14:12:01 +03006682static int skylake_get_display_clock_speed(struct drm_device *dev)
6683{
6684 struct drm_i915_private *dev_priv = to_i915(dev);
6685 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6686 uint32_t cdctl = I915_READ(CDCLK_CTL);
6687 uint32_t linkrate;
6688
Damien Lespiau414355a2015-06-04 18:21:31 +01006689 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006690 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006691
6692 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6693 return 540000;
6694
6695 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006696 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006697
Damien Lespiau71cd8422015-04-30 16:39:17 +01006698 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6699 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006700 /* vco 8640 */
6701 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6702 case CDCLK_FREQ_450_432:
6703 return 432000;
6704 case CDCLK_FREQ_337_308:
6705 return 308570;
6706 case CDCLK_FREQ_675_617:
6707 return 617140;
6708 default:
6709 WARN(1, "Unknown cd freq selection\n");
6710 }
6711 } else {
6712 /* vco 8100 */
6713 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6714 case CDCLK_FREQ_450_432:
6715 return 450000;
6716 case CDCLK_FREQ_337_308:
6717 return 337500;
6718 case CDCLK_FREQ_675_617:
6719 return 675000;
6720 default:
6721 WARN(1, "Unknown cd freq selection\n");
6722 }
6723 }
6724
6725 /* error case, do as if DPLL0 isn't enabled */
6726 return 24000;
6727}
6728
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006729static int broxton_get_display_clock_speed(struct drm_device *dev)
6730{
6731 struct drm_i915_private *dev_priv = to_i915(dev);
6732 uint32_t cdctl = I915_READ(CDCLK_CTL);
6733 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6734 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6735 int cdclk;
6736
6737 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6738 return 19200;
6739
6740 cdclk = 19200 * pll_ratio / 2;
6741
6742 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6743 case BXT_CDCLK_CD2X_DIV_SEL_1:
6744 return cdclk; /* 576MHz or 624MHz */
6745 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6746 return cdclk * 2 / 3; /* 384MHz */
6747 case BXT_CDCLK_CD2X_DIV_SEL_2:
6748 return cdclk / 2; /* 288MHz */
6749 case BXT_CDCLK_CD2X_DIV_SEL_4:
6750 return cdclk / 4; /* 144MHz */
6751 }
6752
6753 /* error case, do as if DE PLL isn't enabled */
6754 return 19200;
6755}
6756
Ville Syrjälä1652d192015-03-31 14:12:01 +03006757static int broadwell_get_display_clock_speed(struct drm_device *dev)
6758{
6759 struct drm_i915_private *dev_priv = dev->dev_private;
6760 uint32_t lcpll = I915_READ(LCPLL_CTL);
6761 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6762
6763 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6764 return 800000;
6765 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6766 return 450000;
6767 else if (freq == LCPLL_CLK_FREQ_450)
6768 return 450000;
6769 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6770 return 540000;
6771 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6772 return 337500;
6773 else
6774 return 675000;
6775}
6776
6777static int haswell_get_display_clock_speed(struct drm_device *dev)
6778{
6779 struct drm_i915_private *dev_priv = dev->dev_private;
6780 uint32_t lcpll = I915_READ(LCPLL_CTL);
6781 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6782
6783 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6784 return 800000;
6785 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6786 return 450000;
6787 else if (freq == LCPLL_CLK_FREQ_450)
6788 return 450000;
6789 else if (IS_HSW_ULT(dev))
6790 return 337500;
6791 else
6792 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006793}
6794
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006795static int valleyview_get_display_clock_speed(struct drm_device *dev)
6796{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006797 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006798 u32 val;
6799 int divider;
6800
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006801 if (dev_priv->hpll_freq == 0)
6802 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6803
Ville Syrjäläa5805162015-05-26 20:42:30 +03006804 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006805 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006806 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006807
6808 divider = val & DISPLAY_FREQUENCY_VALUES;
6809
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006810 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6811 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6812 "cdclk change in progress\n");
6813
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006814 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006815}
6816
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006817static int ilk_get_display_clock_speed(struct drm_device *dev)
6818{
6819 return 450000;
6820}
6821
Jesse Barnese70236a2009-09-21 10:42:27 -07006822static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006823{
Jesse Barnese70236a2009-09-21 10:42:27 -07006824 return 400000;
6825}
Jesse Barnes79e53942008-11-07 14:24:08 -08006826
Jesse Barnese70236a2009-09-21 10:42:27 -07006827static int i915_get_display_clock_speed(struct drm_device *dev)
6828{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006829 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006830}
Jesse Barnes79e53942008-11-07 14:24:08 -08006831
Jesse Barnese70236a2009-09-21 10:42:27 -07006832static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6833{
6834 return 200000;
6835}
Jesse Barnes79e53942008-11-07 14:24:08 -08006836
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006837static int pnv_get_display_clock_speed(struct drm_device *dev)
6838{
6839 u16 gcfgc = 0;
6840
6841 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6842
6843 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6844 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006845 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006846 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006847 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006848 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006849 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006850 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6851 return 200000;
6852 default:
6853 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6854 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006855 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006856 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006857 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006858 }
6859}
6860
Jesse Barnese70236a2009-09-21 10:42:27 -07006861static int i915gm_get_display_clock_speed(struct drm_device *dev)
6862{
6863 u16 gcfgc = 0;
6864
6865 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6866
6867 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006868 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006869 else {
6870 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6871 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006872 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006873 default:
6874 case GC_DISPLAY_CLOCK_190_200_MHZ:
6875 return 190000;
6876 }
6877 }
6878}
Jesse Barnes79e53942008-11-07 14:24:08 -08006879
Jesse Barnese70236a2009-09-21 10:42:27 -07006880static int i865_get_display_clock_speed(struct drm_device *dev)
6881{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006882 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006883}
6884
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006885static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006886{
6887 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006888
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006889 /*
6890 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6891 * encoding is different :(
6892 * FIXME is this the right way to detect 852GM/852GMV?
6893 */
6894 if (dev->pdev->revision == 0x1)
6895 return 133333;
6896
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006897 pci_bus_read_config_word(dev->pdev->bus,
6898 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6899
Jesse Barnese70236a2009-09-21 10:42:27 -07006900 /* Assume that the hardware is in the high speed state. This
6901 * should be the default.
6902 */
6903 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6904 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006905 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006906 case GC_CLOCK_100_200:
6907 return 200000;
6908 case GC_CLOCK_166_250:
6909 return 250000;
6910 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006911 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006912 case GC_CLOCK_133_266:
6913 case GC_CLOCK_133_266_2:
6914 case GC_CLOCK_166_266:
6915 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006916 }
6917
6918 /* Shouldn't happen */
6919 return 0;
6920}
6921
6922static int i830_get_display_clock_speed(struct drm_device *dev)
6923{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006924 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006925}
6926
Ville Syrjälä34edce22015-05-22 11:22:33 +03006927static unsigned int intel_hpll_vco(struct drm_device *dev)
6928{
6929 struct drm_i915_private *dev_priv = dev->dev_private;
6930 static const unsigned int blb_vco[8] = {
6931 [0] = 3200000,
6932 [1] = 4000000,
6933 [2] = 5333333,
6934 [3] = 4800000,
6935 [4] = 6400000,
6936 };
6937 static const unsigned int pnv_vco[8] = {
6938 [0] = 3200000,
6939 [1] = 4000000,
6940 [2] = 5333333,
6941 [3] = 4800000,
6942 [4] = 2666667,
6943 };
6944 static const unsigned int cl_vco[8] = {
6945 [0] = 3200000,
6946 [1] = 4000000,
6947 [2] = 5333333,
6948 [3] = 6400000,
6949 [4] = 3333333,
6950 [5] = 3566667,
6951 [6] = 4266667,
6952 };
6953 static const unsigned int elk_vco[8] = {
6954 [0] = 3200000,
6955 [1] = 4000000,
6956 [2] = 5333333,
6957 [3] = 4800000,
6958 };
6959 static const unsigned int ctg_vco[8] = {
6960 [0] = 3200000,
6961 [1] = 4000000,
6962 [2] = 5333333,
6963 [3] = 6400000,
6964 [4] = 2666667,
6965 [5] = 4266667,
6966 };
6967 const unsigned int *vco_table;
6968 unsigned int vco;
6969 uint8_t tmp = 0;
6970
6971 /* FIXME other chipsets? */
6972 if (IS_GM45(dev))
6973 vco_table = ctg_vco;
6974 else if (IS_G4X(dev))
6975 vco_table = elk_vco;
6976 else if (IS_CRESTLINE(dev))
6977 vco_table = cl_vco;
6978 else if (IS_PINEVIEW(dev))
6979 vco_table = pnv_vco;
6980 else if (IS_G33(dev))
6981 vco_table = blb_vco;
6982 else
6983 return 0;
6984
6985 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6986
6987 vco = vco_table[tmp & 0x7];
6988 if (vco == 0)
6989 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6990 else
6991 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6992
6993 return vco;
6994}
6995
6996static int gm45_get_display_clock_speed(struct drm_device *dev)
6997{
6998 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6999 uint16_t tmp = 0;
7000
7001 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7002
7003 cdclk_sel = (tmp >> 12) & 0x1;
7004
7005 switch (vco) {
7006 case 2666667:
7007 case 4000000:
7008 case 5333333:
7009 return cdclk_sel ? 333333 : 222222;
7010 case 3200000:
7011 return cdclk_sel ? 320000 : 228571;
7012 default:
7013 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7014 return 222222;
7015 }
7016}
7017
7018static int i965gm_get_display_clock_speed(struct drm_device *dev)
7019{
7020 static const uint8_t div_3200[] = { 16, 10, 8 };
7021 static const uint8_t div_4000[] = { 20, 12, 10 };
7022 static const uint8_t div_5333[] = { 24, 16, 14 };
7023 const uint8_t *div_table;
7024 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7025 uint16_t tmp = 0;
7026
7027 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7028
7029 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7030
7031 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7032 goto fail;
7033
7034 switch (vco) {
7035 case 3200000:
7036 div_table = div_3200;
7037 break;
7038 case 4000000:
7039 div_table = div_4000;
7040 break;
7041 case 5333333:
7042 div_table = div_5333;
7043 break;
7044 default:
7045 goto fail;
7046 }
7047
7048 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7049
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007050fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007051 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7052 return 200000;
7053}
7054
7055static int g33_get_display_clock_speed(struct drm_device *dev)
7056{
7057 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7058 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7059 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7060 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7061 const uint8_t *div_table;
7062 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7063 uint16_t tmp = 0;
7064
7065 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7066
7067 cdclk_sel = (tmp >> 4) & 0x7;
7068
7069 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7070 goto fail;
7071
7072 switch (vco) {
7073 case 3200000:
7074 div_table = div_3200;
7075 break;
7076 case 4000000:
7077 div_table = div_4000;
7078 break;
7079 case 4800000:
7080 div_table = div_4800;
7081 break;
7082 case 5333333:
7083 div_table = div_5333;
7084 break;
7085 default:
7086 goto fail;
7087 }
7088
7089 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7090
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007091fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007092 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7093 return 190476;
7094}
7095
Zhenyu Wang2c072452009-06-05 15:38:42 +08007096static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007097intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007098{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007099 while (*num > DATA_LINK_M_N_MASK ||
7100 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007101 *num >>= 1;
7102 *den >>= 1;
7103 }
7104}
7105
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007106static void compute_m_n(unsigned int m, unsigned int n,
7107 uint32_t *ret_m, uint32_t *ret_n)
7108{
7109 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7110 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7111 intel_reduce_m_n_ratio(ret_m, ret_n);
7112}
7113
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007114void
7115intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7116 int pixel_clock, int link_clock,
7117 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007118{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007119 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007120
7121 compute_m_n(bits_per_pixel * pixel_clock,
7122 link_clock * nlanes * 8,
7123 &m_n->gmch_m, &m_n->gmch_n);
7124
7125 compute_m_n(pixel_clock, link_clock,
7126 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007127}
7128
Chris Wilsona7615032011-01-12 17:04:08 +00007129static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7130{
Jani Nikulad330a952014-01-21 11:24:25 +02007131 if (i915.panel_use_ssc >= 0)
7132 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007133 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007134 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007135}
7136
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007137static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7138 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007139{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007140 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007141 struct drm_i915_private *dev_priv = dev->dev_private;
7142 int refclk;
7143
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007144 WARN_ON(!crtc_state->base.state);
7145
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007146 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007147 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007148 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007149 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007150 refclk = dev_priv->vbt.lvds_ssc_freq;
7151 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007152 } else if (!IS_GEN2(dev)) {
7153 refclk = 96000;
7154 } else {
7155 refclk = 48000;
7156 }
7157
7158 return refclk;
7159}
7160
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007161static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007162{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007163 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007164}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007165
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007166static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7167{
7168 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007169}
7170
Daniel Vetterf47709a2013-03-28 10:42:02 +01007171static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007172 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007173 intel_clock_t *reduced_clock)
7174{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007175 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007176 u32 fp, fp2 = 0;
7177
7178 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007179 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007180 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007181 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007182 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007183 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007184 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007185 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007186 }
7187
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007188 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007189
Daniel Vetterf47709a2013-03-28 10:42:02 +01007190 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007191 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007192 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007193 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007194 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007195 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007196 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007197 }
7198}
7199
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007200static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7201 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007202{
7203 u32 reg_val;
7204
7205 /*
7206 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7207 * and set it to a reasonable value instead.
7208 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007209 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007210 reg_val &= 0xffffff00;
7211 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007212 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007213
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007214 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007215 reg_val &= 0x8cffffff;
7216 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007217 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007218
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007219 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007220 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007221 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007222
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007223 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007224 reg_val &= 0x00ffffff;
7225 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007226 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007227}
7228
Daniel Vetterb5518422013-05-03 11:49:48 +02007229static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7230 struct intel_link_m_n *m_n)
7231{
7232 struct drm_device *dev = crtc->base.dev;
7233 struct drm_i915_private *dev_priv = dev->dev_private;
7234 int pipe = crtc->pipe;
7235
Daniel Vettere3b95f12013-05-03 11:49:49 +02007236 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7237 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7238 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7239 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007240}
7241
7242static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007243 struct intel_link_m_n *m_n,
7244 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007245{
7246 struct drm_device *dev = crtc->base.dev;
7247 struct drm_i915_private *dev_priv = dev->dev_private;
7248 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007249 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007250
7251 if (INTEL_INFO(dev)->gen >= 5) {
7252 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7253 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7254 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7255 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007256 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7257 * for gen < 8) and if DRRS is supported (to make sure the
7258 * registers are not unnecessarily accessed).
7259 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307260 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007261 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007262 I915_WRITE(PIPE_DATA_M2(transcoder),
7263 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7264 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7265 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7266 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7267 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007268 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007269 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7270 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7271 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7272 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007273 }
7274}
7275
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307276void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007277{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307278 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7279
7280 if (m_n == M1_N1) {
7281 dp_m_n = &crtc->config->dp_m_n;
7282 dp_m2_n2 = &crtc->config->dp_m2_n2;
7283 } else if (m_n == M2_N2) {
7284
7285 /*
7286 * M2_N2 registers are not supported. Hence m2_n2 divider value
7287 * needs to be programmed into M1_N1.
7288 */
7289 dp_m_n = &crtc->config->dp_m2_n2;
7290 } else {
7291 DRM_ERROR("Unsupported divider value\n");
7292 return;
7293 }
7294
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007295 if (crtc->config->has_pch_encoder)
7296 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007297 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307298 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007299}
7300
Daniel Vetter251ac862015-06-18 10:30:24 +02007301static void vlv_compute_dpll(struct intel_crtc *crtc,
7302 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007303{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007304 u32 dpll, dpll_md;
7305
7306 /*
7307 * Enable DPIO clock input. We should never disable the reference
7308 * clock for pipe B, since VGA hotplug / manual detection depends
7309 * on it.
7310 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007311 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7312 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007313 /* We should never disable this, set it here for state tracking */
7314 if (crtc->pipe == PIPE_B)
7315 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7316 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007317 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007318
Ville Syrjäläd288f652014-10-28 13:20:22 +02007319 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007320 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007321 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007322}
7323
Ville Syrjäläd288f652014-10-28 13:20:22 +02007324static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007325 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007326{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007327 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007328 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007329 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007330 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007331 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007332 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007333
Ville Syrjäläa5805162015-05-26 20:42:30 +03007334 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007335
Ville Syrjäläd288f652014-10-28 13:20:22 +02007336 bestn = pipe_config->dpll.n;
7337 bestm1 = pipe_config->dpll.m1;
7338 bestm2 = pipe_config->dpll.m2;
7339 bestp1 = pipe_config->dpll.p1;
7340 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007341
Jesse Barnes89b667f2013-04-18 14:51:36 -07007342 /* See eDP HDMI DPIO driver vbios notes doc */
7343
7344 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007345 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007346 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007347
7348 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007349 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007350
7351 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007352 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007353 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007354 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007355
7356 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007357 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007358
7359 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007360 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7361 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7362 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007363 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007364
7365 /*
7366 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7367 * but we don't support that).
7368 * Note: don't use the DAC post divider as it seems unstable.
7369 */
7370 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007371 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007372
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007373 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007374 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007375
Jesse Barnes89b667f2013-04-18 14:51:36 -07007376 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007377 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007378 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7379 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007380 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007381 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007382 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007383 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007384 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007385
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007386 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007387 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007388 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007389 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007390 0x0df40000);
7391 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007392 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007393 0x0df70000);
7394 } else { /* HDMI or VGA */
7395 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007396 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007397 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007398 0x0df70000);
7399 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007400 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007401 0x0df40000);
7402 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007403
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007404 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007405 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007406 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7407 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007408 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007409 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007410
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007411 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007412 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007413}
7414
Daniel Vetter251ac862015-06-18 10:30:24 +02007415static void chv_compute_dpll(struct intel_crtc *crtc,
7416 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007417{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007418 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7419 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007420 DPLL_VCO_ENABLE;
7421 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007422 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007423
Ville Syrjäläd288f652014-10-28 13:20:22 +02007424 pipe_config->dpll_hw_state.dpll_md =
7425 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007426}
7427
Ville Syrjäläd288f652014-10-28 13:20:22 +02007428static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007429 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007430{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007431 struct drm_device *dev = crtc->base.dev;
7432 struct drm_i915_private *dev_priv = dev->dev_private;
7433 int pipe = crtc->pipe;
7434 int dpll_reg = DPLL(crtc->pipe);
7435 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307436 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007437 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307438 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307439 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007440
Ville Syrjäläd288f652014-10-28 13:20:22 +02007441 bestn = pipe_config->dpll.n;
7442 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7443 bestm1 = pipe_config->dpll.m1;
7444 bestm2 = pipe_config->dpll.m2 >> 22;
7445 bestp1 = pipe_config->dpll.p1;
7446 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307447 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307448 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307449 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007450
7451 /*
7452 * Enable Refclk and SSC
7453 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007454 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007455 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007456
Ville Syrjäläa5805162015-05-26 20:42:30 +03007457 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007458
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007459 /* p1 and p2 divider */
7460 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7461 5 << DPIO_CHV_S1_DIV_SHIFT |
7462 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7463 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7464 1 << DPIO_CHV_K_DIV_SHIFT);
7465
7466 /* Feedback post-divider - m2 */
7467 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7468
7469 /* Feedback refclk divider - n and m1 */
7470 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7471 DPIO_CHV_M1_DIV_BY_2 |
7472 1 << DPIO_CHV_N_DIV_SHIFT);
7473
7474 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307475 if (bestm2_frac)
7476 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007477
7478 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307479 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7480 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7481 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7482 if (bestm2_frac)
7483 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7484 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007485
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307486 /* Program digital lock detect threshold */
7487 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7488 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7489 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7490 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7491 if (!bestm2_frac)
7492 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7493 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7494
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007495 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307496 if (vco == 5400000) {
7497 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7498 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7499 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7500 tribuf_calcntr = 0x9;
7501 } else if (vco <= 6200000) {
7502 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7503 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7504 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7505 tribuf_calcntr = 0x9;
7506 } else if (vco <= 6480000) {
7507 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7508 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7509 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7510 tribuf_calcntr = 0x8;
7511 } else {
7512 /* Not supported. Apply the same limits as in the max case */
7513 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7514 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7515 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7516 tribuf_calcntr = 0;
7517 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007518 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7519
Ville Syrjälä968040b2015-03-11 22:52:08 +02007520 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307521 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7522 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7523 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7524
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007525 /* AFC Recal */
7526 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7527 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7528 DPIO_AFC_RECAL);
7529
Ville Syrjäläa5805162015-05-26 20:42:30 +03007530 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007531}
7532
Ville Syrjäläd288f652014-10-28 13:20:22 +02007533/**
7534 * vlv_force_pll_on - forcibly enable just the PLL
7535 * @dev_priv: i915 private structure
7536 * @pipe: pipe PLL to enable
7537 * @dpll: PLL configuration
7538 *
7539 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7540 * in cases where we need the PLL enabled even when @pipe is not going to
7541 * be enabled.
7542 */
7543void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7544 const struct dpll *dpll)
7545{
7546 struct intel_crtc *crtc =
7547 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007548 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007549 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007550 .pixel_multiplier = 1,
7551 .dpll = *dpll,
7552 };
7553
7554 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007555 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007556 chv_prepare_pll(crtc, &pipe_config);
7557 chv_enable_pll(crtc, &pipe_config);
7558 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007559 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007560 vlv_prepare_pll(crtc, &pipe_config);
7561 vlv_enable_pll(crtc, &pipe_config);
7562 }
7563}
7564
7565/**
7566 * vlv_force_pll_off - forcibly disable just the PLL
7567 * @dev_priv: i915 private structure
7568 * @pipe: pipe PLL to disable
7569 *
7570 * Disable the PLL for @pipe. To be used in cases where we need
7571 * the PLL enabled even when @pipe is not going to be enabled.
7572 */
7573void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7574{
7575 if (IS_CHERRYVIEW(dev))
7576 chv_disable_pll(to_i915(dev), pipe);
7577 else
7578 vlv_disable_pll(to_i915(dev), pipe);
7579}
7580
Daniel Vetter251ac862015-06-18 10:30:24 +02007581static void i9xx_compute_dpll(struct intel_crtc *crtc,
7582 struct intel_crtc_state *crtc_state,
7583 intel_clock_t *reduced_clock,
7584 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007585{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007586 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007587 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007588 u32 dpll;
7589 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007590 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007591
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007592 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307593
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007594 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7595 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007596
7597 dpll = DPLL_VGA_MODE_DIS;
7598
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007599 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007600 dpll |= DPLLB_MODE_LVDS;
7601 else
7602 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007603
Daniel Vetteref1b4602013-06-01 17:17:04 +02007604 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007605 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007606 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007607 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007608
7609 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007610 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007611
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007612 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007613 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007614
7615 /* compute bitmask from p1 value */
7616 if (IS_PINEVIEW(dev))
7617 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7618 else {
7619 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7620 if (IS_G4X(dev) && reduced_clock)
7621 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7622 }
7623 switch (clock->p2) {
7624 case 5:
7625 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7626 break;
7627 case 7:
7628 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7629 break;
7630 case 10:
7631 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7632 break;
7633 case 14:
7634 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7635 break;
7636 }
7637 if (INTEL_INFO(dev)->gen >= 4)
7638 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7639
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007640 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007641 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007642 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007643 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7644 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7645 else
7646 dpll |= PLL_REF_INPUT_DREFCLK;
7647
7648 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007649 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007650
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007651 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007652 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007653 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007654 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007655 }
7656}
7657
Daniel Vetter251ac862015-06-18 10:30:24 +02007658static void i8xx_compute_dpll(struct intel_crtc *crtc,
7659 struct intel_crtc_state *crtc_state,
7660 intel_clock_t *reduced_clock,
7661 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007662{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007663 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007664 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007665 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007666 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007667
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007668 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307669
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007670 dpll = DPLL_VGA_MODE_DIS;
7671
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007672 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007673 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7674 } else {
7675 if (clock->p1 == 2)
7676 dpll |= PLL_P1_DIVIDE_BY_TWO;
7677 else
7678 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7679 if (clock->p2 == 4)
7680 dpll |= PLL_P2_DIVIDE_BY_4;
7681 }
7682
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007683 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007684 dpll |= DPLL_DVO_2X_MODE;
7685
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007686 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007687 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7688 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7689 else
7690 dpll |= PLL_REF_INPUT_DREFCLK;
7691
7692 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007693 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007694}
7695
Daniel Vetter8a654f32013-06-01 17:16:22 +02007696static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007697{
7698 struct drm_device *dev = intel_crtc->base.dev;
7699 struct drm_i915_private *dev_priv = dev->dev_private;
7700 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007701 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007702 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007703 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007704 uint32_t crtc_vtotal, crtc_vblank_end;
7705 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007706
7707 /* We need to be careful not to changed the adjusted mode, for otherwise
7708 * the hw state checker will get angry at the mismatch. */
7709 crtc_vtotal = adjusted_mode->crtc_vtotal;
7710 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007711
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007712 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007713 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007714 crtc_vtotal -= 1;
7715 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007716
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007717 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007718 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7719 else
7720 vsyncshift = adjusted_mode->crtc_hsync_start -
7721 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007722 if (vsyncshift < 0)
7723 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007724 }
7725
7726 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007727 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007728
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007729 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007730 (adjusted_mode->crtc_hdisplay - 1) |
7731 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007732 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007733 (adjusted_mode->crtc_hblank_start - 1) |
7734 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007735 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007736 (adjusted_mode->crtc_hsync_start - 1) |
7737 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7738
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007739 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007740 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007741 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007742 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007743 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007744 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007745 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007746 (adjusted_mode->crtc_vsync_start - 1) |
7747 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7748
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007749 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7750 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7751 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7752 * bits. */
7753 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7754 (pipe == PIPE_B || pipe == PIPE_C))
7755 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7756
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007757 /* pipesrc controls the size that is scaled from, which should
7758 * always be the user's requested size.
7759 */
7760 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007761 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7762 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007763}
7764
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007765static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007766 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007767{
7768 struct drm_device *dev = crtc->base.dev;
7769 struct drm_i915_private *dev_priv = dev->dev_private;
7770 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7771 uint32_t tmp;
7772
7773 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007774 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7775 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007776 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007777 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7778 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007779 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007780 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7781 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007782
7783 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007784 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7785 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007786 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007787 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7788 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007789 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007790 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7791 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007792
7793 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007794 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7795 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7796 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007797 }
7798
7799 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007800 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7801 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7802
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007803 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7804 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007805}
7806
Daniel Vetterf6a83282014-02-11 15:28:57 -08007807void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007808 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007809{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007810 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7811 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7812 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7813 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007814
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007815 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7816 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7817 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7818 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007819
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007820 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007821 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007822
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007823 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7824 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007825
7826 mode->hsync = drm_mode_hsync(mode);
7827 mode->vrefresh = drm_mode_vrefresh(mode);
7828 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007829}
7830
Daniel Vetter84b046f2013-02-19 18:48:54 +01007831static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7832{
7833 struct drm_device *dev = intel_crtc->base.dev;
7834 struct drm_i915_private *dev_priv = dev->dev_private;
7835 uint32_t pipeconf;
7836
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007837 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007838
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007839 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7840 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7841 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007842
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007843 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007844 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007845
Daniel Vetterff9ce462013-04-24 14:57:17 +02007846 /* only g4x and later have fancy bpc/dither controls */
7847 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007848 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007849 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007850 pipeconf |= PIPECONF_DITHER_EN |
7851 PIPECONF_DITHER_TYPE_SP;
7852
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007853 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007854 case 18:
7855 pipeconf |= PIPECONF_6BPC;
7856 break;
7857 case 24:
7858 pipeconf |= PIPECONF_8BPC;
7859 break;
7860 case 30:
7861 pipeconf |= PIPECONF_10BPC;
7862 break;
7863 default:
7864 /* Case prevented by intel_choose_pipe_bpp_dither. */
7865 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007866 }
7867 }
7868
7869 if (HAS_PIPE_CXSR(dev)) {
7870 if (intel_crtc->lowfreq_avail) {
7871 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7872 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7873 } else {
7874 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007875 }
7876 }
7877
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007878 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007879 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007880 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007881 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7882 else
7883 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7884 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007885 pipeconf |= PIPECONF_PROGRESSIVE;
7886
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007887 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007888 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007889
Daniel Vetter84b046f2013-02-19 18:48:54 +01007890 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7891 POSTING_READ(PIPECONF(intel_crtc->pipe));
7892}
7893
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007894static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7895 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007896{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007897 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007898 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007899 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007900 intel_clock_t clock;
7901 bool ok;
7902 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007903 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007904 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007905 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007906 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007907 struct drm_connector_state *connector_state;
7908 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007909
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007910 memset(&crtc_state->dpll_hw_state, 0,
7911 sizeof(crtc_state->dpll_hw_state));
7912
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007913 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007914 if (connector_state->crtc != &crtc->base)
7915 continue;
7916
7917 encoder = to_intel_encoder(connector_state->best_encoder);
7918
Chris Wilson5eddb702010-09-11 13:48:45 +01007919 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007920 case INTEL_OUTPUT_DSI:
7921 is_dsi = true;
7922 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007923 default:
7924 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007925 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007926
Eric Anholtc751ce42010-03-25 11:48:48 -07007927 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007928 }
7929
Jani Nikulaf2335332013-09-13 11:03:09 +03007930 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007931 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007932
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007933 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007934 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007935
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007936 /*
7937 * Returns a set of divisors for the desired target clock with
7938 * the given refclk, or FALSE. The returned values represent
7939 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7940 * 2) / p1 / p2.
7941 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007942 limit = intel_limit(crtc_state, refclk);
7943 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007944 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007945 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007946 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007947 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7948 return -EINVAL;
7949 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007950
Jani Nikulaf2335332013-09-13 11:03:09 +03007951 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007952 crtc_state->dpll.n = clock.n;
7953 crtc_state->dpll.m1 = clock.m1;
7954 crtc_state->dpll.m2 = clock.m2;
7955 crtc_state->dpll.p1 = clock.p1;
7956 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007957 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007958
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007959 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007960 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007961 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007962 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007963 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007964 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007965 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007966 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007967 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007968 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007969 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007970
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007971 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007972}
7973
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007974static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007975 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007976{
7977 struct drm_device *dev = crtc->base.dev;
7978 struct drm_i915_private *dev_priv = dev->dev_private;
7979 uint32_t tmp;
7980
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007981 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7982 return;
7983
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007984 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007985 if (!(tmp & PFIT_ENABLE))
7986 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007987
Daniel Vetter06922822013-07-11 13:35:40 +02007988 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007989 if (INTEL_INFO(dev)->gen < 4) {
7990 if (crtc->pipe != PIPE_B)
7991 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007992 } else {
7993 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7994 return;
7995 }
7996
Daniel Vetter06922822013-07-11 13:35:40 +02007997 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007998 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7999 if (INTEL_INFO(dev)->gen < 5)
8000 pipe_config->gmch_pfit.lvds_border_bits =
8001 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8002}
8003
Jesse Barnesacbec812013-09-20 11:29:32 -07008004static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008005 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008006{
8007 struct drm_device *dev = crtc->base.dev;
8008 struct drm_i915_private *dev_priv = dev->dev_private;
8009 int pipe = pipe_config->cpu_transcoder;
8010 intel_clock_t clock;
8011 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008012 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008013
Shobhit Kumarf573de52014-07-30 20:32:37 +05308014 /* In case of MIPI DPLL will not even be used */
8015 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8016 return;
8017
Ville Syrjäläa5805162015-05-26 20:42:30 +03008018 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008019 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008020 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008021
8022 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8023 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8024 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8025 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8026 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8027
Imre Deakdccbea32015-06-22 23:35:51 +03008028 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008029}
8030
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008031static void
8032i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8033 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008034{
8035 struct drm_device *dev = crtc->base.dev;
8036 struct drm_i915_private *dev_priv = dev->dev_private;
8037 u32 val, base, offset;
8038 int pipe = crtc->pipe, plane = crtc->plane;
8039 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008040 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008041 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008042 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008043
Damien Lespiau42a7b082015-02-05 19:35:13 +00008044 val = I915_READ(DSPCNTR(plane));
8045 if (!(val & DISPLAY_PLANE_ENABLE))
8046 return;
8047
Damien Lespiaud9806c92015-01-21 14:07:19 +00008048 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008049 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050 DRM_DEBUG_KMS("failed to alloc fb\n");
8051 return;
8052 }
8053
Damien Lespiau1b842c82015-01-21 13:50:54 +00008054 fb = &intel_fb->base;
8055
Daniel Vetter18c52472015-02-10 17:16:09 +00008056 if (INTEL_INFO(dev)->gen >= 4) {
8057 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008058 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008059 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8060 }
8061 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008062
8063 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008064 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008065 fb->pixel_format = fourcc;
8066 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008067
8068 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008069 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008070 offset = I915_READ(DSPTILEOFF(plane));
8071 else
8072 offset = I915_READ(DSPLINOFF(plane));
8073 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8074 } else {
8075 base = I915_READ(DSPADDR(plane));
8076 }
8077 plane_config->base = base;
8078
8079 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008080 fb->width = ((val >> 16) & 0xfff) + 1;
8081 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008082
8083 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008084 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008085
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008086 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008087 fb->pixel_format,
8088 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008089
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008090 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008091
Damien Lespiau2844a922015-01-20 12:51:48 +00008092 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8093 pipe_name(pipe), plane, fb->width, fb->height,
8094 fb->bits_per_pixel, base, fb->pitches[0],
8095 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008096
Damien Lespiau2d140302015-02-05 17:22:18 +00008097 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008098}
8099
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008100static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008101 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008102{
8103 struct drm_device *dev = crtc->base.dev;
8104 struct drm_i915_private *dev_priv = dev->dev_private;
8105 int pipe = pipe_config->cpu_transcoder;
8106 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8107 intel_clock_t clock;
8108 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8109 int refclk = 100000;
8110
Ville Syrjäläa5805162015-05-26 20:42:30 +03008111 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008112 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8113 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8114 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8115 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008116 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008117
8118 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8119 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8120 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8121 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8122 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8123
Imre Deakdccbea32015-06-22 23:35:51 +03008124 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008125}
8126
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008127static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008128 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008129{
8130 struct drm_device *dev = crtc->base.dev;
8131 struct drm_i915_private *dev_priv = dev->dev_private;
8132 uint32_t tmp;
8133
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008134 if (!intel_display_power_is_enabled(dev_priv,
8135 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008136 return false;
8137
Daniel Vettere143a212013-07-04 12:01:15 +02008138 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008139 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008140
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008141 tmp = I915_READ(PIPECONF(crtc->pipe));
8142 if (!(tmp & PIPECONF_ENABLE))
8143 return false;
8144
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008145 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8146 switch (tmp & PIPECONF_BPC_MASK) {
8147 case PIPECONF_6BPC:
8148 pipe_config->pipe_bpp = 18;
8149 break;
8150 case PIPECONF_8BPC:
8151 pipe_config->pipe_bpp = 24;
8152 break;
8153 case PIPECONF_10BPC:
8154 pipe_config->pipe_bpp = 30;
8155 break;
8156 default:
8157 break;
8158 }
8159 }
8160
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008161 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8162 pipe_config->limited_color_range = true;
8163
Ville Syrjälä282740f2013-09-04 18:30:03 +03008164 if (INTEL_INFO(dev)->gen < 4)
8165 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8166
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008167 intel_get_pipe_timings(crtc, pipe_config);
8168
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008169 i9xx_get_pfit_config(crtc, pipe_config);
8170
Daniel Vetter6c49f242013-06-06 12:45:25 +02008171 if (INTEL_INFO(dev)->gen >= 4) {
8172 tmp = I915_READ(DPLL_MD(crtc->pipe));
8173 pipe_config->pixel_multiplier =
8174 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8175 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008176 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008177 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8178 tmp = I915_READ(DPLL(crtc->pipe));
8179 pipe_config->pixel_multiplier =
8180 ((tmp & SDVO_MULTIPLIER_MASK)
8181 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8182 } else {
8183 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8184 * port and will be fixed up in the encoder->get_config
8185 * function. */
8186 pipe_config->pixel_multiplier = 1;
8187 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008188 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8189 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008190 /*
8191 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8192 * on 830. Filter it out here so that we don't
8193 * report errors due to that.
8194 */
8195 if (IS_I830(dev))
8196 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8197
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008198 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8199 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008200 } else {
8201 /* Mask out read-only status bits. */
8202 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8203 DPLL_PORTC_READY_MASK |
8204 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008205 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008206
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008207 if (IS_CHERRYVIEW(dev))
8208 chv_crtc_clock_get(crtc, pipe_config);
8209 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008210 vlv_crtc_clock_get(crtc, pipe_config);
8211 else
8212 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008213
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008214 return true;
8215}
8216
Paulo Zanonidde86e22012-12-01 12:04:25 -02008217static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008218{
8219 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008220 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008221 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008222 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008223 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008224 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008225 bool has_ck505 = false;
8226 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008227
8228 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008229 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008230 switch (encoder->type) {
8231 case INTEL_OUTPUT_LVDS:
8232 has_panel = true;
8233 has_lvds = true;
8234 break;
8235 case INTEL_OUTPUT_EDP:
8236 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008237 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008238 has_cpu_edp = true;
8239 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008240 default:
8241 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008242 }
8243 }
8244
Keith Packard99eb6a02011-09-26 14:29:12 -07008245 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008246 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008247 can_ssc = has_ck505;
8248 } else {
8249 has_ck505 = false;
8250 can_ssc = true;
8251 }
8252
Imre Deak2de69052013-05-08 13:14:04 +03008253 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8254 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008255
8256 /* Ironlake: try to setup display ref clock before DPLL
8257 * enabling. This is only under driver's control after
8258 * PCH B stepping, previous chipset stepping should be
8259 * ignoring this setting.
8260 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008261 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008262
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008263 /* As we must carefully and slowly disable/enable each source in turn,
8264 * compute the final state we want first and check if we need to
8265 * make any changes at all.
8266 */
8267 final = val;
8268 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008269 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008270 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008271 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008272 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8273
8274 final &= ~DREF_SSC_SOURCE_MASK;
8275 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8276 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008277
Keith Packard199e5d72011-09-22 12:01:57 -07008278 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008279 final |= DREF_SSC_SOURCE_ENABLE;
8280
8281 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8282 final |= DREF_SSC1_ENABLE;
8283
8284 if (has_cpu_edp) {
8285 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8286 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8287 else
8288 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8289 } else
8290 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8291 } else {
8292 final |= DREF_SSC_SOURCE_DISABLE;
8293 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8294 }
8295
8296 if (final == val)
8297 return;
8298
8299 /* Always enable nonspread source */
8300 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8301
8302 if (has_ck505)
8303 val |= DREF_NONSPREAD_CK505_ENABLE;
8304 else
8305 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8306
8307 if (has_panel) {
8308 val &= ~DREF_SSC_SOURCE_MASK;
8309 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008310
Keith Packard199e5d72011-09-22 12:01:57 -07008311 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008312 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008313 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008314 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008315 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008316 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008317
8318 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008319 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008320 POSTING_READ(PCH_DREF_CONTROL);
8321 udelay(200);
8322
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008323 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008324
8325 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008326 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008327 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008328 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008329 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008330 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008331 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008332 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008333 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008334
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008335 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008336 POSTING_READ(PCH_DREF_CONTROL);
8337 udelay(200);
8338 } else {
8339 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8340
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008341 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008342
8343 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008344 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008345
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008346 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008347 POSTING_READ(PCH_DREF_CONTROL);
8348 udelay(200);
8349
8350 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008351 val &= ~DREF_SSC_SOURCE_MASK;
8352 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008353
8354 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008355 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008356
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008357 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008358 POSTING_READ(PCH_DREF_CONTROL);
8359 udelay(200);
8360 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008361
8362 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008363}
8364
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008365static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008366{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008367 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008368
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008369 tmp = I915_READ(SOUTH_CHICKEN2);
8370 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8371 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008372
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008373 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8374 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8375 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008376
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008377 tmp = I915_READ(SOUTH_CHICKEN2);
8378 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8379 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008380
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008381 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8382 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8383 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008384}
8385
8386/* WaMPhyProgramming:hsw */
8387static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8388{
8389 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008390
8391 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8392 tmp &= ~(0xFF << 24);
8393 tmp |= (0x12 << 24);
8394 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8395
Paulo Zanonidde86e22012-12-01 12:04:25 -02008396 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8397 tmp |= (1 << 11);
8398 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8399
8400 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8401 tmp |= (1 << 11);
8402 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8403
Paulo Zanonidde86e22012-12-01 12:04:25 -02008404 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8405 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8406 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8407
8408 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8409 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8410 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8411
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008412 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8413 tmp &= ~(7 << 13);
8414 tmp |= (5 << 13);
8415 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008416
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008417 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8418 tmp &= ~(7 << 13);
8419 tmp |= (5 << 13);
8420 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008421
8422 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8423 tmp &= ~0xFF;
8424 tmp |= 0x1C;
8425 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8426
8427 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8428 tmp &= ~0xFF;
8429 tmp |= 0x1C;
8430 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8431
8432 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8433 tmp &= ~(0xFF << 16);
8434 tmp |= (0x1C << 16);
8435 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8436
8437 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8438 tmp &= ~(0xFF << 16);
8439 tmp |= (0x1C << 16);
8440 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8441
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008442 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8443 tmp |= (1 << 27);
8444 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008445
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008446 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8447 tmp |= (1 << 27);
8448 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008449
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008450 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8451 tmp &= ~(0xF << 28);
8452 tmp |= (4 << 28);
8453 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008454
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008455 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8456 tmp &= ~(0xF << 28);
8457 tmp |= (4 << 28);
8458 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008459}
8460
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008461/* Implements 3 different sequences from BSpec chapter "Display iCLK
8462 * Programming" based on the parameters passed:
8463 * - Sequence to enable CLKOUT_DP
8464 * - Sequence to enable CLKOUT_DP without spread
8465 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8466 */
8467static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8468 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008469{
8470 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008471 uint32_t reg, tmp;
8472
8473 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8474 with_spread = true;
8475 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8476 with_fdi, "LP PCH doesn't have FDI\n"))
8477 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008478
Ville Syrjäläa5805162015-05-26 20:42:30 +03008479 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008480
8481 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8482 tmp &= ~SBI_SSCCTL_DISABLE;
8483 tmp |= SBI_SSCCTL_PATHALT;
8484 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8485
8486 udelay(24);
8487
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008488 if (with_spread) {
8489 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8490 tmp &= ~SBI_SSCCTL_PATHALT;
8491 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008492
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008493 if (with_fdi) {
8494 lpt_reset_fdi_mphy(dev_priv);
8495 lpt_program_fdi_mphy(dev_priv);
8496 }
8497 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008498
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008499 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8500 SBI_GEN0 : SBI_DBUFF0;
8501 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8502 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8503 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008504
Ville Syrjäläa5805162015-05-26 20:42:30 +03008505 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008506}
8507
Paulo Zanoni47701c32013-07-23 11:19:25 -03008508/* Sequence to disable CLKOUT_DP */
8509static void lpt_disable_clkout_dp(struct drm_device *dev)
8510{
8511 struct drm_i915_private *dev_priv = dev->dev_private;
8512 uint32_t reg, tmp;
8513
Ville Syrjäläa5805162015-05-26 20:42:30 +03008514 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008515
8516 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8517 SBI_GEN0 : SBI_DBUFF0;
8518 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8519 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8520 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8521
8522 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8523 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8524 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8525 tmp |= SBI_SSCCTL_PATHALT;
8526 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8527 udelay(32);
8528 }
8529 tmp |= SBI_SSCCTL_DISABLE;
8530 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8531 }
8532
Ville Syrjäläa5805162015-05-26 20:42:30 +03008533 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008534}
8535
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008536static void lpt_init_pch_refclk(struct drm_device *dev)
8537{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008538 struct intel_encoder *encoder;
8539 bool has_vga = false;
8540
Damien Lespiaub2784e12014-08-05 11:29:37 +01008541 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008542 switch (encoder->type) {
8543 case INTEL_OUTPUT_ANALOG:
8544 has_vga = true;
8545 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008546 default:
8547 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008548 }
8549 }
8550
Paulo Zanoni47701c32013-07-23 11:19:25 -03008551 if (has_vga)
8552 lpt_enable_clkout_dp(dev, true, true);
8553 else
8554 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008555}
8556
Paulo Zanonidde86e22012-12-01 12:04:25 -02008557/*
8558 * Initialize reference clocks when the driver loads
8559 */
8560void intel_init_pch_refclk(struct drm_device *dev)
8561{
8562 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8563 ironlake_init_pch_refclk(dev);
8564 else if (HAS_PCH_LPT(dev))
8565 lpt_init_pch_refclk(dev);
8566}
8567
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008568static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008569{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008570 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008571 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008572 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008573 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008574 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008575 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008576 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008577 bool is_lvds = false;
8578
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008579 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008580 if (connector_state->crtc != crtc_state->base.crtc)
8581 continue;
8582
8583 encoder = to_intel_encoder(connector_state->best_encoder);
8584
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008585 switch (encoder->type) {
8586 case INTEL_OUTPUT_LVDS:
8587 is_lvds = true;
8588 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008589 default:
8590 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008591 }
8592 num_connectors++;
8593 }
8594
8595 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008596 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008597 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008598 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008599 }
8600
8601 return 120000;
8602}
8603
Daniel Vetter6ff93602013-04-19 11:24:36 +02008604static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008605{
8606 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8607 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8608 int pipe = intel_crtc->pipe;
8609 uint32_t val;
8610
Daniel Vetter78114072013-06-13 00:54:57 +02008611 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008612
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008613 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008614 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008615 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008616 break;
8617 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008618 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008619 break;
8620 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008621 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008622 break;
8623 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008624 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008625 break;
8626 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008627 /* Case prevented by intel_choose_pipe_bpp_dither. */
8628 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008629 }
8630
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008631 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008632 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8633
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008634 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008635 val |= PIPECONF_INTERLACED_ILK;
8636 else
8637 val |= PIPECONF_PROGRESSIVE;
8638
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008639 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008640 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008641
Paulo Zanonic8203562012-09-12 10:06:29 -03008642 I915_WRITE(PIPECONF(pipe), val);
8643 POSTING_READ(PIPECONF(pipe));
8644}
8645
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008646/*
8647 * Set up the pipe CSC unit.
8648 *
8649 * Currently only full range RGB to limited range RGB conversion
8650 * is supported, but eventually this should handle various
8651 * RGB<->YCbCr scenarios as well.
8652 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008653static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008654{
8655 struct drm_device *dev = crtc->dev;
8656 struct drm_i915_private *dev_priv = dev->dev_private;
8657 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8658 int pipe = intel_crtc->pipe;
8659 uint16_t coeff = 0x7800; /* 1.0 */
8660
8661 /*
8662 * TODO: Check what kind of values actually come out of the pipe
8663 * with these coeff/postoff values and adjust to get the best
8664 * accuracy. Perhaps we even need to take the bpc value into
8665 * consideration.
8666 */
8667
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008668 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008669 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8670
8671 /*
8672 * GY/GU and RY/RU should be the other way around according
8673 * to BSpec, but reality doesn't agree. Just set them up in
8674 * a way that results in the correct picture.
8675 */
8676 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8677 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8678
8679 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8680 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8681
8682 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8683 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8684
8685 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8686 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8687 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8688
8689 if (INTEL_INFO(dev)->gen > 6) {
8690 uint16_t postoff = 0;
8691
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008692 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008693 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008694
8695 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8696 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8697 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8698
8699 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8700 } else {
8701 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8702
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008703 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008704 mode |= CSC_BLACK_SCREEN_OFFSET;
8705
8706 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8707 }
8708}
8709
Daniel Vetter6ff93602013-04-19 11:24:36 +02008710static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008711{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008712 struct drm_device *dev = crtc->dev;
8713 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008714 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008715 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008716 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008717 uint32_t val;
8718
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008719 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008720
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008721 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008722 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8723
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008724 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008725 val |= PIPECONF_INTERLACED_ILK;
8726 else
8727 val |= PIPECONF_PROGRESSIVE;
8728
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008729 I915_WRITE(PIPECONF(cpu_transcoder), val);
8730 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008731
8732 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8733 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008734
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308735 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008736 val = 0;
8737
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008738 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008739 case 18:
8740 val |= PIPEMISC_DITHER_6_BPC;
8741 break;
8742 case 24:
8743 val |= PIPEMISC_DITHER_8_BPC;
8744 break;
8745 case 30:
8746 val |= PIPEMISC_DITHER_10_BPC;
8747 break;
8748 case 36:
8749 val |= PIPEMISC_DITHER_12_BPC;
8750 break;
8751 default:
8752 /* Case prevented by pipe_config_set_bpp. */
8753 BUG();
8754 }
8755
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008756 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008757 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8758
8759 I915_WRITE(PIPEMISC(pipe), val);
8760 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008761}
8762
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008763static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008764 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008765 intel_clock_t *clock,
8766 bool *has_reduced_clock,
8767 intel_clock_t *reduced_clock)
8768{
8769 struct drm_device *dev = crtc->dev;
8770 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008771 int refclk;
8772 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008773 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008774
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008775 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008776
8777 /*
8778 * Returns a set of divisors for the desired target clock with the given
8779 * refclk, or FALSE. The returned values represent the clock equation:
8780 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8781 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008782 limit = intel_limit(crtc_state, refclk);
8783 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008784 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008785 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008786 if (!ret)
8787 return false;
8788
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008789 return true;
8790}
8791
Paulo Zanonid4b19312012-11-29 11:29:32 -02008792int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8793{
8794 /*
8795 * Account for spread spectrum to avoid
8796 * oversubscribing the link. Max center spread
8797 * is 2.5%; use 5% for safety's sake.
8798 */
8799 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008800 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008801}
8802
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008803static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008804{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008805 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008806}
8807
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008808static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008809 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008810 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008811 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008812{
8813 struct drm_crtc *crtc = &intel_crtc->base;
8814 struct drm_device *dev = crtc->dev;
8815 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008816 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008817 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008818 struct drm_connector_state *connector_state;
8819 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008820 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008821 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008822 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008823
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008824 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008825 if (connector_state->crtc != crtc_state->base.crtc)
8826 continue;
8827
8828 encoder = to_intel_encoder(connector_state->best_encoder);
8829
8830 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008831 case INTEL_OUTPUT_LVDS:
8832 is_lvds = true;
8833 break;
8834 case INTEL_OUTPUT_SDVO:
8835 case INTEL_OUTPUT_HDMI:
8836 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008837 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008838 default:
8839 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008840 }
8841
8842 num_connectors++;
8843 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008844
Chris Wilsonc1858122010-12-03 21:35:48 +00008845 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008846 factor = 21;
8847 if (is_lvds) {
8848 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008849 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008850 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008851 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008852 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008853 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008854
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008855 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008856 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008857
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008858 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8859 *fp2 |= FP_CB_TUNE;
8860
Chris Wilson5eddb702010-09-11 13:48:45 +01008861 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008862
Eric Anholta07d6782011-03-30 13:01:08 -07008863 if (is_lvds)
8864 dpll |= DPLLB_MODE_LVDS;
8865 else
8866 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008867
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008868 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008869 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008870
8871 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008872 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008873 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008874 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008875
Eric Anholta07d6782011-03-30 13:01:08 -07008876 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008877 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008878 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008879 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008880
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008881 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008882 case 5:
8883 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8884 break;
8885 case 7:
8886 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8887 break;
8888 case 10:
8889 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8890 break;
8891 case 14:
8892 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8893 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008894 }
8895
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008896 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008897 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008898 else
8899 dpll |= PLL_REF_INPUT_DREFCLK;
8900
Daniel Vetter959e16d2013-06-05 13:34:21 +02008901 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008902}
8903
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008904static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8905 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008906{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008907 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008908 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008909 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008910 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008911 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008912 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008913
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008914 memset(&crtc_state->dpll_hw_state, 0,
8915 sizeof(crtc_state->dpll_hw_state));
8916
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008917 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008918
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008919 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8920 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8921
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008922 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008923 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008924 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008925 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8926 return -EINVAL;
8927 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008928 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008929 if (!crtc_state->clock_set) {
8930 crtc_state->dpll.n = clock.n;
8931 crtc_state->dpll.m1 = clock.m1;
8932 crtc_state->dpll.m2 = clock.m2;
8933 crtc_state->dpll.p1 = clock.p1;
8934 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008935 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008936
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008937 /* 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 +02008938 if (crtc_state->has_pch_encoder) {
8939 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008940 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008941 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008942
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008943 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008944 &fp, &reduced_clock,
8945 has_reduced_clock ? &fp2 : NULL);
8946
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008947 crtc_state->dpll_hw_state.dpll = dpll;
8948 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008949 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008950 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008951 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008952 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008953
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008954 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008955 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008956 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008957 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008958 return -EINVAL;
8959 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008960 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008961
Rodrigo Viviab585de2015-03-24 12:40:09 -07008962 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008963 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008964 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008965 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008966
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008967 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008968}
8969
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008970static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8971 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008972{
8973 struct drm_device *dev = crtc->base.dev;
8974 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008975 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008976
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008977 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8978 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8979 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8980 & ~TU_SIZE_MASK;
8981 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8982 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8983 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8984}
8985
8986static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8987 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008988 struct intel_link_m_n *m_n,
8989 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008990{
8991 struct drm_device *dev = crtc->base.dev;
8992 struct drm_i915_private *dev_priv = dev->dev_private;
8993 enum pipe pipe = crtc->pipe;
8994
8995 if (INTEL_INFO(dev)->gen >= 5) {
8996 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8997 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8998 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8999 & ~TU_SIZE_MASK;
9000 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9001 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9002 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009003 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9004 * gen < 8) and if DRRS is supported (to make sure the
9005 * registers are not unnecessarily read).
9006 */
9007 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009008 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009009 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9010 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9011 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9012 & ~TU_SIZE_MASK;
9013 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9014 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9015 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9016 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009017 } else {
9018 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9019 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9020 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9021 & ~TU_SIZE_MASK;
9022 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9023 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9024 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9025 }
9026}
9027
9028void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009029 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009030{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009031 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009032 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9033 else
9034 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009035 &pipe_config->dp_m_n,
9036 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009037}
9038
Daniel Vetter72419202013-04-04 13:28:53 +02009039static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009040 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009041{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009042 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009043 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009044}
9045
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009046static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009047 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009048{
9049 struct drm_device *dev = crtc->base.dev;
9050 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009051 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9052 uint32_t ps_ctrl = 0;
9053 int id = -1;
9054 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009055
Chandra Kondurua1b22782015-04-07 15:28:45 -07009056 /* find scaler attached to this pipe */
9057 for (i = 0; i < crtc->num_scalers; i++) {
9058 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9059 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9060 id = i;
9061 pipe_config->pch_pfit.enabled = true;
9062 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9063 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9064 break;
9065 }
9066 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009067
Chandra Kondurua1b22782015-04-07 15:28:45 -07009068 scaler_state->scaler_id = id;
9069 if (id >= 0) {
9070 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9071 } else {
9072 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009073 }
9074}
9075
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009076static void
9077skylake_get_initial_plane_config(struct intel_crtc *crtc,
9078 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009079{
9080 struct drm_device *dev = crtc->base.dev;
9081 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009082 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009083 int pipe = crtc->pipe;
9084 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009085 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009086 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009087 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009088
Damien Lespiaud9806c92015-01-21 14:07:19 +00009089 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009090 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009091 DRM_DEBUG_KMS("failed to alloc fb\n");
9092 return;
9093 }
9094
Damien Lespiau1b842c82015-01-21 13:50:54 +00009095 fb = &intel_fb->base;
9096
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009097 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009098 if (!(val & PLANE_CTL_ENABLE))
9099 goto error;
9100
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009101 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9102 fourcc = skl_format_to_fourcc(pixel_format,
9103 val & PLANE_CTL_ORDER_RGBX,
9104 val & PLANE_CTL_ALPHA_MASK);
9105 fb->pixel_format = fourcc;
9106 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9107
Damien Lespiau40f46282015-02-27 11:15:21 +00009108 tiling = val & PLANE_CTL_TILED_MASK;
9109 switch (tiling) {
9110 case PLANE_CTL_TILED_LINEAR:
9111 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9112 break;
9113 case PLANE_CTL_TILED_X:
9114 plane_config->tiling = I915_TILING_X;
9115 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9116 break;
9117 case PLANE_CTL_TILED_Y:
9118 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9119 break;
9120 case PLANE_CTL_TILED_YF:
9121 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9122 break;
9123 default:
9124 MISSING_CASE(tiling);
9125 goto error;
9126 }
9127
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009128 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9129 plane_config->base = base;
9130
9131 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9132
9133 val = I915_READ(PLANE_SIZE(pipe, 0));
9134 fb->height = ((val >> 16) & 0xfff) + 1;
9135 fb->width = ((val >> 0) & 0x1fff) + 1;
9136
9137 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009138 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9139 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009140 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9141
9142 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009143 fb->pixel_format,
9144 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009145
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009146 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009147
9148 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9149 pipe_name(pipe), fb->width, fb->height,
9150 fb->bits_per_pixel, base, fb->pitches[0],
9151 plane_config->size);
9152
Damien Lespiau2d140302015-02-05 17:22:18 +00009153 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009154 return;
9155
9156error:
9157 kfree(fb);
9158}
9159
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009160static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009161 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009162{
9163 struct drm_device *dev = crtc->base.dev;
9164 struct drm_i915_private *dev_priv = dev->dev_private;
9165 uint32_t tmp;
9166
9167 tmp = I915_READ(PF_CTL(crtc->pipe));
9168
9169 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009170 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009171 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9172 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009173
9174 /* We currently do not free assignements of panel fitters on
9175 * ivb/hsw (since we don't use the higher upscaling modes which
9176 * differentiates them) so just WARN about this case for now. */
9177 if (IS_GEN7(dev)) {
9178 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9179 PF_PIPE_SEL_IVB(crtc->pipe));
9180 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009181 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009182}
9183
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009184static void
9185ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9186 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009187{
9188 struct drm_device *dev = crtc->base.dev;
9189 struct drm_i915_private *dev_priv = dev->dev_private;
9190 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009191 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009192 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009193 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009194 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009195 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009196
Damien Lespiau42a7b082015-02-05 19:35:13 +00009197 val = I915_READ(DSPCNTR(pipe));
9198 if (!(val & DISPLAY_PLANE_ENABLE))
9199 return;
9200
Damien Lespiaud9806c92015-01-21 14:07:19 +00009201 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009202 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009203 DRM_DEBUG_KMS("failed to alloc fb\n");
9204 return;
9205 }
9206
Damien Lespiau1b842c82015-01-21 13:50:54 +00009207 fb = &intel_fb->base;
9208
Daniel Vetter18c52472015-02-10 17:16:09 +00009209 if (INTEL_INFO(dev)->gen >= 4) {
9210 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009211 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009212 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9213 }
9214 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009215
9216 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009217 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009218 fb->pixel_format = fourcc;
9219 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009220
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009221 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009222 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009223 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009224 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009225 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009226 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009227 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009228 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009229 }
9230 plane_config->base = base;
9231
9232 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009233 fb->width = ((val >> 16) & 0xfff) + 1;
9234 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009235
9236 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009237 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009238
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009239 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009240 fb->pixel_format,
9241 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009242
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009243 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009244
Damien Lespiau2844a922015-01-20 12:51:48 +00009245 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9246 pipe_name(pipe), fb->width, fb->height,
9247 fb->bits_per_pixel, base, fb->pitches[0],
9248 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009249
Damien Lespiau2d140302015-02-05 17:22:18 +00009250 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009251}
9252
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009253static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009254 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009255{
9256 struct drm_device *dev = crtc->base.dev;
9257 struct drm_i915_private *dev_priv = dev->dev_private;
9258 uint32_t tmp;
9259
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009260 if (!intel_display_power_is_enabled(dev_priv,
9261 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009262 return false;
9263
Daniel Vettere143a212013-07-04 12:01:15 +02009264 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009265 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009266
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009267 tmp = I915_READ(PIPECONF(crtc->pipe));
9268 if (!(tmp & PIPECONF_ENABLE))
9269 return false;
9270
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009271 switch (tmp & PIPECONF_BPC_MASK) {
9272 case PIPECONF_6BPC:
9273 pipe_config->pipe_bpp = 18;
9274 break;
9275 case PIPECONF_8BPC:
9276 pipe_config->pipe_bpp = 24;
9277 break;
9278 case PIPECONF_10BPC:
9279 pipe_config->pipe_bpp = 30;
9280 break;
9281 case PIPECONF_12BPC:
9282 pipe_config->pipe_bpp = 36;
9283 break;
9284 default:
9285 break;
9286 }
9287
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009288 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9289 pipe_config->limited_color_range = true;
9290
Daniel Vetterab9412b2013-05-03 11:49:46 +02009291 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009292 struct intel_shared_dpll *pll;
9293
Daniel Vetter88adfff2013-03-28 10:42:01 +01009294 pipe_config->has_pch_encoder = true;
9295
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009296 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9297 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9298 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009299
9300 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009301
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009302 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009303 pipe_config->shared_dpll =
9304 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009305 } else {
9306 tmp = I915_READ(PCH_DPLL_SEL);
9307 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9308 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9309 else
9310 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9311 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009312
9313 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9314
9315 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9316 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009317
9318 tmp = pipe_config->dpll_hw_state.dpll;
9319 pipe_config->pixel_multiplier =
9320 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9321 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009322
9323 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009324 } else {
9325 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009326 }
9327
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009328 intel_get_pipe_timings(crtc, pipe_config);
9329
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009330 ironlake_get_pfit_config(crtc, pipe_config);
9331
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009332 return true;
9333}
9334
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009335static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9336{
9337 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009338 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009339
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009340 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009341 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009342 pipe_name(crtc->pipe));
9343
Rob Clarke2c719b2014-12-15 13:56:32 -05009344 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9345 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9346 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9347 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9348 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9349 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009350 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009351 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009352 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009353 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009354 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009355 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009356 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009357 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009358 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009359
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009360 /*
9361 * In theory we can still leave IRQs enabled, as long as only the HPD
9362 * interrupts remain enabled. We used to check for that, but since it's
9363 * gen-specific and since we only disable LCPLL after we fully disable
9364 * the interrupts, the check below should be enough.
9365 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009366 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009367}
9368
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009369static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9370{
9371 struct drm_device *dev = dev_priv->dev;
9372
9373 if (IS_HASWELL(dev))
9374 return I915_READ(D_COMP_HSW);
9375 else
9376 return I915_READ(D_COMP_BDW);
9377}
9378
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009379static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9380{
9381 struct drm_device *dev = dev_priv->dev;
9382
9383 if (IS_HASWELL(dev)) {
9384 mutex_lock(&dev_priv->rps.hw_lock);
9385 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9386 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009387 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009388 mutex_unlock(&dev_priv->rps.hw_lock);
9389 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009390 I915_WRITE(D_COMP_BDW, val);
9391 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009392 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009393}
9394
9395/*
9396 * This function implements pieces of two sequences from BSpec:
9397 * - Sequence for display software to disable LCPLL
9398 * - Sequence for display software to allow package C8+
9399 * The steps implemented here are just the steps that actually touch the LCPLL
9400 * register. Callers should take care of disabling all the display engine
9401 * functions, doing the mode unset, fixing interrupts, etc.
9402 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009403static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9404 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009405{
9406 uint32_t val;
9407
9408 assert_can_disable_lcpll(dev_priv);
9409
9410 val = I915_READ(LCPLL_CTL);
9411
9412 if (switch_to_fclk) {
9413 val |= LCPLL_CD_SOURCE_FCLK;
9414 I915_WRITE(LCPLL_CTL, val);
9415
9416 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9417 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9418 DRM_ERROR("Switching to FCLK failed\n");
9419
9420 val = I915_READ(LCPLL_CTL);
9421 }
9422
9423 val |= LCPLL_PLL_DISABLE;
9424 I915_WRITE(LCPLL_CTL, val);
9425 POSTING_READ(LCPLL_CTL);
9426
9427 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9428 DRM_ERROR("LCPLL still locked\n");
9429
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009430 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009431 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009432 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009433 ndelay(100);
9434
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009435 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9436 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009437 DRM_ERROR("D_COMP RCOMP still in progress\n");
9438
9439 if (allow_power_down) {
9440 val = I915_READ(LCPLL_CTL);
9441 val |= LCPLL_POWER_DOWN_ALLOW;
9442 I915_WRITE(LCPLL_CTL, val);
9443 POSTING_READ(LCPLL_CTL);
9444 }
9445}
9446
9447/*
9448 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9449 * source.
9450 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009451static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009452{
9453 uint32_t val;
9454
9455 val = I915_READ(LCPLL_CTL);
9456
9457 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9458 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9459 return;
9460
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009461 /*
9462 * Make sure we're not on PC8 state before disabling PC8, otherwise
9463 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009464 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009465 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009466
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009467 if (val & LCPLL_POWER_DOWN_ALLOW) {
9468 val &= ~LCPLL_POWER_DOWN_ALLOW;
9469 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009470 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471 }
9472
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009473 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009474 val |= D_COMP_COMP_FORCE;
9475 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009476 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009477
9478 val = I915_READ(LCPLL_CTL);
9479 val &= ~LCPLL_PLL_DISABLE;
9480 I915_WRITE(LCPLL_CTL, val);
9481
9482 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9483 DRM_ERROR("LCPLL not locked yet\n");
9484
9485 if (val & LCPLL_CD_SOURCE_FCLK) {
9486 val = I915_READ(LCPLL_CTL);
9487 val &= ~LCPLL_CD_SOURCE_FCLK;
9488 I915_WRITE(LCPLL_CTL, val);
9489
9490 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9491 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9492 DRM_ERROR("Switching back to LCPLL failed\n");
9493 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009494
Mika Kuoppala59bad942015-01-16 11:34:40 +02009495 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009496 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009497}
9498
Paulo Zanoni765dab672014-03-07 20:08:18 -03009499/*
9500 * Package states C8 and deeper are really deep PC states that can only be
9501 * reached when all the devices on the system allow it, so even if the graphics
9502 * device allows PC8+, it doesn't mean the system will actually get to these
9503 * states. Our driver only allows PC8+ when going into runtime PM.
9504 *
9505 * The requirements for PC8+ are that all the outputs are disabled, the power
9506 * well is disabled and most interrupts are disabled, and these are also
9507 * requirements for runtime PM. When these conditions are met, we manually do
9508 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9509 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9510 * hang the machine.
9511 *
9512 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9513 * the state of some registers, so when we come back from PC8+ we need to
9514 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9515 * need to take care of the registers kept by RC6. Notice that this happens even
9516 * if we don't put the device in PCI D3 state (which is what currently happens
9517 * because of the runtime PM support).
9518 *
9519 * For more, read "Display Sequences for Package C8" on the hardware
9520 * documentation.
9521 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009522void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009523{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009524 struct drm_device *dev = dev_priv->dev;
9525 uint32_t val;
9526
Paulo Zanonic67a4702013-08-19 13:18:09 -03009527 DRM_DEBUG_KMS("Enabling package C8+\n");
9528
Paulo Zanonic67a4702013-08-19 13:18:09 -03009529 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9530 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9531 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9532 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9533 }
9534
9535 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009536 hsw_disable_lcpll(dev_priv, true, true);
9537}
9538
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009539void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009540{
9541 struct drm_device *dev = dev_priv->dev;
9542 uint32_t val;
9543
Paulo Zanonic67a4702013-08-19 13:18:09 -03009544 DRM_DEBUG_KMS("Disabling package C8+\n");
9545
9546 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009547 lpt_init_pch_refclk(dev);
9548
9549 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9550 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9551 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9552 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9553 }
9554
9555 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009556}
9557
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009558static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309559{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009560 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009561 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309562
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009563 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309564}
9565
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009566/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009567static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009568{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009569 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009570 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009571 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009572
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009573 for_each_intel_crtc(state->dev, intel_crtc) {
9574 int pixel_rate;
9575
9576 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9577 if (IS_ERR(crtc_state))
9578 return PTR_ERR(crtc_state);
9579
9580 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009581 continue;
9582
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009583 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009584
9585 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009586 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009587 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9588
9589 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9590 }
9591
9592 return max_pixel_rate;
9593}
9594
9595static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9596{
9597 struct drm_i915_private *dev_priv = dev->dev_private;
9598 uint32_t val, data;
9599 int ret;
9600
9601 if (WARN((I915_READ(LCPLL_CTL) &
9602 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9603 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9604 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9605 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9606 "trying to change cdclk frequency with cdclk not enabled\n"))
9607 return;
9608
9609 mutex_lock(&dev_priv->rps.hw_lock);
9610 ret = sandybridge_pcode_write(dev_priv,
9611 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9612 mutex_unlock(&dev_priv->rps.hw_lock);
9613 if (ret) {
9614 DRM_ERROR("failed to inform pcode about cdclk change\n");
9615 return;
9616 }
9617
9618 val = I915_READ(LCPLL_CTL);
9619 val |= LCPLL_CD_SOURCE_FCLK;
9620 I915_WRITE(LCPLL_CTL, val);
9621
9622 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9623 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9624 DRM_ERROR("Switching to FCLK failed\n");
9625
9626 val = I915_READ(LCPLL_CTL);
9627 val &= ~LCPLL_CLK_FREQ_MASK;
9628
9629 switch (cdclk) {
9630 case 450000:
9631 val |= LCPLL_CLK_FREQ_450;
9632 data = 0;
9633 break;
9634 case 540000:
9635 val |= LCPLL_CLK_FREQ_54O_BDW;
9636 data = 1;
9637 break;
9638 case 337500:
9639 val |= LCPLL_CLK_FREQ_337_5_BDW;
9640 data = 2;
9641 break;
9642 case 675000:
9643 val |= LCPLL_CLK_FREQ_675_BDW;
9644 data = 3;
9645 break;
9646 default:
9647 WARN(1, "invalid cdclk frequency\n");
9648 return;
9649 }
9650
9651 I915_WRITE(LCPLL_CTL, val);
9652
9653 val = I915_READ(LCPLL_CTL);
9654 val &= ~LCPLL_CD_SOURCE_FCLK;
9655 I915_WRITE(LCPLL_CTL, val);
9656
9657 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9658 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9659 DRM_ERROR("Switching back to LCPLL failed\n");
9660
9661 mutex_lock(&dev_priv->rps.hw_lock);
9662 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9663 mutex_unlock(&dev_priv->rps.hw_lock);
9664
9665 intel_update_cdclk(dev);
9666
9667 WARN(cdclk != dev_priv->cdclk_freq,
9668 "cdclk requested %d kHz but got %d kHz\n",
9669 cdclk, dev_priv->cdclk_freq);
9670}
9671
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009672static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009673{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009674 struct drm_i915_private *dev_priv = to_i915(state->dev);
9675 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009676 int cdclk;
9677
9678 /*
9679 * FIXME should also account for plane ratio
9680 * once 64bpp pixel formats are supported.
9681 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009682 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009683 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009684 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009685 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009686 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009687 cdclk = 450000;
9688 else
9689 cdclk = 337500;
9690
9691 /*
9692 * FIXME move the cdclk caclulation to
9693 * compute_config() so we can fail gracegully.
9694 */
9695 if (cdclk > dev_priv->max_cdclk_freq) {
9696 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9697 cdclk, dev_priv->max_cdclk_freq);
9698 cdclk = dev_priv->max_cdclk_freq;
9699 }
9700
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009701 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009702
9703 return 0;
9704}
9705
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009706static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009707{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009708 struct drm_device *dev = old_state->dev;
9709 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009710
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009711 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009712}
9713
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009714static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9715 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009716{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009717 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009718 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009719
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009720 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009721
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009722 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009723}
9724
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309725static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9726 enum port port,
9727 struct intel_crtc_state *pipe_config)
9728{
9729 switch (port) {
9730 case PORT_A:
9731 pipe_config->ddi_pll_sel = SKL_DPLL0;
9732 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9733 break;
9734 case PORT_B:
9735 pipe_config->ddi_pll_sel = SKL_DPLL1;
9736 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9737 break;
9738 case PORT_C:
9739 pipe_config->ddi_pll_sel = SKL_DPLL2;
9740 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9741 break;
9742 default:
9743 DRM_ERROR("Incorrect port type\n");
9744 }
9745}
9746
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009747static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9748 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009749 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009750{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009751 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009752
9753 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9754 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9755
9756 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009757 case SKL_DPLL0:
9758 /*
9759 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9760 * of the shared DPLL framework and thus needs to be read out
9761 * separately
9762 */
9763 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9764 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9765 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009766 case SKL_DPLL1:
9767 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9768 break;
9769 case SKL_DPLL2:
9770 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9771 break;
9772 case SKL_DPLL3:
9773 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9774 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009775 }
9776}
9777
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009778static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9779 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009780 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009781{
9782 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9783
9784 switch (pipe_config->ddi_pll_sel) {
9785 case PORT_CLK_SEL_WRPLL1:
9786 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9787 break;
9788 case PORT_CLK_SEL_WRPLL2:
9789 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9790 break;
9791 }
9792}
9793
Daniel Vetter26804af2014-06-25 22:01:55 +03009794static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009795 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009796{
9797 struct drm_device *dev = crtc->base.dev;
9798 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009799 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009800 enum port port;
9801 uint32_t tmp;
9802
9803 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9804
9805 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9806
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009807 if (IS_SKYLAKE(dev))
9808 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309809 else if (IS_BROXTON(dev))
9810 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009811 else
9812 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009813
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009814 if (pipe_config->shared_dpll >= 0) {
9815 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9816
9817 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9818 &pipe_config->dpll_hw_state));
9819 }
9820
Daniel Vetter26804af2014-06-25 22:01:55 +03009821 /*
9822 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9823 * DDI E. So just check whether this pipe is wired to DDI E and whether
9824 * the PCH transcoder is on.
9825 */
Damien Lespiauca370452013-12-03 13:56:24 +00009826 if (INTEL_INFO(dev)->gen < 9 &&
9827 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009828 pipe_config->has_pch_encoder = true;
9829
9830 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9831 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9832 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9833
9834 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9835 }
9836}
9837
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009838static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009839 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009840{
9841 struct drm_device *dev = crtc->base.dev;
9842 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009843 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009844 uint32_t tmp;
9845
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009846 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009847 POWER_DOMAIN_PIPE(crtc->pipe)))
9848 return false;
9849
Daniel Vettere143a212013-07-04 12:01:15 +02009850 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009851 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9852
Daniel Vettereccb1402013-05-22 00:50:22 +02009853 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9854 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9855 enum pipe trans_edp_pipe;
9856 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9857 default:
9858 WARN(1, "unknown pipe linked to edp transcoder\n");
9859 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9860 case TRANS_DDI_EDP_INPUT_A_ON:
9861 trans_edp_pipe = PIPE_A;
9862 break;
9863 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9864 trans_edp_pipe = PIPE_B;
9865 break;
9866 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9867 trans_edp_pipe = PIPE_C;
9868 break;
9869 }
9870
9871 if (trans_edp_pipe == crtc->pipe)
9872 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9873 }
9874
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009875 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009876 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009877 return false;
9878
Daniel Vettereccb1402013-05-22 00:50:22 +02009879 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009880 if (!(tmp & PIPECONF_ENABLE))
9881 return false;
9882
Daniel Vetter26804af2014-06-25 22:01:55 +03009883 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009884
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009885 intel_get_pipe_timings(crtc, pipe_config);
9886
Chandra Kondurua1b22782015-04-07 15:28:45 -07009887 if (INTEL_INFO(dev)->gen >= 9) {
9888 skl_init_scalers(dev, crtc, pipe_config);
9889 }
9890
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009891 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009892
9893 if (INTEL_INFO(dev)->gen >= 9) {
9894 pipe_config->scaler_state.scaler_id = -1;
9895 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9896 }
9897
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009898 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009899 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009900 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009901 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009902 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009903 else
9904 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009905 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009906
Jesse Barnese59150d2014-01-07 13:30:45 -08009907 if (IS_HASWELL(dev))
9908 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9909 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009910
Clint Taylorebb69c92014-09-30 10:30:22 -07009911 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9912 pipe_config->pixel_multiplier =
9913 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9914 } else {
9915 pipe_config->pixel_multiplier = 1;
9916 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009917
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009918 return true;
9919}
9920
Chris Wilson560b85b2010-08-07 11:01:38 +01009921static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9922{
9923 struct drm_device *dev = crtc->dev;
9924 struct drm_i915_private *dev_priv = dev->dev_private;
9925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009926 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009927
Ville Syrjälädc41c152014-08-13 11:57:05 +03009928 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009929 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9930 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009931 unsigned int stride = roundup_pow_of_two(width) * 4;
9932
9933 switch (stride) {
9934 default:
9935 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9936 width, stride);
9937 stride = 256;
9938 /* fallthrough */
9939 case 256:
9940 case 512:
9941 case 1024:
9942 case 2048:
9943 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009944 }
9945
Ville Syrjälädc41c152014-08-13 11:57:05 +03009946 cntl |= CURSOR_ENABLE |
9947 CURSOR_GAMMA_ENABLE |
9948 CURSOR_FORMAT_ARGB |
9949 CURSOR_STRIDE(stride);
9950
9951 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009952 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009953
Ville Syrjälädc41c152014-08-13 11:57:05 +03009954 if (intel_crtc->cursor_cntl != 0 &&
9955 (intel_crtc->cursor_base != base ||
9956 intel_crtc->cursor_size != size ||
9957 intel_crtc->cursor_cntl != cntl)) {
9958 /* On these chipsets we can only modify the base/size/stride
9959 * whilst the cursor is disabled.
9960 */
9961 I915_WRITE(_CURACNTR, 0);
9962 POSTING_READ(_CURACNTR);
9963 intel_crtc->cursor_cntl = 0;
9964 }
9965
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009966 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009967 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009968 intel_crtc->cursor_base = base;
9969 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009970
9971 if (intel_crtc->cursor_size != size) {
9972 I915_WRITE(CURSIZE, size);
9973 intel_crtc->cursor_size = size;
9974 }
9975
Chris Wilson4b0e3332014-05-30 16:35:26 +03009976 if (intel_crtc->cursor_cntl != cntl) {
9977 I915_WRITE(_CURACNTR, cntl);
9978 POSTING_READ(_CURACNTR);
9979 intel_crtc->cursor_cntl = cntl;
9980 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009981}
9982
9983static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9984{
9985 struct drm_device *dev = crtc->dev;
9986 struct drm_i915_private *dev_priv = dev->dev_private;
9987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9988 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009989 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009990
Chris Wilson4b0e3332014-05-30 16:35:26 +03009991 cntl = 0;
9992 if (base) {
9993 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009994 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309995 case 64:
9996 cntl |= CURSOR_MODE_64_ARGB_AX;
9997 break;
9998 case 128:
9999 cntl |= CURSOR_MODE_128_ARGB_AX;
10000 break;
10001 case 256:
10002 cntl |= CURSOR_MODE_256_ARGB_AX;
10003 break;
10004 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010005 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010006 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010007 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010008 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010009
10010 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10011 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010012 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010013
Matt Roper8e7d6882015-01-21 16:35:41 -080010014 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010015 cntl |= CURSOR_ROTATE_180;
10016
Chris Wilson4b0e3332014-05-30 16:35:26 +030010017 if (intel_crtc->cursor_cntl != cntl) {
10018 I915_WRITE(CURCNTR(pipe), cntl);
10019 POSTING_READ(CURCNTR(pipe));
10020 intel_crtc->cursor_cntl = cntl;
10021 }
10022
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010023 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010024 I915_WRITE(CURBASE(pipe), base);
10025 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010026
10027 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010028}
10029
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010030/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010031static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10032 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010033{
10034 struct drm_device *dev = crtc->dev;
10035 struct drm_i915_private *dev_priv = dev->dev_private;
10036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10037 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010038 int x = crtc->cursor_x;
10039 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010040 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010041
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010042 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010043 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010044
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010045 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010046 base = 0;
10047
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010048 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010049 base = 0;
10050
10051 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010052 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010053 base = 0;
10054
10055 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10056 x = -x;
10057 }
10058 pos |= x << CURSOR_X_SHIFT;
10059
10060 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010061 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010062 base = 0;
10063
10064 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10065 y = -y;
10066 }
10067 pos |= y << CURSOR_Y_SHIFT;
10068
Chris Wilson4b0e3332014-05-30 16:35:26 +030010069 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010070 return;
10071
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010072 I915_WRITE(CURPOS(pipe), pos);
10073
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010074 /* ILK+ do this automagically */
10075 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010076 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010077 base += (intel_crtc->base.cursor->state->crtc_h *
10078 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010079 }
10080
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010081 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010082 i845_update_cursor(crtc, base);
10083 else
10084 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010085}
10086
Ville Syrjälädc41c152014-08-13 11:57:05 +030010087static bool cursor_size_ok(struct drm_device *dev,
10088 uint32_t width, uint32_t height)
10089{
10090 if (width == 0 || height == 0)
10091 return false;
10092
10093 /*
10094 * 845g/865g are special in that they are only limited by
10095 * the width of their cursors, the height is arbitrary up to
10096 * the precision of the register. Everything else requires
10097 * square cursors, limited to a few power-of-two sizes.
10098 */
10099 if (IS_845G(dev) || IS_I865G(dev)) {
10100 if ((width & 63) != 0)
10101 return false;
10102
10103 if (width > (IS_845G(dev) ? 64 : 512))
10104 return false;
10105
10106 if (height > 1023)
10107 return false;
10108 } else {
10109 switch (width | height) {
10110 case 256:
10111 case 128:
10112 if (IS_GEN2(dev))
10113 return false;
10114 case 64:
10115 break;
10116 default:
10117 return false;
10118 }
10119 }
10120
10121 return true;
10122}
10123
Jesse Barnes79e53942008-11-07 14:24:08 -080010124static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010125 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010126{
James Simmons72034252010-08-03 01:33:19 +010010127 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010128 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010129
James Simmons72034252010-08-03 01:33:19 +010010130 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010131 intel_crtc->lut_r[i] = red[i] >> 8;
10132 intel_crtc->lut_g[i] = green[i] >> 8;
10133 intel_crtc->lut_b[i] = blue[i] >> 8;
10134 }
10135
10136 intel_crtc_load_lut(crtc);
10137}
10138
Jesse Barnes79e53942008-11-07 14:24:08 -080010139/* VESA 640x480x72Hz mode to set on the pipe */
10140static struct drm_display_mode load_detect_mode = {
10141 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10142 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10143};
10144
Daniel Vettera8bb6812014-02-10 18:00:39 +010010145struct drm_framebuffer *
10146__intel_framebuffer_create(struct drm_device *dev,
10147 struct drm_mode_fb_cmd2 *mode_cmd,
10148 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010149{
10150 struct intel_framebuffer *intel_fb;
10151 int ret;
10152
10153 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10154 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010155 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010156 return ERR_PTR(-ENOMEM);
10157 }
10158
10159 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010160 if (ret)
10161 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010162
10163 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010164err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010165 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010166 kfree(intel_fb);
10167
10168 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010169}
10170
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010171static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010172intel_framebuffer_create(struct drm_device *dev,
10173 struct drm_mode_fb_cmd2 *mode_cmd,
10174 struct drm_i915_gem_object *obj)
10175{
10176 struct drm_framebuffer *fb;
10177 int ret;
10178
10179 ret = i915_mutex_lock_interruptible(dev);
10180 if (ret)
10181 return ERR_PTR(ret);
10182 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10183 mutex_unlock(&dev->struct_mutex);
10184
10185 return fb;
10186}
10187
Chris Wilsond2dff872011-04-19 08:36:26 +010010188static u32
10189intel_framebuffer_pitch_for_width(int width, int bpp)
10190{
10191 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10192 return ALIGN(pitch, 64);
10193}
10194
10195static u32
10196intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10197{
10198 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010199 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010200}
10201
10202static struct drm_framebuffer *
10203intel_framebuffer_create_for_mode(struct drm_device *dev,
10204 struct drm_display_mode *mode,
10205 int depth, int bpp)
10206{
10207 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010208 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010209
10210 obj = i915_gem_alloc_object(dev,
10211 intel_framebuffer_size_for_mode(mode, bpp));
10212 if (obj == NULL)
10213 return ERR_PTR(-ENOMEM);
10214
10215 mode_cmd.width = mode->hdisplay;
10216 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010217 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10218 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010219 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010220
10221 return intel_framebuffer_create(dev, &mode_cmd, obj);
10222}
10223
10224static struct drm_framebuffer *
10225mode_fits_in_fbdev(struct drm_device *dev,
10226 struct drm_display_mode *mode)
10227{
Daniel Vetter4520f532013-10-09 09:18:51 +020010228#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010229 struct drm_i915_private *dev_priv = dev->dev_private;
10230 struct drm_i915_gem_object *obj;
10231 struct drm_framebuffer *fb;
10232
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010233 if (!dev_priv->fbdev)
10234 return NULL;
10235
10236 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010237 return NULL;
10238
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010239 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010240 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010241
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010242 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010243 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10244 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010245 return NULL;
10246
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010247 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010248 return NULL;
10249
10250 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010251#else
10252 return NULL;
10253#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010254}
10255
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010256static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10257 struct drm_crtc *crtc,
10258 struct drm_display_mode *mode,
10259 struct drm_framebuffer *fb,
10260 int x, int y)
10261{
10262 struct drm_plane_state *plane_state;
10263 int hdisplay, vdisplay;
10264 int ret;
10265
10266 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10267 if (IS_ERR(plane_state))
10268 return PTR_ERR(plane_state);
10269
10270 if (mode)
10271 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10272 else
10273 hdisplay = vdisplay = 0;
10274
10275 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10276 if (ret)
10277 return ret;
10278 drm_atomic_set_fb_for_plane(plane_state, fb);
10279 plane_state->crtc_x = 0;
10280 plane_state->crtc_y = 0;
10281 plane_state->crtc_w = hdisplay;
10282 plane_state->crtc_h = vdisplay;
10283 plane_state->src_x = x << 16;
10284 plane_state->src_y = y << 16;
10285 plane_state->src_w = hdisplay << 16;
10286 plane_state->src_h = vdisplay << 16;
10287
10288 return 0;
10289}
10290
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010291bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010292 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010293 struct intel_load_detect_pipe *old,
10294 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010295{
10296 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010297 struct intel_encoder *intel_encoder =
10298 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010299 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010300 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010301 struct drm_crtc *crtc = NULL;
10302 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010303 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010304 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010305 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010306 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010307 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010308 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010309
Chris Wilsond2dff872011-04-19 08:36:26 +010010310 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010311 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010312 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010313
Rob Clark51fd3712013-11-19 12:10:12 -050010314retry:
10315 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10316 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010317 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010318
Jesse Barnes79e53942008-11-07 14:24:08 -080010319 /*
10320 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010321 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010322 * - if the connector already has an assigned crtc, use it (but make
10323 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010324 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010325 * - try to find the first unused crtc that can drive this connector,
10326 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010327 */
10328
10329 /* See if we already have a CRTC for this connector */
10330 if (encoder->crtc) {
10331 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010332
Rob Clark51fd3712013-11-19 12:10:12 -050010333 ret = drm_modeset_lock(&crtc->mutex, ctx);
10334 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010335 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010336 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10337 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010338 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010339
Daniel Vetter24218aa2012-08-12 19:27:11 +020010340 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010341 old->load_detect_temp = false;
10342
10343 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010344 if (connector->dpms != DRM_MODE_DPMS_ON)
10345 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010346
Chris Wilson71731882011-04-19 23:10:58 +010010347 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010348 }
10349
10350 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010351 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010352 i++;
10353 if (!(encoder->possible_crtcs & (1 << i)))
10354 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010355 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010356 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010357
10358 crtc = possible_crtc;
10359 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010360 }
10361
10362 /*
10363 * If we didn't find an unused CRTC, don't use any.
10364 */
10365 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010366 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010367 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010368 }
10369
Rob Clark51fd3712013-11-19 12:10:12 -050010370 ret = drm_modeset_lock(&crtc->mutex, ctx);
10371 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010372 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010373 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10374 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010375 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010376
10377 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010378 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010379 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010380 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010381
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010382 state = drm_atomic_state_alloc(dev);
10383 if (!state)
10384 return false;
10385
10386 state->acquire_ctx = ctx;
10387
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010388 connector_state = drm_atomic_get_connector_state(state, connector);
10389 if (IS_ERR(connector_state)) {
10390 ret = PTR_ERR(connector_state);
10391 goto fail;
10392 }
10393
10394 connector_state->crtc = crtc;
10395 connector_state->best_encoder = &intel_encoder->base;
10396
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010397 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10398 if (IS_ERR(crtc_state)) {
10399 ret = PTR_ERR(crtc_state);
10400 goto fail;
10401 }
10402
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010403 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010404
Chris Wilson64927112011-04-20 07:25:26 +010010405 if (!mode)
10406 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010407
Chris Wilsond2dff872011-04-19 08:36:26 +010010408 /* We need a framebuffer large enough to accommodate all accesses
10409 * that the plane may generate whilst we perform load detection.
10410 * We can not rely on the fbcon either being present (we get called
10411 * during its initialisation to detect all boot displays, or it may
10412 * not even exist) or that it is large enough to satisfy the
10413 * requested mode.
10414 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010415 fb = mode_fits_in_fbdev(dev, mode);
10416 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010417 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010418 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10419 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010420 } else
10421 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010422 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010423 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010424 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010425 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010426
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010427 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10428 if (ret)
10429 goto fail;
10430
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010431 drm_mode_copy(&crtc_state->base.mode, mode);
10432
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010433 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010434 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010435 if (old->release_fb)
10436 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010437 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010438 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010439 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010440
Jesse Barnes79e53942008-11-07 14:24:08 -080010441 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010442 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010443 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010444
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010445fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010446 drm_atomic_state_free(state);
10447 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010448
Rob Clark51fd3712013-11-19 12:10:12 -050010449 if (ret == -EDEADLK) {
10450 drm_modeset_backoff(ctx);
10451 goto retry;
10452 }
10453
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010454 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010455}
10456
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010457void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010458 struct intel_load_detect_pipe *old,
10459 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010460{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010461 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010462 struct intel_encoder *intel_encoder =
10463 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010464 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010465 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010466 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010467 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010468 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010469 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010470 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010471
Chris Wilsond2dff872011-04-19 08:36:26 +010010472 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010473 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010474 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010475
Chris Wilson8261b192011-04-19 23:18:09 +010010476 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010477 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010478 if (!state)
10479 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010480
10481 state->acquire_ctx = ctx;
10482
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010483 connector_state = drm_atomic_get_connector_state(state, connector);
10484 if (IS_ERR(connector_state))
10485 goto fail;
10486
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010487 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10488 if (IS_ERR(crtc_state))
10489 goto fail;
10490
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010491 connector_state->best_encoder = NULL;
10492 connector_state->crtc = NULL;
10493
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010494 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010495
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010496 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10497 0, 0);
10498 if (ret)
10499 goto fail;
10500
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010501 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010502 if (ret)
10503 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010504
Daniel Vetter36206362012-12-10 20:42:17 +010010505 if (old->release_fb) {
10506 drm_framebuffer_unregister_private(old->release_fb);
10507 drm_framebuffer_unreference(old->release_fb);
10508 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010509
Chris Wilson0622a532011-04-21 09:32:11 +010010510 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010511 }
10512
Eric Anholtc751ce42010-03-25 11:48:48 -070010513 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010514 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10515 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010516
10517 return;
10518fail:
10519 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10520 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010521}
10522
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010523static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010524 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010525{
10526 struct drm_i915_private *dev_priv = dev->dev_private;
10527 u32 dpll = pipe_config->dpll_hw_state.dpll;
10528
10529 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010530 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010531 else if (HAS_PCH_SPLIT(dev))
10532 return 120000;
10533 else if (!IS_GEN2(dev))
10534 return 96000;
10535 else
10536 return 48000;
10537}
10538
Jesse Barnes79e53942008-11-07 14:24:08 -080010539/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010540static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010541 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010542{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010543 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010544 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010545 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010546 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010547 u32 fp;
10548 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010549 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010550 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010551
10552 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010553 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010554 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010555 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010556
10557 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010558 if (IS_PINEVIEW(dev)) {
10559 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10560 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010561 } else {
10562 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10563 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10564 }
10565
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010566 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010567 if (IS_PINEVIEW(dev))
10568 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10569 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010570 else
10571 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010572 DPLL_FPA01_P1_POST_DIV_SHIFT);
10573
10574 switch (dpll & DPLL_MODE_MASK) {
10575 case DPLLB_MODE_DAC_SERIAL:
10576 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10577 5 : 10;
10578 break;
10579 case DPLLB_MODE_LVDS:
10580 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10581 7 : 14;
10582 break;
10583 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010584 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010585 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010586 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010587 }
10588
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010589 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010590 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010591 else
Imre Deakdccbea32015-06-22 23:35:51 +030010592 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010593 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010594 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010595 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010596
10597 if (is_lvds) {
10598 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10599 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010600
10601 if (lvds & LVDS_CLKB_POWER_UP)
10602 clock.p2 = 7;
10603 else
10604 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010605 } else {
10606 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10607 clock.p1 = 2;
10608 else {
10609 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10610 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10611 }
10612 if (dpll & PLL_P2_DIVIDE_BY_4)
10613 clock.p2 = 4;
10614 else
10615 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010616 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010617
Imre Deakdccbea32015-06-22 23:35:51 +030010618 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010619 }
10620
Ville Syrjälä18442d02013-09-13 16:00:08 +030010621 /*
10622 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010623 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010624 * encoder's get_config() function.
10625 */
Imre Deakdccbea32015-06-22 23:35:51 +030010626 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010627}
10628
Ville Syrjälä6878da02013-09-13 15:59:11 +030010629int intel_dotclock_calculate(int link_freq,
10630 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010631{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010632 /*
10633 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010634 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010635 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010636 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010637 *
10638 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010639 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010640 */
10641
Ville Syrjälä6878da02013-09-13 15:59:11 +030010642 if (!m_n->link_n)
10643 return 0;
10644
10645 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10646}
10647
Ville Syrjälä18442d02013-09-13 16:00:08 +030010648static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010649 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010650{
10651 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010652
10653 /* read out port_clock from the DPLL */
10654 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010655
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010656 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010657 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010658 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010659 * agree once we know their relationship in the encoder's
10660 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010661 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010662 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010663 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10664 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010665}
10666
10667/** Returns the currently programmed mode of the given pipe. */
10668struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10669 struct drm_crtc *crtc)
10670{
Jesse Barnes548f2452011-02-17 10:40:53 -080010671 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010672 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010673 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010674 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010675 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010676 int htot = I915_READ(HTOTAL(cpu_transcoder));
10677 int hsync = I915_READ(HSYNC(cpu_transcoder));
10678 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10679 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010680 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010681
10682 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10683 if (!mode)
10684 return NULL;
10685
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010686 /*
10687 * Construct a pipe_config sufficient for getting the clock info
10688 * back out of crtc_clock_get.
10689 *
10690 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10691 * to use a real value here instead.
10692 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010693 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010694 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010695 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10696 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10697 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010698 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10699
Ville Syrjälä773ae032013-09-23 17:48:20 +030010700 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010701 mode->hdisplay = (htot & 0xffff) + 1;
10702 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10703 mode->hsync_start = (hsync & 0xffff) + 1;
10704 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10705 mode->vdisplay = (vtot & 0xffff) + 1;
10706 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10707 mode->vsync_start = (vsync & 0xffff) + 1;
10708 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10709
10710 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010711
10712 return mode;
10713}
10714
Chris Wilsonf047e392012-07-21 12:31:41 +010010715void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010716{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010717 struct drm_i915_private *dev_priv = dev->dev_private;
10718
Chris Wilsonf62a0072014-02-21 17:55:39 +000010719 if (dev_priv->mm.busy)
10720 return;
10721
Paulo Zanoni43694d62014-03-07 20:08:08 -030010722 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010723 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010724 if (INTEL_INFO(dev)->gen >= 6)
10725 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010726 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010727}
10728
10729void intel_mark_idle(struct drm_device *dev)
10730{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010731 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010732
Chris Wilsonf62a0072014-02-21 17:55:39 +000010733 if (!dev_priv->mm.busy)
10734 return;
10735
10736 dev_priv->mm.busy = false;
10737
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010738 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010739 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010740
Paulo Zanoni43694d62014-03-07 20:08:08 -030010741 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010742}
10743
Jesse Barnes79e53942008-11-07 14:24:08 -080010744static void intel_crtc_destroy(struct drm_crtc *crtc)
10745{
10746 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010747 struct drm_device *dev = crtc->dev;
10748 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010749
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010750 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010751 work = intel_crtc->unpin_work;
10752 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010753 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010754
10755 if (work) {
10756 cancel_work_sync(&work->work);
10757 kfree(work);
10758 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010759
10760 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010761
Jesse Barnes79e53942008-11-07 14:24:08 -080010762 kfree(intel_crtc);
10763}
10764
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010765static void intel_unpin_work_fn(struct work_struct *__work)
10766{
10767 struct intel_unpin_work *work =
10768 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010769 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10770 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -030010771 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010772 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010773
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010774 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010775 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010776 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010777
Paulo Zanoni7733b492015-07-07 15:26:04 -030010778 intel_fbc_update(dev_priv);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010779
10780 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010781 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010782 mutex_unlock(&dev->struct_mutex);
10783
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010784 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010785 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010786
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010787 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10788 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010789
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010790 kfree(work);
10791}
10792
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010793static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010794 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010795{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010796 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10797 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010798 unsigned long flags;
10799
10800 /* Ignore early vblank irqs */
10801 if (intel_crtc == NULL)
10802 return;
10803
Daniel Vetterf3260382014-09-15 14:55:23 +020010804 /*
10805 * This is called both by irq handlers and the reset code (to complete
10806 * lost pageflips) so needs the full irqsave spinlocks.
10807 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010808 spin_lock_irqsave(&dev->event_lock, flags);
10809 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010810
10811 /* Ensure we don't miss a work->pending update ... */
10812 smp_rmb();
10813
10814 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010815 spin_unlock_irqrestore(&dev->event_lock, flags);
10816 return;
10817 }
10818
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010819 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010820
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010821 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010822}
10823
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010824void intel_finish_page_flip(struct drm_device *dev, int pipe)
10825{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010826 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010827 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10828
Mario Kleiner49b14a52010-12-09 07:00:07 +010010829 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010830}
10831
10832void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10833{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010834 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010835 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10836
Mario Kleiner49b14a52010-12-09 07:00:07 +010010837 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010838}
10839
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010840/* Is 'a' after or equal to 'b'? */
10841static bool g4x_flip_count_after_eq(u32 a, u32 b)
10842{
10843 return !((a - b) & 0x80000000);
10844}
10845
10846static bool page_flip_finished(struct intel_crtc *crtc)
10847{
10848 struct drm_device *dev = crtc->base.dev;
10849 struct drm_i915_private *dev_priv = dev->dev_private;
10850
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010851 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10852 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10853 return true;
10854
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010855 /*
10856 * The relevant registers doen't exist on pre-ctg.
10857 * As the flip done interrupt doesn't trigger for mmio
10858 * flips on gmch platforms, a flip count check isn't
10859 * really needed there. But since ctg has the registers,
10860 * include it in the check anyway.
10861 */
10862 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10863 return true;
10864
10865 /*
10866 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10867 * used the same base address. In that case the mmio flip might
10868 * have completed, but the CS hasn't even executed the flip yet.
10869 *
10870 * A flip count check isn't enough as the CS might have updated
10871 * the base address just after start of vblank, but before we
10872 * managed to process the interrupt. This means we'd complete the
10873 * CS flip too soon.
10874 *
10875 * Combining both checks should get us a good enough result. It may
10876 * still happen that the CS flip has been executed, but has not
10877 * yet actually completed. But in case the base address is the same
10878 * anyway, we don't really care.
10879 */
10880 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10881 crtc->unpin_work->gtt_offset &&
10882 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10883 crtc->unpin_work->flip_count);
10884}
10885
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010886void intel_prepare_page_flip(struct drm_device *dev, int plane)
10887{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010888 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010889 struct intel_crtc *intel_crtc =
10890 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10891 unsigned long flags;
10892
Daniel Vetterf3260382014-09-15 14:55:23 +020010893
10894 /*
10895 * This is called both by irq handlers and the reset code (to complete
10896 * lost pageflips) so needs the full irqsave spinlocks.
10897 *
10898 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010899 * generate a page-flip completion irq, i.e. every modeset
10900 * is also accompanied by a spurious intel_prepare_page_flip().
10901 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010902 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010903 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010904 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010905 spin_unlock_irqrestore(&dev->event_lock, flags);
10906}
10907
Robin Schroereba905b2014-05-18 02:24:50 +020010908static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010909{
10910 /* Ensure that the work item is consistent when activating it ... */
10911 smp_wmb();
10912 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10913 /* and that it is marked active as soon as the irq could fire. */
10914 smp_wmb();
10915}
10916
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010917static int intel_gen2_queue_flip(struct drm_device *dev,
10918 struct drm_crtc *crtc,
10919 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010920 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010921 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010922 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010923{
John Harrison6258fbe2015-05-29 17:43:48 +010010924 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010925 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010926 u32 flip_mask;
10927 int ret;
10928
John Harrison5fb9de12015-05-29 17:44:07 +010010929 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010930 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010931 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010932
10933 /* Can't queue multiple flips, so wait for the previous
10934 * one to finish before executing the next.
10935 */
10936 if (intel_crtc->plane)
10937 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10938 else
10939 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010940 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10941 intel_ring_emit(ring, MI_NOOP);
10942 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10943 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10944 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010945 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010946 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010947
10948 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010949 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010950}
10951
10952static int intel_gen3_queue_flip(struct drm_device *dev,
10953 struct drm_crtc *crtc,
10954 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010955 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010956 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010957 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010958{
John Harrison6258fbe2015-05-29 17:43:48 +010010959 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010960 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010961 u32 flip_mask;
10962 int ret;
10963
John Harrison5fb9de12015-05-29 17:44:07 +010010964 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010965 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010966 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010967
10968 if (intel_crtc->plane)
10969 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10970 else
10971 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010972 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10973 intel_ring_emit(ring, MI_NOOP);
10974 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10975 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10976 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010977 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010978 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010979
Chris Wilsone7d841c2012-12-03 11:36:30 +000010980 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010981 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010982}
10983
10984static int intel_gen4_queue_flip(struct drm_device *dev,
10985 struct drm_crtc *crtc,
10986 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010987 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010988 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010989 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010990{
John Harrison6258fbe2015-05-29 17:43:48 +010010991 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010992 struct drm_i915_private *dev_priv = dev->dev_private;
10993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10994 uint32_t pf, pipesrc;
10995 int ret;
10996
John Harrison5fb9de12015-05-29 17:44:07 +010010997 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010998 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010999 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011000
11001 /* i965+ uses the linear or tiled offsets from the
11002 * Display Registers (which do not change across a page-flip)
11003 * so we need only reprogram the base address.
11004 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011005 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11006 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11007 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011008 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011009 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011010
11011 /* XXX Enabling the panel-fitter across page-flip is so far
11012 * untested on non-native modes, so ignore it for now.
11013 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11014 */
11015 pf = 0;
11016 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011017 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011018
11019 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011020 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011021}
11022
11023static int intel_gen6_queue_flip(struct drm_device *dev,
11024 struct drm_crtc *crtc,
11025 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011026 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011027 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011028 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011029{
John Harrison6258fbe2015-05-29 17:43:48 +010011030 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011031 struct drm_i915_private *dev_priv = dev->dev_private;
11032 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11033 uint32_t pf, pipesrc;
11034 int ret;
11035
John Harrison5fb9de12015-05-29 17:44:07 +010011036 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011037 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011038 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011039
Daniel Vetter6d90c952012-04-26 23:28:05 +020011040 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11041 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11042 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011043 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011044
Chris Wilson99d9acd2012-04-17 20:37:00 +010011045 /* Contrary to the suggestions in the documentation,
11046 * "Enable Panel Fitter" does not seem to be required when page
11047 * flipping with a non-native mode, and worse causes a normal
11048 * modeset to fail.
11049 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11050 */
11051 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011052 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011053 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011054
11055 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011056 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011057}
11058
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011059static int intel_gen7_queue_flip(struct drm_device *dev,
11060 struct drm_crtc *crtc,
11061 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011062 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011063 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011064 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011065{
John Harrison6258fbe2015-05-29 17:43:48 +010011066 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011067 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011068 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011069 int len, ret;
11070
Robin Schroereba905b2014-05-18 02:24:50 +020011071 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011072 case PLANE_A:
11073 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11074 break;
11075 case PLANE_B:
11076 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11077 break;
11078 case PLANE_C:
11079 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11080 break;
11081 default:
11082 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011083 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011084 }
11085
Chris Wilsonffe74d72013-08-26 20:58:12 +010011086 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011087 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011088 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011089 /*
11090 * On Gen 8, SRM is now taking an extra dword to accommodate
11091 * 48bits addresses, and we need a NOOP for the batch size to
11092 * stay even.
11093 */
11094 if (IS_GEN8(dev))
11095 len += 2;
11096 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011097
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011098 /*
11099 * BSpec MI_DISPLAY_FLIP for IVB:
11100 * "The full packet must be contained within the same cache line."
11101 *
11102 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11103 * cacheline, if we ever start emitting more commands before
11104 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11105 * then do the cacheline alignment, and finally emit the
11106 * MI_DISPLAY_FLIP.
11107 */
John Harrisonbba09b12015-05-29 17:44:06 +010011108 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011109 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011110 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011111
John Harrison5fb9de12015-05-29 17:44:07 +010011112 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011113 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011114 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011115
Chris Wilsonffe74d72013-08-26 20:58:12 +010011116 /* Unmask the flip-done completion message. Note that the bspec says that
11117 * we should do this for both the BCS and RCS, and that we must not unmask
11118 * more than one flip event at any time (or ensure that one flip message
11119 * can be sent by waiting for flip-done prior to queueing new flips).
11120 * Experimentation says that BCS works despite DERRMR masking all
11121 * flip-done completion events and that unmasking all planes at once
11122 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11123 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11124 */
11125 if (ring->id == RCS) {
11126 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11127 intel_ring_emit(ring, DERRMR);
11128 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11129 DERRMR_PIPEB_PRI_FLIP_DONE |
11130 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011131 if (IS_GEN8(dev))
11132 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11133 MI_SRM_LRM_GLOBAL_GTT);
11134 else
11135 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11136 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011137 intel_ring_emit(ring, DERRMR);
11138 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011139 if (IS_GEN8(dev)) {
11140 intel_ring_emit(ring, 0);
11141 intel_ring_emit(ring, MI_NOOP);
11142 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011143 }
11144
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011145 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011146 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011147 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011148 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011149
11150 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011151 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011152}
11153
Sourab Gupta84c33a62014-06-02 16:47:17 +053011154static bool use_mmio_flip(struct intel_engine_cs *ring,
11155 struct drm_i915_gem_object *obj)
11156{
11157 /*
11158 * This is not being used for older platforms, because
11159 * non-availability of flip done interrupt forces us to use
11160 * CS flips. Older platforms derive flip done using some clever
11161 * tricks involving the flip_pending status bits and vblank irqs.
11162 * So using MMIO flips there would disrupt this mechanism.
11163 */
11164
Chris Wilson8e09bf82014-07-08 10:40:30 +010011165 if (ring == NULL)
11166 return true;
11167
Sourab Gupta84c33a62014-06-02 16:47:17 +053011168 if (INTEL_INFO(ring->dev)->gen < 5)
11169 return false;
11170
11171 if (i915.use_mmio_flip < 0)
11172 return false;
11173 else if (i915.use_mmio_flip > 0)
11174 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011175 else if (i915.enable_execlists)
11176 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011177 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011178 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011179}
11180
Damien Lespiauff944562014-11-20 14:58:16 +000011181static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11182{
11183 struct drm_device *dev = intel_crtc->base.dev;
11184 struct drm_i915_private *dev_priv = dev->dev_private;
11185 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011186 const enum pipe pipe = intel_crtc->pipe;
11187 u32 ctl, stride;
11188
11189 ctl = I915_READ(PLANE_CTL(pipe, 0));
11190 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011191 switch (fb->modifier[0]) {
11192 case DRM_FORMAT_MOD_NONE:
11193 break;
11194 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011195 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011196 break;
11197 case I915_FORMAT_MOD_Y_TILED:
11198 ctl |= PLANE_CTL_TILED_Y;
11199 break;
11200 case I915_FORMAT_MOD_Yf_TILED:
11201 ctl |= PLANE_CTL_TILED_YF;
11202 break;
11203 default:
11204 MISSING_CASE(fb->modifier[0]);
11205 }
Damien Lespiauff944562014-11-20 14:58:16 +000011206
11207 /*
11208 * The stride is either expressed as a multiple of 64 bytes chunks for
11209 * linear buffers or in number of tiles for tiled buffers.
11210 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011211 stride = fb->pitches[0] /
11212 intel_fb_stride_alignment(dev, fb->modifier[0],
11213 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011214
11215 /*
11216 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11217 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11218 */
11219 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11220 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11221
11222 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11223 POSTING_READ(PLANE_SURF(pipe, 0));
11224}
11225
11226static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011227{
11228 struct drm_device *dev = intel_crtc->base.dev;
11229 struct drm_i915_private *dev_priv = dev->dev_private;
11230 struct intel_framebuffer *intel_fb =
11231 to_intel_framebuffer(intel_crtc->base.primary->fb);
11232 struct drm_i915_gem_object *obj = intel_fb->obj;
11233 u32 dspcntr;
11234 u32 reg;
11235
Sourab Gupta84c33a62014-06-02 16:47:17 +053011236 reg = DSPCNTR(intel_crtc->plane);
11237 dspcntr = I915_READ(reg);
11238
Damien Lespiauc5d97472014-10-25 00:11:11 +010011239 if (obj->tiling_mode != I915_TILING_NONE)
11240 dspcntr |= DISPPLANE_TILED;
11241 else
11242 dspcntr &= ~DISPPLANE_TILED;
11243
Sourab Gupta84c33a62014-06-02 16:47:17 +053011244 I915_WRITE(reg, dspcntr);
11245
11246 I915_WRITE(DSPSURF(intel_crtc->plane),
11247 intel_crtc->unpin_work->gtt_offset);
11248 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011249
Damien Lespiauff944562014-11-20 14:58:16 +000011250}
11251
11252/*
11253 * XXX: This is the temporary way to update the plane registers until we get
11254 * around to using the usual plane update functions for MMIO flips
11255 */
11256static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11257{
11258 struct drm_device *dev = intel_crtc->base.dev;
11259 bool atomic_update;
11260 u32 start_vbl_count;
11261
11262 intel_mark_page_flip_active(intel_crtc);
11263
11264 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11265
11266 if (INTEL_INFO(dev)->gen >= 9)
11267 skl_do_mmio_flip(intel_crtc);
11268 else
11269 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11270 ilk_do_mmio_flip(intel_crtc);
11271
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011272 if (atomic_update)
11273 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011274}
11275
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011276static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011277{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011278 struct intel_mmio_flip *mmio_flip =
11279 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011280
Daniel Vettereed29a52015-05-21 14:21:25 +020011281 if (mmio_flip->req)
11282 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011283 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011284 false, NULL,
11285 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011286
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011287 intel_do_mmio_flip(mmio_flip->crtc);
11288
Daniel Vettereed29a52015-05-21 14:21:25 +020011289 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011290 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011291}
11292
11293static int intel_queue_mmio_flip(struct drm_device *dev,
11294 struct drm_crtc *crtc,
11295 struct drm_framebuffer *fb,
11296 struct drm_i915_gem_object *obj,
11297 struct intel_engine_cs *ring,
11298 uint32_t flags)
11299{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011300 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011301
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011302 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11303 if (mmio_flip == NULL)
11304 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011305
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011306 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011307 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011308 mmio_flip->crtc = to_intel_crtc(crtc);
11309
11310 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11311 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011312
Sourab Gupta84c33a62014-06-02 16:47:17 +053011313 return 0;
11314}
11315
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011316static int intel_default_queue_flip(struct drm_device *dev,
11317 struct drm_crtc *crtc,
11318 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011319 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011320 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011321 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011322{
11323 return -ENODEV;
11324}
11325
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011326static bool __intel_pageflip_stall_check(struct drm_device *dev,
11327 struct drm_crtc *crtc)
11328{
11329 struct drm_i915_private *dev_priv = dev->dev_private;
11330 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11331 struct intel_unpin_work *work = intel_crtc->unpin_work;
11332 u32 addr;
11333
11334 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11335 return true;
11336
11337 if (!work->enable_stall_check)
11338 return false;
11339
11340 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011341 if (work->flip_queued_req &&
11342 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011343 return false;
11344
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011345 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011346 }
11347
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011348 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011349 return false;
11350
11351 /* Potential stall - if we see that the flip has happened,
11352 * assume a missed interrupt. */
11353 if (INTEL_INFO(dev)->gen >= 4)
11354 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11355 else
11356 addr = I915_READ(DSPADDR(intel_crtc->plane));
11357
11358 /* There is a potential issue here with a false positive after a flip
11359 * to the same address. We could address this by checking for a
11360 * non-incrementing frame counter.
11361 */
11362 return addr == work->gtt_offset;
11363}
11364
11365void intel_check_page_flip(struct drm_device *dev, int pipe)
11366{
11367 struct drm_i915_private *dev_priv = dev->dev_private;
11368 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11369 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011370 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011371
Dave Gordon6c51d462015-03-06 15:34:26 +000011372 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011373
11374 if (crtc == NULL)
11375 return;
11376
Daniel Vetterf3260382014-09-15 14:55:23 +020011377 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011378 work = intel_crtc->unpin_work;
11379 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011380 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011381 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011382 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011383 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011384 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011385 if (work != NULL &&
11386 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11387 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011388 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011389}
11390
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011391static int intel_crtc_page_flip(struct drm_crtc *crtc,
11392 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011393 struct drm_pending_vblank_event *event,
11394 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011395{
11396 struct drm_device *dev = crtc->dev;
11397 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011398 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011399 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011401 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011402 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011403 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011404 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011405 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011406 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011407 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011408
Matt Roper2ff8fde2014-07-08 07:50:07 -070011409 /*
11410 * drm_mode_page_flip_ioctl() should already catch this, but double
11411 * check to be safe. In the future we may enable pageflipping from
11412 * a disabled primary plane.
11413 */
11414 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11415 return -EBUSY;
11416
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011417 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011418 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011419 return -EINVAL;
11420
11421 /*
11422 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11423 * Note that pitch changes could also affect these register.
11424 */
11425 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011426 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11427 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011428 return -EINVAL;
11429
Chris Wilsonf900db42014-02-20 09:26:13 +000011430 if (i915_terminally_wedged(&dev_priv->gpu_error))
11431 goto out_hang;
11432
Daniel Vetterb14c5672013-09-19 12:18:32 +020011433 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011434 if (work == NULL)
11435 return -ENOMEM;
11436
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011437 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011438 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011439 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011440 INIT_WORK(&work->work, intel_unpin_work_fn);
11441
Daniel Vetter87b6b102014-05-15 15:33:46 +020011442 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011443 if (ret)
11444 goto free_work;
11445
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011446 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011447 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011448 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011449 /* Before declaring the flip queue wedged, check if
11450 * the hardware completed the operation behind our backs.
11451 */
11452 if (__intel_pageflip_stall_check(dev, crtc)) {
11453 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11454 page_flip_completed(intel_crtc);
11455 } else {
11456 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011457 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011458
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011459 drm_crtc_vblank_put(crtc);
11460 kfree(work);
11461 return -EBUSY;
11462 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011463 }
11464 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011465 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011466
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011467 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11468 flush_workqueue(dev_priv->wq);
11469
Jesse Barnes75dfca82010-02-10 15:09:44 -080011470 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011471 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011472 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011473
Matt Roperf4510a22014-04-01 15:22:40 -070011474 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011475 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011476
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011477 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011478
Chris Wilson89ed88b2015-02-16 14:31:49 +000011479 ret = i915_mutex_lock_interruptible(dev);
11480 if (ret)
11481 goto cleanup;
11482
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011483 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011484 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011485
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011486 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011487 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011488
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011489 if (IS_VALLEYVIEW(dev)) {
11490 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011491 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011492 /* vlv: DISPLAY_FLIP fails to change tiling */
11493 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011494 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011495 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011496 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011497 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011498 if (ring == NULL || ring->id != RCS)
11499 ring = &dev_priv->ring[BCS];
11500 } else {
11501 ring = &dev_priv->ring[RCS];
11502 }
11503
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011504 mmio_flip = use_mmio_flip(ring, obj);
11505
11506 /* When using CS flips, we want to emit semaphores between rings.
11507 * However, when using mmio flips we will create a task to do the
11508 * synchronisation, so all we want here is to pin the framebuffer
11509 * into the display plane and skip any waits.
11510 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011511 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011512 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011513 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011514 if (ret)
11515 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011516
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011517 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11518 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011519
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011520 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011521 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11522 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011523 if (ret)
11524 goto cleanup_unpin;
11525
John Harrisonf06cc1b2014-11-24 18:49:37 +000011526 i915_gem_request_assign(&work->flip_queued_req,
11527 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011528 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011529 if (!request) {
11530 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11531 if (ret)
11532 goto cleanup_unpin;
11533 }
11534
11535 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011536 page_flip_flags);
11537 if (ret)
11538 goto cleanup_unpin;
11539
John Harrison6258fbe2015-05-29 17:43:48 +010011540 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011541 }
11542
John Harrison91af1272015-06-18 13:14:56 +010011543 if (request)
John Harrison75289872015-05-29 17:43:49 +010011544 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011545
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011546 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011547 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011548
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011549 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011550 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011551 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011552
Paulo Zanoni7733b492015-07-07 15:26:04 -030011553 intel_fbc_disable(dev_priv);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011554 intel_frontbuffer_flip_prepare(dev,
11555 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011556
Jesse Barnese5510fa2010-07-01 16:48:37 -070011557 trace_i915_flip_request(intel_crtc->plane, obj);
11558
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011559 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011560
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011561cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011562 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011563cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011564 if (request)
11565 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011566 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011567 mutex_unlock(&dev->struct_mutex);
11568cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011569 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011570 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011571
Chris Wilson89ed88b2015-02-16 14:31:49 +000011572 drm_gem_object_unreference_unlocked(&obj->base);
11573 drm_framebuffer_unreference(work->old_fb);
11574
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011575 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011576 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011577 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011578
Daniel Vetter87b6b102014-05-15 15:33:46 +020011579 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011580free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011581 kfree(work);
11582
Chris Wilsonf900db42014-02-20 09:26:13 +000011583 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011584 struct drm_atomic_state *state;
11585 struct drm_plane_state *plane_state;
11586
Chris Wilsonf900db42014-02-20 09:26:13 +000011587out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011588 state = drm_atomic_state_alloc(dev);
11589 if (!state)
11590 return -ENOMEM;
11591 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11592
11593retry:
11594 plane_state = drm_atomic_get_plane_state(state, primary);
11595 ret = PTR_ERR_OR_ZERO(plane_state);
11596 if (!ret) {
11597 drm_atomic_set_fb_for_plane(plane_state, fb);
11598
11599 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11600 if (!ret)
11601 ret = drm_atomic_commit(state);
11602 }
11603
11604 if (ret == -EDEADLK) {
11605 drm_modeset_backoff(state->acquire_ctx);
11606 drm_atomic_state_clear(state);
11607 goto retry;
11608 }
11609
11610 if (ret)
11611 drm_atomic_state_free(state);
11612
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011613 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011614 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011615 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011616 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011617 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011618 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011619 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011620}
11621
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011622
11623/**
11624 * intel_wm_need_update - Check whether watermarks need updating
11625 * @plane: drm plane
11626 * @state: new plane state
11627 *
11628 * Check current plane state versus the new one to determine whether
11629 * watermarks need to be recalculated.
11630 *
11631 * Returns true or false.
11632 */
11633static bool intel_wm_need_update(struct drm_plane *plane,
11634 struct drm_plane_state *state)
11635{
11636 /* Update watermarks on tiling changes. */
11637 if (!plane->state->fb || !state->fb ||
11638 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11639 plane->state->rotation != state->rotation)
11640 return true;
11641
11642 if (plane->state->crtc_w != state->crtc_w)
11643 return true;
11644
11645 return false;
11646}
11647
11648int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11649 struct drm_plane_state *plane_state)
11650{
11651 struct drm_crtc *crtc = crtc_state->crtc;
11652 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11653 struct drm_plane *plane = plane_state->plane;
11654 struct drm_device *dev = crtc->dev;
11655 struct drm_i915_private *dev_priv = dev->dev_private;
11656 struct intel_plane_state *old_plane_state =
11657 to_intel_plane_state(plane->state);
11658 int idx = intel_crtc->base.base.id, ret;
11659 int i = drm_plane_index(plane);
11660 bool mode_changed = needs_modeset(crtc_state);
11661 bool was_crtc_enabled = crtc->state->active;
11662 bool is_crtc_enabled = crtc_state->active;
11663
11664 bool turn_off, turn_on, visible, was_visible;
11665 struct drm_framebuffer *fb = plane_state->fb;
11666
11667 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11668 plane->type != DRM_PLANE_TYPE_CURSOR) {
11669 ret = skl_update_scaler_plane(
11670 to_intel_crtc_state(crtc_state),
11671 to_intel_plane_state(plane_state));
11672 if (ret)
11673 return ret;
11674 }
11675
11676 /*
11677 * Disabling a plane is always okay; we just need to update
11678 * fb tracking in a special way since cleanup_fb() won't
11679 * get called by the plane helpers.
11680 */
11681 if (old_plane_state->base.fb && !fb)
11682 intel_crtc->atomic.disabled_planes |= 1 << i;
11683
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011684 was_visible = old_plane_state->visible;
11685 visible = to_intel_plane_state(plane_state)->visible;
11686
11687 if (!was_crtc_enabled && WARN_ON(was_visible))
11688 was_visible = false;
11689
11690 if (!is_crtc_enabled && WARN_ON(visible))
11691 visible = false;
11692
11693 if (!was_visible && !visible)
11694 return 0;
11695
11696 turn_off = was_visible && (!visible || mode_changed);
11697 turn_on = visible && (!was_visible || mode_changed);
11698
11699 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11700 plane->base.id, fb ? fb->base.id : -1);
11701
11702 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11703 plane->base.id, was_visible, visible,
11704 turn_off, turn_on, mode_changed);
11705
Ville Syrjälä852eb002015-06-24 22:00:07 +030011706 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011707 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011708 /* must disable cxsr around plane enable/disable */
11709 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11710 intel_crtc->atomic.disable_cxsr = true;
11711 /* to potentially re-enable cxsr */
11712 intel_crtc->atomic.wait_vblank = true;
11713 intel_crtc->atomic.update_wm_post = true;
11714 }
11715 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011716 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011717 /* must disable cxsr around plane enable/disable */
11718 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11719 if (is_crtc_enabled)
11720 intel_crtc->atomic.wait_vblank = true;
11721 intel_crtc->atomic.disable_cxsr = true;
11722 }
11723 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011724 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011725 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011726
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011727 if (visible)
11728 intel_crtc->atomic.fb_bits |=
11729 to_intel_plane(plane)->frontbuffer_bit;
11730
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011731 switch (plane->type) {
11732 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011733 intel_crtc->atomic.wait_for_flips = true;
11734 intel_crtc->atomic.pre_disable_primary = turn_off;
11735 intel_crtc->atomic.post_enable_primary = turn_on;
11736
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011737 if (turn_off) {
11738 /*
11739 * FIXME: Actually if we will still have any other
11740 * plane enabled on the pipe we could let IPS enabled
11741 * still, but for now lets consider that when we make
11742 * primary invisible by setting DSPCNTR to 0 on
11743 * update_primary_plane function IPS needs to be
11744 * disable.
11745 */
11746 intel_crtc->atomic.disable_ips = true;
11747
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011748 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011749 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011750
11751 /*
11752 * FBC does not work on some platforms for rotated
11753 * planes, so disable it when rotation is not 0 and
11754 * update it when rotation is set back to 0.
11755 *
11756 * FIXME: This is redundant with the fbc update done in
11757 * the primary plane enable function except that that
11758 * one is done too late. We eventually need to unify
11759 * this.
11760 */
11761
11762 if (visible &&
11763 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11764 dev_priv->fbc.crtc == intel_crtc &&
11765 plane_state->rotation != BIT(DRM_ROTATE_0))
11766 intel_crtc->atomic.disable_fbc = true;
11767
11768 /*
11769 * BDW signals flip done immediately if the plane
11770 * is disabled, even if the plane enable is already
11771 * armed to occur at the next vblank :(
11772 */
11773 if (turn_on && IS_BROADWELL(dev))
11774 intel_crtc->atomic.wait_vblank = true;
11775
11776 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11777 break;
11778 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011779 break;
11780 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011781 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011782 intel_crtc->atomic.wait_vblank = true;
11783 intel_crtc->atomic.update_sprite_watermarks |=
11784 1 << i;
11785 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011786 }
11787 return 0;
11788}
11789
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011790static bool encoders_cloneable(const struct intel_encoder *a,
11791 const struct intel_encoder *b)
11792{
11793 /* masks could be asymmetric, so check both ways */
11794 return a == b || (a->cloneable & (1 << b->type) &&
11795 b->cloneable & (1 << a->type));
11796}
11797
11798static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11799 struct intel_crtc *crtc,
11800 struct intel_encoder *encoder)
11801{
11802 struct intel_encoder *source_encoder;
11803 struct drm_connector *connector;
11804 struct drm_connector_state *connector_state;
11805 int i;
11806
11807 for_each_connector_in_state(state, connector, connector_state, i) {
11808 if (connector_state->crtc != &crtc->base)
11809 continue;
11810
11811 source_encoder =
11812 to_intel_encoder(connector_state->best_encoder);
11813 if (!encoders_cloneable(encoder, source_encoder))
11814 return false;
11815 }
11816
11817 return true;
11818}
11819
11820static bool check_encoder_cloning(struct drm_atomic_state *state,
11821 struct intel_crtc *crtc)
11822{
11823 struct intel_encoder *encoder;
11824 struct drm_connector *connector;
11825 struct drm_connector_state *connector_state;
11826 int i;
11827
11828 for_each_connector_in_state(state, connector, connector_state, i) {
11829 if (connector_state->crtc != &crtc->base)
11830 continue;
11831
11832 encoder = to_intel_encoder(connector_state->best_encoder);
11833 if (!check_single_encoder_cloning(state, crtc, encoder))
11834 return false;
11835 }
11836
11837 return true;
11838}
11839
11840static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11841 struct drm_crtc_state *crtc_state)
11842{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011843 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011844 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011845 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011846 struct intel_crtc_state *pipe_config =
11847 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011848 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011849 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011850 bool mode_changed = needs_modeset(crtc_state);
11851
11852 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11853 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11854 return -EINVAL;
11855 }
11856
11857 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11858 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11859 idx, crtc->state->active, intel_crtc->active);
11860
Ville Syrjälä852eb002015-06-24 22:00:07 +030011861 if (mode_changed && !crtc_state->active)
11862 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011863
Maarten Lankhorstad421372015-06-15 12:33:42 +020011864 if (mode_changed && crtc_state->enable &&
11865 dev_priv->display.crtc_compute_clock &&
11866 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11867 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11868 pipe_config);
11869 if (ret)
11870 return ret;
11871 }
11872
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011873 ret = 0;
11874 if (INTEL_INFO(dev)->gen >= 9) {
11875 if (mode_changed)
11876 ret = skl_update_scaler_crtc(pipe_config);
11877
11878 if (!ret)
11879 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11880 pipe_config);
11881 }
11882
11883 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011884}
11885
Jani Nikula65b38e02015-04-13 11:26:56 +030011886static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011887 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11888 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011889 .atomic_begin = intel_begin_crtc_commit,
11890 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011891 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011892};
11893
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011894static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11895{
11896 struct intel_connector *connector;
11897
11898 for_each_intel_connector(dev, connector) {
11899 if (connector->base.encoder) {
11900 connector->base.state->best_encoder =
11901 connector->base.encoder;
11902 connector->base.state->crtc =
11903 connector->base.encoder->crtc;
11904 } else {
11905 connector->base.state->best_encoder = NULL;
11906 connector->base.state->crtc = NULL;
11907 }
11908 }
11909}
11910
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011911static void
Robin Schroereba905b2014-05-18 02:24:50 +020011912connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011913 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011914{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011915 int bpp = pipe_config->pipe_bpp;
11916
11917 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11918 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011919 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011920
11921 /* Don't use an invalid EDID bpc value */
11922 if (connector->base.display_info.bpc &&
11923 connector->base.display_info.bpc * 3 < bpp) {
11924 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11925 bpp, connector->base.display_info.bpc*3);
11926 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11927 }
11928
11929 /* Clamp bpp to 8 on screens without EDID 1.4 */
11930 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11931 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11932 bpp);
11933 pipe_config->pipe_bpp = 24;
11934 }
11935}
11936
11937static int
11938compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011939 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011940{
11941 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011942 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011943 struct drm_connector *connector;
11944 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011945 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011946
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011947 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011948 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011949 else if (INTEL_INFO(dev)->gen >= 5)
11950 bpp = 12*3;
11951 else
11952 bpp = 8*3;
11953
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011954
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011955 pipe_config->pipe_bpp = bpp;
11956
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011957 state = pipe_config->base.state;
11958
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011959 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011960 for_each_connector_in_state(state, connector, connector_state, i) {
11961 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011962 continue;
11963
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011964 connected_sink_compute_bpp(to_intel_connector(connector),
11965 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011966 }
11967
11968 return bpp;
11969}
11970
Daniel Vetter644db712013-09-19 14:53:58 +020011971static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11972{
11973 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11974 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011975 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011976 mode->crtc_hdisplay, mode->crtc_hsync_start,
11977 mode->crtc_hsync_end, mode->crtc_htotal,
11978 mode->crtc_vdisplay, mode->crtc_vsync_start,
11979 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11980}
11981
Daniel Vetterc0b03412013-05-28 12:05:54 +020011982static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011983 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011984 const char *context)
11985{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011986 struct drm_device *dev = crtc->base.dev;
11987 struct drm_plane *plane;
11988 struct intel_plane *intel_plane;
11989 struct intel_plane_state *state;
11990 struct drm_framebuffer *fb;
11991
11992 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11993 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011994
11995 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11996 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11997 pipe_config->pipe_bpp, pipe_config->dither);
11998 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11999 pipe_config->has_pch_encoder,
12000 pipe_config->fdi_lanes,
12001 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12002 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12003 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012004 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12005 pipe_config->has_dp_encoder,
12006 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12007 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12008 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012009
12010 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
12011 pipe_config->has_dp_encoder,
12012 pipe_config->dp_m2_n2.gmch_m,
12013 pipe_config->dp_m2_n2.gmch_n,
12014 pipe_config->dp_m2_n2.link_m,
12015 pipe_config->dp_m2_n2.link_n,
12016 pipe_config->dp_m2_n2.tu);
12017
Daniel Vetter55072d12014-11-20 16:10:28 +010012018 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12019 pipe_config->has_audio,
12020 pipe_config->has_infoframe);
12021
Daniel Vetterc0b03412013-05-28 12:05:54 +020012022 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012023 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012024 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012025 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12026 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012027 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012028 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12029 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012030 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12031 crtc->num_scalers,
12032 pipe_config->scaler_state.scaler_users,
12033 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012034 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12035 pipe_config->gmch_pfit.control,
12036 pipe_config->gmch_pfit.pgm_ratios,
12037 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012038 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012039 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012040 pipe_config->pch_pfit.size,
12041 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012042 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012043 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012044
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012045 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012046 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012047 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012048 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012049 pipe_config->ddi_pll_sel,
12050 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012051 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012052 pipe_config->dpll_hw_state.pll0,
12053 pipe_config->dpll_hw_state.pll1,
12054 pipe_config->dpll_hw_state.pll2,
12055 pipe_config->dpll_hw_state.pll3,
12056 pipe_config->dpll_hw_state.pll6,
12057 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012058 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012059 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012060 pipe_config->dpll_hw_state.pcsdw12);
12061 } else if (IS_SKYLAKE(dev)) {
12062 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12063 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12064 pipe_config->ddi_pll_sel,
12065 pipe_config->dpll_hw_state.ctrl1,
12066 pipe_config->dpll_hw_state.cfgcr1,
12067 pipe_config->dpll_hw_state.cfgcr2);
12068 } else if (HAS_DDI(dev)) {
12069 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12070 pipe_config->ddi_pll_sel,
12071 pipe_config->dpll_hw_state.wrpll);
12072 } else {
12073 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12074 "fp0: 0x%x, fp1: 0x%x\n",
12075 pipe_config->dpll_hw_state.dpll,
12076 pipe_config->dpll_hw_state.dpll_md,
12077 pipe_config->dpll_hw_state.fp0,
12078 pipe_config->dpll_hw_state.fp1);
12079 }
12080
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012081 DRM_DEBUG_KMS("planes on this crtc\n");
12082 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12083 intel_plane = to_intel_plane(plane);
12084 if (intel_plane->pipe != crtc->pipe)
12085 continue;
12086
12087 state = to_intel_plane_state(plane->state);
12088 fb = state->base.fb;
12089 if (!fb) {
12090 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12091 "disabled, scaler_id = %d\n",
12092 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12093 plane->base.id, intel_plane->pipe,
12094 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12095 drm_plane_index(plane), state->scaler_id);
12096 continue;
12097 }
12098
12099 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12100 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12101 plane->base.id, intel_plane->pipe,
12102 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12103 drm_plane_index(plane));
12104 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12105 fb->base.id, fb->width, fb->height, fb->pixel_format);
12106 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12107 state->scaler_id,
12108 state->src.x1 >> 16, state->src.y1 >> 16,
12109 drm_rect_width(&state->src) >> 16,
12110 drm_rect_height(&state->src) >> 16,
12111 state->dst.x1, state->dst.y1,
12112 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12113 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012114}
12115
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012116static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012117{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012118 struct drm_device *dev = state->dev;
12119 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012120 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012121 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012122 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012123 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012124
12125 /*
12126 * Walk the connector list instead of the encoder
12127 * list to detect the problem on ddi platforms
12128 * where there's just one encoder per digital port.
12129 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012130 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012131 if (!connector_state->best_encoder)
12132 continue;
12133
12134 encoder = to_intel_encoder(connector_state->best_encoder);
12135
12136 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012137
12138 switch (encoder->type) {
12139 unsigned int port_mask;
12140 case INTEL_OUTPUT_UNKNOWN:
12141 if (WARN_ON(!HAS_DDI(dev)))
12142 break;
12143 case INTEL_OUTPUT_DISPLAYPORT:
12144 case INTEL_OUTPUT_HDMI:
12145 case INTEL_OUTPUT_EDP:
12146 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12147
12148 /* the same port mustn't appear more than once */
12149 if (used_ports & port_mask)
12150 return false;
12151
12152 used_ports |= port_mask;
12153 default:
12154 break;
12155 }
12156 }
12157
12158 return true;
12159}
12160
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012161static void
12162clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12163{
12164 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012165 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012166 struct intel_dpll_hw_state dpll_hw_state;
12167 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012168 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012169
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012170 /* FIXME: before the switch to atomic started, a new pipe_config was
12171 * kzalloc'd. Code that depends on any field being zero should be
12172 * fixed, so that the crtc_state can be safely duplicated. For now,
12173 * only fields that are know to not cause problems are preserved. */
12174
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012175 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012176 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012177 shared_dpll = crtc_state->shared_dpll;
12178 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012179 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012180
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012181 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012182
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012183 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012184 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012185 crtc_state->shared_dpll = shared_dpll;
12186 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012187 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012188}
12189
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012190static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012191intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012192 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012193{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012194 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012195 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012196 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012197 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012198 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012199 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012200 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012201
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012202 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012203
Daniel Vettere143a212013-07-04 12:01:15 +020012204 pipe_config->cpu_transcoder =
12205 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012206
Imre Deak2960bc92013-07-30 13:36:32 +030012207 /*
12208 * Sanitize sync polarity flags based on requested ones. If neither
12209 * positive or negative polarity is requested, treat this as meaning
12210 * negative polarity.
12211 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012212 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012213 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012214 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012215
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012216 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012217 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012218 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012219
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012220 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12221 * plane pixel format and any sink constraints into account. Returns the
12222 * source plane bpp so that dithering can be selected on mismatches
12223 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012224 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12225 pipe_config);
12226 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012227 goto fail;
12228
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012229 /*
12230 * Determine the real pipe dimensions. Note that stereo modes can
12231 * increase the actual pipe size due to the frame doubling and
12232 * insertion of additional space for blanks between the frame. This
12233 * is stored in the crtc timings. We use the requested mode to do this
12234 * computation to clearly distinguish it from the adjusted mode, which
12235 * can be changed by the connectors in the below retry loop.
12236 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012237 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012238 &pipe_config->pipe_src_w,
12239 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012240
Daniel Vettere29c22c2013-02-21 00:00:16 +010012241encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012242 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012243 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012244 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012245
Daniel Vetter135c81b2013-07-21 21:37:09 +020012246 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012247 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12248 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012249
Daniel Vetter7758a112012-07-08 19:40:39 +020012250 /* Pass our mode to the connectors and the CRTC to give them a chance to
12251 * adjust it according to limitations or connector properties, and also
12252 * a chance to reject the mode entirely.
12253 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012254 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012255 if (connector_state->crtc != crtc)
12256 continue;
12257
12258 encoder = to_intel_encoder(connector_state->best_encoder);
12259
Daniel Vetterefea6e82013-07-21 21:36:59 +020012260 if (!(encoder->compute_config(encoder, pipe_config))) {
12261 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012262 goto fail;
12263 }
12264 }
12265
Daniel Vetterff9a6752013-06-01 17:16:21 +020012266 /* Set default port clock if not overwritten by the encoder. Needs to be
12267 * done afterwards in case the encoder adjusts the mode. */
12268 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012269 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012270 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012271
Daniel Vettera43f6e02013-06-07 23:10:32 +020012272 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012273 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012274 DRM_DEBUG_KMS("CRTC fixup failed\n");
12275 goto fail;
12276 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012277
12278 if (ret == RETRY) {
12279 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12280 ret = -EINVAL;
12281 goto fail;
12282 }
12283
12284 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12285 retry = false;
12286 goto encoder_retry;
12287 }
12288
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012289 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012290 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012291 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012292
Daniel Vetter7758a112012-07-08 19:40:39 +020012293fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012294 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012295}
12296
Daniel Vetterea9d7582012-07-10 10:42:52 +020012297static bool intel_crtc_in_use(struct drm_crtc *crtc)
12298{
12299 struct drm_encoder *encoder;
12300 struct drm_device *dev = crtc->dev;
12301
12302 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12303 if (encoder->crtc == crtc)
12304 return true;
12305
12306 return false;
12307}
12308
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012309static void
12310intel_modeset_update_state(struct drm_atomic_state *state)
12311{
12312 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012313 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012314 struct drm_crtc *crtc;
12315 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012316 struct drm_connector *connector;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012317 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012318
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012319 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012320
Damien Lespiaub2784e12014-08-05 11:29:37 +010012321 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012322 if (!intel_encoder->base.crtc)
12323 continue;
12324
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012325 crtc = intel_encoder->base.crtc;
12326 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12327 if (!crtc_state || !needs_modeset(crtc->state))
12328 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012329
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012330 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012331 }
12332
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012333 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012334
Ville Syrjälä76688512014-01-10 11:28:06 +020012335 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012336 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012337 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012338
12339 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012340
12341 /* Update hwmode for vblank functions */
12342 if (crtc->state->active)
12343 crtc->hwmode = crtc->state->adjusted_mode;
12344 else
12345 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012346 }
12347
12348 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12349 if (!connector->encoder || !connector->encoder->crtc)
12350 continue;
12351
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012352 crtc = connector->encoder->crtc;
12353 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12354 if (!crtc_state || !needs_modeset(crtc->state))
12355 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012356
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012357 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012358 struct drm_property *dpms_property =
12359 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012360
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012361 connector->dpms = DRM_MODE_DPMS_ON;
12362 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012363
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012364 intel_encoder = to_intel_encoder(connector->encoder);
12365 intel_encoder->connectors_active = true;
12366 } else
12367 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012368 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012369}
12370
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012371static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012372{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012373 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012374
12375 if (clock1 == clock2)
12376 return true;
12377
12378 if (!clock1 || !clock2)
12379 return false;
12380
12381 diff = abs(clock1 - clock2);
12382
12383 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12384 return true;
12385
12386 return false;
12387}
12388
Daniel Vetter25c5b262012-07-08 22:08:04 +020012389#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12390 list_for_each_entry((intel_crtc), \
12391 &(dev)->mode_config.crtc_list, \
12392 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012393 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012394
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012395
12396static bool
12397intel_compare_m_n(unsigned int m, unsigned int n,
12398 unsigned int m2, unsigned int n2,
12399 bool exact)
12400{
12401 if (m == m2 && n == n2)
12402 return true;
12403
12404 if (exact || !m || !n || !m2 || !n2)
12405 return false;
12406
12407 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12408
12409 if (m > m2) {
12410 while (m > m2) {
12411 m2 <<= 1;
12412 n2 <<= 1;
12413 }
12414 } else if (m < m2) {
12415 while (m < m2) {
12416 m <<= 1;
12417 n <<= 1;
12418 }
12419 }
12420
12421 return m == m2 && n == n2;
12422}
12423
12424static bool
12425intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12426 struct intel_link_m_n *m2_n2,
12427 bool adjust)
12428{
12429 if (m_n->tu == m2_n2->tu &&
12430 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12431 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12432 intel_compare_m_n(m_n->link_m, m_n->link_n,
12433 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12434 if (adjust)
12435 *m2_n2 = *m_n;
12436
12437 return true;
12438 }
12439
12440 return false;
12441}
12442
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012443static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012444intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012445 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012446 struct intel_crtc_state *pipe_config,
12447 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012448{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012449 bool ret = true;
12450
12451#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12452 do { \
12453 if (!adjust) \
12454 DRM_ERROR(fmt, ##__VA_ARGS__); \
12455 else \
12456 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12457 } while (0)
12458
Daniel Vetter66e985c2013-06-05 13:34:20 +020012459#define PIPE_CONF_CHECK_X(name) \
12460 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012461 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012462 "(expected 0x%08x, found 0x%08x)\n", \
12463 current_config->name, \
12464 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012465 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012466 }
12467
Daniel Vetter08a24032013-04-19 11:25:34 +020012468#define PIPE_CONF_CHECK_I(name) \
12469 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012470 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012471 "(expected %i, found %i)\n", \
12472 current_config->name, \
12473 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012474 ret = false; \
12475 }
12476
12477#define PIPE_CONF_CHECK_M_N(name) \
12478 if (!intel_compare_link_m_n(&current_config->name, \
12479 &pipe_config->name,\
12480 adjust)) { \
12481 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12482 "(expected tu %i gmch %i/%i link %i/%i, " \
12483 "found tu %i, gmch %i/%i link %i/%i)\n", \
12484 current_config->name.tu, \
12485 current_config->name.gmch_m, \
12486 current_config->name.gmch_n, \
12487 current_config->name.link_m, \
12488 current_config->name.link_n, \
12489 pipe_config->name.tu, \
12490 pipe_config->name.gmch_m, \
12491 pipe_config->name.gmch_n, \
12492 pipe_config->name.link_m, \
12493 pipe_config->name.link_n); \
12494 ret = false; \
12495 }
12496
12497#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12498 if (!intel_compare_link_m_n(&current_config->name, \
12499 &pipe_config->name, adjust) && \
12500 !intel_compare_link_m_n(&current_config->alt_name, \
12501 &pipe_config->name, adjust)) { \
12502 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12503 "(expected tu %i gmch %i/%i link %i/%i, " \
12504 "or tu %i gmch %i/%i link %i/%i, " \
12505 "found tu %i, gmch %i/%i link %i/%i)\n", \
12506 current_config->name.tu, \
12507 current_config->name.gmch_m, \
12508 current_config->name.gmch_n, \
12509 current_config->name.link_m, \
12510 current_config->name.link_n, \
12511 current_config->alt_name.tu, \
12512 current_config->alt_name.gmch_m, \
12513 current_config->alt_name.gmch_n, \
12514 current_config->alt_name.link_m, \
12515 current_config->alt_name.link_n, \
12516 pipe_config->name.tu, \
12517 pipe_config->name.gmch_m, \
12518 pipe_config->name.gmch_n, \
12519 pipe_config->name.link_m, \
12520 pipe_config->name.link_n); \
12521 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012522 }
12523
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012524/* This is required for BDW+ where there is only one set of registers for
12525 * switching between high and low RR.
12526 * This macro can be used whenever a comparison has to be made between one
12527 * hw state and multiple sw state variables.
12528 */
12529#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12530 if ((current_config->name != pipe_config->name) && \
12531 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012532 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012533 "(expected %i or %i, found %i)\n", \
12534 current_config->name, \
12535 current_config->alt_name, \
12536 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012537 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012538 }
12539
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012540#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12541 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012542 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012543 "(expected %i, found %i)\n", \
12544 current_config->name & (mask), \
12545 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012546 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012547 }
12548
Ville Syrjälä5e550652013-09-06 23:29:07 +030012549#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12550 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012551 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012552 "(expected %i, found %i)\n", \
12553 current_config->name, \
12554 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012555 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012556 }
12557
Daniel Vetterbb760062013-06-06 14:55:52 +020012558#define PIPE_CONF_QUIRK(quirk) \
12559 ((current_config->quirks | pipe_config->quirks) & (quirk))
12560
Daniel Vettereccb1402013-05-22 00:50:22 +020012561 PIPE_CONF_CHECK_I(cpu_transcoder);
12562
Daniel Vetter08a24032013-04-19 11:25:34 +020012563 PIPE_CONF_CHECK_I(has_pch_encoder);
12564 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012565 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012566
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012567 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012568
12569 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012570 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012571
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012572 PIPE_CONF_CHECK_I(has_drrs);
12573 if (current_config->has_drrs)
12574 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12575 } else
12576 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012577
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012578 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12579 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12580 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12581 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12582 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12583 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012584
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012585 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12586 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12587 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12588 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12589 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12590 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012591
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012592 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012593 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012594 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12595 IS_VALLEYVIEW(dev))
12596 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012597 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012598
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012599 PIPE_CONF_CHECK_I(has_audio);
12600
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012601 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012602 DRM_MODE_FLAG_INTERLACE);
12603
Daniel Vetterbb760062013-06-06 14:55:52 +020012604 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012605 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012606 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012607 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012608 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012609 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012610 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012611 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012612 DRM_MODE_FLAG_NVSYNC);
12613 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012614
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012615 PIPE_CONF_CHECK_I(pipe_src_w);
12616 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012617
Daniel Vetter99535992014-04-13 12:00:33 +020012618 /*
12619 * FIXME: BIOS likes to set up a cloned config with lvds+external
12620 * screen. Since we don't yet re-compute the pipe config when moving
12621 * just the lvds port away to another pipe the sw tracking won't match.
12622 *
12623 * Proper atomic modesets with recomputed global state will fix this.
12624 * Until then just don't check gmch state for inherited modes.
12625 */
12626 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12627 PIPE_CONF_CHECK_I(gmch_pfit.control);
12628 /* pfit ratios are autocomputed by the hw on gen4+ */
12629 if (INTEL_INFO(dev)->gen < 4)
12630 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12631 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12632 }
12633
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012634 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12635 if (current_config->pch_pfit.enabled) {
12636 PIPE_CONF_CHECK_I(pch_pfit.pos);
12637 PIPE_CONF_CHECK_I(pch_pfit.size);
12638 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012639
Chandra Kondurua1b22782015-04-07 15:28:45 -070012640 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12641
Jesse Barnese59150d2014-01-07 13:30:45 -080012642 /* BDW+ don't expose a synchronous way to read the state */
12643 if (IS_HASWELL(dev))
12644 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012645
Ville Syrjälä282740f2013-09-04 18:30:03 +030012646 PIPE_CONF_CHECK_I(double_wide);
12647
Daniel Vetter26804af2014-06-25 22:01:55 +030012648 PIPE_CONF_CHECK_X(ddi_pll_sel);
12649
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012650 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012651 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012652 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012653 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12654 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012655 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012656 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12657 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12658 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012659
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012660 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12661 PIPE_CONF_CHECK_I(pipe_bpp);
12662
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012663 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012664 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012665
Daniel Vetter66e985c2013-06-05 13:34:20 +020012666#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012667#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012668#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012669#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012670#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012671#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012672#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012673
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012674 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012675}
12676
Damien Lespiau08db6652014-11-04 17:06:52 +000012677static void check_wm_state(struct drm_device *dev)
12678{
12679 struct drm_i915_private *dev_priv = dev->dev_private;
12680 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12681 struct intel_crtc *intel_crtc;
12682 int plane;
12683
12684 if (INTEL_INFO(dev)->gen < 9)
12685 return;
12686
12687 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12688 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12689
12690 for_each_intel_crtc(dev, intel_crtc) {
12691 struct skl_ddb_entry *hw_entry, *sw_entry;
12692 const enum pipe pipe = intel_crtc->pipe;
12693
12694 if (!intel_crtc->active)
12695 continue;
12696
12697 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012698 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012699 hw_entry = &hw_ddb.plane[pipe][plane];
12700 sw_entry = &sw_ddb->plane[pipe][plane];
12701
12702 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12703 continue;
12704
12705 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12706 "(expected (%u,%u), found (%u,%u))\n",
12707 pipe_name(pipe), plane + 1,
12708 sw_entry->start, sw_entry->end,
12709 hw_entry->start, hw_entry->end);
12710 }
12711
12712 /* cursor */
12713 hw_entry = &hw_ddb.cursor[pipe];
12714 sw_entry = &sw_ddb->cursor[pipe];
12715
12716 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12717 continue;
12718
12719 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12720 "(expected (%u,%u), found (%u,%u))\n",
12721 pipe_name(pipe),
12722 sw_entry->start, sw_entry->end,
12723 hw_entry->start, hw_entry->end);
12724 }
12725}
12726
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012727static void
12728check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012729{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012730 struct intel_connector *connector;
12731
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012732 for_each_intel_connector(dev, connector) {
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012733 struct drm_encoder *encoder = connector->base.encoder;
12734 struct drm_connector_state *state = connector->base.state;
12735
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012736 /* This also checks the encoder/connector hw state with the
12737 * ->get_hw_state callbacks. */
12738 intel_connector_check_state(connector);
12739
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012740 I915_STATE_WARN(state->best_encoder != encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012741 "connector's staged encoder doesn't match current encoder\n");
12742 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012743}
12744
12745static void
12746check_encoder_state(struct drm_device *dev)
12747{
12748 struct intel_encoder *encoder;
12749 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012750
Damien Lespiaub2784e12014-08-05 11:29:37 +010012751 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012752 bool enabled = false;
12753 bool active = false;
12754 enum pipe pipe, tracked_pipe;
12755
12756 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12757 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012758 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012759
Rob Clarke2c719b2014-12-15 13:56:32 -050012760 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012761 "encoder's active_connectors set, but no crtc\n");
12762
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012763 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012764 if (connector->base.encoder != &encoder->base)
12765 continue;
12766 enabled = true;
12767 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12768 active = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012769
12770 I915_STATE_WARN(connector->base.state->crtc !=
12771 encoder->base.crtc,
12772 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012773 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012774 /*
12775 * for MST connectors if we unplug the connector is gone
12776 * away but the encoder is still connected to a crtc
12777 * until a modeset happens in response to the hotplug.
12778 */
12779 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12780 continue;
12781
Rob Clarke2c719b2014-12-15 13:56:32 -050012782 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012783 "encoder's enabled state mismatch "
12784 "(expected %i, found %i)\n",
12785 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012786 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012787 "active encoder with no crtc\n");
12788
Rob Clarke2c719b2014-12-15 13:56:32 -050012789 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012790 "encoder's computed active state doesn't match tracked active state "
12791 "(expected %i, found %i)\n", active, encoder->connectors_active);
12792
12793 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012794 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012795 "encoder's hw state doesn't match sw tracking "
12796 "(expected %i, found %i)\n",
12797 encoder->connectors_active, active);
12798
12799 if (!encoder->base.crtc)
12800 continue;
12801
12802 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012803 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012804 "active encoder's pipe doesn't match"
12805 "(expected %i, found %i)\n",
12806 tracked_pipe, pipe);
12807
12808 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012809}
12810
12811static void
12812check_crtc_state(struct drm_device *dev)
12813{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012814 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012815 struct intel_crtc *crtc;
12816 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012817 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012818
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012819 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012820 bool enabled = false;
12821 bool active = false;
12822
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012823 memset(&pipe_config, 0, sizeof(pipe_config));
12824
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012825 DRM_DEBUG_KMS("[CRTC:%d]\n",
12826 crtc->base.base.id);
12827
Matt Roper83d65732015-02-25 13:12:16 -080012828 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012829 "active crtc, but not enabled in sw tracking\n");
12830
Damien Lespiaub2784e12014-08-05 11:29:37 +010012831 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012832 if (encoder->base.crtc != &crtc->base)
12833 continue;
12834 enabled = true;
12835 if (encoder->connectors_active)
12836 active = true;
12837 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012838
Rob Clarke2c719b2014-12-15 13:56:32 -050012839 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012840 "crtc's computed active state doesn't match tracked active state "
12841 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012842 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012843 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012844 "(expected %i, found %i)\n", enabled,
12845 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012846
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012847 active = dev_priv->display.get_pipe_config(crtc,
12848 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012849
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012850 /* hw state is inconsistent with the pipe quirk */
12851 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12852 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012853 active = crtc->active;
12854
Damien Lespiaub2784e12014-08-05 11:29:37 +010012855 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012856 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012857 if (encoder->base.crtc != &crtc->base)
12858 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012859 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012860 encoder->get_config(encoder, &pipe_config);
12861 }
12862
Rob Clarke2c719b2014-12-15 13:56:32 -050012863 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012864 "crtc active state doesn't match with hw state "
12865 "(expected %i, found %i)\n", crtc->active, active);
12866
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012867 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12868 "transitional active state does not match atomic hw state "
12869 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12870
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012871 if (!active)
12872 continue;
12873
12874 if (!intel_pipe_config_compare(dev, crtc->config,
12875 &pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012876 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012877 intel_dump_pipe_config(crtc, &pipe_config,
12878 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012879 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012880 "[sw state]");
12881 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012882 }
12883}
12884
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012885static void
12886check_shared_dpll_state(struct drm_device *dev)
12887{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012888 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012889 struct intel_crtc *crtc;
12890 struct intel_dpll_hw_state dpll_hw_state;
12891 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012892
12893 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12894 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12895 int enabled_crtcs = 0, active_crtcs = 0;
12896 bool active;
12897
12898 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12899
12900 DRM_DEBUG_KMS("%s\n", pll->name);
12901
12902 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12903
Rob Clarke2c719b2014-12-15 13:56:32 -050012904 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012905 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012906 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012907 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012908 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012909 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012910 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012911 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012912 "pll on state mismatch (expected %i, found %i)\n",
12913 pll->on, active);
12914
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012915 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012916 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012917 enabled_crtcs++;
12918 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12919 active_crtcs++;
12920 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012921 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012922 "pll active crtcs mismatch (expected %i, found %i)\n",
12923 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012924 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012925 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012926 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012927
Rob Clarke2c719b2014-12-15 13:56:32 -050012928 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012929 sizeof(dpll_hw_state)),
12930 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012931 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012932}
12933
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012934void
12935intel_modeset_check_state(struct drm_device *dev)
12936{
Damien Lespiau08db6652014-11-04 17:06:52 +000012937 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012938 check_connector_state(dev);
12939 check_encoder_state(dev);
12940 check_crtc_state(dev);
12941 check_shared_dpll_state(dev);
12942}
12943
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012944void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012945 int dotclock)
12946{
12947 /*
12948 * FDI already provided one idea for the dotclock.
12949 * Yell if the encoder disagrees.
12950 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012951 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012952 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012953 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012954}
12955
Ville Syrjälä80715b22014-05-15 20:23:23 +030012956static void update_scanline_offset(struct intel_crtc *crtc)
12957{
12958 struct drm_device *dev = crtc->base.dev;
12959
12960 /*
12961 * The scanline counter increments at the leading edge of hsync.
12962 *
12963 * On most platforms it starts counting from vtotal-1 on the
12964 * first active line. That means the scanline counter value is
12965 * always one less than what we would expect. Ie. just after
12966 * start of vblank, which also occurs at start of hsync (on the
12967 * last active line), the scanline counter will read vblank_start-1.
12968 *
12969 * On gen2 the scanline counter starts counting from 1 instead
12970 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12971 * to keep the value positive), instead of adding one.
12972 *
12973 * On HSW+ the behaviour of the scanline counter depends on the output
12974 * type. For DP ports it behaves like most other platforms, but on HDMI
12975 * there's an extra 1 line difference. So we need to add two instead of
12976 * one to the value.
12977 */
12978 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012979 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012980 int vtotal;
12981
12982 vtotal = mode->crtc_vtotal;
12983 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12984 vtotal /= 2;
12985
12986 crtc->scanline_offset = vtotal - 1;
12987 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012988 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012989 crtc->scanline_offset = 2;
12990 } else
12991 crtc->scanline_offset = 1;
12992}
12993
Maarten Lankhorstad421372015-06-15 12:33:42 +020012994static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012995{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012996 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012997 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012998 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012999 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013000 struct intel_crtc_state *intel_crtc_state;
13001 struct drm_crtc *crtc;
13002 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013003 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013004
13005 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013006 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013007
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013008 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020013009 int dpll;
13010
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013011 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030013012 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013013 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013014
Maarten Lankhorstad421372015-06-15 12:33:42 +020013015 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013016 continue;
13017
Maarten Lankhorstad421372015-06-15 12:33:42 +020013018 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013019
Maarten Lankhorstad421372015-06-15 12:33:42 +020013020 if (!shared_dpll)
13021 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13022
13023 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013024 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013025}
13026
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013027/*
13028 * This implements the workaround described in the "notes" section of the mode
13029 * set sequence documentation. When going from no pipes or single pipe to
13030 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13031 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13032 */
13033static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13034{
13035 struct drm_crtc_state *crtc_state;
13036 struct intel_crtc *intel_crtc;
13037 struct drm_crtc *crtc;
13038 struct intel_crtc_state *first_crtc_state = NULL;
13039 struct intel_crtc_state *other_crtc_state = NULL;
13040 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13041 int i;
13042
13043 /* look at all crtc's that are going to be enabled in during modeset */
13044 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13045 intel_crtc = to_intel_crtc(crtc);
13046
13047 if (!crtc_state->active || !needs_modeset(crtc_state))
13048 continue;
13049
13050 if (first_crtc_state) {
13051 other_crtc_state = to_intel_crtc_state(crtc_state);
13052 break;
13053 } else {
13054 first_crtc_state = to_intel_crtc_state(crtc_state);
13055 first_pipe = intel_crtc->pipe;
13056 }
13057 }
13058
13059 /* No workaround needed? */
13060 if (!first_crtc_state)
13061 return 0;
13062
13063 /* w/a possibly needed, check how many crtc's are already enabled. */
13064 for_each_intel_crtc(state->dev, intel_crtc) {
13065 struct intel_crtc_state *pipe_config;
13066
13067 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13068 if (IS_ERR(pipe_config))
13069 return PTR_ERR(pipe_config);
13070
13071 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13072
13073 if (!pipe_config->base.active ||
13074 needs_modeset(&pipe_config->base))
13075 continue;
13076
13077 /* 2 or more enabled crtcs means no need for w/a */
13078 if (enabled_pipe != INVALID_PIPE)
13079 return 0;
13080
13081 enabled_pipe = intel_crtc->pipe;
13082 }
13083
13084 if (enabled_pipe != INVALID_PIPE)
13085 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13086 else if (other_crtc_state)
13087 other_crtc_state->hsw_workaround_pipe = first_pipe;
13088
13089 return 0;
13090}
13091
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013092static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13093{
13094 struct drm_crtc *crtc;
13095 struct drm_crtc_state *crtc_state;
13096 int ret = 0;
13097
13098 /* add all active pipes to the state */
13099 for_each_crtc(state->dev, crtc) {
13100 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13101 if (IS_ERR(crtc_state))
13102 return PTR_ERR(crtc_state);
13103
13104 if (!crtc_state->active || needs_modeset(crtc_state))
13105 continue;
13106
13107 crtc_state->mode_changed = true;
13108
13109 ret = drm_atomic_add_affected_connectors(state, crtc);
13110 if (ret)
13111 break;
13112
13113 ret = drm_atomic_add_affected_planes(state, crtc);
13114 if (ret)
13115 break;
13116 }
13117
13118 return ret;
13119}
13120
13121
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013122/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013123static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013124{
13125 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013126 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013127 int ret;
13128
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013129 if (!check_digital_port_conflicts(state)) {
13130 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13131 return -EINVAL;
13132 }
13133
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013134 /*
13135 * See if the config requires any additional preparation, e.g.
13136 * to adjust global state with pipes off. We need to do this
13137 * here so we can get the modeset_pipe updated config for the new
13138 * mode set on this crtc. For other crtcs we need to use the
13139 * adjusted_mode bits in the crtc directly.
13140 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013141 if (dev_priv->display.modeset_calc_cdclk) {
13142 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030013143
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013144 ret = dev_priv->display.modeset_calc_cdclk(state);
13145
13146 cdclk = to_intel_atomic_state(state)->cdclk;
13147 if (!ret && cdclk != dev_priv->cdclk_freq)
13148 ret = intel_modeset_all_pipes(state);
13149
13150 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013151 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013152 } else
13153 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013154
Maarten Lankhorstad421372015-06-15 12:33:42 +020013155 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013156
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013157 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013158 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013159
Maarten Lankhorstad421372015-06-15 12:33:42 +020013160 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013161}
13162
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013163static int
13164intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013165{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013166 struct drm_crtc *crtc;
13167 struct drm_crtc_state *crtc_state;
13168 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013169 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013170
13171 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013172 if (ret)
13173 return ret;
13174
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013175 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013176 struct intel_crtc_state *pipe_config =
13177 to_intel_crtc_state(crtc_state);
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013178 bool modeset, recalc = false;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013179
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013180 if (!crtc_state->enable) {
13181 if (needs_modeset(crtc_state))
13182 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013183 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013184 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013185
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013186 modeset = needs_modeset(crtc_state);
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013187 /* see comment in intel_modeset_readout_hw_state */
13188 if (!modeset && crtc_state->mode_blob != crtc->state->mode_blob &&
13189 pipe_config->quirks & PIPE_CONFIG_QUIRK_INHERITED_MODE)
13190 recalc = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013191
13192 if (!modeset && !recalc)
13193 continue;
13194
13195 if (recalc) {
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013196 ret = drm_atomic_add_affected_connectors(state, crtc);
13197 if (ret)
13198 return ret;
13199 }
13200
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013201 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013202 if (ret)
13203 return ret;
13204
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013205 if (recalc && (!i915.fastboot ||
13206 !intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013207 to_intel_crtc_state(crtc->state),
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013208 pipe_config, true))) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013209 modeset = crtc_state->mode_changed = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013210
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013211 ret = drm_atomic_add_affected_planes(state, crtc);
13212 if (ret)
13213 return ret;
13214 }
13215
13216 any_ms = modeset;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013217 intel_dump_pipe_config(to_intel_crtc(crtc),
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013218 pipe_config,
13219 modeset ? "[modeset]" : "[fastboot]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013220 }
13221
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013222 if (any_ms) {
13223 ret = intel_modeset_checks(state);
13224
13225 if (ret)
13226 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013227 } else
13228 to_intel_atomic_state(state)->cdclk =
13229 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013230
13231 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013232}
13233
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013234static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013235{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013236 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013237 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013238 struct drm_crtc *crtc;
13239 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013240 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013241 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013242 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013243
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013244 ret = drm_atomic_helper_prepare_planes(dev, state);
13245 if (ret)
13246 return ret;
13247
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013248 drm_atomic_helper_swap_state(dev, state);
13249
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013250 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013251 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13252
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013253 if (!needs_modeset(crtc->state))
13254 continue;
13255
13256 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013257 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013258
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013259 if (crtc_state->active) {
13260 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13261 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013262 intel_crtc->active = false;
13263 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013264 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013265 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013266
Daniel Vetterea9d7582012-07-10 10:42:52 +020013267 /* Only after disabling all output pipelines that will be changed can we
13268 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013269 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013270
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013271 /* The state has been swaped above, so state actually contains the
13272 * old state now. */
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013273 if (any_ms)
13274 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013275
Daniel Vettera6778b32012-07-02 09:56:42 +020013276 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013277 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013278 if (needs_modeset(crtc->state) && crtc->state->active) {
13279 update_scanline_offset(to_intel_crtc(crtc));
13280 dev_priv->display.crtc_enable(crtc);
13281 }
13282
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013283 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013284 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013285
Daniel Vettera6778b32012-07-02 09:56:42 +020013286 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013287
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013288 drm_atomic_helper_cleanup_planes(dev, state);
13289
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013290 drm_atomic_state_free(state);
13291
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013292 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013293}
13294
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013295static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013296{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013297 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013298 int ret;
13299
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013300 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013301 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013302 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013303
13304 return ret;
13305}
13306
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013307static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013308{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013309 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013310
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013311 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013312 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013313 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013314
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013315 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013316}
13317
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013318void intel_crtc_restore_mode(struct drm_crtc *crtc)
13319{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013320 struct drm_device *dev = crtc->dev;
13321 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013322 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013323 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013324
13325 state = drm_atomic_state_alloc(dev);
13326 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013327 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013328 crtc->base.id);
13329 return;
13330 }
13331
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013332 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013333
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013334retry:
13335 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13336 ret = PTR_ERR_OR_ZERO(crtc_state);
13337 if (!ret) {
13338 if (!crtc_state->active)
13339 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013340
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013341 crtc_state->mode_changed = true;
13342 ret = intel_set_mode(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013343 }
13344
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013345 if (ret == -EDEADLK) {
13346 drm_atomic_state_clear(state);
13347 drm_modeset_backoff(state->acquire_ctx);
13348 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013349 }
13350
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013351 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013352out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013353 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013354}
13355
Daniel Vetter25c5b262012-07-08 22:08:04 +020013356#undef for_each_intel_crtc_masked
13357
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013358static bool intel_connector_in_mode_set(struct intel_connector *connector,
13359 struct drm_mode_set *set)
13360{
13361 int ro;
13362
13363 for (ro = 0; ro < set->num_connectors; ro++)
13364 if (set->connectors[ro] == &connector->base)
13365 return true;
13366
13367 return false;
13368}
13369
Daniel Vetter2e431052012-07-04 22:42:15 +020013370static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013371intel_modeset_stage_output_state(struct drm_device *dev,
13372 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013373 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013374{
Daniel Vetter9a935852012-07-05 22:34:27 +020013375 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013376 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013377 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013378 struct drm_crtc *crtc;
13379 struct drm_crtc_state *crtc_state;
13380 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013381
Damien Lespiau9abdda72013-02-13 13:29:23 +000013382 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013383 * of connectors. For paranoia, double-check this. */
13384 WARN_ON(!set->fb && (set->num_connectors != 0));
13385 WARN_ON(set->fb && (set->num_connectors == 0));
13386
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013387 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013388 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13389
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013390 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13391 continue;
13392
13393 connector_state =
13394 drm_atomic_get_connector_state(state, &connector->base);
13395 if (IS_ERR(connector_state))
13396 return PTR_ERR(connector_state);
13397
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013398 if (in_mode_set) {
13399 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013400 connector_state->best_encoder =
13401 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013402 }
13403
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013404 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013405 continue;
13406
Daniel Vetter9a935852012-07-05 22:34:27 +020013407 /* If we disable the crtc, disable all its connectors. Also, if
13408 * the connector is on the changing crtc but not on the new
13409 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013410 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013411 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013412
13413 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13414 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013415 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013416 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013417 }
13418 /* connector->new_encoder is now updated for all connectors. */
13419
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013420 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13421 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013422
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013423 if (!connector_state->best_encoder) {
13424 ret = drm_atomic_set_crtc_for_connector(connector_state,
13425 NULL);
13426 if (ret)
13427 return ret;
13428
Daniel Vetter50f56112012-07-02 09:35:43 +020013429 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013430 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013431
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013432 if (intel_connector_in_mode_set(connector, set)) {
13433 struct drm_crtc *crtc = connector->base.state->crtc;
13434
13435 /* If this connector was in a previous crtc, add it
13436 * to the state. We might need to disable it. */
13437 if (crtc) {
13438 crtc_state =
13439 drm_atomic_get_crtc_state(state, crtc);
13440 if (IS_ERR(crtc_state))
13441 return PTR_ERR(crtc_state);
13442 }
13443
13444 ret = drm_atomic_set_crtc_for_connector(connector_state,
13445 set->crtc);
13446 if (ret)
13447 return ret;
13448 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013449
13450 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013451 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13452 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013453 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013454 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013455
Daniel Vetter9a935852012-07-05 22:34:27 +020013456 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13457 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013458 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013459 connector_state->crtc->base.id);
13460
13461 if (connector_state->best_encoder != &connector->encoder->base)
13462 connector->encoder =
13463 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013464 }
13465
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013466 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013467 bool has_connectors;
13468
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013469 ret = drm_atomic_add_affected_connectors(state, crtc);
13470 if (ret)
13471 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013472
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013473 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13474 if (has_connectors != crtc_state->enable)
13475 crtc_state->enable =
13476 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013477 }
13478
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013479 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13480 set->fb, set->x, set->y);
13481 if (ret)
13482 return ret;
13483
13484 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13485 if (IS_ERR(crtc_state))
13486 return PTR_ERR(crtc_state);
13487
Matt Roperce522992015-06-05 15:08:24 -070013488 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13489 if (ret)
13490 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013491
13492 if (set->num_connectors)
13493 crtc_state->active = true;
13494
Daniel Vetter2e431052012-07-04 22:42:15 +020013495 return 0;
13496}
13497
13498static int intel_crtc_set_config(struct drm_mode_set *set)
13499{
13500 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013501 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013502 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013503
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013504 BUG_ON(!set);
13505 BUG_ON(!set->crtc);
13506 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013507
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013508 /* Enforce sane interface api - has been abused by the fb helper. */
13509 BUG_ON(!set->mode && set->fb);
13510 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013511
Daniel Vetter2e431052012-07-04 22:42:15 +020013512 if (set->fb) {
13513 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13514 set->crtc->base.id, set->fb->base.id,
13515 (int)set->num_connectors, set->x, set->y);
13516 } else {
13517 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013518 }
13519
13520 dev = set->crtc->dev;
13521
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013522 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013523 if (!state)
13524 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013525
13526 state->acquire_ctx = dev->mode_config.acquire_ctx;
13527
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013528 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013529 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013530 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013531
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013532 ret = intel_modeset_compute_config(state);
13533 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013534 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013535
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013536 intel_update_pipe_size(to_intel_crtc(set->crtc));
13537
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013538 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013539 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013540 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13541 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013542 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013543
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013544out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013545 if (ret)
13546 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013547 return ret;
13548}
13549
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013550static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013551 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013552 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013553 .destroy = intel_crtc_destroy,
13554 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013555 .atomic_duplicate_state = intel_crtc_duplicate_state,
13556 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013557};
13558
Daniel Vetter53589012013-06-05 13:34:16 +020013559static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13560 struct intel_shared_dpll *pll,
13561 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013562{
Daniel Vetter53589012013-06-05 13:34:16 +020013563 uint32_t val;
13564
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013565 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013566 return false;
13567
Daniel Vetter53589012013-06-05 13:34:16 +020013568 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013569 hw_state->dpll = val;
13570 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13571 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013572
13573 return val & DPLL_VCO_ENABLE;
13574}
13575
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013576static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13577 struct intel_shared_dpll *pll)
13578{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013579 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13580 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013581}
13582
Daniel Vettere7b903d2013-06-05 13:34:14 +020013583static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13584 struct intel_shared_dpll *pll)
13585{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013586 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013587 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013588
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013589 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013590
13591 /* Wait for the clocks to stabilize. */
13592 POSTING_READ(PCH_DPLL(pll->id));
13593 udelay(150);
13594
13595 /* The pixel multiplier can only be updated once the
13596 * DPLL is enabled and the clocks are stable.
13597 *
13598 * So write it again.
13599 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013600 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013601 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013602 udelay(200);
13603}
13604
13605static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13606 struct intel_shared_dpll *pll)
13607{
13608 struct drm_device *dev = dev_priv->dev;
13609 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013610
13611 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013612 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013613 if (intel_crtc_to_shared_dpll(crtc) == pll)
13614 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13615 }
13616
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013617 I915_WRITE(PCH_DPLL(pll->id), 0);
13618 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013619 udelay(200);
13620}
13621
Daniel Vetter46edb022013-06-05 13:34:12 +020013622static char *ibx_pch_dpll_names[] = {
13623 "PCH DPLL A",
13624 "PCH DPLL B",
13625};
13626
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013627static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013628{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013629 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013630 int i;
13631
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013632 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013633
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013634 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013635 dev_priv->shared_dplls[i].id = i;
13636 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013637 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013638 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13639 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013640 dev_priv->shared_dplls[i].get_hw_state =
13641 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013642 }
13643}
13644
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013645static void intel_shared_dpll_init(struct drm_device *dev)
13646{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013647 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013648
Ville Syrjäläb6283052015-06-03 15:45:07 +030013649 intel_update_cdclk(dev);
13650
Daniel Vetter9cd86932014-06-25 22:01:57 +030013651 if (HAS_DDI(dev))
13652 intel_ddi_pll_init(dev);
13653 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013654 ibx_pch_dpll_init(dev);
13655 else
13656 dev_priv->num_shared_dpll = 0;
13657
13658 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013659}
13660
Matt Roper6beb8c232014-12-01 15:40:14 -080013661/**
13662 * intel_prepare_plane_fb - Prepare fb for usage on plane
13663 * @plane: drm plane to prepare for
13664 * @fb: framebuffer to prepare for presentation
13665 *
13666 * Prepares a framebuffer for usage on a display plane. Generally this
13667 * involves pinning the underlying object and updating the frontbuffer tracking
13668 * bits. Some older platforms need special physical address handling for
13669 * cursor planes.
13670 *
13671 * Returns 0 on success, negative error code on failure.
13672 */
13673int
13674intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013675 struct drm_framebuffer *fb,
13676 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013677{
13678 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013679 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013680 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13681 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013682 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013683
Matt Roperea2c67b2014-12-23 10:41:52 -080013684 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013685 return 0;
13686
Matt Roper4c345742014-07-09 16:22:10 -070013687 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013688
Matt Roper6beb8c232014-12-01 15:40:14 -080013689 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13690 INTEL_INFO(dev)->cursor_needs_physical) {
13691 int align = IS_I830(dev) ? 16 * 1024 : 256;
13692 ret = i915_gem_object_attach_phys(obj, align);
13693 if (ret)
13694 DRM_DEBUG_KMS("failed to attach phys object\n");
13695 } else {
John Harrison91af1272015-06-18 13:14:56 +010013696 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013697 }
13698
13699 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013700 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013701
13702 mutex_unlock(&dev->struct_mutex);
13703
13704 return ret;
13705}
13706
Matt Roper38f3ce32014-12-02 07:45:25 -080013707/**
13708 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13709 * @plane: drm plane to clean up for
13710 * @fb: old framebuffer that was on plane
13711 *
13712 * Cleans up a framebuffer that has just been removed from a plane.
13713 */
13714void
13715intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013716 struct drm_framebuffer *fb,
13717 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013718{
13719 struct drm_device *dev = plane->dev;
13720 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13721
13722 if (WARN_ON(!obj))
13723 return;
13724
13725 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13726 !INTEL_INFO(dev)->cursor_needs_physical) {
13727 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013728 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013729 mutex_unlock(&dev->struct_mutex);
13730 }
Matt Roper465c1202014-05-29 08:06:54 -070013731}
13732
Chandra Konduru6156a452015-04-27 13:48:39 -070013733int
13734skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13735{
13736 int max_scale;
13737 struct drm_device *dev;
13738 struct drm_i915_private *dev_priv;
13739 int crtc_clock, cdclk;
13740
13741 if (!intel_crtc || !crtc_state)
13742 return DRM_PLANE_HELPER_NO_SCALING;
13743
13744 dev = intel_crtc->base.dev;
13745 dev_priv = dev->dev_private;
13746 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013747 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013748
13749 if (!crtc_clock || !cdclk)
13750 return DRM_PLANE_HELPER_NO_SCALING;
13751
13752 /*
13753 * skl max scale is lower of:
13754 * close to 3 but not 3, -1 is for that purpose
13755 * or
13756 * cdclk/crtc_clock
13757 */
13758 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13759
13760 return max_scale;
13761}
13762
Matt Roper465c1202014-05-29 08:06:54 -070013763static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013764intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013765 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013766 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013767{
Matt Roper2b875c22014-12-01 15:40:13 -080013768 struct drm_crtc *crtc = state->base.crtc;
13769 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013770 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013771 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13772 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013773
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013774 /* use scaler when colorkey is not required */
13775 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013776 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013777 min_scale = 1;
13778 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013779 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013780 }
Sonika Jindald8106362015-04-10 14:37:28 +053013781
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013782 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13783 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013784 min_scale, max_scale,
13785 can_position, true,
13786 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013787}
13788
Gustavo Padovan14af2932014-10-24 14:51:31 +010013789static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013790intel_commit_primary_plane(struct drm_plane *plane,
13791 struct intel_plane_state *state)
13792{
Matt Roper2b875c22014-12-01 15:40:13 -080013793 struct drm_crtc *crtc = state->base.crtc;
13794 struct drm_framebuffer *fb = state->base.fb;
13795 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013796 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013797 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013798 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013799
Matt Roperea2c67b2014-12-23 10:41:52 -080013800 crtc = crtc ? crtc : plane->crtc;
13801 intel_crtc = to_intel_crtc(crtc);
13802
Matt Ropercf4c7c12014-12-04 10:27:42 -080013803 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013804 crtc->x = src->x1 >> 16;
13805 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013806
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013807 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013808 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013809
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013810 if (state->visible)
13811 /* FIXME: kill this fastboot hack */
13812 intel_update_pipe_size(intel_crtc);
13813
13814 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013815}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013816
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013817static void
13818intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013819 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013820{
13821 struct drm_device *dev = plane->dev;
13822 struct drm_i915_private *dev_priv = dev->dev_private;
13823
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013824 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13825}
13826
Matt Roper32b7eee2014-12-24 07:59:06 -080013827static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13828{
13829 struct drm_device *dev = crtc->dev;
13830 struct drm_i915_private *dev_priv = dev->dev_private;
13831 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013832
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013833 if (!needs_modeset(crtc->state))
13834 intel_pre_plane_update(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013835
Ville Syrjäläf015c552015-06-24 22:00:02 +030013836 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013837 intel_update_watermarks(crtc);
13838
13839 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013840
13841 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013842 if (crtc->state->active)
Matt Roperc34c9ee2014-12-23 10:41:50 -080013843 intel_crtc->atomic.evade =
13844 intel_pipe_update_start(intel_crtc,
13845 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013846
13847 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13848 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013849}
13850
13851static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13852{
13853 struct drm_device *dev = crtc->dev;
13854 struct drm_i915_private *dev_priv = dev->dev_private;
13855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013856
Matt Roperc34c9ee2014-12-23 10:41:50 -080013857 if (intel_crtc->atomic.evade)
13858 intel_pipe_update_end(intel_crtc,
13859 intel_crtc->atomic.start_vbl_count);
13860
Matt Roper32b7eee2014-12-24 07:59:06 -080013861 intel_runtime_pm_put(dev_priv);
13862
Maarten Lankhorstac21b222015-06-15 12:33:49 +020013863 intel_post_plane_update(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013864}
13865
Matt Ropercf4c7c12014-12-04 10:27:42 -080013866/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013867 * intel_plane_destroy - destroy a plane
13868 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013869 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013870 * Common destruction function for all types of planes (primary, cursor,
13871 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013872 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013873void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013874{
13875 struct intel_plane *intel_plane = to_intel_plane(plane);
13876 drm_plane_cleanup(plane);
13877 kfree(intel_plane);
13878}
13879
Matt Roper65a3fea2015-01-21 16:35:42 -080013880const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013881 .update_plane = drm_atomic_helper_update_plane,
13882 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013883 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013884 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013885 .atomic_get_property = intel_plane_atomic_get_property,
13886 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013887 .atomic_duplicate_state = intel_plane_duplicate_state,
13888 .atomic_destroy_state = intel_plane_destroy_state,
13889
Matt Roper465c1202014-05-29 08:06:54 -070013890};
13891
13892static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13893 int pipe)
13894{
13895 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013896 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013897 const uint32_t *intel_primary_formats;
13898 int num_formats;
13899
13900 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13901 if (primary == NULL)
13902 return NULL;
13903
Matt Roper8e7d6882015-01-21 16:35:41 -080013904 state = intel_create_plane_state(&primary->base);
13905 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013906 kfree(primary);
13907 return NULL;
13908 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013909 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013910
Matt Roper465c1202014-05-29 08:06:54 -070013911 primary->can_scale = false;
13912 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013913 if (INTEL_INFO(dev)->gen >= 9) {
13914 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013915 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013916 }
Matt Roper465c1202014-05-29 08:06:54 -070013917 primary->pipe = pipe;
13918 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013919 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013920 primary->check_plane = intel_check_primary_plane;
13921 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013922 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013923 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13924 primary->plane = !pipe;
13925
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013926 if (INTEL_INFO(dev)->gen >= 9) {
13927 intel_primary_formats = skl_primary_formats;
13928 num_formats = ARRAY_SIZE(skl_primary_formats);
13929 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013930 intel_primary_formats = i965_primary_formats;
13931 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013932 } else {
13933 intel_primary_formats = i8xx_primary_formats;
13934 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013935 }
13936
13937 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013938 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013939 intel_primary_formats, num_formats,
13940 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013941
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013942 if (INTEL_INFO(dev)->gen >= 4)
13943 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013944
Matt Roperea2c67b2014-12-23 10:41:52 -080013945 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13946
Matt Roper465c1202014-05-29 08:06:54 -070013947 return &primary->base;
13948}
13949
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013950void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13951{
13952 if (!dev->mode_config.rotation_property) {
13953 unsigned long flags = BIT(DRM_ROTATE_0) |
13954 BIT(DRM_ROTATE_180);
13955
13956 if (INTEL_INFO(dev)->gen >= 9)
13957 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13958
13959 dev->mode_config.rotation_property =
13960 drm_mode_create_rotation_property(dev, flags);
13961 }
13962 if (dev->mode_config.rotation_property)
13963 drm_object_attach_property(&plane->base.base,
13964 dev->mode_config.rotation_property,
13965 plane->base.state->rotation);
13966}
13967
Matt Roper3d7d6512014-06-10 08:28:13 -070013968static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013969intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013970 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013971 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013972{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013973 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013974 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013975 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013976 unsigned stride;
13977 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013978
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013979 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13980 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013981 DRM_PLANE_HELPER_NO_SCALING,
13982 DRM_PLANE_HELPER_NO_SCALING,
13983 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013984 if (ret)
13985 return ret;
13986
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013987 /* if we want to turn off the cursor ignore width and height */
13988 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013989 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013990
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013991 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013992 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013993 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13994 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013995 return -EINVAL;
13996 }
13997
Matt Roperea2c67b2014-12-23 10:41:52 -080013998 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13999 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014000 DRM_DEBUG_KMS("buffer is too small\n");
14001 return -ENOMEM;
14002 }
14003
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014004 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014005 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014006 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014007 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014008
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014009 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014010}
14011
Matt Roperf4a2cf22014-12-01 15:40:12 -080014012static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014013intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014014 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014015{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014016 intel_crtc_update_cursor(crtc, false);
14017}
14018
14019static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030014020intel_commit_cursor_plane(struct drm_plane *plane,
14021 struct intel_plane_state *state)
14022{
Matt Roper2b875c22014-12-01 15:40:13 -080014023 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014024 struct drm_device *dev = plane->dev;
14025 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014026 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014027 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014028
Matt Roperea2c67b2014-12-23 10:41:52 -080014029 crtc = crtc ? crtc : plane->crtc;
14030 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014031
Matt Roperea2c67b2014-12-23 10:41:52 -080014032 plane->fb = state->base.fb;
14033 crtc->cursor_x = state->base.crtc_x;
14034 crtc->cursor_y = state->base.crtc_y;
14035
Gustavo Padovana912f122014-12-01 15:40:10 -080014036 if (intel_crtc->cursor_bo == obj)
14037 goto update;
14038
Matt Roperf4a2cf22014-12-01 15:40:12 -080014039 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014040 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014041 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014042 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014043 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014044 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014045
Gustavo Padovana912f122014-12-01 15:40:10 -080014046 intel_crtc->cursor_addr = addr;
14047 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080014048
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020014049update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014050 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014051 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014052}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014053
Matt Roper3d7d6512014-06-10 08:28:13 -070014054static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14055 int pipe)
14056{
14057 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014058 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014059
14060 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14061 if (cursor == NULL)
14062 return NULL;
14063
Matt Roper8e7d6882015-01-21 16:35:41 -080014064 state = intel_create_plane_state(&cursor->base);
14065 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014066 kfree(cursor);
14067 return NULL;
14068 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014069 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014070
Matt Roper3d7d6512014-06-10 08:28:13 -070014071 cursor->can_scale = false;
14072 cursor->max_downscale = 1;
14073 cursor->pipe = pipe;
14074 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014075 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014076 cursor->check_plane = intel_check_cursor_plane;
14077 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014078 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014079
14080 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014081 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014082 intel_cursor_formats,
14083 ARRAY_SIZE(intel_cursor_formats),
14084 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014085
14086 if (INTEL_INFO(dev)->gen >= 4) {
14087 if (!dev->mode_config.rotation_property)
14088 dev->mode_config.rotation_property =
14089 drm_mode_create_rotation_property(dev,
14090 BIT(DRM_ROTATE_0) |
14091 BIT(DRM_ROTATE_180));
14092 if (dev->mode_config.rotation_property)
14093 drm_object_attach_property(&cursor->base.base,
14094 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014095 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014096 }
14097
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014098 if (INTEL_INFO(dev)->gen >=9)
14099 state->scaler_id = -1;
14100
Matt Roperea2c67b2014-12-23 10:41:52 -080014101 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14102
Matt Roper3d7d6512014-06-10 08:28:13 -070014103 return &cursor->base;
14104}
14105
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014106static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14107 struct intel_crtc_state *crtc_state)
14108{
14109 int i;
14110 struct intel_scaler *intel_scaler;
14111 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14112
14113 for (i = 0; i < intel_crtc->num_scalers; i++) {
14114 intel_scaler = &scaler_state->scalers[i];
14115 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014116 intel_scaler->mode = PS_SCALER_MODE_DYN;
14117 }
14118
14119 scaler_state->scaler_id = -1;
14120}
14121
Hannes Ederb358d0a2008-12-18 21:18:47 +010014122static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014123{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014124 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014125 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014126 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014127 struct drm_plane *primary = NULL;
14128 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014129 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014130
Daniel Vetter955382f2013-09-19 14:05:45 +020014131 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014132 if (intel_crtc == NULL)
14133 return;
14134
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014135 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14136 if (!crtc_state)
14137 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014138 intel_crtc->config = crtc_state;
14139 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014140 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014141
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014142 /* initialize shared scalers */
14143 if (INTEL_INFO(dev)->gen >= 9) {
14144 if (pipe == PIPE_C)
14145 intel_crtc->num_scalers = 1;
14146 else
14147 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14148
14149 skl_init_scalers(dev, intel_crtc, crtc_state);
14150 }
14151
Matt Roper465c1202014-05-29 08:06:54 -070014152 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014153 if (!primary)
14154 goto fail;
14155
14156 cursor = intel_cursor_plane_create(dev, pipe);
14157 if (!cursor)
14158 goto fail;
14159
Matt Roper465c1202014-05-29 08:06:54 -070014160 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014161 cursor, &intel_crtc_funcs);
14162 if (ret)
14163 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014164
14165 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014166 for (i = 0; i < 256; i++) {
14167 intel_crtc->lut_r[i] = i;
14168 intel_crtc->lut_g[i] = i;
14169 intel_crtc->lut_b[i] = i;
14170 }
14171
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014172 /*
14173 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014174 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014175 */
Jesse Barnes80824002009-09-10 15:28:06 -070014176 intel_crtc->pipe = pipe;
14177 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014178 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014179 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014180 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014181 }
14182
Chris Wilson4b0e3332014-05-30 16:35:26 +030014183 intel_crtc->cursor_base = ~0;
14184 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014185 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014186
Ville Syrjälä852eb002015-06-24 22:00:07 +030014187 intel_crtc->wm.cxsr_allowed = true;
14188
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014189 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14190 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14191 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14192 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14193
Jesse Barnes79e53942008-11-07 14:24:08 -080014194 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014195
14196 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014197 return;
14198
14199fail:
14200 if (primary)
14201 drm_plane_cleanup(primary);
14202 if (cursor)
14203 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014204 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014205 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014206}
14207
Jesse Barnes752aa882013-10-31 18:55:49 +020014208enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14209{
14210 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014211 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014212
Rob Clark51fd3712013-11-19 12:10:12 -050014213 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014214
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014215 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014216 return INVALID_PIPE;
14217
14218 return to_intel_crtc(encoder->crtc)->pipe;
14219}
14220
Carl Worth08d7b3d2009-04-29 14:43:54 -070014221int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014222 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014223{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014224 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014225 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014226 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014227
Rob Clark7707e652014-07-17 23:30:04 -040014228 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014229
Rob Clark7707e652014-07-17 23:30:04 -040014230 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014231 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014232 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014233 }
14234
Rob Clark7707e652014-07-17 23:30:04 -040014235 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014236 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014237
Daniel Vetterc05422d2009-08-11 16:05:30 +020014238 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014239}
14240
Daniel Vetter66a92782012-07-12 20:08:18 +020014241static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014242{
Daniel Vetter66a92782012-07-12 20:08:18 +020014243 struct drm_device *dev = encoder->base.dev;
14244 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014245 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014246 int entry = 0;
14247
Damien Lespiaub2784e12014-08-05 11:29:37 +010014248 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014249 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014250 index_mask |= (1 << entry);
14251
Jesse Barnes79e53942008-11-07 14:24:08 -080014252 entry++;
14253 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014254
Jesse Barnes79e53942008-11-07 14:24:08 -080014255 return index_mask;
14256}
14257
Chris Wilson4d302442010-12-14 19:21:29 +000014258static bool has_edp_a(struct drm_device *dev)
14259{
14260 struct drm_i915_private *dev_priv = dev->dev_private;
14261
14262 if (!IS_MOBILE(dev))
14263 return false;
14264
14265 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14266 return false;
14267
Damien Lespiaue3589902014-02-07 19:12:50 +000014268 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014269 return false;
14270
14271 return true;
14272}
14273
Jesse Barnes84b4e042014-06-25 08:24:29 -070014274static bool intel_crt_present(struct drm_device *dev)
14275{
14276 struct drm_i915_private *dev_priv = dev->dev_private;
14277
Damien Lespiau884497e2013-12-03 13:56:23 +000014278 if (INTEL_INFO(dev)->gen >= 9)
14279 return false;
14280
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014281 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014282 return false;
14283
14284 if (IS_CHERRYVIEW(dev))
14285 return false;
14286
14287 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14288 return false;
14289
14290 return true;
14291}
14292
Jesse Barnes79e53942008-11-07 14:24:08 -080014293static void intel_setup_outputs(struct drm_device *dev)
14294{
Eric Anholt725e30a2009-01-22 13:01:02 -080014295 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014296 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014297 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014298
Daniel Vetterc9093352013-06-06 22:22:47 +020014299 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014300
Jesse Barnes84b4e042014-06-25 08:24:29 -070014301 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014302 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014303
Vandana Kannanc776eb22014-08-19 12:05:01 +053014304 if (IS_BROXTON(dev)) {
14305 /*
14306 * FIXME: Broxton doesn't support port detection via the
14307 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14308 * detect the ports.
14309 */
14310 intel_ddi_init(dev, PORT_A);
14311 intel_ddi_init(dev, PORT_B);
14312 intel_ddi_init(dev, PORT_C);
14313 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014314 int found;
14315
Jesse Barnesde31fac2015-03-06 15:53:32 -080014316 /*
14317 * Haswell uses DDI functions to detect digital outputs.
14318 * On SKL pre-D0 the strap isn't connected, so we assume
14319 * it's there.
14320 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014321 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014322 /* WaIgnoreDDIAStrap: skl */
14323 if (found ||
14324 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014325 intel_ddi_init(dev, PORT_A);
14326
14327 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14328 * register */
14329 found = I915_READ(SFUSE_STRAP);
14330
14331 if (found & SFUSE_STRAP_DDIB_DETECTED)
14332 intel_ddi_init(dev, PORT_B);
14333 if (found & SFUSE_STRAP_DDIC_DETECTED)
14334 intel_ddi_init(dev, PORT_C);
14335 if (found & SFUSE_STRAP_DDID_DETECTED)
14336 intel_ddi_init(dev, PORT_D);
14337 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014338 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014339 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014340
14341 if (has_edp_a(dev))
14342 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014343
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014344 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014345 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014346 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014347 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014348 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014349 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014350 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014351 }
14352
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014353 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014354 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014355
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014356 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014357 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014358
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014359 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014360 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014361
Daniel Vetter270b3042012-10-27 15:52:05 +020014362 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014363 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014364 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014365 /*
14366 * The DP_DETECTED bit is the latched state of the DDC
14367 * SDA pin at boot. However since eDP doesn't require DDC
14368 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14369 * eDP ports may have been muxed to an alternate function.
14370 * Thus we can't rely on the DP_DETECTED bit alone to detect
14371 * eDP ports. Consult the VBT as well as DP_DETECTED to
14372 * detect eDP ports.
14373 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014374 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14375 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014376 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14377 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014378 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14379 intel_dp_is_edp(dev, PORT_B))
14380 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014381
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014382 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14383 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014384 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14385 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014386 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14387 intel_dp_is_edp(dev, PORT_C))
14388 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014389
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014390 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014391 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014392 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14393 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014394 /* eDP not supported on port D, so don't check VBT */
14395 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14396 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014397 }
14398
Jani Nikula3cfca972013-08-27 15:12:26 +030014399 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014400 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014401 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014402
Paulo Zanonie2debe92013-02-18 19:00:27 -030014403 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014404 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014405 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014406 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014407 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014408 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014409 }
Ma Ling27185ae2009-08-24 13:50:23 +080014410
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014411 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014412 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014413 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014414
14415 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014416
Paulo Zanonie2debe92013-02-18 19:00:27 -030014417 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014418 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014419 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014420 }
Ma Ling27185ae2009-08-24 13:50:23 +080014421
Paulo Zanonie2debe92013-02-18 19:00:27 -030014422 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014423
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014424 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014425 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014426 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014427 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014428 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014429 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014430 }
Ma Ling27185ae2009-08-24 13:50:23 +080014431
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014432 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014433 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014434 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014435 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014436 intel_dvo_init(dev);
14437
Zhenyu Wang103a1962009-11-27 11:44:36 +080014438 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014439 intel_tv_init(dev);
14440
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014441 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014442
Damien Lespiaub2784e12014-08-05 11:29:37 +010014443 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014444 encoder->base.possible_crtcs = encoder->crtc_mask;
14445 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014446 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014447 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014448
Paulo Zanonidde86e22012-12-01 12:04:25 -020014449 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014450
14451 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014452}
14453
14454static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14455{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014456 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014457 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014458
Daniel Vetteref2d6332014-02-10 18:00:38 +010014459 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014460 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014461 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014462 drm_gem_object_unreference(&intel_fb->obj->base);
14463 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014464 kfree(intel_fb);
14465}
14466
14467static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014468 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014469 unsigned int *handle)
14470{
14471 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014472 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014473
Chris Wilson05394f32010-11-08 19:18:58 +000014474 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014475}
14476
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014477static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14478 struct drm_file *file,
14479 unsigned flags, unsigned color,
14480 struct drm_clip_rect *clips,
14481 unsigned num_clips)
14482{
14483 struct drm_device *dev = fb->dev;
14484 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14485 struct drm_i915_gem_object *obj = intel_fb->obj;
14486
14487 mutex_lock(&dev->struct_mutex);
14488 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
14489 mutex_unlock(&dev->struct_mutex);
14490
14491 return 0;
14492}
14493
Jesse Barnes79e53942008-11-07 14:24:08 -080014494static const struct drm_framebuffer_funcs intel_fb_funcs = {
14495 .destroy = intel_user_framebuffer_destroy,
14496 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014497 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014498};
14499
Damien Lespiaub3218032015-02-27 11:15:18 +000014500static
14501u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14502 uint32_t pixel_format)
14503{
14504 u32 gen = INTEL_INFO(dev)->gen;
14505
14506 if (gen >= 9) {
14507 /* "The stride in bytes must not exceed the of the size of 8K
14508 * pixels and 32K bytes."
14509 */
14510 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14511 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14512 return 32*1024;
14513 } else if (gen >= 4) {
14514 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14515 return 16*1024;
14516 else
14517 return 32*1024;
14518 } else if (gen >= 3) {
14519 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14520 return 8*1024;
14521 else
14522 return 16*1024;
14523 } else {
14524 /* XXX DSPC is limited to 4k tiled */
14525 return 8*1024;
14526 }
14527}
14528
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014529static int intel_framebuffer_init(struct drm_device *dev,
14530 struct intel_framebuffer *intel_fb,
14531 struct drm_mode_fb_cmd2 *mode_cmd,
14532 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014533{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014534 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014535 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014536 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014537
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014538 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14539
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014540 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14541 /* Enforce that fb modifier and tiling mode match, but only for
14542 * X-tiled. This is needed for FBC. */
14543 if (!!(obj->tiling_mode == I915_TILING_X) !=
14544 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14545 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14546 return -EINVAL;
14547 }
14548 } else {
14549 if (obj->tiling_mode == I915_TILING_X)
14550 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14551 else if (obj->tiling_mode == I915_TILING_Y) {
14552 DRM_DEBUG("No Y tiling for legacy addfb\n");
14553 return -EINVAL;
14554 }
14555 }
14556
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014557 /* Passed in modifier sanity checking. */
14558 switch (mode_cmd->modifier[0]) {
14559 case I915_FORMAT_MOD_Y_TILED:
14560 case I915_FORMAT_MOD_Yf_TILED:
14561 if (INTEL_INFO(dev)->gen < 9) {
14562 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14563 mode_cmd->modifier[0]);
14564 return -EINVAL;
14565 }
14566 case DRM_FORMAT_MOD_NONE:
14567 case I915_FORMAT_MOD_X_TILED:
14568 break;
14569 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014570 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14571 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014572 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014573 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014574
Damien Lespiaub3218032015-02-27 11:15:18 +000014575 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14576 mode_cmd->pixel_format);
14577 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14578 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14579 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014580 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014581 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014582
Damien Lespiaub3218032015-02-27 11:15:18 +000014583 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14584 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014585 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014586 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14587 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014588 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014589 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014590 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014591 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014592
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014593 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014594 mode_cmd->pitches[0] != obj->stride) {
14595 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14596 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014597 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014598 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014599
Ville Syrjälä57779d02012-10-31 17:50:14 +020014600 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014601 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014602 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014603 case DRM_FORMAT_RGB565:
14604 case DRM_FORMAT_XRGB8888:
14605 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014606 break;
14607 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014608 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014609 DRM_DEBUG("unsupported pixel format: %s\n",
14610 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014611 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014612 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014613 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014614 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014615 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14616 DRM_DEBUG("unsupported pixel format: %s\n",
14617 drm_get_format_name(mode_cmd->pixel_format));
14618 return -EINVAL;
14619 }
14620 break;
14621 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014622 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014623 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014624 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014625 DRM_DEBUG("unsupported pixel format: %s\n",
14626 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014627 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014628 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014629 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014630 case DRM_FORMAT_ABGR2101010:
14631 if (!IS_VALLEYVIEW(dev)) {
14632 DRM_DEBUG("unsupported pixel format: %s\n",
14633 drm_get_format_name(mode_cmd->pixel_format));
14634 return -EINVAL;
14635 }
14636 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014637 case DRM_FORMAT_YUYV:
14638 case DRM_FORMAT_UYVY:
14639 case DRM_FORMAT_YVYU:
14640 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014641 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014642 DRM_DEBUG("unsupported pixel format: %s\n",
14643 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014644 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014645 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014646 break;
14647 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014648 DRM_DEBUG("unsupported pixel format: %s\n",
14649 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014650 return -EINVAL;
14651 }
14652
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014653 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14654 if (mode_cmd->offsets[0] != 0)
14655 return -EINVAL;
14656
Damien Lespiauec2c9812015-01-20 12:51:45 +000014657 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014658 mode_cmd->pixel_format,
14659 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014660 /* FIXME drm helper for size checks (especially planar formats)? */
14661 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14662 return -EINVAL;
14663
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014664 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14665 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014666 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014667
Jesse Barnes79e53942008-11-07 14:24:08 -080014668 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14669 if (ret) {
14670 DRM_ERROR("framebuffer init failed %d\n", ret);
14671 return ret;
14672 }
14673
Jesse Barnes79e53942008-11-07 14:24:08 -080014674 return 0;
14675}
14676
Jesse Barnes79e53942008-11-07 14:24:08 -080014677static struct drm_framebuffer *
14678intel_user_framebuffer_create(struct drm_device *dev,
14679 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014680 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014681{
Chris Wilson05394f32010-11-08 19:18:58 +000014682 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014683
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014684 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14685 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014686 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014687 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014688
Chris Wilsond2dff872011-04-19 08:36:26 +010014689 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014690}
14691
Daniel Vetter4520f532013-10-09 09:18:51 +020014692#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014693static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014694{
14695}
14696#endif
14697
Jesse Barnes79e53942008-11-07 14:24:08 -080014698static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014699 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014700 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014701 .atomic_check = intel_atomic_check,
14702 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014703 .atomic_state_alloc = intel_atomic_state_alloc,
14704 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014705};
14706
Jesse Barnese70236a2009-09-21 10:42:27 -070014707/* Set up chip specific display functions */
14708static void intel_init_display(struct drm_device *dev)
14709{
14710 struct drm_i915_private *dev_priv = dev->dev_private;
14711
Daniel Vetteree9300b2013-06-03 22:40:22 +020014712 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14713 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014714 else if (IS_CHERRYVIEW(dev))
14715 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014716 else if (IS_VALLEYVIEW(dev))
14717 dev_priv->display.find_dpll = vlv_find_best_dpll;
14718 else if (IS_PINEVIEW(dev))
14719 dev_priv->display.find_dpll = pnv_find_best_dpll;
14720 else
14721 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14722
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014723 if (INTEL_INFO(dev)->gen >= 9) {
14724 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014725 dev_priv->display.get_initial_plane_config =
14726 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014727 dev_priv->display.crtc_compute_clock =
14728 haswell_crtc_compute_clock;
14729 dev_priv->display.crtc_enable = haswell_crtc_enable;
14730 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014731 dev_priv->display.update_primary_plane =
14732 skylake_update_primary_plane;
14733 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014734 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014735 dev_priv->display.get_initial_plane_config =
14736 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014737 dev_priv->display.crtc_compute_clock =
14738 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014739 dev_priv->display.crtc_enable = haswell_crtc_enable;
14740 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014741 dev_priv->display.update_primary_plane =
14742 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014743 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014744 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014745 dev_priv->display.get_initial_plane_config =
14746 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014747 dev_priv->display.crtc_compute_clock =
14748 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014749 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14750 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014751 dev_priv->display.update_primary_plane =
14752 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014753 } else if (IS_VALLEYVIEW(dev)) {
14754 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014755 dev_priv->display.get_initial_plane_config =
14756 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014757 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014758 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14759 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014760 dev_priv->display.update_primary_plane =
14761 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014762 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014763 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014764 dev_priv->display.get_initial_plane_config =
14765 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014766 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014767 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14768 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014769 dev_priv->display.update_primary_plane =
14770 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014771 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014772
Jesse Barnese70236a2009-09-21 10:42:27 -070014773 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014774 if (IS_SKYLAKE(dev))
14775 dev_priv->display.get_display_clock_speed =
14776 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014777 else if (IS_BROXTON(dev))
14778 dev_priv->display.get_display_clock_speed =
14779 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014780 else if (IS_BROADWELL(dev))
14781 dev_priv->display.get_display_clock_speed =
14782 broadwell_get_display_clock_speed;
14783 else if (IS_HASWELL(dev))
14784 dev_priv->display.get_display_clock_speed =
14785 haswell_get_display_clock_speed;
14786 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014787 dev_priv->display.get_display_clock_speed =
14788 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014789 else if (IS_GEN5(dev))
14790 dev_priv->display.get_display_clock_speed =
14791 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014792 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014793 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014794 dev_priv->display.get_display_clock_speed =
14795 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014796 else if (IS_GM45(dev))
14797 dev_priv->display.get_display_clock_speed =
14798 gm45_get_display_clock_speed;
14799 else if (IS_CRESTLINE(dev))
14800 dev_priv->display.get_display_clock_speed =
14801 i965gm_get_display_clock_speed;
14802 else if (IS_PINEVIEW(dev))
14803 dev_priv->display.get_display_clock_speed =
14804 pnv_get_display_clock_speed;
14805 else if (IS_G33(dev) || IS_G4X(dev))
14806 dev_priv->display.get_display_clock_speed =
14807 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014808 else if (IS_I915G(dev))
14809 dev_priv->display.get_display_clock_speed =
14810 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014811 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014812 dev_priv->display.get_display_clock_speed =
14813 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014814 else if (IS_PINEVIEW(dev))
14815 dev_priv->display.get_display_clock_speed =
14816 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014817 else if (IS_I915GM(dev))
14818 dev_priv->display.get_display_clock_speed =
14819 i915gm_get_display_clock_speed;
14820 else if (IS_I865G(dev))
14821 dev_priv->display.get_display_clock_speed =
14822 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014823 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014824 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014825 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014826 else { /* 830 */
14827 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014828 dev_priv->display.get_display_clock_speed =
14829 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014830 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014831
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014832 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014833 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014834 } else if (IS_GEN6(dev)) {
14835 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014836 } else if (IS_IVYBRIDGE(dev)) {
14837 /* FIXME: detect B0+ stepping and use auto training */
14838 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014839 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014840 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014841 if (IS_BROADWELL(dev)) {
14842 dev_priv->display.modeset_commit_cdclk =
14843 broadwell_modeset_commit_cdclk;
14844 dev_priv->display.modeset_calc_cdclk =
14845 broadwell_modeset_calc_cdclk;
14846 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014847 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014848 dev_priv->display.modeset_commit_cdclk =
14849 valleyview_modeset_commit_cdclk;
14850 dev_priv->display.modeset_calc_cdclk =
14851 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014852 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014853 dev_priv->display.modeset_commit_cdclk =
14854 broxton_modeset_commit_cdclk;
14855 dev_priv->display.modeset_calc_cdclk =
14856 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014857 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014858
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014859 switch (INTEL_INFO(dev)->gen) {
14860 case 2:
14861 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14862 break;
14863
14864 case 3:
14865 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14866 break;
14867
14868 case 4:
14869 case 5:
14870 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14871 break;
14872
14873 case 6:
14874 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14875 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014876 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014877 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014878 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14879 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014880 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014881 /* Drop through - unsupported since execlist only. */
14882 default:
14883 /* Default just returns -ENODEV to indicate unsupported */
14884 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014885 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014886
14887 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014888
14889 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014890}
14891
Jesse Barnesb690e962010-07-19 13:53:12 -070014892/*
14893 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14894 * resume, or other times. This quirk makes sure that's the case for
14895 * affected systems.
14896 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014897static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014898{
14899 struct drm_i915_private *dev_priv = dev->dev_private;
14900
14901 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014902 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014903}
14904
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014905static void quirk_pipeb_force(struct drm_device *dev)
14906{
14907 struct drm_i915_private *dev_priv = dev->dev_private;
14908
14909 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14910 DRM_INFO("applying pipe b force quirk\n");
14911}
14912
Keith Packard435793d2011-07-12 14:56:22 -070014913/*
14914 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14915 */
14916static void quirk_ssc_force_disable(struct drm_device *dev)
14917{
14918 struct drm_i915_private *dev_priv = dev->dev_private;
14919 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014920 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014921}
14922
Carsten Emde4dca20e2012-03-15 15:56:26 +010014923/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014924 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14925 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014926 */
14927static void quirk_invert_brightness(struct drm_device *dev)
14928{
14929 struct drm_i915_private *dev_priv = dev->dev_private;
14930 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014931 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014932}
14933
Scot Doyle9c72cc62014-07-03 23:27:50 +000014934/* Some VBT's incorrectly indicate no backlight is present */
14935static void quirk_backlight_present(struct drm_device *dev)
14936{
14937 struct drm_i915_private *dev_priv = dev->dev_private;
14938 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14939 DRM_INFO("applying backlight present quirk\n");
14940}
14941
Jesse Barnesb690e962010-07-19 13:53:12 -070014942struct intel_quirk {
14943 int device;
14944 int subsystem_vendor;
14945 int subsystem_device;
14946 void (*hook)(struct drm_device *dev);
14947};
14948
Egbert Eich5f85f172012-10-14 15:46:38 +020014949/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14950struct intel_dmi_quirk {
14951 void (*hook)(struct drm_device *dev);
14952 const struct dmi_system_id (*dmi_id_list)[];
14953};
14954
14955static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14956{
14957 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14958 return 1;
14959}
14960
14961static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14962 {
14963 .dmi_id_list = &(const struct dmi_system_id[]) {
14964 {
14965 .callback = intel_dmi_reverse_brightness,
14966 .ident = "NCR Corporation",
14967 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14968 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14969 },
14970 },
14971 { } /* terminating entry */
14972 },
14973 .hook = quirk_invert_brightness,
14974 },
14975};
14976
Ben Widawskyc43b5632012-04-16 14:07:40 -070014977static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014978 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14979 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14980
Jesse Barnesb690e962010-07-19 13:53:12 -070014981 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14982 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14983
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014984 /* 830 needs to leave pipe A & dpll A up */
14985 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14986
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014987 /* 830 needs to leave pipe B & dpll B up */
14988 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14989
Keith Packard435793d2011-07-12 14:56:22 -070014990 /* Lenovo U160 cannot use SSC on LVDS */
14991 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014992
14993 /* Sony Vaio Y cannot use SSC on LVDS */
14994 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014995
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014996 /* Acer Aspire 5734Z must invert backlight brightness */
14997 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14998
14999 /* Acer/eMachines G725 */
15000 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15001
15002 /* Acer/eMachines e725 */
15003 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15004
15005 /* Acer/Packard Bell NCL20 */
15006 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15007
15008 /* Acer Aspire 4736Z */
15009 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015010
15011 /* Acer Aspire 5336 */
15012 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015013
15014 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15015 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015016
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015017 /* Acer C720 Chromebook (Core i3 4005U) */
15018 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15019
jens steinb2a96012014-10-28 20:25:53 +010015020 /* Apple Macbook 2,1 (Core 2 T7400) */
15021 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15022
Scot Doyled4967d82014-07-03 23:27:52 +000015023 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15024 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015025
15026 /* HP Chromebook 14 (Celeron 2955U) */
15027 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015028
15029 /* Dell Chromebook 11 */
15030 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015031};
15032
15033static void intel_init_quirks(struct drm_device *dev)
15034{
15035 struct pci_dev *d = dev->pdev;
15036 int i;
15037
15038 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15039 struct intel_quirk *q = &intel_quirks[i];
15040
15041 if (d->device == q->device &&
15042 (d->subsystem_vendor == q->subsystem_vendor ||
15043 q->subsystem_vendor == PCI_ANY_ID) &&
15044 (d->subsystem_device == q->subsystem_device ||
15045 q->subsystem_device == PCI_ANY_ID))
15046 q->hook(dev);
15047 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015048 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15049 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15050 intel_dmi_quirks[i].hook(dev);
15051 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015052}
15053
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015054/* Disable the VGA plane that we never use */
15055static void i915_disable_vga(struct drm_device *dev)
15056{
15057 struct drm_i915_private *dev_priv = dev->dev_private;
15058 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015059 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015060
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015061 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015062 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015063 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015064 sr1 = inb(VGA_SR_DATA);
15065 outb(sr1 | 1<<5, VGA_SR_DATA);
15066 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15067 udelay(300);
15068
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015069 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015070 POSTING_READ(vga_reg);
15071}
15072
Daniel Vetterf8175862012-04-10 15:50:11 +020015073void intel_modeset_init_hw(struct drm_device *dev)
15074{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015075 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015076 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015077 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015078 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015079}
15080
Jesse Barnes79e53942008-11-07 14:24:08 -080015081void intel_modeset_init(struct drm_device *dev)
15082{
Jesse Barnes652c3932009-08-17 13:31:43 -070015083 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015084 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015085 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015086 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015087
15088 drm_mode_config_init(dev);
15089
15090 dev->mode_config.min_width = 0;
15091 dev->mode_config.min_height = 0;
15092
Dave Airlie019d96c2011-09-29 16:20:42 +010015093 dev->mode_config.preferred_depth = 24;
15094 dev->mode_config.prefer_shadow = 1;
15095
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015096 dev->mode_config.allow_fb_modifiers = true;
15097
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015098 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015099
Jesse Barnesb690e962010-07-19 13:53:12 -070015100 intel_init_quirks(dev);
15101
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015102 intel_init_pm(dev);
15103
Ben Widawskye3c74752013-04-05 13:12:39 -070015104 if (INTEL_INFO(dev)->num_pipes == 0)
15105 return;
15106
Jesse Barnese70236a2009-09-21 10:42:27 -070015107 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015108 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015109
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015110 if (IS_GEN2(dev)) {
15111 dev->mode_config.max_width = 2048;
15112 dev->mode_config.max_height = 2048;
15113 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015114 dev->mode_config.max_width = 4096;
15115 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015116 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015117 dev->mode_config.max_width = 8192;
15118 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015119 }
Damien Lespiau068be562014-03-28 14:17:49 +000015120
Ville Syrjälädc41c152014-08-13 11:57:05 +030015121 if (IS_845G(dev) || IS_I865G(dev)) {
15122 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15123 dev->mode_config.cursor_height = 1023;
15124 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015125 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15126 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15127 } else {
15128 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15129 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15130 }
15131
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015132 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015133
Zhao Yakui28c97732009-10-09 11:39:41 +080015134 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015135 INTEL_INFO(dev)->num_pipes,
15136 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015137
Damien Lespiau055e3932014-08-18 13:49:10 +010015138 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015139 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015140 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015141 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015142 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015143 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015144 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015145 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015146 }
15147
Jesse Barnesf42bb702013-12-16 16:34:23 -080015148 intel_init_dpio(dev);
15149
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015150 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015151
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015152 /* Just disable it once at startup */
15153 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015154 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015155
15156 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030015157 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015158
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015159 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015160 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015161 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015162
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015163 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015164 struct intel_initial_plane_config plane_config = {};
15165
Jesse Barnes46f297f2014-03-07 08:57:48 -080015166 if (!crtc->active)
15167 continue;
15168
Jesse Barnes46f297f2014-03-07 08:57:48 -080015169 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015170 * Note that reserving the BIOS fb up front prevents us
15171 * from stuffing other stolen allocations like the ring
15172 * on top. This prevents some ugliness at boot time, and
15173 * can even allow for smooth boot transitions if the BIOS
15174 * fb is large enough for the active pipe configuration.
15175 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015176 dev_priv->display.get_initial_plane_config(crtc,
15177 &plane_config);
15178
15179 /*
15180 * If the fb is shared between multiple heads, we'll
15181 * just get the first one.
15182 */
15183 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015184 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015185}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015186
Daniel Vetter7fad7982012-07-04 17:51:47 +020015187static void intel_enable_pipe_a(struct drm_device *dev)
15188{
15189 struct intel_connector *connector;
15190 struct drm_connector *crt = NULL;
15191 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015192 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015193
15194 /* We can't just switch on the pipe A, we need to set things up with a
15195 * proper mode and output configuration. As a gross hack, enable pipe A
15196 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015197 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015198 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15199 crt = &connector->base;
15200 break;
15201 }
15202 }
15203
15204 if (!crt)
15205 return;
15206
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015207 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015208 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015209}
15210
Daniel Vetterfa555832012-10-10 23:14:00 +020015211static bool
15212intel_check_plane_mapping(struct intel_crtc *crtc)
15213{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015214 struct drm_device *dev = crtc->base.dev;
15215 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015216 u32 reg, val;
15217
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015218 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015219 return true;
15220
15221 reg = DSPCNTR(!crtc->plane);
15222 val = I915_READ(reg);
15223
15224 if ((val & DISPLAY_PLANE_ENABLE) &&
15225 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15226 return false;
15227
15228 return true;
15229}
15230
Daniel Vetter24929352012-07-02 20:28:59 +020015231static void intel_sanitize_crtc(struct intel_crtc *crtc)
15232{
15233 struct drm_device *dev = crtc->base.dev;
15234 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015235 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015236 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015237 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015238
Daniel Vetter24929352012-07-02 20:28:59 +020015239 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015240 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015241 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15242
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015243 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015244 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015245 if (crtc->active) {
Maarten Lankhorst3a03dfb2015-07-14 13:46:40 +020015246 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015247 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015248 drm_crtc_vblank_on(&crtc->base);
15249 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015250
Daniel Vetter24929352012-07-02 20:28:59 +020015251 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015252 * disable the crtc (and hence change the state) if it is wrong. Note
15253 * that gen4+ has a fixed plane -> pipe mapping. */
15254 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015255 bool plane;
15256
Daniel Vetter24929352012-07-02 20:28:59 +020015257 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15258 crtc->base.base.id);
15259
15260 /* Pipe has the wrong plane attached and the plane is active.
15261 * Temporarily change the plane mapping and disable everything
15262 * ... */
15263 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015264 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015265 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015266 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015267 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015268 }
Daniel Vetter24929352012-07-02 20:28:59 +020015269
Daniel Vetter7fad7982012-07-04 17:51:47 +020015270 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15271 crtc->pipe == PIPE_A && !crtc->active) {
15272 /* BIOS forgot to enable pipe A, this mostly happens after
15273 * resume. Force-enable the pipe to fix this, the update_dpms
15274 * call below we restore the pipe to the right state, but leave
15275 * the required bits on. */
15276 intel_enable_pipe_a(dev);
15277 }
15278
Daniel Vetter24929352012-07-02 20:28:59 +020015279 /* Adjust the state of the output pipe according to whether we
15280 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015281 enable = false;
15282 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15283 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015284
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015285 if (!enable)
15286 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015287
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015288 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015289
15290 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015291 * functions or because of calls to intel_crtc_disable_noatomic,
15292 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015293 * pipe A quirk. */
15294 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15295 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015296 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015297 crtc->active ? "enabled" : "disabled");
15298
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015299 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015300 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015301 crtc->base.enabled = crtc->active;
15302
15303 /* Because we only establish the connector -> encoder ->
15304 * crtc links if something is active, this means the
15305 * crtc is now deactivated. Break the links. connector
15306 * -> encoder links are only establish when things are
15307 * actually up, hence no need to break them. */
15308 WARN_ON(crtc->active);
15309
15310 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15311 WARN_ON(encoder->connectors_active);
15312 encoder->base.crtc = NULL;
15313 }
15314 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015315
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015316 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015317 /*
15318 * We start out with underrun reporting disabled to avoid races.
15319 * For correct bookkeeping mark this on active crtcs.
15320 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015321 * Also on gmch platforms we dont have any hardware bits to
15322 * disable the underrun reporting. Which means we need to start
15323 * out with underrun reporting disabled also on inactive pipes,
15324 * since otherwise we'll complain about the garbage we read when
15325 * e.g. coming up after runtime pm.
15326 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015327 * No protection against concurrent access is required - at
15328 * worst a fifo underrun happens which also sets this to false.
15329 */
15330 crtc->cpu_fifo_underrun_disabled = true;
15331 crtc->pch_fifo_underrun_disabled = true;
15332 }
Daniel Vetter24929352012-07-02 20:28:59 +020015333}
15334
15335static void intel_sanitize_encoder(struct intel_encoder *encoder)
15336{
15337 struct intel_connector *connector;
15338 struct drm_device *dev = encoder->base.dev;
15339
15340 /* We need to check both for a crtc link (meaning that the
15341 * encoder is active and trying to read from a pipe) and the
15342 * pipe itself being active. */
15343 bool has_active_crtc = encoder->base.crtc &&
15344 to_intel_crtc(encoder->base.crtc)->active;
15345
15346 if (encoder->connectors_active && !has_active_crtc) {
15347 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15348 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015349 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015350
15351 /* Connector is active, but has no active pipe. This is
15352 * fallout from our resume register restoring. Disable
15353 * the encoder manually again. */
15354 if (encoder->base.crtc) {
15355 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15356 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015357 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015358 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015359 if (encoder->post_disable)
15360 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015361 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015362 encoder->base.crtc = NULL;
15363 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015364
15365 /* Inconsistent output/port/pipe state happens presumably due to
15366 * a bug in one of the get_hw_state functions. Or someplace else
15367 * in our code, like the register restore mess on resume. Clamp
15368 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015369 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015370 if (connector->encoder != encoder)
15371 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015372 connector->base.dpms = DRM_MODE_DPMS_OFF;
15373 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015374 }
15375 }
15376 /* Enabled encoders without active connectors will be fixed in
15377 * the crtc fixup. */
15378}
15379
Imre Deak04098752014-02-18 00:02:16 +020015380void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015381{
15382 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015383 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015384
Imre Deak04098752014-02-18 00:02:16 +020015385 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15386 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15387 i915_disable_vga(dev);
15388 }
15389}
15390
15391void i915_redisable_vga(struct drm_device *dev)
15392{
15393 struct drm_i915_private *dev_priv = dev->dev_private;
15394
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015395 /* This function can be called both from intel_modeset_setup_hw_state or
15396 * at a very early point in our resume sequence, where the power well
15397 * structures are not yet restored. Since this function is at a very
15398 * paranoid "someone might have enabled VGA while we were not looking"
15399 * level, just check if the power well is enabled instead of trying to
15400 * follow the "don't touch the power well if we don't need it" policy
15401 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015402 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015403 return;
15404
Imre Deak04098752014-02-18 00:02:16 +020015405 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015406}
15407
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015408static bool primary_get_hw_state(struct intel_crtc *crtc)
15409{
15410 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15411
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015412 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15413}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015414
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015415static void readout_plane_state(struct intel_crtc *crtc,
15416 struct intel_crtc_state *crtc_state)
15417{
15418 struct intel_plane *p;
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015419 struct intel_plane_state *plane_state;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015420 bool active = crtc_state->base.active;
15421
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015422 for_each_intel_plane(crtc->base.dev, p) {
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015423 if (crtc->pipe != p->pipe)
15424 continue;
15425
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015426 plane_state = to_intel_plane_state(p->base.state);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015427
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015428 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15429 plane_state->visible = primary_get_hw_state(crtc);
15430 else {
15431 if (active)
15432 p->disable_plane(&p->base, &crtc->base);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015433
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015434 plane_state->visible = false;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015435 }
15436 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015437}
15438
Daniel Vetter30e984d2013-06-05 13:34:17 +020015439static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015440{
15441 struct drm_i915_private *dev_priv = dev->dev_private;
15442 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015443 struct intel_crtc *crtc;
15444 struct intel_encoder *encoder;
15445 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015446 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015447
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015448 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015449 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015450 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015451 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015452
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015453 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015454
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015455 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015456 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015457
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015458 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015459 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015460
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015461 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15462 if (crtc->base.state->active) {
15463 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15464 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15465 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15466
15467 /*
15468 * The initial mode needs to be set in order to keep
15469 * the atomic core happy. It wants a valid mode if the
15470 * crtc's enabled, so we do the above call.
15471 *
15472 * At this point some state updated by the connectors
15473 * in their ->detect() callback has not run yet, so
15474 * no recalculation can be done yet.
15475 *
15476 * Even if we could do a recalculation and modeset
15477 * right now it would cause a double modeset if
15478 * fbdev or userspace chooses a different initial mode.
15479 *
15480 * So to prevent the double modeset, fail the memcmp
15481 * test in drm_atomic_set_mode_for_crtc to get a new
15482 * mode blob, and compare if the mode blob changed
15483 * when the PIPE_CONFIG_QUIRK_INHERITED_MODE quirk is
15484 * set.
15485 *
15486 * If that happens, someone indicated they wanted a
15487 * mode change, which means it's safe to do a full
15488 * recalculation.
15489 */
15490 crtc->base.state->mode.private_flags = ~0;
15491 }
15492
15493 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015494 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015495
15496 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15497 crtc->base.base.id,
15498 crtc->active ? "enabled" : "disabled");
15499 }
15500
Daniel Vetter53589012013-06-05 13:34:16 +020015501 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15502 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15503
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015504 pll->on = pll->get_hw_state(dev_priv, pll,
15505 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015506 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015507 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015508 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015509 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015510 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015511 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015512 }
Daniel Vetter53589012013-06-05 13:34:16 +020015513 }
Daniel Vetter53589012013-06-05 13:34:16 +020015514
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015515 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015516 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015517
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015518 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015519 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015520 }
15521
Damien Lespiaub2784e12014-08-05 11:29:37 +010015522 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015523 pipe = 0;
15524
15525 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015526 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15527 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015528 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015529 } else {
15530 encoder->base.crtc = NULL;
15531 }
15532
15533 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015534 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015535 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015536 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015537 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015538 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015539 }
15540
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015541 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015542 if (connector->get_hw_state(connector)) {
15543 connector->base.dpms = DRM_MODE_DPMS_ON;
15544 connector->encoder->connectors_active = true;
15545 connector->base.encoder = &connector->encoder->base;
15546 } else {
15547 connector->base.dpms = DRM_MODE_DPMS_OFF;
15548 connector->base.encoder = NULL;
15549 }
15550 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15551 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015552 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015553 connector->base.encoder ? "enabled" : "disabled");
15554 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015555}
15556
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015557/* Scan out the current hw modeset state,
15558 * and sanitizes it to the current state
15559 */
15560static void
15561intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015562{
15563 struct drm_i915_private *dev_priv = dev->dev_private;
15564 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015565 struct intel_crtc *crtc;
15566 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015567 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015568
15569 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015570
15571 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015572 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015573 intel_sanitize_encoder(encoder);
15574 }
15575
Damien Lespiau055e3932014-08-18 13:49:10 +010015576 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015577 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15578 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015579 intel_dump_pipe_config(crtc, crtc->config,
15580 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015581 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015582
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015583 intel_modeset_update_connector_atomic_state(dev);
15584
Daniel Vetter35c95372013-07-17 06:55:04 +020015585 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15586 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15587
15588 if (!pll->on || pll->active)
15589 continue;
15590
15591 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15592
15593 pll->disable(dev_priv, pll);
15594 pll->on = false;
15595 }
15596
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015597 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015598 vlv_wm_get_hw_state(dev);
15599 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015600 skl_wm_get_hw_state(dev);
15601 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015602 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015603
15604 for_each_intel_crtc(dev, crtc) {
15605 unsigned long put_domains;
15606
15607 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15608 if (WARN_ON(put_domains))
15609 modeset_put_power_domains(dev_priv, put_domains);
15610 }
15611 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015612}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015613
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015614void intel_display_resume(struct drm_device *dev)
15615{
15616 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15617 struct intel_connector *conn;
15618 struct intel_plane *plane;
15619 struct drm_crtc *crtc;
15620 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015621
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015622 if (!state)
15623 return;
15624
15625 state->acquire_ctx = dev->mode_config.acquire_ctx;
15626
15627 /* preserve complete old state, including dpll */
15628 intel_atomic_get_shared_dpll_state(state);
15629
15630 for_each_crtc(dev, crtc) {
15631 struct drm_crtc_state *crtc_state =
15632 drm_atomic_get_crtc_state(state, crtc);
15633
15634 ret = PTR_ERR_OR_ZERO(crtc_state);
15635 if (ret)
15636 goto err;
15637
15638 /* force a restore */
15639 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015640 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015641
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015642 for_each_intel_plane(dev, plane) {
15643 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15644 if (ret)
15645 goto err;
15646 }
15647
15648 for_each_intel_connector(dev, conn) {
15649 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15650 if (ret)
15651 goto err;
15652 }
15653
15654 intel_modeset_setup_hw_state(dev);
15655
15656 i915_redisable_vga(dev);
15657 ret = intel_set_mode(state);
15658 if (!ret)
15659 return;
15660
15661err:
15662 DRM_ERROR("Restoring old state failed with %i\n", ret);
15663 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015664}
15665
15666void intel_modeset_gem_init(struct drm_device *dev)
15667{
Jesse Barnes92122782014-10-09 12:57:42 -070015668 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015669 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015670 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015671 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015672
Imre Deakae484342014-03-31 15:10:44 +030015673 mutex_lock(&dev->struct_mutex);
15674 intel_init_gt_powersave(dev);
15675 mutex_unlock(&dev->struct_mutex);
15676
Jesse Barnes92122782014-10-09 12:57:42 -070015677 /*
15678 * There may be no VBT; and if the BIOS enabled SSC we can
15679 * just keep using it to avoid unnecessary flicker. Whereas if the
15680 * BIOS isn't using it, don't assume it will work even if the VBT
15681 * indicates as much.
15682 */
15683 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15684 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15685 DREF_SSC1_ENABLE);
15686
Chris Wilson1833b132012-05-09 11:56:28 +010015687 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015688
15689 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015690
15691 /*
15692 * Make sure any fbs we allocated at startup are properly
15693 * pinned & fenced. When we do the allocation it's too early
15694 * for this.
15695 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015696 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015697 obj = intel_fb_obj(c->primary->fb);
15698 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015699 continue;
15700
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015701 mutex_lock(&dev->struct_mutex);
15702 ret = intel_pin_and_fence_fb_obj(c->primary,
15703 c->primary->fb,
15704 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015705 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015706 mutex_unlock(&dev->struct_mutex);
15707 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015708 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15709 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015710 drm_framebuffer_unreference(c->primary->fb);
15711 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015712 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015713 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015714 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015715 }
15716 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015717
15718 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015719}
15720
Imre Deak4932e2c2014-02-11 17:12:48 +020015721void intel_connector_unregister(struct intel_connector *intel_connector)
15722{
15723 struct drm_connector *connector = &intel_connector->base;
15724
15725 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015726 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015727}
15728
Jesse Barnes79e53942008-11-07 14:24:08 -080015729void intel_modeset_cleanup(struct drm_device *dev)
15730{
Jesse Barnes652c3932009-08-17 13:31:43 -070015731 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015732 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015733
Imre Deak2eb52522014-11-19 15:30:05 +020015734 intel_disable_gt_powersave(dev);
15735
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015736 intel_backlight_unregister(dev);
15737
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015738 /*
15739 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015740 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015741 * experience fancy races otherwise.
15742 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015743 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015744
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015745 /*
15746 * Due to the hpd irq storm handling the hotplug work can re-arm the
15747 * poll handlers. Hence disable polling after hpd handling is shut down.
15748 */
Keith Packardf87ea762010-10-03 19:36:26 -070015749 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015750
Jesse Barnes723bfd72010-10-07 16:01:13 -070015751 intel_unregister_dsm_handler();
15752
Paulo Zanoni7733b492015-07-07 15:26:04 -030015753 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015754
Chris Wilson1630fe72011-07-08 12:22:42 +010015755 /* flush any delayed tasks or pending work */
15756 flush_scheduled_work();
15757
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015758 /* destroy the backlight and sysfs files before encoders/connectors */
15759 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015760 struct intel_connector *intel_connector;
15761
15762 intel_connector = to_intel_connector(connector);
15763 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015764 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015765
Jesse Barnes79e53942008-11-07 14:24:08 -080015766 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015767
15768 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015769
15770 mutex_lock(&dev->struct_mutex);
15771 intel_cleanup_gt_powersave(dev);
15772 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015773}
15774
Dave Airlie28d52042009-09-21 14:33:58 +100015775/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015776 * Return which encoder is currently attached for connector.
15777 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015778struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015779{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015780 return &intel_attached_encoder(connector)->base;
15781}
Jesse Barnes79e53942008-11-07 14:24:08 -080015782
Chris Wilsondf0e9242010-09-09 16:20:55 +010015783void intel_connector_attach_encoder(struct intel_connector *connector,
15784 struct intel_encoder *encoder)
15785{
15786 connector->encoder = encoder;
15787 drm_mode_connector_attach_encoder(&connector->base,
15788 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015789}
Dave Airlie28d52042009-09-21 14:33:58 +100015790
15791/*
15792 * set vga decode state - true == enable VGA decode
15793 */
15794int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15795{
15796 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015797 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015798 u16 gmch_ctrl;
15799
Chris Wilson75fa0412014-02-07 18:37:02 -020015800 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15801 DRM_ERROR("failed to read control word\n");
15802 return -EIO;
15803 }
15804
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015805 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15806 return 0;
15807
Dave Airlie28d52042009-09-21 14:33:58 +100015808 if (state)
15809 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15810 else
15811 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015812
15813 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15814 DRM_ERROR("failed to write control word\n");
15815 return -EIO;
15816 }
15817
Dave Airlie28d52042009-09-21 14:33:58 +100015818 return 0;
15819}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015820
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015821struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015822
15823 u32 power_well_driver;
15824
Chris Wilson63b66e52013-08-08 15:12:06 +020015825 int num_transcoders;
15826
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015827 struct intel_cursor_error_state {
15828 u32 control;
15829 u32 position;
15830 u32 base;
15831 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015832 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015833
15834 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015835 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015836 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015837 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015838 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015839
15840 struct intel_plane_error_state {
15841 u32 control;
15842 u32 stride;
15843 u32 size;
15844 u32 pos;
15845 u32 addr;
15846 u32 surface;
15847 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015848 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015849
15850 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015851 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015852 enum transcoder cpu_transcoder;
15853
15854 u32 conf;
15855
15856 u32 htotal;
15857 u32 hblank;
15858 u32 hsync;
15859 u32 vtotal;
15860 u32 vblank;
15861 u32 vsync;
15862 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015863};
15864
15865struct intel_display_error_state *
15866intel_display_capture_error_state(struct drm_device *dev)
15867{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015868 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015869 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015870 int transcoders[] = {
15871 TRANSCODER_A,
15872 TRANSCODER_B,
15873 TRANSCODER_C,
15874 TRANSCODER_EDP,
15875 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015876 int i;
15877
Chris Wilson63b66e52013-08-08 15:12:06 +020015878 if (INTEL_INFO(dev)->num_pipes == 0)
15879 return NULL;
15880
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015881 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015882 if (error == NULL)
15883 return NULL;
15884
Imre Deak190be112013-11-25 17:15:31 +020015885 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015886 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15887
Damien Lespiau055e3932014-08-18 13:49:10 +010015888 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015889 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015890 __intel_display_power_is_enabled(dev_priv,
15891 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015892 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015893 continue;
15894
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015895 error->cursor[i].control = I915_READ(CURCNTR(i));
15896 error->cursor[i].position = I915_READ(CURPOS(i));
15897 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015898
15899 error->plane[i].control = I915_READ(DSPCNTR(i));
15900 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015901 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015902 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015903 error->plane[i].pos = I915_READ(DSPPOS(i));
15904 }
Paulo Zanonica291362013-03-06 20:03:14 -030015905 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15906 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015907 if (INTEL_INFO(dev)->gen >= 4) {
15908 error->plane[i].surface = I915_READ(DSPSURF(i));
15909 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15910 }
15911
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015912 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015913
Sonika Jindal3abfce72014-07-21 15:23:43 +053015914 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015915 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015916 }
15917
15918 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15919 if (HAS_DDI(dev_priv->dev))
15920 error->num_transcoders++; /* Account for eDP. */
15921
15922 for (i = 0; i < error->num_transcoders; i++) {
15923 enum transcoder cpu_transcoder = transcoders[i];
15924
Imre Deakddf9c532013-11-27 22:02:02 +020015925 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015926 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015927 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015928 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015929 continue;
15930
Chris Wilson63b66e52013-08-08 15:12:06 +020015931 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15932
15933 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15934 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15935 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15936 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15937 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15938 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15939 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015940 }
15941
15942 return error;
15943}
15944
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015945#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15946
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015947void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015948intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015949 struct drm_device *dev,
15950 struct intel_display_error_state *error)
15951{
Damien Lespiau055e3932014-08-18 13:49:10 +010015952 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015953 int i;
15954
Chris Wilson63b66e52013-08-08 15:12:06 +020015955 if (!error)
15956 return;
15957
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015958 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015959 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015960 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015961 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015962 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015963 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015964 err_printf(m, " Power: %s\n",
15965 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015966 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015967 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015968
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015969 err_printf(m, "Plane [%d]:\n", i);
15970 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15971 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015972 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015973 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15974 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015975 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015976 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015977 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015978 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015979 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15980 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015981 }
15982
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015983 err_printf(m, "Cursor [%d]:\n", i);
15984 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15985 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15986 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015987 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015988
15989 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015990 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015991 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015992 err_printf(m, " Power: %s\n",
15993 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015994 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15995 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15996 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15997 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15998 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15999 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
16000 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16001 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016002}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016003
16004void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
16005{
16006 struct intel_crtc *crtc;
16007
16008 for_each_intel_crtc(dev, crtc) {
16009 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016010
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016011 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016012
16013 work = crtc->unpin_work;
16014
16015 if (work && work->event &&
16016 work->event->base.file_priv == file) {
16017 kfree(work->event);
16018 work->event = NULL;
16019 }
16020
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016021 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016022 }
16023}