blob: cec0a6c4ef2a8dc2fcd407c442855e62378e813d [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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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äfdd508a62014-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 Mbcddf6102014-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 Mbcddf6102014-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 Cfe3cd482015-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 Cfe3cd482015-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 Cfe3cd482015-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 Cfe3cd482015-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);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006217
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006218 domains = intel_crtc->enabled_power_domains;
6219 for_each_power_domain(domain, domains)
6220 intel_display_power_put(dev_priv, domain);
6221 intel_crtc->enabled_power_domains = 0;
6222}
6223
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006224/*
6225 * turn all crtc's off, but do not adjust state
6226 * This has to be paired with a call to intel_modeset_setup_hw_state.
6227 */
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006228int intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006229{
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006230 struct drm_mode_config *config = &dev->mode_config;
6231 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
6232 struct drm_atomic_state *state;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006233 struct drm_crtc *crtc;
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006234 unsigned crtc_mask = 0;
6235 int ret = 0;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006236
Maarten Lankhorst70e0bd72015-07-13 16:30:29 +02006237 if (WARN_ON(!ctx))
6238 return 0;
6239
6240 lockdep_assert_held(&ctx->ww_ctx);
6241 state = drm_atomic_state_alloc(dev);
6242 if (WARN_ON(!state))
6243 return -ENOMEM;
6244
6245 state->acquire_ctx = ctx;
6246 state->allow_modeset = true;
6247
6248 for_each_crtc(dev, crtc) {
6249 struct drm_crtc_state *crtc_state =
6250 drm_atomic_get_crtc_state(state, crtc);
6251
6252 ret = PTR_ERR_OR_ZERO(crtc_state);
6253 if (ret)
6254 goto free;
6255
6256 if (!crtc_state->active)
6257 continue;
6258
6259 crtc_state->active = false;
6260 crtc_mask |= 1 << drm_crtc_index(crtc);
6261 }
6262
6263 if (crtc_mask) {
6264 ret = intel_set_mode(state);
6265
6266 if (!ret) {
6267 for_each_crtc(dev, crtc)
6268 if (crtc_mask & (1 << drm_crtc_index(crtc)))
6269 crtc->state->active = true;
6270
6271 return ret;
6272 }
6273 }
6274
6275free:
6276 if (ret)
6277 DRM_ERROR("Suspending crtc's failed with %i\n", ret);
6278 drm_atomic_state_free(state);
6279 return ret;
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006280}
6281
Chris Wilsoncdd59982010-09-08 16:30:16 +01006282/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006283int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006284{
6285 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006286 struct drm_mode_config *config = &dev->mode_config;
6287 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006289 struct intel_crtc_state *pipe_config;
6290 struct drm_atomic_state *state;
6291 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006292
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006293 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006294 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006295
6296 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006297 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006298
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006299 /* this function should be called with drm_modeset_lock_all for now */
6300 if (WARN_ON(!ctx))
6301 return -EIO;
6302 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006303
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006304 state = drm_atomic_state_alloc(dev);
6305 if (WARN_ON(!state))
6306 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006307
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006308 state->acquire_ctx = ctx;
6309 state->allow_modeset = true;
6310
6311 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6312 if (IS_ERR(pipe_config)) {
6313 ret = PTR_ERR(pipe_config);
6314 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006315 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006316 pipe_config->base.active = enable;
6317
6318 ret = intel_set_mode(state);
6319 if (!ret)
6320 return ret;
6321
6322err:
6323 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6324 drm_atomic_state_free(state);
6325 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306326}
6327
6328/**
6329 * Sets the power management mode of the pipe and plane.
6330 */
6331void intel_crtc_update_dpms(struct drm_crtc *crtc)
6332{
6333 struct drm_device *dev = crtc->dev;
6334 struct intel_encoder *intel_encoder;
6335 bool enable = false;
6336
6337 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6338 enable |= intel_encoder->connectors_active;
6339
6340 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006341}
6342
Chris Wilsonea5b2132010-08-04 13:50:23 +01006343void intel_encoder_destroy(struct drm_encoder *encoder)
6344{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006345 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006346
Chris Wilsonea5b2132010-08-04 13:50:23 +01006347 drm_encoder_cleanup(encoder);
6348 kfree(intel_encoder);
6349}
6350
Damien Lespiau92373292013-08-08 22:28:57 +01006351/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006352 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6353 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006354static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006355{
6356 if (mode == DRM_MODE_DPMS_ON) {
6357 encoder->connectors_active = true;
6358
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006359 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006360 } else {
6361 encoder->connectors_active = false;
6362
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006363 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006364 }
6365}
6366
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006367/* Cross check the actual hw state with our own modeset state tracking (and it's
6368 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006369static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006370{
6371 if (connector->get_hw_state(connector)) {
6372 struct intel_encoder *encoder = connector->encoder;
6373 struct drm_crtc *crtc;
6374 bool encoder_enabled;
6375 enum pipe pipe;
6376
6377 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6378 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006379 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006380
Dave Airlie0e32b392014-05-02 14:02:48 +10006381 /* there is no real hw state for MST connectors */
6382 if (connector->mst_port)
6383 return;
6384
Rob Clarke2c719b2014-12-15 13:56:32 -05006385 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006386 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006387 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006388 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006389
Dave Airlie36cd7442014-05-02 13:44:18 +10006390 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006391 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006392 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006393
Dave Airlie36cd7442014-05-02 13:44:18 +10006394 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006395 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6396 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006397 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006398
Dave Airlie36cd7442014-05-02 13:44:18 +10006399 crtc = encoder->base.crtc;
6400
Matt Roper83d65732015-02-25 13:12:16 -08006401 I915_STATE_WARN(!crtc->state->enable,
6402 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006403 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6404 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006405 "encoder active on the wrong pipe\n");
6406 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006407 }
6408}
6409
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006410int intel_connector_init(struct intel_connector *connector)
6411{
6412 struct drm_connector_state *connector_state;
6413
6414 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6415 if (!connector_state)
6416 return -ENOMEM;
6417
6418 connector->base.state = connector_state;
6419 return 0;
6420}
6421
6422struct intel_connector *intel_connector_alloc(void)
6423{
6424 struct intel_connector *connector;
6425
6426 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6427 if (!connector)
6428 return NULL;
6429
6430 if (intel_connector_init(connector) < 0) {
6431 kfree(connector);
6432 return NULL;
6433 }
6434
6435 return connector;
6436}
6437
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006438/* Even simpler default implementation, if there's really no special case to
6439 * consider. */
6440void intel_connector_dpms(struct drm_connector *connector, int mode)
6441{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006442 /* All the simple cases only support two dpms states. */
6443 if (mode != DRM_MODE_DPMS_ON)
6444 mode = DRM_MODE_DPMS_OFF;
6445
6446 if (mode == connector->dpms)
6447 return;
6448
6449 connector->dpms = mode;
6450
6451 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006452 if (connector->encoder)
6453 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006454
Daniel Vetterb9805142012-08-31 17:37:33 +02006455 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006456}
6457
Daniel Vetterf0947c32012-07-02 13:10:34 +02006458/* Simple connector->get_hw_state implementation for encoders that support only
6459 * one connector and no cloning and hence the encoder state determines the state
6460 * of the connector. */
6461bool intel_connector_get_hw_state(struct intel_connector *connector)
6462{
Daniel Vetter24929352012-07-02 20:28:59 +02006463 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006464 struct intel_encoder *encoder = connector->encoder;
6465
6466 return encoder->get_hw_state(encoder, &pipe);
6467}
6468
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006469static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006470{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006471 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6472 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006473
6474 return 0;
6475}
6476
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006477static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006478 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006479{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006480 struct drm_atomic_state *state = pipe_config->base.state;
6481 struct intel_crtc *other_crtc;
6482 struct intel_crtc_state *other_crtc_state;
6483
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006484 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6485 pipe_name(pipe), pipe_config->fdi_lanes);
6486 if (pipe_config->fdi_lanes > 4) {
6487 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6488 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006489 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006490 }
6491
Paulo Zanonibafb6552013-11-02 21:07:44 -07006492 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006493 if (pipe_config->fdi_lanes > 2) {
6494 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6495 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006496 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006497 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006498 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006499 }
6500 }
6501
6502 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006504
6505 /* Ivybridge 3 pipe is really complicated */
6506 switch (pipe) {
6507 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006508 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006509 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006510 if (pipe_config->fdi_lanes <= 2)
6511 return 0;
6512
6513 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6514 other_crtc_state =
6515 intel_atomic_get_crtc_state(state, other_crtc);
6516 if (IS_ERR(other_crtc_state))
6517 return PTR_ERR(other_crtc_state);
6518
6519 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006520 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6521 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006522 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006523 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006524 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006525 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006526 if (pipe_config->fdi_lanes > 2) {
6527 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6528 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006529 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006530 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006531
6532 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6533 other_crtc_state =
6534 intel_atomic_get_crtc_state(state, other_crtc);
6535 if (IS_ERR(other_crtc_state))
6536 return PTR_ERR(other_crtc_state);
6537
6538 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006539 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006540 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006541 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006542 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006543 default:
6544 BUG();
6545 }
6546}
6547
Daniel Vettere29c22c2013-02-21 00:00:16 +01006548#define RETRY 1
6549static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006550 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006551{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006552 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006553 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006554 int lane, link_bw, fdi_dotclock, ret;
6555 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006556
Daniel Vettere29c22c2013-02-21 00:00:16 +01006557retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006558 /* FDI is a binary signal running at ~2.7GHz, encoding
6559 * each output octet as 10 bits. The actual frequency
6560 * is stored as a divider into a 100MHz clock, and the
6561 * mode pixel clock is stored in units of 1KHz.
6562 * Hence the bw of each lane in terms of the mode signal
6563 * is:
6564 */
6565 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6566
Damien Lespiau241bfc32013-09-25 16:45:37 +01006567 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006568
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006569 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006570 pipe_config->pipe_bpp);
6571
6572 pipe_config->fdi_lanes = lane;
6573
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006574 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006575 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006576
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006577 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6578 intel_crtc->pipe, pipe_config);
6579 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006580 pipe_config->pipe_bpp -= 2*3;
6581 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6582 pipe_config->pipe_bpp);
6583 needs_recompute = true;
6584 pipe_config->bw_constrained = true;
6585
6586 goto retry;
6587 }
6588
6589 if (needs_recompute)
6590 return RETRY;
6591
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006592 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006593}
6594
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006595static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6596 struct intel_crtc_state *pipe_config)
6597{
6598 if (pipe_config->pipe_bpp > 24)
6599 return false;
6600
6601 /* HSW can handle pixel rate up to cdclk? */
6602 if (IS_HASWELL(dev_priv->dev))
6603 return true;
6604
6605 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006606 * We compare against max which means we must take
6607 * the increased cdclk requirement into account when
6608 * calculating the new cdclk.
6609 *
6610 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006611 */
6612 return ilk_pipe_pixel_rate(pipe_config) <=
6613 dev_priv->max_cdclk_freq * 95 / 100;
6614}
6615
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006616static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006617 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006618{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006619 struct drm_device *dev = crtc->base.dev;
6620 struct drm_i915_private *dev_priv = dev->dev_private;
6621
Jani Nikulad330a952014-01-21 11:24:25 +02006622 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006623 hsw_crtc_supports_ips(crtc) &&
6624 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006625}
6626
Daniel Vettera43f6e02013-06-07 23:10:32 +02006627static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006628 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006629{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006630 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006631 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006632 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006633
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006634 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006635 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006636 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006637
6638 /*
6639 * Enable pixel doubling when the dot clock
6640 * is > 90% of the (display) core speed.
6641 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006642 * GDG double wide on either pipe,
6643 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006644 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006645 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006646 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006647 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006648 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006649 }
6650
Damien Lespiau241bfc32013-09-25 16:45:37 +01006651 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006652 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006653 }
Chris Wilson89749352010-09-12 18:25:19 +01006654
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006655 /*
6656 * Pipe horizontal size must be even in:
6657 * - DVO ganged mode
6658 * - LVDS dual channel mode
6659 * - Double wide pipe
6660 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006661 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006662 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6663 pipe_config->pipe_src_w &= ~1;
6664
Damien Lespiau8693a822013-05-03 18:48:11 +01006665 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6666 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006667 */
6668 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6669 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006670 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006671
Damien Lespiauf5adf942013-06-24 18:29:34 +01006672 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006673 hsw_compute_ips_config(crtc, pipe_config);
6674
Daniel Vetter877d48d2013-04-19 11:24:43 +02006675 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006676 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006677
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006678 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006679}
6680
Ville Syrjälä1652d192015-03-31 14:12:01 +03006681static int skylake_get_display_clock_speed(struct drm_device *dev)
6682{
6683 struct drm_i915_private *dev_priv = to_i915(dev);
6684 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6685 uint32_t cdctl = I915_READ(CDCLK_CTL);
6686 uint32_t linkrate;
6687
Damien Lespiau414355a2015-06-04 18:21:31 +01006688 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006689 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006690
6691 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6692 return 540000;
6693
6694 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006695 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006696
Damien Lespiau71cd8422015-04-30 16:39:17 +01006697 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6698 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006699 /* vco 8640 */
6700 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6701 case CDCLK_FREQ_450_432:
6702 return 432000;
6703 case CDCLK_FREQ_337_308:
6704 return 308570;
6705 case CDCLK_FREQ_675_617:
6706 return 617140;
6707 default:
6708 WARN(1, "Unknown cd freq selection\n");
6709 }
6710 } else {
6711 /* vco 8100 */
6712 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6713 case CDCLK_FREQ_450_432:
6714 return 450000;
6715 case CDCLK_FREQ_337_308:
6716 return 337500;
6717 case CDCLK_FREQ_675_617:
6718 return 675000;
6719 default:
6720 WARN(1, "Unknown cd freq selection\n");
6721 }
6722 }
6723
6724 /* error case, do as if DPLL0 isn't enabled */
6725 return 24000;
6726}
6727
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006728static int broxton_get_display_clock_speed(struct drm_device *dev)
6729{
6730 struct drm_i915_private *dev_priv = to_i915(dev);
6731 uint32_t cdctl = I915_READ(CDCLK_CTL);
6732 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6733 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6734 int cdclk;
6735
6736 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6737 return 19200;
6738
6739 cdclk = 19200 * pll_ratio / 2;
6740
6741 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6742 case BXT_CDCLK_CD2X_DIV_SEL_1:
6743 return cdclk; /* 576MHz or 624MHz */
6744 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6745 return cdclk * 2 / 3; /* 384MHz */
6746 case BXT_CDCLK_CD2X_DIV_SEL_2:
6747 return cdclk / 2; /* 288MHz */
6748 case BXT_CDCLK_CD2X_DIV_SEL_4:
6749 return cdclk / 4; /* 144MHz */
6750 }
6751
6752 /* error case, do as if DE PLL isn't enabled */
6753 return 19200;
6754}
6755
Ville Syrjälä1652d192015-03-31 14:12:01 +03006756static int broadwell_get_display_clock_speed(struct drm_device *dev)
6757{
6758 struct drm_i915_private *dev_priv = dev->dev_private;
6759 uint32_t lcpll = I915_READ(LCPLL_CTL);
6760 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6761
6762 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6763 return 800000;
6764 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6765 return 450000;
6766 else if (freq == LCPLL_CLK_FREQ_450)
6767 return 450000;
6768 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6769 return 540000;
6770 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6771 return 337500;
6772 else
6773 return 675000;
6774}
6775
6776static int haswell_get_display_clock_speed(struct drm_device *dev)
6777{
6778 struct drm_i915_private *dev_priv = dev->dev_private;
6779 uint32_t lcpll = I915_READ(LCPLL_CTL);
6780 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6781
6782 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6783 return 800000;
6784 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6785 return 450000;
6786 else if (freq == LCPLL_CLK_FREQ_450)
6787 return 450000;
6788 else if (IS_HSW_ULT(dev))
6789 return 337500;
6790 else
6791 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006792}
6793
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006794static int valleyview_get_display_clock_speed(struct drm_device *dev)
6795{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006796 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006797 u32 val;
6798 int divider;
6799
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006800 if (dev_priv->hpll_freq == 0)
6801 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6802
Ville Syrjäläa5805162015-05-26 20:42:30 +03006803 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006804 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006805 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006806
6807 divider = val & DISPLAY_FREQUENCY_VALUES;
6808
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006809 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6810 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6811 "cdclk change in progress\n");
6812
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006813 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006814}
6815
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006816static int ilk_get_display_clock_speed(struct drm_device *dev)
6817{
6818 return 450000;
6819}
6820
Jesse Barnese70236a2009-09-21 10:42:27 -07006821static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006822{
Jesse Barnese70236a2009-09-21 10:42:27 -07006823 return 400000;
6824}
Jesse Barnes79e53942008-11-07 14:24:08 -08006825
Jesse Barnese70236a2009-09-21 10:42:27 -07006826static int i915_get_display_clock_speed(struct drm_device *dev)
6827{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006828 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006829}
Jesse Barnes79e53942008-11-07 14:24:08 -08006830
Jesse Barnese70236a2009-09-21 10:42:27 -07006831static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6832{
6833 return 200000;
6834}
Jesse Barnes79e53942008-11-07 14:24:08 -08006835
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006836static int pnv_get_display_clock_speed(struct drm_device *dev)
6837{
6838 u16 gcfgc = 0;
6839
6840 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6841
6842 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6843 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006844 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006845 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006846 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006847 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006848 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006849 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6850 return 200000;
6851 default:
6852 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6853 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006854 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006855 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006856 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006857 }
6858}
6859
Jesse Barnese70236a2009-09-21 10:42:27 -07006860static int i915gm_get_display_clock_speed(struct drm_device *dev)
6861{
6862 u16 gcfgc = 0;
6863
6864 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6865
6866 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006867 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006868 else {
6869 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6870 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006871 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006872 default:
6873 case GC_DISPLAY_CLOCK_190_200_MHZ:
6874 return 190000;
6875 }
6876 }
6877}
Jesse Barnes79e53942008-11-07 14:24:08 -08006878
Jesse Barnese70236a2009-09-21 10:42:27 -07006879static int i865_get_display_clock_speed(struct drm_device *dev)
6880{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006881 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006882}
6883
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006884static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006885{
6886 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006887
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006888 /*
6889 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6890 * encoding is different :(
6891 * FIXME is this the right way to detect 852GM/852GMV?
6892 */
6893 if (dev->pdev->revision == 0x1)
6894 return 133333;
6895
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006896 pci_bus_read_config_word(dev->pdev->bus,
6897 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6898
Jesse Barnese70236a2009-09-21 10:42:27 -07006899 /* Assume that the hardware is in the high speed state. This
6900 * should be the default.
6901 */
6902 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6903 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006904 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006905 case GC_CLOCK_100_200:
6906 return 200000;
6907 case GC_CLOCK_166_250:
6908 return 250000;
6909 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006910 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006911 case GC_CLOCK_133_266:
6912 case GC_CLOCK_133_266_2:
6913 case GC_CLOCK_166_266:
6914 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006915 }
6916
6917 /* Shouldn't happen */
6918 return 0;
6919}
6920
6921static int i830_get_display_clock_speed(struct drm_device *dev)
6922{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006923 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006924}
6925
Ville Syrjälä34edce22015-05-22 11:22:33 +03006926static unsigned int intel_hpll_vco(struct drm_device *dev)
6927{
6928 struct drm_i915_private *dev_priv = dev->dev_private;
6929 static const unsigned int blb_vco[8] = {
6930 [0] = 3200000,
6931 [1] = 4000000,
6932 [2] = 5333333,
6933 [3] = 4800000,
6934 [4] = 6400000,
6935 };
6936 static const unsigned int pnv_vco[8] = {
6937 [0] = 3200000,
6938 [1] = 4000000,
6939 [2] = 5333333,
6940 [3] = 4800000,
6941 [4] = 2666667,
6942 };
6943 static const unsigned int cl_vco[8] = {
6944 [0] = 3200000,
6945 [1] = 4000000,
6946 [2] = 5333333,
6947 [3] = 6400000,
6948 [4] = 3333333,
6949 [5] = 3566667,
6950 [6] = 4266667,
6951 };
6952 static const unsigned int elk_vco[8] = {
6953 [0] = 3200000,
6954 [1] = 4000000,
6955 [2] = 5333333,
6956 [3] = 4800000,
6957 };
6958 static const unsigned int ctg_vco[8] = {
6959 [0] = 3200000,
6960 [1] = 4000000,
6961 [2] = 5333333,
6962 [3] = 6400000,
6963 [4] = 2666667,
6964 [5] = 4266667,
6965 };
6966 const unsigned int *vco_table;
6967 unsigned int vco;
6968 uint8_t tmp = 0;
6969
6970 /* FIXME other chipsets? */
6971 if (IS_GM45(dev))
6972 vco_table = ctg_vco;
6973 else if (IS_G4X(dev))
6974 vco_table = elk_vco;
6975 else if (IS_CRESTLINE(dev))
6976 vco_table = cl_vco;
6977 else if (IS_PINEVIEW(dev))
6978 vco_table = pnv_vco;
6979 else if (IS_G33(dev))
6980 vco_table = blb_vco;
6981 else
6982 return 0;
6983
6984 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6985
6986 vco = vco_table[tmp & 0x7];
6987 if (vco == 0)
6988 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6989 else
6990 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6991
6992 return vco;
6993}
6994
6995static int gm45_get_display_clock_speed(struct drm_device *dev)
6996{
6997 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6998 uint16_t tmp = 0;
6999
7000 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7001
7002 cdclk_sel = (tmp >> 12) & 0x1;
7003
7004 switch (vco) {
7005 case 2666667:
7006 case 4000000:
7007 case 5333333:
7008 return cdclk_sel ? 333333 : 222222;
7009 case 3200000:
7010 return cdclk_sel ? 320000 : 228571;
7011 default:
7012 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7013 return 222222;
7014 }
7015}
7016
7017static int i965gm_get_display_clock_speed(struct drm_device *dev)
7018{
7019 static const uint8_t div_3200[] = { 16, 10, 8 };
7020 static const uint8_t div_4000[] = { 20, 12, 10 };
7021 static const uint8_t div_5333[] = { 24, 16, 14 };
7022 const uint8_t *div_table;
7023 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7024 uint16_t tmp = 0;
7025
7026 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7027
7028 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7029
7030 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7031 goto fail;
7032
7033 switch (vco) {
7034 case 3200000:
7035 div_table = div_3200;
7036 break;
7037 case 4000000:
7038 div_table = div_4000;
7039 break;
7040 case 5333333:
7041 div_table = div_5333;
7042 break;
7043 default:
7044 goto fail;
7045 }
7046
7047 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7048
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007049fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007050 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7051 return 200000;
7052}
7053
7054static int g33_get_display_clock_speed(struct drm_device *dev)
7055{
7056 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7057 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7058 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7059 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7060 const uint8_t *div_table;
7061 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7062 uint16_t tmp = 0;
7063
7064 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7065
7066 cdclk_sel = (tmp >> 4) & 0x7;
7067
7068 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7069 goto fail;
7070
7071 switch (vco) {
7072 case 3200000:
7073 div_table = div_3200;
7074 break;
7075 case 4000000:
7076 div_table = div_4000;
7077 break;
7078 case 4800000:
7079 div_table = div_4800;
7080 break;
7081 case 5333333:
7082 div_table = div_5333;
7083 break;
7084 default:
7085 goto fail;
7086 }
7087
7088 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7089
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007090fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007091 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7092 return 190476;
7093}
7094
Zhenyu Wang2c072452009-06-05 15:38:42 +08007095static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007096intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007097{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007098 while (*num > DATA_LINK_M_N_MASK ||
7099 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007100 *num >>= 1;
7101 *den >>= 1;
7102 }
7103}
7104
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007105static void compute_m_n(unsigned int m, unsigned int n,
7106 uint32_t *ret_m, uint32_t *ret_n)
7107{
7108 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7109 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7110 intel_reduce_m_n_ratio(ret_m, ret_n);
7111}
7112
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007113void
7114intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7115 int pixel_clock, int link_clock,
7116 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007117{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007118 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007119
7120 compute_m_n(bits_per_pixel * pixel_clock,
7121 link_clock * nlanes * 8,
7122 &m_n->gmch_m, &m_n->gmch_n);
7123
7124 compute_m_n(pixel_clock, link_clock,
7125 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007126}
7127
Chris Wilsona7615032011-01-12 17:04:08 +00007128static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7129{
Jani Nikulad330a952014-01-21 11:24:25 +02007130 if (i915.panel_use_ssc >= 0)
7131 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007132 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007133 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007134}
7135
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007136static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7137 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007138{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007139 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007140 struct drm_i915_private *dev_priv = dev->dev_private;
7141 int refclk;
7142
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007143 WARN_ON(!crtc_state->base.state);
7144
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007145 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007146 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007147 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007148 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007149 refclk = dev_priv->vbt.lvds_ssc_freq;
7150 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007151 } else if (!IS_GEN2(dev)) {
7152 refclk = 96000;
7153 } else {
7154 refclk = 48000;
7155 }
7156
7157 return refclk;
7158}
7159
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007160static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007161{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007162 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007163}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007164
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007165static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7166{
7167 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007168}
7169
Daniel Vetterf47709a2013-03-28 10:42:02 +01007170static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007171 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007172 intel_clock_t *reduced_clock)
7173{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007174 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007175 u32 fp, fp2 = 0;
7176
7177 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007178 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007179 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007180 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007181 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007182 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007183 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007184 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007185 }
7186
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007187 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007188
Daniel Vetterf47709a2013-03-28 10:42:02 +01007189 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007190 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007191 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007192 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007193 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007194 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007195 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007196 }
7197}
7198
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007199static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7200 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007201{
7202 u32 reg_val;
7203
7204 /*
7205 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7206 * and set it to a reasonable value instead.
7207 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007208 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007209 reg_val &= 0xffffff00;
7210 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007211 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007212
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007213 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007214 reg_val &= 0x8cffffff;
7215 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007216 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007217
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007218 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007219 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007220 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007221
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007222 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007223 reg_val &= 0x00ffffff;
7224 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007225 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007226}
7227
Daniel Vetterb5518422013-05-03 11:49:48 +02007228static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7229 struct intel_link_m_n *m_n)
7230{
7231 struct drm_device *dev = crtc->base.dev;
7232 struct drm_i915_private *dev_priv = dev->dev_private;
7233 int pipe = crtc->pipe;
7234
Daniel Vettere3b95f12013-05-03 11:49:49 +02007235 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7236 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7237 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7238 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007239}
7240
7241static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007242 struct intel_link_m_n *m_n,
7243 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007244{
7245 struct drm_device *dev = crtc->base.dev;
7246 struct drm_i915_private *dev_priv = dev->dev_private;
7247 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007248 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007249
7250 if (INTEL_INFO(dev)->gen >= 5) {
7251 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7252 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7253 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7254 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007255 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7256 * for gen < 8) and if DRRS is supported (to make sure the
7257 * registers are not unnecessarily accessed).
7258 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307259 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007260 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007261 I915_WRITE(PIPE_DATA_M2(transcoder),
7262 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7263 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7264 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7265 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7266 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007267 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007268 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7269 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7270 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7271 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007272 }
7273}
7274
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307275void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007276{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307277 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7278
7279 if (m_n == M1_N1) {
7280 dp_m_n = &crtc->config->dp_m_n;
7281 dp_m2_n2 = &crtc->config->dp_m2_n2;
7282 } else if (m_n == M2_N2) {
7283
7284 /*
7285 * M2_N2 registers are not supported. Hence m2_n2 divider value
7286 * needs to be programmed into M1_N1.
7287 */
7288 dp_m_n = &crtc->config->dp_m2_n2;
7289 } else {
7290 DRM_ERROR("Unsupported divider value\n");
7291 return;
7292 }
7293
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007294 if (crtc->config->has_pch_encoder)
7295 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007296 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307297 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007298}
7299
Daniel Vetter251ac862015-06-18 10:30:24 +02007300static void vlv_compute_dpll(struct intel_crtc *crtc,
7301 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007302{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007303 u32 dpll, dpll_md;
7304
7305 /*
7306 * Enable DPIO clock input. We should never disable the reference
7307 * clock for pipe B, since VGA hotplug / manual detection depends
7308 * on it.
7309 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007310 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7311 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007312 /* We should never disable this, set it here for state tracking */
7313 if (crtc->pipe == PIPE_B)
7314 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7315 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007316 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007317
Ville Syrjäläd288f652014-10-28 13:20:22 +02007318 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007319 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007320 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007321}
7322
Ville Syrjäläd288f652014-10-28 13:20:22 +02007323static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007324 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007325{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007326 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007327 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007328 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007329 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007330 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007331 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007332
Ville Syrjäläa5805162015-05-26 20:42:30 +03007333 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007334
Ville Syrjäläd288f652014-10-28 13:20:22 +02007335 bestn = pipe_config->dpll.n;
7336 bestm1 = pipe_config->dpll.m1;
7337 bestm2 = pipe_config->dpll.m2;
7338 bestp1 = pipe_config->dpll.p1;
7339 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007340
Jesse Barnes89b667f2013-04-18 14:51:36 -07007341 /* See eDP HDMI DPIO driver vbios notes doc */
7342
7343 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007344 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007345 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007346
7347 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007348 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007349
7350 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007351 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007352 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007353 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007354
7355 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007356 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007357
7358 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007359 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7360 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7361 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007362 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007363
7364 /*
7365 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7366 * but we don't support that).
7367 * Note: don't use the DAC post divider as it seems unstable.
7368 */
7369 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007370 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007371
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007372 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007373 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007374
Jesse Barnes89b667f2013-04-18 14:51:36 -07007375 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007376 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007377 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7378 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007380 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007381 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007382 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007383 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007384
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007385 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007386 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007387 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007388 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007389 0x0df40000);
7390 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007391 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007392 0x0df70000);
7393 } else { /* HDMI or VGA */
7394 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007395 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007396 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007397 0x0df70000);
7398 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007399 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007400 0x0df40000);
7401 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007402
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007403 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007404 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007405 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7406 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007407 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007408 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007409
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007410 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007411 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007412}
7413
Daniel Vetter251ac862015-06-18 10:30:24 +02007414static void chv_compute_dpll(struct intel_crtc *crtc,
7415 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007416{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007417 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7418 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007419 DPLL_VCO_ENABLE;
7420 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007421 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007422
Ville Syrjäläd288f652014-10-28 13:20:22 +02007423 pipe_config->dpll_hw_state.dpll_md =
7424 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007425}
7426
Ville Syrjäläd288f652014-10-28 13:20:22 +02007427static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007428 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007429{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007430 struct drm_device *dev = crtc->base.dev;
7431 struct drm_i915_private *dev_priv = dev->dev_private;
7432 int pipe = crtc->pipe;
7433 int dpll_reg = DPLL(crtc->pipe);
7434 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307435 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007436 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307437 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307438 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007439
Ville Syrjäläd288f652014-10-28 13:20:22 +02007440 bestn = pipe_config->dpll.n;
7441 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7442 bestm1 = pipe_config->dpll.m1;
7443 bestm2 = pipe_config->dpll.m2 >> 22;
7444 bestp1 = pipe_config->dpll.p1;
7445 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307446 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307447 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307448 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007449
7450 /*
7451 * Enable Refclk and SSC
7452 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007453 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007454 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007455
Ville Syrjäläa5805162015-05-26 20:42:30 +03007456 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007457
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007458 /* p1 and p2 divider */
7459 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7460 5 << DPIO_CHV_S1_DIV_SHIFT |
7461 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7462 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7463 1 << DPIO_CHV_K_DIV_SHIFT);
7464
7465 /* Feedback post-divider - m2 */
7466 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7467
7468 /* Feedback refclk divider - n and m1 */
7469 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7470 DPIO_CHV_M1_DIV_BY_2 |
7471 1 << DPIO_CHV_N_DIV_SHIFT);
7472
7473 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307474 if (bestm2_frac)
7475 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007476
7477 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307478 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7479 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7480 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7481 if (bestm2_frac)
7482 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7483 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007484
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307485 /* Program digital lock detect threshold */
7486 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7487 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7488 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7489 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7490 if (!bestm2_frac)
7491 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7492 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7493
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007494 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307495 if (vco == 5400000) {
7496 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7497 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7498 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7499 tribuf_calcntr = 0x9;
7500 } else if (vco <= 6200000) {
7501 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7502 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7503 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7504 tribuf_calcntr = 0x9;
7505 } else if (vco <= 6480000) {
7506 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7507 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7508 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7509 tribuf_calcntr = 0x8;
7510 } else {
7511 /* Not supported. Apply the same limits as in the max case */
7512 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7513 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7514 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7515 tribuf_calcntr = 0;
7516 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007517 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7518
Ville Syrjälä968040b2015-03-11 22:52:08 +02007519 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307520 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7521 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7522 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7523
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007524 /* AFC Recal */
7525 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7526 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7527 DPIO_AFC_RECAL);
7528
Ville Syrjäläa5805162015-05-26 20:42:30 +03007529 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007530}
7531
Ville Syrjäläd288f652014-10-28 13:20:22 +02007532/**
7533 * vlv_force_pll_on - forcibly enable just the PLL
7534 * @dev_priv: i915 private structure
7535 * @pipe: pipe PLL to enable
7536 * @dpll: PLL configuration
7537 *
7538 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7539 * in cases where we need the PLL enabled even when @pipe is not going to
7540 * be enabled.
7541 */
7542void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7543 const struct dpll *dpll)
7544{
7545 struct intel_crtc *crtc =
7546 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007547 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007548 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007549 .pixel_multiplier = 1,
7550 .dpll = *dpll,
7551 };
7552
7553 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007554 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007555 chv_prepare_pll(crtc, &pipe_config);
7556 chv_enable_pll(crtc, &pipe_config);
7557 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007558 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007559 vlv_prepare_pll(crtc, &pipe_config);
7560 vlv_enable_pll(crtc, &pipe_config);
7561 }
7562}
7563
7564/**
7565 * vlv_force_pll_off - forcibly disable just the PLL
7566 * @dev_priv: i915 private structure
7567 * @pipe: pipe PLL to disable
7568 *
7569 * Disable the PLL for @pipe. To be used in cases where we need
7570 * the PLL enabled even when @pipe is not going to be enabled.
7571 */
7572void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7573{
7574 if (IS_CHERRYVIEW(dev))
7575 chv_disable_pll(to_i915(dev), pipe);
7576 else
7577 vlv_disable_pll(to_i915(dev), pipe);
7578}
7579
Daniel Vetter251ac862015-06-18 10:30:24 +02007580static void i9xx_compute_dpll(struct intel_crtc *crtc,
7581 struct intel_crtc_state *crtc_state,
7582 intel_clock_t *reduced_clock,
7583 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007584{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007585 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007586 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007587 u32 dpll;
7588 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007589 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007590
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007591 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307592
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007593 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7594 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007595
7596 dpll = DPLL_VGA_MODE_DIS;
7597
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007598 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007599 dpll |= DPLLB_MODE_LVDS;
7600 else
7601 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007602
Daniel Vetteref1b4602013-06-01 17:17:04 +02007603 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007604 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007605 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007606 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007607
7608 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007609 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007610
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007611 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007612 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007613
7614 /* compute bitmask from p1 value */
7615 if (IS_PINEVIEW(dev))
7616 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7617 else {
7618 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7619 if (IS_G4X(dev) && reduced_clock)
7620 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7621 }
7622 switch (clock->p2) {
7623 case 5:
7624 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7625 break;
7626 case 7:
7627 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7628 break;
7629 case 10:
7630 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7631 break;
7632 case 14:
7633 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7634 break;
7635 }
7636 if (INTEL_INFO(dev)->gen >= 4)
7637 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7638
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007639 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007640 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007641 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007642 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7643 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7644 else
7645 dpll |= PLL_REF_INPUT_DREFCLK;
7646
7647 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007648 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007649
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007650 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007651 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007652 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007653 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007654 }
7655}
7656
Daniel Vetter251ac862015-06-18 10:30:24 +02007657static void i8xx_compute_dpll(struct intel_crtc *crtc,
7658 struct intel_crtc_state *crtc_state,
7659 intel_clock_t *reduced_clock,
7660 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007661{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007662 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007663 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007664 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007665 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007666
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007667 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307668
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007669 dpll = DPLL_VGA_MODE_DIS;
7670
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007671 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007672 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7673 } else {
7674 if (clock->p1 == 2)
7675 dpll |= PLL_P1_DIVIDE_BY_TWO;
7676 else
7677 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7678 if (clock->p2 == 4)
7679 dpll |= PLL_P2_DIVIDE_BY_4;
7680 }
7681
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007682 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007683 dpll |= DPLL_DVO_2X_MODE;
7684
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007685 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007686 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7687 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7688 else
7689 dpll |= PLL_REF_INPUT_DREFCLK;
7690
7691 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007692 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007693}
7694
Daniel Vetter8a654f32013-06-01 17:16:22 +02007695static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007696{
7697 struct drm_device *dev = intel_crtc->base.dev;
7698 struct drm_i915_private *dev_priv = dev->dev_private;
7699 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007700 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007701 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007702 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007703 uint32_t crtc_vtotal, crtc_vblank_end;
7704 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007705
7706 /* We need to be careful not to changed the adjusted mode, for otherwise
7707 * the hw state checker will get angry at the mismatch. */
7708 crtc_vtotal = adjusted_mode->crtc_vtotal;
7709 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007710
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007711 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007712 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007713 crtc_vtotal -= 1;
7714 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007715
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007716 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007717 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7718 else
7719 vsyncshift = adjusted_mode->crtc_hsync_start -
7720 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007721 if (vsyncshift < 0)
7722 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007723 }
7724
7725 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007726 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007727
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007728 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007729 (adjusted_mode->crtc_hdisplay - 1) |
7730 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007731 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007732 (adjusted_mode->crtc_hblank_start - 1) |
7733 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007734 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007735 (adjusted_mode->crtc_hsync_start - 1) |
7736 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7737
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007738 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007739 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007740 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007741 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007742 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007743 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007744 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007745 (adjusted_mode->crtc_vsync_start - 1) |
7746 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7747
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007748 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7749 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7750 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7751 * bits. */
7752 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7753 (pipe == PIPE_B || pipe == PIPE_C))
7754 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7755
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007756 /* pipesrc controls the size that is scaled from, which should
7757 * always be the user's requested size.
7758 */
7759 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007760 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7761 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007762}
7763
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007764static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007765 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007766{
7767 struct drm_device *dev = crtc->base.dev;
7768 struct drm_i915_private *dev_priv = dev->dev_private;
7769 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7770 uint32_t tmp;
7771
7772 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007773 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7774 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007775 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007776 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7777 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007778 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007779 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7780 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007781
7782 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007783 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7784 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007785 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007786 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7787 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007788 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007789 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7790 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007791
7792 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007793 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7794 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7795 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007796 }
7797
7798 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007799 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7800 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7801
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007802 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7803 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007804}
7805
Daniel Vetterf6a83282014-02-11 15:28:57 -08007806void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007807 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007808{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007809 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7810 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7811 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7812 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007813
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007814 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7815 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7816 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7817 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007818
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007819 mode->flags = pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007820 mode->type = DRM_MODE_TYPE_DRIVER;
Jesse Barnesbabea612013-06-26 18:57:38 +03007821
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007822 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7823 mode->flags |= pipe_config->base.adjusted_mode.flags;
Maarten Lankhorstcd13f5a2015-07-14 14:12:02 +02007824
7825 mode->hsync = drm_mode_hsync(mode);
7826 mode->vrefresh = drm_mode_vrefresh(mode);
7827 drm_mode_set_name(mode);
Jesse Barnesbabea612013-06-26 18:57:38 +03007828}
7829
Daniel Vetter84b046f2013-02-19 18:48:54 +01007830static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7831{
7832 struct drm_device *dev = intel_crtc->base.dev;
7833 struct drm_i915_private *dev_priv = dev->dev_private;
7834 uint32_t pipeconf;
7835
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007836 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007837
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007838 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7839 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7840 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007841
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007842 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007843 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007844
Daniel Vetterff9ce462013-04-24 14:57:17 +02007845 /* only g4x and later have fancy bpc/dither controls */
7846 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007847 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007848 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007849 pipeconf |= PIPECONF_DITHER_EN |
7850 PIPECONF_DITHER_TYPE_SP;
7851
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007852 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007853 case 18:
7854 pipeconf |= PIPECONF_6BPC;
7855 break;
7856 case 24:
7857 pipeconf |= PIPECONF_8BPC;
7858 break;
7859 case 30:
7860 pipeconf |= PIPECONF_10BPC;
7861 break;
7862 default:
7863 /* Case prevented by intel_choose_pipe_bpp_dither. */
7864 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007865 }
7866 }
7867
7868 if (HAS_PIPE_CXSR(dev)) {
7869 if (intel_crtc->lowfreq_avail) {
7870 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7871 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7872 } else {
7873 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007874 }
7875 }
7876
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007877 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007878 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007879 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007880 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7881 else
7882 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7883 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007884 pipeconf |= PIPECONF_PROGRESSIVE;
7885
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007886 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007887 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007888
Daniel Vetter84b046f2013-02-19 18:48:54 +01007889 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7890 POSTING_READ(PIPECONF(intel_crtc->pipe));
7891}
7892
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007893static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7894 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007895{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007896 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007897 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007898 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007899 intel_clock_t clock;
7900 bool ok;
7901 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007902 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007903 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007904 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007905 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007906 struct drm_connector_state *connector_state;
7907 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007908
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007909 memset(&crtc_state->dpll_hw_state, 0,
7910 sizeof(crtc_state->dpll_hw_state));
7911
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007912 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007913 if (connector_state->crtc != &crtc->base)
7914 continue;
7915
7916 encoder = to_intel_encoder(connector_state->best_encoder);
7917
Chris Wilson5eddb702010-09-11 13:48:45 +01007918 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007919 case INTEL_OUTPUT_DSI:
7920 is_dsi = true;
7921 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007922 default:
7923 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007924 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007925
Eric Anholtc751ce42010-03-25 11:48:48 -07007926 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007927 }
7928
Jani Nikulaf2335332013-09-13 11:03:09 +03007929 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007930 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007931
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007932 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007933 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007934
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007935 /*
7936 * Returns a set of divisors for the desired target clock with
7937 * the given refclk, or FALSE. The returned values represent
7938 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7939 * 2) / p1 / p2.
7940 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007941 limit = intel_limit(crtc_state, refclk);
7942 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007943 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007944 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007945 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007946 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7947 return -EINVAL;
7948 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007949
Jani Nikulaf2335332013-09-13 11:03:09 +03007950 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007951 crtc_state->dpll.n = clock.n;
7952 crtc_state->dpll.m1 = clock.m1;
7953 crtc_state->dpll.m2 = clock.m2;
7954 crtc_state->dpll.p1 = clock.p1;
7955 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007956 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007957
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007958 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007959 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007960 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007961 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007962 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007963 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007964 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007965 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007966 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007967 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007968 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007969
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007970 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007971}
7972
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007973static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007974 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007975{
7976 struct drm_device *dev = crtc->base.dev;
7977 struct drm_i915_private *dev_priv = dev->dev_private;
7978 uint32_t tmp;
7979
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007980 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7981 return;
7982
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007983 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007984 if (!(tmp & PFIT_ENABLE))
7985 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007986
Daniel Vetter06922822013-07-11 13:35:40 +02007987 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007988 if (INTEL_INFO(dev)->gen < 4) {
7989 if (crtc->pipe != PIPE_B)
7990 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007991 } else {
7992 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7993 return;
7994 }
7995
Daniel Vetter06922822013-07-11 13:35:40 +02007996 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007997 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7998 if (INTEL_INFO(dev)->gen < 5)
7999 pipe_config->gmch_pfit.lvds_border_bits =
8000 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8001}
8002
Jesse Barnesacbec812013-09-20 11:29:32 -07008003static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008004 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008005{
8006 struct drm_device *dev = crtc->base.dev;
8007 struct drm_i915_private *dev_priv = dev->dev_private;
8008 int pipe = pipe_config->cpu_transcoder;
8009 intel_clock_t clock;
8010 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008011 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008012
Shobhit Kumarf573de52014-07-30 20:32:37 +05308013 /* In case of MIPI DPLL will not even be used */
8014 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8015 return;
8016
Ville Syrjäläa5805162015-05-26 20:42:30 +03008017 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008018 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008019 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008020
8021 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8022 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8023 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8024 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8025 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8026
Imre Deakdccbea32015-06-22 23:35:51 +03008027 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008028}
8029
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008030static void
8031i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8032 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008033{
8034 struct drm_device *dev = crtc->base.dev;
8035 struct drm_i915_private *dev_priv = dev->dev_private;
8036 u32 val, base, offset;
8037 int pipe = crtc->pipe, plane = crtc->plane;
8038 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008039 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008040 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008041 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008042
Damien Lespiau42a7b082015-02-05 19:35:13 +00008043 val = I915_READ(DSPCNTR(plane));
8044 if (!(val & DISPLAY_PLANE_ENABLE))
8045 return;
8046
Damien Lespiaud9806c92015-01-21 14:07:19 +00008047 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008048 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008049 DRM_DEBUG_KMS("failed to alloc fb\n");
8050 return;
8051 }
8052
Damien Lespiau1b842c82015-01-21 13:50:54 +00008053 fb = &intel_fb->base;
8054
Daniel Vetter18c52472015-02-10 17:16:09 +00008055 if (INTEL_INFO(dev)->gen >= 4) {
8056 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008057 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008058 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8059 }
8060 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008061
8062 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008063 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008064 fb->pixel_format = fourcc;
8065 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008066
8067 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008068 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008069 offset = I915_READ(DSPTILEOFF(plane));
8070 else
8071 offset = I915_READ(DSPLINOFF(plane));
8072 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8073 } else {
8074 base = I915_READ(DSPADDR(plane));
8075 }
8076 plane_config->base = base;
8077
8078 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008079 fb->width = ((val >> 16) & 0xfff) + 1;
8080 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008081
8082 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008083 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008084
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008085 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008086 fb->pixel_format,
8087 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008088
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008089 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008090
Damien Lespiau2844a922015-01-20 12:51:48 +00008091 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8092 pipe_name(pipe), plane, fb->width, fb->height,
8093 fb->bits_per_pixel, base, fb->pitches[0],
8094 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008095
Damien Lespiau2d140302015-02-05 17:22:18 +00008096 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008097}
8098
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008099static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008100 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008101{
8102 struct drm_device *dev = crtc->base.dev;
8103 struct drm_i915_private *dev_priv = dev->dev_private;
8104 int pipe = pipe_config->cpu_transcoder;
8105 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8106 intel_clock_t clock;
8107 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8108 int refclk = 100000;
8109
Ville Syrjäläa5805162015-05-26 20:42:30 +03008110 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008111 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8112 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8113 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8114 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008115 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008116
8117 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8118 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8119 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8120 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8121 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8122
Imre Deakdccbea32015-06-22 23:35:51 +03008123 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008124}
8125
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008126static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008127 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008128{
8129 struct drm_device *dev = crtc->base.dev;
8130 struct drm_i915_private *dev_priv = dev->dev_private;
8131 uint32_t tmp;
8132
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008133 if (!intel_display_power_is_enabled(dev_priv,
8134 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008135 return false;
8136
Daniel Vettere143a212013-07-04 12:01:15 +02008137 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008138 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008139
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008140 tmp = I915_READ(PIPECONF(crtc->pipe));
8141 if (!(tmp & PIPECONF_ENABLE))
8142 return false;
8143
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008144 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8145 switch (tmp & PIPECONF_BPC_MASK) {
8146 case PIPECONF_6BPC:
8147 pipe_config->pipe_bpp = 18;
8148 break;
8149 case PIPECONF_8BPC:
8150 pipe_config->pipe_bpp = 24;
8151 break;
8152 case PIPECONF_10BPC:
8153 pipe_config->pipe_bpp = 30;
8154 break;
8155 default:
8156 break;
8157 }
8158 }
8159
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008160 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8161 pipe_config->limited_color_range = true;
8162
Ville Syrjälä282740f2013-09-04 18:30:03 +03008163 if (INTEL_INFO(dev)->gen < 4)
8164 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8165
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008166 intel_get_pipe_timings(crtc, pipe_config);
8167
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008168 i9xx_get_pfit_config(crtc, pipe_config);
8169
Daniel Vetter6c49f242013-06-06 12:45:25 +02008170 if (INTEL_INFO(dev)->gen >= 4) {
8171 tmp = I915_READ(DPLL_MD(crtc->pipe));
8172 pipe_config->pixel_multiplier =
8173 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8174 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008175 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008176 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8177 tmp = I915_READ(DPLL(crtc->pipe));
8178 pipe_config->pixel_multiplier =
8179 ((tmp & SDVO_MULTIPLIER_MASK)
8180 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8181 } else {
8182 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8183 * port and will be fixed up in the encoder->get_config
8184 * function. */
8185 pipe_config->pixel_multiplier = 1;
8186 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008187 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8188 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008189 /*
8190 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8191 * on 830. Filter it out here so that we don't
8192 * report errors due to that.
8193 */
8194 if (IS_I830(dev))
8195 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8196
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008197 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8198 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008199 } else {
8200 /* Mask out read-only status bits. */
8201 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8202 DPLL_PORTC_READY_MASK |
8203 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008204 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008205
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008206 if (IS_CHERRYVIEW(dev))
8207 chv_crtc_clock_get(crtc, pipe_config);
8208 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008209 vlv_crtc_clock_get(crtc, pipe_config);
8210 else
8211 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008212
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008213 return true;
8214}
8215
Paulo Zanonidde86e22012-12-01 12:04:25 -02008216static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008217{
8218 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008219 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008220 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008221 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008222 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008223 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008224 bool has_ck505 = false;
8225 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008226
8227 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008228 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008229 switch (encoder->type) {
8230 case INTEL_OUTPUT_LVDS:
8231 has_panel = true;
8232 has_lvds = true;
8233 break;
8234 case INTEL_OUTPUT_EDP:
8235 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008236 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008237 has_cpu_edp = true;
8238 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008239 default:
8240 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008241 }
8242 }
8243
Keith Packard99eb6a02011-09-26 14:29:12 -07008244 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008245 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008246 can_ssc = has_ck505;
8247 } else {
8248 has_ck505 = false;
8249 can_ssc = true;
8250 }
8251
Imre Deak2de69052013-05-08 13:14:04 +03008252 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8253 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008254
8255 /* Ironlake: try to setup display ref clock before DPLL
8256 * enabling. This is only under driver's control after
8257 * PCH B stepping, previous chipset stepping should be
8258 * ignoring this setting.
8259 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008260 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008261
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008262 /* As we must carefully and slowly disable/enable each source in turn,
8263 * compute the final state we want first and check if we need to
8264 * make any changes at all.
8265 */
8266 final = val;
8267 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008268 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008269 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008270 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008271 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8272
8273 final &= ~DREF_SSC_SOURCE_MASK;
8274 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8275 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008276
Keith Packard199e5d72011-09-22 12:01:57 -07008277 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008278 final |= DREF_SSC_SOURCE_ENABLE;
8279
8280 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8281 final |= DREF_SSC1_ENABLE;
8282
8283 if (has_cpu_edp) {
8284 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8285 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8286 else
8287 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8288 } else
8289 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8290 } else {
8291 final |= DREF_SSC_SOURCE_DISABLE;
8292 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8293 }
8294
8295 if (final == val)
8296 return;
8297
8298 /* Always enable nonspread source */
8299 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8300
8301 if (has_ck505)
8302 val |= DREF_NONSPREAD_CK505_ENABLE;
8303 else
8304 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8305
8306 if (has_panel) {
8307 val &= ~DREF_SSC_SOURCE_MASK;
8308 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008309
Keith Packard199e5d72011-09-22 12:01:57 -07008310 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008311 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008312 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008313 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008314 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008315 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008316
8317 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008318 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008319 POSTING_READ(PCH_DREF_CONTROL);
8320 udelay(200);
8321
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008322 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008323
8324 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008325 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008326 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008327 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008328 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008329 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008330 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008331 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008332 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008333
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008334 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008335 POSTING_READ(PCH_DREF_CONTROL);
8336 udelay(200);
8337 } else {
8338 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8339
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008340 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008341
8342 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008343 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008344
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008345 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008346 POSTING_READ(PCH_DREF_CONTROL);
8347 udelay(200);
8348
8349 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008350 val &= ~DREF_SSC_SOURCE_MASK;
8351 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008352
8353 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008354 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008355
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008356 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008357 POSTING_READ(PCH_DREF_CONTROL);
8358 udelay(200);
8359 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008360
8361 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008362}
8363
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008364static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008365{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008366 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008367
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008368 tmp = I915_READ(SOUTH_CHICKEN2);
8369 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8370 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008371
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008372 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8373 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8374 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008375
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008376 tmp = I915_READ(SOUTH_CHICKEN2);
8377 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8378 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008379
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008380 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8381 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8382 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008383}
8384
8385/* WaMPhyProgramming:hsw */
8386static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8387{
8388 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008389
8390 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8391 tmp &= ~(0xFF << 24);
8392 tmp |= (0x12 << 24);
8393 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8394
Paulo Zanonidde86e22012-12-01 12:04:25 -02008395 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8396 tmp |= (1 << 11);
8397 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8398
8399 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8400 tmp |= (1 << 11);
8401 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8402
Paulo Zanonidde86e22012-12-01 12:04:25 -02008403 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8404 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8405 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8406
8407 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8408 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8409 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8410
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008411 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8412 tmp &= ~(7 << 13);
8413 tmp |= (5 << 13);
8414 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008415
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008416 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8417 tmp &= ~(7 << 13);
8418 tmp |= (5 << 13);
8419 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008420
8421 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8422 tmp &= ~0xFF;
8423 tmp |= 0x1C;
8424 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8425
8426 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8427 tmp &= ~0xFF;
8428 tmp |= 0x1C;
8429 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8430
8431 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8432 tmp &= ~(0xFF << 16);
8433 tmp |= (0x1C << 16);
8434 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8435
8436 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8437 tmp &= ~(0xFF << 16);
8438 tmp |= (0x1C << 16);
8439 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8440
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008441 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8442 tmp |= (1 << 27);
8443 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008444
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008445 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8446 tmp |= (1 << 27);
8447 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008448
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008449 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8450 tmp &= ~(0xF << 28);
8451 tmp |= (4 << 28);
8452 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008453
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008454 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8455 tmp &= ~(0xF << 28);
8456 tmp |= (4 << 28);
8457 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008458}
8459
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008460/* Implements 3 different sequences from BSpec chapter "Display iCLK
8461 * Programming" based on the parameters passed:
8462 * - Sequence to enable CLKOUT_DP
8463 * - Sequence to enable CLKOUT_DP without spread
8464 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8465 */
8466static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8467 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008468{
8469 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008470 uint32_t reg, tmp;
8471
8472 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8473 with_spread = true;
8474 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8475 with_fdi, "LP PCH doesn't have FDI\n"))
8476 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008477
Ville Syrjäläa5805162015-05-26 20:42:30 +03008478 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008479
8480 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8481 tmp &= ~SBI_SSCCTL_DISABLE;
8482 tmp |= SBI_SSCCTL_PATHALT;
8483 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8484
8485 udelay(24);
8486
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008487 if (with_spread) {
8488 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8489 tmp &= ~SBI_SSCCTL_PATHALT;
8490 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008491
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008492 if (with_fdi) {
8493 lpt_reset_fdi_mphy(dev_priv);
8494 lpt_program_fdi_mphy(dev_priv);
8495 }
8496 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008497
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008498 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8499 SBI_GEN0 : SBI_DBUFF0;
8500 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8501 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8502 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008503
Ville Syrjäläa5805162015-05-26 20:42:30 +03008504 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008505}
8506
Paulo Zanoni47701c32013-07-23 11:19:25 -03008507/* Sequence to disable CLKOUT_DP */
8508static void lpt_disable_clkout_dp(struct drm_device *dev)
8509{
8510 struct drm_i915_private *dev_priv = dev->dev_private;
8511 uint32_t reg, tmp;
8512
Ville Syrjäläa5805162015-05-26 20:42:30 +03008513 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008514
8515 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8516 SBI_GEN0 : SBI_DBUFF0;
8517 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8518 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8519 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8520
8521 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8522 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8523 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8524 tmp |= SBI_SSCCTL_PATHALT;
8525 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8526 udelay(32);
8527 }
8528 tmp |= SBI_SSCCTL_DISABLE;
8529 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8530 }
8531
Ville Syrjäläa5805162015-05-26 20:42:30 +03008532 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008533}
8534
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008535static void lpt_init_pch_refclk(struct drm_device *dev)
8536{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008537 struct intel_encoder *encoder;
8538 bool has_vga = false;
8539
Damien Lespiaub2784e12014-08-05 11:29:37 +01008540 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008541 switch (encoder->type) {
8542 case INTEL_OUTPUT_ANALOG:
8543 has_vga = true;
8544 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008545 default:
8546 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008547 }
8548 }
8549
Paulo Zanoni47701c32013-07-23 11:19:25 -03008550 if (has_vga)
8551 lpt_enable_clkout_dp(dev, true, true);
8552 else
8553 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008554}
8555
Paulo Zanonidde86e22012-12-01 12:04:25 -02008556/*
8557 * Initialize reference clocks when the driver loads
8558 */
8559void intel_init_pch_refclk(struct drm_device *dev)
8560{
8561 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8562 ironlake_init_pch_refclk(dev);
8563 else if (HAS_PCH_LPT(dev))
8564 lpt_init_pch_refclk(dev);
8565}
8566
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008567static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008568{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008569 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008570 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008571 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008572 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008573 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008574 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008575 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008576 bool is_lvds = false;
8577
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008578 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008579 if (connector_state->crtc != crtc_state->base.crtc)
8580 continue;
8581
8582 encoder = to_intel_encoder(connector_state->best_encoder);
8583
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008584 switch (encoder->type) {
8585 case INTEL_OUTPUT_LVDS:
8586 is_lvds = true;
8587 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008588 default:
8589 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008590 }
8591 num_connectors++;
8592 }
8593
8594 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008595 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008596 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008597 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008598 }
8599
8600 return 120000;
8601}
8602
Daniel Vetter6ff93602013-04-19 11:24:36 +02008603static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008604{
8605 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8606 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8607 int pipe = intel_crtc->pipe;
8608 uint32_t val;
8609
Daniel Vetter78114072013-06-13 00:54:57 +02008610 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008611
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008612 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008613 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008614 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008615 break;
8616 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008617 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008618 break;
8619 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008620 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008621 break;
8622 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008623 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008624 break;
8625 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008626 /* Case prevented by intel_choose_pipe_bpp_dither. */
8627 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008628 }
8629
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008630 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008631 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8632
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008633 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008634 val |= PIPECONF_INTERLACED_ILK;
8635 else
8636 val |= PIPECONF_PROGRESSIVE;
8637
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008638 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008639 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008640
Paulo Zanonic8203562012-09-12 10:06:29 -03008641 I915_WRITE(PIPECONF(pipe), val);
8642 POSTING_READ(PIPECONF(pipe));
8643}
8644
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008645/*
8646 * Set up the pipe CSC unit.
8647 *
8648 * Currently only full range RGB to limited range RGB conversion
8649 * is supported, but eventually this should handle various
8650 * RGB<->YCbCr scenarios as well.
8651 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008652static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008653{
8654 struct drm_device *dev = crtc->dev;
8655 struct drm_i915_private *dev_priv = dev->dev_private;
8656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8657 int pipe = intel_crtc->pipe;
8658 uint16_t coeff = 0x7800; /* 1.0 */
8659
8660 /*
8661 * TODO: Check what kind of values actually come out of the pipe
8662 * with these coeff/postoff values and adjust to get the best
8663 * accuracy. Perhaps we even need to take the bpc value into
8664 * consideration.
8665 */
8666
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008667 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008668 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8669
8670 /*
8671 * GY/GU and RY/RU should be the other way around according
8672 * to BSpec, but reality doesn't agree. Just set them up in
8673 * a way that results in the correct picture.
8674 */
8675 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8676 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8677
8678 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8679 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8680
8681 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8682 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8683
8684 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8685 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8686 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8687
8688 if (INTEL_INFO(dev)->gen > 6) {
8689 uint16_t postoff = 0;
8690
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008691 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008692 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008693
8694 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8695 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8696 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8697
8698 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8699 } else {
8700 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8701
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008702 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008703 mode |= CSC_BLACK_SCREEN_OFFSET;
8704
8705 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8706 }
8707}
8708
Daniel Vetter6ff93602013-04-19 11:24:36 +02008709static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008710{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008711 struct drm_device *dev = crtc->dev;
8712 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008713 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008714 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008715 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008716 uint32_t val;
8717
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008718 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008719
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008720 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008721 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8722
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008723 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008724 val |= PIPECONF_INTERLACED_ILK;
8725 else
8726 val |= PIPECONF_PROGRESSIVE;
8727
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008728 I915_WRITE(PIPECONF(cpu_transcoder), val);
8729 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008730
8731 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8732 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008733
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308734 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008735 val = 0;
8736
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008737 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008738 case 18:
8739 val |= PIPEMISC_DITHER_6_BPC;
8740 break;
8741 case 24:
8742 val |= PIPEMISC_DITHER_8_BPC;
8743 break;
8744 case 30:
8745 val |= PIPEMISC_DITHER_10_BPC;
8746 break;
8747 case 36:
8748 val |= PIPEMISC_DITHER_12_BPC;
8749 break;
8750 default:
8751 /* Case prevented by pipe_config_set_bpp. */
8752 BUG();
8753 }
8754
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008755 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008756 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8757
8758 I915_WRITE(PIPEMISC(pipe), val);
8759 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008760}
8761
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008762static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008763 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008764 intel_clock_t *clock,
8765 bool *has_reduced_clock,
8766 intel_clock_t *reduced_clock)
8767{
8768 struct drm_device *dev = crtc->dev;
8769 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008770 int refclk;
8771 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008772 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008773
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008774 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008775
8776 /*
8777 * Returns a set of divisors for the desired target clock with the given
8778 * refclk, or FALSE. The returned values represent the clock equation:
8779 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8780 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008781 limit = intel_limit(crtc_state, refclk);
8782 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008783 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008784 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008785 if (!ret)
8786 return false;
8787
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008788 return true;
8789}
8790
Paulo Zanonid4b19312012-11-29 11:29:32 -02008791int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8792{
8793 /*
8794 * Account for spread spectrum to avoid
8795 * oversubscribing the link. Max center spread
8796 * is 2.5%; use 5% for safety's sake.
8797 */
8798 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008799 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008800}
8801
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008802static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008803{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008804 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008805}
8806
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008807static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008808 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008809 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008810 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008811{
8812 struct drm_crtc *crtc = &intel_crtc->base;
8813 struct drm_device *dev = crtc->dev;
8814 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008815 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008816 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008817 struct drm_connector_state *connector_state;
8818 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008819 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008820 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008821 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008822
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008823 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008824 if (connector_state->crtc != crtc_state->base.crtc)
8825 continue;
8826
8827 encoder = to_intel_encoder(connector_state->best_encoder);
8828
8829 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008830 case INTEL_OUTPUT_LVDS:
8831 is_lvds = true;
8832 break;
8833 case INTEL_OUTPUT_SDVO:
8834 case INTEL_OUTPUT_HDMI:
8835 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008836 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008837 default:
8838 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008839 }
8840
8841 num_connectors++;
8842 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008843
Chris Wilsonc1858122010-12-03 21:35:48 +00008844 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008845 factor = 21;
8846 if (is_lvds) {
8847 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008848 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008849 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008850 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008851 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008852 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008853
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008854 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008855 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008856
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008857 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8858 *fp2 |= FP_CB_TUNE;
8859
Chris Wilson5eddb702010-09-11 13:48:45 +01008860 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008861
Eric Anholta07d6782011-03-30 13:01:08 -07008862 if (is_lvds)
8863 dpll |= DPLLB_MODE_LVDS;
8864 else
8865 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008866
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008867 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008868 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008869
8870 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008871 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008872 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008873 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008874
Eric Anholta07d6782011-03-30 13:01:08 -07008875 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008876 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008877 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008878 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008879
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008880 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008881 case 5:
8882 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8883 break;
8884 case 7:
8885 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8886 break;
8887 case 10:
8888 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8889 break;
8890 case 14:
8891 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8892 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008893 }
8894
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008895 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008896 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008897 else
8898 dpll |= PLL_REF_INPUT_DREFCLK;
8899
Daniel Vetter959e16d2013-06-05 13:34:21 +02008900 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008901}
8902
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008903static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8904 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008905{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008906 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008907 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008908 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008909 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008910 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008911 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008912
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008913 memset(&crtc_state->dpll_hw_state, 0,
8914 sizeof(crtc_state->dpll_hw_state));
8915
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008916 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008917
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008918 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8919 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8920
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008921 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008922 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008923 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008924 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8925 return -EINVAL;
8926 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008927 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008928 if (!crtc_state->clock_set) {
8929 crtc_state->dpll.n = clock.n;
8930 crtc_state->dpll.m1 = clock.m1;
8931 crtc_state->dpll.m2 = clock.m2;
8932 crtc_state->dpll.p1 = clock.p1;
8933 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008934 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008935
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008936 /* 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 +02008937 if (crtc_state->has_pch_encoder) {
8938 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008939 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008940 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008941
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008942 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008943 &fp, &reduced_clock,
8944 has_reduced_clock ? &fp2 : NULL);
8945
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008946 crtc_state->dpll_hw_state.dpll = dpll;
8947 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008948 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008949 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008950 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008951 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008952
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008953 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008954 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008955 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008956 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008957 return -EINVAL;
8958 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008959 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008960
Rodrigo Viviab585de2015-03-24 12:40:09 -07008961 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008962 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008963 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008964 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008965
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008966 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008967}
8968
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008969static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8970 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008971{
8972 struct drm_device *dev = crtc->base.dev;
8973 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008974 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008975
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008976 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8977 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8978 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8979 & ~TU_SIZE_MASK;
8980 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8981 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8982 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8983}
8984
8985static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8986 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008987 struct intel_link_m_n *m_n,
8988 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008989{
8990 struct drm_device *dev = crtc->base.dev;
8991 struct drm_i915_private *dev_priv = dev->dev_private;
8992 enum pipe pipe = crtc->pipe;
8993
8994 if (INTEL_INFO(dev)->gen >= 5) {
8995 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8996 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8997 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8998 & ~TU_SIZE_MASK;
8999 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9000 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9001 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009002 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9003 * gen < 8) and if DRRS is supported (to make sure the
9004 * registers are not unnecessarily read).
9005 */
9006 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009007 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009008 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9009 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9010 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9011 & ~TU_SIZE_MASK;
9012 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9013 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9014 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9015 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009016 } else {
9017 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9018 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9019 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9020 & ~TU_SIZE_MASK;
9021 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9022 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9023 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9024 }
9025}
9026
9027void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009028 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009029{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009030 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009031 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9032 else
9033 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009034 &pipe_config->dp_m_n,
9035 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009036}
9037
Daniel Vetter72419202013-04-04 13:28:53 +02009038static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009039 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009040{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009041 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009042 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009043}
9044
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009045static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009046 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009047{
9048 struct drm_device *dev = crtc->base.dev;
9049 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009050 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9051 uint32_t ps_ctrl = 0;
9052 int id = -1;
9053 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009054
Chandra Kondurua1b22782015-04-07 15:28:45 -07009055 /* find scaler attached to this pipe */
9056 for (i = 0; i < crtc->num_scalers; i++) {
9057 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9058 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9059 id = i;
9060 pipe_config->pch_pfit.enabled = true;
9061 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9062 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9063 break;
9064 }
9065 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009066
Chandra Kondurua1b22782015-04-07 15:28:45 -07009067 scaler_state->scaler_id = id;
9068 if (id >= 0) {
9069 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9070 } else {
9071 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009072 }
9073}
9074
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009075static void
9076skylake_get_initial_plane_config(struct intel_crtc *crtc,
9077 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009078{
9079 struct drm_device *dev = crtc->base.dev;
9080 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009081 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009082 int pipe = crtc->pipe;
9083 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009084 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009085 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009086 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009087
Damien Lespiaud9806c92015-01-21 14:07:19 +00009088 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009089 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009090 DRM_DEBUG_KMS("failed to alloc fb\n");
9091 return;
9092 }
9093
Damien Lespiau1b842c82015-01-21 13:50:54 +00009094 fb = &intel_fb->base;
9095
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009096 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009097 if (!(val & PLANE_CTL_ENABLE))
9098 goto error;
9099
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009100 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9101 fourcc = skl_format_to_fourcc(pixel_format,
9102 val & PLANE_CTL_ORDER_RGBX,
9103 val & PLANE_CTL_ALPHA_MASK);
9104 fb->pixel_format = fourcc;
9105 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9106
Damien Lespiau40f46282015-02-27 11:15:21 +00009107 tiling = val & PLANE_CTL_TILED_MASK;
9108 switch (tiling) {
9109 case PLANE_CTL_TILED_LINEAR:
9110 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9111 break;
9112 case PLANE_CTL_TILED_X:
9113 plane_config->tiling = I915_TILING_X;
9114 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9115 break;
9116 case PLANE_CTL_TILED_Y:
9117 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9118 break;
9119 case PLANE_CTL_TILED_YF:
9120 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9121 break;
9122 default:
9123 MISSING_CASE(tiling);
9124 goto error;
9125 }
9126
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009127 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9128 plane_config->base = base;
9129
9130 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9131
9132 val = I915_READ(PLANE_SIZE(pipe, 0));
9133 fb->height = ((val >> 16) & 0xfff) + 1;
9134 fb->width = ((val >> 0) & 0x1fff) + 1;
9135
9136 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009137 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9138 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009139 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9140
9141 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009142 fb->pixel_format,
9143 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009144
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009145 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009146
9147 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9148 pipe_name(pipe), fb->width, fb->height,
9149 fb->bits_per_pixel, base, fb->pitches[0],
9150 plane_config->size);
9151
Damien Lespiau2d140302015-02-05 17:22:18 +00009152 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009153 return;
9154
9155error:
9156 kfree(fb);
9157}
9158
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009159static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009160 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009161{
9162 struct drm_device *dev = crtc->base.dev;
9163 struct drm_i915_private *dev_priv = dev->dev_private;
9164 uint32_t tmp;
9165
9166 tmp = I915_READ(PF_CTL(crtc->pipe));
9167
9168 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009169 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009170 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9171 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009172
9173 /* We currently do not free assignements of panel fitters on
9174 * ivb/hsw (since we don't use the higher upscaling modes which
9175 * differentiates them) so just WARN about this case for now. */
9176 if (IS_GEN7(dev)) {
9177 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9178 PF_PIPE_SEL_IVB(crtc->pipe));
9179 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009180 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009181}
9182
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009183static void
9184ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9185 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009186{
9187 struct drm_device *dev = crtc->base.dev;
9188 struct drm_i915_private *dev_priv = dev->dev_private;
9189 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009190 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009192 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009193 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009194 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009195
Damien Lespiau42a7b082015-02-05 19:35:13 +00009196 val = I915_READ(DSPCNTR(pipe));
9197 if (!(val & DISPLAY_PLANE_ENABLE))
9198 return;
9199
Damien Lespiaud9806c92015-01-21 14:07:19 +00009200 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009201 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009202 DRM_DEBUG_KMS("failed to alloc fb\n");
9203 return;
9204 }
9205
Damien Lespiau1b842c82015-01-21 13:50:54 +00009206 fb = &intel_fb->base;
9207
Daniel Vetter18c52472015-02-10 17:16:09 +00009208 if (INTEL_INFO(dev)->gen >= 4) {
9209 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009210 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009211 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9212 }
9213 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009214
9215 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009216 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009217 fb->pixel_format = fourcc;
9218 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009219
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009220 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009221 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009222 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009223 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009224 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009225 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009226 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009227 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009228 }
9229 plane_config->base = base;
9230
9231 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009232 fb->width = ((val >> 16) & 0xfff) + 1;
9233 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009234
9235 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009236 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009237
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009238 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009239 fb->pixel_format,
9240 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009241
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009242 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009243
Damien Lespiau2844a922015-01-20 12:51:48 +00009244 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9245 pipe_name(pipe), fb->width, fb->height,
9246 fb->bits_per_pixel, base, fb->pitches[0],
9247 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009248
Damien Lespiau2d140302015-02-05 17:22:18 +00009249 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009250}
9251
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009252static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009253 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009254{
9255 struct drm_device *dev = crtc->base.dev;
9256 struct drm_i915_private *dev_priv = dev->dev_private;
9257 uint32_t tmp;
9258
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009259 if (!intel_display_power_is_enabled(dev_priv,
9260 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009261 return false;
9262
Daniel Vettere143a212013-07-04 12:01:15 +02009263 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009264 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009265
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009266 tmp = I915_READ(PIPECONF(crtc->pipe));
9267 if (!(tmp & PIPECONF_ENABLE))
9268 return false;
9269
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009270 switch (tmp & PIPECONF_BPC_MASK) {
9271 case PIPECONF_6BPC:
9272 pipe_config->pipe_bpp = 18;
9273 break;
9274 case PIPECONF_8BPC:
9275 pipe_config->pipe_bpp = 24;
9276 break;
9277 case PIPECONF_10BPC:
9278 pipe_config->pipe_bpp = 30;
9279 break;
9280 case PIPECONF_12BPC:
9281 pipe_config->pipe_bpp = 36;
9282 break;
9283 default:
9284 break;
9285 }
9286
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009287 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9288 pipe_config->limited_color_range = true;
9289
Daniel Vetterab9412b2013-05-03 11:49:46 +02009290 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009291 struct intel_shared_dpll *pll;
9292
Daniel Vetter88adfff2013-03-28 10:42:01 +01009293 pipe_config->has_pch_encoder = true;
9294
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009295 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9296 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9297 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009298
9299 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009300
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009301 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009302 pipe_config->shared_dpll =
9303 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009304 } else {
9305 tmp = I915_READ(PCH_DPLL_SEL);
9306 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9307 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9308 else
9309 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9310 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009311
9312 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9313
9314 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9315 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009316
9317 tmp = pipe_config->dpll_hw_state.dpll;
9318 pipe_config->pixel_multiplier =
9319 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9320 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009321
9322 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009323 } else {
9324 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009325 }
9326
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009327 intel_get_pipe_timings(crtc, pipe_config);
9328
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009329 ironlake_get_pfit_config(crtc, pipe_config);
9330
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009331 return true;
9332}
9333
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009334static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9335{
9336 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009337 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009338
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009339 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009340 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009341 pipe_name(crtc->pipe));
9342
Rob Clarke2c719b2014-12-15 13:56:32 -05009343 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9344 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9345 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9346 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9347 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9348 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009349 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009350 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009351 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009352 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009353 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009354 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009355 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009356 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009357 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009358
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009359 /*
9360 * In theory we can still leave IRQs enabled, as long as only the HPD
9361 * interrupts remain enabled. We used to check for that, but since it's
9362 * gen-specific and since we only disable LCPLL after we fully disable
9363 * the interrupts, the check below should be enough.
9364 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009365 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009366}
9367
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009368static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9369{
9370 struct drm_device *dev = dev_priv->dev;
9371
9372 if (IS_HASWELL(dev))
9373 return I915_READ(D_COMP_HSW);
9374 else
9375 return I915_READ(D_COMP_BDW);
9376}
9377
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009378static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9379{
9380 struct drm_device *dev = dev_priv->dev;
9381
9382 if (IS_HASWELL(dev)) {
9383 mutex_lock(&dev_priv->rps.hw_lock);
9384 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9385 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009386 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009387 mutex_unlock(&dev_priv->rps.hw_lock);
9388 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009389 I915_WRITE(D_COMP_BDW, val);
9390 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009391 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009392}
9393
9394/*
9395 * This function implements pieces of two sequences from BSpec:
9396 * - Sequence for display software to disable LCPLL
9397 * - Sequence for display software to allow package C8+
9398 * The steps implemented here are just the steps that actually touch the LCPLL
9399 * register. Callers should take care of disabling all the display engine
9400 * functions, doing the mode unset, fixing interrupts, etc.
9401 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009402static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9403 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404{
9405 uint32_t val;
9406
9407 assert_can_disable_lcpll(dev_priv);
9408
9409 val = I915_READ(LCPLL_CTL);
9410
9411 if (switch_to_fclk) {
9412 val |= LCPLL_CD_SOURCE_FCLK;
9413 I915_WRITE(LCPLL_CTL, val);
9414
9415 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9416 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9417 DRM_ERROR("Switching to FCLK failed\n");
9418
9419 val = I915_READ(LCPLL_CTL);
9420 }
9421
9422 val |= LCPLL_PLL_DISABLE;
9423 I915_WRITE(LCPLL_CTL, val);
9424 POSTING_READ(LCPLL_CTL);
9425
9426 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9427 DRM_ERROR("LCPLL still locked\n");
9428
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009429 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009430 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009431 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009432 ndelay(100);
9433
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009434 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9435 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009436 DRM_ERROR("D_COMP RCOMP still in progress\n");
9437
9438 if (allow_power_down) {
9439 val = I915_READ(LCPLL_CTL);
9440 val |= LCPLL_POWER_DOWN_ALLOW;
9441 I915_WRITE(LCPLL_CTL, val);
9442 POSTING_READ(LCPLL_CTL);
9443 }
9444}
9445
9446/*
9447 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9448 * source.
9449 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009450static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009451{
9452 uint32_t val;
9453
9454 val = I915_READ(LCPLL_CTL);
9455
9456 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9457 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9458 return;
9459
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009460 /*
9461 * Make sure we're not on PC8 state before disabling PC8, otherwise
9462 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009463 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009464 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009465
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009466 if (val & LCPLL_POWER_DOWN_ALLOW) {
9467 val &= ~LCPLL_POWER_DOWN_ALLOW;
9468 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009469 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009470 }
9471
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009472 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009473 val |= D_COMP_COMP_FORCE;
9474 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009475 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009476
9477 val = I915_READ(LCPLL_CTL);
9478 val &= ~LCPLL_PLL_DISABLE;
9479 I915_WRITE(LCPLL_CTL, val);
9480
9481 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9482 DRM_ERROR("LCPLL not locked yet\n");
9483
9484 if (val & LCPLL_CD_SOURCE_FCLK) {
9485 val = I915_READ(LCPLL_CTL);
9486 val &= ~LCPLL_CD_SOURCE_FCLK;
9487 I915_WRITE(LCPLL_CTL, val);
9488
9489 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9490 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9491 DRM_ERROR("Switching back to LCPLL failed\n");
9492 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009493
Mika Kuoppala59bad942015-01-16 11:34:40 +02009494 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009495 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009496}
9497
Paulo Zanoni765dab672014-03-07 20:08:18 -03009498/*
9499 * Package states C8 and deeper are really deep PC states that can only be
9500 * reached when all the devices on the system allow it, so even if the graphics
9501 * device allows PC8+, it doesn't mean the system will actually get to these
9502 * states. Our driver only allows PC8+ when going into runtime PM.
9503 *
9504 * The requirements for PC8+ are that all the outputs are disabled, the power
9505 * well is disabled and most interrupts are disabled, and these are also
9506 * requirements for runtime PM. When these conditions are met, we manually do
9507 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9508 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9509 * hang the machine.
9510 *
9511 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9512 * the state of some registers, so when we come back from PC8+ we need to
9513 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9514 * need to take care of the registers kept by RC6. Notice that this happens even
9515 * if we don't put the device in PCI D3 state (which is what currently happens
9516 * because of the runtime PM support).
9517 *
9518 * For more, read "Display Sequences for Package C8" on the hardware
9519 * documentation.
9520 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009521void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009522{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009523 struct drm_device *dev = dev_priv->dev;
9524 uint32_t val;
9525
Paulo Zanonic67a4702013-08-19 13:18:09 -03009526 DRM_DEBUG_KMS("Enabling package C8+\n");
9527
Paulo Zanonic67a4702013-08-19 13:18:09 -03009528 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9529 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9530 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9531 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9532 }
9533
9534 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009535 hsw_disable_lcpll(dev_priv, true, true);
9536}
9537
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009538void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009539{
9540 struct drm_device *dev = dev_priv->dev;
9541 uint32_t val;
9542
Paulo Zanonic67a4702013-08-19 13:18:09 -03009543 DRM_DEBUG_KMS("Disabling package C8+\n");
9544
9545 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009546 lpt_init_pch_refclk(dev);
9547
9548 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9549 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9550 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9551 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9552 }
9553
9554 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009555}
9556
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009557static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309558{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009559 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009560 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309561
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009562 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309563}
9564
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009565/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009566static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009567{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009568 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009569 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009570 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009571
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009572 for_each_intel_crtc(state->dev, intel_crtc) {
9573 int pixel_rate;
9574
9575 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9576 if (IS_ERR(crtc_state))
9577 return PTR_ERR(crtc_state);
9578
9579 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009580 continue;
9581
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009582 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009583
9584 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009585 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009586 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9587
9588 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9589 }
9590
9591 return max_pixel_rate;
9592}
9593
9594static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9595{
9596 struct drm_i915_private *dev_priv = dev->dev_private;
9597 uint32_t val, data;
9598 int ret;
9599
9600 if (WARN((I915_READ(LCPLL_CTL) &
9601 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9602 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9603 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9604 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9605 "trying to change cdclk frequency with cdclk not enabled\n"))
9606 return;
9607
9608 mutex_lock(&dev_priv->rps.hw_lock);
9609 ret = sandybridge_pcode_write(dev_priv,
9610 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9611 mutex_unlock(&dev_priv->rps.hw_lock);
9612 if (ret) {
9613 DRM_ERROR("failed to inform pcode about cdclk change\n");
9614 return;
9615 }
9616
9617 val = I915_READ(LCPLL_CTL);
9618 val |= LCPLL_CD_SOURCE_FCLK;
9619 I915_WRITE(LCPLL_CTL, val);
9620
9621 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9622 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9623 DRM_ERROR("Switching to FCLK failed\n");
9624
9625 val = I915_READ(LCPLL_CTL);
9626 val &= ~LCPLL_CLK_FREQ_MASK;
9627
9628 switch (cdclk) {
9629 case 450000:
9630 val |= LCPLL_CLK_FREQ_450;
9631 data = 0;
9632 break;
9633 case 540000:
9634 val |= LCPLL_CLK_FREQ_54O_BDW;
9635 data = 1;
9636 break;
9637 case 337500:
9638 val |= LCPLL_CLK_FREQ_337_5_BDW;
9639 data = 2;
9640 break;
9641 case 675000:
9642 val |= LCPLL_CLK_FREQ_675_BDW;
9643 data = 3;
9644 break;
9645 default:
9646 WARN(1, "invalid cdclk frequency\n");
9647 return;
9648 }
9649
9650 I915_WRITE(LCPLL_CTL, val);
9651
9652 val = I915_READ(LCPLL_CTL);
9653 val &= ~LCPLL_CD_SOURCE_FCLK;
9654 I915_WRITE(LCPLL_CTL, val);
9655
9656 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9657 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9658 DRM_ERROR("Switching back to LCPLL failed\n");
9659
9660 mutex_lock(&dev_priv->rps.hw_lock);
9661 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9662 mutex_unlock(&dev_priv->rps.hw_lock);
9663
9664 intel_update_cdclk(dev);
9665
9666 WARN(cdclk != dev_priv->cdclk_freq,
9667 "cdclk requested %d kHz but got %d kHz\n",
9668 cdclk, dev_priv->cdclk_freq);
9669}
9670
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009671static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009672{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009673 struct drm_i915_private *dev_priv = to_i915(state->dev);
9674 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009675 int cdclk;
9676
9677 /*
9678 * FIXME should also account for plane ratio
9679 * once 64bpp pixel formats are supported.
9680 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009681 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009682 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009683 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009684 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009685 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009686 cdclk = 450000;
9687 else
9688 cdclk = 337500;
9689
9690 /*
9691 * FIXME move the cdclk caclulation to
9692 * compute_config() so we can fail gracegully.
9693 */
9694 if (cdclk > dev_priv->max_cdclk_freq) {
9695 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9696 cdclk, dev_priv->max_cdclk_freq);
9697 cdclk = dev_priv->max_cdclk_freq;
9698 }
9699
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009700 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009701
9702 return 0;
9703}
9704
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009705static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009706{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009707 struct drm_device *dev = old_state->dev;
9708 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009709
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009710 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009711}
9712
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009713static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9714 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009715{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009716 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009717 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009718
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009719 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009720
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009721 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009722}
9723
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309724static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9725 enum port port,
9726 struct intel_crtc_state *pipe_config)
9727{
9728 switch (port) {
9729 case PORT_A:
9730 pipe_config->ddi_pll_sel = SKL_DPLL0;
9731 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9732 break;
9733 case PORT_B:
9734 pipe_config->ddi_pll_sel = SKL_DPLL1;
9735 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9736 break;
9737 case PORT_C:
9738 pipe_config->ddi_pll_sel = SKL_DPLL2;
9739 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9740 break;
9741 default:
9742 DRM_ERROR("Incorrect port type\n");
9743 }
9744}
9745
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009746static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9747 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009748 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009749{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009750 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009751
9752 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9753 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9754
9755 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009756 case SKL_DPLL0:
9757 /*
9758 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9759 * of the shared DPLL framework and thus needs to be read out
9760 * separately
9761 */
9762 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9763 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9764 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009765 case SKL_DPLL1:
9766 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9767 break;
9768 case SKL_DPLL2:
9769 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9770 break;
9771 case SKL_DPLL3:
9772 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9773 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009774 }
9775}
9776
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009777static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9778 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009779 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009780{
9781 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9782
9783 switch (pipe_config->ddi_pll_sel) {
9784 case PORT_CLK_SEL_WRPLL1:
9785 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9786 break;
9787 case PORT_CLK_SEL_WRPLL2:
9788 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9789 break;
9790 }
9791}
9792
Daniel Vetter26804af2014-06-25 22:01:55 +03009793static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009794 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009795{
9796 struct drm_device *dev = crtc->base.dev;
9797 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009798 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009799 enum port port;
9800 uint32_t tmp;
9801
9802 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9803
9804 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9805
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009806 if (IS_SKYLAKE(dev))
9807 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309808 else if (IS_BROXTON(dev))
9809 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009810 else
9811 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009812
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009813 if (pipe_config->shared_dpll >= 0) {
9814 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9815
9816 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9817 &pipe_config->dpll_hw_state));
9818 }
9819
Daniel Vetter26804af2014-06-25 22:01:55 +03009820 /*
9821 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9822 * DDI E. So just check whether this pipe is wired to DDI E and whether
9823 * the PCH transcoder is on.
9824 */
Damien Lespiauca370452013-12-03 13:56:24 +00009825 if (INTEL_INFO(dev)->gen < 9 &&
9826 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009827 pipe_config->has_pch_encoder = true;
9828
9829 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9830 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9831 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9832
9833 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9834 }
9835}
9836
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009837static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009838 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009839{
9840 struct drm_device *dev = crtc->base.dev;
9841 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009842 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009843 uint32_t tmp;
9844
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009845 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009846 POWER_DOMAIN_PIPE(crtc->pipe)))
9847 return false;
9848
Daniel Vettere143a212013-07-04 12:01:15 +02009849 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009850 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9851
Daniel Vettereccb1402013-05-22 00:50:22 +02009852 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9853 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9854 enum pipe trans_edp_pipe;
9855 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9856 default:
9857 WARN(1, "unknown pipe linked to edp transcoder\n");
9858 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9859 case TRANS_DDI_EDP_INPUT_A_ON:
9860 trans_edp_pipe = PIPE_A;
9861 break;
9862 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9863 trans_edp_pipe = PIPE_B;
9864 break;
9865 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9866 trans_edp_pipe = PIPE_C;
9867 break;
9868 }
9869
9870 if (trans_edp_pipe == crtc->pipe)
9871 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9872 }
9873
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009874 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009875 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009876 return false;
9877
Daniel Vettereccb1402013-05-22 00:50:22 +02009878 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009879 if (!(tmp & PIPECONF_ENABLE))
9880 return false;
9881
Daniel Vetter26804af2014-06-25 22:01:55 +03009882 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009883
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009884 intel_get_pipe_timings(crtc, pipe_config);
9885
Chandra Kondurua1b22782015-04-07 15:28:45 -07009886 if (INTEL_INFO(dev)->gen >= 9) {
9887 skl_init_scalers(dev, crtc, pipe_config);
9888 }
9889
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009890 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009891
9892 if (INTEL_INFO(dev)->gen >= 9) {
9893 pipe_config->scaler_state.scaler_id = -1;
9894 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9895 }
9896
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009897 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009898 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009899 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009900 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009901 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009902 else
9903 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009904 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009905
Jesse Barnese59150d2014-01-07 13:30:45 -08009906 if (IS_HASWELL(dev))
9907 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9908 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009909
Clint Taylorebb69c92014-09-30 10:30:22 -07009910 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9911 pipe_config->pixel_multiplier =
9912 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9913 } else {
9914 pipe_config->pixel_multiplier = 1;
9915 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009916
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009917 return true;
9918}
9919
Chris Wilson560b85b2010-08-07 11:01:38 +01009920static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9921{
9922 struct drm_device *dev = crtc->dev;
9923 struct drm_i915_private *dev_priv = dev->dev_private;
9924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009925 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009926
Ville Syrjälädc41c152014-08-13 11:57:05 +03009927 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009928 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9929 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009930 unsigned int stride = roundup_pow_of_two(width) * 4;
9931
9932 switch (stride) {
9933 default:
9934 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9935 width, stride);
9936 stride = 256;
9937 /* fallthrough */
9938 case 256:
9939 case 512:
9940 case 1024:
9941 case 2048:
9942 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009943 }
9944
Ville Syrjälädc41c152014-08-13 11:57:05 +03009945 cntl |= CURSOR_ENABLE |
9946 CURSOR_GAMMA_ENABLE |
9947 CURSOR_FORMAT_ARGB |
9948 CURSOR_STRIDE(stride);
9949
9950 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009951 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009952
Ville Syrjälädc41c152014-08-13 11:57:05 +03009953 if (intel_crtc->cursor_cntl != 0 &&
9954 (intel_crtc->cursor_base != base ||
9955 intel_crtc->cursor_size != size ||
9956 intel_crtc->cursor_cntl != cntl)) {
9957 /* On these chipsets we can only modify the base/size/stride
9958 * whilst the cursor is disabled.
9959 */
9960 I915_WRITE(_CURACNTR, 0);
9961 POSTING_READ(_CURACNTR);
9962 intel_crtc->cursor_cntl = 0;
9963 }
9964
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009965 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009966 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009967 intel_crtc->cursor_base = base;
9968 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009969
9970 if (intel_crtc->cursor_size != size) {
9971 I915_WRITE(CURSIZE, size);
9972 intel_crtc->cursor_size = size;
9973 }
9974
Chris Wilson4b0e3332014-05-30 16:35:26 +03009975 if (intel_crtc->cursor_cntl != cntl) {
9976 I915_WRITE(_CURACNTR, cntl);
9977 POSTING_READ(_CURACNTR);
9978 intel_crtc->cursor_cntl = cntl;
9979 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009980}
9981
9982static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9983{
9984 struct drm_device *dev = crtc->dev;
9985 struct drm_i915_private *dev_priv = dev->dev_private;
9986 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9987 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009988 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009989
Chris Wilson4b0e3332014-05-30 16:35:26 +03009990 cntl = 0;
9991 if (base) {
9992 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009993 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309994 case 64:
9995 cntl |= CURSOR_MODE_64_ARGB_AX;
9996 break;
9997 case 128:
9998 cntl |= CURSOR_MODE_128_ARGB_AX;
9999 break;
10000 case 256:
10001 cntl |= CURSOR_MODE_256_ARGB_AX;
10002 break;
10003 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010004 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010005 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010006 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010007 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010008
10009 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10010 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010011 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010012
Matt Roper8e7d6882015-01-21 16:35:41 -080010013 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010014 cntl |= CURSOR_ROTATE_180;
10015
Chris Wilson4b0e3332014-05-30 16:35:26 +030010016 if (intel_crtc->cursor_cntl != cntl) {
10017 I915_WRITE(CURCNTR(pipe), cntl);
10018 POSTING_READ(CURCNTR(pipe));
10019 intel_crtc->cursor_cntl = cntl;
10020 }
10021
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010022 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010023 I915_WRITE(CURBASE(pipe), base);
10024 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010025
10026 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010027}
10028
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010029/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010030static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10031 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010032{
10033 struct drm_device *dev = crtc->dev;
10034 struct drm_i915_private *dev_priv = dev->dev_private;
10035 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10036 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010037 int x = crtc->cursor_x;
10038 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010039 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010040
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010041 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010042 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010043
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010044 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010045 base = 0;
10046
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010047 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010048 base = 0;
10049
10050 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010051 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010052 base = 0;
10053
10054 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10055 x = -x;
10056 }
10057 pos |= x << CURSOR_X_SHIFT;
10058
10059 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010060 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010061 base = 0;
10062
10063 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10064 y = -y;
10065 }
10066 pos |= y << CURSOR_Y_SHIFT;
10067
Chris Wilson4b0e3332014-05-30 16:35:26 +030010068 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010069 return;
10070
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010071 I915_WRITE(CURPOS(pipe), pos);
10072
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010073 /* ILK+ do this automagically */
10074 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010075 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010076 base += (intel_crtc->base.cursor->state->crtc_h *
10077 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010078 }
10079
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010080 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010081 i845_update_cursor(crtc, base);
10082 else
10083 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010084}
10085
Ville Syrjälädc41c152014-08-13 11:57:05 +030010086static bool cursor_size_ok(struct drm_device *dev,
10087 uint32_t width, uint32_t height)
10088{
10089 if (width == 0 || height == 0)
10090 return false;
10091
10092 /*
10093 * 845g/865g are special in that they are only limited by
10094 * the width of their cursors, the height is arbitrary up to
10095 * the precision of the register. Everything else requires
10096 * square cursors, limited to a few power-of-two sizes.
10097 */
10098 if (IS_845G(dev) || IS_I865G(dev)) {
10099 if ((width & 63) != 0)
10100 return false;
10101
10102 if (width > (IS_845G(dev) ? 64 : 512))
10103 return false;
10104
10105 if (height > 1023)
10106 return false;
10107 } else {
10108 switch (width | height) {
10109 case 256:
10110 case 128:
10111 if (IS_GEN2(dev))
10112 return false;
10113 case 64:
10114 break;
10115 default:
10116 return false;
10117 }
10118 }
10119
10120 return true;
10121}
10122
Jesse Barnes79e53942008-11-07 14:24:08 -080010123static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010124 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010125{
James Simmons72034252010-08-03 01:33:19 +010010126 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010127 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010128
James Simmons72034252010-08-03 01:33:19 +010010129 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010130 intel_crtc->lut_r[i] = red[i] >> 8;
10131 intel_crtc->lut_g[i] = green[i] >> 8;
10132 intel_crtc->lut_b[i] = blue[i] >> 8;
10133 }
10134
10135 intel_crtc_load_lut(crtc);
10136}
10137
Jesse Barnes79e53942008-11-07 14:24:08 -080010138/* VESA 640x480x72Hz mode to set on the pipe */
10139static struct drm_display_mode load_detect_mode = {
10140 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10141 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10142};
10143
Daniel Vettera8bb6812014-02-10 18:00:39 +010010144struct drm_framebuffer *
10145__intel_framebuffer_create(struct drm_device *dev,
10146 struct drm_mode_fb_cmd2 *mode_cmd,
10147 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010148{
10149 struct intel_framebuffer *intel_fb;
10150 int ret;
10151
10152 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10153 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010154 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010155 return ERR_PTR(-ENOMEM);
10156 }
10157
10158 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010159 if (ret)
10160 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010161
10162 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010163err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010164 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010165 kfree(intel_fb);
10166
10167 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010168}
10169
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010170static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010171intel_framebuffer_create(struct drm_device *dev,
10172 struct drm_mode_fb_cmd2 *mode_cmd,
10173 struct drm_i915_gem_object *obj)
10174{
10175 struct drm_framebuffer *fb;
10176 int ret;
10177
10178 ret = i915_mutex_lock_interruptible(dev);
10179 if (ret)
10180 return ERR_PTR(ret);
10181 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10182 mutex_unlock(&dev->struct_mutex);
10183
10184 return fb;
10185}
10186
Chris Wilsond2dff872011-04-19 08:36:26 +010010187static u32
10188intel_framebuffer_pitch_for_width(int width, int bpp)
10189{
10190 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10191 return ALIGN(pitch, 64);
10192}
10193
10194static u32
10195intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10196{
10197 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010198 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010199}
10200
10201static struct drm_framebuffer *
10202intel_framebuffer_create_for_mode(struct drm_device *dev,
10203 struct drm_display_mode *mode,
10204 int depth, int bpp)
10205{
10206 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010207 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010208
10209 obj = i915_gem_alloc_object(dev,
10210 intel_framebuffer_size_for_mode(mode, bpp));
10211 if (obj == NULL)
10212 return ERR_PTR(-ENOMEM);
10213
10214 mode_cmd.width = mode->hdisplay;
10215 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010216 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10217 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010218 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010219
10220 return intel_framebuffer_create(dev, &mode_cmd, obj);
10221}
10222
10223static struct drm_framebuffer *
10224mode_fits_in_fbdev(struct drm_device *dev,
10225 struct drm_display_mode *mode)
10226{
Daniel Vetter4520f532013-10-09 09:18:51 +020010227#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010228 struct drm_i915_private *dev_priv = dev->dev_private;
10229 struct drm_i915_gem_object *obj;
10230 struct drm_framebuffer *fb;
10231
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010232 if (!dev_priv->fbdev)
10233 return NULL;
10234
10235 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010236 return NULL;
10237
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010238 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010239 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010240
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010241 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010242 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10243 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010244 return NULL;
10245
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010246 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010247 return NULL;
10248
10249 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010250#else
10251 return NULL;
10252#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010253}
10254
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010255static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10256 struct drm_crtc *crtc,
10257 struct drm_display_mode *mode,
10258 struct drm_framebuffer *fb,
10259 int x, int y)
10260{
10261 struct drm_plane_state *plane_state;
10262 int hdisplay, vdisplay;
10263 int ret;
10264
10265 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10266 if (IS_ERR(plane_state))
10267 return PTR_ERR(plane_state);
10268
10269 if (mode)
10270 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10271 else
10272 hdisplay = vdisplay = 0;
10273
10274 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10275 if (ret)
10276 return ret;
10277 drm_atomic_set_fb_for_plane(plane_state, fb);
10278 plane_state->crtc_x = 0;
10279 plane_state->crtc_y = 0;
10280 plane_state->crtc_w = hdisplay;
10281 plane_state->crtc_h = vdisplay;
10282 plane_state->src_x = x << 16;
10283 plane_state->src_y = y << 16;
10284 plane_state->src_w = hdisplay << 16;
10285 plane_state->src_h = vdisplay << 16;
10286
10287 return 0;
10288}
10289
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010290bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010291 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010292 struct intel_load_detect_pipe *old,
10293 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010294{
10295 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010296 struct intel_encoder *intel_encoder =
10297 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010298 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010299 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010300 struct drm_crtc *crtc = NULL;
10301 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010302 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010303 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010304 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010305 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010306 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010307 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010308
Chris Wilsond2dff872011-04-19 08:36:26 +010010309 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010310 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010311 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010312
Rob Clark51fd3712013-11-19 12:10:12 -050010313retry:
10314 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10315 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010316 goto fail;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010317
Jesse Barnes79e53942008-11-07 14:24:08 -080010318 /*
10319 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010320 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010321 * - if the connector already has an assigned crtc, use it (but make
10322 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010323 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010324 * - try to find the first unused crtc that can drive this connector,
10325 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010326 */
10327
10328 /* See if we already have a CRTC for this connector */
10329 if (encoder->crtc) {
10330 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010331
Rob Clark51fd3712013-11-19 12:10:12 -050010332 ret = drm_modeset_lock(&crtc->mutex, ctx);
10333 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010334 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010335 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10336 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010337 goto fail;
Daniel Vetter7b240562012-12-12 00:35:33 +010010338
Daniel Vetter24218aa2012-08-12 19:27:11 +020010339 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010340 old->load_detect_temp = false;
10341
10342 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010343 if (connector->dpms != DRM_MODE_DPMS_ON)
10344 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010345
Chris Wilson71731882011-04-19 23:10:58 +010010346 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010347 }
10348
10349 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010350 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010351 i++;
10352 if (!(encoder->possible_crtcs & (1 << i)))
10353 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010354 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010355 continue;
Ville Syrjäläa4592492014-08-11 13:15:36 +030010356
10357 crtc = possible_crtc;
10358 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010359 }
10360
10361 /*
10362 * If we didn't find an unused CRTC, don't use any.
10363 */
10364 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010365 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010366 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010367 }
10368
Rob Clark51fd3712013-11-19 12:10:12 -050010369 ret = drm_modeset_lock(&crtc->mutex, ctx);
10370 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010371 goto fail;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010372 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10373 if (ret)
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010374 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010375
10376 intel_crtc = to_intel_crtc(crtc);
Daniel Vetter24218aa2012-08-12 19:27:11 +020010377 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010378 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010379 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010380
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010381 state = drm_atomic_state_alloc(dev);
10382 if (!state)
10383 return false;
10384
10385 state->acquire_ctx = ctx;
10386
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010387 connector_state = drm_atomic_get_connector_state(state, connector);
10388 if (IS_ERR(connector_state)) {
10389 ret = PTR_ERR(connector_state);
10390 goto fail;
10391 }
10392
10393 connector_state->crtc = crtc;
10394 connector_state->best_encoder = &intel_encoder->base;
10395
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010396 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10397 if (IS_ERR(crtc_state)) {
10398 ret = PTR_ERR(crtc_state);
10399 goto fail;
10400 }
10401
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010402 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010403
Chris Wilson64927112011-04-20 07:25:26 +010010404 if (!mode)
10405 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010406
Chris Wilsond2dff872011-04-19 08:36:26 +010010407 /* We need a framebuffer large enough to accommodate all accesses
10408 * that the plane may generate whilst we perform load detection.
10409 * We can not rely on the fbcon either being present (we get called
10410 * during its initialisation to detect all boot displays, or it may
10411 * not even exist) or that it is large enough to satisfy the
10412 * requested mode.
10413 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010414 fb = mode_fits_in_fbdev(dev, mode);
10415 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010416 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010417 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10418 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010419 } else
10420 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010421 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010422 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010423 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010424 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010425
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010426 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10427 if (ret)
10428 goto fail;
10429
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010430 drm_mode_copy(&crtc_state->base.mode, mode);
10431
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010432 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010433 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010434 if (old->release_fb)
10435 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010436 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010437 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010438 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010439
Jesse Barnes79e53942008-11-07 14:24:08 -080010440 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010441 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010442 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010443
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020010444fail:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010445 drm_atomic_state_free(state);
10446 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010447
Rob Clark51fd3712013-11-19 12:10:12 -050010448 if (ret == -EDEADLK) {
10449 drm_modeset_backoff(ctx);
10450 goto retry;
10451 }
10452
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010453 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010454}
10455
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010456void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010457 struct intel_load_detect_pipe *old,
10458 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010459{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010460 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010461 struct intel_encoder *intel_encoder =
10462 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010463 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010464 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010465 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010466 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010467 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010468 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010469 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010470
Chris Wilsond2dff872011-04-19 08:36:26 +010010471 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010472 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010473 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010474
Chris Wilson8261b192011-04-19 23:18:09 +010010475 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010476 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010477 if (!state)
10478 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010479
10480 state->acquire_ctx = ctx;
10481
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010482 connector_state = drm_atomic_get_connector_state(state, connector);
10483 if (IS_ERR(connector_state))
10484 goto fail;
10485
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010486 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10487 if (IS_ERR(crtc_state))
10488 goto fail;
10489
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010490 connector_state->best_encoder = NULL;
10491 connector_state->crtc = NULL;
10492
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010493 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010494
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010495 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10496 0, 0);
10497 if (ret)
10498 goto fail;
10499
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010500 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010501 if (ret)
10502 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010503
Daniel Vetter36206362012-12-10 20:42:17 +010010504 if (old->release_fb) {
10505 drm_framebuffer_unregister_private(old->release_fb);
10506 drm_framebuffer_unreference(old->release_fb);
10507 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010508
Chris Wilson0622a532011-04-21 09:32:11 +010010509 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010510 }
10511
Eric Anholtc751ce42010-03-25 11:48:48 -070010512 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010513 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10514 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010515
10516 return;
10517fail:
10518 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10519 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010520}
10521
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010522static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010523 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010524{
10525 struct drm_i915_private *dev_priv = dev->dev_private;
10526 u32 dpll = pipe_config->dpll_hw_state.dpll;
10527
10528 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010529 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010530 else if (HAS_PCH_SPLIT(dev))
10531 return 120000;
10532 else if (!IS_GEN2(dev))
10533 return 96000;
10534 else
10535 return 48000;
10536}
10537
Jesse Barnes79e53942008-11-07 14:24:08 -080010538/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010539static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010540 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010541{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010542 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010543 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010544 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010545 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010546 u32 fp;
10547 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010548 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010549 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010550
10551 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010552 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010553 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010554 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010555
10556 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010557 if (IS_PINEVIEW(dev)) {
10558 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10559 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010560 } else {
10561 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10562 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10563 }
10564
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010565 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010566 if (IS_PINEVIEW(dev))
10567 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10568 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010569 else
10570 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010571 DPLL_FPA01_P1_POST_DIV_SHIFT);
10572
10573 switch (dpll & DPLL_MODE_MASK) {
10574 case DPLLB_MODE_DAC_SERIAL:
10575 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10576 5 : 10;
10577 break;
10578 case DPLLB_MODE_LVDS:
10579 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10580 7 : 14;
10581 break;
10582 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010583 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010584 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010585 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010586 }
10587
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010588 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010589 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010590 else
Imre Deakdccbea32015-06-22 23:35:51 +030010591 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010592 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010593 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010594 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010595
10596 if (is_lvds) {
10597 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10598 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010599
10600 if (lvds & LVDS_CLKB_POWER_UP)
10601 clock.p2 = 7;
10602 else
10603 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010604 } else {
10605 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10606 clock.p1 = 2;
10607 else {
10608 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10609 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10610 }
10611 if (dpll & PLL_P2_DIVIDE_BY_4)
10612 clock.p2 = 4;
10613 else
10614 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010615 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010616
Imre Deakdccbea32015-06-22 23:35:51 +030010617 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010618 }
10619
Ville Syrjälä18442d02013-09-13 16:00:08 +030010620 /*
10621 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010622 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010623 * encoder's get_config() function.
10624 */
Imre Deakdccbea32015-06-22 23:35:51 +030010625 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010626}
10627
Ville Syrjälä6878da02013-09-13 15:59:11 +030010628int intel_dotclock_calculate(int link_freq,
10629 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010630{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010631 /*
10632 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010633 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010634 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010635 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010636 *
10637 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010638 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 */
10640
Ville Syrjälä6878da02013-09-13 15:59:11 +030010641 if (!m_n->link_n)
10642 return 0;
10643
10644 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10645}
10646
Ville Syrjälä18442d02013-09-13 16:00:08 +030010647static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010648 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010649{
10650 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010651
10652 /* read out port_clock from the DPLL */
10653 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010654
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010655 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010656 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010657 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010658 * agree once we know their relationship in the encoder's
10659 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010660 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010661 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010662 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10663 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010664}
10665
10666/** Returns the currently programmed mode of the given pipe. */
10667struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10668 struct drm_crtc *crtc)
10669{
Jesse Barnes548f2452011-02-17 10:40:53 -080010670 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010671 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010672 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010673 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010674 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010675 int htot = I915_READ(HTOTAL(cpu_transcoder));
10676 int hsync = I915_READ(HSYNC(cpu_transcoder));
10677 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10678 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010679 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010680
10681 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10682 if (!mode)
10683 return NULL;
10684
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010685 /*
10686 * Construct a pipe_config sufficient for getting the clock info
10687 * back out of crtc_clock_get.
10688 *
10689 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10690 * to use a real value here instead.
10691 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010692 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010693 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010694 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10695 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10696 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010697 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10698
Ville Syrjälä773ae032013-09-23 17:48:20 +030010699 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010700 mode->hdisplay = (htot & 0xffff) + 1;
10701 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10702 mode->hsync_start = (hsync & 0xffff) + 1;
10703 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10704 mode->vdisplay = (vtot & 0xffff) + 1;
10705 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10706 mode->vsync_start = (vsync & 0xffff) + 1;
10707 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10708
10709 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010710
10711 return mode;
10712}
10713
Chris Wilsonf047e392012-07-21 12:31:41 +010010714void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010715{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010716 struct drm_i915_private *dev_priv = dev->dev_private;
10717
Chris Wilsonf62a0072014-02-21 17:55:39 +000010718 if (dev_priv->mm.busy)
10719 return;
10720
Paulo Zanoni43694d62014-03-07 20:08:08 -030010721 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010722 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010723 if (INTEL_INFO(dev)->gen >= 6)
10724 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010725 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010726}
10727
10728void intel_mark_idle(struct drm_device *dev)
10729{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010730 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010731
Chris Wilsonf62a0072014-02-21 17:55:39 +000010732 if (!dev_priv->mm.busy)
10733 return;
10734
10735 dev_priv->mm.busy = false;
10736
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010737 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010738 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010739
Paulo Zanoni43694d62014-03-07 20:08:08 -030010740 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010741}
10742
Jesse Barnes79e53942008-11-07 14:24:08 -080010743static void intel_crtc_destroy(struct drm_crtc *crtc)
10744{
10745 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010746 struct drm_device *dev = crtc->dev;
10747 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010748
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010749 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010750 work = intel_crtc->unpin_work;
10751 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010752 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010753
10754 if (work) {
10755 cancel_work_sync(&work->work);
10756 kfree(work);
10757 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010758
10759 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010760
Jesse Barnes79e53942008-11-07 14:24:08 -080010761 kfree(intel_crtc);
10762}
10763
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010764static void intel_unpin_work_fn(struct work_struct *__work)
10765{
10766 struct intel_unpin_work *work =
10767 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010768 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10769 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -030010770 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010771 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010772
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010773 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010774 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010775 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010776
Paulo Zanoni7733b492015-07-07 15:26:04 -030010777 intel_fbc_update(dev_priv);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010778
10779 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010780 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010781 mutex_unlock(&dev->struct_mutex);
10782
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010783 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010784 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010785
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010786 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10787 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010788
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010789 kfree(work);
10790}
10791
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010792static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010793 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010794{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10796 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010797 unsigned long flags;
10798
10799 /* Ignore early vblank irqs */
10800 if (intel_crtc == NULL)
10801 return;
10802
Daniel Vetterf3260382014-09-15 14:55:23 +020010803 /*
10804 * This is called both by irq handlers and the reset code (to complete
10805 * lost pageflips) so needs the full irqsave spinlocks.
10806 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010807 spin_lock_irqsave(&dev->event_lock, flags);
10808 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010809
10810 /* Ensure we don't miss a work->pending update ... */
10811 smp_rmb();
10812
10813 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010814 spin_unlock_irqrestore(&dev->event_lock, flags);
10815 return;
10816 }
10817
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010818 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010819
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010820 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010821}
10822
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010823void intel_finish_page_flip(struct drm_device *dev, int pipe)
10824{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010825 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010826 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10827
Mario Kleiner49b14a52010-12-09 07:00:07 +010010828 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010829}
10830
10831void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10832{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010833 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010834 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10835
Mario Kleiner49b14a52010-12-09 07:00:07 +010010836 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010837}
10838
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010839/* Is 'a' after or equal to 'b'? */
10840static bool g4x_flip_count_after_eq(u32 a, u32 b)
10841{
10842 return !((a - b) & 0x80000000);
10843}
10844
10845static bool page_flip_finished(struct intel_crtc *crtc)
10846{
10847 struct drm_device *dev = crtc->base.dev;
10848 struct drm_i915_private *dev_priv = dev->dev_private;
10849
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010850 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10851 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10852 return true;
10853
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010854 /*
10855 * The relevant registers doen't exist on pre-ctg.
10856 * As the flip done interrupt doesn't trigger for mmio
10857 * flips on gmch platforms, a flip count check isn't
10858 * really needed there. But since ctg has the registers,
10859 * include it in the check anyway.
10860 */
10861 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10862 return true;
10863
10864 /*
10865 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10866 * used the same base address. In that case the mmio flip might
10867 * have completed, but the CS hasn't even executed the flip yet.
10868 *
10869 * A flip count check isn't enough as the CS might have updated
10870 * the base address just after start of vblank, but before we
10871 * managed to process the interrupt. This means we'd complete the
10872 * CS flip too soon.
10873 *
10874 * Combining both checks should get us a good enough result. It may
10875 * still happen that the CS flip has been executed, but has not
10876 * yet actually completed. But in case the base address is the same
10877 * anyway, we don't really care.
10878 */
10879 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10880 crtc->unpin_work->gtt_offset &&
10881 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10882 crtc->unpin_work->flip_count);
10883}
10884
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010885void intel_prepare_page_flip(struct drm_device *dev, int plane)
10886{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010887 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010888 struct intel_crtc *intel_crtc =
10889 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10890 unsigned long flags;
10891
Daniel Vetterf3260382014-09-15 14:55:23 +020010892
10893 /*
10894 * This is called both by irq handlers and the reset code (to complete
10895 * lost pageflips) so needs the full irqsave spinlocks.
10896 *
10897 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010898 * generate a page-flip completion irq, i.e. every modeset
10899 * is also accompanied by a spurious intel_prepare_page_flip().
10900 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010901 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010902 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010903 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010904 spin_unlock_irqrestore(&dev->event_lock, flags);
10905}
10906
Robin Schroereba905b2014-05-18 02:24:50 +020010907static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010908{
10909 /* Ensure that the work item is consistent when activating it ... */
10910 smp_wmb();
10911 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10912 /* and that it is marked active as soon as the irq could fire. */
10913 smp_wmb();
10914}
10915
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010916static int intel_gen2_queue_flip(struct drm_device *dev,
10917 struct drm_crtc *crtc,
10918 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010919 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010920 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010921 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010922{
John Harrison6258fbe2015-05-29 17:43:48 +010010923 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010924 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010925 u32 flip_mask;
10926 int ret;
10927
John Harrison5fb9de12015-05-29 17:44:07 +010010928 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010929 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010930 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010931
10932 /* Can't queue multiple flips, so wait for the previous
10933 * one to finish before executing the next.
10934 */
10935 if (intel_crtc->plane)
10936 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10937 else
10938 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010939 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10940 intel_ring_emit(ring, MI_NOOP);
10941 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10942 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10943 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010944 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010945 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010946
10947 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010948 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010949}
10950
10951static int intel_gen3_queue_flip(struct drm_device *dev,
10952 struct drm_crtc *crtc,
10953 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010954 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010955 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010956 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010957{
John Harrison6258fbe2015-05-29 17:43:48 +010010958 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010959 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010960 u32 flip_mask;
10961 int ret;
10962
John Harrison5fb9de12015-05-29 17:44:07 +010010963 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010964 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010965 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010966
10967 if (intel_crtc->plane)
10968 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10969 else
10970 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010971 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10972 intel_ring_emit(ring, MI_NOOP);
10973 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10974 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10975 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010976 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010977 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010978
Chris Wilsone7d841c2012-12-03 11:36:30 +000010979 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010980 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010981}
10982
10983static int intel_gen4_queue_flip(struct drm_device *dev,
10984 struct drm_crtc *crtc,
10985 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010986 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010987 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010988 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010989{
John Harrison6258fbe2015-05-29 17:43:48 +010010990 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010991 struct drm_i915_private *dev_priv = dev->dev_private;
10992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10993 uint32_t pf, pipesrc;
10994 int ret;
10995
John Harrison5fb9de12015-05-29 17:44:07 +010010996 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010997 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010998 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010999
11000 /* i965+ uses the linear or tiled offsets from the
11001 * Display Registers (which do not change across a page-flip)
11002 * so we need only reprogram the base address.
11003 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011004 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11005 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11006 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011007 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011008 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011009
11010 /* XXX Enabling the panel-fitter across page-flip is so far
11011 * untested on non-native modes, so ignore it for now.
11012 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11013 */
11014 pf = 0;
11015 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011016 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011017
11018 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011019 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011020}
11021
11022static int intel_gen6_queue_flip(struct drm_device *dev,
11023 struct drm_crtc *crtc,
11024 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011025 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011026 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011027 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011028{
John Harrison6258fbe2015-05-29 17:43:48 +010011029 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011030 struct drm_i915_private *dev_priv = dev->dev_private;
11031 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11032 uint32_t pf, pipesrc;
11033 int ret;
11034
John Harrison5fb9de12015-05-29 17:44:07 +010011035 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011036 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011037 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011038
Daniel Vetter6d90c952012-04-26 23:28:05 +020011039 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11040 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11041 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011042 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011043
Chris Wilson99d9acd2012-04-17 20:37:00 +010011044 /* Contrary to the suggestions in the documentation,
11045 * "Enable Panel Fitter" does not seem to be required when page
11046 * flipping with a non-native mode, and worse causes a normal
11047 * modeset to fail.
11048 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11049 */
11050 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011051 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011052 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011053
11054 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011055 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011056}
11057
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011058static int intel_gen7_queue_flip(struct drm_device *dev,
11059 struct drm_crtc *crtc,
11060 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011061 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011062 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011063 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011064{
John Harrison6258fbe2015-05-29 17:43:48 +010011065 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011066 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011067 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011068 int len, ret;
11069
Robin Schroereba905b2014-05-18 02:24:50 +020011070 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011071 case PLANE_A:
11072 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11073 break;
11074 case PLANE_B:
11075 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11076 break;
11077 case PLANE_C:
11078 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11079 break;
11080 default:
11081 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011082 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011083 }
11084
Chris Wilsonffe74d72013-08-26 20:58:12 +010011085 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011086 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011087 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011088 /*
11089 * On Gen 8, SRM is now taking an extra dword to accommodate
11090 * 48bits addresses, and we need a NOOP for the batch size to
11091 * stay even.
11092 */
11093 if (IS_GEN8(dev))
11094 len += 2;
11095 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011096
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011097 /*
11098 * BSpec MI_DISPLAY_FLIP for IVB:
11099 * "The full packet must be contained within the same cache line."
11100 *
11101 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11102 * cacheline, if we ever start emitting more commands before
11103 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11104 * then do the cacheline alignment, and finally emit the
11105 * MI_DISPLAY_FLIP.
11106 */
John Harrisonbba09b12015-05-29 17:44:06 +010011107 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011108 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011109 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011110
John Harrison5fb9de12015-05-29 17:44:07 +010011111 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011112 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011113 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011114
Chris Wilsonffe74d72013-08-26 20:58:12 +010011115 /* Unmask the flip-done completion message. Note that the bspec says that
11116 * we should do this for both the BCS and RCS, and that we must not unmask
11117 * more than one flip event at any time (or ensure that one flip message
11118 * can be sent by waiting for flip-done prior to queueing new flips).
11119 * Experimentation says that BCS works despite DERRMR masking all
11120 * flip-done completion events and that unmasking all planes at once
11121 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11122 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11123 */
11124 if (ring->id == RCS) {
11125 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11126 intel_ring_emit(ring, DERRMR);
11127 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11128 DERRMR_PIPEB_PRI_FLIP_DONE |
11129 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011130 if (IS_GEN8(dev))
11131 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11132 MI_SRM_LRM_GLOBAL_GTT);
11133 else
11134 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11135 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011136 intel_ring_emit(ring, DERRMR);
11137 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011138 if (IS_GEN8(dev)) {
11139 intel_ring_emit(ring, 0);
11140 intel_ring_emit(ring, MI_NOOP);
11141 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011142 }
11143
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011144 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011145 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011146 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011147 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011148
11149 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011150 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011151}
11152
Sourab Gupta84c33a62014-06-02 16:47:17 +053011153static bool use_mmio_flip(struct intel_engine_cs *ring,
11154 struct drm_i915_gem_object *obj)
11155{
11156 /*
11157 * This is not being used for older platforms, because
11158 * non-availability of flip done interrupt forces us to use
11159 * CS flips. Older platforms derive flip done using some clever
11160 * tricks involving the flip_pending status bits and vblank irqs.
11161 * So using MMIO flips there would disrupt this mechanism.
11162 */
11163
Chris Wilson8e09bf82014-07-08 10:40:30 +010011164 if (ring == NULL)
11165 return true;
11166
Sourab Gupta84c33a62014-06-02 16:47:17 +053011167 if (INTEL_INFO(ring->dev)->gen < 5)
11168 return false;
11169
11170 if (i915.use_mmio_flip < 0)
11171 return false;
11172 else if (i915.use_mmio_flip > 0)
11173 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011174 else if (i915.enable_execlists)
11175 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011176 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011177 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011178}
11179
Damien Lespiauff944562014-11-20 14:58:16 +000011180static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11181{
11182 struct drm_device *dev = intel_crtc->base.dev;
11183 struct drm_i915_private *dev_priv = dev->dev_private;
11184 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011185 const enum pipe pipe = intel_crtc->pipe;
11186 u32 ctl, stride;
11187
11188 ctl = I915_READ(PLANE_CTL(pipe, 0));
11189 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011190 switch (fb->modifier[0]) {
11191 case DRM_FORMAT_MOD_NONE:
11192 break;
11193 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011194 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011195 break;
11196 case I915_FORMAT_MOD_Y_TILED:
11197 ctl |= PLANE_CTL_TILED_Y;
11198 break;
11199 case I915_FORMAT_MOD_Yf_TILED:
11200 ctl |= PLANE_CTL_TILED_YF;
11201 break;
11202 default:
11203 MISSING_CASE(fb->modifier[0]);
11204 }
Damien Lespiauff944562014-11-20 14:58:16 +000011205
11206 /*
11207 * The stride is either expressed as a multiple of 64 bytes chunks for
11208 * linear buffers or in number of tiles for tiled buffers.
11209 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011210 stride = fb->pitches[0] /
11211 intel_fb_stride_alignment(dev, fb->modifier[0],
11212 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011213
11214 /*
11215 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11216 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11217 */
11218 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11219 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11220
11221 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11222 POSTING_READ(PLANE_SURF(pipe, 0));
11223}
11224
11225static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011226{
11227 struct drm_device *dev = intel_crtc->base.dev;
11228 struct drm_i915_private *dev_priv = dev->dev_private;
11229 struct intel_framebuffer *intel_fb =
11230 to_intel_framebuffer(intel_crtc->base.primary->fb);
11231 struct drm_i915_gem_object *obj = intel_fb->obj;
11232 u32 dspcntr;
11233 u32 reg;
11234
Sourab Gupta84c33a62014-06-02 16:47:17 +053011235 reg = DSPCNTR(intel_crtc->plane);
11236 dspcntr = I915_READ(reg);
11237
Damien Lespiauc5d97472014-10-25 00:11:11 +010011238 if (obj->tiling_mode != I915_TILING_NONE)
11239 dspcntr |= DISPPLANE_TILED;
11240 else
11241 dspcntr &= ~DISPPLANE_TILED;
11242
Sourab Gupta84c33a62014-06-02 16:47:17 +053011243 I915_WRITE(reg, dspcntr);
11244
11245 I915_WRITE(DSPSURF(intel_crtc->plane),
11246 intel_crtc->unpin_work->gtt_offset);
11247 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011248
Damien Lespiauff944562014-11-20 14:58:16 +000011249}
11250
11251/*
11252 * XXX: This is the temporary way to update the plane registers until we get
11253 * around to using the usual plane update functions for MMIO flips
11254 */
11255static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11256{
11257 struct drm_device *dev = intel_crtc->base.dev;
11258 bool atomic_update;
11259 u32 start_vbl_count;
11260
11261 intel_mark_page_flip_active(intel_crtc);
11262
11263 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11264
11265 if (INTEL_INFO(dev)->gen >= 9)
11266 skl_do_mmio_flip(intel_crtc);
11267 else
11268 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11269 ilk_do_mmio_flip(intel_crtc);
11270
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011271 if (atomic_update)
11272 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011273}
11274
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011275static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011276{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011277 struct intel_mmio_flip *mmio_flip =
11278 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011279
Daniel Vettereed29a52015-05-21 14:21:25 +020011280 if (mmio_flip->req)
11281 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011282 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011283 false, NULL,
11284 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011285
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011286 intel_do_mmio_flip(mmio_flip->crtc);
11287
Daniel Vettereed29a52015-05-21 14:21:25 +020011288 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011289 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011290}
11291
11292static int intel_queue_mmio_flip(struct drm_device *dev,
11293 struct drm_crtc *crtc,
11294 struct drm_framebuffer *fb,
11295 struct drm_i915_gem_object *obj,
11296 struct intel_engine_cs *ring,
11297 uint32_t flags)
11298{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011299 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011300
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011301 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11302 if (mmio_flip == NULL)
11303 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011304
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011305 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011306 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011307 mmio_flip->crtc = to_intel_crtc(crtc);
11308
11309 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11310 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011311
Sourab Gupta84c33a62014-06-02 16:47:17 +053011312 return 0;
11313}
11314
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011315static int intel_default_queue_flip(struct drm_device *dev,
11316 struct drm_crtc *crtc,
11317 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011318 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011319 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011320 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011321{
11322 return -ENODEV;
11323}
11324
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011325static bool __intel_pageflip_stall_check(struct drm_device *dev,
11326 struct drm_crtc *crtc)
11327{
11328 struct drm_i915_private *dev_priv = dev->dev_private;
11329 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11330 struct intel_unpin_work *work = intel_crtc->unpin_work;
11331 u32 addr;
11332
11333 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11334 return true;
11335
11336 if (!work->enable_stall_check)
11337 return false;
11338
11339 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011340 if (work->flip_queued_req &&
11341 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011342 return false;
11343
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011344 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011345 }
11346
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011347 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011348 return false;
11349
11350 /* Potential stall - if we see that the flip has happened,
11351 * assume a missed interrupt. */
11352 if (INTEL_INFO(dev)->gen >= 4)
11353 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11354 else
11355 addr = I915_READ(DSPADDR(intel_crtc->plane));
11356
11357 /* There is a potential issue here with a false positive after a flip
11358 * to the same address. We could address this by checking for a
11359 * non-incrementing frame counter.
11360 */
11361 return addr == work->gtt_offset;
11362}
11363
11364void intel_check_page_flip(struct drm_device *dev, int pipe)
11365{
11366 struct drm_i915_private *dev_priv = dev->dev_private;
11367 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011369 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011370
Dave Gordon6c51d462015-03-06 15:34:26 +000011371 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011372
11373 if (crtc == NULL)
11374 return;
11375
Daniel Vetterf3260382014-09-15 14:55:23 +020011376 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011377 work = intel_crtc->unpin_work;
11378 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011379 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011380 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011381 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011382 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011383 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011384 if (work != NULL &&
11385 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11386 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011387 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011388}
11389
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011390static int intel_crtc_page_flip(struct drm_crtc *crtc,
11391 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011392 struct drm_pending_vblank_event *event,
11393 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011394{
11395 struct drm_device *dev = crtc->dev;
11396 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011397 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011398 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011399 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011400 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011401 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011402 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011403 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011404 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011405 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011406 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011407
Matt Roper2ff8fde2014-07-08 07:50:07 -070011408 /*
11409 * drm_mode_page_flip_ioctl() should already catch this, but double
11410 * check to be safe. In the future we may enable pageflipping from
11411 * a disabled primary plane.
11412 */
11413 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11414 return -EBUSY;
11415
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011416 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011417 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011418 return -EINVAL;
11419
11420 /*
11421 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11422 * Note that pitch changes could also affect these register.
11423 */
11424 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011425 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11426 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011427 return -EINVAL;
11428
Chris Wilsonf900db42014-02-20 09:26:13 +000011429 if (i915_terminally_wedged(&dev_priv->gpu_error))
11430 goto out_hang;
11431
Daniel Vetterb14c5672013-09-19 12:18:32 +020011432 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011433 if (work == NULL)
11434 return -ENOMEM;
11435
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011436 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011437 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011438 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011439 INIT_WORK(&work->work, intel_unpin_work_fn);
11440
Daniel Vetter87b6b102014-05-15 15:33:46 +020011441 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011442 if (ret)
11443 goto free_work;
11444
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011445 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011446 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011447 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011448 /* Before declaring the flip queue wedged, check if
11449 * the hardware completed the operation behind our backs.
11450 */
11451 if (__intel_pageflip_stall_check(dev, crtc)) {
11452 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11453 page_flip_completed(intel_crtc);
11454 } else {
11455 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011456 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011457
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011458 drm_crtc_vblank_put(crtc);
11459 kfree(work);
11460 return -EBUSY;
11461 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011462 }
11463 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011464 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011465
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011466 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11467 flush_workqueue(dev_priv->wq);
11468
Jesse Barnes75dfca82010-02-10 15:09:44 -080011469 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011470 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011471 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011472
Matt Roperf4510a22014-04-01 15:22:40 -070011473 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011474 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011475
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011476 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011477
Chris Wilson89ed88b2015-02-16 14:31:49 +000011478 ret = i915_mutex_lock_interruptible(dev);
11479 if (ret)
11480 goto cleanup;
11481
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011482 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011483 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011484
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011485 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011486 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011487
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011488 if (IS_VALLEYVIEW(dev)) {
11489 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011490 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011491 /* vlv: DISPLAY_FLIP fails to change tiling */
11492 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011493 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011494 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011495 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011496 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011497 if (ring == NULL || ring->id != RCS)
11498 ring = &dev_priv->ring[BCS];
11499 } else {
11500 ring = &dev_priv->ring[RCS];
11501 }
11502
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011503 mmio_flip = use_mmio_flip(ring, obj);
11504
11505 /* When using CS flips, we want to emit semaphores between rings.
11506 * However, when using mmio flips we will create a task to do the
11507 * synchronisation, so all we want here is to pin the framebuffer
11508 * into the display plane and skip any waits.
11509 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011510 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011511 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011512 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011513 if (ret)
11514 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011515
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011516 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11517 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011518
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011519 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011520 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11521 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011522 if (ret)
11523 goto cleanup_unpin;
11524
John Harrisonf06cc1b2014-11-24 18:49:37 +000011525 i915_gem_request_assign(&work->flip_queued_req,
11526 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011527 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011528 if (!request) {
11529 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11530 if (ret)
11531 goto cleanup_unpin;
11532 }
11533
11534 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011535 page_flip_flags);
11536 if (ret)
11537 goto cleanup_unpin;
11538
John Harrison6258fbe2015-05-29 17:43:48 +010011539 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011540 }
11541
John Harrison91af1272015-06-18 13:14:56 +010011542 if (request)
John Harrison75289872015-05-29 17:43:49 +010011543 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011544
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011545 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011546 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011547
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011548 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011549 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011550 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011551
Paulo Zanoni7733b492015-07-07 15:26:04 -030011552 intel_fbc_disable(dev_priv);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011553 intel_frontbuffer_flip_prepare(dev,
11554 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011555
Jesse Barnese5510fa2010-07-01 16:48:37 -070011556 trace_i915_flip_request(intel_crtc->plane, obj);
11557
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011558 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011559
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011560cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011561 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011562cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011563 if (request)
11564 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011565 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011566 mutex_unlock(&dev->struct_mutex);
11567cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011568 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011569 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011570
Chris Wilson89ed88b2015-02-16 14:31:49 +000011571 drm_gem_object_unreference_unlocked(&obj->base);
11572 drm_framebuffer_unreference(work->old_fb);
11573
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011574 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011575 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011576 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011577
Daniel Vetter87b6b102014-05-15 15:33:46 +020011578 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011579free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011580 kfree(work);
11581
Chris Wilsonf900db42014-02-20 09:26:13 +000011582 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011583 struct drm_atomic_state *state;
11584 struct drm_plane_state *plane_state;
11585
Chris Wilsonf900db42014-02-20 09:26:13 +000011586out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011587 state = drm_atomic_state_alloc(dev);
11588 if (!state)
11589 return -ENOMEM;
11590 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11591
11592retry:
11593 plane_state = drm_atomic_get_plane_state(state, primary);
11594 ret = PTR_ERR_OR_ZERO(plane_state);
11595 if (!ret) {
11596 drm_atomic_set_fb_for_plane(plane_state, fb);
11597
11598 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11599 if (!ret)
11600 ret = drm_atomic_commit(state);
11601 }
11602
11603 if (ret == -EDEADLK) {
11604 drm_modeset_backoff(state->acquire_ctx);
11605 drm_atomic_state_clear(state);
11606 goto retry;
11607 }
11608
11609 if (ret)
11610 drm_atomic_state_free(state);
11611
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011612 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011613 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011614 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011615 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011616 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011617 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011618 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011619}
11620
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011621
11622/**
11623 * intel_wm_need_update - Check whether watermarks need updating
11624 * @plane: drm plane
11625 * @state: new plane state
11626 *
11627 * Check current plane state versus the new one to determine whether
11628 * watermarks need to be recalculated.
11629 *
11630 * Returns true or false.
11631 */
11632static bool intel_wm_need_update(struct drm_plane *plane,
11633 struct drm_plane_state *state)
11634{
11635 /* Update watermarks on tiling changes. */
11636 if (!plane->state->fb || !state->fb ||
11637 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11638 plane->state->rotation != state->rotation)
11639 return true;
11640
11641 if (plane->state->crtc_w != state->crtc_w)
11642 return true;
11643
11644 return false;
11645}
11646
11647int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11648 struct drm_plane_state *plane_state)
11649{
11650 struct drm_crtc *crtc = crtc_state->crtc;
11651 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11652 struct drm_plane *plane = plane_state->plane;
11653 struct drm_device *dev = crtc->dev;
11654 struct drm_i915_private *dev_priv = dev->dev_private;
11655 struct intel_plane_state *old_plane_state =
11656 to_intel_plane_state(plane->state);
11657 int idx = intel_crtc->base.base.id, ret;
11658 int i = drm_plane_index(plane);
11659 bool mode_changed = needs_modeset(crtc_state);
11660 bool was_crtc_enabled = crtc->state->active;
11661 bool is_crtc_enabled = crtc_state->active;
11662
11663 bool turn_off, turn_on, visible, was_visible;
11664 struct drm_framebuffer *fb = plane_state->fb;
11665
11666 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11667 plane->type != DRM_PLANE_TYPE_CURSOR) {
11668 ret = skl_update_scaler_plane(
11669 to_intel_crtc_state(crtc_state),
11670 to_intel_plane_state(plane_state));
11671 if (ret)
11672 return ret;
11673 }
11674
11675 /*
11676 * Disabling a plane is always okay; we just need to update
11677 * fb tracking in a special way since cleanup_fb() won't
11678 * get called by the plane helpers.
11679 */
11680 if (old_plane_state->base.fb && !fb)
11681 intel_crtc->atomic.disabled_planes |= 1 << i;
11682
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011683 was_visible = old_plane_state->visible;
11684 visible = to_intel_plane_state(plane_state)->visible;
11685
11686 if (!was_crtc_enabled && WARN_ON(was_visible))
11687 was_visible = false;
11688
11689 if (!is_crtc_enabled && WARN_ON(visible))
11690 visible = false;
11691
11692 if (!was_visible && !visible)
11693 return 0;
11694
11695 turn_off = was_visible && (!visible || mode_changed);
11696 turn_on = visible && (!was_visible || mode_changed);
11697
11698 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11699 plane->base.id, fb ? fb->base.id : -1);
11700
11701 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11702 plane->base.id, was_visible, visible,
11703 turn_off, turn_on, mode_changed);
11704
Ville Syrjälä852eb002015-06-24 22:00:07 +030011705 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011706 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011707 /* must disable cxsr around plane enable/disable */
11708 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11709 intel_crtc->atomic.disable_cxsr = true;
11710 /* to potentially re-enable cxsr */
11711 intel_crtc->atomic.wait_vblank = true;
11712 intel_crtc->atomic.update_wm_post = true;
11713 }
11714 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011715 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011716 /* must disable cxsr around plane enable/disable */
11717 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11718 if (is_crtc_enabled)
11719 intel_crtc->atomic.wait_vblank = true;
11720 intel_crtc->atomic.disable_cxsr = true;
11721 }
11722 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011723 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011724 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011725
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011726 if (visible)
11727 intel_crtc->atomic.fb_bits |=
11728 to_intel_plane(plane)->frontbuffer_bit;
11729
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011730 switch (plane->type) {
11731 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011732 intel_crtc->atomic.wait_for_flips = true;
11733 intel_crtc->atomic.pre_disable_primary = turn_off;
11734 intel_crtc->atomic.post_enable_primary = turn_on;
11735
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011736 if (turn_off) {
11737 /*
11738 * FIXME: Actually if we will still have any other
11739 * plane enabled on the pipe we could let IPS enabled
11740 * still, but for now lets consider that when we make
11741 * primary invisible by setting DSPCNTR to 0 on
11742 * update_primary_plane function IPS needs to be
11743 * disable.
11744 */
11745 intel_crtc->atomic.disable_ips = true;
11746
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011747 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf552015-06-26 13:55:54 -070011748 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011749
11750 /*
11751 * FBC does not work on some platforms for rotated
11752 * planes, so disable it when rotation is not 0 and
11753 * update it when rotation is set back to 0.
11754 *
11755 * FIXME: This is redundant with the fbc update done in
11756 * the primary plane enable function except that that
11757 * one is done too late. We eventually need to unify
11758 * this.
11759 */
11760
11761 if (visible &&
11762 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11763 dev_priv->fbc.crtc == intel_crtc &&
11764 plane_state->rotation != BIT(DRM_ROTATE_0))
11765 intel_crtc->atomic.disable_fbc = true;
11766
11767 /*
11768 * BDW signals flip done immediately if the plane
11769 * is disabled, even if the plane enable is already
11770 * armed to occur at the next vblank :(
11771 */
11772 if (turn_on && IS_BROADWELL(dev))
11773 intel_crtc->atomic.wait_vblank = true;
11774
11775 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11776 break;
11777 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011778 break;
11779 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011780 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011781 intel_crtc->atomic.wait_vblank = true;
11782 intel_crtc->atomic.update_sprite_watermarks |=
11783 1 << i;
11784 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011785 }
11786 return 0;
11787}
11788
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011789static bool encoders_cloneable(const struct intel_encoder *a,
11790 const struct intel_encoder *b)
11791{
11792 /* masks could be asymmetric, so check both ways */
11793 return a == b || (a->cloneable & (1 << b->type) &&
11794 b->cloneable & (1 << a->type));
11795}
11796
11797static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11798 struct intel_crtc *crtc,
11799 struct intel_encoder *encoder)
11800{
11801 struct intel_encoder *source_encoder;
11802 struct drm_connector *connector;
11803 struct drm_connector_state *connector_state;
11804 int i;
11805
11806 for_each_connector_in_state(state, connector, connector_state, i) {
11807 if (connector_state->crtc != &crtc->base)
11808 continue;
11809
11810 source_encoder =
11811 to_intel_encoder(connector_state->best_encoder);
11812 if (!encoders_cloneable(encoder, source_encoder))
11813 return false;
11814 }
11815
11816 return true;
11817}
11818
11819static bool check_encoder_cloning(struct drm_atomic_state *state,
11820 struct intel_crtc *crtc)
11821{
11822 struct intel_encoder *encoder;
11823 struct drm_connector *connector;
11824 struct drm_connector_state *connector_state;
11825 int i;
11826
11827 for_each_connector_in_state(state, connector, connector_state, i) {
11828 if (connector_state->crtc != &crtc->base)
11829 continue;
11830
11831 encoder = to_intel_encoder(connector_state->best_encoder);
11832 if (!check_single_encoder_cloning(state, crtc, encoder))
11833 return false;
11834 }
11835
11836 return true;
11837}
11838
11839static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11840 struct drm_crtc_state *crtc_state)
11841{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011842 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011843 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011845 struct intel_crtc_state *pipe_config =
11846 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011847 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011848 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011849 bool mode_changed = needs_modeset(crtc_state);
11850
11851 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11852 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11853 return -EINVAL;
11854 }
11855
11856 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11857 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11858 idx, crtc->state->active, intel_crtc->active);
11859
Ville Syrjälä852eb002015-06-24 22:00:07 +030011860 if (mode_changed && !crtc_state->active)
11861 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011862
Maarten Lankhorstad421372015-06-15 12:33:42 +020011863 if (mode_changed && crtc_state->enable &&
11864 dev_priv->display.crtc_compute_clock &&
11865 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11866 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11867 pipe_config);
11868 if (ret)
11869 return ret;
11870 }
11871
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011872 ret = 0;
11873 if (INTEL_INFO(dev)->gen >= 9) {
11874 if (mode_changed)
11875 ret = skl_update_scaler_crtc(pipe_config);
11876
11877 if (!ret)
11878 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11879 pipe_config);
11880 }
11881
11882 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011883}
11884
Jani Nikula65b38e02015-04-13 11:26:56 +030011885static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011886 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11887 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011888 .atomic_begin = intel_begin_crtc_commit,
11889 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011890 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011891};
11892
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011893static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11894{
11895 struct intel_connector *connector;
11896
11897 for_each_intel_connector(dev, connector) {
11898 if (connector->base.encoder) {
11899 connector->base.state->best_encoder =
11900 connector->base.encoder;
11901 connector->base.state->crtc =
11902 connector->base.encoder->crtc;
11903 } else {
11904 connector->base.state->best_encoder = NULL;
11905 connector->base.state->crtc = NULL;
11906 }
11907 }
11908}
11909
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011910static void
Robin Schroereba905b2014-05-18 02:24:50 +020011911connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011912 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011913{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011914 int bpp = pipe_config->pipe_bpp;
11915
11916 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11917 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011918 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011919
11920 /* Don't use an invalid EDID bpc value */
11921 if (connector->base.display_info.bpc &&
11922 connector->base.display_info.bpc * 3 < bpp) {
11923 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11924 bpp, connector->base.display_info.bpc*3);
11925 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11926 }
11927
11928 /* Clamp bpp to 8 on screens without EDID 1.4 */
11929 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11930 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11931 bpp);
11932 pipe_config->pipe_bpp = 24;
11933 }
11934}
11935
11936static int
11937compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011938 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011939{
11940 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011941 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011942 struct drm_connector *connector;
11943 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011944 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011945
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011946 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011947 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011948 else if (INTEL_INFO(dev)->gen >= 5)
11949 bpp = 12*3;
11950 else
11951 bpp = 8*3;
11952
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011953
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011954 pipe_config->pipe_bpp = bpp;
11955
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011956 state = pipe_config->base.state;
11957
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011958 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011959 for_each_connector_in_state(state, connector, connector_state, i) {
11960 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011961 continue;
11962
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011963 connected_sink_compute_bpp(to_intel_connector(connector),
11964 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011965 }
11966
11967 return bpp;
11968}
11969
Daniel Vetter644db712013-09-19 14:53:58 +020011970static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11971{
11972 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11973 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011974 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011975 mode->crtc_hdisplay, mode->crtc_hsync_start,
11976 mode->crtc_hsync_end, mode->crtc_htotal,
11977 mode->crtc_vdisplay, mode->crtc_vsync_start,
11978 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11979}
11980
Daniel Vetterc0b03412013-05-28 12:05:54 +020011981static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011982 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011983 const char *context)
11984{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011985 struct drm_device *dev = crtc->base.dev;
11986 struct drm_plane *plane;
11987 struct intel_plane *intel_plane;
11988 struct intel_plane_state *state;
11989 struct drm_framebuffer *fb;
11990
11991 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11992 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011993
11994 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11995 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11996 pipe_config->pipe_bpp, pipe_config->dither);
11997 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11998 pipe_config->has_pch_encoder,
11999 pipe_config->fdi_lanes,
12000 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
12001 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
12002 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012003 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
12004 pipe_config->has_dp_encoder,
12005 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
12006 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
12007 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012008
12009 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
12010 pipe_config->has_dp_encoder,
12011 pipe_config->dp_m2_n2.gmch_m,
12012 pipe_config->dp_m2_n2.gmch_n,
12013 pipe_config->dp_m2_n2.link_m,
12014 pipe_config->dp_m2_n2.link_n,
12015 pipe_config->dp_m2_n2.tu);
12016
Daniel Vetter55072d12014-11-20 16:10:28 +010012017 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
12018 pipe_config->has_audio,
12019 pipe_config->has_infoframe);
12020
Daniel Vetterc0b03412013-05-28 12:05:54 +020012021 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012022 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012023 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012024 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12025 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012026 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012027 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12028 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012029 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12030 crtc->num_scalers,
12031 pipe_config->scaler_state.scaler_users,
12032 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012033 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12034 pipe_config->gmch_pfit.control,
12035 pipe_config->gmch_pfit.pgm_ratios,
12036 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012037 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012038 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012039 pipe_config->pch_pfit.size,
12040 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012041 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012042 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012043
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012044 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012045 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012046 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012047 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012048 pipe_config->ddi_pll_sel,
12049 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012050 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012051 pipe_config->dpll_hw_state.pll0,
12052 pipe_config->dpll_hw_state.pll1,
12053 pipe_config->dpll_hw_state.pll2,
12054 pipe_config->dpll_hw_state.pll3,
12055 pipe_config->dpll_hw_state.pll6,
12056 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012057 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012058 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012059 pipe_config->dpll_hw_state.pcsdw12);
12060 } else if (IS_SKYLAKE(dev)) {
12061 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12062 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12063 pipe_config->ddi_pll_sel,
12064 pipe_config->dpll_hw_state.ctrl1,
12065 pipe_config->dpll_hw_state.cfgcr1,
12066 pipe_config->dpll_hw_state.cfgcr2);
12067 } else if (HAS_DDI(dev)) {
12068 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12069 pipe_config->ddi_pll_sel,
12070 pipe_config->dpll_hw_state.wrpll);
12071 } else {
12072 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12073 "fp0: 0x%x, fp1: 0x%x\n",
12074 pipe_config->dpll_hw_state.dpll,
12075 pipe_config->dpll_hw_state.dpll_md,
12076 pipe_config->dpll_hw_state.fp0,
12077 pipe_config->dpll_hw_state.fp1);
12078 }
12079
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012080 DRM_DEBUG_KMS("planes on this crtc\n");
12081 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12082 intel_plane = to_intel_plane(plane);
12083 if (intel_plane->pipe != crtc->pipe)
12084 continue;
12085
12086 state = to_intel_plane_state(plane->state);
12087 fb = state->base.fb;
12088 if (!fb) {
12089 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12090 "disabled, scaler_id = %d\n",
12091 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12092 plane->base.id, intel_plane->pipe,
12093 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12094 drm_plane_index(plane), state->scaler_id);
12095 continue;
12096 }
12097
12098 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12099 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12100 plane->base.id, intel_plane->pipe,
12101 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12102 drm_plane_index(plane));
12103 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12104 fb->base.id, fb->width, fb->height, fb->pixel_format);
12105 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12106 state->scaler_id,
12107 state->src.x1 >> 16, state->src.y1 >> 16,
12108 drm_rect_width(&state->src) >> 16,
12109 drm_rect_height(&state->src) >> 16,
12110 state->dst.x1, state->dst.y1,
12111 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12112 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012113}
12114
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012115static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012116{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012117 struct drm_device *dev = state->dev;
12118 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012119 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012120 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012121 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012122 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012123
12124 /*
12125 * Walk the connector list instead of the encoder
12126 * list to detect the problem on ddi platforms
12127 * where there's just one encoder per digital port.
12128 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012129 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012130 if (!connector_state->best_encoder)
12131 continue;
12132
12133 encoder = to_intel_encoder(connector_state->best_encoder);
12134
12135 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012136
12137 switch (encoder->type) {
12138 unsigned int port_mask;
12139 case INTEL_OUTPUT_UNKNOWN:
12140 if (WARN_ON(!HAS_DDI(dev)))
12141 break;
12142 case INTEL_OUTPUT_DISPLAYPORT:
12143 case INTEL_OUTPUT_HDMI:
12144 case INTEL_OUTPUT_EDP:
12145 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12146
12147 /* the same port mustn't appear more than once */
12148 if (used_ports & port_mask)
12149 return false;
12150
12151 used_ports |= port_mask;
12152 default:
12153 break;
12154 }
12155 }
12156
12157 return true;
12158}
12159
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012160static void
12161clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12162{
12163 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012164 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012165 struct intel_dpll_hw_state dpll_hw_state;
12166 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012167 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012168
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012169 /* FIXME: before the switch to atomic started, a new pipe_config was
12170 * kzalloc'd. Code that depends on any field being zero should be
12171 * fixed, so that the crtc_state can be safely duplicated. For now,
12172 * only fields that are know to not cause problems are preserved. */
12173
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012174 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012175 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012176 shared_dpll = crtc_state->shared_dpll;
12177 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012178 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012179
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012180 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012181
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012182 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012183 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012184 crtc_state->shared_dpll = shared_dpll;
12185 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012186 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012187}
12188
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012189static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012190intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012191 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012192{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012193 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012194 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012195 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012196 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012197 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012198 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012199 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012200
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012201 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012202
Daniel Vettere143a212013-07-04 12:01:15 +020012203 pipe_config->cpu_transcoder =
12204 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012205
Imre Deak2960bc92013-07-30 13:36:32 +030012206 /*
12207 * Sanitize sync polarity flags based on requested ones. If neither
12208 * positive or negative polarity is requested, treat this as meaning
12209 * negative polarity.
12210 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012211 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012212 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012213 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012214
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012215 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012216 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012217 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012218
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012219 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12220 * plane pixel format and any sink constraints into account. Returns the
12221 * source plane bpp so that dithering can be selected on mismatches
12222 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012223 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12224 pipe_config);
12225 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012226 goto fail;
12227
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012228 /*
12229 * Determine the real pipe dimensions. Note that stereo modes can
12230 * increase the actual pipe size due to the frame doubling and
12231 * insertion of additional space for blanks between the frame. This
12232 * is stored in the crtc timings. We use the requested mode to do this
12233 * computation to clearly distinguish it from the adjusted mode, which
12234 * can be changed by the connectors in the below retry loop.
12235 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012236 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012237 &pipe_config->pipe_src_w,
12238 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012239
Daniel Vettere29c22c2013-02-21 00:00:16 +010012240encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012241 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012242 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012243 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012244
Daniel Vetter135c81b2013-07-21 21:37:09 +020012245 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012246 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12247 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012248
Daniel Vetter7758a112012-07-08 19:40:39 +020012249 /* Pass our mode to the connectors and the CRTC to give them a chance to
12250 * adjust it according to limitations or connector properties, and also
12251 * a chance to reject the mode entirely.
12252 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012253 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012254 if (connector_state->crtc != crtc)
12255 continue;
12256
12257 encoder = to_intel_encoder(connector_state->best_encoder);
12258
Daniel Vetterefea6e82013-07-21 21:36:59 +020012259 if (!(encoder->compute_config(encoder, pipe_config))) {
12260 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012261 goto fail;
12262 }
12263 }
12264
Daniel Vetterff9a6752013-06-01 17:16:21 +020012265 /* Set default port clock if not overwritten by the encoder. Needs to be
12266 * done afterwards in case the encoder adjusts the mode. */
12267 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012268 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012269 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012270
Daniel Vettera43f6e02013-06-07 23:10:32 +020012271 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012272 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012273 DRM_DEBUG_KMS("CRTC fixup failed\n");
12274 goto fail;
12275 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012276
12277 if (ret == RETRY) {
12278 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12279 ret = -EINVAL;
12280 goto fail;
12281 }
12282
12283 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12284 retry = false;
12285 goto encoder_retry;
12286 }
12287
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012288 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012289 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012290 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012291
Daniel Vetter7758a112012-07-08 19:40:39 +020012292fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012293 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012294}
12295
Daniel Vetterea9d7582012-07-10 10:42:52 +020012296static bool intel_crtc_in_use(struct drm_crtc *crtc)
12297{
12298 struct drm_encoder *encoder;
12299 struct drm_device *dev = crtc->dev;
12300
12301 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12302 if (encoder->crtc == crtc)
12303 return true;
12304
12305 return false;
12306}
12307
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012308static void
12309intel_modeset_update_state(struct drm_atomic_state *state)
12310{
12311 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012312 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012313 struct drm_crtc *crtc;
12314 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012315 struct drm_connector *connector;
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012316 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012317
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012318 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012319
Damien Lespiaub2784e12014-08-05 11:29:37 +010012320 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012321 if (!intel_encoder->base.crtc)
12322 continue;
12323
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012324 crtc = intel_encoder->base.crtc;
12325 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12326 if (!crtc_state || !needs_modeset(crtc->state))
12327 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012328
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012329 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012330 }
12331
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012332 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012333
Ville Syrjälä76688512014-01-10 11:28:06 +020012334 /* Double check state. */
Maarten Lankhorst8a75d152015-07-13 16:30:14 +020012335 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012336 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012337
12338 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012339
12340 /* Update hwmode for vblank functions */
12341 if (crtc->state->active)
12342 crtc->hwmode = crtc->state->adjusted_mode;
12343 else
12344 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012345 }
12346
12347 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12348 if (!connector->encoder || !connector->encoder->crtc)
12349 continue;
12350
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012351 crtc = connector->encoder->crtc;
12352 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12353 if (!crtc_state || !needs_modeset(crtc->state))
12354 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012355
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012356 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012357 struct drm_property *dpms_property =
12358 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012359
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012360 connector->dpms = DRM_MODE_DPMS_ON;
12361 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012362
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012363 intel_encoder = to_intel_encoder(connector->encoder);
12364 intel_encoder->connectors_active = true;
12365 } else
12366 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012367 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012368}
12369
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012370static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012371{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012372 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012373
12374 if (clock1 == clock2)
12375 return true;
12376
12377 if (!clock1 || !clock2)
12378 return false;
12379
12380 diff = abs(clock1 - clock2);
12381
12382 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12383 return true;
12384
12385 return false;
12386}
12387
Daniel Vetter25c5b262012-07-08 22:08:04 +020012388#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12389 list_for_each_entry((intel_crtc), \
12390 &(dev)->mode_config.crtc_list, \
12391 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012392 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012393
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012394
12395static bool
12396intel_compare_m_n(unsigned int m, unsigned int n,
12397 unsigned int m2, unsigned int n2,
12398 bool exact)
12399{
12400 if (m == m2 && n == n2)
12401 return true;
12402
12403 if (exact || !m || !n || !m2 || !n2)
12404 return false;
12405
12406 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12407
12408 if (m > m2) {
12409 while (m > m2) {
12410 m2 <<= 1;
12411 n2 <<= 1;
12412 }
12413 } else if (m < m2) {
12414 while (m < m2) {
12415 m <<= 1;
12416 n <<= 1;
12417 }
12418 }
12419
12420 return m == m2 && n == n2;
12421}
12422
12423static bool
12424intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12425 struct intel_link_m_n *m2_n2,
12426 bool adjust)
12427{
12428 if (m_n->tu == m2_n2->tu &&
12429 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12430 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12431 intel_compare_m_n(m_n->link_m, m_n->link_n,
12432 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12433 if (adjust)
12434 *m2_n2 = *m_n;
12435
12436 return true;
12437 }
12438
12439 return false;
12440}
12441
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012442static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012443intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012444 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012445 struct intel_crtc_state *pipe_config,
12446 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012447{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012448 bool ret = true;
12449
12450#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12451 do { \
12452 if (!adjust) \
12453 DRM_ERROR(fmt, ##__VA_ARGS__); \
12454 else \
12455 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12456 } while (0)
12457
Daniel Vetter66e985c2013-06-05 13:34:20 +020012458#define PIPE_CONF_CHECK_X(name) \
12459 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012460 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012461 "(expected 0x%08x, found 0x%08x)\n", \
12462 current_config->name, \
12463 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012464 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012465 }
12466
Daniel Vetter08a24032013-04-19 11:25:34 +020012467#define PIPE_CONF_CHECK_I(name) \
12468 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012469 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012470 "(expected %i, found %i)\n", \
12471 current_config->name, \
12472 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012473 ret = false; \
12474 }
12475
12476#define PIPE_CONF_CHECK_M_N(name) \
12477 if (!intel_compare_link_m_n(&current_config->name, \
12478 &pipe_config->name,\
12479 adjust)) { \
12480 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12481 "(expected tu %i gmch %i/%i link %i/%i, " \
12482 "found tu %i, gmch %i/%i link %i/%i)\n", \
12483 current_config->name.tu, \
12484 current_config->name.gmch_m, \
12485 current_config->name.gmch_n, \
12486 current_config->name.link_m, \
12487 current_config->name.link_n, \
12488 pipe_config->name.tu, \
12489 pipe_config->name.gmch_m, \
12490 pipe_config->name.gmch_n, \
12491 pipe_config->name.link_m, \
12492 pipe_config->name.link_n); \
12493 ret = false; \
12494 }
12495
12496#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12497 if (!intel_compare_link_m_n(&current_config->name, \
12498 &pipe_config->name, adjust) && \
12499 !intel_compare_link_m_n(&current_config->alt_name, \
12500 &pipe_config->name, adjust)) { \
12501 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12502 "(expected tu %i gmch %i/%i link %i/%i, " \
12503 "or tu %i gmch %i/%i link %i/%i, " \
12504 "found tu %i, gmch %i/%i link %i/%i)\n", \
12505 current_config->name.tu, \
12506 current_config->name.gmch_m, \
12507 current_config->name.gmch_n, \
12508 current_config->name.link_m, \
12509 current_config->name.link_n, \
12510 current_config->alt_name.tu, \
12511 current_config->alt_name.gmch_m, \
12512 current_config->alt_name.gmch_n, \
12513 current_config->alt_name.link_m, \
12514 current_config->alt_name.link_n, \
12515 pipe_config->name.tu, \
12516 pipe_config->name.gmch_m, \
12517 pipe_config->name.gmch_n, \
12518 pipe_config->name.link_m, \
12519 pipe_config->name.link_n); \
12520 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012521 }
12522
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012523/* This is required for BDW+ where there is only one set of registers for
12524 * switching between high and low RR.
12525 * This macro can be used whenever a comparison has to be made between one
12526 * hw state and multiple sw state variables.
12527 */
12528#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12529 if ((current_config->name != pipe_config->name) && \
12530 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012531 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012532 "(expected %i or %i, found %i)\n", \
12533 current_config->name, \
12534 current_config->alt_name, \
12535 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012536 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012537 }
12538
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012539#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12540 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012541 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012542 "(expected %i, found %i)\n", \
12543 current_config->name & (mask), \
12544 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012545 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012546 }
12547
Ville Syrjälä5e550652013-09-06 23:29:07 +030012548#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12549 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012550 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012551 "(expected %i, found %i)\n", \
12552 current_config->name, \
12553 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012554 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012555 }
12556
Daniel Vetterbb760062013-06-06 14:55:52 +020012557#define PIPE_CONF_QUIRK(quirk) \
12558 ((current_config->quirks | pipe_config->quirks) & (quirk))
12559
Daniel Vettereccb1402013-05-22 00:50:22 +020012560 PIPE_CONF_CHECK_I(cpu_transcoder);
12561
Daniel Vetter08a24032013-04-19 11:25:34 +020012562 PIPE_CONF_CHECK_I(has_pch_encoder);
12563 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012564 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012565
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012566 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012567
12568 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012569 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012570
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012571 PIPE_CONF_CHECK_I(has_drrs);
12572 if (current_config->has_drrs)
12573 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12574 } else
12575 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012576
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012577 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12578 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12579 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12580 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12581 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12582 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012583
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012584 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12585 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12586 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12587 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12588 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12589 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012590
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012591 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012592 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012593 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12594 IS_VALLEYVIEW(dev))
12595 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012596 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012597
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012598 PIPE_CONF_CHECK_I(has_audio);
12599
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012600 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012601 DRM_MODE_FLAG_INTERLACE);
12602
Daniel Vetterbb760062013-06-06 14:55:52 +020012603 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012604 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012605 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012606 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012607 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012608 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012609 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012610 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012611 DRM_MODE_FLAG_NVSYNC);
12612 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012613
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012614 PIPE_CONF_CHECK_I(pipe_src_w);
12615 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012616
Daniel Vetter99535992014-04-13 12:00:33 +020012617 /*
12618 * FIXME: BIOS likes to set up a cloned config with lvds+external
12619 * screen. Since we don't yet re-compute the pipe config when moving
12620 * just the lvds port away to another pipe the sw tracking won't match.
12621 *
12622 * Proper atomic modesets with recomputed global state will fix this.
12623 * Until then just don't check gmch state for inherited modes.
12624 */
12625 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12626 PIPE_CONF_CHECK_I(gmch_pfit.control);
12627 /* pfit ratios are autocomputed by the hw on gen4+ */
12628 if (INTEL_INFO(dev)->gen < 4)
12629 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12630 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12631 }
12632
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012633 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12634 if (current_config->pch_pfit.enabled) {
12635 PIPE_CONF_CHECK_I(pch_pfit.pos);
12636 PIPE_CONF_CHECK_I(pch_pfit.size);
12637 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012638
Chandra Kondurua1b22782015-04-07 15:28:45 -070012639 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12640
Jesse Barnese59150d2014-01-07 13:30:45 -080012641 /* BDW+ don't expose a synchronous way to read the state */
12642 if (IS_HASWELL(dev))
12643 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012644
Ville Syrjälä282740f2013-09-04 18:30:03 +030012645 PIPE_CONF_CHECK_I(double_wide);
12646
Daniel Vetter26804af2014-06-25 22:01:55 +030012647 PIPE_CONF_CHECK_X(ddi_pll_sel);
12648
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012649 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012650 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012651 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012652 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12653 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012654 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012655 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12656 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12657 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012658
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012659 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12660 PIPE_CONF_CHECK_I(pipe_bpp);
12661
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012662 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012663 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012664
Daniel Vetter66e985c2013-06-05 13:34:20 +020012665#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012666#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012667#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012668#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012669#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012670#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012671#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012672
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012673 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012674}
12675
Damien Lespiau08db6652014-11-04 17:06:52 +000012676static void check_wm_state(struct drm_device *dev)
12677{
12678 struct drm_i915_private *dev_priv = dev->dev_private;
12679 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12680 struct intel_crtc *intel_crtc;
12681 int plane;
12682
12683 if (INTEL_INFO(dev)->gen < 9)
12684 return;
12685
12686 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12687 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12688
12689 for_each_intel_crtc(dev, intel_crtc) {
12690 struct skl_ddb_entry *hw_entry, *sw_entry;
12691 const enum pipe pipe = intel_crtc->pipe;
12692
12693 if (!intel_crtc->active)
12694 continue;
12695
12696 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012697 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012698 hw_entry = &hw_ddb.plane[pipe][plane];
12699 sw_entry = &sw_ddb->plane[pipe][plane];
12700
12701 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12702 continue;
12703
12704 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12705 "(expected (%u,%u), found (%u,%u))\n",
12706 pipe_name(pipe), plane + 1,
12707 sw_entry->start, sw_entry->end,
12708 hw_entry->start, hw_entry->end);
12709 }
12710
12711 /* cursor */
12712 hw_entry = &hw_ddb.cursor[pipe];
12713 sw_entry = &sw_ddb->cursor[pipe];
12714
12715 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12716 continue;
12717
12718 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12719 "(expected (%u,%u), found (%u,%u))\n",
12720 pipe_name(pipe),
12721 sw_entry->start, sw_entry->end,
12722 hw_entry->start, hw_entry->end);
12723 }
12724}
12725
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012726static void
12727check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012728{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012729 struct intel_connector *connector;
12730
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012731 for_each_intel_connector(dev, connector) {
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012732 struct drm_encoder *encoder = connector->base.encoder;
12733 struct drm_connector_state *state = connector->base.state;
12734
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012735 /* This also checks the encoder/connector hw state with the
12736 * ->get_hw_state callbacks. */
12737 intel_connector_check_state(connector);
12738
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012739 I915_STATE_WARN(state->best_encoder != encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012740 "connector's staged encoder doesn't match current encoder\n");
12741 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012742}
12743
12744static void
12745check_encoder_state(struct drm_device *dev)
12746{
12747 struct intel_encoder *encoder;
12748 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012749
Damien Lespiaub2784e12014-08-05 11:29:37 +010012750 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012751 bool enabled = false;
12752 bool active = false;
12753 enum pipe pipe, tracked_pipe;
12754
12755 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12756 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012757 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012758
Rob Clarke2c719b2014-12-15 13:56:32 -050012759 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012760 "encoder's active_connectors set, but no crtc\n");
12761
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012762 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012763 if (connector->base.encoder != &encoder->base)
12764 continue;
12765 enabled = true;
12766 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12767 active = true;
Maarten Lankhorstad3c5582015-07-13 16:30:26 +020012768
12769 I915_STATE_WARN(connector->base.state->crtc !=
12770 encoder->base.crtc,
12771 "connector's crtc doesn't match encoder crtc\n");
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012772 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012773 /*
12774 * for MST connectors if we unplug the connector is gone
12775 * away but the encoder is still connected to a crtc
12776 * until a modeset happens in response to the hotplug.
12777 */
12778 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12779 continue;
12780
Rob Clarke2c719b2014-12-15 13:56:32 -050012781 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012782 "encoder's enabled state mismatch "
12783 "(expected %i, found %i)\n",
12784 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012785 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012786 "active encoder with no crtc\n");
12787
Rob Clarke2c719b2014-12-15 13:56:32 -050012788 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012789 "encoder's computed active state doesn't match tracked active state "
12790 "(expected %i, found %i)\n", active, encoder->connectors_active);
12791
12792 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012793 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012794 "encoder's hw state doesn't match sw tracking "
12795 "(expected %i, found %i)\n",
12796 encoder->connectors_active, active);
12797
12798 if (!encoder->base.crtc)
12799 continue;
12800
12801 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012802 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012803 "active encoder's pipe doesn't match"
12804 "(expected %i, found %i)\n",
12805 tracked_pipe, pipe);
12806
12807 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012808}
12809
12810static void
12811check_crtc_state(struct drm_device *dev)
12812{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012813 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012814 struct intel_crtc *crtc;
12815 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012816 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012817
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012818 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012819 bool enabled = false;
12820 bool active = false;
12821
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012822 memset(&pipe_config, 0, sizeof(pipe_config));
12823
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012824 DRM_DEBUG_KMS("[CRTC:%d]\n",
12825 crtc->base.base.id);
12826
Matt Roper83d65732015-02-25 13:12:16 -080012827 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012828 "active crtc, but not enabled in sw tracking\n");
12829
Damien Lespiaub2784e12014-08-05 11:29:37 +010012830 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012831 if (encoder->base.crtc != &crtc->base)
12832 continue;
12833 enabled = true;
12834 if (encoder->connectors_active)
12835 active = true;
12836 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012837
Rob Clarke2c719b2014-12-15 13:56:32 -050012838 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012839 "crtc's computed active state doesn't match tracked active state "
12840 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012841 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012842 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012843 "(expected %i, found %i)\n", enabled,
12844 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012845
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012846 active = dev_priv->display.get_pipe_config(crtc,
12847 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012848
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012849 /* hw state is inconsistent with the pipe quirk */
12850 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12851 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012852 active = crtc->active;
12853
Damien Lespiaub2784e12014-08-05 11:29:37 +010012854 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012855 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012856 if (encoder->base.crtc != &crtc->base)
12857 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012858 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012859 encoder->get_config(encoder, &pipe_config);
12860 }
12861
Rob Clarke2c719b2014-12-15 13:56:32 -050012862 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012863 "crtc active state doesn't match with hw state "
12864 "(expected %i, found %i)\n", crtc->active, active);
12865
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012866 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12867 "transitional active state does not match atomic hw state "
12868 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12869
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012870 if (!active)
12871 continue;
12872
12873 if (!intel_pipe_config_compare(dev, crtc->config,
12874 &pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012875 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012876 intel_dump_pipe_config(crtc, &pipe_config,
12877 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012878 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012879 "[sw state]");
12880 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012881 }
12882}
12883
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012884static void
12885check_shared_dpll_state(struct drm_device *dev)
12886{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012887 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012888 struct intel_crtc *crtc;
12889 struct intel_dpll_hw_state dpll_hw_state;
12890 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012891
12892 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12893 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12894 int enabled_crtcs = 0, active_crtcs = 0;
12895 bool active;
12896
12897 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12898
12899 DRM_DEBUG_KMS("%s\n", pll->name);
12900
12901 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12902
Rob Clarke2c719b2014-12-15 13:56:32 -050012903 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012904 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012905 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012906 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012907 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012908 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012909 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012910 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012911 "pll on state mismatch (expected %i, found %i)\n",
12912 pll->on, active);
12913
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012914 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012915 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012916 enabled_crtcs++;
12917 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12918 active_crtcs++;
12919 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012920 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012921 "pll active crtcs mismatch (expected %i, found %i)\n",
12922 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012923 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012924 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012925 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012926
Rob Clarke2c719b2014-12-15 13:56:32 -050012927 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012928 sizeof(dpll_hw_state)),
12929 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012930 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012931}
12932
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012933void
12934intel_modeset_check_state(struct drm_device *dev)
12935{
Damien Lespiau08db6652014-11-04 17:06:52 +000012936 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012937 check_connector_state(dev);
12938 check_encoder_state(dev);
12939 check_crtc_state(dev);
12940 check_shared_dpll_state(dev);
12941}
12942
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012943void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012944 int dotclock)
12945{
12946 /*
12947 * FDI already provided one idea for the dotclock.
12948 * Yell if the encoder disagrees.
12949 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012950 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012951 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012952 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012953}
12954
Ville Syrjälä80715b22014-05-15 20:23:23 +030012955static void update_scanline_offset(struct intel_crtc *crtc)
12956{
12957 struct drm_device *dev = crtc->base.dev;
12958
12959 /*
12960 * The scanline counter increments at the leading edge of hsync.
12961 *
12962 * On most platforms it starts counting from vtotal-1 on the
12963 * first active line. That means the scanline counter value is
12964 * always one less than what we would expect. Ie. just after
12965 * start of vblank, which also occurs at start of hsync (on the
12966 * last active line), the scanline counter will read vblank_start-1.
12967 *
12968 * On gen2 the scanline counter starts counting from 1 instead
12969 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12970 * to keep the value positive), instead of adding one.
12971 *
12972 * On HSW+ the behaviour of the scanline counter depends on the output
12973 * type. For DP ports it behaves like most other platforms, but on HDMI
12974 * there's an extra 1 line difference. So we need to add two instead of
12975 * one to the value.
12976 */
12977 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012978 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012979 int vtotal;
12980
12981 vtotal = mode->crtc_vtotal;
12982 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12983 vtotal /= 2;
12984
12985 crtc->scanline_offset = vtotal - 1;
12986 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012987 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012988 crtc->scanline_offset = 2;
12989 } else
12990 crtc->scanline_offset = 1;
12991}
12992
Maarten Lankhorstad421372015-06-15 12:33:42 +020012993static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012994{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012995 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012996 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012997 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012998 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012999 struct intel_crtc_state *intel_crtc_state;
13000 struct drm_crtc *crtc;
13001 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013002 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013003
13004 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020013005 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013006
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013007 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020013008 int dpll;
13009
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013010 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030013011 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020013012 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013013
Maarten Lankhorstad421372015-06-15 12:33:42 +020013014 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030013015 continue;
13016
Maarten Lankhorstad421372015-06-15 12:33:42 +020013017 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013018
Maarten Lankhorstad421372015-06-15 12:33:42 +020013019 if (!shared_dpll)
13020 shared_dpll = intel_atomic_get_shared_dpll_state(state);
13021
13022 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013023 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020013024}
13025
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013026/*
13027 * This implements the workaround described in the "notes" section of the mode
13028 * set sequence documentation. When going from no pipes or single pipe to
13029 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13030 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13031 */
13032static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13033{
13034 struct drm_crtc_state *crtc_state;
13035 struct intel_crtc *intel_crtc;
13036 struct drm_crtc *crtc;
13037 struct intel_crtc_state *first_crtc_state = NULL;
13038 struct intel_crtc_state *other_crtc_state = NULL;
13039 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13040 int i;
13041
13042 /* look at all crtc's that are going to be enabled in during modeset */
13043 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13044 intel_crtc = to_intel_crtc(crtc);
13045
13046 if (!crtc_state->active || !needs_modeset(crtc_state))
13047 continue;
13048
13049 if (first_crtc_state) {
13050 other_crtc_state = to_intel_crtc_state(crtc_state);
13051 break;
13052 } else {
13053 first_crtc_state = to_intel_crtc_state(crtc_state);
13054 first_pipe = intel_crtc->pipe;
13055 }
13056 }
13057
13058 /* No workaround needed? */
13059 if (!first_crtc_state)
13060 return 0;
13061
13062 /* w/a possibly needed, check how many crtc's are already enabled. */
13063 for_each_intel_crtc(state->dev, intel_crtc) {
13064 struct intel_crtc_state *pipe_config;
13065
13066 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13067 if (IS_ERR(pipe_config))
13068 return PTR_ERR(pipe_config);
13069
13070 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13071
13072 if (!pipe_config->base.active ||
13073 needs_modeset(&pipe_config->base))
13074 continue;
13075
13076 /* 2 or more enabled crtcs means no need for w/a */
13077 if (enabled_pipe != INVALID_PIPE)
13078 return 0;
13079
13080 enabled_pipe = intel_crtc->pipe;
13081 }
13082
13083 if (enabled_pipe != INVALID_PIPE)
13084 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13085 else if (other_crtc_state)
13086 other_crtc_state->hsw_workaround_pipe = first_pipe;
13087
13088 return 0;
13089}
13090
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013091static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13092{
13093 struct drm_crtc *crtc;
13094 struct drm_crtc_state *crtc_state;
13095 int ret = 0;
13096
13097 /* add all active pipes to the state */
13098 for_each_crtc(state->dev, crtc) {
13099 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13100 if (IS_ERR(crtc_state))
13101 return PTR_ERR(crtc_state);
13102
13103 if (!crtc_state->active || needs_modeset(crtc_state))
13104 continue;
13105
13106 crtc_state->mode_changed = true;
13107
13108 ret = drm_atomic_add_affected_connectors(state, crtc);
13109 if (ret)
13110 break;
13111
13112 ret = drm_atomic_add_affected_planes(state, crtc);
13113 if (ret)
13114 break;
13115 }
13116
13117 return ret;
13118}
13119
13120
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013121/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013122static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013123{
13124 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013125 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013126 int ret;
13127
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013128 if (!check_digital_port_conflicts(state)) {
13129 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13130 return -EINVAL;
13131 }
13132
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013133 /*
13134 * See if the config requires any additional preparation, e.g.
13135 * to adjust global state with pipes off. We need to do this
13136 * here so we can get the modeset_pipe updated config for the new
13137 * mode set on this crtc. For other crtcs we need to use the
13138 * adjusted_mode bits in the crtc directly.
13139 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013140 if (dev_priv->display.modeset_calc_cdclk) {
13141 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030013142
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013143 ret = dev_priv->display.modeset_calc_cdclk(state);
13144
13145 cdclk = to_intel_atomic_state(state)->cdclk;
13146 if (!ret && cdclk != dev_priv->cdclk_freq)
13147 ret = intel_modeset_all_pipes(state);
13148
13149 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013150 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013151 } else
13152 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013153
Maarten Lankhorstad421372015-06-15 12:33:42 +020013154 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013155
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013156 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013157 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013158
Maarten Lankhorstad421372015-06-15 12:33:42 +020013159 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013160}
13161
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013162static int
13163intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013164{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013165 struct drm_crtc *crtc;
13166 struct drm_crtc_state *crtc_state;
13167 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013168 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013169
13170 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013171 if (ret)
13172 return ret;
13173
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013174 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013175 struct intel_crtc_state *pipe_config =
13176 to_intel_crtc_state(crtc_state);
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013177 bool modeset, recalc = false;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013178
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013179 if (!crtc_state->enable) {
13180 if (needs_modeset(crtc_state))
13181 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013182 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013183 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013184
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013185 modeset = needs_modeset(crtc_state);
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013186 /* see comment in intel_modeset_readout_hw_state */
13187 if (!modeset && crtc_state->mode_blob != crtc->state->mode_blob &&
13188 pipe_config->quirks & PIPE_CONFIG_QUIRK_INHERITED_MODE)
13189 recalc = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013190
13191 if (!modeset && !recalc)
13192 continue;
13193
13194 if (recalc) {
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013195 ret = drm_atomic_add_affected_connectors(state, crtc);
13196 if (ret)
13197 return ret;
13198 }
13199
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013200 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013201 if (ret)
13202 return ret;
13203
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013204 if (recalc && (!i915.fastboot ||
13205 !intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013206 to_intel_crtc_state(crtc->state),
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013207 pipe_config, true))) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013208 modeset = crtc_state->mode_changed = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013209
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013210 ret = drm_atomic_add_affected_planes(state, crtc);
13211 if (ret)
13212 return ret;
13213 }
13214
13215 any_ms = modeset;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013216 intel_dump_pipe_config(to_intel_crtc(crtc),
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013217 pipe_config,
13218 modeset ? "[modeset]" : "[fastboot]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013219 }
13220
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013221 if (any_ms) {
13222 ret = intel_modeset_checks(state);
13223
13224 if (ret)
13225 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013226 } else
13227 to_intel_atomic_state(state)->cdclk =
13228 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013229
13230 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013231}
13232
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013233static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013234{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013235 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013236 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013237 struct drm_crtc *crtc;
13238 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013239 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013240 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013241 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013242
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013243 ret = drm_atomic_helper_prepare_planes(dev, state);
13244 if (ret)
13245 return ret;
13246
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013247 drm_atomic_helper_swap_state(dev, state);
13248
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013249 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013250 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13251
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013252 if (!needs_modeset(crtc->state))
13253 continue;
13254
13255 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013256 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013257
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013258 if (crtc_state->active) {
13259 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13260 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013261 intel_crtc->active = false;
13262 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013263 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013264 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013265
Daniel Vetterea9d7582012-07-10 10:42:52 +020013266 /* Only after disabling all output pipelines that will be changed can we
13267 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013268 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013269
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013270 /* The state has been swaped above, so state actually contains the
13271 * old state now. */
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013272 if (any_ms)
13273 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013274
Daniel Vettera6778b32012-07-02 09:56:42 +020013275 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013276 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013277 if (needs_modeset(crtc->state) && crtc->state->active) {
13278 update_scanline_offset(to_intel_crtc(crtc));
13279 dev_priv->display.crtc_enable(crtc);
13280 }
13281
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013282 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013283 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013284
Daniel Vettera6778b32012-07-02 09:56:42 +020013285 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013286
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013287 drm_atomic_helper_cleanup_planes(dev, state);
13288
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013289 drm_atomic_state_free(state);
13290
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013291 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013292}
13293
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013294static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013295{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013296 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013297 int ret;
13298
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013299 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013300 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013301 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013302
13303 return ret;
13304}
13305
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013306static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013307{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013308 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013309
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013310 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013311 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013312 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013313
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013314 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013315}
13316
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013317void intel_crtc_restore_mode(struct drm_crtc *crtc)
13318{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013319 struct drm_device *dev = crtc->dev;
13320 struct drm_atomic_state *state;
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013321 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013322 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013323
13324 state = drm_atomic_state_alloc(dev);
13325 if (!state) {
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013326 DRM_DEBUG_KMS("[CRTC:%d] crtc restore failed, out of memory",
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013327 crtc->base.id);
13328 return;
13329 }
13330
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013331 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013332
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013333retry:
13334 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13335 ret = PTR_ERR_OR_ZERO(crtc_state);
13336 if (!ret) {
13337 if (!crtc_state->active)
13338 goto out;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013339
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013340 crtc_state->mode_changed = true;
13341 ret = intel_set_mode(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013342 }
13343
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013344 if (ret == -EDEADLK) {
13345 drm_atomic_state_clear(state);
13346 drm_modeset_backoff(state->acquire_ctx);
13347 goto retry;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013348 }
13349
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013350 if (ret)
Maarten Lankhorste694eb02015-07-14 16:19:12 +020013351out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013352 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013353}
13354
Daniel Vetter25c5b262012-07-08 22:08:04 +020013355#undef for_each_intel_crtc_masked
13356
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013357static bool intel_connector_in_mode_set(struct intel_connector *connector,
13358 struct drm_mode_set *set)
13359{
13360 int ro;
13361
13362 for (ro = 0; ro < set->num_connectors; ro++)
13363 if (set->connectors[ro] == &connector->base)
13364 return true;
13365
13366 return false;
13367}
13368
Daniel Vetter2e431052012-07-04 22:42:15 +020013369static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013370intel_modeset_stage_output_state(struct drm_device *dev,
13371 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013372 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013373{
Daniel Vetter9a935852012-07-05 22:34:27 +020013374 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013375 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013376 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013377 struct drm_crtc *crtc;
13378 struct drm_crtc_state *crtc_state;
13379 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013380
Damien Lespiau9abdda72013-02-13 13:29:23 +000013381 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013382 * of connectors. For paranoia, double-check this. */
13383 WARN_ON(!set->fb && (set->num_connectors != 0));
13384 WARN_ON(set->fb && (set->num_connectors == 0));
13385
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013386 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013387 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13388
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013389 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13390 continue;
13391
13392 connector_state =
13393 drm_atomic_get_connector_state(state, &connector->base);
13394 if (IS_ERR(connector_state))
13395 return PTR_ERR(connector_state);
13396
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013397 if (in_mode_set) {
13398 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013399 connector_state->best_encoder =
13400 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013401 }
13402
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013403 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013404 continue;
13405
Daniel Vetter9a935852012-07-05 22:34:27 +020013406 /* If we disable the crtc, disable all its connectors. Also, if
13407 * the connector is on the changing crtc but not on the new
13408 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013409 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013410 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013411
13412 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13413 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013414 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013415 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013416 }
13417 /* connector->new_encoder is now updated for all connectors. */
13418
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013419 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13420 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013421
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013422 if (!connector_state->best_encoder) {
13423 ret = drm_atomic_set_crtc_for_connector(connector_state,
13424 NULL);
13425 if (ret)
13426 return ret;
13427
Daniel Vetter50f56112012-07-02 09:35:43 +020013428 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013429 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013430
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013431 if (intel_connector_in_mode_set(connector, set)) {
13432 struct drm_crtc *crtc = connector->base.state->crtc;
13433
13434 /* If this connector was in a previous crtc, add it
13435 * to the state. We might need to disable it. */
13436 if (crtc) {
13437 crtc_state =
13438 drm_atomic_get_crtc_state(state, crtc);
13439 if (IS_ERR(crtc_state))
13440 return PTR_ERR(crtc_state);
13441 }
13442
13443 ret = drm_atomic_set_crtc_for_connector(connector_state,
13444 set->crtc);
13445 if (ret)
13446 return ret;
13447 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013448
13449 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013450 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13451 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013452 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013453 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013454
Daniel Vetter9a935852012-07-05 22:34:27 +020013455 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13456 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013457 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013458 connector_state->crtc->base.id);
13459
13460 if (connector_state->best_encoder != &connector->encoder->base)
13461 connector->encoder =
13462 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013463 }
13464
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013465 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013466 bool has_connectors;
13467
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013468 ret = drm_atomic_add_affected_connectors(state, crtc);
13469 if (ret)
13470 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013471
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013472 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13473 if (has_connectors != crtc_state->enable)
13474 crtc_state->enable =
13475 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013476 }
13477
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013478 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13479 set->fb, set->x, set->y);
13480 if (ret)
13481 return ret;
13482
13483 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13484 if (IS_ERR(crtc_state))
13485 return PTR_ERR(crtc_state);
13486
Matt Roperce522992015-06-05 15:08:24 -070013487 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13488 if (ret)
13489 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013490
13491 if (set->num_connectors)
13492 crtc_state->active = true;
13493
Daniel Vetter2e431052012-07-04 22:42:15 +020013494 return 0;
13495}
13496
13497static int intel_crtc_set_config(struct drm_mode_set *set)
13498{
13499 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013500 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013501 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013502
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013503 BUG_ON(!set);
13504 BUG_ON(!set->crtc);
13505 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013506
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013507 /* Enforce sane interface api - has been abused by the fb helper. */
13508 BUG_ON(!set->mode && set->fb);
13509 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013510
Daniel Vetter2e431052012-07-04 22:42:15 +020013511 if (set->fb) {
13512 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13513 set->crtc->base.id, set->fb->base.id,
13514 (int)set->num_connectors, set->x, set->y);
13515 } else {
13516 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013517 }
13518
13519 dev = set->crtc->dev;
13520
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013521 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013522 if (!state)
13523 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013524
13525 state->acquire_ctx = dev->mode_config.acquire_ctx;
13526
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013527 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013528 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013529 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013530
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013531 ret = intel_modeset_compute_config(state);
13532 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013533 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013534
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013535 intel_update_pipe_size(to_intel_crtc(set->crtc));
13536
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013537 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013538 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013539 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13540 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013541 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013542
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013543out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013544 if (ret)
13545 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013546 return ret;
13547}
13548
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013549static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013550 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013551 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013552 .destroy = intel_crtc_destroy,
13553 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013554 .atomic_duplicate_state = intel_crtc_duplicate_state,
13555 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013556};
13557
Daniel Vetter53589012013-06-05 13:34:16 +020013558static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13559 struct intel_shared_dpll *pll,
13560 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013561{
Daniel Vetter53589012013-06-05 13:34:16 +020013562 uint32_t val;
13563
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013564 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013565 return false;
13566
Daniel Vetter53589012013-06-05 13:34:16 +020013567 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013568 hw_state->dpll = val;
13569 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13570 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013571
13572 return val & DPLL_VCO_ENABLE;
13573}
13574
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013575static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13576 struct intel_shared_dpll *pll)
13577{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013578 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13579 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013580}
13581
Daniel Vettere7b903d2013-06-05 13:34:14 +020013582static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13583 struct intel_shared_dpll *pll)
13584{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013585 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013586 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013587
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013588 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013589
13590 /* Wait for the clocks to stabilize. */
13591 POSTING_READ(PCH_DPLL(pll->id));
13592 udelay(150);
13593
13594 /* The pixel multiplier can only be updated once the
13595 * DPLL is enabled and the clocks are stable.
13596 *
13597 * So write it again.
13598 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013599 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013600 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013601 udelay(200);
13602}
13603
13604static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13605 struct intel_shared_dpll *pll)
13606{
13607 struct drm_device *dev = dev_priv->dev;
13608 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013609
13610 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013611 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013612 if (intel_crtc_to_shared_dpll(crtc) == pll)
13613 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13614 }
13615
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013616 I915_WRITE(PCH_DPLL(pll->id), 0);
13617 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013618 udelay(200);
13619}
13620
Daniel Vetter46edb022013-06-05 13:34:12 +020013621static char *ibx_pch_dpll_names[] = {
13622 "PCH DPLL A",
13623 "PCH DPLL B",
13624};
13625
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013626static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013627{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013628 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013629 int i;
13630
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013631 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013632
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013633 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013634 dev_priv->shared_dplls[i].id = i;
13635 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013636 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013637 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13638 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013639 dev_priv->shared_dplls[i].get_hw_state =
13640 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013641 }
13642}
13643
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013644static void intel_shared_dpll_init(struct drm_device *dev)
13645{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013646 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013647
Ville Syrjäläb6283052015-06-03 15:45:07 +030013648 intel_update_cdclk(dev);
13649
Daniel Vetter9cd86932014-06-25 22:01:57 +030013650 if (HAS_DDI(dev))
13651 intel_ddi_pll_init(dev);
13652 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013653 ibx_pch_dpll_init(dev);
13654 else
13655 dev_priv->num_shared_dpll = 0;
13656
13657 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013658}
13659
Matt Roper6beb8c232014-12-01 15:40:14 -080013660/**
13661 * intel_prepare_plane_fb - Prepare fb for usage on plane
13662 * @plane: drm plane to prepare for
13663 * @fb: framebuffer to prepare for presentation
13664 *
13665 * Prepares a framebuffer for usage on a display plane. Generally this
13666 * involves pinning the underlying object and updating the frontbuffer tracking
13667 * bits. Some older platforms need special physical address handling for
13668 * cursor planes.
13669 *
13670 * Returns 0 on success, negative error code on failure.
13671 */
13672int
13673intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013674 struct drm_framebuffer *fb,
13675 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013676{
13677 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013678 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013679 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13680 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013681 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013682
Matt Roperea2c67b2014-12-23 10:41:52 -080013683 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013684 return 0;
13685
Matt Roper4c345742014-07-09 16:22:10 -070013686 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013687
Matt Roper6beb8c232014-12-01 15:40:14 -080013688 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13689 INTEL_INFO(dev)->cursor_needs_physical) {
13690 int align = IS_I830(dev) ? 16 * 1024 : 256;
13691 ret = i915_gem_object_attach_phys(obj, align);
13692 if (ret)
13693 DRM_DEBUG_KMS("failed to attach phys object\n");
13694 } else {
John Harrison91af1272015-06-18 13:14:56 +010013695 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013696 }
13697
13698 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013699 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013700
13701 mutex_unlock(&dev->struct_mutex);
13702
13703 return ret;
13704}
13705
Matt Roper38f3ce32014-12-02 07:45:25 -080013706/**
13707 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13708 * @plane: drm plane to clean up for
13709 * @fb: old framebuffer that was on plane
13710 *
13711 * Cleans up a framebuffer that has just been removed from a plane.
13712 */
13713void
13714intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013715 struct drm_framebuffer *fb,
13716 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013717{
13718 struct drm_device *dev = plane->dev;
13719 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13720
13721 if (WARN_ON(!obj))
13722 return;
13723
13724 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13725 !INTEL_INFO(dev)->cursor_needs_physical) {
13726 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013727 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013728 mutex_unlock(&dev->struct_mutex);
13729 }
Matt Roper465c1202014-05-29 08:06:54 -070013730}
13731
Chandra Konduru6156a452015-04-27 13:48:39 -070013732int
13733skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13734{
13735 int max_scale;
13736 struct drm_device *dev;
13737 struct drm_i915_private *dev_priv;
13738 int crtc_clock, cdclk;
13739
13740 if (!intel_crtc || !crtc_state)
13741 return DRM_PLANE_HELPER_NO_SCALING;
13742
13743 dev = intel_crtc->base.dev;
13744 dev_priv = dev->dev_private;
13745 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013746 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013747
13748 if (!crtc_clock || !cdclk)
13749 return DRM_PLANE_HELPER_NO_SCALING;
13750
13751 /*
13752 * skl max scale is lower of:
13753 * close to 3 but not 3, -1 is for that purpose
13754 * or
13755 * cdclk/crtc_clock
13756 */
13757 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13758
13759 return max_scale;
13760}
13761
Matt Roper465c1202014-05-29 08:06:54 -070013762static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013763intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013764 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013765 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013766{
Matt Roper2b875c22014-12-01 15:40:13 -080013767 struct drm_crtc *crtc = state->base.crtc;
13768 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013769 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013770 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13771 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013772
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013773 /* use scaler when colorkey is not required */
13774 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013775 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013776 min_scale = 1;
13777 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013778 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013779 }
Sonika Jindald8106362015-04-10 14:37:28 +053013780
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013781 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13782 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013783 min_scale, max_scale,
13784 can_position, true,
13785 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013786}
13787
Gustavo Padovan14af2932014-10-24 14:51:31 +010013788static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013789intel_commit_primary_plane(struct drm_plane *plane,
13790 struct intel_plane_state *state)
13791{
Matt Roper2b875c22014-12-01 15:40:13 -080013792 struct drm_crtc *crtc = state->base.crtc;
13793 struct drm_framebuffer *fb = state->base.fb;
13794 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013795 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013796 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013797 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013798
Matt Roperea2c67b2014-12-23 10:41:52 -080013799 crtc = crtc ? crtc : plane->crtc;
13800 intel_crtc = to_intel_crtc(crtc);
13801
Matt Ropercf4c7c12014-12-04 10:27:42 -080013802 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013803 crtc->x = src->x1 >> 16;
13804 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013805
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013806 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013807 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013808
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013809 if (state->visible)
13810 /* FIXME: kill this fastboot hack */
13811 intel_update_pipe_size(intel_crtc);
13812
13813 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013814}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013815
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013816static void
13817intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013818 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013819{
13820 struct drm_device *dev = plane->dev;
13821 struct drm_i915_private *dev_priv = dev->dev_private;
13822
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013823 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13824}
13825
Matt Roper32b7eee2014-12-24 07:59:06 -080013826static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13827{
13828 struct drm_device *dev = crtc->dev;
13829 struct drm_i915_private *dev_priv = dev->dev_private;
13830 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013831
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013832 if (!needs_modeset(crtc->state))
13833 intel_pre_plane_update(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013834
Ville Syrjäläf015c552015-06-24 22:00:02 +030013835 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013836 intel_update_watermarks(crtc);
13837
13838 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013839
13840 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013841 if (crtc->state->active)
Matt Roperc34c9ee2014-12-23 10:41:50 -080013842 intel_crtc->atomic.evade =
13843 intel_pipe_update_start(intel_crtc,
13844 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013845
13846 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13847 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013848}
13849
13850static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13851{
13852 struct drm_device *dev = crtc->dev;
13853 struct drm_i915_private *dev_priv = dev->dev_private;
13854 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013855
Matt Roperc34c9ee2014-12-23 10:41:50 -080013856 if (intel_crtc->atomic.evade)
13857 intel_pipe_update_end(intel_crtc,
13858 intel_crtc->atomic.start_vbl_count);
13859
Matt Roper32b7eee2014-12-24 07:59:06 -080013860 intel_runtime_pm_put(dev_priv);
13861
Maarten Lankhorstac21b222015-06-15 12:33:49 +020013862 intel_post_plane_update(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013863}
13864
Matt Ropercf4c7c12014-12-04 10:27:42 -080013865/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013866 * intel_plane_destroy - destroy a plane
13867 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013868 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013869 * Common destruction function for all types of planes (primary, cursor,
13870 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013871 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013872void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013873{
13874 struct intel_plane *intel_plane = to_intel_plane(plane);
13875 drm_plane_cleanup(plane);
13876 kfree(intel_plane);
13877}
13878
Matt Roper65a3fea2015-01-21 16:35:42 -080013879const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013880 .update_plane = drm_atomic_helper_update_plane,
13881 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013882 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013883 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013884 .atomic_get_property = intel_plane_atomic_get_property,
13885 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013886 .atomic_duplicate_state = intel_plane_duplicate_state,
13887 .atomic_destroy_state = intel_plane_destroy_state,
13888
Matt Roper465c1202014-05-29 08:06:54 -070013889};
13890
13891static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13892 int pipe)
13893{
13894 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013895 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013896 const uint32_t *intel_primary_formats;
13897 int num_formats;
13898
13899 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13900 if (primary == NULL)
13901 return NULL;
13902
Matt Roper8e7d6882015-01-21 16:35:41 -080013903 state = intel_create_plane_state(&primary->base);
13904 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013905 kfree(primary);
13906 return NULL;
13907 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013908 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013909
Matt Roper465c1202014-05-29 08:06:54 -070013910 primary->can_scale = false;
13911 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013912 if (INTEL_INFO(dev)->gen >= 9) {
13913 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013914 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013915 }
Matt Roper465c1202014-05-29 08:06:54 -070013916 primary->pipe = pipe;
13917 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013918 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013919 primary->check_plane = intel_check_primary_plane;
13920 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013921 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013922 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13923 primary->plane = !pipe;
13924
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013925 if (INTEL_INFO(dev)->gen >= 9) {
13926 intel_primary_formats = skl_primary_formats;
13927 num_formats = ARRAY_SIZE(skl_primary_formats);
13928 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013929 intel_primary_formats = i965_primary_formats;
13930 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013931 } else {
13932 intel_primary_formats = i8xx_primary_formats;
13933 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013934 }
13935
13936 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013937 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013938 intel_primary_formats, num_formats,
13939 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013940
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013941 if (INTEL_INFO(dev)->gen >= 4)
13942 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013943
Matt Roperea2c67b2014-12-23 10:41:52 -080013944 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13945
Matt Roper465c1202014-05-29 08:06:54 -070013946 return &primary->base;
13947}
13948
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013949void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13950{
13951 if (!dev->mode_config.rotation_property) {
13952 unsigned long flags = BIT(DRM_ROTATE_0) |
13953 BIT(DRM_ROTATE_180);
13954
13955 if (INTEL_INFO(dev)->gen >= 9)
13956 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13957
13958 dev->mode_config.rotation_property =
13959 drm_mode_create_rotation_property(dev, flags);
13960 }
13961 if (dev->mode_config.rotation_property)
13962 drm_object_attach_property(&plane->base.base,
13963 dev->mode_config.rotation_property,
13964 plane->base.state->rotation);
13965}
13966
Matt Roper3d7d6512014-06-10 08:28:13 -070013967static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013968intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013969 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013970 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013971{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013972 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013973 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013974 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013975 unsigned stride;
13976 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013977
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013978 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13979 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013980 DRM_PLANE_HELPER_NO_SCALING,
13981 DRM_PLANE_HELPER_NO_SCALING,
13982 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013983 if (ret)
13984 return ret;
13985
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013986 /* if we want to turn off the cursor ignore width and height */
13987 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013988 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013989
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013990 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013991 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013992 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13993 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013994 return -EINVAL;
13995 }
13996
Matt Roperea2c67b2014-12-23 10:41:52 -080013997 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13998 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013999 DRM_DEBUG_KMS("buffer is too small\n");
14000 return -ENOMEM;
14001 }
14002
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014003 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014004 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014005 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014006 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014007
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014008 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014009}
14010
Matt Roperf4a2cf22014-12-01 15:40:12 -080014011static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014012intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014013 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014014{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014015 intel_crtc_update_cursor(crtc, false);
14016}
14017
14018static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030014019intel_commit_cursor_plane(struct drm_plane *plane,
14020 struct intel_plane_state *state)
14021{
Matt Roper2b875c22014-12-01 15:40:13 -080014022 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014023 struct drm_device *dev = plane->dev;
14024 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014025 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014026 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014027
Matt Roperea2c67b2014-12-23 10:41:52 -080014028 crtc = crtc ? crtc : plane->crtc;
14029 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014030
Matt Roperea2c67b2014-12-23 10:41:52 -080014031 plane->fb = state->base.fb;
14032 crtc->cursor_x = state->base.crtc_x;
14033 crtc->cursor_y = state->base.crtc_y;
14034
Gustavo Padovana912f122014-12-01 15:40:10 -080014035 if (intel_crtc->cursor_bo == obj)
14036 goto update;
14037
Matt Roperf4a2cf22014-12-01 15:40:12 -080014038 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014039 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014040 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014041 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014042 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014043 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014044
Gustavo Padovana912f122014-12-01 15:40:10 -080014045 intel_crtc->cursor_addr = addr;
14046 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080014047
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020014048update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014049 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014050 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014051}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014052
Matt Roper3d7d6512014-06-10 08:28:13 -070014053static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14054 int pipe)
14055{
14056 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014057 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014058
14059 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14060 if (cursor == NULL)
14061 return NULL;
14062
Matt Roper8e7d6882015-01-21 16:35:41 -080014063 state = intel_create_plane_state(&cursor->base);
14064 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014065 kfree(cursor);
14066 return NULL;
14067 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014068 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014069
Matt Roper3d7d6512014-06-10 08:28:13 -070014070 cursor->can_scale = false;
14071 cursor->max_downscale = 1;
14072 cursor->pipe = pipe;
14073 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014074 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014075 cursor->check_plane = intel_check_cursor_plane;
14076 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014077 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014078
14079 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014080 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014081 intel_cursor_formats,
14082 ARRAY_SIZE(intel_cursor_formats),
14083 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014084
14085 if (INTEL_INFO(dev)->gen >= 4) {
14086 if (!dev->mode_config.rotation_property)
14087 dev->mode_config.rotation_property =
14088 drm_mode_create_rotation_property(dev,
14089 BIT(DRM_ROTATE_0) |
14090 BIT(DRM_ROTATE_180));
14091 if (dev->mode_config.rotation_property)
14092 drm_object_attach_property(&cursor->base.base,
14093 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014094 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014095 }
14096
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014097 if (INTEL_INFO(dev)->gen >=9)
14098 state->scaler_id = -1;
14099
Matt Roperea2c67b2014-12-23 10:41:52 -080014100 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14101
Matt Roper3d7d6512014-06-10 08:28:13 -070014102 return &cursor->base;
14103}
14104
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014105static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14106 struct intel_crtc_state *crtc_state)
14107{
14108 int i;
14109 struct intel_scaler *intel_scaler;
14110 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14111
14112 for (i = 0; i < intel_crtc->num_scalers; i++) {
14113 intel_scaler = &scaler_state->scalers[i];
14114 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014115 intel_scaler->mode = PS_SCALER_MODE_DYN;
14116 }
14117
14118 scaler_state->scaler_id = -1;
14119}
14120
Hannes Ederb358d0a2008-12-18 21:18:47 +010014121static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014122{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014123 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014124 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014125 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014126 struct drm_plane *primary = NULL;
14127 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014128 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014129
Daniel Vetter955382f2013-09-19 14:05:45 +020014130 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014131 if (intel_crtc == NULL)
14132 return;
14133
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014134 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14135 if (!crtc_state)
14136 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014137 intel_crtc->config = crtc_state;
14138 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014139 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014140
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014141 /* initialize shared scalers */
14142 if (INTEL_INFO(dev)->gen >= 9) {
14143 if (pipe == PIPE_C)
14144 intel_crtc->num_scalers = 1;
14145 else
14146 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14147
14148 skl_init_scalers(dev, intel_crtc, crtc_state);
14149 }
14150
Matt Roper465c1202014-05-29 08:06:54 -070014151 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014152 if (!primary)
14153 goto fail;
14154
14155 cursor = intel_cursor_plane_create(dev, pipe);
14156 if (!cursor)
14157 goto fail;
14158
Matt Roper465c1202014-05-29 08:06:54 -070014159 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014160 cursor, &intel_crtc_funcs);
14161 if (ret)
14162 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014163
14164 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014165 for (i = 0; i < 256; i++) {
14166 intel_crtc->lut_r[i] = i;
14167 intel_crtc->lut_g[i] = i;
14168 intel_crtc->lut_b[i] = i;
14169 }
14170
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014171 /*
14172 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014173 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014174 */
Jesse Barnes80824002009-09-10 15:28:06 -070014175 intel_crtc->pipe = pipe;
14176 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014177 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014178 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014179 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014180 }
14181
Chris Wilson4b0e3332014-05-30 16:35:26 +030014182 intel_crtc->cursor_base = ~0;
14183 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014184 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014185
Ville Syrjälä852eb002015-06-24 22:00:07 +030014186 intel_crtc->wm.cxsr_allowed = true;
14187
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014188 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14189 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14190 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14191 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14192
Jesse Barnes79e53942008-11-07 14:24:08 -080014193 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014194
14195 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014196 return;
14197
14198fail:
14199 if (primary)
14200 drm_plane_cleanup(primary);
14201 if (cursor)
14202 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014203 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014204 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014205}
14206
Jesse Barnes752aa882013-10-31 18:55:49 +020014207enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14208{
14209 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014210 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014211
Rob Clark51fd3712013-11-19 12:10:12 -050014212 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014213
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014214 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014215 return INVALID_PIPE;
14216
14217 return to_intel_crtc(encoder->crtc)->pipe;
14218}
14219
Carl Worth08d7b3d2009-04-29 14:43:54 -070014220int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014221 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014222{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014223 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014224 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014225 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014226
Rob Clark7707e652014-07-17 23:30:04 -040014227 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014228
Rob Clark7707e652014-07-17 23:30:04 -040014229 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014230 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014231 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014232 }
14233
Rob Clark7707e652014-07-17 23:30:04 -040014234 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014235 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014236
Daniel Vetterc05422d2009-08-11 16:05:30 +020014237 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014238}
14239
Daniel Vetter66a92782012-07-12 20:08:18 +020014240static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014241{
Daniel Vetter66a92782012-07-12 20:08:18 +020014242 struct drm_device *dev = encoder->base.dev;
14243 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014244 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014245 int entry = 0;
14246
Damien Lespiaub2784e12014-08-05 11:29:37 +010014247 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014248 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014249 index_mask |= (1 << entry);
14250
Jesse Barnes79e53942008-11-07 14:24:08 -080014251 entry++;
14252 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014253
Jesse Barnes79e53942008-11-07 14:24:08 -080014254 return index_mask;
14255}
14256
Chris Wilson4d302442010-12-14 19:21:29 +000014257static bool has_edp_a(struct drm_device *dev)
14258{
14259 struct drm_i915_private *dev_priv = dev->dev_private;
14260
14261 if (!IS_MOBILE(dev))
14262 return false;
14263
14264 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14265 return false;
14266
Damien Lespiaue3589902014-02-07 19:12:50 +000014267 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014268 return false;
14269
14270 return true;
14271}
14272
Jesse Barnes84b4e042014-06-25 08:24:29 -070014273static bool intel_crt_present(struct drm_device *dev)
14274{
14275 struct drm_i915_private *dev_priv = dev->dev_private;
14276
Damien Lespiau884497e2013-12-03 13:56:23 +000014277 if (INTEL_INFO(dev)->gen >= 9)
14278 return false;
14279
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014280 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014281 return false;
14282
14283 if (IS_CHERRYVIEW(dev))
14284 return false;
14285
14286 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14287 return false;
14288
14289 return true;
14290}
14291
Jesse Barnes79e53942008-11-07 14:24:08 -080014292static void intel_setup_outputs(struct drm_device *dev)
14293{
Eric Anholt725e30a2009-01-22 13:01:02 -080014294 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014295 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014296 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014297
Daniel Vetterc9093352013-06-06 22:22:47 +020014298 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014299
Jesse Barnes84b4e042014-06-25 08:24:29 -070014300 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014301 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014302
Vandana Kannanc776eb22014-08-19 12:05:01 +053014303 if (IS_BROXTON(dev)) {
14304 /*
14305 * FIXME: Broxton doesn't support port detection via the
14306 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14307 * detect the ports.
14308 */
14309 intel_ddi_init(dev, PORT_A);
14310 intel_ddi_init(dev, PORT_B);
14311 intel_ddi_init(dev, PORT_C);
14312 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014313 int found;
14314
Jesse Barnesde31fac2015-03-06 15:53:32 -080014315 /*
14316 * Haswell uses DDI functions to detect digital outputs.
14317 * On SKL pre-D0 the strap isn't connected, so we assume
14318 * it's there.
14319 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014320 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014321 /* WaIgnoreDDIAStrap: skl */
14322 if (found ||
14323 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014324 intel_ddi_init(dev, PORT_A);
14325
14326 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14327 * register */
14328 found = I915_READ(SFUSE_STRAP);
14329
14330 if (found & SFUSE_STRAP_DDIB_DETECTED)
14331 intel_ddi_init(dev, PORT_B);
14332 if (found & SFUSE_STRAP_DDIC_DETECTED)
14333 intel_ddi_init(dev, PORT_C);
14334 if (found & SFUSE_STRAP_DDID_DETECTED)
14335 intel_ddi_init(dev, PORT_D);
14336 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014337 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014338 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014339
14340 if (has_edp_a(dev))
14341 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014342
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014343 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014344 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014345 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014346 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014347 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014348 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014349 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014350 }
14351
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014352 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014353 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014354
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014355 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014356 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014357
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014358 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014359 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014360
Daniel Vetter270b3042012-10-27 15:52:05 +020014361 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014362 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014363 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014364 /*
14365 * The DP_DETECTED bit is the latched state of the DDC
14366 * SDA pin at boot. However since eDP doesn't require DDC
14367 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14368 * eDP ports may have been muxed to an alternate function.
14369 * Thus we can't rely on the DP_DETECTED bit alone to detect
14370 * eDP ports. Consult the VBT as well as DP_DETECTED to
14371 * detect eDP ports.
14372 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014373 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14374 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014375 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14376 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014377 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14378 intel_dp_is_edp(dev, PORT_B))
14379 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014380
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014381 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14382 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014383 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14384 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014385 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14386 intel_dp_is_edp(dev, PORT_C))
14387 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014388
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014389 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014390 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014391 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14392 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014393 /* eDP not supported on port D, so don't check VBT */
14394 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14395 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014396 }
14397
Jani Nikula3cfca972013-08-27 15:12:26 +030014398 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014399 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014400 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014401
Paulo Zanonie2debe92013-02-18 19:00:27 -030014402 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014403 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014404 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014405 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014406 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014407 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014408 }
Ma Ling27185ae2009-08-24 13:50:23 +080014409
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014410 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014411 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014412 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014413
14414 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014415
Paulo Zanonie2debe92013-02-18 19:00:27 -030014416 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014417 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014418 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014419 }
Ma Ling27185ae2009-08-24 13:50:23 +080014420
Paulo Zanonie2debe92013-02-18 19:00:27 -030014421 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014422
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014423 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014424 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014425 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014426 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014427 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014428 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014429 }
Ma Ling27185ae2009-08-24 13:50:23 +080014430
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014431 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014432 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014433 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014434 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014435 intel_dvo_init(dev);
14436
Zhenyu Wang103a1962009-11-27 11:44:36 +080014437 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014438 intel_tv_init(dev);
14439
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014440 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014441
Damien Lespiaub2784e12014-08-05 11:29:37 +010014442 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014443 encoder->base.possible_crtcs = encoder->crtc_mask;
14444 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014445 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014446 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014447
Paulo Zanonidde86e22012-12-01 12:04:25 -020014448 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014449
14450 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014451}
14452
14453static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14454{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014455 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014456 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014457
Daniel Vetteref2d6332014-02-10 18:00:38 +010014458 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014459 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014460 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014461 drm_gem_object_unreference(&intel_fb->obj->base);
14462 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014463 kfree(intel_fb);
14464}
14465
14466static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014467 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014468 unsigned int *handle)
14469{
14470 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014471 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014472
Chris Wilson05394f32010-11-08 19:18:58 +000014473 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014474}
14475
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014476static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14477 struct drm_file *file,
14478 unsigned flags, unsigned color,
14479 struct drm_clip_rect *clips,
14480 unsigned num_clips)
14481{
14482 struct drm_device *dev = fb->dev;
14483 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14484 struct drm_i915_gem_object *obj = intel_fb->obj;
14485
14486 mutex_lock(&dev->struct_mutex);
14487 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
14488 mutex_unlock(&dev->struct_mutex);
14489
14490 return 0;
14491}
14492
Jesse Barnes79e53942008-11-07 14:24:08 -080014493static const struct drm_framebuffer_funcs intel_fb_funcs = {
14494 .destroy = intel_user_framebuffer_destroy,
14495 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014496 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014497};
14498
Damien Lespiaub3218032015-02-27 11:15:18 +000014499static
14500u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14501 uint32_t pixel_format)
14502{
14503 u32 gen = INTEL_INFO(dev)->gen;
14504
14505 if (gen >= 9) {
14506 /* "The stride in bytes must not exceed the of the size of 8K
14507 * pixels and 32K bytes."
14508 */
14509 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14510 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14511 return 32*1024;
14512 } else if (gen >= 4) {
14513 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14514 return 16*1024;
14515 else
14516 return 32*1024;
14517 } else if (gen >= 3) {
14518 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14519 return 8*1024;
14520 else
14521 return 16*1024;
14522 } else {
14523 /* XXX DSPC is limited to 4k tiled */
14524 return 8*1024;
14525 }
14526}
14527
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014528static int intel_framebuffer_init(struct drm_device *dev,
14529 struct intel_framebuffer *intel_fb,
14530 struct drm_mode_fb_cmd2 *mode_cmd,
14531 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014532{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014533 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014534 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014535 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014536
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014537 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14538
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014539 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14540 /* Enforce that fb modifier and tiling mode match, but only for
14541 * X-tiled. This is needed for FBC. */
14542 if (!!(obj->tiling_mode == I915_TILING_X) !=
14543 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14544 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14545 return -EINVAL;
14546 }
14547 } else {
14548 if (obj->tiling_mode == I915_TILING_X)
14549 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14550 else if (obj->tiling_mode == I915_TILING_Y) {
14551 DRM_DEBUG("No Y tiling for legacy addfb\n");
14552 return -EINVAL;
14553 }
14554 }
14555
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014556 /* Passed in modifier sanity checking. */
14557 switch (mode_cmd->modifier[0]) {
14558 case I915_FORMAT_MOD_Y_TILED:
14559 case I915_FORMAT_MOD_Yf_TILED:
14560 if (INTEL_INFO(dev)->gen < 9) {
14561 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14562 mode_cmd->modifier[0]);
14563 return -EINVAL;
14564 }
14565 case DRM_FORMAT_MOD_NONE:
14566 case I915_FORMAT_MOD_X_TILED:
14567 break;
14568 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014569 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14570 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014571 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014572 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014573
Damien Lespiaub3218032015-02-27 11:15:18 +000014574 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14575 mode_cmd->pixel_format);
14576 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14577 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14578 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014579 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014580 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014581
Damien Lespiaub3218032015-02-27 11:15:18 +000014582 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14583 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014584 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014585 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14586 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014587 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014588 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014589 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014590 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014591
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014592 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014593 mode_cmd->pitches[0] != obj->stride) {
14594 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14595 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014596 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014597 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014598
Ville Syrjälä57779d02012-10-31 17:50:14 +020014599 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014600 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014601 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014602 case DRM_FORMAT_RGB565:
14603 case DRM_FORMAT_XRGB8888:
14604 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014605 break;
14606 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014607 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014608 DRM_DEBUG("unsupported pixel format: %s\n",
14609 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014610 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014611 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014612 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014613 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014614 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14615 DRM_DEBUG("unsupported pixel format: %s\n",
14616 drm_get_format_name(mode_cmd->pixel_format));
14617 return -EINVAL;
14618 }
14619 break;
14620 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014621 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014622 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014623 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014624 DRM_DEBUG("unsupported pixel format: %s\n",
14625 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014626 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014627 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014628 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014629 case DRM_FORMAT_ABGR2101010:
14630 if (!IS_VALLEYVIEW(dev)) {
14631 DRM_DEBUG("unsupported pixel format: %s\n",
14632 drm_get_format_name(mode_cmd->pixel_format));
14633 return -EINVAL;
14634 }
14635 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014636 case DRM_FORMAT_YUYV:
14637 case DRM_FORMAT_UYVY:
14638 case DRM_FORMAT_YVYU:
14639 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014640 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014641 DRM_DEBUG("unsupported pixel format: %s\n",
14642 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014643 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014644 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014645 break;
14646 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014647 DRM_DEBUG("unsupported pixel format: %s\n",
14648 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014649 return -EINVAL;
14650 }
14651
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014652 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14653 if (mode_cmd->offsets[0] != 0)
14654 return -EINVAL;
14655
Damien Lespiauec2c9812015-01-20 12:51:45 +000014656 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014657 mode_cmd->pixel_format,
14658 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014659 /* FIXME drm helper for size checks (especially planar formats)? */
14660 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14661 return -EINVAL;
14662
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014663 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14664 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014665 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014666
Jesse Barnes79e53942008-11-07 14:24:08 -080014667 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14668 if (ret) {
14669 DRM_ERROR("framebuffer init failed %d\n", ret);
14670 return ret;
14671 }
14672
Jesse Barnes79e53942008-11-07 14:24:08 -080014673 return 0;
14674}
14675
Jesse Barnes79e53942008-11-07 14:24:08 -080014676static struct drm_framebuffer *
14677intel_user_framebuffer_create(struct drm_device *dev,
14678 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014679 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014680{
Chris Wilson05394f32010-11-08 19:18:58 +000014681 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014682
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014683 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14684 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014685 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014686 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014687
Chris Wilsond2dff872011-04-19 08:36:26 +010014688 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014689}
14690
Daniel Vetter4520f532013-10-09 09:18:51 +020014691#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014692static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014693{
14694}
14695#endif
14696
Jesse Barnes79e53942008-11-07 14:24:08 -080014697static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014698 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014699 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014700 .atomic_check = intel_atomic_check,
14701 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014702 .atomic_state_alloc = intel_atomic_state_alloc,
14703 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014704};
14705
Jesse Barnese70236a2009-09-21 10:42:27 -070014706/* Set up chip specific display functions */
14707static void intel_init_display(struct drm_device *dev)
14708{
14709 struct drm_i915_private *dev_priv = dev->dev_private;
14710
Daniel Vetteree9300b2013-06-03 22:40:22 +020014711 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14712 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014713 else if (IS_CHERRYVIEW(dev))
14714 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014715 else if (IS_VALLEYVIEW(dev))
14716 dev_priv->display.find_dpll = vlv_find_best_dpll;
14717 else if (IS_PINEVIEW(dev))
14718 dev_priv->display.find_dpll = pnv_find_best_dpll;
14719 else
14720 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14721
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014722 if (INTEL_INFO(dev)->gen >= 9) {
14723 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014724 dev_priv->display.get_initial_plane_config =
14725 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014726 dev_priv->display.crtc_compute_clock =
14727 haswell_crtc_compute_clock;
14728 dev_priv->display.crtc_enable = haswell_crtc_enable;
14729 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014730 dev_priv->display.update_primary_plane =
14731 skylake_update_primary_plane;
14732 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014733 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014734 dev_priv->display.get_initial_plane_config =
14735 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014736 dev_priv->display.crtc_compute_clock =
14737 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014738 dev_priv->display.crtc_enable = haswell_crtc_enable;
14739 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014740 dev_priv->display.update_primary_plane =
14741 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014742 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014743 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014744 dev_priv->display.get_initial_plane_config =
14745 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014746 dev_priv->display.crtc_compute_clock =
14747 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014748 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14749 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014750 dev_priv->display.update_primary_plane =
14751 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014752 } else if (IS_VALLEYVIEW(dev)) {
14753 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014754 dev_priv->display.get_initial_plane_config =
14755 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014756 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014757 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14758 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014759 dev_priv->display.update_primary_plane =
14760 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014761 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014762 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014763 dev_priv->display.get_initial_plane_config =
14764 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014765 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014766 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14767 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014768 dev_priv->display.update_primary_plane =
14769 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014770 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014771
Jesse Barnese70236a2009-09-21 10:42:27 -070014772 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014773 if (IS_SKYLAKE(dev))
14774 dev_priv->display.get_display_clock_speed =
14775 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014776 else if (IS_BROXTON(dev))
14777 dev_priv->display.get_display_clock_speed =
14778 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014779 else if (IS_BROADWELL(dev))
14780 dev_priv->display.get_display_clock_speed =
14781 broadwell_get_display_clock_speed;
14782 else if (IS_HASWELL(dev))
14783 dev_priv->display.get_display_clock_speed =
14784 haswell_get_display_clock_speed;
14785 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014786 dev_priv->display.get_display_clock_speed =
14787 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014788 else if (IS_GEN5(dev))
14789 dev_priv->display.get_display_clock_speed =
14790 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014791 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014792 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014793 dev_priv->display.get_display_clock_speed =
14794 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014795 else if (IS_GM45(dev))
14796 dev_priv->display.get_display_clock_speed =
14797 gm45_get_display_clock_speed;
14798 else if (IS_CRESTLINE(dev))
14799 dev_priv->display.get_display_clock_speed =
14800 i965gm_get_display_clock_speed;
14801 else if (IS_PINEVIEW(dev))
14802 dev_priv->display.get_display_clock_speed =
14803 pnv_get_display_clock_speed;
14804 else if (IS_G33(dev) || IS_G4X(dev))
14805 dev_priv->display.get_display_clock_speed =
14806 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014807 else if (IS_I915G(dev))
14808 dev_priv->display.get_display_clock_speed =
14809 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014810 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014811 dev_priv->display.get_display_clock_speed =
14812 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014813 else if (IS_PINEVIEW(dev))
14814 dev_priv->display.get_display_clock_speed =
14815 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014816 else if (IS_I915GM(dev))
14817 dev_priv->display.get_display_clock_speed =
14818 i915gm_get_display_clock_speed;
14819 else if (IS_I865G(dev))
14820 dev_priv->display.get_display_clock_speed =
14821 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014822 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014823 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014824 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014825 else { /* 830 */
14826 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014827 dev_priv->display.get_display_clock_speed =
14828 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014829 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014830
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014831 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014832 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014833 } else if (IS_GEN6(dev)) {
14834 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014835 } else if (IS_IVYBRIDGE(dev)) {
14836 /* FIXME: detect B0+ stepping and use auto training */
14837 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014838 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014839 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014840 if (IS_BROADWELL(dev)) {
14841 dev_priv->display.modeset_commit_cdclk =
14842 broadwell_modeset_commit_cdclk;
14843 dev_priv->display.modeset_calc_cdclk =
14844 broadwell_modeset_calc_cdclk;
14845 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014846 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014847 dev_priv->display.modeset_commit_cdclk =
14848 valleyview_modeset_commit_cdclk;
14849 dev_priv->display.modeset_calc_cdclk =
14850 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014851 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014852 dev_priv->display.modeset_commit_cdclk =
14853 broxton_modeset_commit_cdclk;
14854 dev_priv->display.modeset_calc_cdclk =
14855 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014856 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014857
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014858 switch (INTEL_INFO(dev)->gen) {
14859 case 2:
14860 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14861 break;
14862
14863 case 3:
14864 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14865 break;
14866
14867 case 4:
14868 case 5:
14869 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14870 break;
14871
14872 case 6:
14873 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14874 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014875 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014876 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014877 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14878 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014879 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014880 /* Drop through - unsupported since execlist only. */
14881 default:
14882 /* Default just returns -ENODEV to indicate unsupported */
14883 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014884 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014885
14886 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014887
14888 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014889}
14890
Jesse Barnesb690e962010-07-19 13:53:12 -070014891/*
14892 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14893 * resume, or other times. This quirk makes sure that's the case for
14894 * affected systems.
14895 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014896static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014897{
14898 struct drm_i915_private *dev_priv = dev->dev_private;
14899
14900 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014901 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014902}
14903
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014904static void quirk_pipeb_force(struct drm_device *dev)
14905{
14906 struct drm_i915_private *dev_priv = dev->dev_private;
14907
14908 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14909 DRM_INFO("applying pipe b force quirk\n");
14910}
14911
Keith Packard435793d2011-07-12 14:56:22 -070014912/*
14913 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14914 */
14915static void quirk_ssc_force_disable(struct drm_device *dev)
14916{
14917 struct drm_i915_private *dev_priv = dev->dev_private;
14918 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014919 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014920}
14921
Carsten Emde4dca20e2012-03-15 15:56:26 +010014922/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014923 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14924 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014925 */
14926static void quirk_invert_brightness(struct drm_device *dev)
14927{
14928 struct drm_i915_private *dev_priv = dev->dev_private;
14929 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014930 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014931}
14932
Scot Doyle9c72cc62014-07-03 23:27:50 +000014933/* Some VBT's incorrectly indicate no backlight is present */
14934static void quirk_backlight_present(struct drm_device *dev)
14935{
14936 struct drm_i915_private *dev_priv = dev->dev_private;
14937 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14938 DRM_INFO("applying backlight present quirk\n");
14939}
14940
Jesse Barnesb690e962010-07-19 13:53:12 -070014941struct intel_quirk {
14942 int device;
14943 int subsystem_vendor;
14944 int subsystem_device;
14945 void (*hook)(struct drm_device *dev);
14946};
14947
Egbert Eich5f85f172012-10-14 15:46:38 +020014948/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14949struct intel_dmi_quirk {
14950 void (*hook)(struct drm_device *dev);
14951 const struct dmi_system_id (*dmi_id_list)[];
14952};
14953
14954static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14955{
14956 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14957 return 1;
14958}
14959
14960static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14961 {
14962 .dmi_id_list = &(const struct dmi_system_id[]) {
14963 {
14964 .callback = intel_dmi_reverse_brightness,
14965 .ident = "NCR Corporation",
14966 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14967 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14968 },
14969 },
14970 { } /* terminating entry */
14971 },
14972 .hook = quirk_invert_brightness,
14973 },
14974};
14975
Ben Widawskyc43b5632012-04-16 14:07:40 -070014976static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014977 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14978 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14979
Jesse Barnesb690e962010-07-19 13:53:12 -070014980 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14981 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14982
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014983 /* 830 needs to leave pipe A & dpll A up */
14984 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14985
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014986 /* 830 needs to leave pipe B & dpll B up */
14987 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14988
Keith Packard435793d2011-07-12 14:56:22 -070014989 /* Lenovo U160 cannot use SSC on LVDS */
14990 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014991
14992 /* Sony Vaio Y cannot use SSC on LVDS */
14993 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014994
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014995 /* Acer Aspire 5734Z must invert backlight brightness */
14996 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14997
14998 /* Acer/eMachines G725 */
14999 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15000
15001 /* Acer/eMachines e725 */
15002 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15003
15004 /* Acer/Packard Bell NCL20 */
15005 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15006
15007 /* Acer Aspire 4736Z */
15008 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015009
15010 /* Acer Aspire 5336 */
15011 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015012
15013 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15014 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015015
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015016 /* Acer C720 Chromebook (Core i3 4005U) */
15017 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15018
jens steinb2a96012014-10-28 20:25:53 +010015019 /* Apple Macbook 2,1 (Core 2 T7400) */
15020 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15021
Scot Doyled4967d82014-07-03 23:27:52 +000015022 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15023 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015024
15025 /* HP Chromebook 14 (Celeron 2955U) */
15026 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015027
15028 /* Dell Chromebook 11 */
15029 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015030};
15031
15032static void intel_init_quirks(struct drm_device *dev)
15033{
15034 struct pci_dev *d = dev->pdev;
15035 int i;
15036
15037 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15038 struct intel_quirk *q = &intel_quirks[i];
15039
15040 if (d->device == q->device &&
15041 (d->subsystem_vendor == q->subsystem_vendor ||
15042 q->subsystem_vendor == PCI_ANY_ID) &&
15043 (d->subsystem_device == q->subsystem_device ||
15044 q->subsystem_device == PCI_ANY_ID))
15045 q->hook(dev);
15046 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015047 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15048 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15049 intel_dmi_quirks[i].hook(dev);
15050 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015051}
15052
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015053/* Disable the VGA plane that we never use */
15054static void i915_disable_vga(struct drm_device *dev)
15055{
15056 struct drm_i915_private *dev_priv = dev->dev_private;
15057 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015058 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015059
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015060 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015061 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015062 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015063 sr1 = inb(VGA_SR_DATA);
15064 outb(sr1 | 1<<5, VGA_SR_DATA);
15065 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15066 udelay(300);
15067
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015068 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015069 POSTING_READ(vga_reg);
15070}
15071
Daniel Vetterf8175862012-04-10 15:50:11 +020015072void intel_modeset_init_hw(struct drm_device *dev)
15073{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015074 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015075 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015076 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015077 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015078}
15079
Jesse Barnes79e53942008-11-07 14:24:08 -080015080void intel_modeset_init(struct drm_device *dev)
15081{
Jesse Barnes652c3932009-08-17 13:31:43 -070015082 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015083 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015084 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015085 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015086
15087 drm_mode_config_init(dev);
15088
15089 dev->mode_config.min_width = 0;
15090 dev->mode_config.min_height = 0;
15091
Dave Airlie019d96c2011-09-29 16:20:42 +010015092 dev->mode_config.preferred_depth = 24;
15093 dev->mode_config.prefer_shadow = 1;
15094
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015095 dev->mode_config.allow_fb_modifiers = true;
15096
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015097 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015098
Jesse Barnesb690e962010-07-19 13:53:12 -070015099 intel_init_quirks(dev);
15100
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015101 intel_init_pm(dev);
15102
Ben Widawskye3c74752013-04-05 13:12:39 -070015103 if (INTEL_INFO(dev)->num_pipes == 0)
15104 return;
15105
Jesse Barnese70236a2009-09-21 10:42:27 -070015106 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015107 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015108
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015109 if (IS_GEN2(dev)) {
15110 dev->mode_config.max_width = 2048;
15111 dev->mode_config.max_height = 2048;
15112 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015113 dev->mode_config.max_width = 4096;
15114 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015115 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015116 dev->mode_config.max_width = 8192;
15117 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015118 }
Damien Lespiau068be562014-03-28 14:17:49 +000015119
Ville Syrjälädc41c152014-08-13 11:57:05 +030015120 if (IS_845G(dev) || IS_I865G(dev)) {
15121 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15122 dev->mode_config.cursor_height = 1023;
15123 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015124 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15125 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15126 } else {
15127 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15128 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15129 }
15130
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015131 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015132
Zhao Yakui28c97732009-10-09 11:39:41 +080015133 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015134 INTEL_INFO(dev)->num_pipes,
15135 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015136
Damien Lespiau055e3932014-08-18 13:49:10 +010015137 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015138 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015139 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015140 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015141 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015142 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015143 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015144 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015145 }
15146
Jesse Barnesf42bb702013-12-16 16:34:23 -080015147 intel_init_dpio(dev);
15148
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015149 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015150
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015151 /* Just disable it once at startup */
15152 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015153 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015154
15155 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030015156 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015157
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015158 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015159 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015160 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015161
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015162 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015163 struct intel_initial_plane_config plane_config = {};
15164
Jesse Barnes46f297f2014-03-07 08:57:48 -080015165 if (!crtc->active)
15166 continue;
15167
Jesse Barnes46f297f2014-03-07 08:57:48 -080015168 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015169 * Note that reserving the BIOS fb up front prevents us
15170 * from stuffing other stolen allocations like the ring
15171 * on top. This prevents some ugliness at boot time, and
15172 * can even allow for smooth boot transitions if the BIOS
15173 * fb is large enough for the active pipe configuration.
15174 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015175 dev_priv->display.get_initial_plane_config(crtc,
15176 &plane_config);
15177
15178 /*
15179 * If the fb is shared between multiple heads, we'll
15180 * just get the first one.
15181 */
15182 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015183 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015184}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015185
Daniel Vetter7fad7982012-07-04 17:51:47 +020015186static void intel_enable_pipe_a(struct drm_device *dev)
15187{
15188 struct intel_connector *connector;
15189 struct drm_connector *crt = NULL;
15190 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015191 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015192
15193 /* We can't just switch on the pipe A, we need to set things up with a
15194 * proper mode and output configuration. As a gross hack, enable pipe A
15195 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015196 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015197 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15198 crt = &connector->base;
15199 break;
15200 }
15201 }
15202
15203 if (!crt)
15204 return;
15205
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015206 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015207 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015208}
15209
Daniel Vetterfa555832012-10-10 23:14:00 +020015210static bool
15211intel_check_plane_mapping(struct intel_crtc *crtc)
15212{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015213 struct drm_device *dev = crtc->base.dev;
15214 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015215 u32 reg, val;
15216
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015217 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015218 return true;
15219
15220 reg = DSPCNTR(!crtc->plane);
15221 val = I915_READ(reg);
15222
15223 if ((val & DISPLAY_PLANE_ENABLE) &&
15224 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15225 return false;
15226
15227 return true;
15228}
15229
Daniel Vetter24929352012-07-02 20:28:59 +020015230static void intel_sanitize_crtc(struct intel_crtc *crtc)
15231{
15232 struct drm_device *dev = crtc->base.dev;
15233 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015234 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015235 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015236 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015237
Daniel Vetter24929352012-07-02 20:28:59 +020015238 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015239 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015240 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15241
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015242 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015243 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015244 if (crtc->active) {
Maarten Lankhorst3a03dfb2015-07-14 13:46:40 +020015245 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015246 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015247 drm_crtc_vblank_on(&crtc->base);
15248 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015249
Daniel Vetter24929352012-07-02 20:28:59 +020015250 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015251 * disable the crtc (and hence change the state) if it is wrong. Note
15252 * that gen4+ has a fixed plane -> pipe mapping. */
15253 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015254 bool plane;
15255
Daniel Vetter24929352012-07-02 20:28:59 +020015256 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15257 crtc->base.base.id);
15258
15259 /* Pipe has the wrong plane attached and the plane is active.
15260 * Temporarily change the plane mapping and disable everything
15261 * ... */
15262 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015263 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015264 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015265 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015266 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015267 }
Daniel Vetter24929352012-07-02 20:28:59 +020015268
Daniel Vetter7fad7982012-07-04 17:51:47 +020015269 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15270 crtc->pipe == PIPE_A && !crtc->active) {
15271 /* BIOS forgot to enable pipe A, this mostly happens after
15272 * resume. Force-enable the pipe to fix this, the update_dpms
15273 * call below we restore the pipe to the right state, but leave
15274 * the required bits on. */
15275 intel_enable_pipe_a(dev);
15276 }
15277
Daniel Vetter24929352012-07-02 20:28:59 +020015278 /* Adjust the state of the output pipe according to whether we
15279 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015280 enable = false;
15281 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15282 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015283
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015284 if (!enable)
15285 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015286
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015287 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015288
15289 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015290 * functions or because of calls to intel_crtc_disable_noatomic,
15291 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015292 * pipe A quirk. */
15293 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15294 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015295 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015296 crtc->active ? "enabled" : "disabled");
15297
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015298 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015299 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015300 crtc->base.enabled = crtc->active;
15301
15302 /* Because we only establish the connector -> encoder ->
15303 * crtc links if something is active, this means the
15304 * crtc is now deactivated. Break the links. connector
15305 * -> encoder links are only establish when things are
15306 * actually up, hence no need to break them. */
15307 WARN_ON(crtc->active);
15308
15309 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15310 WARN_ON(encoder->connectors_active);
15311 encoder->base.crtc = NULL;
15312 }
15313 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015314
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015315 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015316 /*
15317 * We start out with underrun reporting disabled to avoid races.
15318 * For correct bookkeeping mark this on active crtcs.
15319 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015320 * Also on gmch platforms we dont have any hardware bits to
15321 * disable the underrun reporting. Which means we need to start
15322 * out with underrun reporting disabled also on inactive pipes,
15323 * since otherwise we'll complain about the garbage we read when
15324 * e.g. coming up after runtime pm.
15325 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015326 * No protection against concurrent access is required - at
15327 * worst a fifo underrun happens which also sets this to false.
15328 */
15329 crtc->cpu_fifo_underrun_disabled = true;
15330 crtc->pch_fifo_underrun_disabled = true;
15331 }
Daniel Vetter24929352012-07-02 20:28:59 +020015332}
15333
15334static void intel_sanitize_encoder(struct intel_encoder *encoder)
15335{
15336 struct intel_connector *connector;
15337 struct drm_device *dev = encoder->base.dev;
15338
15339 /* We need to check both for a crtc link (meaning that the
15340 * encoder is active and trying to read from a pipe) and the
15341 * pipe itself being active. */
15342 bool has_active_crtc = encoder->base.crtc &&
15343 to_intel_crtc(encoder->base.crtc)->active;
15344
15345 if (encoder->connectors_active && !has_active_crtc) {
15346 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15347 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015348 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015349
15350 /* Connector is active, but has no active pipe. This is
15351 * fallout from our resume register restoring. Disable
15352 * the encoder manually again. */
15353 if (encoder->base.crtc) {
15354 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15355 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015356 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015357 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015358 if (encoder->post_disable)
15359 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015360 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015361 encoder->base.crtc = NULL;
15362 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015363
15364 /* Inconsistent output/port/pipe state happens presumably due to
15365 * a bug in one of the get_hw_state functions. Or someplace else
15366 * in our code, like the register restore mess on resume. Clamp
15367 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015368 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015369 if (connector->encoder != encoder)
15370 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015371 connector->base.dpms = DRM_MODE_DPMS_OFF;
15372 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015373 }
15374 }
15375 /* Enabled encoders without active connectors will be fixed in
15376 * the crtc fixup. */
15377}
15378
Imre Deak04098752014-02-18 00:02:16 +020015379void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015380{
15381 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015382 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015383
Imre Deak04098752014-02-18 00:02:16 +020015384 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15385 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15386 i915_disable_vga(dev);
15387 }
15388}
15389
15390void i915_redisable_vga(struct drm_device *dev)
15391{
15392 struct drm_i915_private *dev_priv = dev->dev_private;
15393
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015394 /* This function can be called both from intel_modeset_setup_hw_state or
15395 * at a very early point in our resume sequence, where the power well
15396 * structures are not yet restored. Since this function is at a very
15397 * paranoid "someone might have enabled VGA while we were not looking"
15398 * level, just check if the power well is enabled instead of trying to
15399 * follow the "don't touch the power well if we don't need it" policy
15400 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015401 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015402 return;
15403
Imre Deak04098752014-02-18 00:02:16 +020015404 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015405}
15406
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015407static bool primary_get_hw_state(struct intel_crtc *crtc)
15408{
15409 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15410
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015411 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15412}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015413
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015414static void readout_plane_state(struct intel_crtc *crtc,
15415 struct intel_crtc_state *crtc_state)
15416{
15417 struct intel_plane *p;
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015418 struct intel_plane_state *plane_state;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015419 bool active = crtc_state->base.active;
15420
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015421 for_each_intel_plane(crtc->base.dev, p) {
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015422 if (crtc->pipe != p->pipe)
15423 continue;
15424
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015425 plane_state = to_intel_plane_state(p->base.state);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015426
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015427 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15428 plane_state->visible = primary_get_hw_state(crtc);
15429 else {
15430 if (active)
15431 p->disable_plane(&p->base, &crtc->base);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015432
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015433 plane_state->visible = false;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015434 }
15435 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015436}
15437
Daniel Vetter30e984d2013-06-05 13:34:17 +020015438static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015439{
15440 struct drm_i915_private *dev_priv = dev->dev_private;
15441 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015442 struct intel_crtc *crtc;
15443 struct intel_encoder *encoder;
15444 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015445 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015446
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015447 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015448 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015449 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015450 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015451
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015452 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015453
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015454 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015455 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015456
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015457 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015458 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015459
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015460 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15461 if (crtc->base.state->active) {
15462 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15463 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15464 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15465
15466 /*
15467 * The initial mode needs to be set in order to keep
15468 * the atomic core happy. It wants a valid mode if the
15469 * crtc's enabled, so we do the above call.
15470 *
15471 * At this point some state updated by the connectors
15472 * in their ->detect() callback has not run yet, so
15473 * no recalculation can be done yet.
15474 *
15475 * Even if we could do a recalculation and modeset
15476 * right now it would cause a double modeset if
15477 * fbdev or userspace chooses a different initial mode.
15478 *
15479 * So to prevent the double modeset, fail the memcmp
15480 * test in drm_atomic_set_mode_for_crtc to get a new
15481 * mode blob, and compare if the mode blob changed
15482 * when the PIPE_CONFIG_QUIRK_INHERITED_MODE quirk is
15483 * set.
15484 *
15485 * If that happens, someone indicated they wanted a
15486 * mode change, which means it's safe to do a full
15487 * recalculation.
15488 */
15489 crtc->base.state->mode.private_flags = ~0;
15490 }
15491
15492 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015493 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015494
15495 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15496 crtc->base.base.id,
15497 crtc->active ? "enabled" : "disabled");
15498 }
15499
Daniel Vetter53589012013-06-05 13:34:16 +020015500 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15501 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15502
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015503 pll->on = pll->get_hw_state(dev_priv, pll,
15504 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015505 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015506 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015507 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015508 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015509 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015510 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015511 }
Daniel Vetter53589012013-06-05 13:34:16 +020015512 }
Daniel Vetter53589012013-06-05 13:34:16 +020015513
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015514 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015515 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015516
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015517 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015518 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015519 }
15520
Damien Lespiaub2784e12014-08-05 11:29:37 +010015521 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015522 pipe = 0;
15523
15524 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015525 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15526 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015527 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015528 } else {
15529 encoder->base.crtc = NULL;
15530 }
15531
15532 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015533 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015534 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015535 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015536 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015537 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015538 }
15539
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015540 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015541 if (connector->get_hw_state(connector)) {
15542 connector->base.dpms = DRM_MODE_DPMS_ON;
15543 connector->encoder->connectors_active = true;
15544 connector->base.encoder = &connector->encoder->base;
15545 } else {
15546 connector->base.dpms = DRM_MODE_DPMS_OFF;
15547 connector->base.encoder = NULL;
15548 }
15549 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15550 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015551 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015552 connector->base.encoder ? "enabled" : "disabled");
15553 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015554}
15555
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015556/* Scan out the current hw modeset state,
15557 * and sanitizes it to the current state
15558 */
15559static void
15560intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015561{
15562 struct drm_i915_private *dev_priv = dev->dev_private;
15563 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015564 struct intel_crtc *crtc;
15565 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015566 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015567
15568 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015569
15570 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015571 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015572 intel_sanitize_encoder(encoder);
15573 }
15574
Damien Lespiau055e3932014-08-18 13:49:10 +010015575 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015576 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15577 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015578 intel_dump_pipe_config(crtc, crtc->config,
15579 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015580 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015581
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015582 intel_modeset_update_connector_atomic_state(dev);
15583
Daniel Vetter35c95372013-07-17 06:55:04 +020015584 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15585 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15586
15587 if (!pll->on || pll->active)
15588 continue;
15589
15590 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15591
15592 pll->disable(dev_priv, pll);
15593 pll->on = false;
15594 }
15595
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015596 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015597 vlv_wm_get_hw_state(dev);
15598 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015599 skl_wm_get_hw_state(dev);
15600 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015601 ilk_wm_get_hw_state(dev);
Maarten Lankhorst292b9902015-07-13 16:30:27 +020015602
15603 for_each_intel_crtc(dev, crtc) {
15604 unsigned long put_domains;
15605
15606 put_domains = modeset_get_crtc_power_domains(&crtc->base);
15607 if (WARN_ON(put_domains))
15608 modeset_put_power_domains(dev_priv, put_domains);
15609 }
15610 intel_display_set_init_power(dev_priv, false);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015611}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015612
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015613void intel_display_resume(struct drm_device *dev)
15614{
15615 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15616 struct intel_connector *conn;
15617 struct intel_plane *plane;
15618 struct drm_crtc *crtc;
15619 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015620
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015621 if (!state)
15622 return;
15623
15624 state->acquire_ctx = dev->mode_config.acquire_ctx;
15625
15626 /* preserve complete old state, including dpll */
15627 intel_atomic_get_shared_dpll_state(state);
15628
15629 for_each_crtc(dev, crtc) {
15630 struct drm_crtc_state *crtc_state =
15631 drm_atomic_get_crtc_state(state, crtc);
15632
15633 ret = PTR_ERR_OR_ZERO(crtc_state);
15634 if (ret)
15635 goto err;
15636
15637 /* force a restore */
15638 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015639 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015640
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015641 for_each_intel_plane(dev, plane) {
15642 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15643 if (ret)
15644 goto err;
15645 }
15646
15647 for_each_intel_connector(dev, conn) {
15648 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15649 if (ret)
15650 goto err;
15651 }
15652
15653 intel_modeset_setup_hw_state(dev);
15654
15655 i915_redisable_vga(dev);
15656 ret = intel_set_mode(state);
15657 if (!ret)
15658 return;
15659
15660err:
15661 DRM_ERROR("Restoring old state failed with %i\n", ret);
15662 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015663}
15664
15665void intel_modeset_gem_init(struct drm_device *dev)
15666{
Jesse Barnes92122782014-10-09 12:57:42 -070015667 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015668 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015669 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015670 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015671
Imre Deakae484342014-03-31 15:10:44 +030015672 mutex_lock(&dev->struct_mutex);
15673 intel_init_gt_powersave(dev);
15674 mutex_unlock(&dev->struct_mutex);
15675
Jesse Barnes92122782014-10-09 12:57:42 -070015676 /*
15677 * There may be no VBT; and if the BIOS enabled SSC we can
15678 * just keep using it to avoid unnecessary flicker. Whereas if the
15679 * BIOS isn't using it, don't assume it will work even if the VBT
15680 * indicates as much.
15681 */
15682 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15683 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15684 DREF_SSC1_ENABLE);
15685
Chris Wilson1833b132012-05-09 11:56:28 +010015686 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015687
15688 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015689
15690 /*
15691 * Make sure any fbs we allocated at startup are properly
15692 * pinned & fenced. When we do the allocation it's too early
15693 * for this.
15694 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015695 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015696 obj = intel_fb_obj(c->primary->fb);
15697 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015698 continue;
15699
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015700 mutex_lock(&dev->struct_mutex);
15701 ret = intel_pin_and_fence_fb_obj(c->primary,
15702 c->primary->fb,
15703 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015704 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015705 mutex_unlock(&dev->struct_mutex);
15706 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015707 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15708 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015709 drm_framebuffer_unreference(c->primary->fb);
15710 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015711 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015712 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015713 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015714 }
15715 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015716
15717 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015718}
15719
Imre Deak4932e2c2014-02-11 17:12:48 +020015720void intel_connector_unregister(struct intel_connector *intel_connector)
15721{
15722 struct drm_connector *connector = &intel_connector->base;
15723
15724 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015725 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015726}
15727
Jesse Barnes79e53942008-11-07 14:24:08 -080015728void intel_modeset_cleanup(struct drm_device *dev)
15729{
Jesse Barnes652c3932009-08-17 13:31:43 -070015730 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015731 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015732
Imre Deak2eb52522014-11-19 15:30:05 +020015733 intel_disable_gt_powersave(dev);
15734
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015735 intel_backlight_unregister(dev);
15736
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015737 /*
15738 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015739 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015740 * experience fancy races otherwise.
15741 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015742 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015743
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015744 /*
15745 * Due to the hpd irq storm handling the hotplug work can re-arm the
15746 * poll handlers. Hence disable polling after hpd handling is shut down.
15747 */
Keith Packardf87ea762010-10-03 19:36:26 -070015748 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015749
Jesse Barnes723bfd72010-10-07 16:01:13 -070015750 intel_unregister_dsm_handler();
15751
Paulo Zanoni7733b492015-07-07 15:26:04 -030015752 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015753
Chris Wilson1630fe72011-07-08 12:22:42 +010015754 /* flush any delayed tasks or pending work */
15755 flush_scheduled_work();
15756
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015757 /* destroy the backlight and sysfs files before encoders/connectors */
15758 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015759 struct intel_connector *intel_connector;
15760
15761 intel_connector = to_intel_connector(connector);
15762 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015763 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015764
Jesse Barnes79e53942008-11-07 14:24:08 -080015765 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015766
15767 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015768
15769 mutex_lock(&dev->struct_mutex);
15770 intel_cleanup_gt_powersave(dev);
15771 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015772}
15773
Dave Airlie28d52042009-09-21 14:33:58 +100015774/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015775 * Return which encoder is currently attached for connector.
15776 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015777struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015778{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015779 return &intel_attached_encoder(connector)->base;
15780}
Jesse Barnes79e53942008-11-07 14:24:08 -080015781
Chris Wilsondf0e9242010-09-09 16:20:55 +010015782void intel_connector_attach_encoder(struct intel_connector *connector,
15783 struct intel_encoder *encoder)
15784{
15785 connector->encoder = encoder;
15786 drm_mode_connector_attach_encoder(&connector->base,
15787 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015788}
Dave Airlie28d52042009-09-21 14:33:58 +100015789
15790/*
15791 * set vga decode state - true == enable VGA decode
15792 */
15793int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15794{
15795 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015796 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015797 u16 gmch_ctrl;
15798
Chris Wilson75fa0412014-02-07 18:37:02 -020015799 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15800 DRM_ERROR("failed to read control word\n");
15801 return -EIO;
15802 }
15803
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015804 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15805 return 0;
15806
Dave Airlie28d52042009-09-21 14:33:58 +100015807 if (state)
15808 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15809 else
15810 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015811
15812 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15813 DRM_ERROR("failed to write control word\n");
15814 return -EIO;
15815 }
15816
Dave Airlie28d52042009-09-21 14:33:58 +100015817 return 0;
15818}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015819
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015820struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015821
15822 u32 power_well_driver;
15823
Chris Wilson63b66e52013-08-08 15:12:06 +020015824 int num_transcoders;
15825
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015826 struct intel_cursor_error_state {
15827 u32 control;
15828 u32 position;
15829 u32 base;
15830 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015831 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015832
15833 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015834 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015835 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015836 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015837 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015838
15839 struct intel_plane_error_state {
15840 u32 control;
15841 u32 stride;
15842 u32 size;
15843 u32 pos;
15844 u32 addr;
15845 u32 surface;
15846 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015847 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015848
15849 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015850 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015851 enum transcoder cpu_transcoder;
15852
15853 u32 conf;
15854
15855 u32 htotal;
15856 u32 hblank;
15857 u32 hsync;
15858 u32 vtotal;
15859 u32 vblank;
15860 u32 vsync;
15861 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015862};
15863
15864struct intel_display_error_state *
15865intel_display_capture_error_state(struct drm_device *dev)
15866{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015867 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015868 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015869 int transcoders[] = {
15870 TRANSCODER_A,
15871 TRANSCODER_B,
15872 TRANSCODER_C,
15873 TRANSCODER_EDP,
15874 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015875 int i;
15876
Chris Wilson63b66e52013-08-08 15:12:06 +020015877 if (INTEL_INFO(dev)->num_pipes == 0)
15878 return NULL;
15879
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015880 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015881 if (error == NULL)
15882 return NULL;
15883
Imre Deak190be112013-11-25 17:15:31 +020015884 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015885 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15886
Damien Lespiau055e3932014-08-18 13:49:10 +010015887 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015888 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015889 __intel_display_power_is_enabled(dev_priv,
15890 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015891 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015892 continue;
15893
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015894 error->cursor[i].control = I915_READ(CURCNTR(i));
15895 error->cursor[i].position = I915_READ(CURPOS(i));
15896 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015897
15898 error->plane[i].control = I915_READ(DSPCNTR(i));
15899 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015900 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015901 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015902 error->plane[i].pos = I915_READ(DSPPOS(i));
15903 }
Paulo Zanonica291362013-03-06 20:03:14 -030015904 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15905 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015906 if (INTEL_INFO(dev)->gen >= 4) {
15907 error->plane[i].surface = I915_READ(DSPSURF(i));
15908 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15909 }
15910
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015911 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015912
Sonika Jindal3abfce72014-07-21 15:23:43 +053015913 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030015914 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015915 }
15916
15917 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15918 if (HAS_DDI(dev_priv->dev))
15919 error->num_transcoders++; /* Account for eDP. */
15920
15921 for (i = 0; i < error->num_transcoders; i++) {
15922 enum transcoder cpu_transcoder = transcoders[i];
15923
Imre Deakddf9c532013-11-27 22:02:02 +020015924 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015925 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015926 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015927 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015928 continue;
15929
Chris Wilson63b66e52013-08-08 15:12:06 +020015930 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15931
15932 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15933 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15934 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15935 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15936 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15937 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15938 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015939 }
15940
15941 return error;
15942}
15943
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015944#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15945
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015946void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015947intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015948 struct drm_device *dev,
15949 struct intel_display_error_state *error)
15950{
Damien Lespiau055e3932014-08-18 13:49:10 +010015951 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015952 int i;
15953
Chris Wilson63b66e52013-08-08 15:12:06 +020015954 if (!error)
15955 return;
15956
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015957 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015958 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015959 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015960 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015961 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015962 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015963 err_printf(m, " Power: %s\n",
15964 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015965 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015966 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015967
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015968 err_printf(m, "Plane [%d]:\n", i);
15969 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15970 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015971 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015972 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15973 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015974 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015975 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015976 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015977 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015978 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15979 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015980 }
15981
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015982 err_printf(m, "Cursor [%d]:\n", i);
15983 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15984 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15985 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015986 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015987
15988 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015989 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015990 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015991 err_printf(m, " Power: %s\n",
15992 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015993 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15994 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15995 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15996 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15997 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15998 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15999 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
16000 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000016001}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016002
16003void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
16004{
16005 struct intel_crtc *crtc;
16006
16007 for_each_intel_crtc(dev, crtc) {
16008 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016009
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016010 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016011
16012 work = crtc->unpin_work;
16013
16014 if (work && work->event &&
16015 work->event->base.file_priv == file) {
16016 kfree(work->event);
16017 work->event = NULL;
16018 }
16019
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016020 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016021 }
16022}