blob: b7f17e8a556edf117bdf0883f6c5cd95695293cd [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 Vivi066cf55b2015-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
5199 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5200
5201 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5202 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005203 if (intel_crtc->config->pch_pfit.enabled ||
5204 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005205 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5206
Imre Deak319be8a2014-03-04 19:22:57 +02005207 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5208 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5209
Imre Deak77d22dc2014-03-05 16:20:52 +02005210 return mask;
5211}
5212
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005213static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005214{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005215 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005216 struct drm_i915_private *dev_priv = dev->dev_private;
5217 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5218 struct intel_crtc *crtc;
5219
5220 /*
5221 * First get all needed power domains, then put all unneeded, to avoid
5222 * any unnecessary toggling of the power wells.
5223 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005224 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005225 enum intel_display_power_domain domain;
5226
Matt Roper83d65732015-02-25 13:12:16 -08005227 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005228 continue;
5229
Imre Deak319be8a2014-03-04 19:22:57 +02005230 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005231
5232 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5233 intel_display_power_get(dev_priv, domain);
5234 }
5235
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005236 if (dev_priv->display.modeset_commit_cdclk) {
5237 unsigned int cdclk = to_intel_atomic_state(state)->cdclk;
5238
5239 if (cdclk != dev_priv->cdclk_freq &&
5240 !WARN_ON(!state->allow_modeset))
5241 dev_priv->display.modeset_commit_cdclk(state);
5242 }
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005243
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005244 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005245 enum intel_display_power_domain domain;
5246
5247 for_each_power_domain(domain, crtc->enabled_power_domains)
5248 intel_display_power_put(dev_priv, domain);
5249
5250 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5251 }
5252
5253 intel_display_set_init_power(dev_priv, false);
5254}
5255
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005256static void intel_update_max_cdclk(struct drm_device *dev)
5257{
5258 struct drm_i915_private *dev_priv = dev->dev_private;
5259
5260 if (IS_SKYLAKE(dev)) {
5261 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5262
5263 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5264 dev_priv->max_cdclk_freq = 675000;
5265 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5266 dev_priv->max_cdclk_freq = 540000;
5267 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5268 dev_priv->max_cdclk_freq = 450000;
5269 else
5270 dev_priv->max_cdclk_freq = 337500;
5271 } else if (IS_BROADWELL(dev)) {
5272 /*
5273 * FIXME with extra cooling we can allow
5274 * 540 MHz for ULX and 675 Mhz for ULT.
5275 * How can we know if extra cooling is
5276 * available? PCI ID, VTB, something else?
5277 */
5278 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5279 dev_priv->max_cdclk_freq = 450000;
5280 else if (IS_BDW_ULX(dev))
5281 dev_priv->max_cdclk_freq = 450000;
5282 else if (IS_BDW_ULT(dev))
5283 dev_priv->max_cdclk_freq = 540000;
5284 else
5285 dev_priv->max_cdclk_freq = 675000;
Mika Kahola0904dea2015-06-12 10:11:32 +03005286 } else if (IS_CHERRYVIEW(dev)) {
5287 dev_priv->max_cdclk_freq = 320000;
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005288 } else if (IS_VALLEYVIEW(dev)) {
5289 dev_priv->max_cdclk_freq = 400000;
5290 } else {
5291 /* otherwise assume cdclk is fixed */
5292 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5293 }
5294
5295 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5296 dev_priv->max_cdclk_freq);
5297}
5298
5299static void intel_update_cdclk(struct drm_device *dev)
5300{
5301 struct drm_i915_private *dev_priv = dev->dev_private;
5302
5303 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5304 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5305 dev_priv->cdclk_freq);
5306
5307 /*
5308 * Program the gmbus_freq based on the cdclk frequency.
5309 * BSpec erroneously claims we should aim for 4MHz, but
5310 * in fact 1MHz is the correct frequency.
5311 */
5312 if (IS_VALLEYVIEW(dev)) {
5313 /*
5314 * Program the gmbus_freq based on the cdclk frequency.
5315 * BSpec erroneously claims we should aim for 4MHz, but
5316 * in fact 1MHz is the correct frequency.
5317 */
5318 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5319 }
5320
5321 if (dev_priv->max_cdclk_freq == 0)
5322 intel_update_max_cdclk(dev);
5323}
5324
Damien Lespiau70d0c572015-06-04 18:21:29 +01005325static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305326{
5327 struct drm_i915_private *dev_priv = dev->dev_private;
5328 uint32_t divider;
5329 uint32_t ratio;
5330 uint32_t current_freq;
5331 int ret;
5332
5333 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5334 switch (frequency) {
5335 case 144000:
5336 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5337 ratio = BXT_DE_PLL_RATIO(60);
5338 break;
5339 case 288000:
5340 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5341 ratio = BXT_DE_PLL_RATIO(60);
5342 break;
5343 case 384000:
5344 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5345 ratio = BXT_DE_PLL_RATIO(60);
5346 break;
5347 case 576000:
5348 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5349 ratio = BXT_DE_PLL_RATIO(60);
5350 break;
5351 case 624000:
5352 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5353 ratio = BXT_DE_PLL_RATIO(65);
5354 break;
5355 case 19200:
5356 /*
5357 * Bypass frequency with DE PLL disabled. Init ratio, divider
5358 * to suppress GCC warning.
5359 */
5360 ratio = 0;
5361 divider = 0;
5362 break;
5363 default:
5364 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5365
5366 return;
5367 }
5368
5369 mutex_lock(&dev_priv->rps.hw_lock);
5370 /* Inform power controller of upcoming frequency change */
5371 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5372 0x80000000);
5373 mutex_unlock(&dev_priv->rps.hw_lock);
5374
5375 if (ret) {
5376 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5377 ret, frequency);
5378 return;
5379 }
5380
5381 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5382 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5383 current_freq = current_freq * 500 + 1000;
5384
5385 /*
5386 * DE PLL has to be disabled when
5387 * - setting to 19.2MHz (bypass, PLL isn't used)
5388 * - before setting to 624MHz (PLL needs toggling)
5389 * - before setting to any frequency from 624MHz (PLL needs toggling)
5390 */
5391 if (frequency == 19200 || frequency == 624000 ||
5392 current_freq == 624000) {
5393 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5394 /* Timeout 200us */
5395 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5396 1))
5397 DRM_ERROR("timout waiting for DE PLL unlock\n");
5398 }
5399
5400 if (frequency != 19200) {
5401 uint32_t val;
5402
5403 val = I915_READ(BXT_DE_PLL_CTL);
5404 val &= ~BXT_DE_PLL_RATIO_MASK;
5405 val |= ratio;
5406 I915_WRITE(BXT_DE_PLL_CTL, val);
5407
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, 1))
5411 DRM_ERROR("timeout waiting for DE PLL lock\n");
5412
5413 val = I915_READ(CDCLK_CTL);
5414 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5415 val |= divider;
5416 /*
5417 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5418 * enable otherwise.
5419 */
5420 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5421 if (frequency >= 500000)
5422 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5423
5424 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5425 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5426 val |= (frequency - 1000) / 500;
5427 I915_WRITE(CDCLK_CTL, val);
5428 }
5429
5430 mutex_lock(&dev_priv->rps.hw_lock);
5431 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5432 DIV_ROUND_UP(frequency, 25000));
5433 mutex_unlock(&dev_priv->rps.hw_lock);
5434
5435 if (ret) {
5436 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5437 ret, frequency);
5438 return;
5439 }
5440
Damien Lespiaua47871b2015-06-04 18:21:34 +01005441 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305442}
5443
5444void broxton_init_cdclk(struct drm_device *dev)
5445{
5446 struct drm_i915_private *dev_priv = dev->dev_private;
5447 uint32_t val;
5448
5449 /*
5450 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5451 * or else the reset will hang because there is no PCH to respond.
5452 * Move the handshake programming to initialization sequence.
5453 * Previously was left up to BIOS.
5454 */
5455 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5456 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5457 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5458
5459 /* Enable PG1 for cdclk */
5460 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5461
5462 /* check if cd clock is enabled */
5463 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5464 DRM_DEBUG_KMS("Display already initialized\n");
5465 return;
5466 }
5467
5468 /*
5469 * FIXME:
5470 * - The initial CDCLK needs to be read from VBT.
5471 * Need to make this change after VBT has changes for BXT.
5472 * - check if setting the max (or any) cdclk freq is really necessary
5473 * here, it belongs to modeset time
5474 */
5475 broxton_set_cdclk(dev, 624000);
5476
5477 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005478 POSTING_READ(DBUF_CTL);
5479
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305480 udelay(10);
5481
5482 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5483 DRM_ERROR("DBuf power enable timeout!\n");
5484}
5485
5486void broxton_uninit_cdclk(struct drm_device *dev)
5487{
5488 struct drm_i915_private *dev_priv = dev->dev_private;
5489
5490 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005491 POSTING_READ(DBUF_CTL);
5492
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305493 udelay(10);
5494
5495 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5496 DRM_ERROR("DBuf power disable timeout!\n");
5497
5498 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5499 broxton_set_cdclk(dev, 19200);
5500
5501 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5502}
5503
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005504static const struct skl_cdclk_entry {
5505 unsigned int freq;
5506 unsigned int vco;
5507} skl_cdclk_frequencies[] = {
5508 { .freq = 308570, .vco = 8640 },
5509 { .freq = 337500, .vco = 8100 },
5510 { .freq = 432000, .vco = 8640 },
5511 { .freq = 450000, .vco = 8100 },
5512 { .freq = 540000, .vco = 8100 },
5513 { .freq = 617140, .vco = 8640 },
5514 { .freq = 675000, .vco = 8100 },
5515};
5516
5517static unsigned int skl_cdclk_decimal(unsigned int freq)
5518{
5519 return (freq - 1000) / 500;
5520}
5521
5522static unsigned int skl_cdclk_get_vco(unsigned int freq)
5523{
5524 unsigned int i;
5525
5526 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5527 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5528
5529 if (e->freq == freq)
5530 return e->vco;
5531 }
5532
5533 return 8100;
5534}
5535
5536static void
5537skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5538{
5539 unsigned int min_freq;
5540 u32 val;
5541
5542 /* select the minimum CDCLK before enabling DPLL 0 */
5543 val = I915_READ(CDCLK_CTL);
5544 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5545 val |= CDCLK_FREQ_337_308;
5546
5547 if (required_vco == 8640)
5548 min_freq = 308570;
5549 else
5550 min_freq = 337500;
5551
5552 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5553
5554 I915_WRITE(CDCLK_CTL, val);
5555 POSTING_READ(CDCLK_CTL);
5556
5557 /*
5558 * We always enable DPLL0 with the lowest link rate possible, but still
5559 * taking into account the VCO required to operate the eDP panel at the
5560 * desired frequency. The usual DP link rates operate with a VCO of
5561 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5562 * The modeset code is responsible for the selection of the exact link
5563 * rate later on, with the constraint of choosing a frequency that
5564 * works with required_vco.
5565 */
5566 val = I915_READ(DPLL_CTRL1);
5567
5568 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5569 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5570 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5571 if (required_vco == 8640)
5572 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5573 SKL_DPLL0);
5574 else
5575 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5576 SKL_DPLL0);
5577
5578 I915_WRITE(DPLL_CTRL1, val);
5579 POSTING_READ(DPLL_CTRL1);
5580
5581 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5582
5583 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5584 DRM_ERROR("DPLL0 not locked\n");
5585}
5586
5587static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5588{
5589 int ret;
5590 u32 val;
5591
5592 /* inform PCU we want to change CDCLK */
5593 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5594 mutex_lock(&dev_priv->rps.hw_lock);
5595 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5596 mutex_unlock(&dev_priv->rps.hw_lock);
5597
5598 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5599}
5600
5601static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5602{
5603 unsigned int i;
5604
5605 for (i = 0; i < 15; i++) {
5606 if (skl_cdclk_pcu_ready(dev_priv))
5607 return true;
5608 udelay(10);
5609 }
5610
5611 return false;
5612}
5613
5614static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5615{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005616 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005617 u32 freq_select, pcu_ack;
5618
5619 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5620
5621 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5622 DRM_ERROR("failed to inform PCU about cdclk change\n");
5623 return;
5624 }
5625
5626 /* set CDCLK_CTL */
5627 switch(freq) {
5628 case 450000:
5629 case 432000:
5630 freq_select = CDCLK_FREQ_450_432;
5631 pcu_ack = 1;
5632 break;
5633 case 540000:
5634 freq_select = CDCLK_FREQ_540;
5635 pcu_ack = 2;
5636 break;
5637 case 308570:
5638 case 337500:
5639 default:
5640 freq_select = CDCLK_FREQ_337_308;
5641 pcu_ack = 0;
5642 break;
5643 case 617140:
5644 case 675000:
5645 freq_select = CDCLK_FREQ_675_617;
5646 pcu_ack = 3;
5647 break;
5648 }
5649
5650 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5651 POSTING_READ(CDCLK_CTL);
5652
5653 /* inform PCU of the change */
5654 mutex_lock(&dev_priv->rps.hw_lock);
5655 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5656 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005657
5658 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005659}
5660
5661void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5662{
5663 /* disable DBUF power */
5664 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5665 POSTING_READ(DBUF_CTL);
5666
5667 udelay(10);
5668
5669 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5670 DRM_ERROR("DBuf power disable timeout\n");
5671
5672 /* disable DPLL0 */
5673 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5674 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5675 DRM_ERROR("Couldn't disable DPLL0\n");
5676
5677 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5678}
5679
5680void skl_init_cdclk(struct drm_i915_private *dev_priv)
5681{
5682 u32 val;
5683 unsigned int required_vco;
5684
5685 /* enable PCH reset handshake */
5686 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5687 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5688
5689 /* enable PG1 and Misc I/O */
5690 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5691
5692 /* DPLL0 already enabed !? */
5693 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5694 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5695 return;
5696 }
5697
5698 /* enable DPLL0 */
5699 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5700 skl_dpll0_enable(dev_priv, required_vco);
5701
5702 /* set CDCLK to the frequency the BIOS chose */
5703 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5704
5705 /* enable DBUF power */
5706 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5707 POSTING_READ(DBUF_CTL);
5708
5709 udelay(10);
5710
5711 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5712 DRM_ERROR("DBuf power enable timeout\n");
5713}
5714
Ville Syrjälädfcab172014-06-13 13:37:47 +03005715/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005716static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005717{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005718 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005719
Jesse Barnes586f49d2013-11-04 16:06:59 -08005720 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005721 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005722 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5723 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005724 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005725
Ville Syrjälädfcab172014-06-13 13:37:47 +03005726 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005727}
5728
5729/* Adjust CDclk dividers to allow high res or save power if possible */
5730static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5731{
5732 struct drm_i915_private *dev_priv = dev->dev_private;
5733 u32 val, cmd;
5734
Vandana Kannan164dfd22014-11-24 13:37:41 +05305735 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5736 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005737
Ville Syrjälädfcab172014-06-13 13:37:47 +03005738 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005739 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005740 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005741 cmd = 1;
5742 else
5743 cmd = 0;
5744
5745 mutex_lock(&dev_priv->rps.hw_lock);
5746 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5747 val &= ~DSPFREQGUAR_MASK;
5748 val |= (cmd << DSPFREQGUAR_SHIFT);
5749 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5750 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5751 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5752 50)) {
5753 DRM_ERROR("timed out waiting for CDclk change\n");
5754 }
5755 mutex_unlock(&dev_priv->rps.hw_lock);
5756
Ville Syrjälä54433e92015-05-26 20:42:31 +03005757 mutex_lock(&dev_priv->sb_lock);
5758
Ville Syrjälädfcab172014-06-13 13:37:47 +03005759 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005760 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005761
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005762 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005763
Jesse Barnes30a970c2013-11-04 13:48:12 -08005764 /* adjust cdclk divider */
5765 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005766 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005767 val |= divider;
5768 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005769
5770 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5771 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5772 50))
5773 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005774 }
5775
Jesse Barnes30a970c2013-11-04 13:48:12 -08005776 /* adjust self-refresh exit latency value */
5777 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5778 val &= ~0x7f;
5779
5780 /*
5781 * For high bandwidth configs, we set a higher latency in the bunit
5782 * so that the core display fetch happens in time to avoid underruns.
5783 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005784 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005785 val |= 4500 / 250; /* 4.5 usec */
5786 else
5787 val |= 3000 / 250; /* 3.0 usec */
5788 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005789
Ville Syrjäläa5805162015-05-26 20:42:30 +03005790 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005791
Ville Syrjäläb6283052015-06-03 15:45:07 +03005792 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793}
5794
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005795static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5796{
5797 struct drm_i915_private *dev_priv = dev->dev_private;
5798 u32 val, cmd;
5799
Vandana Kannan164dfd22014-11-24 13:37:41 +05305800 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5801 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005802
5803 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005804 case 333333:
5805 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005806 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005807 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005808 break;
5809 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005810 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005811 return;
5812 }
5813
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005814 /*
5815 * Specs are full of misinformation, but testing on actual
5816 * hardware has shown that we just need to write the desired
5817 * CCK divider into the Punit register.
5818 */
5819 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5820
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821 mutex_lock(&dev_priv->rps.hw_lock);
5822 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5823 val &= ~DSPFREQGUAR_MASK_CHV;
5824 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5825 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5826 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5827 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5828 50)) {
5829 DRM_ERROR("timed out waiting for CDclk change\n");
5830 }
5831 mutex_unlock(&dev_priv->rps.hw_lock);
5832
Ville Syrjäläb6283052015-06-03 15:45:07 +03005833 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005834}
5835
Jesse Barnes30a970c2013-11-04 13:48:12 -08005836static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5837 int max_pixclk)
5838{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005839 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005840 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005841
Jesse Barnes30a970c2013-11-04 13:48:12 -08005842 /*
5843 * Really only a few cases to deal with, as only 4 CDclks are supported:
5844 * 200MHz
5845 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005846 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005847 * 400MHz (VLV only)
5848 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5849 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005850 *
5851 * We seem to get an unstable or solid color picture at 200MHz.
5852 * Not sure what's wrong. For now use 200MHz only when all pipes
5853 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005854 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005855 if (!IS_CHERRYVIEW(dev_priv) &&
5856 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005857 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005858 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005859 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005860 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005861 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005862 else
5863 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005864}
5865
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305866static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5867 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005868{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305869 /*
5870 * FIXME:
5871 * - remove the guardband, it's not needed on BXT
5872 * - set 19.2MHz bypass frequency if there are no active pipes
5873 */
5874 if (max_pixclk > 576000*9/10)
5875 return 624000;
5876 else if (max_pixclk > 384000*9/10)
5877 return 576000;
5878 else if (max_pixclk > 288000*9/10)
5879 return 384000;
5880 else if (max_pixclk > 144000*9/10)
5881 return 288000;
5882 else
5883 return 144000;
5884}
5885
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005886/* Compute the max pixel clock for new configuration. Uses atomic state if
5887 * that's non-NULL, look at current state otherwise. */
5888static int intel_mode_max_pixclk(struct drm_device *dev,
5889 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005890{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005891 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005892 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005893 int max_pixclk = 0;
5894
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005895 for_each_intel_crtc(dev, intel_crtc) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005896 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005897 if (IS_ERR(crtc_state))
5898 return PTR_ERR(crtc_state);
5899
5900 if (!crtc_state->base.enable)
5901 continue;
5902
5903 max_pixclk = max(max_pixclk,
5904 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005905 }
5906
5907 return max_pixclk;
5908}
5909
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005910static int valleyview_modeset_calc_cdclk(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005911{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005912 struct drm_device *dev = state->dev;
5913 struct drm_i915_private *dev_priv = dev->dev_private;
5914 int max_pixclk = intel_mode_max_pixclk(dev, state);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005915
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005916 if (max_pixclk < 0)
5917 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005918
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005919 to_intel_atomic_state(state)->cdclk =
5920 valleyview_calc_cdclk(dev_priv, max_pixclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305921
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005922 return 0;
5923}
Jesse Barnes30a970c2013-11-04 13:48:12 -08005924
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005925static int broxton_modeset_calc_cdclk(struct drm_atomic_state *state)
5926{
5927 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);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005930
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005931 if (max_pixclk < 0)
5932 return max_pixclk;
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005933
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005934 to_intel_atomic_state(state)->cdclk =
5935 broxton_calc_cdclk(dev_priv, max_pixclk);
Maarten Lankhorst85a96e72015-06-01 12:49:53 +02005936
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005937 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005938}
5939
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005940static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5941{
5942 unsigned int credits, default_credits;
5943
5944 if (IS_CHERRYVIEW(dev_priv))
5945 default_credits = PFI_CREDIT(12);
5946 else
5947 default_credits = PFI_CREDIT(8);
5948
Vandana Kannan164dfd22014-11-24 13:37:41 +05305949 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005950 /* CHV suggested value is 31 or 63 */
5951 if (IS_CHERRYVIEW(dev_priv))
Ville Syrjäläfcc00082015-05-26 20:22:40 +03005952 credits = PFI_CREDIT_63;
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005953 else
5954 credits = PFI_CREDIT(15);
5955 } else {
5956 credits = default_credits;
5957 }
5958
5959 /*
5960 * WA - write default credits before re-programming
5961 * FIXME: should we also set the resend bit here?
5962 */
5963 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5964 default_credits);
5965
5966 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
5967 credits | PFI_CREDIT_RESEND);
5968
5969 /*
5970 * FIXME is this guaranteed to clear
5971 * immediately or should we poll for it?
5972 */
5973 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
5974}
5975
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005976static void valleyview_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005977{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005978 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005979 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005980 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005981
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005982 /*
5983 * FIXME: We can end up here with all power domains off, yet
5984 * with a CDCLK frequency other than the minimum. To account
5985 * for this take the PIPE-A power domain, which covers the HW
5986 * blocks needed for the following programming. This can be
5987 * removed once it's guaranteed that we get here either with
5988 * the minimum CDCLK set, or the required power domains
5989 * enabled.
5990 */
5991 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005992
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005993 if (IS_CHERRYVIEW(dev))
5994 cherryview_set_cdclk(dev, req_cdclk);
5995 else
5996 valleyview_set_cdclk(dev, req_cdclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005997
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02005998 vlv_program_pfi_credits(dev_priv);
Imre Deak738c05c2014-11-19 16:25:37 +02005999
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02006000 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006001}
6002
Jesse Barnes89b667f2013-04-18 14:51:36 -07006003static void valleyview_crtc_enable(struct drm_crtc *crtc)
6004{
6005 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006006 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006007 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6008 struct intel_encoder *encoder;
6009 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006010 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006011
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006012 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006013 return;
6014
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006015 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306016
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006017 if (!is_dsi) {
6018 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006019 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006020 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006021 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006022 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006023
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006024 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306025 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006026
6027 intel_set_pipe_timings(intel_crtc);
6028
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006029 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6030 struct drm_i915_private *dev_priv = dev->dev_private;
6031
6032 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6033 I915_WRITE(CHV_CANVAS(pipe), 0);
6034 }
6035
Daniel Vetter5b18e572014-04-24 23:55:06 +02006036 i9xx_set_pipeconf(intel_crtc);
6037
Jesse Barnes89b667f2013-04-18 14:51:36 -07006038 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006039
Daniel Vettera72e4c92014-09-30 10:56:47 +02006040 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006041
Jesse Barnes89b667f2013-04-18 14:51:36 -07006042 for_each_encoder_on_crtc(dev, crtc, encoder)
6043 if (encoder->pre_pll_enable)
6044 encoder->pre_pll_enable(encoder);
6045
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006046 if (!is_dsi) {
6047 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006048 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006049 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006050 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006051 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006052
6053 for_each_encoder_on_crtc(dev, crtc, encoder)
6054 if (encoder->pre_enable)
6055 encoder->pre_enable(encoder);
6056
Jesse Barnes2dd24552013-04-25 12:55:01 -07006057 i9xx_pfit_enable(intel_crtc);
6058
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006059 intel_crtc_load_lut(crtc);
6060
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006061 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006062
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006063 assert_vblank_disabled(crtc);
6064 drm_crtc_vblank_on(crtc);
6065
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006066 for_each_encoder_on_crtc(dev, crtc, encoder)
6067 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006068}
6069
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006070static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6071{
6072 struct drm_device *dev = crtc->base.dev;
6073 struct drm_i915_private *dev_priv = dev->dev_private;
6074
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006075 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6076 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006077}
6078
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006079static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006080{
6081 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006082 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006083 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006084 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006085 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006086
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006087 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006088 return;
6089
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006090 i9xx_set_pll_dividers(intel_crtc);
6091
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006092 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306093 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006094
6095 intel_set_pipe_timings(intel_crtc);
6096
Daniel Vetter5b18e572014-04-24 23:55:06 +02006097 i9xx_set_pipeconf(intel_crtc);
6098
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006099 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006100
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006101 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006102 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006103
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006104 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006105 if (encoder->pre_enable)
6106 encoder->pre_enable(encoder);
6107
Daniel Vetterf6736a12013-06-05 13:34:30 +02006108 i9xx_enable_pll(intel_crtc);
6109
Jesse Barnes2dd24552013-04-25 12:55:01 -07006110 i9xx_pfit_enable(intel_crtc);
6111
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006112 intel_crtc_load_lut(crtc);
6113
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006114 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006115 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006116
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006117 assert_vblank_disabled(crtc);
6118 drm_crtc_vblank_on(crtc);
6119
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006120 for_each_encoder_on_crtc(dev, crtc, encoder)
6121 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006122}
6123
Daniel Vetter87476d62013-04-11 16:29:06 +02006124static void i9xx_pfit_disable(struct intel_crtc *crtc)
6125{
6126 struct drm_device *dev = crtc->base.dev;
6127 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006128
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006129 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006130 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006131
6132 assert_pipe_disabled(dev_priv, crtc->pipe);
6133
Daniel Vetter328d8e82013-05-08 10:36:31 +02006134 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6135 I915_READ(PFIT_CONTROL));
6136 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006137}
6138
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006139static void i9xx_crtc_disable(struct drm_crtc *crtc)
6140{
6141 struct drm_device *dev = crtc->dev;
6142 struct drm_i915_private *dev_priv = dev->dev_private;
6143 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006144 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006145 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006146
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006147 /*
6148 * On gen2 planes are double buffered but the pipe isn't, so we must
6149 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006150 * We also need to wait on all gmch platforms because of the
6151 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006152 */
Imre Deak564ed192014-06-13 14:54:21 +03006153 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006154
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006155 for_each_encoder_on_crtc(dev, crtc, encoder)
6156 encoder->disable(encoder);
6157
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006158 drm_crtc_vblank_off(crtc);
6159 assert_vblank_disabled(crtc);
6160
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006161 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006162
Daniel Vetter87476d62013-04-11 16:29:06 +02006163 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006164
Jesse Barnes89b667f2013-04-18 14:51:36 -07006165 for_each_encoder_on_crtc(dev, crtc, encoder)
6166 if (encoder->post_disable)
6167 encoder->post_disable(encoder);
6168
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006169 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006170 if (IS_CHERRYVIEW(dev))
6171 chv_disable_pll(dev_priv, pipe);
6172 else if (IS_VALLEYVIEW(dev))
6173 vlv_disable_pll(dev_priv, pipe);
6174 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006175 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006176 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006177
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006178 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006179 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Patrik Jakobssone4ca0612015-07-08 15:31:52 +02006180
6181 intel_crtc->active = false;
6182 intel_update_watermarks(crtc);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006183}
6184
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006185static void intel_crtc_disable_noatomic(struct drm_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006186{
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006187 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006188 struct drm_i915_private *dev_priv = to_i915(crtc->dev);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006189 enum intel_display_power_domain domain;
6190 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006191
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006192 if (!intel_crtc->active)
6193 return;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006194
Maarten Lankhorsta5392052015-06-15 12:33:52 +02006195 if (to_intel_plane_state(crtc->primary->state)->visible) {
6196 intel_crtc_wait_for_pending_flips(crtc);
6197 intel_pre_disable_primary(crtc);
6198 }
6199
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +02006200 intel_crtc_disable_planes(crtc, crtc->state->plane_mask);
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006201 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006202
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006203 domains = intel_crtc->enabled_power_domains;
6204 for_each_power_domain(domain, domains)
6205 intel_display_power_put(dev_priv, domain);
6206 intel_crtc->enabled_power_domains = 0;
6207}
6208
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006209/*
6210 * turn all crtc's off, but do not adjust state
6211 * This has to be paired with a call to intel_modeset_setup_hw_state.
6212 */
Maarten Lankhorst9716c692015-06-10 10:24:19 +02006213void intel_display_suspend(struct drm_device *dev)
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006214{
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006215 struct drm_crtc *crtc;
6216
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +02006217 for_each_crtc(dev, crtc)
6218 intel_crtc_disable_noatomic(crtc);
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006219}
6220
Chris Wilsoncdd59982010-09-08 16:30:16 +01006221/* Master function to enable/disable CRTC and corresponding power wells */
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006222int intel_crtc_control(struct drm_crtc *crtc, bool enable)
Daniel Vetter976f8a22012-07-08 22:34:21 +02006223{
6224 struct drm_device *dev = crtc->dev;
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006225 struct drm_mode_config *config = &dev->mode_config;
6226 struct drm_modeset_acquire_ctx *ctx = config->acquire_ctx;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006227 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006228 struct intel_crtc_state *pipe_config;
6229 struct drm_atomic_state *state;
6230 int ret;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006231
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006232 if (enable == intel_crtc->active)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006233 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006234
6235 if (enable && !crtc->state->enable)
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006236 return 0;
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006237
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006238 /* this function should be called with drm_modeset_lock_all for now */
6239 if (WARN_ON(!ctx))
6240 return -EIO;
6241 lockdep_assert_held(&ctx->ww_ctx);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006242
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006243 state = drm_atomic_state_alloc(dev);
6244 if (WARN_ON(!state))
6245 return -ENOMEM;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006246
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006247 state->acquire_ctx = ctx;
6248 state->allow_modeset = true;
6249
6250 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
6251 if (IS_ERR(pipe_config)) {
6252 ret = PTR_ERR(pipe_config);
6253 goto err;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006254 }
Maarten Lankhorst5da76e92015-06-01 12:50:04 +02006255 pipe_config->base.active = enable;
6256
6257 ret = intel_set_mode(state);
6258 if (!ret)
6259 return ret;
6260
6261err:
6262 DRM_ERROR("Updating crtc active failed with %i\n", ret);
6263 drm_atomic_state_free(state);
6264 return ret;
Borun Fub04c5bd2014-07-12 10:02:27 +05306265}
6266
6267/**
6268 * Sets the power management mode of the pipe and plane.
6269 */
6270void intel_crtc_update_dpms(struct drm_crtc *crtc)
6271{
6272 struct drm_device *dev = crtc->dev;
6273 struct intel_encoder *intel_encoder;
6274 bool enable = false;
6275
6276 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6277 enable |= intel_encoder->connectors_active;
6278
6279 intel_crtc_control(crtc, enable);
Chris Wilsoncdd59982010-09-08 16:30:16 +01006280}
6281
Chris Wilsonea5b2132010-08-04 13:50:23 +01006282void intel_encoder_destroy(struct drm_encoder *encoder)
6283{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006284 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006285
Chris Wilsonea5b2132010-08-04 13:50:23 +01006286 drm_encoder_cleanup(encoder);
6287 kfree(intel_encoder);
6288}
6289
Damien Lespiau92373292013-08-08 22:28:57 +01006290/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006291 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6292 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006293static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006294{
6295 if (mode == DRM_MODE_DPMS_ON) {
6296 encoder->connectors_active = true;
6297
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006298 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006299 } else {
6300 encoder->connectors_active = false;
6301
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006302 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006303 }
6304}
6305
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006306/* Cross check the actual hw state with our own modeset state tracking (and it's
6307 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006308static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006309{
6310 if (connector->get_hw_state(connector)) {
6311 struct intel_encoder *encoder = connector->encoder;
6312 struct drm_crtc *crtc;
6313 bool encoder_enabled;
6314 enum pipe pipe;
6315
6316 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6317 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006318 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006319
Dave Airlie0e32b392014-05-02 14:02:48 +10006320 /* there is no real hw state for MST connectors */
6321 if (connector->mst_port)
6322 return;
6323
Rob Clarke2c719b2014-12-15 13:56:32 -05006324 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006325 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006326 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006327 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006328
Dave Airlie36cd7442014-05-02 13:44:18 +10006329 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006330 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006331 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006332
Dave Airlie36cd7442014-05-02 13:44:18 +10006333 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006334 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6335 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006336 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006337
Dave Airlie36cd7442014-05-02 13:44:18 +10006338 crtc = encoder->base.crtc;
6339
Matt Roper83d65732015-02-25 13:12:16 -08006340 I915_STATE_WARN(!crtc->state->enable,
6341 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006342 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6343 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006344 "encoder active on the wrong pipe\n");
6345 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006346 }
6347}
6348
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006349int intel_connector_init(struct intel_connector *connector)
6350{
6351 struct drm_connector_state *connector_state;
6352
6353 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6354 if (!connector_state)
6355 return -ENOMEM;
6356
6357 connector->base.state = connector_state;
6358 return 0;
6359}
6360
6361struct intel_connector *intel_connector_alloc(void)
6362{
6363 struct intel_connector *connector;
6364
6365 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6366 if (!connector)
6367 return NULL;
6368
6369 if (intel_connector_init(connector) < 0) {
6370 kfree(connector);
6371 return NULL;
6372 }
6373
6374 return connector;
6375}
6376
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006377/* Even simpler default implementation, if there's really no special case to
6378 * consider. */
6379void intel_connector_dpms(struct drm_connector *connector, int mode)
6380{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006381 /* All the simple cases only support two dpms states. */
6382 if (mode != DRM_MODE_DPMS_ON)
6383 mode = DRM_MODE_DPMS_OFF;
6384
6385 if (mode == connector->dpms)
6386 return;
6387
6388 connector->dpms = mode;
6389
6390 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006391 if (connector->encoder)
6392 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006393
Daniel Vetterb9805142012-08-31 17:37:33 +02006394 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006395}
6396
Daniel Vetterf0947c32012-07-02 13:10:34 +02006397/* Simple connector->get_hw_state implementation for encoders that support only
6398 * one connector and no cloning and hence the encoder state determines the state
6399 * of the connector. */
6400bool intel_connector_get_hw_state(struct intel_connector *connector)
6401{
Daniel Vetter24929352012-07-02 20:28:59 +02006402 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006403 struct intel_encoder *encoder = connector->encoder;
6404
6405 return encoder->get_hw_state(encoder, &pipe);
6406}
6407
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006408static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006409{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006410 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6411 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006412
6413 return 0;
6414}
6415
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006416static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006417 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006418{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006419 struct drm_atomic_state *state = pipe_config->base.state;
6420 struct intel_crtc *other_crtc;
6421 struct intel_crtc_state *other_crtc_state;
6422
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006423 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6424 pipe_name(pipe), pipe_config->fdi_lanes);
6425 if (pipe_config->fdi_lanes > 4) {
6426 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6427 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006428 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006429 }
6430
Paulo Zanonibafb6552013-11-02 21:07:44 -07006431 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006432 if (pipe_config->fdi_lanes > 2) {
6433 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6434 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006435 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006436 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006437 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006438 }
6439 }
6440
6441 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006442 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006443
6444 /* Ivybridge 3 pipe is really complicated */
6445 switch (pipe) {
6446 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006447 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006448 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006449 if (pipe_config->fdi_lanes <= 2)
6450 return 0;
6451
6452 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6453 other_crtc_state =
6454 intel_atomic_get_crtc_state(state, other_crtc);
6455 if (IS_ERR(other_crtc_state))
6456 return PTR_ERR(other_crtc_state);
6457
6458 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006459 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6460 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006462 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006463 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006464 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006465 if (pipe_config->fdi_lanes > 2) {
6466 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6467 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006468 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006469 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470
6471 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6472 other_crtc_state =
6473 intel_atomic_get_crtc_state(state, other_crtc);
6474 if (IS_ERR(other_crtc_state))
6475 return PTR_ERR(other_crtc_state);
6476
6477 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006478 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006479 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006481 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006482 default:
6483 BUG();
6484 }
6485}
6486
Daniel Vettere29c22c2013-02-21 00:00:16 +01006487#define RETRY 1
6488static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006489 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006490{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006492 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006493 int lane, link_bw, fdi_dotclock, ret;
6494 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006495
Daniel Vettere29c22c2013-02-21 00:00:16 +01006496retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006497 /* FDI is a binary signal running at ~2.7GHz, encoding
6498 * each output octet as 10 bits. The actual frequency
6499 * is stored as a divider into a 100MHz clock, and the
6500 * mode pixel clock is stored in units of 1KHz.
6501 * Hence the bw of each lane in terms of the mode signal
6502 * is:
6503 */
6504 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6505
Damien Lespiau241bfc32013-09-25 16:45:37 +01006506 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006507
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006508 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006509 pipe_config->pipe_bpp);
6510
6511 pipe_config->fdi_lanes = lane;
6512
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006513 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006514 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006515
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006516 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6517 intel_crtc->pipe, pipe_config);
6518 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006519 pipe_config->pipe_bpp -= 2*3;
6520 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6521 pipe_config->pipe_bpp);
6522 needs_recompute = true;
6523 pipe_config->bw_constrained = true;
6524
6525 goto retry;
6526 }
6527
6528 if (needs_recompute)
6529 return RETRY;
6530
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006531 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006532}
6533
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006534static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6535 struct intel_crtc_state *pipe_config)
6536{
6537 if (pipe_config->pipe_bpp > 24)
6538 return false;
6539
6540 /* HSW can handle pixel rate up to cdclk? */
6541 if (IS_HASWELL(dev_priv->dev))
6542 return true;
6543
6544 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006545 * We compare against max which means we must take
6546 * the increased cdclk requirement into account when
6547 * calculating the new cdclk.
6548 *
6549 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006550 */
6551 return ilk_pipe_pixel_rate(pipe_config) <=
6552 dev_priv->max_cdclk_freq * 95 / 100;
6553}
6554
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006555static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006556 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006557{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006558 struct drm_device *dev = crtc->base.dev;
6559 struct drm_i915_private *dev_priv = dev->dev_private;
6560
Jani Nikulad330a952014-01-21 11:24:25 +02006561 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006562 hsw_crtc_supports_ips(crtc) &&
6563 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006564}
6565
Daniel Vettera43f6e02013-06-07 23:10:32 +02006566static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006567 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006568{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006569 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006570 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006571 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chris Wilson89749352010-09-12 18:25:19 +01006572
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006573 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006574 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006575 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006576
6577 /*
6578 * Enable pixel doubling when the dot clock
6579 * is > 90% of the (display) core speed.
6580 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006581 * GDG double wide on either pipe,
6582 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006583 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006584 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006585 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006586 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006587 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006588 }
6589
Damien Lespiau241bfc32013-09-25 16:45:37 +01006590 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006591 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006592 }
Chris Wilson89749352010-09-12 18:25:19 +01006593
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006594 /*
6595 * Pipe horizontal size must be even in:
6596 * - DVO ganged mode
6597 * - LVDS dual channel mode
6598 * - Double wide pipe
6599 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006600 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006601 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6602 pipe_config->pipe_src_w &= ~1;
6603
Damien Lespiau8693a822013-05-03 18:48:11 +01006604 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6605 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006606 */
6607 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6608 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006609 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006610
Damien Lespiauf5adf942013-06-24 18:29:34 +01006611 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006612 hsw_compute_ips_config(crtc, pipe_config);
6613
Daniel Vetter877d48d2013-04-19 11:24:43 +02006614 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006615 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006616
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +02006617 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08006618}
6619
Ville Syrjälä1652d192015-03-31 14:12:01 +03006620static int skylake_get_display_clock_speed(struct drm_device *dev)
6621{
6622 struct drm_i915_private *dev_priv = to_i915(dev);
6623 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6624 uint32_t cdctl = I915_READ(CDCLK_CTL);
6625 uint32_t linkrate;
6626
Damien Lespiau414355a2015-06-04 18:21:31 +01006627 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006628 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006629
6630 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6631 return 540000;
6632
6633 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006634 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006635
Damien Lespiau71cd8422015-04-30 16:39:17 +01006636 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6637 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006638 /* vco 8640 */
6639 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6640 case CDCLK_FREQ_450_432:
6641 return 432000;
6642 case CDCLK_FREQ_337_308:
6643 return 308570;
6644 case CDCLK_FREQ_675_617:
6645 return 617140;
6646 default:
6647 WARN(1, "Unknown cd freq selection\n");
6648 }
6649 } else {
6650 /* vco 8100 */
6651 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6652 case CDCLK_FREQ_450_432:
6653 return 450000;
6654 case CDCLK_FREQ_337_308:
6655 return 337500;
6656 case CDCLK_FREQ_675_617:
6657 return 675000;
6658 default:
6659 WARN(1, "Unknown cd freq selection\n");
6660 }
6661 }
6662
6663 /* error case, do as if DPLL0 isn't enabled */
6664 return 24000;
6665}
6666
Bob Paauweacd3f3d2015-06-23 14:14:26 -07006667static int broxton_get_display_clock_speed(struct drm_device *dev)
6668{
6669 struct drm_i915_private *dev_priv = to_i915(dev);
6670 uint32_t cdctl = I915_READ(CDCLK_CTL);
6671 uint32_t pll_ratio = I915_READ(BXT_DE_PLL_CTL) & BXT_DE_PLL_RATIO_MASK;
6672 uint32_t pll_enab = I915_READ(BXT_DE_PLL_ENABLE);
6673 int cdclk;
6674
6675 if (!(pll_enab & BXT_DE_PLL_PLL_ENABLE))
6676 return 19200;
6677
6678 cdclk = 19200 * pll_ratio / 2;
6679
6680 switch (cdctl & BXT_CDCLK_CD2X_DIV_SEL_MASK) {
6681 case BXT_CDCLK_CD2X_DIV_SEL_1:
6682 return cdclk; /* 576MHz or 624MHz */
6683 case BXT_CDCLK_CD2X_DIV_SEL_1_5:
6684 return cdclk * 2 / 3; /* 384MHz */
6685 case BXT_CDCLK_CD2X_DIV_SEL_2:
6686 return cdclk / 2; /* 288MHz */
6687 case BXT_CDCLK_CD2X_DIV_SEL_4:
6688 return cdclk / 4; /* 144MHz */
6689 }
6690
6691 /* error case, do as if DE PLL isn't enabled */
6692 return 19200;
6693}
6694
Ville Syrjälä1652d192015-03-31 14:12:01 +03006695static int broadwell_get_display_clock_speed(struct drm_device *dev)
6696{
6697 struct drm_i915_private *dev_priv = dev->dev_private;
6698 uint32_t lcpll = I915_READ(LCPLL_CTL);
6699 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6700
6701 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6702 return 800000;
6703 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6704 return 450000;
6705 else if (freq == LCPLL_CLK_FREQ_450)
6706 return 450000;
6707 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6708 return 540000;
6709 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6710 return 337500;
6711 else
6712 return 675000;
6713}
6714
6715static int haswell_get_display_clock_speed(struct drm_device *dev)
6716{
6717 struct drm_i915_private *dev_priv = dev->dev_private;
6718 uint32_t lcpll = I915_READ(LCPLL_CTL);
6719 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6720
6721 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6722 return 800000;
6723 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6724 return 450000;
6725 else if (freq == LCPLL_CLK_FREQ_450)
6726 return 450000;
6727 else if (IS_HSW_ULT(dev))
6728 return 337500;
6729 else
6730 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006731}
6732
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006733static int valleyview_get_display_clock_speed(struct drm_device *dev)
6734{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006735 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006736 u32 val;
6737 int divider;
6738
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006739 if (dev_priv->hpll_freq == 0)
6740 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6741
Ville Syrjäläa5805162015-05-26 20:42:30 +03006742 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006743 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006744 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006745
6746 divider = val & DISPLAY_FREQUENCY_VALUES;
6747
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006748 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6749 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6750 "cdclk change in progress\n");
6751
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006752 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006753}
6754
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006755static int ilk_get_display_clock_speed(struct drm_device *dev)
6756{
6757 return 450000;
6758}
6759
Jesse Barnese70236a2009-09-21 10:42:27 -07006760static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006761{
Jesse Barnese70236a2009-09-21 10:42:27 -07006762 return 400000;
6763}
Jesse Barnes79e53942008-11-07 14:24:08 -08006764
Jesse Barnese70236a2009-09-21 10:42:27 -07006765static int i915_get_display_clock_speed(struct drm_device *dev)
6766{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006767 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006768}
Jesse Barnes79e53942008-11-07 14:24:08 -08006769
Jesse Barnese70236a2009-09-21 10:42:27 -07006770static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6771{
6772 return 200000;
6773}
Jesse Barnes79e53942008-11-07 14:24:08 -08006774
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006775static int pnv_get_display_clock_speed(struct drm_device *dev)
6776{
6777 u16 gcfgc = 0;
6778
6779 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6780
6781 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6782 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006783 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006784 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006785 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006786 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006787 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006788 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6789 return 200000;
6790 default:
6791 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6792 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006793 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006794 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006795 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006796 }
6797}
6798
Jesse Barnese70236a2009-09-21 10:42:27 -07006799static int i915gm_get_display_clock_speed(struct drm_device *dev)
6800{
6801 u16 gcfgc = 0;
6802
6803 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6804
6805 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006806 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006807 else {
6808 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6809 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006810 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006811 default:
6812 case GC_DISPLAY_CLOCK_190_200_MHZ:
6813 return 190000;
6814 }
6815 }
6816}
Jesse Barnes79e53942008-11-07 14:24:08 -08006817
Jesse Barnese70236a2009-09-21 10:42:27 -07006818static int i865_get_display_clock_speed(struct drm_device *dev)
6819{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006820 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006821}
6822
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006823static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006824{
6825 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006826
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006827 /*
6828 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6829 * encoding is different :(
6830 * FIXME is this the right way to detect 852GM/852GMV?
6831 */
6832 if (dev->pdev->revision == 0x1)
6833 return 133333;
6834
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006835 pci_bus_read_config_word(dev->pdev->bus,
6836 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6837
Jesse Barnese70236a2009-09-21 10:42:27 -07006838 /* Assume that the hardware is in the high speed state. This
6839 * should be the default.
6840 */
6841 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6842 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006843 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006844 case GC_CLOCK_100_200:
6845 return 200000;
6846 case GC_CLOCK_166_250:
6847 return 250000;
6848 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006849 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006850 case GC_CLOCK_133_266:
6851 case GC_CLOCK_133_266_2:
6852 case GC_CLOCK_166_266:
6853 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006854 }
6855
6856 /* Shouldn't happen */
6857 return 0;
6858}
6859
6860static int i830_get_display_clock_speed(struct drm_device *dev)
6861{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006862 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006863}
6864
Ville Syrjälä34edce22015-05-22 11:22:33 +03006865static unsigned int intel_hpll_vco(struct drm_device *dev)
6866{
6867 struct drm_i915_private *dev_priv = dev->dev_private;
6868 static const unsigned int blb_vco[8] = {
6869 [0] = 3200000,
6870 [1] = 4000000,
6871 [2] = 5333333,
6872 [3] = 4800000,
6873 [4] = 6400000,
6874 };
6875 static const unsigned int pnv_vco[8] = {
6876 [0] = 3200000,
6877 [1] = 4000000,
6878 [2] = 5333333,
6879 [3] = 4800000,
6880 [4] = 2666667,
6881 };
6882 static const unsigned int cl_vco[8] = {
6883 [0] = 3200000,
6884 [1] = 4000000,
6885 [2] = 5333333,
6886 [3] = 6400000,
6887 [4] = 3333333,
6888 [5] = 3566667,
6889 [6] = 4266667,
6890 };
6891 static const unsigned int elk_vco[8] = {
6892 [0] = 3200000,
6893 [1] = 4000000,
6894 [2] = 5333333,
6895 [3] = 4800000,
6896 };
6897 static const unsigned int ctg_vco[8] = {
6898 [0] = 3200000,
6899 [1] = 4000000,
6900 [2] = 5333333,
6901 [3] = 6400000,
6902 [4] = 2666667,
6903 [5] = 4266667,
6904 };
6905 const unsigned int *vco_table;
6906 unsigned int vco;
6907 uint8_t tmp = 0;
6908
6909 /* FIXME other chipsets? */
6910 if (IS_GM45(dev))
6911 vco_table = ctg_vco;
6912 else if (IS_G4X(dev))
6913 vco_table = elk_vco;
6914 else if (IS_CRESTLINE(dev))
6915 vco_table = cl_vco;
6916 else if (IS_PINEVIEW(dev))
6917 vco_table = pnv_vco;
6918 else if (IS_G33(dev))
6919 vco_table = blb_vco;
6920 else
6921 return 0;
6922
6923 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6924
6925 vco = vco_table[tmp & 0x7];
6926 if (vco == 0)
6927 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6928 else
6929 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6930
6931 return vco;
6932}
6933
6934static int gm45_get_display_clock_speed(struct drm_device *dev)
6935{
6936 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6937 uint16_t tmp = 0;
6938
6939 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6940
6941 cdclk_sel = (tmp >> 12) & 0x1;
6942
6943 switch (vco) {
6944 case 2666667:
6945 case 4000000:
6946 case 5333333:
6947 return cdclk_sel ? 333333 : 222222;
6948 case 3200000:
6949 return cdclk_sel ? 320000 : 228571;
6950 default:
6951 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6952 return 222222;
6953 }
6954}
6955
6956static int i965gm_get_display_clock_speed(struct drm_device *dev)
6957{
6958 static const uint8_t div_3200[] = { 16, 10, 8 };
6959 static const uint8_t div_4000[] = { 20, 12, 10 };
6960 static const uint8_t div_5333[] = { 24, 16, 14 };
6961 const uint8_t *div_table;
6962 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6963 uint16_t tmp = 0;
6964
6965 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6966
6967 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6968
6969 if (cdclk_sel >= ARRAY_SIZE(div_3200))
6970 goto fail;
6971
6972 switch (vco) {
6973 case 3200000:
6974 div_table = div_3200;
6975 break;
6976 case 4000000:
6977 div_table = div_4000;
6978 break;
6979 case 5333333:
6980 div_table = div_5333;
6981 break;
6982 default:
6983 goto fail;
6984 }
6985
6986 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
6987
Damien Lespiaucaf4e252015-06-04 16:56:18 +01006988fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03006989 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
6990 return 200000;
6991}
6992
6993static int g33_get_display_clock_speed(struct drm_device *dev)
6994{
6995 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
6996 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
6997 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
6998 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
6999 const uint8_t *div_table;
7000 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7001 uint16_t tmp = 0;
7002
7003 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7004
7005 cdclk_sel = (tmp >> 4) & 0x7;
7006
7007 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7008 goto fail;
7009
7010 switch (vco) {
7011 case 3200000:
7012 div_table = div_3200;
7013 break;
7014 case 4000000:
7015 div_table = div_4000;
7016 break;
7017 case 4800000:
7018 div_table = div_4800;
7019 break;
7020 case 5333333:
7021 div_table = div_5333;
7022 break;
7023 default:
7024 goto fail;
7025 }
7026
7027 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7028
Damien Lespiaucaf4e252015-06-04 16:56:18 +01007029fail:
Ville Syrjälä34edce22015-05-22 11:22:33 +03007030 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7031 return 190476;
7032}
7033
Zhenyu Wang2c072452009-06-05 15:38:42 +08007034static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007035intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007036{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007037 while (*num > DATA_LINK_M_N_MASK ||
7038 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007039 *num >>= 1;
7040 *den >>= 1;
7041 }
7042}
7043
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007044static void compute_m_n(unsigned int m, unsigned int n,
7045 uint32_t *ret_m, uint32_t *ret_n)
7046{
7047 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7048 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7049 intel_reduce_m_n_ratio(ret_m, ret_n);
7050}
7051
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007052void
7053intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7054 int pixel_clock, int link_clock,
7055 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007056{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007057 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007058
7059 compute_m_n(bits_per_pixel * pixel_clock,
7060 link_clock * nlanes * 8,
7061 &m_n->gmch_m, &m_n->gmch_n);
7062
7063 compute_m_n(pixel_clock, link_clock,
7064 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007065}
7066
Chris Wilsona7615032011-01-12 17:04:08 +00007067static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7068{
Jani Nikulad330a952014-01-21 11:24:25 +02007069 if (i915.panel_use_ssc >= 0)
7070 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007071 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007072 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007073}
7074
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007075static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7076 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007077{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007078 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007079 struct drm_i915_private *dev_priv = dev->dev_private;
7080 int refclk;
7081
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007082 WARN_ON(!crtc_state->base.state);
7083
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007084 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007085 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007086 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007087 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007088 refclk = dev_priv->vbt.lvds_ssc_freq;
7089 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007090 } else if (!IS_GEN2(dev)) {
7091 refclk = 96000;
7092 } else {
7093 refclk = 48000;
7094 }
7095
7096 return refclk;
7097}
7098
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007099static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007100{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007101 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007102}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007103
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007104static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7105{
7106 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007107}
7108
Daniel Vetterf47709a2013-03-28 10:42:02 +01007109static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007110 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007111 intel_clock_t *reduced_clock)
7112{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007113 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007114 u32 fp, fp2 = 0;
7115
7116 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007117 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007118 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007119 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007120 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007121 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007122 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007123 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007124 }
7125
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007126 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007127
Daniel Vetterf47709a2013-03-28 10:42:02 +01007128 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007129 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007130 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007131 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007132 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007133 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007134 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007135 }
7136}
7137
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007138static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7139 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007140{
7141 u32 reg_val;
7142
7143 /*
7144 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7145 * and set it to a reasonable value instead.
7146 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007147 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007148 reg_val &= 0xffffff00;
7149 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007150 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007151
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007152 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007153 reg_val &= 0x8cffffff;
7154 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007155 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007156
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007157 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007158 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007159 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007160
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007161 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007162 reg_val &= 0x00ffffff;
7163 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007164 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007165}
7166
Daniel Vetterb5518422013-05-03 11:49:48 +02007167static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7168 struct intel_link_m_n *m_n)
7169{
7170 struct drm_device *dev = crtc->base.dev;
7171 struct drm_i915_private *dev_priv = dev->dev_private;
7172 int pipe = crtc->pipe;
7173
Daniel Vettere3b95f12013-05-03 11:49:49 +02007174 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7175 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7176 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7177 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007178}
7179
7180static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007181 struct intel_link_m_n *m_n,
7182 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007183{
7184 struct drm_device *dev = crtc->base.dev;
7185 struct drm_i915_private *dev_priv = dev->dev_private;
7186 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007187 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007188
7189 if (INTEL_INFO(dev)->gen >= 5) {
7190 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7191 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7192 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7193 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007194 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7195 * for gen < 8) and if DRRS is supported (to make sure the
7196 * registers are not unnecessarily accessed).
7197 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307198 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007199 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007200 I915_WRITE(PIPE_DATA_M2(transcoder),
7201 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7202 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7203 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7204 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7205 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007206 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007207 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7208 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7209 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7210 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007211 }
7212}
7213
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307214void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007215{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307216 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7217
7218 if (m_n == M1_N1) {
7219 dp_m_n = &crtc->config->dp_m_n;
7220 dp_m2_n2 = &crtc->config->dp_m2_n2;
7221 } else if (m_n == M2_N2) {
7222
7223 /*
7224 * M2_N2 registers are not supported. Hence m2_n2 divider value
7225 * needs to be programmed into M1_N1.
7226 */
7227 dp_m_n = &crtc->config->dp_m2_n2;
7228 } else {
7229 DRM_ERROR("Unsupported divider value\n");
7230 return;
7231 }
7232
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007233 if (crtc->config->has_pch_encoder)
7234 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007235 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307236 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007237}
7238
Daniel Vetter251ac862015-06-18 10:30:24 +02007239static void vlv_compute_dpll(struct intel_crtc *crtc,
7240 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007241{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007242 u32 dpll, dpll_md;
7243
7244 /*
7245 * Enable DPIO clock input. We should never disable the reference
7246 * clock for pipe B, since VGA hotplug / manual detection depends
7247 * on it.
7248 */
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007249 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REF_CLK_ENABLE_VLV |
7250 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_REF_CLK_VLV;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007251 /* We should never disable this, set it here for state tracking */
7252 if (crtc->pipe == PIPE_B)
7253 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7254 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007255 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007256
Ville Syrjäläd288f652014-10-28 13:20:22 +02007257 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007258 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007259 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007260}
7261
Ville Syrjäläd288f652014-10-28 13:20:22 +02007262static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007263 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007264{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007265 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007266 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007267 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007268 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007269 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007270 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007271
Ville Syrjäläa5805162015-05-26 20:42:30 +03007272 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007273
Ville Syrjäläd288f652014-10-28 13:20:22 +02007274 bestn = pipe_config->dpll.n;
7275 bestm1 = pipe_config->dpll.m1;
7276 bestm2 = pipe_config->dpll.m2;
7277 bestp1 = pipe_config->dpll.p1;
7278 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007279
Jesse Barnes89b667f2013-04-18 14:51:36 -07007280 /* See eDP HDMI DPIO driver vbios notes doc */
7281
7282 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007283 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007284 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007285
7286 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007287 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007288
7289 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007290 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007291 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007292 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007293
7294 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007295 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007296
7297 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007298 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7299 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7300 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007301 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007302
7303 /*
7304 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7305 * but we don't support that).
7306 * Note: don't use the DAC post divider as it seems unstable.
7307 */
7308 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007309 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007310
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007311 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007312 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007313
Jesse Barnes89b667f2013-04-18 14:51:36 -07007314 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007315 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007316 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7317 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007318 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007319 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007320 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007321 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007322 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007323
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007324 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007325 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007326 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007327 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007328 0x0df40000);
7329 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007330 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007331 0x0df70000);
7332 } else { /* HDMI or VGA */
7333 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007334 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007335 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007336 0x0df70000);
7337 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007338 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007339 0x0df40000);
7340 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007341
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007342 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007343 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007344 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7345 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007346 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007347 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007348
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007349 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007350 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007351}
7352
Daniel Vetter251ac862015-06-18 10:30:24 +02007353static void chv_compute_dpll(struct intel_crtc *crtc,
7354 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007355{
Ville Syrjälä60bfe442015-06-29 15:25:49 +03007356 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLK_CHV |
7357 DPLL_REF_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007358 DPLL_VCO_ENABLE;
7359 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007360 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007361
Ville Syrjäläd288f652014-10-28 13:20:22 +02007362 pipe_config->dpll_hw_state.dpll_md =
7363 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007364}
7365
Ville Syrjäläd288f652014-10-28 13:20:22 +02007366static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007367 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007368{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007369 struct drm_device *dev = crtc->base.dev;
7370 struct drm_i915_private *dev_priv = dev->dev_private;
7371 int pipe = crtc->pipe;
7372 int dpll_reg = DPLL(crtc->pipe);
7373 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307374 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007375 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307376 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307377 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007378
Ville Syrjäläd288f652014-10-28 13:20:22 +02007379 bestn = pipe_config->dpll.n;
7380 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7381 bestm1 = pipe_config->dpll.m1;
7382 bestm2 = pipe_config->dpll.m2 >> 22;
7383 bestp1 = pipe_config->dpll.p1;
7384 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307385 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307386 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307387 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007388
7389 /*
7390 * Enable Refclk and SSC
7391 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007392 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007393 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007394
Ville Syrjäläa5805162015-05-26 20:42:30 +03007395 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007396
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007397 /* p1 and p2 divider */
7398 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7399 5 << DPIO_CHV_S1_DIV_SHIFT |
7400 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7401 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7402 1 << DPIO_CHV_K_DIV_SHIFT);
7403
7404 /* Feedback post-divider - m2 */
7405 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7406
7407 /* Feedback refclk divider - n and m1 */
7408 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7409 DPIO_CHV_M1_DIV_BY_2 |
7410 1 << DPIO_CHV_N_DIV_SHIFT);
7411
7412 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307413 if (bestm2_frac)
7414 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007415
7416 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307417 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7418 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7419 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7420 if (bestm2_frac)
7421 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7422 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007423
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307424 /* Program digital lock detect threshold */
7425 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7426 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7427 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7428 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7429 if (!bestm2_frac)
7430 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7431 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7432
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007433 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307434 if (vco == 5400000) {
7435 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7436 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7437 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7438 tribuf_calcntr = 0x9;
7439 } else if (vco <= 6200000) {
7440 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7441 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7442 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7443 tribuf_calcntr = 0x9;
7444 } else if (vco <= 6480000) {
7445 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7446 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7447 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7448 tribuf_calcntr = 0x8;
7449 } else {
7450 /* Not supported. Apply the same limits as in the max case */
7451 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7452 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7453 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7454 tribuf_calcntr = 0;
7455 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007456 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7457
Ville Syrjälä968040b2015-03-11 22:52:08 +02007458 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307459 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7460 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7461 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7462
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007463 /* AFC Recal */
7464 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7465 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7466 DPIO_AFC_RECAL);
7467
Ville Syrjäläa5805162015-05-26 20:42:30 +03007468 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007469}
7470
Ville Syrjäläd288f652014-10-28 13:20:22 +02007471/**
7472 * vlv_force_pll_on - forcibly enable just the PLL
7473 * @dev_priv: i915 private structure
7474 * @pipe: pipe PLL to enable
7475 * @dpll: PLL configuration
7476 *
7477 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7478 * in cases where we need the PLL enabled even when @pipe is not going to
7479 * be enabled.
7480 */
7481void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7482 const struct dpll *dpll)
7483{
7484 struct intel_crtc *crtc =
7485 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007486 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007487 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007488 .pixel_multiplier = 1,
7489 .dpll = *dpll,
7490 };
7491
7492 if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007493 chv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007494 chv_prepare_pll(crtc, &pipe_config);
7495 chv_enable_pll(crtc, &pipe_config);
7496 } else {
Daniel Vetter251ac862015-06-18 10:30:24 +02007497 vlv_compute_dpll(crtc, &pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +02007498 vlv_prepare_pll(crtc, &pipe_config);
7499 vlv_enable_pll(crtc, &pipe_config);
7500 }
7501}
7502
7503/**
7504 * vlv_force_pll_off - forcibly disable just the PLL
7505 * @dev_priv: i915 private structure
7506 * @pipe: pipe PLL to disable
7507 *
7508 * Disable the PLL for @pipe. To be used in cases where we need
7509 * the PLL enabled even when @pipe is not going to be enabled.
7510 */
7511void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7512{
7513 if (IS_CHERRYVIEW(dev))
7514 chv_disable_pll(to_i915(dev), pipe);
7515 else
7516 vlv_disable_pll(to_i915(dev), pipe);
7517}
7518
Daniel Vetter251ac862015-06-18 10:30:24 +02007519static void i9xx_compute_dpll(struct intel_crtc *crtc,
7520 struct intel_crtc_state *crtc_state,
7521 intel_clock_t *reduced_clock,
7522 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007523{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007524 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007525 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007526 u32 dpll;
7527 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007528 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007529
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007530 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307531
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007532 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7533 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007534
7535 dpll = DPLL_VGA_MODE_DIS;
7536
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007537 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007538 dpll |= DPLLB_MODE_LVDS;
7539 else
7540 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007541
Daniel Vetteref1b4602013-06-01 17:17:04 +02007542 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007543 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007544 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007545 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007546
7547 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007548 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007549
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007550 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007551 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007552
7553 /* compute bitmask from p1 value */
7554 if (IS_PINEVIEW(dev))
7555 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7556 else {
7557 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7558 if (IS_G4X(dev) && reduced_clock)
7559 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7560 }
7561 switch (clock->p2) {
7562 case 5:
7563 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7564 break;
7565 case 7:
7566 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7567 break;
7568 case 10:
7569 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7570 break;
7571 case 14:
7572 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7573 break;
7574 }
7575 if (INTEL_INFO(dev)->gen >= 4)
7576 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7577
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007578 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007579 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007580 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007581 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7582 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7583 else
7584 dpll |= PLL_REF_INPUT_DREFCLK;
7585
7586 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007587 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007588
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007589 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007590 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007591 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007592 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007593 }
7594}
7595
Daniel Vetter251ac862015-06-18 10:30:24 +02007596static void i8xx_compute_dpll(struct intel_crtc *crtc,
7597 struct intel_crtc_state *crtc_state,
7598 intel_clock_t *reduced_clock,
7599 int num_connectors)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007600{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007601 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007602 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007603 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007604 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007605
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007606 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307607
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007608 dpll = DPLL_VGA_MODE_DIS;
7609
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007610 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007611 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7612 } else {
7613 if (clock->p1 == 2)
7614 dpll |= PLL_P1_DIVIDE_BY_TWO;
7615 else
7616 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7617 if (clock->p2 == 4)
7618 dpll |= PLL_P2_DIVIDE_BY_4;
7619 }
7620
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007621 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007622 dpll |= DPLL_DVO_2X_MODE;
7623
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007624 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007625 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7626 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7627 else
7628 dpll |= PLL_REF_INPUT_DREFCLK;
7629
7630 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007631 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007632}
7633
Daniel Vetter8a654f32013-06-01 17:16:22 +02007634static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007635{
7636 struct drm_device *dev = intel_crtc->base.dev;
7637 struct drm_i915_private *dev_priv = dev->dev_private;
7638 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007639 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007640 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007641 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007642 uint32_t crtc_vtotal, crtc_vblank_end;
7643 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007644
7645 /* We need to be careful not to changed the adjusted mode, for otherwise
7646 * the hw state checker will get angry at the mismatch. */
7647 crtc_vtotal = adjusted_mode->crtc_vtotal;
7648 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007649
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007650 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007651 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007652 crtc_vtotal -= 1;
7653 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007654
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007655 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007656 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7657 else
7658 vsyncshift = adjusted_mode->crtc_hsync_start -
7659 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007660 if (vsyncshift < 0)
7661 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007662 }
7663
7664 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007665 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007666
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007667 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007668 (adjusted_mode->crtc_hdisplay - 1) |
7669 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007670 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007671 (adjusted_mode->crtc_hblank_start - 1) |
7672 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007673 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007674 (adjusted_mode->crtc_hsync_start - 1) |
7675 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7676
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007677 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007678 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007679 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007680 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007681 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007682 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007683 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007684 (adjusted_mode->crtc_vsync_start - 1) |
7685 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7686
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007687 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7688 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7689 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7690 * bits. */
7691 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7692 (pipe == PIPE_B || pipe == PIPE_C))
7693 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7694
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007695 /* pipesrc controls the size that is scaled from, which should
7696 * always be the user's requested size.
7697 */
7698 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007699 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7700 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007701}
7702
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007703static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007704 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007705{
7706 struct drm_device *dev = crtc->base.dev;
7707 struct drm_i915_private *dev_priv = dev->dev_private;
7708 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7709 uint32_t tmp;
7710
7711 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007712 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7713 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007714 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007715 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7716 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007717 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007718 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7719 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007720
7721 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007722 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7723 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007724 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007725 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7726 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007727 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007728 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7729 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007730
7731 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007732 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7733 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7734 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007735 }
7736
7737 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007738 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7739 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7740
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007741 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7742 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007743}
7744
Daniel Vetterf6a83282014-02-11 15:28:57 -08007745void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007746 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007747{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007748 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7749 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7750 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7751 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007752
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007753 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7754 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7755 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7756 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007757
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007758 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007759
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007760 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7761 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007762}
7763
Daniel Vetter84b046f2013-02-19 18:48:54 +01007764static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7765{
7766 struct drm_device *dev = intel_crtc->base.dev;
7767 struct drm_i915_private *dev_priv = dev->dev_private;
7768 uint32_t pipeconf;
7769
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007770 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007771
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007772 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7773 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7774 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007775
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007776 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007777 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007778
Daniel Vetterff9ce462013-04-24 14:57:17 +02007779 /* only g4x and later have fancy bpc/dither controls */
7780 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007781 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007782 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007783 pipeconf |= PIPECONF_DITHER_EN |
7784 PIPECONF_DITHER_TYPE_SP;
7785
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007786 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007787 case 18:
7788 pipeconf |= PIPECONF_6BPC;
7789 break;
7790 case 24:
7791 pipeconf |= PIPECONF_8BPC;
7792 break;
7793 case 30:
7794 pipeconf |= PIPECONF_10BPC;
7795 break;
7796 default:
7797 /* Case prevented by intel_choose_pipe_bpp_dither. */
7798 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007799 }
7800 }
7801
7802 if (HAS_PIPE_CXSR(dev)) {
7803 if (intel_crtc->lowfreq_avail) {
7804 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7805 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7806 } else {
7807 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007808 }
7809 }
7810
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007811 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007812 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007813 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007814 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7815 else
7816 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7817 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007818 pipeconf |= PIPECONF_PROGRESSIVE;
7819
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007820 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007821 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007822
Daniel Vetter84b046f2013-02-19 18:48:54 +01007823 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7824 POSTING_READ(PIPECONF(intel_crtc->pipe));
7825}
7826
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007827static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7828 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007829{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007830 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007831 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007832 int refclk, num_connectors = 0;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007833 intel_clock_t clock;
7834 bool ok;
7835 bool is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007836 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007837 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007838 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007839 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007840 struct drm_connector_state *connector_state;
7841 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007842
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007843 memset(&crtc_state->dpll_hw_state, 0,
7844 sizeof(crtc_state->dpll_hw_state));
7845
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007846 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007847 if (connector_state->crtc != &crtc->base)
7848 continue;
7849
7850 encoder = to_intel_encoder(connector_state->best_encoder);
7851
Chris Wilson5eddb702010-09-11 13:48:45 +01007852 switch (encoder->type) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007853 case INTEL_OUTPUT_DSI:
7854 is_dsi = true;
7855 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007856 default:
7857 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007858 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007859
Eric Anholtc751ce42010-03-25 11:48:48 -07007860 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007861 }
7862
Jani Nikulaf2335332013-09-13 11:03:09 +03007863 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007864 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007865
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007866 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007867 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007868
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007869 /*
7870 * Returns a set of divisors for the desired target clock with
7871 * the given refclk, or FALSE. The returned values represent
7872 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7873 * 2) / p1 / p2.
7874 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007875 limit = intel_limit(crtc_state, refclk);
7876 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007877 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007878 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007879 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007880 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7881 return -EINVAL;
7882 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007883
Jani Nikulaf2335332013-09-13 11:03:09 +03007884 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007885 crtc_state->dpll.n = clock.n;
7886 crtc_state->dpll.m1 = clock.m1;
7887 crtc_state->dpll.m2 = clock.m2;
7888 crtc_state->dpll.p1 = clock.p1;
7889 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007890 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007891
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007892 if (IS_GEN2(dev)) {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007893 i8xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007894 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007895 } else if (IS_CHERRYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007896 chv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007897 } else if (IS_VALLEYVIEW(dev)) {
Daniel Vetter251ac862015-06-18 10:30:24 +02007898 vlv_compute_dpll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007899 } else {
Daniel Vetterc329a4e2015-06-18 10:30:23 +02007900 i9xx_compute_dpll(crtc, crtc_state, NULL,
Daniel Vetter251ac862015-06-18 10:30:24 +02007901 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007902 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007903
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007904 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007905}
7906
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007907static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007908 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007909{
7910 struct drm_device *dev = crtc->base.dev;
7911 struct drm_i915_private *dev_priv = dev->dev_private;
7912 uint32_t tmp;
7913
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007914 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7915 return;
7916
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007917 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007918 if (!(tmp & PFIT_ENABLE))
7919 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007920
Daniel Vetter06922822013-07-11 13:35:40 +02007921 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007922 if (INTEL_INFO(dev)->gen < 4) {
7923 if (crtc->pipe != PIPE_B)
7924 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007925 } else {
7926 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7927 return;
7928 }
7929
Daniel Vetter06922822013-07-11 13:35:40 +02007930 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007931 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7932 if (INTEL_INFO(dev)->gen < 5)
7933 pipe_config->gmch_pfit.lvds_border_bits =
7934 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7935}
7936
Jesse Barnesacbec812013-09-20 11:29:32 -07007937static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007938 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007939{
7940 struct drm_device *dev = crtc->base.dev;
7941 struct drm_i915_private *dev_priv = dev->dev_private;
7942 int pipe = pipe_config->cpu_transcoder;
7943 intel_clock_t clock;
7944 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007945 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007946
Shobhit Kumarf573de52014-07-30 20:32:37 +05307947 /* In case of MIPI DPLL will not even be used */
7948 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7949 return;
7950
Ville Syrjäläa5805162015-05-26 20:42:30 +03007951 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007952 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007953 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007954
7955 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7956 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7957 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7958 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7959 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7960
Imre Deakdccbea32015-06-22 23:35:51 +03007961 pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007962}
7963
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007964static void
7965i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7966 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007967{
7968 struct drm_device *dev = crtc->base.dev;
7969 struct drm_i915_private *dev_priv = dev->dev_private;
7970 u32 val, base, offset;
7971 int pipe = crtc->pipe, plane = crtc->plane;
7972 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007973 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007974 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007975 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007976
Damien Lespiau42a7b082015-02-05 19:35:13 +00007977 val = I915_READ(DSPCNTR(plane));
7978 if (!(val & DISPLAY_PLANE_ENABLE))
7979 return;
7980
Damien Lespiaud9806c92015-01-21 14:07:19 +00007981 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007982 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007983 DRM_DEBUG_KMS("failed to alloc fb\n");
7984 return;
7985 }
7986
Damien Lespiau1b842c82015-01-21 13:50:54 +00007987 fb = &intel_fb->base;
7988
Daniel Vetter18c52472015-02-10 17:16:09 +00007989 if (INTEL_INFO(dev)->gen >= 4) {
7990 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007991 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007992 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7993 }
7994 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007995
7996 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007997 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007998 fb->pixel_format = fourcc;
7999 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008000
8001 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008002 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008003 offset = I915_READ(DSPTILEOFF(plane));
8004 else
8005 offset = I915_READ(DSPLINOFF(plane));
8006 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8007 } else {
8008 base = I915_READ(DSPADDR(plane));
8009 }
8010 plane_config->base = base;
8011
8012 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008013 fb->width = ((val >> 16) & 0xfff) + 1;
8014 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008015
8016 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008017 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008018
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008019 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008020 fb->pixel_format,
8021 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008022
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008023 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008024
Damien Lespiau2844a922015-01-20 12:51:48 +00008025 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8026 pipe_name(pipe), plane, fb->width, fb->height,
8027 fb->bits_per_pixel, base, fb->pitches[0],
8028 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008029
Damien Lespiau2d140302015-02-05 17:22:18 +00008030 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008031}
8032
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008033static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008034 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008035{
8036 struct drm_device *dev = crtc->base.dev;
8037 struct drm_i915_private *dev_priv = dev->dev_private;
8038 int pipe = pipe_config->cpu_transcoder;
8039 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8040 intel_clock_t clock;
8041 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8042 int refclk = 100000;
8043
Ville Syrjäläa5805162015-05-26 20:42:30 +03008044 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008045 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8046 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8047 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8048 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008049 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008050
8051 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8052 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8053 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8054 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8055 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8056
Imre Deakdccbea32015-06-22 23:35:51 +03008057 pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008058}
8059
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008060static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008061 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008062{
8063 struct drm_device *dev = crtc->base.dev;
8064 struct drm_i915_private *dev_priv = dev->dev_private;
8065 uint32_t tmp;
8066
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008067 if (!intel_display_power_is_enabled(dev_priv,
8068 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008069 return false;
8070
Daniel Vettere143a212013-07-04 12:01:15 +02008071 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008072 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008073
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008074 tmp = I915_READ(PIPECONF(crtc->pipe));
8075 if (!(tmp & PIPECONF_ENABLE))
8076 return false;
8077
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008078 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8079 switch (tmp & PIPECONF_BPC_MASK) {
8080 case PIPECONF_6BPC:
8081 pipe_config->pipe_bpp = 18;
8082 break;
8083 case PIPECONF_8BPC:
8084 pipe_config->pipe_bpp = 24;
8085 break;
8086 case PIPECONF_10BPC:
8087 pipe_config->pipe_bpp = 30;
8088 break;
8089 default:
8090 break;
8091 }
8092 }
8093
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008094 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8095 pipe_config->limited_color_range = true;
8096
Ville Syrjälä282740f2013-09-04 18:30:03 +03008097 if (INTEL_INFO(dev)->gen < 4)
8098 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8099
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008100 intel_get_pipe_timings(crtc, pipe_config);
8101
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008102 i9xx_get_pfit_config(crtc, pipe_config);
8103
Daniel Vetter6c49f242013-06-06 12:45:25 +02008104 if (INTEL_INFO(dev)->gen >= 4) {
8105 tmp = I915_READ(DPLL_MD(crtc->pipe));
8106 pipe_config->pixel_multiplier =
8107 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8108 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008109 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008110 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8111 tmp = I915_READ(DPLL(crtc->pipe));
8112 pipe_config->pixel_multiplier =
8113 ((tmp & SDVO_MULTIPLIER_MASK)
8114 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8115 } else {
8116 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8117 * port and will be fixed up in the encoder->get_config
8118 * function. */
8119 pipe_config->pixel_multiplier = 1;
8120 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008121 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8122 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008123 /*
8124 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8125 * on 830. Filter it out here so that we don't
8126 * report errors due to that.
8127 */
8128 if (IS_I830(dev))
8129 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8130
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008131 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8132 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008133 } else {
8134 /* Mask out read-only status bits. */
8135 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8136 DPLL_PORTC_READY_MASK |
8137 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008138 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008139
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008140 if (IS_CHERRYVIEW(dev))
8141 chv_crtc_clock_get(crtc, pipe_config);
8142 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008143 vlv_crtc_clock_get(crtc, pipe_config);
8144 else
8145 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008146
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008147 return true;
8148}
8149
Paulo Zanonidde86e22012-12-01 12:04:25 -02008150static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008151{
8152 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008153 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008154 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008155 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008156 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008157 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008158 bool has_ck505 = false;
8159 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008160
8161 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008162 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008163 switch (encoder->type) {
8164 case INTEL_OUTPUT_LVDS:
8165 has_panel = true;
8166 has_lvds = true;
8167 break;
8168 case INTEL_OUTPUT_EDP:
8169 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008170 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008171 has_cpu_edp = true;
8172 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008173 default:
8174 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008175 }
8176 }
8177
Keith Packard99eb6a02011-09-26 14:29:12 -07008178 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008179 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008180 can_ssc = has_ck505;
8181 } else {
8182 has_ck505 = false;
8183 can_ssc = true;
8184 }
8185
Imre Deak2de69052013-05-08 13:14:04 +03008186 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8187 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008188
8189 /* Ironlake: try to setup display ref clock before DPLL
8190 * enabling. This is only under driver's control after
8191 * PCH B stepping, previous chipset stepping should be
8192 * ignoring this setting.
8193 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008194 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008195
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008196 /* As we must carefully and slowly disable/enable each source in turn,
8197 * compute the final state we want first and check if we need to
8198 * make any changes at all.
8199 */
8200 final = val;
8201 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008202 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008203 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008204 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008205 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8206
8207 final &= ~DREF_SSC_SOURCE_MASK;
8208 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8209 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008210
Keith Packard199e5d72011-09-22 12:01:57 -07008211 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008212 final |= DREF_SSC_SOURCE_ENABLE;
8213
8214 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8215 final |= DREF_SSC1_ENABLE;
8216
8217 if (has_cpu_edp) {
8218 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8219 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8220 else
8221 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8222 } else
8223 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8224 } else {
8225 final |= DREF_SSC_SOURCE_DISABLE;
8226 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8227 }
8228
8229 if (final == val)
8230 return;
8231
8232 /* Always enable nonspread source */
8233 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8234
8235 if (has_ck505)
8236 val |= DREF_NONSPREAD_CK505_ENABLE;
8237 else
8238 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8239
8240 if (has_panel) {
8241 val &= ~DREF_SSC_SOURCE_MASK;
8242 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008243
Keith Packard199e5d72011-09-22 12:01:57 -07008244 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008245 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008246 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008247 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008248 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008249 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008250
8251 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008252 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008253 POSTING_READ(PCH_DREF_CONTROL);
8254 udelay(200);
8255
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008256 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008257
8258 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008259 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008260 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008261 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008262 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008263 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008264 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008265 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008266 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008267
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008268 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008269 POSTING_READ(PCH_DREF_CONTROL);
8270 udelay(200);
8271 } else {
8272 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8273
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008274 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008275
8276 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008277 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008278
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008279 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008280 POSTING_READ(PCH_DREF_CONTROL);
8281 udelay(200);
8282
8283 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008284 val &= ~DREF_SSC_SOURCE_MASK;
8285 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008286
8287 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008288 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008289
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008290 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008291 POSTING_READ(PCH_DREF_CONTROL);
8292 udelay(200);
8293 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008294
8295 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008296}
8297
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008298static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008299{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008300 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008301
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008302 tmp = I915_READ(SOUTH_CHICKEN2);
8303 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8304 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008305
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008306 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8307 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8308 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008309
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008310 tmp = I915_READ(SOUTH_CHICKEN2);
8311 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8312 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008313
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008314 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8315 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8316 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008317}
8318
8319/* WaMPhyProgramming:hsw */
8320static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8321{
8322 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008323
8324 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8325 tmp &= ~(0xFF << 24);
8326 tmp |= (0x12 << 24);
8327 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8328
Paulo Zanonidde86e22012-12-01 12:04:25 -02008329 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8330 tmp |= (1 << 11);
8331 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8332
8333 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8334 tmp |= (1 << 11);
8335 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8336
Paulo Zanonidde86e22012-12-01 12:04:25 -02008337 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8338 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8339 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8340
8341 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8342 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8343 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8344
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008345 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8346 tmp &= ~(7 << 13);
8347 tmp |= (5 << 13);
8348 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008349
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008350 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8351 tmp &= ~(7 << 13);
8352 tmp |= (5 << 13);
8353 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008354
8355 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8356 tmp &= ~0xFF;
8357 tmp |= 0x1C;
8358 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8359
8360 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8361 tmp &= ~0xFF;
8362 tmp |= 0x1C;
8363 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8364
8365 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8366 tmp &= ~(0xFF << 16);
8367 tmp |= (0x1C << 16);
8368 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8369
8370 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8371 tmp &= ~(0xFF << 16);
8372 tmp |= (0x1C << 16);
8373 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8374
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008375 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8376 tmp |= (1 << 27);
8377 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008378
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008379 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8380 tmp |= (1 << 27);
8381 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008382
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008383 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8384 tmp &= ~(0xF << 28);
8385 tmp |= (4 << 28);
8386 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008387
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008388 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8389 tmp &= ~(0xF << 28);
8390 tmp |= (4 << 28);
8391 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008392}
8393
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008394/* Implements 3 different sequences from BSpec chapter "Display iCLK
8395 * Programming" based on the parameters passed:
8396 * - Sequence to enable CLKOUT_DP
8397 * - Sequence to enable CLKOUT_DP without spread
8398 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8399 */
8400static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8401 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008402{
8403 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008404 uint32_t reg, tmp;
8405
8406 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8407 with_spread = true;
8408 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8409 with_fdi, "LP PCH doesn't have FDI\n"))
8410 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008411
Ville Syrjäläa5805162015-05-26 20:42:30 +03008412 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008413
8414 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8415 tmp &= ~SBI_SSCCTL_DISABLE;
8416 tmp |= SBI_SSCCTL_PATHALT;
8417 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8418
8419 udelay(24);
8420
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008421 if (with_spread) {
8422 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8423 tmp &= ~SBI_SSCCTL_PATHALT;
8424 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008425
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008426 if (with_fdi) {
8427 lpt_reset_fdi_mphy(dev_priv);
8428 lpt_program_fdi_mphy(dev_priv);
8429 }
8430 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008431
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008432 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8433 SBI_GEN0 : SBI_DBUFF0;
8434 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8435 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8436 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008437
Ville Syrjäläa5805162015-05-26 20:42:30 +03008438 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008439}
8440
Paulo Zanoni47701c32013-07-23 11:19:25 -03008441/* Sequence to disable CLKOUT_DP */
8442static void lpt_disable_clkout_dp(struct drm_device *dev)
8443{
8444 struct drm_i915_private *dev_priv = dev->dev_private;
8445 uint32_t reg, tmp;
8446
Ville Syrjäläa5805162015-05-26 20:42:30 +03008447 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008448
8449 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8450 SBI_GEN0 : SBI_DBUFF0;
8451 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8452 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8453 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8454
8455 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8456 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8457 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8458 tmp |= SBI_SSCCTL_PATHALT;
8459 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8460 udelay(32);
8461 }
8462 tmp |= SBI_SSCCTL_DISABLE;
8463 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8464 }
8465
Ville Syrjäläa5805162015-05-26 20:42:30 +03008466 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008467}
8468
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008469static void lpt_init_pch_refclk(struct drm_device *dev)
8470{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008471 struct intel_encoder *encoder;
8472 bool has_vga = false;
8473
Damien Lespiaub2784e12014-08-05 11:29:37 +01008474 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008475 switch (encoder->type) {
8476 case INTEL_OUTPUT_ANALOG:
8477 has_vga = true;
8478 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008479 default:
8480 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008481 }
8482 }
8483
Paulo Zanoni47701c32013-07-23 11:19:25 -03008484 if (has_vga)
8485 lpt_enable_clkout_dp(dev, true, true);
8486 else
8487 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008488}
8489
Paulo Zanonidde86e22012-12-01 12:04:25 -02008490/*
8491 * Initialize reference clocks when the driver loads
8492 */
8493void intel_init_pch_refclk(struct drm_device *dev)
8494{
8495 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8496 ironlake_init_pch_refclk(dev);
8497 else if (HAS_PCH_LPT(dev))
8498 lpt_init_pch_refclk(dev);
8499}
8500
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008501static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008502{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008503 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008504 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008505 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008506 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008507 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008508 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008509 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008510 bool is_lvds = false;
8511
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008512 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008513 if (connector_state->crtc != crtc_state->base.crtc)
8514 continue;
8515
8516 encoder = to_intel_encoder(connector_state->best_encoder);
8517
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008518 switch (encoder->type) {
8519 case INTEL_OUTPUT_LVDS:
8520 is_lvds = true;
8521 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008522 default:
8523 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008524 }
8525 num_connectors++;
8526 }
8527
8528 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008529 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008530 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008531 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008532 }
8533
8534 return 120000;
8535}
8536
Daniel Vetter6ff93602013-04-19 11:24:36 +02008537static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008538{
8539 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8540 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8541 int pipe = intel_crtc->pipe;
8542 uint32_t val;
8543
Daniel Vetter78114072013-06-13 00:54:57 +02008544 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008545
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008546 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008547 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008548 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008549 break;
8550 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008551 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008552 break;
8553 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008554 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008555 break;
8556 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008557 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008558 break;
8559 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008560 /* Case prevented by intel_choose_pipe_bpp_dither. */
8561 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008562 }
8563
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008564 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008565 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8566
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008567 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008568 val |= PIPECONF_INTERLACED_ILK;
8569 else
8570 val |= PIPECONF_PROGRESSIVE;
8571
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008572 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008573 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008574
Paulo Zanonic8203562012-09-12 10:06:29 -03008575 I915_WRITE(PIPECONF(pipe), val);
8576 POSTING_READ(PIPECONF(pipe));
8577}
8578
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008579/*
8580 * Set up the pipe CSC unit.
8581 *
8582 * Currently only full range RGB to limited range RGB conversion
8583 * is supported, but eventually this should handle various
8584 * RGB<->YCbCr scenarios as well.
8585 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008586static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008587{
8588 struct drm_device *dev = crtc->dev;
8589 struct drm_i915_private *dev_priv = dev->dev_private;
8590 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8591 int pipe = intel_crtc->pipe;
8592 uint16_t coeff = 0x7800; /* 1.0 */
8593
8594 /*
8595 * TODO: Check what kind of values actually come out of the pipe
8596 * with these coeff/postoff values and adjust to get the best
8597 * accuracy. Perhaps we even need to take the bpc value into
8598 * consideration.
8599 */
8600
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008601 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008602 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8603
8604 /*
8605 * GY/GU and RY/RU should be the other way around according
8606 * to BSpec, but reality doesn't agree. Just set them up in
8607 * a way that results in the correct picture.
8608 */
8609 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8610 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8611
8612 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8613 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8614
8615 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8616 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8617
8618 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8619 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8620 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8621
8622 if (INTEL_INFO(dev)->gen > 6) {
8623 uint16_t postoff = 0;
8624
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008625 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008626 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008627
8628 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8629 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8630 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8631
8632 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8633 } else {
8634 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8635
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008636 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008637 mode |= CSC_BLACK_SCREEN_OFFSET;
8638
8639 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8640 }
8641}
8642
Daniel Vetter6ff93602013-04-19 11:24:36 +02008643static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008644{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008645 struct drm_device *dev = crtc->dev;
8646 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008648 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008649 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008650 uint32_t val;
8651
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008652 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008653
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008654 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008655 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8656
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008657 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008658 val |= PIPECONF_INTERLACED_ILK;
8659 else
8660 val |= PIPECONF_PROGRESSIVE;
8661
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008662 I915_WRITE(PIPECONF(cpu_transcoder), val);
8663 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008664
8665 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8666 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008667
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308668 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008669 val = 0;
8670
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008671 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008672 case 18:
8673 val |= PIPEMISC_DITHER_6_BPC;
8674 break;
8675 case 24:
8676 val |= PIPEMISC_DITHER_8_BPC;
8677 break;
8678 case 30:
8679 val |= PIPEMISC_DITHER_10_BPC;
8680 break;
8681 case 36:
8682 val |= PIPEMISC_DITHER_12_BPC;
8683 break;
8684 default:
8685 /* Case prevented by pipe_config_set_bpp. */
8686 BUG();
8687 }
8688
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008689 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008690 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8691
8692 I915_WRITE(PIPEMISC(pipe), val);
8693 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008694}
8695
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008696static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008697 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008698 intel_clock_t *clock,
8699 bool *has_reduced_clock,
8700 intel_clock_t *reduced_clock)
8701{
8702 struct drm_device *dev = crtc->dev;
8703 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008704 int refclk;
8705 const intel_limit_t *limit;
Daniel Vetterc329a4e2015-06-18 10:30:23 +02008706 bool ret;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008707
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008708 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008709
8710 /*
8711 * Returns a set of divisors for the desired target clock with the given
8712 * refclk, or FALSE. The returned values represent the clock equation:
8713 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8714 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008715 limit = intel_limit(crtc_state, refclk);
8716 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008717 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008718 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008719 if (!ret)
8720 return false;
8721
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008722 return true;
8723}
8724
Paulo Zanonid4b19312012-11-29 11:29:32 -02008725int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8726{
8727 /*
8728 * Account for spread spectrum to avoid
8729 * oversubscribing the link. Max center spread
8730 * is 2.5%; use 5% for safety's sake.
8731 */
8732 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008733 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008734}
8735
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008736static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008737{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008738 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008739}
8740
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008741static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008742 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008743 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008744 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008745{
8746 struct drm_crtc *crtc = &intel_crtc->base;
8747 struct drm_device *dev = crtc->dev;
8748 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008749 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008750 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008751 struct drm_connector_state *connector_state;
8752 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008753 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008754 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008755 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008756
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008757 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008758 if (connector_state->crtc != crtc_state->base.crtc)
8759 continue;
8760
8761 encoder = to_intel_encoder(connector_state->best_encoder);
8762
8763 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008764 case INTEL_OUTPUT_LVDS:
8765 is_lvds = true;
8766 break;
8767 case INTEL_OUTPUT_SDVO:
8768 case INTEL_OUTPUT_HDMI:
8769 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008770 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008771 default:
8772 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008773 }
8774
8775 num_connectors++;
8776 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008777
Chris Wilsonc1858122010-12-03 21:35:48 +00008778 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008779 factor = 21;
8780 if (is_lvds) {
8781 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008782 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008783 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008784 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008785 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008786 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008787
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008788 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008789 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008790
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008791 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8792 *fp2 |= FP_CB_TUNE;
8793
Chris Wilson5eddb702010-09-11 13:48:45 +01008794 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008795
Eric Anholta07d6782011-03-30 13:01:08 -07008796 if (is_lvds)
8797 dpll |= DPLLB_MODE_LVDS;
8798 else
8799 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008800
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008801 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008802 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008803
8804 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008805 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008806 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008807 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008808
Eric Anholta07d6782011-03-30 13:01:08 -07008809 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008810 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008811 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008812 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008813
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008814 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008815 case 5:
8816 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8817 break;
8818 case 7:
8819 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8820 break;
8821 case 10:
8822 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8823 break;
8824 case 14:
8825 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8826 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008827 }
8828
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008829 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008830 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008831 else
8832 dpll |= PLL_REF_INPUT_DREFCLK;
8833
Daniel Vetter959e16d2013-06-05 13:34:21 +02008834 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008835}
8836
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008837static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8838 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008839{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008840 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008841 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008842 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008843 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008844 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008845 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008846
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008847 memset(&crtc_state->dpll_hw_state, 0,
8848 sizeof(crtc_state->dpll_hw_state));
8849
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008850 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008851
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008852 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8853 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8854
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008855 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008856 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008857 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008858 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8859 return -EINVAL;
8860 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008861 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008862 if (!crtc_state->clock_set) {
8863 crtc_state->dpll.n = clock.n;
8864 crtc_state->dpll.m1 = clock.m1;
8865 crtc_state->dpll.m2 = clock.m2;
8866 crtc_state->dpll.p1 = clock.p1;
8867 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008868 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008869
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008870 /* 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 +02008871 if (crtc_state->has_pch_encoder) {
8872 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008873 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008874 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008875
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008876 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008877 &fp, &reduced_clock,
8878 has_reduced_clock ? &fp2 : NULL);
8879
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008880 crtc_state->dpll_hw_state.dpll = dpll;
8881 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008882 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008883 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008884 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008885 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008886
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008887 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008888 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008889 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008890 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008891 return -EINVAL;
8892 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008893 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008894
Rodrigo Viviab585de2015-03-24 12:40:09 -07008895 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008896 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008897 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008898 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008899
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008900 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008901}
8902
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008903static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8904 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008905{
8906 struct drm_device *dev = crtc->base.dev;
8907 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008908 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008909
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008910 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8911 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8912 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8913 & ~TU_SIZE_MASK;
8914 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8915 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8916 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8917}
8918
8919static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8920 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008921 struct intel_link_m_n *m_n,
8922 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008923{
8924 struct drm_device *dev = crtc->base.dev;
8925 struct drm_i915_private *dev_priv = dev->dev_private;
8926 enum pipe pipe = crtc->pipe;
8927
8928 if (INTEL_INFO(dev)->gen >= 5) {
8929 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8930 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8931 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8932 & ~TU_SIZE_MASK;
8933 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8934 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8935 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008936 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8937 * gen < 8) and if DRRS is supported (to make sure the
8938 * registers are not unnecessarily read).
8939 */
8940 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008941 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008942 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8943 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8944 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8945 & ~TU_SIZE_MASK;
8946 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8947 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8948 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8949 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008950 } else {
8951 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8952 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8953 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8954 & ~TU_SIZE_MASK;
8955 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8956 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8957 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8958 }
8959}
8960
8961void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008962 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008963{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008964 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008965 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8966 else
8967 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008968 &pipe_config->dp_m_n,
8969 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008970}
8971
Daniel Vetter72419202013-04-04 13:28:53 +02008972static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008973 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008974{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008975 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008976 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008977}
8978
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008979static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008980 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008981{
8982 struct drm_device *dev = crtc->base.dev;
8983 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008984 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8985 uint32_t ps_ctrl = 0;
8986 int id = -1;
8987 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008988
Chandra Kondurua1b22782015-04-07 15:28:45 -07008989 /* find scaler attached to this pipe */
8990 for (i = 0; i < crtc->num_scalers; i++) {
8991 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8992 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8993 id = i;
8994 pipe_config->pch_pfit.enabled = true;
8995 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8996 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8997 break;
8998 }
8999 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009000
Chandra Kondurua1b22782015-04-07 15:28:45 -07009001 scaler_state->scaler_id = id;
9002 if (id >= 0) {
9003 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9004 } else {
9005 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009006 }
9007}
9008
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009009static void
9010skylake_get_initial_plane_config(struct intel_crtc *crtc,
9011 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009012{
9013 struct drm_device *dev = crtc->base.dev;
9014 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009015 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009016 int pipe = crtc->pipe;
9017 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009018 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009019 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009020 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009021
Damien Lespiaud9806c92015-01-21 14:07:19 +00009022 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009023 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009024 DRM_DEBUG_KMS("failed to alloc fb\n");
9025 return;
9026 }
9027
Damien Lespiau1b842c82015-01-21 13:50:54 +00009028 fb = &intel_fb->base;
9029
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009030 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009031 if (!(val & PLANE_CTL_ENABLE))
9032 goto error;
9033
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009034 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9035 fourcc = skl_format_to_fourcc(pixel_format,
9036 val & PLANE_CTL_ORDER_RGBX,
9037 val & PLANE_CTL_ALPHA_MASK);
9038 fb->pixel_format = fourcc;
9039 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9040
Damien Lespiau40f46282015-02-27 11:15:21 +00009041 tiling = val & PLANE_CTL_TILED_MASK;
9042 switch (tiling) {
9043 case PLANE_CTL_TILED_LINEAR:
9044 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9045 break;
9046 case PLANE_CTL_TILED_X:
9047 plane_config->tiling = I915_TILING_X;
9048 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9049 break;
9050 case PLANE_CTL_TILED_Y:
9051 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9052 break;
9053 case PLANE_CTL_TILED_YF:
9054 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9055 break;
9056 default:
9057 MISSING_CASE(tiling);
9058 goto error;
9059 }
9060
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009061 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9062 plane_config->base = base;
9063
9064 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9065
9066 val = I915_READ(PLANE_SIZE(pipe, 0));
9067 fb->height = ((val >> 16) & 0xfff) + 1;
9068 fb->width = ((val >> 0) & 0x1fff) + 1;
9069
9070 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009071 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9072 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009073 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9074
9075 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009076 fb->pixel_format,
9077 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009078
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009079 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009080
9081 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9082 pipe_name(pipe), fb->width, fb->height,
9083 fb->bits_per_pixel, base, fb->pitches[0],
9084 plane_config->size);
9085
Damien Lespiau2d140302015-02-05 17:22:18 +00009086 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009087 return;
9088
9089error:
9090 kfree(fb);
9091}
9092
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009093static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009094 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009095{
9096 struct drm_device *dev = crtc->base.dev;
9097 struct drm_i915_private *dev_priv = dev->dev_private;
9098 uint32_t tmp;
9099
9100 tmp = I915_READ(PF_CTL(crtc->pipe));
9101
9102 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009103 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009104 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9105 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009106
9107 /* We currently do not free assignements of panel fitters on
9108 * ivb/hsw (since we don't use the higher upscaling modes which
9109 * differentiates them) so just WARN about this case for now. */
9110 if (IS_GEN7(dev)) {
9111 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9112 PF_PIPE_SEL_IVB(crtc->pipe));
9113 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009114 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009115}
9116
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009117static void
9118ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9119 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009120{
9121 struct drm_device *dev = crtc->base.dev;
9122 struct drm_i915_private *dev_priv = dev->dev_private;
9123 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009124 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009125 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009126 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009127 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009128 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009129
Damien Lespiau42a7b082015-02-05 19:35:13 +00009130 val = I915_READ(DSPCNTR(pipe));
9131 if (!(val & DISPLAY_PLANE_ENABLE))
9132 return;
9133
Damien Lespiaud9806c92015-01-21 14:07:19 +00009134 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009135 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009136 DRM_DEBUG_KMS("failed to alloc fb\n");
9137 return;
9138 }
9139
Damien Lespiau1b842c82015-01-21 13:50:54 +00009140 fb = &intel_fb->base;
9141
Daniel Vetter18c52472015-02-10 17:16:09 +00009142 if (INTEL_INFO(dev)->gen >= 4) {
9143 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009144 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009145 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9146 }
9147 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009148
9149 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009150 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009151 fb->pixel_format = fourcc;
9152 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009153
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009154 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009155 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009156 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009157 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009158 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009159 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009160 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009161 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009162 }
9163 plane_config->base = base;
9164
9165 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009166 fb->width = ((val >> 16) & 0xfff) + 1;
9167 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009168
9169 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009170 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009171
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009172 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009173 fb->pixel_format,
9174 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009175
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009176 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009177
Damien Lespiau2844a922015-01-20 12:51:48 +00009178 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9179 pipe_name(pipe), fb->width, fb->height,
9180 fb->bits_per_pixel, base, fb->pitches[0],
9181 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009182
Damien Lespiau2d140302015-02-05 17:22:18 +00009183 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009184}
9185
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009186static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009187 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009188{
9189 struct drm_device *dev = crtc->base.dev;
9190 struct drm_i915_private *dev_priv = dev->dev_private;
9191 uint32_t tmp;
9192
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009193 if (!intel_display_power_is_enabled(dev_priv,
9194 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009195 return false;
9196
Daniel Vettere143a212013-07-04 12:01:15 +02009197 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009198 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009199
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009200 tmp = I915_READ(PIPECONF(crtc->pipe));
9201 if (!(tmp & PIPECONF_ENABLE))
9202 return false;
9203
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009204 switch (tmp & PIPECONF_BPC_MASK) {
9205 case PIPECONF_6BPC:
9206 pipe_config->pipe_bpp = 18;
9207 break;
9208 case PIPECONF_8BPC:
9209 pipe_config->pipe_bpp = 24;
9210 break;
9211 case PIPECONF_10BPC:
9212 pipe_config->pipe_bpp = 30;
9213 break;
9214 case PIPECONF_12BPC:
9215 pipe_config->pipe_bpp = 36;
9216 break;
9217 default:
9218 break;
9219 }
9220
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009221 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9222 pipe_config->limited_color_range = true;
9223
Daniel Vetterab9412b2013-05-03 11:49:46 +02009224 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009225 struct intel_shared_dpll *pll;
9226
Daniel Vetter88adfff2013-03-28 10:42:01 +01009227 pipe_config->has_pch_encoder = true;
9228
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009229 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9230 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9231 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009232
9233 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009234
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009235 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009236 pipe_config->shared_dpll =
9237 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009238 } else {
9239 tmp = I915_READ(PCH_DPLL_SEL);
9240 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9241 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9242 else
9243 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9244 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009245
9246 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9247
9248 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9249 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009250
9251 tmp = pipe_config->dpll_hw_state.dpll;
9252 pipe_config->pixel_multiplier =
9253 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9254 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009255
9256 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009257 } else {
9258 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009259 }
9260
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009261 intel_get_pipe_timings(crtc, pipe_config);
9262
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009263 ironlake_get_pfit_config(crtc, pipe_config);
9264
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009265 return true;
9266}
9267
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009268static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9269{
9270 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009271 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009272
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009273 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009274 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009275 pipe_name(crtc->pipe));
9276
Rob Clarke2c719b2014-12-15 13:56:32 -05009277 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9278 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9279 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9280 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9281 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9282 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009283 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009284 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009285 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009286 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009287 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009288 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009289 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009290 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009291 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009292
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009293 /*
9294 * In theory we can still leave IRQs enabled, as long as only the HPD
9295 * interrupts remain enabled. We used to check for that, but since it's
9296 * gen-specific and since we only disable LCPLL after we fully disable
9297 * the interrupts, the check below should be enough.
9298 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009299 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009300}
9301
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009302static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9303{
9304 struct drm_device *dev = dev_priv->dev;
9305
9306 if (IS_HASWELL(dev))
9307 return I915_READ(D_COMP_HSW);
9308 else
9309 return I915_READ(D_COMP_BDW);
9310}
9311
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009312static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9313{
9314 struct drm_device *dev = dev_priv->dev;
9315
9316 if (IS_HASWELL(dev)) {
9317 mutex_lock(&dev_priv->rps.hw_lock);
9318 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9319 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009320 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009321 mutex_unlock(&dev_priv->rps.hw_lock);
9322 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009323 I915_WRITE(D_COMP_BDW, val);
9324 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009325 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009326}
9327
9328/*
9329 * This function implements pieces of two sequences from BSpec:
9330 * - Sequence for display software to disable LCPLL
9331 * - Sequence for display software to allow package C8+
9332 * The steps implemented here are just the steps that actually touch the LCPLL
9333 * register. Callers should take care of disabling all the display engine
9334 * functions, doing the mode unset, fixing interrupts, etc.
9335 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009336static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9337 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009338{
9339 uint32_t val;
9340
9341 assert_can_disable_lcpll(dev_priv);
9342
9343 val = I915_READ(LCPLL_CTL);
9344
9345 if (switch_to_fclk) {
9346 val |= LCPLL_CD_SOURCE_FCLK;
9347 I915_WRITE(LCPLL_CTL, val);
9348
9349 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9350 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9351 DRM_ERROR("Switching to FCLK failed\n");
9352
9353 val = I915_READ(LCPLL_CTL);
9354 }
9355
9356 val |= LCPLL_PLL_DISABLE;
9357 I915_WRITE(LCPLL_CTL, val);
9358 POSTING_READ(LCPLL_CTL);
9359
9360 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9361 DRM_ERROR("LCPLL still locked\n");
9362
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009363 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009364 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009365 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009366 ndelay(100);
9367
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009368 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9369 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009370 DRM_ERROR("D_COMP RCOMP still in progress\n");
9371
9372 if (allow_power_down) {
9373 val = I915_READ(LCPLL_CTL);
9374 val |= LCPLL_POWER_DOWN_ALLOW;
9375 I915_WRITE(LCPLL_CTL, val);
9376 POSTING_READ(LCPLL_CTL);
9377 }
9378}
9379
9380/*
9381 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9382 * source.
9383 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009384static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009385{
9386 uint32_t val;
9387
9388 val = I915_READ(LCPLL_CTL);
9389
9390 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9391 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9392 return;
9393
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009394 /*
9395 * Make sure we're not on PC8 state before disabling PC8, otherwise
9396 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009397 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009398 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009399
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009400 if (val & LCPLL_POWER_DOWN_ALLOW) {
9401 val &= ~LCPLL_POWER_DOWN_ALLOW;
9402 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009403 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404 }
9405
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009406 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009407 val |= D_COMP_COMP_FORCE;
9408 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009409 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009410
9411 val = I915_READ(LCPLL_CTL);
9412 val &= ~LCPLL_PLL_DISABLE;
9413 I915_WRITE(LCPLL_CTL, val);
9414
9415 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9416 DRM_ERROR("LCPLL not locked yet\n");
9417
9418 if (val & LCPLL_CD_SOURCE_FCLK) {
9419 val = I915_READ(LCPLL_CTL);
9420 val &= ~LCPLL_CD_SOURCE_FCLK;
9421 I915_WRITE(LCPLL_CTL, val);
9422
9423 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9424 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9425 DRM_ERROR("Switching back to LCPLL failed\n");
9426 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009427
Mika Kuoppala59bad942015-01-16 11:34:40 +02009428 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009429 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009430}
9431
Paulo Zanoni765dab672014-03-07 20:08:18 -03009432/*
9433 * Package states C8 and deeper are really deep PC states that can only be
9434 * reached when all the devices on the system allow it, so even if the graphics
9435 * device allows PC8+, it doesn't mean the system will actually get to these
9436 * states. Our driver only allows PC8+ when going into runtime PM.
9437 *
9438 * The requirements for PC8+ are that all the outputs are disabled, the power
9439 * well is disabled and most interrupts are disabled, and these are also
9440 * requirements for runtime PM. When these conditions are met, we manually do
9441 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9442 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9443 * hang the machine.
9444 *
9445 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9446 * the state of some registers, so when we come back from PC8+ we need to
9447 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9448 * need to take care of the registers kept by RC6. Notice that this happens even
9449 * if we don't put the device in PCI D3 state (which is what currently happens
9450 * because of the runtime PM support).
9451 *
9452 * For more, read "Display Sequences for Package C8" on the hardware
9453 * documentation.
9454 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009455void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009456{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009457 struct drm_device *dev = dev_priv->dev;
9458 uint32_t val;
9459
Paulo Zanonic67a4702013-08-19 13:18:09 -03009460 DRM_DEBUG_KMS("Enabling package C8+\n");
9461
Paulo Zanonic67a4702013-08-19 13:18:09 -03009462 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9463 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9464 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9465 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9466 }
9467
9468 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009469 hsw_disable_lcpll(dev_priv, true, true);
9470}
9471
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009472void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009473{
9474 struct drm_device *dev = dev_priv->dev;
9475 uint32_t val;
9476
Paulo Zanonic67a4702013-08-19 13:18:09 -03009477 DRM_DEBUG_KMS("Disabling package C8+\n");
9478
9479 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009480 lpt_init_pch_refclk(dev);
9481
9482 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9483 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9484 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9485 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9486 }
9487
9488 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009489}
9490
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009491static void broxton_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309492{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009493 struct drm_device *dev = old_state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009494 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309495
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009496 broxton_set_cdclk(dev, req_cdclk);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309497}
9498
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009499/* compute the max rate for new configuration */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009500static int ilk_max_pixel_rate(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009501{
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009502 struct intel_crtc *intel_crtc;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009503 struct intel_crtc_state *crtc_state;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009504 int max_pixel_rate = 0;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009505
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009506 for_each_intel_crtc(state->dev, intel_crtc) {
9507 int pixel_rate;
9508
9509 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
9510 if (IS_ERR(crtc_state))
9511 return PTR_ERR(crtc_state);
9512
9513 if (!crtc_state->base.enable)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009514 continue;
9515
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009516 pixel_rate = ilk_pipe_pixel_rate(crtc_state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009517
9518 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009519 if (IS_BROADWELL(state->dev) && crtc_state->ips_enabled)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009520 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9521
9522 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9523 }
9524
9525 return max_pixel_rate;
9526}
9527
9528static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9529{
9530 struct drm_i915_private *dev_priv = dev->dev_private;
9531 uint32_t val, data;
9532 int ret;
9533
9534 if (WARN((I915_READ(LCPLL_CTL) &
9535 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9536 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9537 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9538 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9539 "trying to change cdclk frequency with cdclk not enabled\n"))
9540 return;
9541
9542 mutex_lock(&dev_priv->rps.hw_lock);
9543 ret = sandybridge_pcode_write(dev_priv,
9544 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9545 mutex_unlock(&dev_priv->rps.hw_lock);
9546 if (ret) {
9547 DRM_ERROR("failed to inform pcode about cdclk change\n");
9548 return;
9549 }
9550
9551 val = I915_READ(LCPLL_CTL);
9552 val |= LCPLL_CD_SOURCE_FCLK;
9553 I915_WRITE(LCPLL_CTL, val);
9554
9555 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9556 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9557 DRM_ERROR("Switching to FCLK failed\n");
9558
9559 val = I915_READ(LCPLL_CTL);
9560 val &= ~LCPLL_CLK_FREQ_MASK;
9561
9562 switch (cdclk) {
9563 case 450000:
9564 val |= LCPLL_CLK_FREQ_450;
9565 data = 0;
9566 break;
9567 case 540000:
9568 val |= LCPLL_CLK_FREQ_54O_BDW;
9569 data = 1;
9570 break;
9571 case 337500:
9572 val |= LCPLL_CLK_FREQ_337_5_BDW;
9573 data = 2;
9574 break;
9575 case 675000:
9576 val |= LCPLL_CLK_FREQ_675_BDW;
9577 data = 3;
9578 break;
9579 default:
9580 WARN(1, "invalid cdclk frequency\n");
9581 return;
9582 }
9583
9584 I915_WRITE(LCPLL_CTL, val);
9585
9586 val = I915_READ(LCPLL_CTL);
9587 val &= ~LCPLL_CD_SOURCE_FCLK;
9588 I915_WRITE(LCPLL_CTL, val);
9589
9590 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9591 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9592 DRM_ERROR("Switching back to LCPLL failed\n");
9593
9594 mutex_lock(&dev_priv->rps.hw_lock);
9595 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9596 mutex_unlock(&dev_priv->rps.hw_lock);
9597
9598 intel_update_cdclk(dev);
9599
9600 WARN(cdclk != dev_priv->cdclk_freq,
9601 "cdclk requested %d kHz but got %d kHz\n",
9602 cdclk, dev_priv->cdclk_freq);
9603}
9604
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009605static int broadwell_modeset_calc_cdclk(struct drm_atomic_state *state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009606{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009607 struct drm_i915_private *dev_priv = to_i915(state->dev);
9608 int max_pixclk = ilk_max_pixel_rate(state);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009609 int cdclk;
9610
9611 /*
9612 * FIXME should also account for plane ratio
9613 * once 64bpp pixel formats are supported.
9614 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009615 if (max_pixclk > 540000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009616 cdclk = 675000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009617 else if (max_pixclk > 450000)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009618 cdclk = 540000;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009619 else if (max_pixclk > 337500)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009620 cdclk = 450000;
9621 else
9622 cdclk = 337500;
9623
9624 /*
9625 * FIXME move the cdclk caclulation to
9626 * compute_config() so we can fail gracegully.
9627 */
9628 if (cdclk > dev_priv->max_cdclk_freq) {
9629 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9630 cdclk, dev_priv->max_cdclk_freq);
9631 cdclk = dev_priv->max_cdclk_freq;
9632 }
9633
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009634 to_intel_atomic_state(state)->cdclk = cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009635
9636 return 0;
9637}
9638
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009639static void broadwell_modeset_commit_cdclk(struct drm_atomic_state *old_state)
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009640{
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009641 struct drm_device *dev = old_state->dev;
9642 unsigned int req_cdclk = to_intel_atomic_state(old_state)->cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009643
Maarten Lankhorst27c329e2015-06-15 12:33:56 +02009644 broadwell_set_cdclk(dev, req_cdclk);
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009645}
9646
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009647static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9648 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009649{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009650 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009651 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009652
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009653 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009654
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009655 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009656}
9657
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309658static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9659 enum port port,
9660 struct intel_crtc_state *pipe_config)
9661{
9662 switch (port) {
9663 case PORT_A:
9664 pipe_config->ddi_pll_sel = SKL_DPLL0;
9665 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9666 break;
9667 case PORT_B:
9668 pipe_config->ddi_pll_sel = SKL_DPLL1;
9669 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9670 break;
9671 case PORT_C:
9672 pipe_config->ddi_pll_sel = SKL_DPLL2;
9673 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9674 break;
9675 default:
9676 DRM_ERROR("Incorrect port type\n");
9677 }
9678}
9679
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009680static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9681 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009682 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009683{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009684 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009685
9686 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9687 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9688
9689 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009690 case SKL_DPLL0:
9691 /*
9692 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9693 * of the shared DPLL framework and thus needs to be read out
9694 * separately
9695 */
9696 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9697 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9698 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009699 case SKL_DPLL1:
9700 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9701 break;
9702 case SKL_DPLL2:
9703 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9704 break;
9705 case SKL_DPLL3:
9706 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9707 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009708 }
9709}
9710
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009711static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9712 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009713 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009714{
9715 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9716
9717 switch (pipe_config->ddi_pll_sel) {
9718 case PORT_CLK_SEL_WRPLL1:
9719 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9720 break;
9721 case PORT_CLK_SEL_WRPLL2:
9722 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9723 break;
9724 }
9725}
9726
Daniel Vetter26804af2014-06-25 22:01:55 +03009727static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009728 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009729{
9730 struct drm_device *dev = crtc->base.dev;
9731 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009732 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009733 enum port port;
9734 uint32_t tmp;
9735
9736 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9737
9738 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9739
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009740 if (IS_SKYLAKE(dev))
9741 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309742 else if (IS_BROXTON(dev))
9743 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009744 else
9745 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009746
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009747 if (pipe_config->shared_dpll >= 0) {
9748 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9749
9750 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9751 &pipe_config->dpll_hw_state));
9752 }
9753
Daniel Vetter26804af2014-06-25 22:01:55 +03009754 /*
9755 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9756 * DDI E. So just check whether this pipe is wired to DDI E and whether
9757 * the PCH transcoder is on.
9758 */
Damien Lespiauca370452013-12-03 13:56:24 +00009759 if (INTEL_INFO(dev)->gen < 9 &&
9760 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009761 pipe_config->has_pch_encoder = true;
9762
9763 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9764 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9765 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9766
9767 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9768 }
9769}
9770
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009771static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009772 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009773{
9774 struct drm_device *dev = crtc->base.dev;
9775 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009776 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009777 uint32_t tmp;
9778
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009779 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009780 POWER_DOMAIN_PIPE(crtc->pipe)))
9781 return false;
9782
Daniel Vettere143a212013-07-04 12:01:15 +02009783 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009784 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9785
Daniel Vettereccb1402013-05-22 00:50:22 +02009786 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9787 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9788 enum pipe trans_edp_pipe;
9789 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9790 default:
9791 WARN(1, "unknown pipe linked to edp transcoder\n");
9792 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9793 case TRANS_DDI_EDP_INPUT_A_ON:
9794 trans_edp_pipe = PIPE_A;
9795 break;
9796 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9797 trans_edp_pipe = PIPE_B;
9798 break;
9799 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9800 trans_edp_pipe = PIPE_C;
9801 break;
9802 }
9803
9804 if (trans_edp_pipe == crtc->pipe)
9805 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9806 }
9807
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009808 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009809 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009810 return false;
9811
Daniel Vettereccb1402013-05-22 00:50:22 +02009812 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009813 if (!(tmp & PIPECONF_ENABLE))
9814 return false;
9815
Daniel Vetter26804af2014-06-25 22:01:55 +03009816 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009817
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009818 intel_get_pipe_timings(crtc, pipe_config);
9819
Chandra Kondurua1b22782015-04-07 15:28:45 -07009820 if (INTEL_INFO(dev)->gen >= 9) {
9821 skl_init_scalers(dev, crtc, pipe_config);
9822 }
9823
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009824 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009825
9826 if (INTEL_INFO(dev)->gen >= 9) {
9827 pipe_config->scaler_state.scaler_id = -1;
9828 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9829 }
9830
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009831 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009832 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009833 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009834 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009835 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009836 else
9837 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009838 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009839
Jesse Barnese59150d2014-01-07 13:30:45 -08009840 if (IS_HASWELL(dev))
9841 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9842 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009843
Clint Taylorebb69c92014-09-30 10:30:22 -07009844 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9845 pipe_config->pixel_multiplier =
9846 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9847 } else {
9848 pipe_config->pixel_multiplier = 1;
9849 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009850
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009851 return true;
9852}
9853
Chris Wilson560b85b2010-08-07 11:01:38 +01009854static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9855{
9856 struct drm_device *dev = crtc->dev;
9857 struct drm_i915_private *dev_priv = dev->dev_private;
9858 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009859 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009860
Ville Syrjälädc41c152014-08-13 11:57:05 +03009861 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009862 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9863 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009864 unsigned int stride = roundup_pow_of_two(width) * 4;
9865
9866 switch (stride) {
9867 default:
9868 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9869 width, stride);
9870 stride = 256;
9871 /* fallthrough */
9872 case 256:
9873 case 512:
9874 case 1024:
9875 case 2048:
9876 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009877 }
9878
Ville Syrjälädc41c152014-08-13 11:57:05 +03009879 cntl |= CURSOR_ENABLE |
9880 CURSOR_GAMMA_ENABLE |
9881 CURSOR_FORMAT_ARGB |
9882 CURSOR_STRIDE(stride);
9883
9884 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009885 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009886
Ville Syrjälädc41c152014-08-13 11:57:05 +03009887 if (intel_crtc->cursor_cntl != 0 &&
9888 (intel_crtc->cursor_base != base ||
9889 intel_crtc->cursor_size != size ||
9890 intel_crtc->cursor_cntl != cntl)) {
9891 /* On these chipsets we can only modify the base/size/stride
9892 * whilst the cursor is disabled.
9893 */
9894 I915_WRITE(_CURACNTR, 0);
9895 POSTING_READ(_CURACNTR);
9896 intel_crtc->cursor_cntl = 0;
9897 }
9898
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009899 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009900 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009901 intel_crtc->cursor_base = base;
9902 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009903
9904 if (intel_crtc->cursor_size != size) {
9905 I915_WRITE(CURSIZE, size);
9906 intel_crtc->cursor_size = size;
9907 }
9908
Chris Wilson4b0e3332014-05-30 16:35:26 +03009909 if (intel_crtc->cursor_cntl != cntl) {
9910 I915_WRITE(_CURACNTR, cntl);
9911 POSTING_READ(_CURACNTR);
9912 intel_crtc->cursor_cntl = cntl;
9913 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009914}
9915
9916static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9917{
9918 struct drm_device *dev = crtc->dev;
9919 struct drm_i915_private *dev_priv = dev->dev_private;
9920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9921 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009922 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009923
Chris Wilson4b0e3332014-05-30 16:35:26 +03009924 cntl = 0;
9925 if (base) {
9926 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009927 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309928 case 64:
9929 cntl |= CURSOR_MODE_64_ARGB_AX;
9930 break;
9931 case 128:
9932 cntl |= CURSOR_MODE_128_ARGB_AX;
9933 break;
9934 case 256:
9935 cntl |= CURSOR_MODE_256_ARGB_AX;
9936 break;
9937 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009938 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309939 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009940 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009941 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009942
9943 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9944 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009945 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009946
Matt Roper8e7d6882015-01-21 16:35:41 -08009947 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009948 cntl |= CURSOR_ROTATE_180;
9949
Chris Wilson4b0e3332014-05-30 16:35:26 +03009950 if (intel_crtc->cursor_cntl != cntl) {
9951 I915_WRITE(CURCNTR(pipe), cntl);
9952 POSTING_READ(CURCNTR(pipe));
9953 intel_crtc->cursor_cntl = cntl;
9954 }
9955
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009956 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009957 I915_WRITE(CURBASE(pipe), base);
9958 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009959
9960 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009961}
9962
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009963/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009964static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9965 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009966{
9967 struct drm_device *dev = crtc->dev;
9968 struct drm_i915_private *dev_priv = dev->dev_private;
9969 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9970 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009971 int x = crtc->cursor_x;
9972 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009973 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009974
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009975 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009976 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009977
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009978 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009979 base = 0;
9980
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009981 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009982 base = 0;
9983
9984 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009985 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009986 base = 0;
9987
9988 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9989 x = -x;
9990 }
9991 pos |= x << CURSOR_X_SHIFT;
9992
9993 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009994 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009995 base = 0;
9996
9997 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9998 y = -y;
9999 }
10000 pos |= y << CURSOR_Y_SHIFT;
10001
Chris Wilson4b0e3332014-05-30 16:35:26 +030010002 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010003 return;
10004
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010005 I915_WRITE(CURPOS(pipe), pos);
10006
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010007 /* ILK+ do this automagically */
10008 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010009 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010010 base += (intel_crtc->base.cursor->state->crtc_h *
10011 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010012 }
10013
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010014 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010015 i845_update_cursor(crtc, base);
10016 else
10017 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010018}
10019
Ville Syrjälädc41c152014-08-13 11:57:05 +030010020static bool cursor_size_ok(struct drm_device *dev,
10021 uint32_t width, uint32_t height)
10022{
10023 if (width == 0 || height == 0)
10024 return false;
10025
10026 /*
10027 * 845g/865g are special in that they are only limited by
10028 * the width of their cursors, the height is arbitrary up to
10029 * the precision of the register. Everything else requires
10030 * square cursors, limited to a few power-of-two sizes.
10031 */
10032 if (IS_845G(dev) || IS_I865G(dev)) {
10033 if ((width & 63) != 0)
10034 return false;
10035
10036 if (width > (IS_845G(dev) ? 64 : 512))
10037 return false;
10038
10039 if (height > 1023)
10040 return false;
10041 } else {
10042 switch (width | height) {
10043 case 256:
10044 case 128:
10045 if (IS_GEN2(dev))
10046 return false;
10047 case 64:
10048 break;
10049 default:
10050 return false;
10051 }
10052 }
10053
10054 return true;
10055}
10056
Jesse Barnes79e53942008-11-07 14:24:08 -080010057static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010058 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010059{
James Simmons72034252010-08-03 01:33:19 +010010060 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010062
James Simmons72034252010-08-03 01:33:19 +010010063 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010064 intel_crtc->lut_r[i] = red[i] >> 8;
10065 intel_crtc->lut_g[i] = green[i] >> 8;
10066 intel_crtc->lut_b[i] = blue[i] >> 8;
10067 }
10068
10069 intel_crtc_load_lut(crtc);
10070}
10071
Jesse Barnes79e53942008-11-07 14:24:08 -080010072/* VESA 640x480x72Hz mode to set on the pipe */
10073static struct drm_display_mode load_detect_mode = {
10074 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10075 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10076};
10077
Daniel Vettera8bb6812014-02-10 18:00:39 +010010078struct drm_framebuffer *
10079__intel_framebuffer_create(struct drm_device *dev,
10080 struct drm_mode_fb_cmd2 *mode_cmd,
10081 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010082{
10083 struct intel_framebuffer *intel_fb;
10084 int ret;
10085
10086 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10087 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010088 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010089 return ERR_PTR(-ENOMEM);
10090 }
10091
10092 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010093 if (ret)
10094 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010095
10096 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010097err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010098 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010099 kfree(intel_fb);
10100
10101 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010102}
10103
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010104static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010105intel_framebuffer_create(struct drm_device *dev,
10106 struct drm_mode_fb_cmd2 *mode_cmd,
10107 struct drm_i915_gem_object *obj)
10108{
10109 struct drm_framebuffer *fb;
10110 int ret;
10111
10112 ret = i915_mutex_lock_interruptible(dev);
10113 if (ret)
10114 return ERR_PTR(ret);
10115 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10116 mutex_unlock(&dev->struct_mutex);
10117
10118 return fb;
10119}
10120
Chris Wilsond2dff872011-04-19 08:36:26 +010010121static u32
10122intel_framebuffer_pitch_for_width(int width, int bpp)
10123{
10124 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10125 return ALIGN(pitch, 64);
10126}
10127
10128static u32
10129intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10130{
10131 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010132 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010133}
10134
10135static struct drm_framebuffer *
10136intel_framebuffer_create_for_mode(struct drm_device *dev,
10137 struct drm_display_mode *mode,
10138 int depth, int bpp)
10139{
10140 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010141 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010142
10143 obj = i915_gem_alloc_object(dev,
10144 intel_framebuffer_size_for_mode(mode, bpp));
10145 if (obj == NULL)
10146 return ERR_PTR(-ENOMEM);
10147
10148 mode_cmd.width = mode->hdisplay;
10149 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010150 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10151 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010152 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010153
10154 return intel_framebuffer_create(dev, &mode_cmd, obj);
10155}
10156
10157static struct drm_framebuffer *
10158mode_fits_in_fbdev(struct drm_device *dev,
10159 struct drm_display_mode *mode)
10160{
Daniel Vetter4520f532013-10-09 09:18:51 +020010161#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010162 struct drm_i915_private *dev_priv = dev->dev_private;
10163 struct drm_i915_gem_object *obj;
10164 struct drm_framebuffer *fb;
10165
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010166 if (!dev_priv->fbdev)
10167 return NULL;
10168
10169 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010170 return NULL;
10171
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010172 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010173 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010174
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010175 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010176 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10177 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010178 return NULL;
10179
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010180 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010181 return NULL;
10182
10183 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010184#else
10185 return NULL;
10186#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010187}
10188
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010189static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10190 struct drm_crtc *crtc,
10191 struct drm_display_mode *mode,
10192 struct drm_framebuffer *fb,
10193 int x, int y)
10194{
10195 struct drm_plane_state *plane_state;
10196 int hdisplay, vdisplay;
10197 int ret;
10198
10199 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10200 if (IS_ERR(plane_state))
10201 return PTR_ERR(plane_state);
10202
10203 if (mode)
10204 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10205 else
10206 hdisplay = vdisplay = 0;
10207
10208 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10209 if (ret)
10210 return ret;
10211 drm_atomic_set_fb_for_plane(plane_state, fb);
10212 plane_state->crtc_x = 0;
10213 plane_state->crtc_y = 0;
10214 plane_state->crtc_w = hdisplay;
10215 plane_state->crtc_h = vdisplay;
10216 plane_state->src_x = x << 16;
10217 plane_state->src_y = y << 16;
10218 plane_state->src_w = hdisplay << 16;
10219 plane_state->src_h = vdisplay << 16;
10220
10221 return 0;
10222}
10223
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010224bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010225 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010226 struct intel_load_detect_pipe *old,
10227 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010228{
10229 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010230 struct intel_encoder *intel_encoder =
10231 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010232 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010233 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010234 struct drm_crtc *crtc = NULL;
10235 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010236 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010237 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010238 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010239 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010240 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010241 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010242
Chris Wilsond2dff872011-04-19 08:36:26 +010010243 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010244 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010245 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010246
Rob Clark51fd3712013-11-19 12:10:12 -050010247retry:
10248 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10249 if (ret)
10250 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010251
Jesse Barnes79e53942008-11-07 14:24:08 -080010252 /*
10253 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010254 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010255 * - if the connector already has an assigned crtc, use it (but make
10256 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010257 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010258 * - try to find the first unused crtc that can drive this connector,
10259 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010260 */
10261
10262 /* See if we already have a CRTC for this connector */
10263 if (encoder->crtc) {
10264 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010265
Rob Clark51fd3712013-11-19 12:10:12 -050010266 ret = drm_modeset_lock(&crtc->mutex, ctx);
10267 if (ret)
10268 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010269 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10270 if (ret)
10271 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010272
Daniel Vetter24218aa2012-08-12 19:27:11 +020010273 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010274 old->load_detect_temp = false;
10275
10276 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010277 if (connector->dpms != DRM_MODE_DPMS_ON)
10278 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010279
Chris Wilson71731882011-04-19 23:10:58 +010010280 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010281 }
10282
10283 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010284 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010285 i++;
10286 if (!(encoder->possible_crtcs & (1 << i)))
10287 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010288 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010289 continue;
10290 /* This can occur when applying the pipe A quirk on resume. */
10291 if (to_intel_crtc(possible_crtc)->new_enabled)
10292 continue;
10293
10294 crtc = possible_crtc;
10295 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010296 }
10297
10298 /*
10299 * If we didn't find an unused CRTC, don't use any.
10300 */
10301 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010302 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010303 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010304 }
10305
Rob Clark51fd3712013-11-19 12:10:12 -050010306 ret = drm_modeset_lock(&crtc->mutex, ctx);
10307 if (ret)
10308 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010309 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10310 if (ret)
10311 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010312 intel_encoder->new_crtc = to_intel_crtc(crtc);
10313 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010314
10315 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010316 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010317 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010318 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010319 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010320
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010321 state = drm_atomic_state_alloc(dev);
10322 if (!state)
10323 return false;
10324
10325 state->acquire_ctx = ctx;
10326
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010327 connector_state = drm_atomic_get_connector_state(state, connector);
10328 if (IS_ERR(connector_state)) {
10329 ret = PTR_ERR(connector_state);
10330 goto fail;
10331 }
10332
10333 connector_state->crtc = crtc;
10334 connector_state->best_encoder = &intel_encoder->base;
10335
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010336 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10337 if (IS_ERR(crtc_state)) {
10338 ret = PTR_ERR(crtc_state);
10339 goto fail;
10340 }
10341
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010342 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010343
Chris Wilson64927112011-04-20 07:25:26 +010010344 if (!mode)
10345 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010346
Chris Wilsond2dff872011-04-19 08:36:26 +010010347 /* We need a framebuffer large enough to accommodate all accesses
10348 * that the plane may generate whilst we perform load detection.
10349 * We can not rely on the fbcon either being present (we get called
10350 * during its initialisation to detect all boot displays, or it may
10351 * not even exist) or that it is large enough to satisfy the
10352 * requested mode.
10353 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010354 fb = mode_fits_in_fbdev(dev, mode);
10355 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010356 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010357 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10358 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010359 } else
10360 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010361 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010362 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010363 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010364 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010365
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010366 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10367 if (ret)
10368 goto fail;
10369
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010370 drm_mode_copy(&crtc_state->base.mode, mode);
10371
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010372 if (intel_set_mode(state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010373 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010374 if (old->release_fb)
10375 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010376 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010377 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010378 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010379
Jesse Barnes79e53942008-11-07 14:24:08 -080010380 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010381 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010382 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010383
10384 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010385 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010386fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010387 drm_atomic_state_free(state);
10388 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010389
Rob Clark51fd3712013-11-19 12:10:12 -050010390 if (ret == -EDEADLK) {
10391 drm_modeset_backoff(ctx);
10392 goto retry;
10393 }
10394
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010395 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010396}
10397
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010398void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010399 struct intel_load_detect_pipe *old,
10400 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010401{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010402 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010403 struct intel_encoder *intel_encoder =
10404 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010405 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010406 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010407 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010408 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010409 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010410 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010411 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010412
Chris Wilsond2dff872011-04-19 08:36:26 +010010413 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010414 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010415 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010416
Chris Wilson8261b192011-04-19 23:18:09 +010010417 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010418 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010419 if (!state)
10420 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010421
10422 state->acquire_ctx = ctx;
10423
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010424 connector_state = drm_atomic_get_connector_state(state, connector);
10425 if (IS_ERR(connector_state))
10426 goto fail;
10427
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010428 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10429 if (IS_ERR(crtc_state))
10430 goto fail;
10431
Daniel Vetterfc303102012-07-09 10:40:58 +020010432 to_intel_connector(connector)->new_encoder = NULL;
10433 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010434 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010435
10436 connector_state->best_encoder = NULL;
10437 connector_state->crtc = NULL;
10438
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010439 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010440
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010441 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10442 0, 0);
10443 if (ret)
10444 goto fail;
10445
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020010446 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010447 if (ret)
10448 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010449
Daniel Vetter36206362012-12-10 20:42:17 +010010450 if (old->release_fb) {
10451 drm_framebuffer_unregister_private(old->release_fb);
10452 drm_framebuffer_unreference(old->release_fb);
10453 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010454
Chris Wilson0622a532011-04-21 09:32:11 +010010455 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010456 }
10457
Eric Anholtc751ce42010-03-25 11:48:48 -070010458 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010459 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10460 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010461
10462 return;
10463fail:
10464 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10465 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010466}
10467
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010468static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010469 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010470{
10471 struct drm_i915_private *dev_priv = dev->dev_private;
10472 u32 dpll = pipe_config->dpll_hw_state.dpll;
10473
10474 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010475 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010476 else if (HAS_PCH_SPLIT(dev))
10477 return 120000;
10478 else if (!IS_GEN2(dev))
10479 return 96000;
10480 else
10481 return 48000;
10482}
10483
Jesse Barnes79e53942008-11-07 14:24:08 -080010484/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010485static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010486 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010487{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010488 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010489 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010490 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010491 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010492 u32 fp;
10493 intel_clock_t clock;
Imre Deakdccbea32015-06-22 23:35:51 +030010494 int port_clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010495 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010496
10497 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010498 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010499 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010500 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010501
10502 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010503 if (IS_PINEVIEW(dev)) {
10504 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10505 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010506 } else {
10507 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10508 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10509 }
10510
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010511 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010512 if (IS_PINEVIEW(dev))
10513 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10514 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010515 else
10516 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010517 DPLL_FPA01_P1_POST_DIV_SHIFT);
10518
10519 switch (dpll & DPLL_MODE_MASK) {
10520 case DPLLB_MODE_DAC_SERIAL:
10521 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10522 5 : 10;
10523 break;
10524 case DPLLB_MODE_LVDS:
10525 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10526 7 : 14;
10527 break;
10528 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010529 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010530 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010531 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010532 }
10533
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010534 if (IS_PINEVIEW(dev))
Imre Deakdccbea32015-06-22 23:35:51 +030010535 port_clock = pnv_calc_dpll_params(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010536 else
Imre Deakdccbea32015-06-22 23:35:51 +030010537 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010538 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010539 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010540 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010541
10542 if (is_lvds) {
10543 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10544 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010545
10546 if (lvds & LVDS_CLKB_POWER_UP)
10547 clock.p2 = 7;
10548 else
10549 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010550 } else {
10551 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10552 clock.p1 = 2;
10553 else {
10554 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10555 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10556 }
10557 if (dpll & PLL_P2_DIVIDE_BY_4)
10558 clock.p2 = 4;
10559 else
10560 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010561 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010562
Imre Deakdccbea32015-06-22 23:35:51 +030010563 port_clock = i9xx_calc_dpll_params(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010564 }
10565
Ville Syrjälä18442d02013-09-13 16:00:08 +030010566 /*
10567 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010568 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010569 * encoder's get_config() function.
10570 */
Imre Deakdccbea32015-06-22 23:35:51 +030010571 pipe_config->port_clock = port_clock;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010572}
10573
Ville Syrjälä6878da02013-09-13 15:59:11 +030010574int intel_dotclock_calculate(int link_freq,
10575 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010576{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010577 /*
10578 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010579 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010580 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010581 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010582 *
10583 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010584 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010585 */
10586
Ville Syrjälä6878da02013-09-13 15:59:11 +030010587 if (!m_n->link_n)
10588 return 0;
10589
10590 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10591}
10592
Ville Syrjälä18442d02013-09-13 16:00:08 +030010593static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010594 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010595{
10596 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010597
10598 /* read out port_clock from the DPLL */
10599 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010600
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010601 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010602 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010603 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010604 * agree once we know their relationship in the encoder's
10605 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010606 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010607 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010608 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10609 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010610}
10611
10612/** Returns the currently programmed mode of the given pipe. */
10613struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10614 struct drm_crtc *crtc)
10615{
Jesse Barnes548f2452011-02-17 10:40:53 -080010616 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010617 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010618 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010619 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010620 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010621 int htot = I915_READ(HTOTAL(cpu_transcoder));
10622 int hsync = I915_READ(HSYNC(cpu_transcoder));
10623 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10624 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010625 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010626
10627 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10628 if (!mode)
10629 return NULL;
10630
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010631 /*
10632 * Construct a pipe_config sufficient for getting the clock info
10633 * back out of crtc_clock_get.
10634 *
10635 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10636 * to use a real value here instead.
10637 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010638 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010639 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010640 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10641 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10642 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010643 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10644
Ville Syrjälä773ae032013-09-23 17:48:20 +030010645 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010646 mode->hdisplay = (htot & 0xffff) + 1;
10647 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10648 mode->hsync_start = (hsync & 0xffff) + 1;
10649 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10650 mode->vdisplay = (vtot & 0xffff) + 1;
10651 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10652 mode->vsync_start = (vsync & 0xffff) + 1;
10653 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10654
10655 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010656
10657 return mode;
10658}
10659
Chris Wilsonf047e392012-07-21 12:31:41 +010010660void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010661{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010662 struct drm_i915_private *dev_priv = dev->dev_private;
10663
Chris Wilsonf62a0072014-02-21 17:55:39 +000010664 if (dev_priv->mm.busy)
10665 return;
10666
Paulo Zanoni43694d62014-03-07 20:08:08 -030010667 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010668 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010669 if (INTEL_INFO(dev)->gen >= 6)
10670 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010671 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010672}
10673
10674void intel_mark_idle(struct drm_device *dev)
10675{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010676 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010677
Chris Wilsonf62a0072014-02-21 17:55:39 +000010678 if (!dev_priv->mm.busy)
10679 return;
10680
10681 dev_priv->mm.busy = false;
10682
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010683 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010684 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010685
Paulo Zanoni43694d62014-03-07 20:08:08 -030010686 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010687}
10688
Jesse Barnes79e53942008-11-07 14:24:08 -080010689static void intel_crtc_destroy(struct drm_crtc *crtc)
10690{
10691 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010692 struct drm_device *dev = crtc->dev;
10693 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010694
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010695 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010696 work = intel_crtc->unpin_work;
10697 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010698 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010699
10700 if (work) {
10701 cancel_work_sync(&work->work);
10702 kfree(work);
10703 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010704
10705 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010706
Jesse Barnes79e53942008-11-07 14:24:08 -080010707 kfree(intel_crtc);
10708}
10709
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010710static void intel_unpin_work_fn(struct work_struct *__work)
10711{
10712 struct intel_unpin_work *work =
10713 container_of(__work, struct intel_unpin_work, work);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010714 struct intel_crtc *crtc = to_intel_crtc(work->crtc);
10715 struct drm_device *dev = crtc->base.dev;
Paulo Zanoni7733b492015-07-07 15:26:04 -030010716 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010717 struct drm_plane *primary = crtc->base.primary;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010718
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010719 mutex_lock(&dev->struct_mutex);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010720 intel_unpin_fb_obj(work->old_fb, primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010721 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010722
Paulo Zanoni7733b492015-07-07 15:26:04 -030010723 intel_fbc_update(dev_priv);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010724
10725 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010726 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010727 mutex_unlock(&dev->struct_mutex);
10728
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010729 intel_frontbuffer_flip_complete(dev, to_intel_plane(primary)->frontbuffer_bit);
Chris Wilson89ed88b2015-02-16 14:31:49 +000010730 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010731
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030010732 BUG_ON(atomic_read(&crtc->unpin_work_count) == 0);
10733 atomic_dec(&crtc->unpin_work_count);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010734
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010735 kfree(work);
10736}
10737
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010738static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010739 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010740{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010741 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10742 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010743 unsigned long flags;
10744
10745 /* Ignore early vblank irqs */
10746 if (intel_crtc == NULL)
10747 return;
10748
Daniel Vetterf3260382014-09-15 14:55:23 +020010749 /*
10750 * This is called both by irq handlers and the reset code (to complete
10751 * lost pageflips) so needs the full irqsave spinlocks.
10752 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010753 spin_lock_irqsave(&dev->event_lock, flags);
10754 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010755
10756 /* Ensure we don't miss a work->pending update ... */
10757 smp_rmb();
10758
10759 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010760 spin_unlock_irqrestore(&dev->event_lock, flags);
10761 return;
10762 }
10763
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010764 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010765
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010766 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010767}
10768
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010769void intel_finish_page_flip(struct drm_device *dev, int pipe)
10770{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010771 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010772 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10773
Mario Kleiner49b14a52010-12-09 07:00:07 +010010774 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010775}
10776
10777void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10778{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010779 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010780 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10781
Mario Kleiner49b14a52010-12-09 07:00:07 +010010782 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010783}
10784
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010785/* Is 'a' after or equal to 'b'? */
10786static bool g4x_flip_count_after_eq(u32 a, u32 b)
10787{
10788 return !((a - b) & 0x80000000);
10789}
10790
10791static bool page_flip_finished(struct intel_crtc *crtc)
10792{
10793 struct drm_device *dev = crtc->base.dev;
10794 struct drm_i915_private *dev_priv = dev->dev_private;
10795
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010796 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10797 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10798 return true;
10799
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010800 /*
10801 * The relevant registers doen't exist on pre-ctg.
10802 * As the flip done interrupt doesn't trigger for mmio
10803 * flips on gmch platforms, a flip count check isn't
10804 * really needed there. But since ctg has the registers,
10805 * include it in the check anyway.
10806 */
10807 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10808 return true;
10809
10810 /*
10811 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10812 * used the same base address. In that case the mmio flip might
10813 * have completed, but the CS hasn't even executed the flip yet.
10814 *
10815 * A flip count check isn't enough as the CS might have updated
10816 * the base address just after start of vblank, but before we
10817 * managed to process the interrupt. This means we'd complete the
10818 * CS flip too soon.
10819 *
10820 * Combining both checks should get us a good enough result. It may
10821 * still happen that the CS flip has been executed, but has not
10822 * yet actually completed. But in case the base address is the same
10823 * anyway, we don't really care.
10824 */
10825 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10826 crtc->unpin_work->gtt_offset &&
10827 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10828 crtc->unpin_work->flip_count);
10829}
10830
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010831void intel_prepare_page_flip(struct drm_device *dev, int plane)
10832{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010833 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010834 struct intel_crtc *intel_crtc =
10835 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10836 unsigned long flags;
10837
Daniel Vetterf3260382014-09-15 14:55:23 +020010838
10839 /*
10840 * This is called both by irq handlers and the reset code (to complete
10841 * lost pageflips) so needs the full irqsave spinlocks.
10842 *
10843 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010844 * generate a page-flip completion irq, i.e. every modeset
10845 * is also accompanied by a spurious intel_prepare_page_flip().
10846 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010847 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010848 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010849 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010850 spin_unlock_irqrestore(&dev->event_lock, flags);
10851}
10852
Robin Schroereba905b2014-05-18 02:24:50 +020010853static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010854{
10855 /* Ensure that the work item is consistent when activating it ... */
10856 smp_wmb();
10857 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10858 /* and that it is marked active as soon as the irq could fire. */
10859 smp_wmb();
10860}
10861
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010862static int intel_gen2_queue_flip(struct drm_device *dev,
10863 struct drm_crtc *crtc,
10864 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010865 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010866 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010867 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010868{
John Harrison6258fbe2015-05-29 17:43:48 +010010869 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010870 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010871 u32 flip_mask;
10872 int ret;
10873
John Harrison5fb9de12015-05-29 17:44:07 +010010874 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010875 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010876 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010877
10878 /* Can't queue multiple flips, so wait for the previous
10879 * one to finish before executing the next.
10880 */
10881 if (intel_crtc->plane)
10882 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10883 else
10884 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010885 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10886 intel_ring_emit(ring, MI_NOOP);
10887 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10888 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10889 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010890 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010891 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010892
10893 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010894 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010895}
10896
10897static int intel_gen3_queue_flip(struct drm_device *dev,
10898 struct drm_crtc *crtc,
10899 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010900 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010901 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010902 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010903{
John Harrison6258fbe2015-05-29 17:43:48 +010010904 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010905 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010906 u32 flip_mask;
10907 int ret;
10908
John Harrison5fb9de12015-05-29 17:44:07 +010010909 ret = intel_ring_begin(req, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010910 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010911 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010912
10913 if (intel_crtc->plane)
10914 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10915 else
10916 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010917 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10918 intel_ring_emit(ring, MI_NOOP);
10919 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10920 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10921 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010922 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010923 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010924
Chris Wilsone7d841c2012-12-03 11:36:30 +000010925 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010926 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010927}
10928
10929static int intel_gen4_queue_flip(struct drm_device *dev,
10930 struct drm_crtc *crtc,
10931 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010932 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010933 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010934 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010935{
John Harrison6258fbe2015-05-29 17:43:48 +010010936 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010937 struct drm_i915_private *dev_priv = dev->dev_private;
10938 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10939 uint32_t pf, pipesrc;
10940 int ret;
10941
John Harrison5fb9de12015-05-29 17:44:07 +010010942 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010943 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010944 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010945
10946 /* i965+ uses the linear or tiled offsets from the
10947 * Display Registers (which do not change across a page-flip)
10948 * so we need only reprogram the base address.
10949 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010950 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10951 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10952 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010953 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010954 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010955
10956 /* XXX Enabling the panel-fitter across page-flip is so far
10957 * untested on non-native modes, so ignore it for now.
10958 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10959 */
10960 pf = 0;
10961 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010962 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010963
10964 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010010965 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010966}
10967
10968static int intel_gen6_queue_flip(struct drm_device *dev,
10969 struct drm_crtc *crtc,
10970 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010971 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010010972 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070010973 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010974{
John Harrison6258fbe2015-05-29 17:43:48 +010010975 struct intel_engine_cs *ring = req->ring;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010976 struct drm_i915_private *dev_priv = dev->dev_private;
10977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10978 uint32_t pf, pipesrc;
10979 int ret;
10980
John Harrison5fb9de12015-05-29 17:44:07 +010010981 ret = intel_ring_begin(req, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010982 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010983 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010984
Daniel Vetter6d90c952012-04-26 23:28:05 +020010985 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10986 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10987 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010988 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010989
Chris Wilson99d9acd2012-04-17 20:37:00 +010010990 /* Contrary to the suggestions in the documentation,
10991 * "Enable Panel Fitter" does not seem to be required when page
10992 * flipping with a non-native mode, and worse causes a normal
10993 * modeset to fail.
10994 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10995 */
10996 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010997 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010998 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010999
11000 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011001 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011002}
11003
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011004static int intel_gen7_queue_flip(struct drm_device *dev,
11005 struct drm_crtc *crtc,
11006 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011007 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011008 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011009 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011010{
John Harrison6258fbe2015-05-29 17:43:48 +010011011 struct intel_engine_cs *ring = req->ring;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011012 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011013 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011014 int len, ret;
11015
Robin Schroereba905b2014-05-18 02:24:50 +020011016 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011017 case PLANE_A:
11018 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11019 break;
11020 case PLANE_B:
11021 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11022 break;
11023 case PLANE_C:
11024 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11025 break;
11026 default:
11027 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011028 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011029 }
11030
Chris Wilsonffe74d72013-08-26 20:58:12 +010011031 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011032 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011033 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011034 /*
11035 * On Gen 8, SRM is now taking an extra dword to accommodate
11036 * 48bits addresses, and we need a NOOP for the batch size to
11037 * stay even.
11038 */
11039 if (IS_GEN8(dev))
11040 len += 2;
11041 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011042
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011043 /*
11044 * BSpec MI_DISPLAY_FLIP for IVB:
11045 * "The full packet must be contained within the same cache line."
11046 *
11047 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11048 * cacheline, if we ever start emitting more commands before
11049 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11050 * then do the cacheline alignment, and finally emit the
11051 * MI_DISPLAY_FLIP.
11052 */
John Harrisonbba09b12015-05-29 17:44:06 +010011053 ret = intel_ring_cacheline_align(req);
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011054 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011055 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011056
John Harrison5fb9de12015-05-29 17:44:07 +010011057 ret = intel_ring_begin(req, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011058 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011059 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011060
Chris Wilsonffe74d72013-08-26 20:58:12 +010011061 /* Unmask the flip-done completion message. Note that the bspec says that
11062 * we should do this for both the BCS and RCS, and that we must not unmask
11063 * more than one flip event at any time (or ensure that one flip message
11064 * can be sent by waiting for flip-done prior to queueing new flips).
11065 * Experimentation says that BCS works despite DERRMR masking all
11066 * flip-done completion events and that unmasking all planes at once
11067 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11068 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11069 */
11070 if (ring->id == RCS) {
11071 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11072 intel_ring_emit(ring, DERRMR);
11073 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11074 DERRMR_PIPEB_PRI_FLIP_DONE |
11075 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011076 if (IS_GEN8(dev))
11077 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11078 MI_SRM_LRM_GLOBAL_GTT);
11079 else
11080 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11081 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011082 intel_ring_emit(ring, DERRMR);
11083 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011084 if (IS_GEN8(dev)) {
11085 intel_ring_emit(ring, 0);
11086 intel_ring_emit(ring, MI_NOOP);
11087 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011088 }
11089
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011090 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011091 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011092 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011093 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011094
11095 intel_mark_page_flip_active(intel_crtc);
Chris Wilson83d40922012-04-17 19:35:53 +010011096 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011097}
11098
Sourab Gupta84c33a62014-06-02 16:47:17 +053011099static bool use_mmio_flip(struct intel_engine_cs *ring,
11100 struct drm_i915_gem_object *obj)
11101{
11102 /*
11103 * This is not being used for older platforms, because
11104 * non-availability of flip done interrupt forces us to use
11105 * CS flips. Older platforms derive flip done using some clever
11106 * tricks involving the flip_pending status bits and vblank irqs.
11107 * So using MMIO flips there would disrupt this mechanism.
11108 */
11109
Chris Wilson8e09bf82014-07-08 10:40:30 +010011110 if (ring == NULL)
11111 return true;
11112
Sourab Gupta84c33a62014-06-02 16:47:17 +053011113 if (INTEL_INFO(ring->dev)->gen < 5)
11114 return false;
11115
11116 if (i915.use_mmio_flip < 0)
11117 return false;
11118 else if (i915.use_mmio_flip > 0)
11119 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011120 else if (i915.enable_execlists)
11121 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011122 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011123 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011124}
11125
Damien Lespiauff944562014-11-20 14:58:16 +000011126static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11127{
11128 struct drm_device *dev = intel_crtc->base.dev;
11129 struct drm_i915_private *dev_priv = dev->dev_private;
11130 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011131 const enum pipe pipe = intel_crtc->pipe;
11132 u32 ctl, stride;
11133
11134 ctl = I915_READ(PLANE_CTL(pipe, 0));
11135 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011136 switch (fb->modifier[0]) {
11137 case DRM_FORMAT_MOD_NONE:
11138 break;
11139 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011140 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011141 break;
11142 case I915_FORMAT_MOD_Y_TILED:
11143 ctl |= PLANE_CTL_TILED_Y;
11144 break;
11145 case I915_FORMAT_MOD_Yf_TILED:
11146 ctl |= PLANE_CTL_TILED_YF;
11147 break;
11148 default:
11149 MISSING_CASE(fb->modifier[0]);
11150 }
Damien Lespiauff944562014-11-20 14:58:16 +000011151
11152 /*
11153 * The stride is either expressed as a multiple of 64 bytes chunks for
11154 * linear buffers or in number of tiles for tiled buffers.
11155 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011156 stride = fb->pitches[0] /
11157 intel_fb_stride_alignment(dev, fb->modifier[0],
11158 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011159
11160 /*
11161 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11162 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11163 */
11164 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11165 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11166
11167 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11168 POSTING_READ(PLANE_SURF(pipe, 0));
11169}
11170
11171static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011172{
11173 struct drm_device *dev = intel_crtc->base.dev;
11174 struct drm_i915_private *dev_priv = dev->dev_private;
11175 struct intel_framebuffer *intel_fb =
11176 to_intel_framebuffer(intel_crtc->base.primary->fb);
11177 struct drm_i915_gem_object *obj = intel_fb->obj;
11178 u32 dspcntr;
11179 u32 reg;
11180
Sourab Gupta84c33a62014-06-02 16:47:17 +053011181 reg = DSPCNTR(intel_crtc->plane);
11182 dspcntr = I915_READ(reg);
11183
Damien Lespiauc5d97472014-10-25 00:11:11 +010011184 if (obj->tiling_mode != I915_TILING_NONE)
11185 dspcntr |= DISPPLANE_TILED;
11186 else
11187 dspcntr &= ~DISPPLANE_TILED;
11188
Sourab Gupta84c33a62014-06-02 16:47:17 +053011189 I915_WRITE(reg, dspcntr);
11190
11191 I915_WRITE(DSPSURF(intel_crtc->plane),
11192 intel_crtc->unpin_work->gtt_offset);
11193 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011194
Damien Lespiauff944562014-11-20 14:58:16 +000011195}
11196
11197/*
11198 * XXX: This is the temporary way to update the plane registers until we get
11199 * around to using the usual plane update functions for MMIO flips
11200 */
11201static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11202{
11203 struct drm_device *dev = intel_crtc->base.dev;
11204 bool atomic_update;
11205 u32 start_vbl_count;
11206
11207 intel_mark_page_flip_active(intel_crtc);
11208
11209 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11210
11211 if (INTEL_INFO(dev)->gen >= 9)
11212 skl_do_mmio_flip(intel_crtc);
11213 else
11214 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11215 ilk_do_mmio_flip(intel_crtc);
11216
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011217 if (atomic_update)
11218 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011219}
11220
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011221static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011222{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011223 struct intel_mmio_flip *mmio_flip =
11224 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011225
Daniel Vettereed29a52015-05-21 14:21:25 +020011226 if (mmio_flip->req)
11227 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011228 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011229 false, NULL,
11230 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011231
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011232 intel_do_mmio_flip(mmio_flip->crtc);
11233
Daniel Vettereed29a52015-05-21 14:21:25 +020011234 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011235 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011236}
11237
11238static int intel_queue_mmio_flip(struct drm_device *dev,
11239 struct drm_crtc *crtc,
11240 struct drm_framebuffer *fb,
11241 struct drm_i915_gem_object *obj,
11242 struct intel_engine_cs *ring,
11243 uint32_t flags)
11244{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011245 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011246
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011247 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11248 if (mmio_flip == NULL)
11249 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011250
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011251 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011252 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011253 mmio_flip->crtc = to_intel_crtc(crtc);
11254
11255 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11256 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011257
Sourab Gupta84c33a62014-06-02 16:47:17 +053011258 return 0;
11259}
11260
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011261static int intel_default_queue_flip(struct drm_device *dev,
11262 struct drm_crtc *crtc,
11263 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011264 struct drm_i915_gem_object *obj,
John Harrison6258fbe2015-05-29 17:43:48 +010011265 struct drm_i915_gem_request *req,
Keith Packarded8d1972013-07-22 18:49:58 -070011266 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011267{
11268 return -ENODEV;
11269}
11270
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011271static bool __intel_pageflip_stall_check(struct drm_device *dev,
11272 struct drm_crtc *crtc)
11273{
11274 struct drm_i915_private *dev_priv = dev->dev_private;
11275 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11276 struct intel_unpin_work *work = intel_crtc->unpin_work;
11277 u32 addr;
11278
11279 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11280 return true;
11281
11282 if (!work->enable_stall_check)
11283 return false;
11284
11285 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011286 if (work->flip_queued_req &&
11287 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011288 return false;
11289
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011290 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011291 }
11292
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011293 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011294 return false;
11295
11296 /* Potential stall - if we see that the flip has happened,
11297 * assume a missed interrupt. */
11298 if (INTEL_INFO(dev)->gen >= 4)
11299 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11300 else
11301 addr = I915_READ(DSPADDR(intel_crtc->plane));
11302
11303 /* There is a potential issue here with a false positive after a flip
11304 * to the same address. We could address this by checking for a
11305 * non-incrementing frame counter.
11306 */
11307 return addr == work->gtt_offset;
11308}
11309
11310void intel_check_page_flip(struct drm_device *dev, int pipe)
11311{
11312 struct drm_i915_private *dev_priv = dev->dev_private;
11313 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11314 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011315 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011316
Dave Gordon6c51d462015-03-06 15:34:26 +000011317 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011318
11319 if (crtc == NULL)
11320 return;
11321
Daniel Vetterf3260382014-09-15 14:55:23 +020011322 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011323 work = intel_crtc->unpin_work;
11324 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011325 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011326 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011327 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011328 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011329 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011330 if (work != NULL &&
11331 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11332 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011333 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011334}
11335
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011336static int intel_crtc_page_flip(struct drm_crtc *crtc,
11337 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011338 struct drm_pending_vblank_event *event,
11339 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011340{
11341 struct drm_device *dev = crtc->dev;
11342 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011343 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011344 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011346 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011347 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011348 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011349 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011350 bool mmio_flip;
John Harrison91af1272015-06-18 13:14:56 +010011351 struct drm_i915_gem_request *request = NULL;
Chris Wilson52e68632010-08-08 10:15:59 +010011352 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011353
Matt Roper2ff8fde2014-07-08 07:50:07 -070011354 /*
11355 * drm_mode_page_flip_ioctl() should already catch this, but double
11356 * check to be safe. In the future we may enable pageflipping from
11357 * a disabled primary plane.
11358 */
11359 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11360 return -EBUSY;
11361
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011362 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011363 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011364 return -EINVAL;
11365
11366 /*
11367 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11368 * Note that pitch changes could also affect these register.
11369 */
11370 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011371 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11372 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011373 return -EINVAL;
11374
Chris Wilsonf900db42014-02-20 09:26:13 +000011375 if (i915_terminally_wedged(&dev_priv->gpu_error))
11376 goto out_hang;
11377
Daniel Vetterb14c5672013-09-19 12:18:32 +020011378 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011379 if (work == NULL)
11380 return -ENOMEM;
11381
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011382 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011383 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011384 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011385 INIT_WORK(&work->work, intel_unpin_work_fn);
11386
Daniel Vetter87b6b102014-05-15 15:33:46 +020011387 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011388 if (ret)
11389 goto free_work;
11390
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011391 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011392 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011393 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011394 /* Before declaring the flip queue wedged, check if
11395 * the hardware completed the operation behind our backs.
11396 */
11397 if (__intel_pageflip_stall_check(dev, crtc)) {
11398 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11399 page_flip_completed(intel_crtc);
11400 } else {
11401 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011402 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011403
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011404 drm_crtc_vblank_put(crtc);
11405 kfree(work);
11406 return -EBUSY;
11407 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011408 }
11409 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011410 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011411
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011412 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11413 flush_workqueue(dev_priv->wq);
11414
Jesse Barnes75dfca82010-02-10 15:09:44 -080011415 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011416 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011417 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011418
Matt Roperf4510a22014-04-01 15:22:40 -070011419 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011420 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011421
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011422 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011423
Chris Wilson89ed88b2015-02-16 14:31:49 +000011424 ret = i915_mutex_lock_interruptible(dev);
11425 if (ret)
11426 goto cleanup;
11427
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011428 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011429 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011430
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011431 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011432 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011433
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011434 if (IS_VALLEYVIEW(dev)) {
11435 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011436 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011437 /* vlv: DISPLAY_FLIP fails to change tiling */
11438 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011439 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011440 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011441 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011442 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011443 if (ring == NULL || ring->id != RCS)
11444 ring = &dev_priv->ring[BCS];
11445 } else {
11446 ring = &dev_priv->ring[RCS];
11447 }
11448
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011449 mmio_flip = use_mmio_flip(ring, obj);
11450
11451 /* When using CS flips, we want to emit semaphores between rings.
11452 * However, when using mmio flips we will create a task to do the
11453 * synchronisation, so all we want here is to pin the framebuffer
11454 * into the display plane and skip any waits.
11455 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011456 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011457 crtc->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010011458 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring, &request);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011459 if (ret)
11460 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011461
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011462 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11463 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011464
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011465 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011466 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11467 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011468 if (ret)
11469 goto cleanup_unpin;
11470
John Harrisonf06cc1b2014-11-24 18:49:37 +000011471 i915_gem_request_assign(&work->flip_queued_req,
11472 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011473 } else {
John Harrison6258fbe2015-05-29 17:43:48 +010011474 if (!request) {
11475 ret = i915_gem_request_alloc(ring, ring->default_context, &request);
11476 if (ret)
11477 goto cleanup_unpin;
11478 }
11479
11480 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, request,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011481 page_flip_flags);
11482 if (ret)
11483 goto cleanup_unpin;
11484
John Harrison6258fbe2015-05-29 17:43:48 +010011485 i915_gem_request_assign(&work->flip_queued_req, request);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011486 }
11487
John Harrison91af1272015-06-18 13:14:56 +010011488 if (request)
John Harrison75289872015-05-29 17:43:49 +010011489 i915_add_request_no_flush(request);
John Harrison91af1272015-06-18 13:14:56 +010011490
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011491 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011492 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011493
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011494 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011495 to_intel_plane(primary)->frontbuffer_bit);
Paulo Zanonic80ac852015-07-02 19:25:13 -030011496 mutex_unlock(&dev->struct_mutex);
Daniel Vettera071fa02014-06-18 23:28:09 +020011497
Paulo Zanoni7733b492015-07-07 15:26:04 -030011498 intel_fbc_disable(dev_priv);
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011499 intel_frontbuffer_flip_prepare(dev,
11500 to_intel_plane(primary)->frontbuffer_bit);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011501
Jesse Barnese5510fa2010-07-01 16:48:37 -070011502 trace_i915_flip_request(intel_crtc->plane, obj);
11503
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011504 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011505
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011506cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011507 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011508cleanup_pending:
John Harrison91af1272015-06-18 13:14:56 +010011509 if (request)
11510 i915_gem_request_cancel(request);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011511 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011512 mutex_unlock(&dev->struct_mutex);
11513cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011514 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011515 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011516
Chris Wilson89ed88b2015-02-16 14:31:49 +000011517 drm_gem_object_unreference_unlocked(&obj->base);
11518 drm_framebuffer_unreference(work->old_fb);
11519
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011520 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011521 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011522 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011523
Daniel Vetter87b6b102014-05-15 15:33:46 +020011524 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011525free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011526 kfree(work);
11527
Chris Wilsonf900db42014-02-20 09:26:13 +000011528 if (ret == -EIO) {
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011529 struct drm_atomic_state *state;
11530 struct drm_plane_state *plane_state;
11531
Chris Wilsonf900db42014-02-20 09:26:13 +000011532out_hang:
Maarten Lankhorst02e0efb2015-06-12 11:15:40 +020011533 state = drm_atomic_state_alloc(dev);
11534 if (!state)
11535 return -ENOMEM;
11536 state->acquire_ctx = drm_modeset_legacy_acquire_ctx(crtc);
11537
11538retry:
11539 plane_state = drm_atomic_get_plane_state(state, primary);
11540 ret = PTR_ERR_OR_ZERO(plane_state);
11541 if (!ret) {
11542 drm_atomic_set_fb_for_plane(plane_state, fb);
11543
11544 ret = drm_atomic_set_crtc_for_plane(plane_state, crtc);
11545 if (!ret)
11546 ret = drm_atomic_commit(state);
11547 }
11548
11549 if (ret == -EDEADLK) {
11550 drm_modeset_backoff(state->acquire_ctx);
11551 drm_atomic_state_clear(state);
11552 goto retry;
11553 }
11554
11555 if (ret)
11556 drm_atomic_state_free(state);
11557
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011558 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011559 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011560 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011561 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011562 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011563 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011564 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011565}
11566
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011567
11568/**
11569 * intel_wm_need_update - Check whether watermarks need updating
11570 * @plane: drm plane
11571 * @state: new plane state
11572 *
11573 * Check current plane state versus the new one to determine whether
11574 * watermarks need to be recalculated.
11575 *
11576 * Returns true or false.
11577 */
11578static bool intel_wm_need_update(struct drm_plane *plane,
11579 struct drm_plane_state *state)
11580{
11581 /* Update watermarks on tiling changes. */
11582 if (!plane->state->fb || !state->fb ||
11583 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
11584 plane->state->rotation != state->rotation)
11585 return true;
11586
11587 if (plane->state->crtc_w != state->crtc_w)
11588 return true;
11589
11590 return false;
11591}
11592
11593int intel_plane_atomic_calc_changes(struct drm_crtc_state *crtc_state,
11594 struct drm_plane_state *plane_state)
11595{
11596 struct drm_crtc *crtc = crtc_state->crtc;
11597 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11598 struct drm_plane *plane = plane_state->plane;
11599 struct drm_device *dev = crtc->dev;
11600 struct drm_i915_private *dev_priv = dev->dev_private;
11601 struct intel_plane_state *old_plane_state =
11602 to_intel_plane_state(plane->state);
11603 int idx = intel_crtc->base.base.id, ret;
11604 int i = drm_plane_index(plane);
11605 bool mode_changed = needs_modeset(crtc_state);
11606 bool was_crtc_enabled = crtc->state->active;
11607 bool is_crtc_enabled = crtc_state->active;
11608
11609 bool turn_off, turn_on, visible, was_visible;
11610 struct drm_framebuffer *fb = plane_state->fb;
11611
11612 if (crtc_state && INTEL_INFO(dev)->gen >= 9 &&
11613 plane->type != DRM_PLANE_TYPE_CURSOR) {
11614 ret = skl_update_scaler_plane(
11615 to_intel_crtc_state(crtc_state),
11616 to_intel_plane_state(plane_state));
11617 if (ret)
11618 return ret;
11619 }
11620
11621 /*
11622 * Disabling a plane is always okay; we just need to update
11623 * fb tracking in a special way since cleanup_fb() won't
11624 * get called by the plane helpers.
11625 */
11626 if (old_plane_state->base.fb && !fb)
11627 intel_crtc->atomic.disabled_planes |= 1 << i;
11628
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011629 was_visible = old_plane_state->visible;
11630 visible = to_intel_plane_state(plane_state)->visible;
11631
11632 if (!was_crtc_enabled && WARN_ON(was_visible))
11633 was_visible = false;
11634
11635 if (!is_crtc_enabled && WARN_ON(visible))
11636 visible = false;
11637
11638 if (!was_visible && !visible)
11639 return 0;
11640
11641 turn_off = was_visible && (!visible || mode_changed);
11642 turn_on = visible && (!was_visible || mode_changed);
11643
11644 DRM_DEBUG_ATOMIC("[CRTC:%i] has [PLANE:%i] with fb %i\n", idx,
11645 plane->base.id, fb ? fb->base.id : -1);
11646
11647 DRM_DEBUG_ATOMIC("[PLANE:%i] visible %i -> %i, off %i, on %i, ms %i\n",
11648 plane->base.id, was_visible, visible,
11649 turn_off, turn_on, mode_changed);
11650
Ville Syrjälä852eb002015-06-24 22:00:07 +030011651 if (turn_on) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011652 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011653 /* must disable cxsr around plane enable/disable */
11654 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11655 intel_crtc->atomic.disable_cxsr = true;
11656 /* to potentially re-enable cxsr */
11657 intel_crtc->atomic.wait_vblank = true;
11658 intel_crtc->atomic.update_wm_post = true;
11659 }
11660 } else if (turn_off) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011661 intel_crtc->atomic.update_wm_post = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011662 /* must disable cxsr around plane enable/disable */
11663 if (plane->type != DRM_PLANE_TYPE_CURSOR) {
11664 if (is_crtc_enabled)
11665 intel_crtc->atomic.wait_vblank = true;
11666 intel_crtc->atomic.disable_cxsr = true;
11667 }
11668 } else if (intel_wm_need_update(plane, plane_state)) {
Ville Syrjäläf015c552015-06-24 22:00:02 +030011669 intel_crtc->atomic.update_wm_pre = true;
Ville Syrjälä852eb002015-06-24 22:00:07 +030011670 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011671
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030011672 if (visible)
11673 intel_crtc->atomic.fb_bits |=
11674 to_intel_plane(plane)->frontbuffer_bit;
11675
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011676 switch (plane->type) {
11677 case DRM_PLANE_TYPE_PRIMARY:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011678 intel_crtc->atomic.wait_for_flips = true;
11679 intel_crtc->atomic.pre_disable_primary = turn_off;
11680 intel_crtc->atomic.post_enable_primary = turn_on;
11681
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011682 if (turn_off) {
11683 /*
11684 * FIXME: Actually if we will still have any other
11685 * plane enabled on the pipe we could let IPS enabled
11686 * still, but for now lets consider that when we make
11687 * primary invisible by setting DSPCNTR to 0 on
11688 * update_primary_plane function IPS needs to be
11689 * disable.
11690 */
11691 intel_crtc->atomic.disable_ips = true;
11692
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011693 intel_crtc->atomic.disable_fbc = true;
Rodrigo Vivi066cf55b2015-06-26 13:55:54 -070011694 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011695
11696 /*
11697 * FBC does not work on some platforms for rotated
11698 * planes, so disable it when rotation is not 0 and
11699 * update it when rotation is set back to 0.
11700 *
11701 * FIXME: This is redundant with the fbc update done in
11702 * the primary plane enable function except that that
11703 * one is done too late. We eventually need to unify
11704 * this.
11705 */
11706
11707 if (visible &&
11708 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
11709 dev_priv->fbc.crtc == intel_crtc &&
11710 plane_state->rotation != BIT(DRM_ROTATE_0))
11711 intel_crtc->atomic.disable_fbc = true;
11712
11713 /*
11714 * BDW signals flip done immediately if the plane
11715 * is disabled, even if the plane enable is already
11716 * armed to occur at the next vblank :(
11717 */
11718 if (turn_on && IS_BROADWELL(dev))
11719 intel_crtc->atomic.wait_vblank = true;
11720
11721 intel_crtc->atomic.update_fbc |= visible || mode_changed;
11722 break;
11723 case DRM_PLANE_TYPE_CURSOR:
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011724 break;
11725 case DRM_PLANE_TYPE_OVERLAY:
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020011726 if (turn_off && !mode_changed) {
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011727 intel_crtc->atomic.wait_vblank = true;
11728 intel_crtc->atomic.update_sprite_watermarks |=
11729 1 << i;
11730 }
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020011731 }
11732 return 0;
11733}
11734
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011735static bool encoders_cloneable(const struct intel_encoder *a,
11736 const struct intel_encoder *b)
11737{
11738 /* masks could be asymmetric, so check both ways */
11739 return a == b || (a->cloneable & (1 << b->type) &&
11740 b->cloneable & (1 << a->type));
11741}
11742
11743static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11744 struct intel_crtc *crtc,
11745 struct intel_encoder *encoder)
11746{
11747 struct intel_encoder *source_encoder;
11748 struct drm_connector *connector;
11749 struct drm_connector_state *connector_state;
11750 int i;
11751
11752 for_each_connector_in_state(state, connector, connector_state, i) {
11753 if (connector_state->crtc != &crtc->base)
11754 continue;
11755
11756 source_encoder =
11757 to_intel_encoder(connector_state->best_encoder);
11758 if (!encoders_cloneable(encoder, source_encoder))
11759 return false;
11760 }
11761
11762 return true;
11763}
11764
11765static bool check_encoder_cloning(struct drm_atomic_state *state,
11766 struct intel_crtc *crtc)
11767{
11768 struct intel_encoder *encoder;
11769 struct drm_connector *connector;
11770 struct drm_connector_state *connector_state;
11771 int i;
11772
11773 for_each_connector_in_state(state, connector, connector_state, i) {
11774 if (connector_state->crtc != &crtc->base)
11775 continue;
11776
11777 encoder = to_intel_encoder(connector_state->best_encoder);
11778 if (!check_single_encoder_cloning(state, crtc, encoder))
11779 return false;
11780 }
11781
11782 return true;
11783}
11784
11785static int intel_crtc_atomic_check(struct drm_crtc *crtc,
11786 struct drm_crtc_state *crtc_state)
11787{
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011788 struct drm_device *dev = crtc->dev;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011789 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011790 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstcf5a15b2015-06-15 12:33:41 +020011791 struct intel_crtc_state *pipe_config =
11792 to_intel_crtc_state(crtc_state);
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011793 struct drm_atomic_state *state = crtc_state->state;
Maarten Lankhorstad421372015-06-15 12:33:42 +020011794 int ret, idx = crtc->base.id;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011795 bool mode_changed = needs_modeset(crtc_state);
11796
11797 if (mode_changed && !check_encoder_cloning(state, intel_crtc)) {
11798 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
11799 return -EINVAL;
11800 }
11801
11802 I915_STATE_WARN(crtc->state->active != intel_crtc->active,
11803 "[CRTC:%i] mismatch between state->active(%i) and crtc->active(%i)\n",
11804 idx, crtc->state->active, intel_crtc->active);
11805
Ville Syrjälä852eb002015-06-24 22:00:07 +030011806 if (mode_changed && !crtc_state->active)
11807 intel_crtc->atomic.update_wm_post = true;
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020011808
Maarten Lankhorstad421372015-06-15 12:33:42 +020011809 if (mode_changed && crtc_state->enable &&
11810 dev_priv->display.crtc_compute_clock &&
11811 !WARN_ON(pipe_config->shared_dpll != DPLL_ID_PRIVATE)) {
11812 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
11813 pipe_config);
11814 if (ret)
11815 return ret;
11816 }
11817
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020011818 ret = 0;
11819 if (INTEL_INFO(dev)->gen >= 9) {
11820 if (mode_changed)
11821 ret = skl_update_scaler_crtc(pipe_config);
11822
11823 if (!ret)
11824 ret = intel_atomic_setup_scalers(dev, intel_crtc,
11825 pipe_config);
11826 }
11827
11828 return ret;
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011829}
11830
Jani Nikula65b38e02015-04-13 11:26:56 +030011831static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011832 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11833 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011834 .atomic_begin = intel_begin_crtc_commit,
11835 .atomic_flush = intel_finish_crtc_commit,
Maarten Lankhorst6d3a1ce2015-06-15 12:33:40 +020011836 .atomic_check = intel_crtc_atomic_check,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011837};
11838
Daniel Vetter9a935852012-07-05 22:34:27 +020011839/**
11840 * intel_modeset_update_staged_output_state
11841 *
11842 * Updates the staged output configuration state, e.g. after we've read out the
11843 * current hw state.
11844 */
11845static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11846{
Ville Syrjälä76688512014-01-10 11:28:06 +020011847 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011848 struct intel_encoder *encoder;
11849 struct intel_connector *connector;
11850
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011851 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011852 connector->new_encoder =
11853 to_intel_encoder(connector->base.encoder);
11854 }
11855
Damien Lespiaub2784e12014-08-05 11:29:37 +010011856 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011857 encoder->new_crtc =
11858 to_intel_crtc(encoder->base.crtc);
11859 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011860
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011861 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011862 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011863 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011864}
11865
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011866/* Transitional helper to copy current connector/encoder state to
11867 * connector->state. This is needed so that code that is partially
11868 * converted to atomic does the right thing.
11869 */
11870static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11871{
11872 struct intel_connector *connector;
11873
11874 for_each_intel_connector(dev, connector) {
11875 if (connector->base.encoder) {
11876 connector->base.state->best_encoder =
11877 connector->base.encoder;
11878 connector->base.state->crtc =
11879 connector->base.encoder->crtc;
11880 } else {
11881 connector->base.state->best_encoder = NULL;
11882 connector->base.state->crtc = NULL;
11883 }
11884 }
11885}
11886
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011887static void
Robin Schroereba905b2014-05-18 02:24:50 +020011888connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011889 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011890{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011891 int bpp = pipe_config->pipe_bpp;
11892
11893 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11894 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011895 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011896
11897 /* Don't use an invalid EDID bpc value */
11898 if (connector->base.display_info.bpc &&
11899 connector->base.display_info.bpc * 3 < bpp) {
11900 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11901 bpp, connector->base.display_info.bpc*3);
11902 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11903 }
11904
11905 /* Clamp bpp to 8 on screens without EDID 1.4 */
11906 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11907 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11908 bpp);
11909 pipe_config->pipe_bpp = 24;
11910 }
11911}
11912
11913static int
11914compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011915 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011916{
11917 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011918 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011919 struct drm_connector *connector;
11920 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011921 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011922
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011923 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011924 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011925 else if (INTEL_INFO(dev)->gen >= 5)
11926 bpp = 12*3;
11927 else
11928 bpp = 8*3;
11929
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011930
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011931 pipe_config->pipe_bpp = bpp;
11932
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011933 state = pipe_config->base.state;
11934
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011935 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011936 for_each_connector_in_state(state, connector, connector_state, i) {
11937 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011938 continue;
11939
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011940 connected_sink_compute_bpp(to_intel_connector(connector),
11941 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011942 }
11943
11944 return bpp;
11945}
11946
Daniel Vetter644db712013-09-19 14:53:58 +020011947static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11948{
11949 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11950 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011951 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011952 mode->crtc_hdisplay, mode->crtc_hsync_start,
11953 mode->crtc_hsync_end, mode->crtc_htotal,
11954 mode->crtc_vdisplay, mode->crtc_vsync_start,
11955 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11956}
11957
Daniel Vetterc0b03412013-05-28 12:05:54 +020011958static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011959 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011960 const char *context)
11961{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011962 struct drm_device *dev = crtc->base.dev;
11963 struct drm_plane *plane;
11964 struct intel_plane *intel_plane;
11965 struct intel_plane_state *state;
11966 struct drm_framebuffer *fb;
11967
11968 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11969 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011970
11971 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11972 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11973 pipe_config->pipe_bpp, pipe_config->dither);
11974 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11975 pipe_config->has_pch_encoder,
11976 pipe_config->fdi_lanes,
11977 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11978 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11979 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011980 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11981 pipe_config->has_dp_encoder,
11982 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11983 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11984 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011985
11986 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11987 pipe_config->has_dp_encoder,
11988 pipe_config->dp_m2_n2.gmch_m,
11989 pipe_config->dp_m2_n2.gmch_n,
11990 pipe_config->dp_m2_n2.link_m,
11991 pipe_config->dp_m2_n2.link_n,
11992 pipe_config->dp_m2_n2.tu);
11993
Daniel Vetter55072d12014-11-20 16:10:28 +010011994 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11995 pipe_config->has_audio,
11996 pipe_config->has_infoframe);
11997
Daniel Vetterc0b03412013-05-28 12:05:54 +020011998 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011999 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012000 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012001 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
12002 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030012003 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012004 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
12005 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010012006 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
12007 crtc->num_scalers,
12008 pipe_config->scaler_state.scaler_users,
12009 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020012010 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
12011 pipe_config->gmch_pfit.control,
12012 pipe_config->gmch_pfit.pgm_ratios,
12013 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012014 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020012015 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012016 pipe_config->pch_pfit.size,
12017 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012018 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030012019 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012020
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012021 if (IS_BROXTON(dev)) {
Imre Deak05712c12015-06-18 17:25:54 +030012022 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, ebb4: 0x%x,"
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012023 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
Imre Deakc8453332015-06-18 17:25:55 +030012024 "pll6: 0x%x, pll8: 0x%x, pll9: 0x%x, pll10: 0x%x, pcsdw12: 0x%x\n",
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012025 pipe_config->ddi_pll_sel,
12026 pipe_config->dpll_hw_state.ebb0,
Imre Deak05712c12015-06-18 17:25:54 +030012027 pipe_config->dpll_hw_state.ebb4,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012028 pipe_config->dpll_hw_state.pll0,
12029 pipe_config->dpll_hw_state.pll1,
12030 pipe_config->dpll_hw_state.pll2,
12031 pipe_config->dpll_hw_state.pll3,
12032 pipe_config->dpll_hw_state.pll6,
12033 pipe_config->dpll_hw_state.pll8,
Imre Deak05712c12015-06-18 17:25:54 +030012034 pipe_config->dpll_hw_state.pll9,
Imre Deakc8453332015-06-18 17:25:55 +030012035 pipe_config->dpll_hw_state.pll10,
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010012036 pipe_config->dpll_hw_state.pcsdw12);
12037 } else if (IS_SKYLAKE(dev)) {
12038 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
12039 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
12040 pipe_config->ddi_pll_sel,
12041 pipe_config->dpll_hw_state.ctrl1,
12042 pipe_config->dpll_hw_state.cfgcr1,
12043 pipe_config->dpll_hw_state.cfgcr2);
12044 } else if (HAS_DDI(dev)) {
12045 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
12046 pipe_config->ddi_pll_sel,
12047 pipe_config->dpll_hw_state.wrpll);
12048 } else {
12049 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
12050 "fp0: 0x%x, fp1: 0x%x\n",
12051 pipe_config->dpll_hw_state.dpll,
12052 pipe_config->dpll_hw_state.dpll_md,
12053 pipe_config->dpll_hw_state.fp0,
12054 pipe_config->dpll_hw_state.fp1);
12055 }
12056
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012057 DRM_DEBUG_KMS("planes on this crtc\n");
12058 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12059 intel_plane = to_intel_plane(plane);
12060 if (intel_plane->pipe != crtc->pipe)
12061 continue;
12062
12063 state = to_intel_plane_state(plane->state);
12064 fb = state->base.fb;
12065 if (!fb) {
12066 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12067 "disabled, scaler_id = %d\n",
12068 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12069 plane->base.id, intel_plane->pipe,
12070 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12071 drm_plane_index(plane), state->scaler_id);
12072 continue;
12073 }
12074
12075 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12076 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12077 plane->base.id, intel_plane->pipe,
12078 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12079 drm_plane_index(plane));
12080 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12081 fb->base.id, fb->width, fb->height, fb->pixel_format);
12082 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12083 state->scaler_id,
12084 state->src.x1 >> 16, state->src.y1 >> 16,
12085 drm_rect_width(&state->src) >> 16,
12086 drm_rect_height(&state->src) >> 16,
12087 state->dst.x1, state->dst.y1,
12088 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12089 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012090}
12091
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012092static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012093{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012094 struct drm_device *dev = state->dev;
12095 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012096 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012097 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012098 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012099 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012100
12101 /*
12102 * Walk the connector list instead of the encoder
12103 * list to detect the problem on ddi platforms
12104 * where there's just one encoder per digital port.
12105 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012106 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012107 if (!connector_state->best_encoder)
12108 continue;
12109
12110 encoder = to_intel_encoder(connector_state->best_encoder);
12111
12112 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012113
12114 switch (encoder->type) {
12115 unsigned int port_mask;
12116 case INTEL_OUTPUT_UNKNOWN:
12117 if (WARN_ON(!HAS_DDI(dev)))
12118 break;
12119 case INTEL_OUTPUT_DISPLAYPORT:
12120 case INTEL_OUTPUT_HDMI:
12121 case INTEL_OUTPUT_EDP:
12122 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12123
12124 /* the same port mustn't appear more than once */
12125 if (used_ports & port_mask)
12126 return false;
12127
12128 used_ports |= port_mask;
12129 default:
12130 break;
12131 }
12132 }
12133
12134 return true;
12135}
12136
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012137static void
12138clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12139{
12140 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012141 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012142 struct intel_dpll_hw_state dpll_hw_state;
12143 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012144 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012145
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012146 /* FIXME: before the switch to atomic started, a new pipe_config was
12147 * kzalloc'd. Code that depends on any field being zero should be
12148 * fixed, so that the crtc_state can be safely duplicated. For now,
12149 * only fields that are know to not cause problems are preserved. */
12150
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012151 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012152 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012153 shared_dpll = crtc_state->shared_dpll;
12154 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012155 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012156
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012157 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012158
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012159 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012160 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012161 crtc_state->shared_dpll = shared_dpll;
12162 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012163 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012164}
12165
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012166static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012167intel_modeset_pipe_config(struct drm_crtc *crtc,
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012168 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012169{
Maarten Lankhorstb3592832015-06-15 12:33:38 +020012170 struct drm_atomic_state *state = pipe_config->base.state;
Daniel Vetter7758a112012-07-08 19:40:39 +020012171 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012172 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012173 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012174 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012175 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012176 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012177
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012178 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012179
Daniel Vettere143a212013-07-04 12:01:15 +020012180 pipe_config->cpu_transcoder =
12181 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012182
Imre Deak2960bc92013-07-30 13:36:32 +030012183 /*
12184 * Sanitize sync polarity flags based on requested ones. If neither
12185 * positive or negative polarity is requested, treat this as meaning
12186 * negative polarity.
12187 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012188 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012189 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012190 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012191
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012192 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012193 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012194 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012195
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012196 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12197 * plane pixel format and any sink constraints into account. Returns the
12198 * source plane bpp so that dithering can be selected on mismatches
12199 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012200 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12201 pipe_config);
12202 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012203 goto fail;
12204
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012205 /*
12206 * Determine the real pipe dimensions. Note that stereo modes can
12207 * increase the actual pipe size due to the frame doubling and
12208 * insertion of additional space for blanks between the frame. This
12209 * is stored in the crtc timings. We use the requested mode to do this
12210 * computation to clearly distinguish it from the adjusted mode, which
12211 * can be changed by the connectors in the below retry loop.
12212 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012213 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012214 &pipe_config->pipe_src_w,
12215 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012216
Daniel Vettere29c22c2013-02-21 00:00:16 +010012217encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012218 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012219 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012220 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012221
Daniel Vetter135c81b2013-07-21 21:37:09 +020012222 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012223 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12224 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012225
Daniel Vetter7758a112012-07-08 19:40:39 +020012226 /* Pass our mode to the connectors and the CRTC to give them a chance to
12227 * adjust it according to limitations or connector properties, and also
12228 * a chance to reject the mode entirely.
12229 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012230 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012231 if (connector_state->crtc != crtc)
12232 continue;
12233
12234 encoder = to_intel_encoder(connector_state->best_encoder);
12235
Daniel Vetterefea6e82013-07-21 21:36:59 +020012236 if (!(encoder->compute_config(encoder, pipe_config))) {
12237 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012238 goto fail;
12239 }
12240 }
12241
Daniel Vetterff9a6752013-06-01 17:16:21 +020012242 /* Set default port clock if not overwritten by the encoder. Needs to be
12243 * done afterwards in case the encoder adjusts the mode. */
12244 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012245 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012246 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012247
Daniel Vettera43f6e02013-06-07 23:10:32 +020012248 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012249 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012250 DRM_DEBUG_KMS("CRTC fixup failed\n");
12251 goto fail;
12252 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012253
12254 if (ret == RETRY) {
12255 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12256 ret = -EINVAL;
12257 goto fail;
12258 }
12259
12260 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12261 retry = false;
12262 goto encoder_retry;
12263 }
12264
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012265 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012266 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012267 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012268
Daniel Vetter7758a112012-07-08 19:40:39 +020012269fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012270 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012271}
12272
Daniel Vetterea9d7582012-07-10 10:42:52 +020012273static bool intel_crtc_in_use(struct drm_crtc *crtc)
12274{
12275 struct drm_encoder *encoder;
12276 struct drm_device *dev = crtc->dev;
12277
12278 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12279 if (encoder->crtc == crtc)
12280 return true;
12281
12282 return false;
12283}
12284
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012285static void
12286intel_modeset_update_state(struct drm_atomic_state *state)
12287{
12288 struct drm_device *dev = state->dev;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012289 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012290 struct drm_crtc *crtc;
12291 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012292 struct drm_connector *connector;
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012293 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012294
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020012295 intel_shared_dpll_commit(state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012296
Damien Lespiaub2784e12014-08-05 11:29:37 +010012297 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012298 if (!intel_encoder->base.crtc)
12299 continue;
12300
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012301 crtc = intel_encoder->base.crtc;
12302 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12303 if (!crtc_state || !needs_modeset(crtc->state))
12304 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012305
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012306 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012307 }
12308
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012309 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
Maarten Lankhorstf7217902015-06-10 10:24:20 +020012310 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012311
Ville Syrjälä76688512014-01-10 11:28:06 +020012312 /* Double check state. */
Maarten Lankhorst8a75d157c2015-07-13 16:30:14 +020012313 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012314 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012315
12316 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Maarten Lankhorstfc467a222015-06-01 12:50:07 +020012317
12318 /* Update hwmode for vblank functions */
12319 if (crtc->state->active)
12320 crtc->hwmode = crtc->state->adjusted_mode;
12321 else
12322 crtc->hwmode.crtc_clock = 0;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012323 }
12324
12325 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12326 if (!connector->encoder || !connector->encoder->crtc)
12327 continue;
12328
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012329 crtc = connector->encoder->crtc;
12330 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12331 if (!crtc_state || !needs_modeset(crtc->state))
12332 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012333
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012334 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012335 struct drm_property *dpms_property =
12336 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012337
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012338 connector->dpms = DRM_MODE_DPMS_ON;
12339 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012340
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012341 intel_encoder = to_intel_encoder(connector->encoder);
12342 intel_encoder->connectors_active = true;
12343 } else
12344 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012345 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012346}
12347
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012348static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012349{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012350 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012351
12352 if (clock1 == clock2)
12353 return true;
12354
12355 if (!clock1 || !clock2)
12356 return false;
12357
12358 diff = abs(clock1 - clock2);
12359
12360 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12361 return true;
12362
12363 return false;
12364}
12365
Daniel Vetter25c5b262012-07-08 22:08:04 +020012366#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12367 list_for_each_entry((intel_crtc), \
12368 &(dev)->mode_config.crtc_list, \
12369 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012370 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012371
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012372
12373static bool
12374intel_compare_m_n(unsigned int m, unsigned int n,
12375 unsigned int m2, unsigned int n2,
12376 bool exact)
12377{
12378 if (m == m2 && n == n2)
12379 return true;
12380
12381 if (exact || !m || !n || !m2 || !n2)
12382 return false;
12383
12384 BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
12385
12386 if (m > m2) {
12387 while (m > m2) {
12388 m2 <<= 1;
12389 n2 <<= 1;
12390 }
12391 } else if (m < m2) {
12392 while (m < m2) {
12393 m <<= 1;
12394 n <<= 1;
12395 }
12396 }
12397
12398 return m == m2 && n == n2;
12399}
12400
12401static bool
12402intel_compare_link_m_n(const struct intel_link_m_n *m_n,
12403 struct intel_link_m_n *m2_n2,
12404 bool adjust)
12405{
12406 if (m_n->tu == m2_n2->tu &&
12407 intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
12408 m2_n2->gmch_m, m2_n2->gmch_n, !adjust) &&
12409 intel_compare_m_n(m_n->link_m, m_n->link_n,
12410 m2_n2->link_m, m2_n2->link_n, !adjust)) {
12411 if (adjust)
12412 *m2_n2 = *m_n;
12413
12414 return true;
12415 }
12416
12417 return false;
12418}
12419
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012420static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012421intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012422 struct intel_crtc_state *current_config,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012423 struct intel_crtc_state *pipe_config,
12424 bool adjust)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012425{
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012426 bool ret = true;
12427
12428#define INTEL_ERR_OR_DBG_KMS(fmt, ...) \
12429 do { \
12430 if (!adjust) \
12431 DRM_ERROR(fmt, ##__VA_ARGS__); \
12432 else \
12433 DRM_DEBUG_KMS(fmt, ##__VA_ARGS__); \
12434 } while (0)
12435
Daniel Vetter66e985c2013-06-05 13:34:20 +020012436#define PIPE_CONF_CHECK_X(name) \
12437 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012438 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012439 "(expected 0x%08x, found 0x%08x)\n", \
12440 current_config->name, \
12441 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012442 ret = false; \
Daniel Vetter66e985c2013-06-05 13:34:20 +020012443 }
12444
Daniel Vetter08a24032013-04-19 11:25:34 +020012445#define PIPE_CONF_CHECK_I(name) \
12446 if (current_config->name != pipe_config->name) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012447 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Daniel Vetter08a24032013-04-19 11:25:34 +020012448 "(expected %i, found %i)\n", \
12449 current_config->name, \
12450 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012451 ret = false; \
12452 }
12453
12454#define PIPE_CONF_CHECK_M_N(name) \
12455 if (!intel_compare_link_m_n(&current_config->name, \
12456 &pipe_config->name,\
12457 adjust)) { \
12458 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12459 "(expected tu %i gmch %i/%i link %i/%i, " \
12460 "found tu %i, gmch %i/%i link %i/%i)\n", \
12461 current_config->name.tu, \
12462 current_config->name.gmch_m, \
12463 current_config->name.gmch_n, \
12464 current_config->name.link_m, \
12465 current_config->name.link_n, \
12466 pipe_config->name.tu, \
12467 pipe_config->name.gmch_m, \
12468 pipe_config->name.gmch_n, \
12469 pipe_config->name.link_m, \
12470 pipe_config->name.link_n); \
12471 ret = false; \
12472 }
12473
12474#define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) \
12475 if (!intel_compare_link_m_n(&current_config->name, \
12476 &pipe_config->name, adjust) && \
12477 !intel_compare_link_m_n(&current_config->alt_name, \
12478 &pipe_config->name, adjust)) { \
12479 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
12480 "(expected tu %i gmch %i/%i link %i/%i, " \
12481 "or 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 current_config->alt_name.tu, \
12489 current_config->alt_name.gmch_m, \
12490 current_config->alt_name.gmch_n, \
12491 current_config->alt_name.link_m, \
12492 current_config->alt_name.link_n, \
12493 pipe_config->name.tu, \
12494 pipe_config->name.gmch_m, \
12495 pipe_config->name.gmch_n, \
12496 pipe_config->name.link_m, \
12497 pipe_config->name.link_n); \
12498 ret = false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012499 }
12500
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012501/* This is required for BDW+ where there is only one set of registers for
12502 * switching between high and low RR.
12503 * This macro can be used whenever a comparison has to be made between one
12504 * hw state and multiple sw state variables.
12505 */
12506#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12507 if ((current_config->name != pipe_config->name) && \
12508 (current_config->alt_name != pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012509 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012510 "(expected %i or %i, found %i)\n", \
12511 current_config->name, \
12512 current_config->alt_name, \
12513 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012514 ret = false; \
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012515 }
12516
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012517#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12518 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012519 INTEL_ERR_OR_DBG_KMS("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012520 "(expected %i, found %i)\n", \
12521 current_config->name & (mask), \
12522 pipe_config->name & (mask)); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012523 ret = false; \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012524 }
12525
Ville Syrjälä5e550652013-09-06 23:29:07 +030012526#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12527 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012528 INTEL_ERR_OR_DBG_KMS("mismatch in " #name " " \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012529 "(expected %i, found %i)\n", \
12530 current_config->name, \
12531 pipe_config->name); \
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012532 ret = false; \
Ville Syrjälä5e550652013-09-06 23:29:07 +030012533 }
12534
Daniel Vetterbb760062013-06-06 14:55:52 +020012535#define PIPE_CONF_QUIRK(quirk) \
12536 ((current_config->quirks | pipe_config->quirks) & (quirk))
12537
Daniel Vettereccb1402013-05-22 00:50:22 +020012538 PIPE_CONF_CHECK_I(cpu_transcoder);
12539
Daniel Vetter08a24032013-04-19 11:25:34 +020012540 PIPE_CONF_CHECK_I(has_pch_encoder);
12541 PIPE_CONF_CHECK_I(fdi_lanes);
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012542 PIPE_CONF_CHECK_M_N(fdi_m_n);
Daniel Vetter08a24032013-04-19 11:25:34 +020012543
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012544 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012545
12546 if (INTEL_INFO(dev)->gen < 8) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012547 PIPE_CONF_CHECK_M_N(dp_m_n);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012548
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012549 PIPE_CONF_CHECK_I(has_drrs);
12550 if (current_config->has_drrs)
12551 PIPE_CONF_CHECK_M_N(dp_m2_n2);
12552 } else
12553 PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012554
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012555 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12556 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12557 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12558 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12559 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12560 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012561
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012562 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12563 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12564 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12565 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12566 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12567 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012568
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012569 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012570 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012571 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12572 IS_VALLEYVIEW(dev))
12573 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012574 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012575
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012576 PIPE_CONF_CHECK_I(has_audio);
12577
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012578 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012579 DRM_MODE_FLAG_INTERLACE);
12580
Daniel Vetterbb760062013-06-06 14:55:52 +020012581 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012582 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012583 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012584 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012585 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012586 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012587 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012588 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012589 DRM_MODE_FLAG_NVSYNC);
12590 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012591
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012592 PIPE_CONF_CHECK_I(pipe_src_w);
12593 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012594
Daniel Vetter99535992014-04-13 12:00:33 +020012595 /*
12596 * FIXME: BIOS likes to set up a cloned config with lvds+external
12597 * screen. Since we don't yet re-compute the pipe config when moving
12598 * just the lvds port away to another pipe the sw tracking won't match.
12599 *
12600 * Proper atomic modesets with recomputed global state will fix this.
12601 * Until then just don't check gmch state for inherited modes.
12602 */
12603 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12604 PIPE_CONF_CHECK_I(gmch_pfit.control);
12605 /* pfit ratios are autocomputed by the hw on gen4+ */
12606 if (INTEL_INFO(dev)->gen < 4)
12607 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12608 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12609 }
12610
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012611 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12612 if (current_config->pch_pfit.enabled) {
12613 PIPE_CONF_CHECK_I(pch_pfit.pos);
12614 PIPE_CONF_CHECK_I(pch_pfit.size);
12615 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012616
Chandra Kondurua1b22782015-04-07 15:28:45 -070012617 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12618
Jesse Barnese59150d2014-01-07 13:30:45 -080012619 /* BDW+ don't expose a synchronous way to read the state */
12620 if (IS_HASWELL(dev))
12621 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012622
Ville Syrjälä282740f2013-09-04 18:30:03 +030012623 PIPE_CONF_CHECK_I(double_wide);
12624
Daniel Vetter26804af2014-06-25 22:01:55 +030012625 PIPE_CONF_CHECK_X(ddi_pll_sel);
12626
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012627 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012628 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012629 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012630 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12631 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012632 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012633 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12634 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12635 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012636
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012637 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12638 PIPE_CONF_CHECK_I(pipe_bpp);
12639
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012640 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012641 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012642
Daniel Vetter66e985c2013-06-05 13:34:20 +020012643#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012644#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012645#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012646#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012647#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012648#undef PIPE_CONF_QUIRK
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012649#undef INTEL_ERR_OR_DBG_KMS
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012650
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012651 return ret;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012652}
12653
Damien Lespiau08db6652014-11-04 17:06:52 +000012654static void check_wm_state(struct drm_device *dev)
12655{
12656 struct drm_i915_private *dev_priv = dev->dev_private;
12657 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12658 struct intel_crtc *intel_crtc;
12659 int plane;
12660
12661 if (INTEL_INFO(dev)->gen < 9)
12662 return;
12663
12664 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12665 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12666
12667 for_each_intel_crtc(dev, intel_crtc) {
12668 struct skl_ddb_entry *hw_entry, *sw_entry;
12669 const enum pipe pipe = intel_crtc->pipe;
12670
12671 if (!intel_crtc->active)
12672 continue;
12673
12674 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012675 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012676 hw_entry = &hw_ddb.plane[pipe][plane];
12677 sw_entry = &sw_ddb->plane[pipe][plane];
12678
12679 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12680 continue;
12681
12682 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12683 "(expected (%u,%u), found (%u,%u))\n",
12684 pipe_name(pipe), plane + 1,
12685 sw_entry->start, sw_entry->end,
12686 hw_entry->start, hw_entry->end);
12687 }
12688
12689 /* cursor */
12690 hw_entry = &hw_ddb.cursor[pipe];
12691 sw_entry = &sw_ddb->cursor[pipe];
12692
12693 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12694 continue;
12695
12696 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12697 "(expected (%u,%u), found (%u,%u))\n",
12698 pipe_name(pipe),
12699 sw_entry->start, sw_entry->end,
12700 hw_entry->start, hw_entry->end);
12701 }
12702}
12703
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012704static void
12705check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012706{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012707 struct intel_connector *connector;
12708
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012709 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012710 /* This also checks the encoder/connector hw state with the
12711 * ->get_hw_state callbacks. */
12712 intel_connector_check_state(connector);
12713
Rob Clarke2c719b2014-12-15 13:56:32 -050012714 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012715 "connector's staged encoder doesn't match current encoder\n");
12716 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012717}
12718
12719static void
12720check_encoder_state(struct drm_device *dev)
12721{
12722 struct intel_encoder *encoder;
12723 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012724
Damien Lespiaub2784e12014-08-05 11:29:37 +010012725 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012726 bool enabled = false;
12727 bool active = false;
12728 enum pipe pipe, tracked_pipe;
12729
12730 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12731 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012732 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012733
Rob Clarke2c719b2014-12-15 13:56:32 -050012734 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012735 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012736 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012737 "encoder's active_connectors set, but no crtc\n");
12738
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012739 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012740 if (connector->base.encoder != &encoder->base)
12741 continue;
12742 enabled = true;
12743 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12744 active = true;
12745 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012746 /*
12747 * for MST connectors if we unplug the connector is gone
12748 * away but the encoder is still connected to a crtc
12749 * until a modeset happens in response to the hotplug.
12750 */
12751 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12752 continue;
12753
Rob Clarke2c719b2014-12-15 13:56:32 -050012754 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012755 "encoder's enabled state mismatch "
12756 "(expected %i, found %i)\n",
12757 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012758 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012759 "active encoder with no crtc\n");
12760
Rob Clarke2c719b2014-12-15 13:56:32 -050012761 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012762 "encoder's computed active state doesn't match tracked active state "
12763 "(expected %i, found %i)\n", active, encoder->connectors_active);
12764
12765 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012766 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012767 "encoder's hw state doesn't match sw tracking "
12768 "(expected %i, found %i)\n",
12769 encoder->connectors_active, active);
12770
12771 if (!encoder->base.crtc)
12772 continue;
12773
12774 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012775 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012776 "active encoder's pipe doesn't match"
12777 "(expected %i, found %i)\n",
12778 tracked_pipe, pipe);
12779
12780 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012781}
12782
12783static void
12784check_crtc_state(struct drm_device *dev)
12785{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012786 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012787 struct intel_crtc *crtc;
12788 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012789 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012790
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012791 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012792 bool enabled = false;
12793 bool active = false;
12794
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012795 memset(&pipe_config, 0, sizeof(pipe_config));
12796
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012797 DRM_DEBUG_KMS("[CRTC:%d]\n",
12798 crtc->base.base.id);
12799
Matt Roper83d65732015-02-25 13:12:16 -080012800 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012801 "active crtc, but not enabled in sw tracking\n");
12802
Damien Lespiaub2784e12014-08-05 11:29:37 +010012803 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012804 if (encoder->base.crtc != &crtc->base)
12805 continue;
12806 enabled = true;
12807 if (encoder->connectors_active)
12808 active = true;
12809 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012810
Rob Clarke2c719b2014-12-15 13:56:32 -050012811 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012812 "crtc's computed active state doesn't match tracked active state "
12813 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012814 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012815 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012816 "(expected %i, found %i)\n", enabled,
12817 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012818
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012819 active = dev_priv->display.get_pipe_config(crtc,
12820 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012821
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012822 /* hw state is inconsistent with the pipe quirk */
12823 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12824 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012825 active = crtc->active;
12826
Damien Lespiaub2784e12014-08-05 11:29:37 +010012827 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012828 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012829 if (encoder->base.crtc != &crtc->base)
12830 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012831 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012832 encoder->get_config(encoder, &pipe_config);
12833 }
12834
Rob Clarke2c719b2014-12-15 13:56:32 -050012835 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012836 "crtc active state doesn't match with hw state "
12837 "(expected %i, found %i)\n", crtc->active, active);
12838
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012839 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12840 "transitional active state does not match atomic hw state "
12841 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12842
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020012843 if (!active)
12844 continue;
12845
12846 if (!intel_pipe_config_compare(dev, crtc->config,
12847 &pipe_config, false)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012848 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012849 intel_dump_pipe_config(crtc, &pipe_config,
12850 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012851 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012852 "[sw state]");
12853 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012854 }
12855}
12856
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012857static void
12858check_shared_dpll_state(struct drm_device *dev)
12859{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012860 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012861 struct intel_crtc *crtc;
12862 struct intel_dpll_hw_state dpll_hw_state;
12863 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012864
12865 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12866 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12867 int enabled_crtcs = 0, active_crtcs = 0;
12868 bool active;
12869
12870 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12871
12872 DRM_DEBUG_KMS("%s\n", pll->name);
12873
12874 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12875
Rob Clarke2c719b2014-12-15 13:56:32 -050012876 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012877 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012878 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012879 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012880 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012881 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012882 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012883 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012884 "pll on state mismatch (expected %i, found %i)\n",
12885 pll->on, active);
12886
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012887 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012888 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012889 enabled_crtcs++;
12890 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12891 active_crtcs++;
12892 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012893 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012894 "pll active crtcs mismatch (expected %i, found %i)\n",
12895 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012896 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012897 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012898 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012899
Rob Clarke2c719b2014-12-15 13:56:32 -050012900 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012901 sizeof(dpll_hw_state)),
12902 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012903 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012904}
12905
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012906void
12907intel_modeset_check_state(struct drm_device *dev)
12908{
Damien Lespiau08db6652014-11-04 17:06:52 +000012909 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012910 check_connector_state(dev);
12911 check_encoder_state(dev);
12912 check_crtc_state(dev);
12913 check_shared_dpll_state(dev);
12914}
12915
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012916void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012917 int dotclock)
12918{
12919 /*
12920 * FDI already provided one idea for the dotclock.
12921 * Yell if the encoder disagrees.
12922 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012923 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012924 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012925 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012926}
12927
Ville Syrjälä80715b22014-05-15 20:23:23 +030012928static void update_scanline_offset(struct intel_crtc *crtc)
12929{
12930 struct drm_device *dev = crtc->base.dev;
12931
12932 /*
12933 * The scanline counter increments at the leading edge of hsync.
12934 *
12935 * On most platforms it starts counting from vtotal-1 on the
12936 * first active line. That means the scanline counter value is
12937 * always one less than what we would expect. Ie. just after
12938 * start of vblank, which also occurs at start of hsync (on the
12939 * last active line), the scanline counter will read vblank_start-1.
12940 *
12941 * On gen2 the scanline counter starts counting from 1 instead
12942 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12943 * to keep the value positive), instead of adding one.
12944 *
12945 * On HSW+ the behaviour of the scanline counter depends on the output
12946 * type. For DP ports it behaves like most other platforms, but on HDMI
12947 * there's an extra 1 line difference. So we need to add two instead of
12948 * one to the value.
12949 */
12950 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012951 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012952 int vtotal;
12953
12954 vtotal = mode->crtc_vtotal;
12955 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12956 vtotal /= 2;
12957
12958 crtc->scanline_offset = vtotal - 1;
12959 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012960 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012961 crtc->scanline_offset = 2;
12962 } else
12963 crtc->scanline_offset = 1;
12964}
12965
Maarten Lankhorstad421372015-06-15 12:33:42 +020012966static void intel_modeset_clear_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012967{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012968 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012969 struct drm_i915_private *dev_priv = to_i915(dev);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012970 struct intel_shared_dpll_config *shared_dpll = NULL;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012971 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012972 struct intel_crtc_state *intel_crtc_state;
12973 struct drm_crtc *crtc;
12974 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012975 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012976
12977 if (!dev_priv->display.crtc_compute_clock)
Maarten Lankhorstad421372015-06-15 12:33:42 +020012978 return;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012979
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012980 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstad421372015-06-15 12:33:42 +020012981 int dpll;
12982
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012983 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012984 intel_crtc_state = to_intel_crtc_state(crtc_state);
Maarten Lankhorstad421372015-06-15 12:33:42 +020012985 dpll = intel_crtc_state->shared_dpll;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012986
Maarten Lankhorstad421372015-06-15 12:33:42 +020012987 if (!needs_modeset(crtc_state) || dpll == DPLL_ID_PRIVATE)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012988 continue;
12989
Maarten Lankhorstad421372015-06-15 12:33:42 +020012990 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012991
Maarten Lankhorstad421372015-06-15 12:33:42 +020012992 if (!shared_dpll)
12993 shared_dpll = intel_atomic_get_shared_dpll_state(state);
12994
12995 shared_dpll[dpll].crtc_mask &= ~(1 << intel_crtc->pipe);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012996 }
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012997}
12998
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020012999/*
13000 * This implements the workaround described in the "notes" section of the mode
13001 * set sequence documentation. When going from no pipes or single pipe to
13002 * multiple pipes, and planes are enabled after the pipe, we need to wait at
13003 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
13004 */
13005static int haswell_mode_set_planes_workaround(struct drm_atomic_state *state)
13006{
13007 struct drm_crtc_state *crtc_state;
13008 struct intel_crtc *intel_crtc;
13009 struct drm_crtc *crtc;
13010 struct intel_crtc_state *first_crtc_state = NULL;
13011 struct intel_crtc_state *other_crtc_state = NULL;
13012 enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
13013 int i;
13014
13015 /* look at all crtc's that are going to be enabled in during modeset */
13016 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13017 intel_crtc = to_intel_crtc(crtc);
13018
13019 if (!crtc_state->active || !needs_modeset(crtc_state))
13020 continue;
13021
13022 if (first_crtc_state) {
13023 other_crtc_state = to_intel_crtc_state(crtc_state);
13024 break;
13025 } else {
13026 first_crtc_state = to_intel_crtc_state(crtc_state);
13027 first_pipe = intel_crtc->pipe;
13028 }
13029 }
13030
13031 /* No workaround needed? */
13032 if (!first_crtc_state)
13033 return 0;
13034
13035 /* w/a possibly needed, check how many crtc's are already enabled. */
13036 for_each_intel_crtc(state->dev, intel_crtc) {
13037 struct intel_crtc_state *pipe_config;
13038
13039 pipe_config = intel_atomic_get_crtc_state(state, intel_crtc);
13040 if (IS_ERR(pipe_config))
13041 return PTR_ERR(pipe_config);
13042
13043 pipe_config->hsw_workaround_pipe = INVALID_PIPE;
13044
13045 if (!pipe_config->base.active ||
13046 needs_modeset(&pipe_config->base))
13047 continue;
13048
13049 /* 2 or more enabled crtcs means no need for w/a */
13050 if (enabled_pipe != INVALID_PIPE)
13051 return 0;
13052
13053 enabled_pipe = intel_crtc->pipe;
13054 }
13055
13056 if (enabled_pipe != INVALID_PIPE)
13057 first_crtc_state->hsw_workaround_pipe = enabled_pipe;
13058 else if (other_crtc_state)
13059 other_crtc_state->hsw_workaround_pipe = first_pipe;
13060
13061 return 0;
13062}
13063
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013064static int intel_modeset_all_pipes(struct drm_atomic_state *state)
13065{
13066 struct drm_crtc *crtc;
13067 struct drm_crtc_state *crtc_state;
13068 int ret = 0;
13069
13070 /* add all active pipes to the state */
13071 for_each_crtc(state->dev, crtc) {
13072 crtc_state = drm_atomic_get_crtc_state(state, crtc);
13073 if (IS_ERR(crtc_state))
13074 return PTR_ERR(crtc_state);
13075
13076 if (!crtc_state->active || needs_modeset(crtc_state))
13077 continue;
13078
13079 crtc_state->mode_changed = true;
13080
13081 ret = drm_atomic_add_affected_connectors(state, crtc);
13082 if (ret)
13083 break;
13084
13085 ret = drm_atomic_add_affected_planes(state, crtc);
13086 if (ret)
13087 break;
13088 }
13089
13090 return ret;
13091}
13092
13093
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013094/* Code that should eventually be part of atomic_check() */
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013095static int intel_modeset_checks(struct drm_atomic_state *state)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013096{
13097 struct drm_device *dev = state->dev;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013098 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013099 int ret;
13100
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013101 if (!check_digital_port_conflicts(state)) {
13102 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
13103 return -EINVAL;
13104 }
13105
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013106 /*
13107 * See if the config requires any additional preparation, e.g.
13108 * to adjust global state with pipes off. We need to do this
13109 * here so we can get the modeset_pipe updated config for the new
13110 * mode set on this crtc. For other crtcs we need to use the
13111 * adjusted_mode bits in the crtc directly.
13112 */
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013113 if (dev_priv->display.modeset_calc_cdclk) {
13114 unsigned int cdclk;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030013115
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013116 ret = dev_priv->display.modeset_calc_cdclk(state);
13117
13118 cdclk = to_intel_atomic_state(state)->cdclk;
13119 if (!ret && cdclk != dev_priv->cdclk_freq)
13120 ret = intel_modeset_all_pipes(state);
13121
13122 if (ret < 0)
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013123 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013124 } else
13125 to_intel_atomic_state(state)->cdclk = dev_priv->cdclk_freq;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013126
Maarten Lankhorstad421372015-06-15 12:33:42 +020013127 intel_modeset_clear_plls(state);
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013128
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013129 if (IS_HASWELL(dev))
Maarten Lankhorstad421372015-06-15 12:33:42 +020013130 return haswell_mode_set_planes_workaround(state);
Maarten Lankhorst99d736a2015-06-01 12:50:09 +020013131
Maarten Lankhorstad421372015-06-15 12:33:42 +020013132 return 0;
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013133}
13134
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013135static int
13136intel_modeset_compute_config(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013137{
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013138 struct drm_crtc *crtc;
13139 struct drm_crtc_state *crtc_state;
13140 int ret, i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013141 bool any_ms = false;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013142
13143 ret = drm_atomic_helper_check_modeset(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013144 if (ret)
13145 return ret;
13146
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013147 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013148 struct intel_crtc_state *pipe_config =
13149 to_intel_crtc_state(crtc_state);
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013150 bool modeset, recalc = false;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013151
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013152 if (!crtc_state->enable) {
13153 if (needs_modeset(crtc_state))
13154 any_ms = true;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013155 continue;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013156 }
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013157
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013158 modeset = needs_modeset(crtc_state);
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013159 /* see comment in intel_modeset_readout_hw_state */
13160 if (!modeset && crtc_state->mode_blob != crtc->state->mode_blob &&
13161 pipe_config->quirks & PIPE_CONFIG_QUIRK_INHERITED_MODE)
13162 recalc = true;
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013163
13164 if (!modeset && !recalc)
13165 continue;
13166
13167 if (recalc) {
Maarten Lankhorstb3592832015-06-15 12:33:38 +020013168 ret = drm_atomic_add_affected_connectors(state, crtc);
13169 if (ret)
13170 return ret;
13171 }
13172
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013173 ret = intel_modeset_pipe_config(crtc, pipe_config);
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013174 if (ret)
13175 return ret;
13176
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013177 if (recalc && (!i915.fastboot ||
13178 !intel_pipe_config_compare(state->dev,
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013179 to_intel_crtc_state(crtc->state),
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020013180 pipe_config, true))) {
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013181 modeset = crtc_state->mode_changed = true;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013182
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013183 ret = drm_atomic_add_affected_planes(state, crtc);
13184 if (ret)
13185 return ret;
13186 }
13187
13188 any_ms = modeset;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013189 intel_dump_pipe_config(to_intel_crtc(crtc),
Maarten Lankhorstcfb23ed2015-07-14 12:17:40 +020013190 pipe_config,
13191 modeset ? "[modeset]" : "[fastboot]");
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013192 }
13193
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013194 if (any_ms) {
13195 ret = intel_modeset_checks(state);
13196
13197 if (ret)
13198 return ret;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013199 } else
13200 to_intel_atomic_state(state)->cdclk =
13201 to_i915(state->dev)->cdclk_freq;
Ander Conselvan de Oliveirac347a672015-06-01 12:50:02 +020013202
13203 return drm_atomic_helper_check_planes(state->dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013204}
13205
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013206static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020013207{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020013208 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013209 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013210 struct drm_crtc *crtc;
13211 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013212 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013213 int i;
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013214 bool any_ms = false;
Daniel Vettera6778b32012-07-02 09:56:42 +020013215
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013216 ret = drm_atomic_helper_prepare_planes(dev, state);
13217 if (ret)
13218 return ret;
13219
Maarten Lankhorst1c5e19f2015-06-01 12:50:06 +020013220 drm_atomic_helper_swap_state(dev, state);
13221
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013222 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13224
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013225 if (!needs_modeset(crtc->state))
13226 continue;
13227
13228 any_ms = true;
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013229 intel_pre_plane_update(intel_crtc);
Daniel Vetter460da9162013-03-27 00:44:51 +010013230
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013231 if (crtc_state->active) {
13232 intel_crtc_disable_planes(crtc, crtc_state->plane_mask);
13233 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorsteddfcbc2015-06-15 12:33:53 +020013234 intel_crtc->active = false;
13235 intel_disable_shared_dpll(intel_crtc);
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013236 }
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013237 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013238
Daniel Vetterea9d7582012-07-10 10:42:52 +020013239 /* Only after disabling all output pipelines that will be changed can we
13240 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013241 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013242
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013243 /* The state has been swaped above, so state actually contains the
13244 * old state now. */
Maarten Lankhorst61333b62015-06-15 12:33:50 +020013245 if (any_ms)
13246 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013247
Daniel Vettera6778b32012-07-02 09:56:42 +020013248 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
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 if (needs_modeset(crtc->state) && crtc->state->active) {
13251 update_scanline_offset(to_intel_crtc(crtc));
13252 dev_priv->display.crtc_enable(crtc);
13253 }
13254
Maarten Lankhorst5ac1c4b2015-06-01 12:50:01 +020013255 drm_atomic_helper_commit_planes_on_crtc(crtc_state);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013256 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013257
Daniel Vettera6778b32012-07-02 09:56:42 +020013258 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013259
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013260 drm_atomic_helper_cleanup_planes(dev, state);
13261
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013262 drm_atomic_state_free(state);
13263
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013264 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013265}
13266
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013267static int intel_set_mode_checked(struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013268{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013269 struct drm_device *dev = state->dev;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013270 int ret;
13271
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013272 ret = __intel_set_mode(state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013273 if (ret == 0)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013274 intel_modeset_check_state(dev);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013275
13276 return ret;
13277}
13278
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013279static int intel_set_mode(struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013280{
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013281 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013282
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013283 ret = intel_modeset_compute_config(state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013284 if (ret)
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013285 return ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013286
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013287 return intel_set_mode_checked(state);
Daniel Vetterf30da182013-04-11 20:22:50 +020013288}
13289
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013290void intel_crtc_restore_mode(struct drm_crtc *crtc)
13291{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013292 struct drm_device *dev = crtc->dev;
13293 struct drm_atomic_state *state;
13294 struct intel_encoder *encoder;
13295 struct intel_connector *connector;
13296 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013297 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013298 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013299
13300 state = drm_atomic_state_alloc(dev);
13301 if (!state) {
13302 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13303 crtc->base.id);
13304 return;
13305 }
13306
13307 state->acquire_ctx = dev->mode_config.acquire_ctx;
13308
13309 /* The force restore path in the HW readout code relies on the staged
13310 * config still keeping the user requested config while the actual
13311 * state has been overwritten by the configuration read from HW. We
13312 * need to copy the staged config to the atomic state, otherwise the
13313 * mode set will just reapply the state the HW is already in. */
13314 for_each_intel_encoder(dev, encoder) {
13315 if (&encoder->new_crtc->base != crtc)
13316 continue;
13317
13318 for_each_intel_connector(dev, connector) {
13319 if (connector->new_encoder != encoder)
13320 continue;
13321
13322 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13323 if (IS_ERR(connector_state)) {
13324 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13325 connector->base.base.id,
13326 connector->base.name,
13327 PTR_ERR(connector_state));
13328 continue;
13329 }
13330
13331 connector_state->crtc = crtc;
13332 connector_state->best_encoder = &encoder->base;
13333 }
13334 }
13335
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013336 crtc_state = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
13337 if (IS_ERR(crtc_state)) {
13338 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13339 crtc->base.id, PTR_ERR(crtc_state));
13340 drm_atomic_state_free(state);
13341 return;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013342 }
13343
Ander Conselvan de Oliveira4ed9fb32015-06-16 11:49:45 +030013344 crtc_state->base.active = crtc_state->base.enable =
13345 to_intel_crtc(crtc)->new_enabled;
13346
13347 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
13348
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013349 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13350 crtc->primary->fb, crtc->x, crtc->y);
13351
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013352 ret = intel_set_mode(state);
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013353 if (ret)
13354 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013355}
13356
Daniel Vetter25c5b262012-07-08 22:08:04 +020013357#undef for_each_intel_crtc_masked
13358
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013359static bool intel_connector_in_mode_set(struct intel_connector *connector,
13360 struct drm_mode_set *set)
13361{
13362 int ro;
13363
13364 for (ro = 0; ro < set->num_connectors; ro++)
13365 if (set->connectors[ro] == &connector->base)
13366 return true;
13367
13368 return false;
13369}
13370
Daniel Vetter2e431052012-07-04 22:42:15 +020013371static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013372intel_modeset_stage_output_state(struct drm_device *dev,
13373 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013374 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013375{
Daniel Vetter9a935852012-07-05 22:34:27 +020013376 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013377 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013378 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013379 struct drm_crtc *crtc;
13380 struct drm_crtc_state *crtc_state;
13381 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013382
Damien Lespiau9abdda72013-02-13 13:29:23 +000013383 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013384 * of connectors. For paranoia, double-check this. */
13385 WARN_ON(!set->fb && (set->num_connectors != 0));
13386 WARN_ON(set->fb && (set->num_connectors == 0));
13387
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013388 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013389 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13390
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013391 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13392 continue;
13393
13394 connector_state =
13395 drm_atomic_get_connector_state(state, &connector->base);
13396 if (IS_ERR(connector_state))
13397 return PTR_ERR(connector_state);
13398
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013399 if (in_mode_set) {
13400 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013401 connector_state->best_encoder =
13402 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013403 }
13404
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013405 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013406 continue;
13407
Daniel Vetter9a935852012-07-05 22:34:27 +020013408 /* If we disable the crtc, disable all its connectors. Also, if
13409 * the connector is on the changing crtc but not on the new
13410 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013411 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013412 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013413
13414 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13415 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013416 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013417 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013418 }
13419 /* connector->new_encoder is now updated for all connectors. */
13420
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013421 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13422 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013423
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013424 if (!connector_state->best_encoder) {
13425 ret = drm_atomic_set_crtc_for_connector(connector_state,
13426 NULL);
13427 if (ret)
13428 return ret;
13429
Daniel Vetter50f56112012-07-02 09:35:43 +020013430 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013431 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013432
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013433 if (intel_connector_in_mode_set(connector, set)) {
13434 struct drm_crtc *crtc = connector->base.state->crtc;
13435
13436 /* If this connector was in a previous crtc, add it
13437 * to the state. We might need to disable it. */
13438 if (crtc) {
13439 crtc_state =
13440 drm_atomic_get_crtc_state(state, crtc);
13441 if (IS_ERR(crtc_state))
13442 return PTR_ERR(crtc_state);
13443 }
13444
13445 ret = drm_atomic_set_crtc_for_connector(connector_state,
13446 set->crtc);
13447 if (ret)
13448 return ret;
13449 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013450
13451 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013452 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13453 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013454 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013455 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013456
Daniel Vetter9a935852012-07-05 22:34:27 +020013457 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13458 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013459 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013460 connector_state->crtc->base.id);
13461
13462 if (connector_state->best_encoder != &connector->encoder->base)
13463 connector->encoder =
13464 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013465 }
13466
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013467 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013468 bool has_connectors;
13469
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013470 ret = drm_atomic_add_affected_connectors(state, crtc);
13471 if (ret)
13472 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013473
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013474 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13475 if (has_connectors != crtc_state->enable)
13476 crtc_state->enable =
13477 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013478 }
13479
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013480 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13481 set->fb, set->x, set->y);
13482 if (ret)
13483 return ret;
13484
13485 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13486 if (IS_ERR(crtc_state))
13487 return PTR_ERR(crtc_state);
13488
Matt Roperce522992015-06-05 15:08:24 -070013489 ret = drm_atomic_set_mode_for_crtc(crtc_state, set->mode);
13490 if (ret)
13491 return ret;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013492
13493 if (set->num_connectors)
13494 crtc_state->active = true;
13495
Daniel Vetter2e431052012-07-04 22:42:15 +020013496 return 0;
13497}
13498
13499static int intel_crtc_set_config(struct drm_mode_set *set)
13500{
13501 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013502 struct drm_atomic_state *state = NULL;
Daniel Vetter2e431052012-07-04 22:42:15 +020013503 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013504
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013505 BUG_ON(!set);
13506 BUG_ON(!set->crtc);
13507 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013508
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013509 /* Enforce sane interface api - has been abused by the fb helper. */
13510 BUG_ON(!set->mode && set->fb);
13511 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013512
Daniel Vetter2e431052012-07-04 22:42:15 +020013513 if (set->fb) {
13514 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13515 set->crtc->base.id, set->fb->base.id,
13516 (int)set->num_connectors, set->x, set->y);
13517 } else {
13518 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013519 }
13520
13521 dev = set->crtc->dev;
13522
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013523 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013524 if (!state)
13525 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013526
13527 state->acquire_ctx = dev->mode_config.acquire_ctx;
13528
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013529 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013530 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013531 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013532
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013533 ret = intel_modeset_compute_config(state);
13534 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013535 goto out;
Jesse Barnes50f52752014-11-07 13:11:00 -080013536
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013537 intel_update_pipe_size(to_intel_crtc(set->crtc));
13538
Ander Conselvan de Oliveira568c6342015-06-01 12:49:57 +020013539 ret = intel_set_mode_checked(state);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013540 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013541 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13542 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013543 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013544
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013545out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013546 if (ret)
13547 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013548 return ret;
13549}
13550
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013551static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013552 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013553 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013554 .destroy = intel_crtc_destroy,
13555 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013556 .atomic_duplicate_state = intel_crtc_duplicate_state,
13557 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013558};
13559
Daniel Vetter53589012013-06-05 13:34:16 +020013560static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13561 struct intel_shared_dpll *pll,
13562 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013563{
Daniel Vetter53589012013-06-05 13:34:16 +020013564 uint32_t val;
13565
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013566 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013567 return false;
13568
Daniel Vetter53589012013-06-05 13:34:16 +020013569 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013570 hw_state->dpll = val;
13571 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13572 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013573
13574 return val & DPLL_VCO_ENABLE;
13575}
13576
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013577static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13578 struct intel_shared_dpll *pll)
13579{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013580 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13581 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013582}
13583
Daniel Vettere7b903d2013-06-05 13:34:14 +020013584static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13585 struct intel_shared_dpll *pll)
13586{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013587 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013588 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013589
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013590 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013591
13592 /* Wait for the clocks to stabilize. */
13593 POSTING_READ(PCH_DPLL(pll->id));
13594 udelay(150);
13595
13596 /* The pixel multiplier can only be updated once the
13597 * DPLL is enabled and the clocks are stable.
13598 *
13599 * So write it again.
13600 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013601 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013602 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013603 udelay(200);
13604}
13605
13606static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13607 struct intel_shared_dpll *pll)
13608{
13609 struct drm_device *dev = dev_priv->dev;
13610 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013611
13612 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013613 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013614 if (intel_crtc_to_shared_dpll(crtc) == pll)
13615 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13616 }
13617
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013618 I915_WRITE(PCH_DPLL(pll->id), 0);
13619 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013620 udelay(200);
13621}
13622
Daniel Vetter46edb022013-06-05 13:34:12 +020013623static char *ibx_pch_dpll_names[] = {
13624 "PCH DPLL A",
13625 "PCH DPLL B",
13626};
13627
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013628static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013629{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013630 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013631 int i;
13632
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013633 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013634
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013635 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013636 dev_priv->shared_dplls[i].id = i;
13637 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013638 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013639 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13640 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013641 dev_priv->shared_dplls[i].get_hw_state =
13642 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013643 }
13644}
13645
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013646static void intel_shared_dpll_init(struct drm_device *dev)
13647{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013648 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013649
Ville Syrjäläb6283052015-06-03 15:45:07 +030013650 intel_update_cdclk(dev);
13651
Daniel Vetter9cd86932014-06-25 22:01:57 +030013652 if (HAS_DDI(dev))
13653 intel_ddi_pll_init(dev);
13654 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013655 ibx_pch_dpll_init(dev);
13656 else
13657 dev_priv->num_shared_dpll = 0;
13658
13659 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013660}
13661
Matt Roper6beb8c232014-12-01 15:40:14 -080013662/**
13663 * intel_prepare_plane_fb - Prepare fb for usage on plane
13664 * @plane: drm plane to prepare for
13665 * @fb: framebuffer to prepare for presentation
13666 *
13667 * Prepares a framebuffer for usage on a display plane. Generally this
13668 * involves pinning the underlying object and updating the frontbuffer tracking
13669 * bits. Some older platforms need special physical address handling for
13670 * cursor planes.
13671 *
13672 * Returns 0 on success, negative error code on failure.
13673 */
13674int
13675intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013676 struct drm_framebuffer *fb,
13677 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013678{
13679 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013680 struct intel_plane *intel_plane = to_intel_plane(plane);
Matt Roper6beb8c232014-12-01 15:40:14 -080013681 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13682 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
Matt Roper6beb8c232014-12-01 15:40:14 -080013683 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013684
Matt Roperea2c67b2014-12-23 10:41:52 -080013685 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013686 return 0;
13687
Matt Roper4c345742014-07-09 16:22:10 -070013688 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013689
Matt Roper6beb8c232014-12-01 15:40:14 -080013690 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13691 INTEL_INFO(dev)->cursor_needs_physical) {
13692 int align = IS_I830(dev) ? 16 * 1024 : 256;
13693 ret = i915_gem_object_attach_phys(obj, align);
13694 if (ret)
13695 DRM_DEBUG_KMS("failed to attach phys object\n");
13696 } else {
John Harrison91af1272015-06-18 13:14:56 +010013697 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013698 }
13699
13700 if (ret == 0)
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013701 i915_gem_track_fb(old_obj, obj, intel_plane->frontbuffer_bit);
Matt Roper6beb8c232014-12-01 15:40:14 -080013702
13703 mutex_unlock(&dev->struct_mutex);
13704
13705 return ret;
13706}
13707
Matt Roper38f3ce32014-12-02 07:45:25 -080013708/**
13709 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13710 * @plane: drm plane to clean up for
13711 * @fb: old framebuffer that was on plane
13712 *
13713 * Cleans up a framebuffer that has just been removed from a plane.
13714 */
13715void
13716intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013717 struct drm_framebuffer *fb,
13718 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013719{
13720 struct drm_device *dev = plane->dev;
13721 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13722
13723 if (WARN_ON(!obj))
13724 return;
13725
13726 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13727 !INTEL_INFO(dev)->cursor_needs_physical) {
13728 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013729 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013730 mutex_unlock(&dev->struct_mutex);
13731 }
Matt Roper465c1202014-05-29 08:06:54 -070013732}
13733
Chandra Konduru6156a452015-04-27 13:48:39 -070013734int
13735skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13736{
13737 int max_scale;
13738 struct drm_device *dev;
13739 struct drm_i915_private *dev_priv;
13740 int crtc_clock, cdclk;
13741
13742 if (!intel_crtc || !crtc_state)
13743 return DRM_PLANE_HELPER_NO_SCALING;
13744
13745 dev = intel_crtc->base.dev;
13746 dev_priv = dev->dev_private;
13747 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020013748 cdclk = to_intel_atomic_state(crtc_state->base.state)->cdclk;
Chandra Konduru6156a452015-04-27 13:48:39 -070013749
13750 if (!crtc_clock || !cdclk)
13751 return DRM_PLANE_HELPER_NO_SCALING;
13752
13753 /*
13754 * skl max scale is lower of:
13755 * close to 3 but not 3, -1 is for that purpose
13756 * or
13757 * cdclk/crtc_clock
13758 */
13759 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13760
13761 return max_scale;
13762}
13763
Matt Roper465c1202014-05-29 08:06:54 -070013764static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013765intel_check_primary_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013766 struct intel_crtc_state *crtc_state,
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013767 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013768{
Matt Roper2b875c22014-12-01 15:40:13 -080013769 struct drm_crtc *crtc = state->base.crtc;
13770 struct drm_framebuffer *fb = state->base.fb;
Chandra Konduru6156a452015-04-27 13:48:39 -070013771 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013772 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13773 bool can_position = false;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013774
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013775 /* use scaler when colorkey is not required */
13776 if (INTEL_INFO(plane->dev)->gen >= 9 &&
Maarten Lankhorst818ed962015-06-15 12:33:54 +020013777 state->ckey.flags == I915_SET_COLORKEY_NONE) {
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013778 min_scale = 1;
13779 max_scale = skl_max_scale(to_intel_crtc(crtc), crtc_state);
Sonika Jindald8106362015-04-10 14:37:28 +053013780 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013781 }
Sonika Jindald8106362015-04-10 14:37:28 +053013782
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013783 return drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13784 &state->dst, &state->clip,
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013785 min_scale, max_scale,
13786 can_position, true,
13787 &state->visible);
Matt Roper465c1202014-05-29 08:06:54 -070013788}
13789
Gustavo Padovan14af2932014-10-24 14:51:31 +010013790static void
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013791intel_commit_primary_plane(struct drm_plane *plane,
13792 struct intel_plane_state *state)
13793{
Matt Roper2b875c22014-12-01 15:40:13 -080013794 struct drm_crtc *crtc = state->base.crtc;
13795 struct drm_framebuffer *fb = state->base.fb;
13796 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013797 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013798 struct intel_crtc *intel_crtc;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013799 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013800
Matt Roperea2c67b2014-12-23 10:41:52 -080013801 crtc = crtc ? crtc : plane->crtc;
13802 intel_crtc = to_intel_crtc(crtc);
13803
Matt Ropercf4c7c12014-12-04 10:27:42 -080013804 plane->fb = fb;
Matt Roper9dc806f2014-11-17 18:10:38 -080013805 crtc->x = src->x1 >> 16;
13806 crtc->y = src->y1 >> 16;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013807
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013808 if (!crtc->state->active)
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013809 return;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013810
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020013811 if (state->visible)
13812 /* FIXME: kill this fastboot hack */
13813 intel_update_pipe_size(intel_crtc);
13814
13815 dev_priv->display.update_primary_plane(crtc, fb, crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013816}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013817
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013818static void
13819intel_disable_primary_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020013820 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013821{
13822 struct drm_device *dev = plane->dev;
13823 struct drm_i915_private *dev_priv = dev->dev_private;
13824
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013825 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13826}
13827
Matt Roper32b7eee2014-12-24 07:59:06 -080013828static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13829{
13830 struct drm_device *dev = crtc->dev;
13831 struct drm_i915_private *dev_priv = dev->dev_private;
13832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013833
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013834 if (!needs_modeset(crtc->state))
13835 intel_pre_plane_update(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013836
Ville Syrjäläf015c552015-06-24 22:00:02 +030013837 if (intel_crtc->atomic.update_wm_pre)
Matt Roper32b7eee2014-12-24 07:59:06 -080013838 intel_update_watermarks(crtc);
13839
13840 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013841
13842 /* Perform vblank evasion around commit operation */
Maarten Lankhorsta5392052015-06-15 12:33:52 +020013843 if (crtc->state->active)
Matt Roperc34c9ee2014-12-23 10:41:50 -080013844 intel_crtc->atomic.evade =
13845 intel_pipe_update_start(intel_crtc,
13846 &intel_crtc->atomic.start_vbl_count);
Maarten Lankhorst05832362015-06-15 12:33:48 +020013847
13848 if (!needs_modeset(crtc->state) && INTEL_INFO(dev)->gen >= 9)
13849 skl_detach_scalers(intel_crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013850}
13851
13852static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13853{
13854 struct drm_device *dev = crtc->dev;
13855 struct drm_i915_private *dev_priv = dev->dev_private;
13856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roper32b7eee2014-12-24 07:59:06 -080013857
Matt Roperc34c9ee2014-12-23 10:41:50 -080013858 if (intel_crtc->atomic.evade)
13859 intel_pipe_update_end(intel_crtc,
13860 intel_crtc->atomic.start_vbl_count);
13861
Matt Roper32b7eee2014-12-24 07:59:06 -080013862 intel_runtime_pm_put(dev_priv);
13863
Maarten Lankhorstac21b222015-06-15 12:33:49 +020013864 intel_post_plane_update(intel_crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013865}
13866
Matt Ropercf4c7c12014-12-04 10:27:42 -080013867/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013868 * intel_plane_destroy - destroy a plane
13869 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013870 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013871 * Common destruction function for all types of planes (primary, cursor,
13872 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013873 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013874void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013875{
13876 struct intel_plane *intel_plane = to_intel_plane(plane);
13877 drm_plane_cleanup(plane);
13878 kfree(intel_plane);
13879}
13880
Matt Roper65a3fea2015-01-21 16:35:42 -080013881const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013882 .update_plane = drm_atomic_helper_update_plane,
13883 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013884 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013885 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013886 .atomic_get_property = intel_plane_atomic_get_property,
13887 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013888 .atomic_duplicate_state = intel_plane_duplicate_state,
13889 .atomic_destroy_state = intel_plane_destroy_state,
13890
Matt Roper465c1202014-05-29 08:06:54 -070013891};
13892
13893static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13894 int pipe)
13895{
13896 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013897 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013898 const uint32_t *intel_primary_formats;
13899 int num_formats;
13900
13901 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13902 if (primary == NULL)
13903 return NULL;
13904
Matt Roper8e7d6882015-01-21 16:35:41 -080013905 state = intel_create_plane_state(&primary->base);
13906 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013907 kfree(primary);
13908 return NULL;
13909 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013910 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013911
Matt Roper465c1202014-05-29 08:06:54 -070013912 primary->can_scale = false;
13913 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013914 if (INTEL_INFO(dev)->gen >= 9) {
13915 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013916 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013917 }
Matt Roper465c1202014-05-29 08:06:54 -070013918 primary->pipe = pipe;
13919 primary->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030013920 primary->frontbuffer_bit = INTEL_FRONTBUFFER_PRIMARY(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080013921 primary->check_plane = intel_check_primary_plane;
13922 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013923 primary->disable_plane = intel_disable_primary_plane;
Matt Roper465c1202014-05-29 08:06:54 -070013924 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13925 primary->plane = !pipe;
13926
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013927 if (INTEL_INFO(dev)->gen >= 9) {
13928 intel_primary_formats = skl_primary_formats;
13929 num_formats = ARRAY_SIZE(skl_primary_formats);
13930 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013931 intel_primary_formats = i965_primary_formats;
13932 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013933 } else {
13934 intel_primary_formats = i8xx_primary_formats;
13935 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013936 }
13937
13938 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013939 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013940 intel_primary_formats, num_formats,
13941 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013942
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013943 if (INTEL_INFO(dev)->gen >= 4)
13944 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013945
Matt Roperea2c67b2014-12-23 10:41:52 -080013946 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13947
Matt Roper465c1202014-05-29 08:06:54 -070013948 return &primary->base;
13949}
13950
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013951void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13952{
13953 if (!dev->mode_config.rotation_property) {
13954 unsigned long flags = BIT(DRM_ROTATE_0) |
13955 BIT(DRM_ROTATE_180);
13956
13957 if (INTEL_INFO(dev)->gen >= 9)
13958 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13959
13960 dev->mode_config.rotation_property =
13961 drm_mode_create_rotation_property(dev, flags);
13962 }
13963 if (dev->mode_config.rotation_property)
13964 drm_object_attach_property(&plane->base.base,
13965 dev->mode_config.rotation_property,
13966 plane->base.state->rotation);
13967}
13968
Matt Roper3d7d6512014-06-10 08:28:13 -070013969static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013970intel_check_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013971 struct intel_crtc_state *crtc_state,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013972 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013973{
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013974 struct drm_crtc *crtc = crtc_state->base.crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013975 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013976 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013977 unsigned stride;
13978 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013979
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013980 ret = drm_plane_helper_check_update(plane, crtc, fb, &state->src,
13981 &state->dst, &state->clip,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013982 DRM_PLANE_HELPER_NO_SCALING,
13983 DRM_PLANE_HELPER_NO_SCALING,
13984 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013985 if (ret)
13986 return ret;
13987
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013988 /* if we want to turn off the cursor ignore width and height */
13989 if (!obj)
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020013990 return 0;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013991
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013992 /* Check for which cursor types we support */
Maarten Lankhorst061e4b82015-06-15 12:33:46 +020013993 if (!cursor_size_ok(plane->dev, state->base.crtc_w, state->base.crtc_h)) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013994 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13995 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013996 return -EINVAL;
13997 }
13998
Matt Roperea2c67b2014-12-23 10:41:52 -080013999 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14000 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014001 DRM_DEBUG_KMS("buffer is too small\n");
14002 return -ENOMEM;
14003 }
14004
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014005 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014006 DRM_DEBUG_KMS("cursor cannot be tiled\n");
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014007 return -EINVAL;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014008 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014009
Maarten Lankhorstda20eab2015-06-15 12:33:44 +020014010 return 0;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014011}
14012
Matt Roperf4a2cf22014-12-01 15:40:12 -080014013static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014014intel_disable_cursor_plane(struct drm_plane *plane,
Maarten Lankhorst7fabf5e2015-06-15 12:33:47 +020014015 struct drm_crtc *crtc)
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014016{
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014017 intel_crtc_update_cursor(crtc, false);
14018}
14019
14020static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030014021intel_commit_cursor_plane(struct drm_plane *plane,
14022 struct intel_plane_state *state)
14023{
Matt Roper2b875c22014-12-01 15:40:13 -080014024 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014025 struct drm_device *dev = plane->dev;
14026 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014027 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014028 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014029
Matt Roperea2c67b2014-12-23 10:41:52 -080014030 crtc = crtc ? crtc : plane->crtc;
14031 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014032
Matt Roperea2c67b2014-12-23 10:41:52 -080014033 plane->fb = state->base.fb;
14034 crtc->cursor_x = state->base.crtc_x;
14035 crtc->cursor_y = state->base.crtc_y;
14036
Gustavo Padovana912f122014-12-01 15:40:10 -080014037 if (intel_crtc->cursor_bo == obj)
14038 goto update;
14039
Matt Roperf4a2cf22014-12-01 15:40:12 -080014040 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014041 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014042 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014043 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014044 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014045 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014046
Gustavo Padovana912f122014-12-01 15:40:10 -080014047 intel_crtc->cursor_addr = addr;
14048 intel_crtc->cursor_bo = obj;
Gustavo Padovana912f122014-12-01 15:40:10 -080014049
Maarten Lankhorst302d19a2015-06-15 12:33:45 +020014050update:
Maarten Lankhorsta5392052015-06-15 12:33:52 +020014051 if (crtc->state->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014052 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014053}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014054
Matt Roper3d7d6512014-06-10 08:28:13 -070014055static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14056 int pipe)
14057{
14058 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014059 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014060
14061 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14062 if (cursor == NULL)
14063 return NULL;
14064
Matt Roper8e7d6882015-01-21 16:35:41 -080014065 state = intel_create_plane_state(&cursor->base);
14066 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014067 kfree(cursor);
14068 return NULL;
14069 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014070 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014071
Matt Roper3d7d6512014-06-10 08:28:13 -070014072 cursor->can_scale = false;
14073 cursor->max_downscale = 1;
14074 cursor->pipe = pipe;
14075 cursor->plane = pipe;
Ville Syrjäläa9ff8712015-06-24 21:59:34 +030014076 cursor->frontbuffer_bit = INTEL_FRONTBUFFER_CURSOR(pipe);
Matt Roperc59cb172014-12-01 15:40:16 -080014077 cursor->check_plane = intel_check_cursor_plane;
14078 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014079 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014080
14081 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014082 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014083 intel_cursor_formats,
14084 ARRAY_SIZE(intel_cursor_formats),
14085 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014086
14087 if (INTEL_INFO(dev)->gen >= 4) {
14088 if (!dev->mode_config.rotation_property)
14089 dev->mode_config.rotation_property =
14090 drm_mode_create_rotation_property(dev,
14091 BIT(DRM_ROTATE_0) |
14092 BIT(DRM_ROTATE_180));
14093 if (dev->mode_config.rotation_property)
14094 drm_object_attach_property(&cursor->base.base,
14095 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014096 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014097 }
14098
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070014099 if (INTEL_INFO(dev)->gen >=9)
14100 state->scaler_id = -1;
14101
Matt Roperea2c67b2014-12-23 10:41:52 -080014102 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14103
Matt Roper3d7d6512014-06-10 08:28:13 -070014104 return &cursor->base;
14105}
14106
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014107static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14108 struct intel_crtc_state *crtc_state)
14109{
14110 int i;
14111 struct intel_scaler *intel_scaler;
14112 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14113
14114 for (i = 0; i < intel_crtc->num_scalers; i++) {
14115 intel_scaler = &scaler_state->scalers[i];
14116 intel_scaler->in_use = 0;
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014117 intel_scaler->mode = PS_SCALER_MODE_DYN;
14118 }
14119
14120 scaler_state->scaler_id = -1;
14121}
14122
Hannes Ederb358d0a2008-12-18 21:18:47 +010014123static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014124{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014125 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014126 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014127 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014128 struct drm_plane *primary = NULL;
14129 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014130 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014131
Daniel Vetter955382f2013-09-19 14:05:45 +020014132 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014133 if (intel_crtc == NULL)
14134 return;
14135
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014136 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14137 if (!crtc_state)
14138 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014139 intel_crtc->config = crtc_state;
14140 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014141 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014142
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014143 /* initialize shared scalers */
14144 if (INTEL_INFO(dev)->gen >= 9) {
14145 if (pipe == PIPE_C)
14146 intel_crtc->num_scalers = 1;
14147 else
14148 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14149
14150 skl_init_scalers(dev, intel_crtc, crtc_state);
14151 }
14152
Matt Roper465c1202014-05-29 08:06:54 -070014153 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014154 if (!primary)
14155 goto fail;
14156
14157 cursor = intel_cursor_plane_create(dev, pipe);
14158 if (!cursor)
14159 goto fail;
14160
Matt Roper465c1202014-05-29 08:06:54 -070014161 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014162 cursor, &intel_crtc_funcs);
14163 if (ret)
14164 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014165
14166 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014167 for (i = 0; i < 256; i++) {
14168 intel_crtc->lut_r[i] = i;
14169 intel_crtc->lut_g[i] = i;
14170 intel_crtc->lut_b[i] = i;
14171 }
14172
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014173 /*
14174 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014175 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014176 */
Jesse Barnes80824002009-09-10 15:28:06 -070014177 intel_crtc->pipe = pipe;
14178 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014179 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014180 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014181 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014182 }
14183
Chris Wilson4b0e3332014-05-30 16:35:26 +030014184 intel_crtc->cursor_base = ~0;
14185 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014186 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014187
Ville Syrjälä852eb002015-06-24 22:00:07 +030014188 intel_crtc->wm.cxsr_allowed = true;
14189
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014190 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14191 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14192 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14193 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14194
Jesse Barnes79e53942008-11-07 14:24:08 -080014195 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014196
14197 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014198 return;
14199
14200fail:
14201 if (primary)
14202 drm_plane_cleanup(primary);
14203 if (cursor)
14204 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014205 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014206 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014207}
14208
Jesse Barnes752aa882013-10-31 18:55:49 +020014209enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14210{
14211 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014212 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014213
Rob Clark51fd3712013-11-19 12:10:12 -050014214 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014215
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014216 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014217 return INVALID_PIPE;
14218
14219 return to_intel_crtc(encoder->crtc)->pipe;
14220}
14221
Carl Worth08d7b3d2009-04-29 14:43:54 -070014222int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014223 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014224{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014225 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014226 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014227 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014228
Rob Clark7707e652014-07-17 23:30:04 -040014229 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014230
Rob Clark7707e652014-07-17 23:30:04 -040014231 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014232 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014233 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014234 }
14235
Rob Clark7707e652014-07-17 23:30:04 -040014236 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014237 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014238
Daniel Vetterc05422d2009-08-11 16:05:30 +020014239 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014240}
14241
Daniel Vetter66a92782012-07-12 20:08:18 +020014242static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014243{
Daniel Vetter66a92782012-07-12 20:08:18 +020014244 struct drm_device *dev = encoder->base.dev;
14245 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014246 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014247 int entry = 0;
14248
Damien Lespiaub2784e12014-08-05 11:29:37 +010014249 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014250 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014251 index_mask |= (1 << entry);
14252
Jesse Barnes79e53942008-11-07 14:24:08 -080014253 entry++;
14254 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014255
Jesse Barnes79e53942008-11-07 14:24:08 -080014256 return index_mask;
14257}
14258
Chris Wilson4d302442010-12-14 19:21:29 +000014259static bool has_edp_a(struct drm_device *dev)
14260{
14261 struct drm_i915_private *dev_priv = dev->dev_private;
14262
14263 if (!IS_MOBILE(dev))
14264 return false;
14265
14266 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14267 return false;
14268
Damien Lespiaue3589902014-02-07 19:12:50 +000014269 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014270 return false;
14271
14272 return true;
14273}
14274
Jesse Barnes84b4e042014-06-25 08:24:29 -070014275static bool intel_crt_present(struct drm_device *dev)
14276{
14277 struct drm_i915_private *dev_priv = dev->dev_private;
14278
Damien Lespiau884497e2013-12-03 13:56:23 +000014279 if (INTEL_INFO(dev)->gen >= 9)
14280 return false;
14281
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014282 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014283 return false;
14284
14285 if (IS_CHERRYVIEW(dev))
14286 return false;
14287
14288 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14289 return false;
14290
14291 return true;
14292}
14293
Jesse Barnes79e53942008-11-07 14:24:08 -080014294static void intel_setup_outputs(struct drm_device *dev)
14295{
Eric Anholt725e30a2009-01-22 13:01:02 -080014296 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014297 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014298 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014299
Daniel Vetterc9093352013-06-06 22:22:47 +020014300 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014301
Jesse Barnes84b4e042014-06-25 08:24:29 -070014302 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014303 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014304
Vandana Kannanc776eb22014-08-19 12:05:01 +053014305 if (IS_BROXTON(dev)) {
14306 /*
14307 * FIXME: Broxton doesn't support port detection via the
14308 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14309 * detect the ports.
14310 */
14311 intel_ddi_init(dev, PORT_A);
14312 intel_ddi_init(dev, PORT_B);
14313 intel_ddi_init(dev, PORT_C);
14314 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014315 int found;
14316
Jesse Barnesde31fac2015-03-06 15:53:32 -080014317 /*
14318 * Haswell uses DDI functions to detect digital outputs.
14319 * On SKL pre-D0 the strap isn't connected, so we assume
14320 * it's there.
14321 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014322 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014323 /* WaIgnoreDDIAStrap: skl */
14324 if (found ||
14325 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014326 intel_ddi_init(dev, PORT_A);
14327
14328 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14329 * register */
14330 found = I915_READ(SFUSE_STRAP);
14331
14332 if (found & SFUSE_STRAP_DDIB_DETECTED)
14333 intel_ddi_init(dev, PORT_B);
14334 if (found & SFUSE_STRAP_DDIC_DETECTED)
14335 intel_ddi_init(dev, PORT_C);
14336 if (found & SFUSE_STRAP_DDID_DETECTED)
14337 intel_ddi_init(dev, PORT_D);
14338 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014339 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014340 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014341
14342 if (has_edp_a(dev))
14343 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014344
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014345 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014346 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014347 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014348 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014349 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014350 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014351 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014352 }
14353
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014354 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014355 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014356
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014357 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014358 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014359
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014360 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014361 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014362
Daniel Vetter270b3042012-10-27 15:52:05 +020014363 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014364 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014365 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014366 /*
14367 * The DP_DETECTED bit is the latched state of the DDC
14368 * SDA pin at boot. However since eDP doesn't require DDC
14369 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14370 * eDP ports may have been muxed to an alternate function.
14371 * Thus we can't rely on the DP_DETECTED bit alone to detect
14372 * eDP ports. Consult the VBT as well as DP_DETECTED to
14373 * detect eDP ports.
14374 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014375 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14376 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014377 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14378 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014379 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14380 intel_dp_is_edp(dev, PORT_B))
14381 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014382
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014383 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14384 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014385 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14386 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014387 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14388 intel_dp_is_edp(dev, PORT_C))
14389 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014390
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014391 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014392 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014393 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14394 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014395 /* eDP not supported on port D, so don't check VBT */
14396 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14397 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014398 }
14399
Jani Nikula3cfca972013-08-27 15:12:26 +030014400 intel_dsi_init(dev);
Daniel Vetter09da55d2015-07-07 11:44:32 +020014401 } else if (!IS_GEN2(dev) && !IS_PINEVIEW(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014402 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014403
Paulo Zanonie2debe92013-02-18 19:00:27 -030014404 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014405 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014406 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014407 if (!found && IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014408 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014409 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014410 }
Ma Ling27185ae2009-08-24 13:50:23 +080014411
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014412 if (!found && IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014413 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014414 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014415
14416 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014417
Paulo Zanonie2debe92013-02-18 19:00:27 -030014418 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014419 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014420 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014421 }
Ma Ling27185ae2009-08-24 13:50:23 +080014422
Paulo Zanonie2debe92013-02-18 19:00:27 -030014423 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014424
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014425 if (IS_G4X(dev)) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014426 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014427 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014428 }
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014429 if (IS_G4X(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014430 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014431 }
Ma Ling27185ae2009-08-24 13:50:23 +080014432
Daniel Vetter3fec3d22015-07-07 09:10:07 +020014433 if (IS_G4X(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014434 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014435 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014436 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014437 intel_dvo_init(dev);
14438
Zhenyu Wang103a1962009-11-27 11:44:36 +080014439 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014440 intel_tv_init(dev);
14441
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014442 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014443
Damien Lespiaub2784e12014-08-05 11:29:37 +010014444 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014445 encoder->base.possible_crtcs = encoder->crtc_mask;
14446 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014447 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014448 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014449
Paulo Zanonidde86e22012-12-01 12:04:25 -020014450 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014451
14452 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014453}
14454
14455static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14456{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014457 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014458 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014459
Daniel Vetteref2d6332014-02-10 18:00:38 +010014460 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014461 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014462 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014463 drm_gem_object_unreference(&intel_fb->obj->base);
14464 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014465 kfree(intel_fb);
14466}
14467
14468static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014469 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014470 unsigned int *handle)
14471{
14472 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014473 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014474
Chris Wilson05394f32010-11-08 19:18:58 +000014475 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014476}
14477
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014478static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
14479 struct drm_file *file,
14480 unsigned flags, unsigned color,
14481 struct drm_clip_rect *clips,
14482 unsigned num_clips)
14483{
14484 struct drm_device *dev = fb->dev;
14485 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
14486 struct drm_i915_gem_object *obj = intel_fb->obj;
14487
14488 mutex_lock(&dev->struct_mutex);
14489 intel_fb_obj_flush(obj, false, ORIGIN_GTT);
14490 mutex_unlock(&dev->struct_mutex);
14491
14492 return 0;
14493}
14494
Jesse Barnes79e53942008-11-07 14:24:08 -080014495static const struct drm_framebuffer_funcs intel_fb_funcs = {
14496 .destroy = intel_user_framebuffer_destroy,
14497 .create_handle = intel_user_framebuffer_create_handle,
Rodrigo Vivi86c98582015-07-08 16:22:45 -070014498 .dirty = intel_user_framebuffer_dirty,
Jesse Barnes79e53942008-11-07 14:24:08 -080014499};
14500
Damien Lespiaub3218032015-02-27 11:15:18 +000014501static
14502u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14503 uint32_t pixel_format)
14504{
14505 u32 gen = INTEL_INFO(dev)->gen;
14506
14507 if (gen >= 9) {
14508 /* "The stride in bytes must not exceed the of the size of 8K
14509 * pixels and 32K bytes."
14510 */
14511 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14512 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14513 return 32*1024;
14514 } else if (gen >= 4) {
14515 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14516 return 16*1024;
14517 else
14518 return 32*1024;
14519 } else if (gen >= 3) {
14520 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14521 return 8*1024;
14522 else
14523 return 16*1024;
14524 } else {
14525 /* XXX DSPC is limited to 4k tiled */
14526 return 8*1024;
14527 }
14528}
14529
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014530static int intel_framebuffer_init(struct drm_device *dev,
14531 struct intel_framebuffer *intel_fb,
14532 struct drm_mode_fb_cmd2 *mode_cmd,
14533 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014534{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014535 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014536 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014537 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014538
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014539 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14540
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014541 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14542 /* Enforce that fb modifier and tiling mode match, but only for
14543 * X-tiled. This is needed for FBC. */
14544 if (!!(obj->tiling_mode == I915_TILING_X) !=
14545 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14546 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14547 return -EINVAL;
14548 }
14549 } else {
14550 if (obj->tiling_mode == I915_TILING_X)
14551 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14552 else if (obj->tiling_mode == I915_TILING_Y) {
14553 DRM_DEBUG("No Y tiling for legacy addfb\n");
14554 return -EINVAL;
14555 }
14556 }
14557
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014558 /* Passed in modifier sanity checking. */
14559 switch (mode_cmd->modifier[0]) {
14560 case I915_FORMAT_MOD_Y_TILED:
14561 case I915_FORMAT_MOD_Yf_TILED:
14562 if (INTEL_INFO(dev)->gen < 9) {
14563 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14564 mode_cmd->modifier[0]);
14565 return -EINVAL;
14566 }
14567 case DRM_FORMAT_MOD_NONE:
14568 case I915_FORMAT_MOD_X_TILED:
14569 break;
14570 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014571 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14572 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014573 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014574 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014575
Damien Lespiaub3218032015-02-27 11:15:18 +000014576 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14577 mode_cmd->pixel_format);
14578 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14579 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14580 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014581 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014582 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014583
Damien Lespiaub3218032015-02-27 11:15:18 +000014584 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14585 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014586 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014587 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14588 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014589 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014590 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014591 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014592 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014593
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014594 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014595 mode_cmd->pitches[0] != obj->stride) {
14596 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14597 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014598 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014599 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014600
Ville Syrjälä57779d02012-10-31 17:50:14 +020014601 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014602 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014603 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014604 case DRM_FORMAT_RGB565:
14605 case DRM_FORMAT_XRGB8888:
14606 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014607 break;
14608 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014609 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014610 DRM_DEBUG("unsupported pixel format: %s\n",
14611 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014612 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014613 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014614 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014615 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014616 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14617 DRM_DEBUG("unsupported pixel format: %s\n",
14618 drm_get_format_name(mode_cmd->pixel_format));
14619 return -EINVAL;
14620 }
14621 break;
14622 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014623 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014624 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014625 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014626 DRM_DEBUG("unsupported pixel format: %s\n",
14627 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014628 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014629 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014630 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014631 case DRM_FORMAT_ABGR2101010:
14632 if (!IS_VALLEYVIEW(dev)) {
14633 DRM_DEBUG("unsupported pixel format: %s\n",
14634 drm_get_format_name(mode_cmd->pixel_format));
14635 return -EINVAL;
14636 }
14637 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014638 case DRM_FORMAT_YUYV:
14639 case DRM_FORMAT_UYVY:
14640 case DRM_FORMAT_YVYU:
14641 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014642 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014643 DRM_DEBUG("unsupported pixel format: %s\n",
14644 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014645 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014646 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014647 break;
14648 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014649 DRM_DEBUG("unsupported pixel format: %s\n",
14650 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014651 return -EINVAL;
14652 }
14653
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014654 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14655 if (mode_cmd->offsets[0] != 0)
14656 return -EINVAL;
14657
Damien Lespiauec2c9812015-01-20 12:51:45 +000014658 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014659 mode_cmd->pixel_format,
14660 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014661 /* FIXME drm helper for size checks (especially planar formats)? */
14662 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14663 return -EINVAL;
14664
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014665 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14666 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014667 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014668
Jesse Barnes79e53942008-11-07 14:24:08 -080014669 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14670 if (ret) {
14671 DRM_ERROR("framebuffer init failed %d\n", ret);
14672 return ret;
14673 }
14674
Jesse Barnes79e53942008-11-07 14:24:08 -080014675 return 0;
14676}
14677
Jesse Barnes79e53942008-11-07 14:24:08 -080014678static struct drm_framebuffer *
14679intel_user_framebuffer_create(struct drm_device *dev,
14680 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014681 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014682{
Chris Wilson05394f32010-11-08 19:18:58 +000014683 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014684
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014685 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14686 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014687 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014688 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014689
Chris Wilsond2dff872011-04-19 08:36:26 +010014690 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014691}
14692
Daniel Vetter4520f532013-10-09 09:18:51 +020014693#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014694static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014695{
14696}
14697#endif
14698
Jesse Barnes79e53942008-11-07 14:24:08 -080014699static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014700 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014701 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014702 .atomic_check = intel_atomic_check,
14703 .atomic_commit = intel_atomic_commit,
Maarten Lankhorstde419ab2015-06-04 10:21:28 +020014704 .atomic_state_alloc = intel_atomic_state_alloc,
14705 .atomic_state_clear = intel_atomic_state_clear,
Jesse Barnes79e53942008-11-07 14:24:08 -080014706};
14707
Jesse Barnese70236a2009-09-21 10:42:27 -070014708/* Set up chip specific display functions */
14709static void intel_init_display(struct drm_device *dev)
14710{
14711 struct drm_i915_private *dev_priv = dev->dev_private;
14712
Daniel Vetteree9300b2013-06-03 22:40:22 +020014713 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14714 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014715 else if (IS_CHERRYVIEW(dev))
14716 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014717 else if (IS_VALLEYVIEW(dev))
14718 dev_priv->display.find_dpll = vlv_find_best_dpll;
14719 else if (IS_PINEVIEW(dev))
14720 dev_priv->display.find_dpll = pnv_find_best_dpll;
14721 else
14722 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14723
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014724 if (INTEL_INFO(dev)->gen >= 9) {
14725 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014726 dev_priv->display.get_initial_plane_config =
14727 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014728 dev_priv->display.crtc_compute_clock =
14729 haswell_crtc_compute_clock;
14730 dev_priv->display.crtc_enable = haswell_crtc_enable;
14731 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014732 dev_priv->display.update_primary_plane =
14733 skylake_update_primary_plane;
14734 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014735 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014736 dev_priv->display.get_initial_plane_config =
14737 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014738 dev_priv->display.crtc_compute_clock =
14739 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014740 dev_priv->display.crtc_enable = haswell_crtc_enable;
14741 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014742 dev_priv->display.update_primary_plane =
14743 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014744 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014745 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014746 dev_priv->display.get_initial_plane_config =
14747 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014748 dev_priv->display.crtc_compute_clock =
14749 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014750 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14751 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014752 dev_priv->display.update_primary_plane =
14753 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014754 } else if (IS_VALLEYVIEW(dev)) {
14755 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014756 dev_priv->display.get_initial_plane_config =
14757 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014758 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014759 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14760 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014761 dev_priv->display.update_primary_plane =
14762 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014763 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014764 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014765 dev_priv->display.get_initial_plane_config =
14766 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014767 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014768 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14769 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014770 dev_priv->display.update_primary_plane =
14771 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014772 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014773
Jesse Barnese70236a2009-09-21 10:42:27 -070014774 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014775 if (IS_SKYLAKE(dev))
14776 dev_priv->display.get_display_clock_speed =
14777 skylake_get_display_clock_speed;
Bob Paauweacd3f3d2015-06-23 14:14:26 -070014778 else if (IS_BROXTON(dev))
14779 dev_priv->display.get_display_clock_speed =
14780 broxton_get_display_clock_speed;
Ville Syrjälä1652d192015-03-31 14:12:01 +030014781 else if (IS_BROADWELL(dev))
14782 dev_priv->display.get_display_clock_speed =
14783 broadwell_get_display_clock_speed;
14784 else if (IS_HASWELL(dev))
14785 dev_priv->display.get_display_clock_speed =
14786 haswell_get_display_clock_speed;
14787 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014788 dev_priv->display.get_display_clock_speed =
14789 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014790 else if (IS_GEN5(dev))
14791 dev_priv->display.get_display_clock_speed =
14792 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014793 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014794 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014795 dev_priv->display.get_display_clock_speed =
14796 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014797 else if (IS_GM45(dev))
14798 dev_priv->display.get_display_clock_speed =
14799 gm45_get_display_clock_speed;
14800 else if (IS_CRESTLINE(dev))
14801 dev_priv->display.get_display_clock_speed =
14802 i965gm_get_display_clock_speed;
14803 else if (IS_PINEVIEW(dev))
14804 dev_priv->display.get_display_clock_speed =
14805 pnv_get_display_clock_speed;
14806 else if (IS_G33(dev) || IS_G4X(dev))
14807 dev_priv->display.get_display_clock_speed =
14808 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014809 else if (IS_I915G(dev))
14810 dev_priv->display.get_display_clock_speed =
14811 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014812 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014813 dev_priv->display.get_display_clock_speed =
14814 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014815 else if (IS_PINEVIEW(dev))
14816 dev_priv->display.get_display_clock_speed =
14817 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014818 else if (IS_I915GM(dev))
14819 dev_priv->display.get_display_clock_speed =
14820 i915gm_get_display_clock_speed;
14821 else if (IS_I865G(dev))
14822 dev_priv->display.get_display_clock_speed =
14823 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014824 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014825 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014826 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014827 else { /* 830 */
14828 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014829 dev_priv->display.get_display_clock_speed =
14830 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014831 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014832
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014833 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014834 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014835 } else if (IS_GEN6(dev)) {
14836 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014837 } else if (IS_IVYBRIDGE(dev)) {
14838 /* FIXME: detect B0+ stepping and use auto training */
14839 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014840 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014841 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014842 if (IS_BROADWELL(dev)) {
14843 dev_priv->display.modeset_commit_cdclk =
14844 broadwell_modeset_commit_cdclk;
14845 dev_priv->display.modeset_calc_cdclk =
14846 broadwell_modeset_calc_cdclk;
14847 }
Jesse Barnes30a970c2013-11-04 13:48:12 -080014848 } else if (IS_VALLEYVIEW(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014849 dev_priv->display.modeset_commit_cdclk =
14850 valleyview_modeset_commit_cdclk;
14851 dev_priv->display.modeset_calc_cdclk =
14852 valleyview_modeset_calc_cdclk;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014853 } else if (IS_BROXTON(dev)) {
Maarten Lankhorst27c329e2015-06-15 12:33:56 +020014854 dev_priv->display.modeset_commit_cdclk =
14855 broxton_modeset_commit_cdclk;
14856 dev_priv->display.modeset_calc_cdclk =
14857 broxton_modeset_calc_cdclk;
Jesse Barnese70236a2009-09-21 10:42:27 -070014858 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014859
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014860 switch (INTEL_INFO(dev)->gen) {
14861 case 2:
14862 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14863 break;
14864
14865 case 3:
14866 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14867 break;
14868
14869 case 4:
14870 case 5:
14871 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14872 break;
14873
14874 case 6:
14875 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14876 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014877 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014878 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014879 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14880 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014881 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014882 /* Drop through - unsupported since execlist only. */
14883 default:
14884 /* Default just returns -ENODEV to indicate unsupported */
14885 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014886 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014887
14888 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014889
14890 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014891}
14892
Jesse Barnesb690e962010-07-19 13:53:12 -070014893/*
14894 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14895 * resume, or other times. This quirk makes sure that's the case for
14896 * affected systems.
14897 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014898static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014899{
14900 struct drm_i915_private *dev_priv = dev->dev_private;
14901
14902 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014903 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014904}
14905
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014906static void quirk_pipeb_force(struct drm_device *dev)
14907{
14908 struct drm_i915_private *dev_priv = dev->dev_private;
14909
14910 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14911 DRM_INFO("applying pipe b force quirk\n");
14912}
14913
Keith Packard435793d2011-07-12 14:56:22 -070014914/*
14915 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14916 */
14917static void quirk_ssc_force_disable(struct drm_device *dev)
14918{
14919 struct drm_i915_private *dev_priv = dev->dev_private;
14920 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014921 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014922}
14923
Carsten Emde4dca20e2012-03-15 15:56:26 +010014924/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014925 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14926 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014927 */
14928static void quirk_invert_brightness(struct drm_device *dev)
14929{
14930 struct drm_i915_private *dev_priv = dev->dev_private;
14931 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014932 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014933}
14934
Scot Doyle9c72cc62014-07-03 23:27:50 +000014935/* Some VBT's incorrectly indicate no backlight is present */
14936static void quirk_backlight_present(struct drm_device *dev)
14937{
14938 struct drm_i915_private *dev_priv = dev->dev_private;
14939 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14940 DRM_INFO("applying backlight present quirk\n");
14941}
14942
Jesse Barnesb690e962010-07-19 13:53:12 -070014943struct intel_quirk {
14944 int device;
14945 int subsystem_vendor;
14946 int subsystem_device;
14947 void (*hook)(struct drm_device *dev);
14948};
14949
Egbert Eich5f85f172012-10-14 15:46:38 +020014950/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14951struct intel_dmi_quirk {
14952 void (*hook)(struct drm_device *dev);
14953 const struct dmi_system_id (*dmi_id_list)[];
14954};
14955
14956static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14957{
14958 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14959 return 1;
14960}
14961
14962static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14963 {
14964 .dmi_id_list = &(const struct dmi_system_id[]) {
14965 {
14966 .callback = intel_dmi_reverse_brightness,
14967 .ident = "NCR Corporation",
14968 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14969 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14970 },
14971 },
14972 { } /* terminating entry */
14973 },
14974 .hook = quirk_invert_brightness,
14975 },
14976};
14977
Ben Widawskyc43b5632012-04-16 14:07:40 -070014978static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014979 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14980 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14981
Jesse Barnesb690e962010-07-19 13:53:12 -070014982 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14983 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14984
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014985 /* 830 needs to leave pipe A & dpll A up */
14986 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14987
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014988 /* 830 needs to leave pipe B & dpll B up */
14989 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14990
Keith Packard435793d2011-07-12 14:56:22 -070014991 /* Lenovo U160 cannot use SSC on LVDS */
14992 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014993
14994 /* Sony Vaio Y cannot use SSC on LVDS */
14995 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014996
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014997 /* Acer Aspire 5734Z must invert backlight brightness */
14998 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14999
15000 /* Acer/eMachines G725 */
15001 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15002
15003 /* Acer/eMachines e725 */
15004 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15005
15006 /* Acer/Packard Bell NCL20 */
15007 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15008
15009 /* Acer Aspire 4736Z */
15010 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015011
15012 /* Acer Aspire 5336 */
15013 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015014
15015 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15016 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015017
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015018 /* Acer C720 Chromebook (Core i3 4005U) */
15019 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15020
jens steinb2a96012014-10-28 20:25:53 +010015021 /* Apple Macbook 2,1 (Core 2 T7400) */
15022 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15023
Scot Doyled4967d82014-07-03 23:27:52 +000015024 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15025 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015026
15027 /* HP Chromebook 14 (Celeron 2955U) */
15028 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015029
15030 /* Dell Chromebook 11 */
15031 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015032};
15033
15034static void intel_init_quirks(struct drm_device *dev)
15035{
15036 struct pci_dev *d = dev->pdev;
15037 int i;
15038
15039 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15040 struct intel_quirk *q = &intel_quirks[i];
15041
15042 if (d->device == q->device &&
15043 (d->subsystem_vendor == q->subsystem_vendor ||
15044 q->subsystem_vendor == PCI_ANY_ID) &&
15045 (d->subsystem_device == q->subsystem_device ||
15046 q->subsystem_device == PCI_ANY_ID))
15047 q->hook(dev);
15048 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015049 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15050 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15051 intel_dmi_quirks[i].hook(dev);
15052 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015053}
15054
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015055/* Disable the VGA plane that we never use */
15056static void i915_disable_vga(struct drm_device *dev)
15057{
15058 struct drm_i915_private *dev_priv = dev->dev_private;
15059 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015060 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015061
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015062 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015063 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015064 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015065 sr1 = inb(VGA_SR_DATA);
15066 outb(sr1 | 1<<5, VGA_SR_DATA);
15067 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15068 udelay(300);
15069
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015070 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015071 POSTING_READ(vga_reg);
15072}
15073
Daniel Vetterf8175862012-04-10 15:50:11 +020015074void intel_modeset_init_hw(struct drm_device *dev)
15075{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015076 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015077 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015078 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015079 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015080}
15081
Jesse Barnes79e53942008-11-07 14:24:08 -080015082void intel_modeset_init(struct drm_device *dev)
15083{
Jesse Barnes652c3932009-08-17 13:31:43 -070015084 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015085 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015086 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015087 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015088
15089 drm_mode_config_init(dev);
15090
15091 dev->mode_config.min_width = 0;
15092 dev->mode_config.min_height = 0;
15093
Dave Airlie019d96c2011-09-29 16:20:42 +010015094 dev->mode_config.preferred_depth = 24;
15095 dev->mode_config.prefer_shadow = 1;
15096
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015097 dev->mode_config.allow_fb_modifiers = true;
15098
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015099 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015100
Jesse Barnesb690e962010-07-19 13:53:12 -070015101 intel_init_quirks(dev);
15102
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015103 intel_init_pm(dev);
15104
Ben Widawskye3c74752013-04-05 13:12:39 -070015105 if (INTEL_INFO(dev)->num_pipes == 0)
15106 return;
15107
Jesse Barnese70236a2009-09-21 10:42:27 -070015108 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015109 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015110
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015111 if (IS_GEN2(dev)) {
15112 dev->mode_config.max_width = 2048;
15113 dev->mode_config.max_height = 2048;
15114 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015115 dev->mode_config.max_width = 4096;
15116 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015117 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015118 dev->mode_config.max_width = 8192;
15119 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015120 }
Damien Lespiau068be562014-03-28 14:17:49 +000015121
Ville Syrjälädc41c152014-08-13 11:57:05 +030015122 if (IS_845G(dev) || IS_I865G(dev)) {
15123 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15124 dev->mode_config.cursor_height = 1023;
15125 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015126 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15127 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15128 } else {
15129 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15130 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15131 }
15132
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015133 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015134
Zhao Yakui28c97732009-10-09 11:39:41 +080015135 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015136 INTEL_INFO(dev)->num_pipes,
15137 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015138
Damien Lespiau055e3932014-08-18 13:49:10 +010015139 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015140 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015141 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015142 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015143 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015144 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015145 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015146 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015147 }
15148
Jesse Barnesf42bb702013-12-16 16:34:23 -080015149 intel_init_dpio(dev);
15150
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015151 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015152
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015153 /* Just disable it once at startup */
15154 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015155 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015156
15157 /* Just in case the BIOS is doing something questionable. */
Paulo Zanoni7733b492015-07-07 15:26:04 -030015158 intel_fbc_disable(dev_priv);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015159
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015160 drm_modeset_lock_all(dev);
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015161 intel_modeset_setup_hw_state(dev);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015162 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015163
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015164 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015165 struct intel_initial_plane_config plane_config = {};
15166
Jesse Barnes46f297f2014-03-07 08:57:48 -080015167 if (!crtc->active)
15168 continue;
15169
Jesse Barnes46f297f2014-03-07 08:57:48 -080015170 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015171 * Note that reserving the BIOS fb up front prevents us
15172 * from stuffing other stolen allocations like the ring
15173 * on top. This prevents some ugliness at boot time, and
15174 * can even allow for smooth boot transitions if the BIOS
15175 * fb is large enough for the active pipe configuration.
15176 */
Maarten Lankhorsteeebeac2015-07-14 12:33:29 +020015177 dev_priv->display.get_initial_plane_config(crtc,
15178 &plane_config);
15179
15180 /*
15181 * If the fb is shared between multiple heads, we'll
15182 * just get the first one.
15183 */
15184 intel_find_initial_plane_obj(crtc, &plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015185 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015186}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015187
Daniel Vetter7fad7982012-07-04 17:51:47 +020015188static void intel_enable_pipe_a(struct drm_device *dev)
15189{
15190 struct intel_connector *connector;
15191 struct drm_connector *crt = NULL;
15192 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015193 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015194
15195 /* We can't just switch on the pipe A, we need to set things up with a
15196 * proper mode and output configuration. As a gross hack, enable pipe A
15197 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015198 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015199 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15200 crt = &connector->base;
15201 break;
15202 }
15203 }
15204
15205 if (!crt)
15206 return;
15207
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015208 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015209 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015210}
15211
Daniel Vetterfa555832012-10-10 23:14:00 +020015212static bool
15213intel_check_plane_mapping(struct intel_crtc *crtc)
15214{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015215 struct drm_device *dev = crtc->base.dev;
15216 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015217 u32 reg, val;
15218
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015219 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015220 return true;
15221
15222 reg = DSPCNTR(!crtc->plane);
15223 val = I915_READ(reg);
15224
15225 if ((val & DISPLAY_PLANE_ENABLE) &&
15226 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15227 return false;
15228
15229 return true;
15230}
15231
Daniel Vetter24929352012-07-02 20:28:59 +020015232static void intel_sanitize_crtc(struct intel_crtc *crtc)
15233{
15234 struct drm_device *dev = crtc->base.dev;
15235 struct drm_i915_private *dev_priv = dev->dev_private;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015236 struct intel_encoder *encoder;
Daniel Vetterfa555832012-10-10 23:14:00 +020015237 u32 reg;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015238 bool enable;
Daniel Vetter24929352012-07-02 20:28:59 +020015239
Daniel Vetter24929352012-07-02 20:28:59 +020015240 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015241 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015242 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15243
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015244 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015245 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015246 if (crtc->active) {
Maarten Lankhorst3a03dfb2015-07-14 13:46:40 +020015247 drm_calc_timestamping_constants(&crtc->base, &crtc->base.hwmode);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015248 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015249 drm_crtc_vblank_on(&crtc->base);
15250 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015251
Daniel Vetter24929352012-07-02 20:28:59 +020015252 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015253 * disable the crtc (and hence change the state) if it is wrong. Note
15254 * that gen4+ has a fixed plane -> pipe mapping. */
15255 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015256 bool plane;
15257
Daniel Vetter24929352012-07-02 20:28:59 +020015258 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15259 crtc->base.base.id);
15260
15261 /* Pipe has the wrong plane attached and the plane is active.
15262 * Temporarily change the plane mapping and disable everything
15263 * ... */
15264 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015265 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015266 crtc->plane = !plane;
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015267 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015268 crtc->plane = plane;
Daniel Vetter24929352012-07-02 20:28:59 +020015269 }
Daniel Vetter24929352012-07-02 20:28:59 +020015270
Daniel Vetter7fad7982012-07-04 17:51:47 +020015271 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15272 crtc->pipe == PIPE_A && !crtc->active) {
15273 /* BIOS forgot to enable pipe A, this mostly happens after
15274 * resume. Force-enable the pipe to fix this, the update_dpms
15275 * call below we restore the pipe to the right state, but leave
15276 * the required bits on. */
15277 intel_enable_pipe_a(dev);
15278 }
15279
Daniel Vetter24929352012-07-02 20:28:59 +020015280 /* Adjust the state of the output pipe according to whether we
15281 * have active connectors/encoders. */
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015282 enable = false;
15283 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
15284 enable |= encoder->connectors_active;
Daniel Vetter24929352012-07-02 20:28:59 +020015285
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015286 if (!enable)
15287 intel_crtc_disable_noatomic(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015288
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015289 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015290
15291 /* This can happen either due to bugs in the get_hw_state
Maarten Lankhorstb17d48e2015-06-12 11:15:39 +020015292 * functions or because of calls to intel_crtc_disable_noatomic,
15293 * or because the pipe is force-enabled due to the
Daniel Vetter24929352012-07-02 20:28:59 +020015294 * pipe A quirk. */
15295 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15296 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015297 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015298 crtc->active ? "enabled" : "disabled");
15299
Maarten Lankhorst4be40c92015-07-14 13:45:32 +020015300 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, NULL) < 0);
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015301 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015302 crtc->base.enabled = crtc->active;
15303
15304 /* Because we only establish the connector -> encoder ->
15305 * crtc links if something is active, this means the
15306 * crtc is now deactivated. Break the links. connector
15307 * -> encoder links are only establish when things are
15308 * actually up, hence no need to break them. */
15309 WARN_ON(crtc->active);
15310
15311 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15312 WARN_ON(encoder->connectors_active);
15313 encoder->base.crtc = NULL;
15314 }
15315 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015316
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015317 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015318 /*
15319 * We start out with underrun reporting disabled to avoid races.
15320 * For correct bookkeeping mark this on active crtcs.
15321 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015322 * Also on gmch platforms we dont have any hardware bits to
15323 * disable the underrun reporting. Which means we need to start
15324 * out with underrun reporting disabled also on inactive pipes,
15325 * since otherwise we'll complain about the garbage we read when
15326 * e.g. coming up after runtime pm.
15327 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015328 * No protection against concurrent access is required - at
15329 * worst a fifo underrun happens which also sets this to false.
15330 */
15331 crtc->cpu_fifo_underrun_disabled = true;
15332 crtc->pch_fifo_underrun_disabled = true;
15333 }
Daniel Vetter24929352012-07-02 20:28:59 +020015334}
15335
15336static void intel_sanitize_encoder(struct intel_encoder *encoder)
15337{
15338 struct intel_connector *connector;
15339 struct drm_device *dev = encoder->base.dev;
15340
15341 /* We need to check both for a crtc link (meaning that the
15342 * encoder is active and trying to read from a pipe) and the
15343 * pipe itself being active. */
15344 bool has_active_crtc = encoder->base.crtc &&
15345 to_intel_crtc(encoder->base.crtc)->active;
15346
15347 if (encoder->connectors_active && !has_active_crtc) {
15348 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15349 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015350 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015351
15352 /* Connector is active, but has no active pipe. This is
15353 * fallout from our resume register restoring. Disable
15354 * the encoder manually again. */
15355 if (encoder->base.crtc) {
15356 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15357 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015358 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015359 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015360 if (encoder->post_disable)
15361 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015362 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015363 encoder->base.crtc = NULL;
15364 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015365
15366 /* Inconsistent output/port/pipe state happens presumably due to
15367 * a bug in one of the get_hw_state functions. Or someplace else
15368 * in our code, like the register restore mess on resume. Clamp
15369 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015370 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015371 if (connector->encoder != encoder)
15372 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015373 connector->base.dpms = DRM_MODE_DPMS_OFF;
15374 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015375 }
15376 }
15377 /* Enabled encoders without active connectors will be fixed in
15378 * the crtc fixup. */
15379}
15380
Imre Deak04098752014-02-18 00:02:16 +020015381void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015382{
15383 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015384 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015385
Imre Deak04098752014-02-18 00:02:16 +020015386 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15387 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15388 i915_disable_vga(dev);
15389 }
15390}
15391
15392void i915_redisable_vga(struct drm_device *dev)
15393{
15394 struct drm_i915_private *dev_priv = dev->dev_private;
15395
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015396 /* This function can be called both from intel_modeset_setup_hw_state or
15397 * at a very early point in our resume sequence, where the power well
15398 * structures are not yet restored. Since this function is at a very
15399 * paranoid "someone might have enabled VGA while we were not looking"
15400 * level, just check if the power well is enabled instead of trying to
15401 * follow the "don't touch the power well if we don't need it" policy
15402 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015403 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015404 return;
15405
Imre Deak04098752014-02-18 00:02:16 +020015406 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015407}
15408
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015409static bool primary_get_hw_state(struct intel_crtc *crtc)
15410{
15411 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15412
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015413 return !!(I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE);
15414}
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015415
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015416static void readout_plane_state(struct intel_crtc *crtc,
15417 struct intel_crtc_state *crtc_state)
15418{
15419 struct intel_plane *p;
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015420 struct intel_plane_state *plane_state;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015421 bool active = crtc_state->base.active;
15422
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015423 for_each_intel_plane(crtc->base.dev, p) {
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015424 if (crtc->pipe != p->pipe)
15425 continue;
15426
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015427 plane_state = to_intel_plane_state(p->base.state);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015428
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015429 if (p->base.type == DRM_PLANE_TYPE_PRIMARY)
15430 plane_state->visible = primary_get_hw_state(crtc);
15431 else {
15432 if (active)
15433 p->disable_plane(&p->base, &crtc->base);
Maarten Lankhorste435d6e2015-07-13 16:30:15 +020015434
Maarten Lankhorst4cf0ebb2015-07-13 16:30:20 +020015435 plane_state->visible = false;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015436 }
15437 }
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015438}
15439
Daniel Vetter30e984d2013-06-05 13:34:17 +020015440static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015441{
15442 struct drm_i915_private *dev_priv = dev->dev_private;
15443 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015444 struct intel_crtc *crtc;
15445 struct intel_encoder *encoder;
15446 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015447 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015448
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015449 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb06f8b02015-07-14 13:42:49 +020015450 __drm_atomic_helper_crtc_destroy_state(&crtc->base, crtc->base.state);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015451 memset(crtc->config, 0, sizeof(*crtc->config));
Maarten Lankhorstf7217902015-06-10 10:24:20 +020015452 crtc->config->base.crtc = &crtc->base;
Daniel Vetter3b117c82013-04-17 20:15:07 +020015453
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015454 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015455
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015456 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015457 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015458
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015459 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015460 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015461
Maarten Lankhorst5c1e3422015-07-14 15:58:28 +020015462 memset(&crtc->base.mode, 0, sizeof(crtc->base.mode));
15463 if (crtc->base.state->active) {
15464 intel_mode_from_pipe_config(&crtc->base.mode, crtc->config);
15465 intel_mode_from_pipe_config(&crtc->base.state->adjusted_mode, crtc->config);
15466 WARN_ON(drm_atomic_set_mode_for_crtc(crtc->base.state, &crtc->base.mode));
15467
15468 /*
15469 * The initial mode needs to be set in order to keep
15470 * the atomic core happy. It wants a valid mode if the
15471 * crtc's enabled, so we do the above call.
15472 *
15473 * At this point some state updated by the connectors
15474 * in their ->detect() callback has not run yet, so
15475 * no recalculation can be done yet.
15476 *
15477 * Even if we could do a recalculation and modeset
15478 * right now it would cause a double modeset if
15479 * fbdev or userspace chooses a different initial mode.
15480 *
15481 * So to prevent the double modeset, fail the memcmp
15482 * test in drm_atomic_set_mode_for_crtc to get a new
15483 * mode blob, and compare if the mode blob changed
15484 * when the PIPE_CONFIG_QUIRK_INHERITED_MODE quirk is
15485 * set.
15486 *
15487 * If that happens, someone indicated they wanted a
15488 * mode change, which means it's safe to do a full
15489 * recalculation.
15490 */
15491 crtc->base.state->mode.private_flags = ~0;
15492 }
15493
15494 crtc->base.hwmode = crtc->config->base.adjusted_mode;
Maarten Lankhorstd032ffa2015-06-15 12:33:51 +020015495 readout_plane_state(crtc, to_intel_crtc_state(crtc->base.state));
Daniel Vetter24929352012-07-02 20:28:59 +020015496
15497 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15498 crtc->base.base.id,
15499 crtc->active ? "enabled" : "disabled");
15500 }
15501
Daniel Vetter53589012013-06-05 13:34:16 +020015502 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15503 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15504
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015505 pll->on = pll->get_hw_state(dev_priv, pll,
15506 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015507 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015508 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015509 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015510 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015511 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015512 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015513 }
Daniel Vetter53589012013-06-05 13:34:16 +020015514 }
Daniel Vetter53589012013-06-05 13:34:16 +020015515
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015516 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015517 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015518
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015519 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015520 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015521 }
15522
Damien Lespiaub2784e12014-08-05 11:29:37 +010015523 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015524 pipe = 0;
15525
15526 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015527 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15528 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015529 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015530 } else {
15531 encoder->base.crtc = NULL;
15532 }
15533
15534 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015535 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015536 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015537 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015538 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015539 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015540 }
15541
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015542 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015543 if (connector->get_hw_state(connector)) {
15544 connector->base.dpms = DRM_MODE_DPMS_ON;
15545 connector->encoder->connectors_active = true;
15546 connector->base.encoder = &connector->encoder->base;
15547 } else {
15548 connector->base.dpms = DRM_MODE_DPMS_OFF;
15549 connector->base.encoder = NULL;
15550 }
15551 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15552 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015553 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015554 connector->base.encoder ? "enabled" : "disabled");
15555 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015556}
15557
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015558/* Scan out the current hw modeset state,
15559 * and sanitizes it to the current state
15560 */
15561static void
15562intel_modeset_setup_hw_state(struct drm_device *dev)
Daniel Vetter30e984d2013-06-05 13:34:17 +020015563{
15564 struct drm_i915_private *dev_priv = dev->dev_private;
15565 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015566 struct intel_crtc *crtc;
15567 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015568 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015569
15570 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015571
15572 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015573 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015574 intel_sanitize_encoder(encoder);
15575 }
15576
Damien Lespiau055e3932014-08-18 13:49:10 +010015577 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015578 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15579 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015580 intel_dump_pipe_config(crtc, crtc->config,
15581 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015582 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015583
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015584 intel_modeset_update_connector_atomic_state(dev);
15585
Daniel Vetter35c95372013-07-17 06:55:04 +020015586 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15587 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15588
15589 if (!pll->on || pll->active)
15590 continue;
15591
15592 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15593
15594 pll->disable(dev_priv, pll);
15595 pll->on = false;
15596 }
15597
Ville Syrjälä26e1fe42015-06-24 22:00:06 +030015598 if (IS_VALLEYVIEW(dev))
Ville Syrjälä6eb1a682015-06-24 22:00:03 +030015599 vlv_wm_get_hw_state(dev);
15600 else if (IS_GEN9(dev))
Pradeep Bhat30789992014-11-04 17:06:45 +000015601 skl_wm_get_hw_state(dev);
15602 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015603 ilk_wm_get_hw_state(dev);
15604
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015605 intel_modeset_update_staged_output_state(dev);
15606}
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015607
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015608void intel_display_resume(struct drm_device *dev)
15609{
15610 struct drm_atomic_state *state = drm_atomic_state_alloc(dev);
15611 struct intel_connector *conn;
15612 struct intel_plane *plane;
15613 struct drm_crtc *crtc;
15614 int ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020015615
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015616 if (!state)
15617 return;
15618
15619 state->acquire_ctx = dev->mode_config.acquire_ctx;
15620
15621 /* preserve complete old state, including dpll */
15622 intel_atomic_get_shared_dpll_state(state);
15623
15624 for_each_crtc(dev, crtc) {
15625 struct drm_crtc_state *crtc_state =
15626 drm_atomic_get_crtc_state(state, crtc);
15627
15628 ret = PTR_ERR_OR_ZERO(crtc_state);
15629 if (ret)
15630 goto err;
15631
15632 /* force a restore */
15633 crtc_state->mode_changed = true;
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015634 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015635
Maarten Lankhorst043e9bd2015-07-13 16:30:25 +020015636 for_each_intel_plane(dev, plane) {
15637 ret = PTR_ERR_OR_ZERO(drm_atomic_get_plane_state(state, &plane->base));
15638 if (ret)
15639 goto err;
15640 }
15641
15642 for_each_intel_connector(dev, conn) {
15643 ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(state, &conn->base));
15644 if (ret)
15645 goto err;
15646 }
15647
15648 intel_modeset_setup_hw_state(dev);
15649
15650 i915_redisable_vga(dev);
15651 ret = intel_set_mode(state);
15652 if (!ret)
15653 return;
15654
15655err:
15656 DRM_ERROR("Restoring old state failed with %i\n", ret);
15657 drm_atomic_state_free(state);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015658}
15659
15660void intel_modeset_gem_init(struct drm_device *dev)
15661{
Jesse Barnes92122782014-10-09 12:57:42 -070015662 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015663 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015664 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015665 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015666
Imre Deakae484342014-03-31 15:10:44 +030015667 mutex_lock(&dev->struct_mutex);
15668 intel_init_gt_powersave(dev);
15669 mutex_unlock(&dev->struct_mutex);
15670
Jesse Barnes92122782014-10-09 12:57:42 -070015671 /*
15672 * There may be no VBT; and if the BIOS enabled SSC we can
15673 * just keep using it to avoid unnecessary flicker. Whereas if the
15674 * BIOS isn't using it, don't assume it will work even if the VBT
15675 * indicates as much.
15676 */
15677 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15678 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15679 DREF_SSC1_ENABLE);
15680
Chris Wilson1833b132012-05-09 11:56:28 +010015681 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015682
15683 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015684
15685 /*
15686 * Make sure any fbs we allocated at startup are properly
15687 * pinned & fenced. When we do the allocation it's too early
15688 * for this.
15689 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015690 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015691 obj = intel_fb_obj(c->primary->fb);
15692 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015693 continue;
15694
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015695 mutex_lock(&dev->struct_mutex);
15696 ret = intel_pin_and_fence_fb_obj(c->primary,
15697 c->primary->fb,
15698 c->primary->state,
John Harrison91af1272015-06-18 13:14:56 +010015699 NULL, NULL);
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015700 mutex_unlock(&dev->struct_mutex);
15701 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015702 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15703 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015704 drm_framebuffer_unreference(c->primary->fb);
15705 c->primary->fb = NULL;
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015706 c->primary->crtc = c->primary->state->crtc = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015707 update_state_fb(c->primary);
Maarten Lankhorst36750f22015-06-01 12:49:54 +020015708 c->state->plane_mask &= ~(1 << drm_plane_index(c->primary));
Jesse Barnes484b41d2014-03-07 08:57:55 -080015709 }
15710 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015711
15712 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015713}
15714
Imre Deak4932e2c2014-02-11 17:12:48 +020015715void intel_connector_unregister(struct intel_connector *intel_connector)
15716{
15717 struct drm_connector *connector = &intel_connector->base;
15718
15719 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015720 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015721}
15722
Jesse Barnes79e53942008-11-07 14:24:08 -080015723void intel_modeset_cleanup(struct drm_device *dev)
15724{
Jesse Barnes652c3932009-08-17 13:31:43 -070015725 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015726 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015727
Imre Deak2eb52522014-11-19 15:30:05 +020015728 intel_disable_gt_powersave(dev);
15729
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015730 intel_backlight_unregister(dev);
15731
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015732 /*
15733 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015734 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015735 * experience fancy races otherwise.
15736 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015737 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015738
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015739 /*
15740 * Due to the hpd irq storm handling the hotplug work can re-arm the
15741 * poll handlers. Hence disable polling after hpd handling is shut down.
15742 */
Keith Packardf87ea762010-10-03 19:36:26 -070015743 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015744
Jesse Barnes723bfd72010-10-07 16:01:13 -070015745 intel_unregister_dsm_handler();
15746
Paulo Zanoni7733b492015-07-07 15:26:04 -030015747 intel_fbc_disable(dev_priv);
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015748
Chris Wilson1630fe72011-07-08 12:22:42 +010015749 /* flush any delayed tasks or pending work */
15750 flush_scheduled_work();
15751
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015752 /* destroy the backlight and sysfs files before encoders/connectors */
15753 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015754 struct intel_connector *intel_connector;
15755
15756 intel_connector = to_intel_connector(connector);
15757 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015758 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015759
Jesse Barnes79e53942008-11-07 14:24:08 -080015760 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015761
15762 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015763
15764 mutex_lock(&dev->struct_mutex);
15765 intel_cleanup_gt_powersave(dev);
15766 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015767}
15768
Dave Airlie28d52042009-09-21 14:33:58 +100015769/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015770 * Return which encoder is currently attached for connector.
15771 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015772struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015773{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015774 return &intel_attached_encoder(connector)->base;
15775}
Jesse Barnes79e53942008-11-07 14:24:08 -080015776
Chris Wilsondf0e9242010-09-09 16:20:55 +010015777void intel_connector_attach_encoder(struct intel_connector *connector,
15778 struct intel_encoder *encoder)
15779{
15780 connector->encoder = encoder;
15781 drm_mode_connector_attach_encoder(&connector->base,
15782 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015783}
Dave Airlie28d52042009-09-21 14:33:58 +100015784
15785/*
15786 * set vga decode state - true == enable VGA decode
15787 */
15788int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15789{
15790 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015791 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015792 u16 gmch_ctrl;
15793
Chris Wilson75fa0412014-02-07 18:37:02 -020015794 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15795 DRM_ERROR("failed to read control word\n");
15796 return -EIO;
15797 }
15798
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015799 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15800 return 0;
15801
Dave Airlie28d52042009-09-21 14:33:58 +100015802 if (state)
15803 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15804 else
15805 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015806
15807 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15808 DRM_ERROR("failed to write control word\n");
15809 return -EIO;
15810 }
15811
Dave Airlie28d52042009-09-21 14:33:58 +100015812 return 0;
15813}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015814
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015815struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015816
15817 u32 power_well_driver;
15818
Chris Wilson63b66e52013-08-08 15:12:06 +020015819 int num_transcoders;
15820
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015821 struct intel_cursor_error_state {
15822 u32 control;
15823 u32 position;
15824 u32 base;
15825 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015826 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015827
15828 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015829 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015830 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015831 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015832 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015833
15834 struct intel_plane_error_state {
15835 u32 control;
15836 u32 stride;
15837 u32 size;
15838 u32 pos;
15839 u32 addr;
15840 u32 surface;
15841 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015842 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015843
15844 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015845 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015846 enum transcoder cpu_transcoder;
15847
15848 u32 conf;
15849
15850 u32 htotal;
15851 u32 hblank;
15852 u32 hsync;
15853 u32 vtotal;
15854 u32 vblank;
15855 u32 vsync;
15856 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015857};
15858
15859struct intel_display_error_state *
15860intel_display_capture_error_state(struct drm_device *dev)
15861{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015862 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015863 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015864 int transcoders[] = {
15865 TRANSCODER_A,
15866 TRANSCODER_B,
15867 TRANSCODER_C,
15868 TRANSCODER_EDP,
15869 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015870 int i;
15871
Chris Wilson63b66e52013-08-08 15:12:06 +020015872 if (INTEL_INFO(dev)->num_pipes == 0)
15873 return NULL;
15874
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015875 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015876 if (error == NULL)
15877 return NULL;
15878
Imre Deak190be112013-11-25 17:15:31 +020015879 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015880 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15881
Damien Lespiau055e3932014-08-18 13:49:10 +010015882 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015883 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015884 __intel_display_power_is_enabled(dev_priv,
15885 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015886 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015887 continue;
15888
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015889 error->cursor[i].control = I915_READ(CURCNTR(i));
15890 error->cursor[i].position = I915_READ(CURPOS(i));
15891 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015892
15893 error->plane[i].control = I915_READ(DSPCNTR(i));
15894 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015895 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015896 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015897 error->plane[i].pos = I915_READ(DSPPOS(i));
15898 }
Paulo Zanonica291362013-03-06 20:03:14 -030015899 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15900 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015901 if (INTEL_INFO(dev)->gen >= 4) {
15902 error->plane[i].surface = I915_READ(DSPSURF(i));
15903 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15904 }
15905
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015906 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015907
Sonika Jindal3abfce72014-07-21 15:23:43 +053015908 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015909 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015910 }
15911
15912 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15913 if (HAS_DDI(dev_priv->dev))
15914 error->num_transcoders++; /* Account for eDP. */
15915
15916 for (i = 0; i < error->num_transcoders; i++) {
15917 enum transcoder cpu_transcoder = transcoders[i];
15918
Imre Deakddf9c532013-11-27 22:02:02 +020015919 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015920 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015921 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015922 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015923 continue;
15924
Chris Wilson63b66e52013-08-08 15:12:06 +020015925 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15926
15927 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15928 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15929 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15930 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15931 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15932 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15933 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015934 }
15935
15936 return error;
15937}
15938
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015939#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15940
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015941void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015942intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015943 struct drm_device *dev,
15944 struct intel_display_error_state *error)
15945{
Damien Lespiau055e3932014-08-18 13:49:10 +010015946 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015947 int i;
15948
Chris Wilson63b66e52013-08-08 15:12:06 +020015949 if (!error)
15950 return;
15951
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015952 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015953 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015954 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015955 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015956 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015957 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015958 err_printf(m, " Power: %s\n",
15959 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015960 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015961 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015962
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015963 err_printf(m, "Plane [%d]:\n", i);
15964 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15965 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015966 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015967 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15968 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015969 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015970 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015971 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015972 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015973 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15974 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015975 }
15976
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015977 err_printf(m, "Cursor [%d]:\n", i);
15978 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15979 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15980 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015981 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015982
15983 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015984 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015985 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015986 err_printf(m, " Power: %s\n",
15987 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015988 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15989 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15990 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15991 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15992 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15993 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15994 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15995 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015996}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015997
15998void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15999{
16000 struct intel_crtc *crtc;
16001
16002 for_each_intel_crtc(dev, crtc) {
16003 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016004
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016005 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016006
16007 work = crtc->unpin_work;
16008
16009 if (work && work->event &&
16010 work->event->base.file_priv == file) {
16011 kfree(work->event);
16012 work->event = NULL;
16013 }
16014
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020016015 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030016016 }
16017}