blob: c4ccc376ae44e122b379d99b67343cbbb8d41cf9 [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 Oliveira8c7b5cc2015-04-21 17:13:19 +030089static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020090 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080091static int intel_framebuffer_init(struct drm_device *dev,
92 struct intel_framebuffer *ifb,
93 struct drm_mode_fb_cmd2 *mode_cmd,
94 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020095static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
96static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020097static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070098 struct intel_link_m_n *m_n,
99 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200100static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200101static void haswell_set_pipeconf(struct drm_crtc *crtc);
102static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200103static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200104 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200105static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200106 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800107static void intel_begin_crtc_commit(struct drm_crtc *crtc);
108static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700109static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
110 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200111static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
112 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300113static void intel_crtc_enable_planes(struct drm_crtc *crtc);
114static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100115
Dave Airlie0e32b392014-05-02 14:02:48 +1000116static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
117{
118 if (!connector->mst_port)
119 return connector->encoder;
120 else
121 return &connector->mst_port->mst_encoders[pipe]->base;
122}
123
Jesse Barnes79e53942008-11-07 14:24:08 -0800124typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126} intel_range_t;
127
128typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400129 int dot_limit;
130 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800131} intel_p2_t;
132
Ma Lingd4906092009-03-18 20:13:27 +0800133typedef struct intel_limit intel_limit_t;
134struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400135 intel_range_t dot, vco, n, m, m1, m2, p, p1;
136 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Daniel Vetterd2acd212012-10-20 20:57:43 +0200139int
140intel_pch_rawclk(struct drm_device *dev)
141{
142 struct drm_i915_private *dev_priv = dev->dev_private;
143
144 WARN_ON(!HAS_PCH_SPLIT(dev));
145
146 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
147}
148
Chris Wilson021357a2010-09-07 20:54:59 +0100149static inline u32 /* units of 100MHz */
150intel_fdi_link_freq(struct drm_device *dev)
151{
Chris Wilson8b99e682010-10-13 09:59:17 +0100152 if (IS_GEN5(dev)) {
153 struct drm_i915_private *dev_priv = dev->dev_private;
154 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
155 } else
156 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100157}
158
Daniel Vetter5d536e22013-07-06 12:52:06 +0200159static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200161 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200162 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
Daniel Vetter5d536e22013-07-06 12:52:06 +0200172static const intel_limit_t intel_limits_i8xx_dvo = {
173 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200174 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200175 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200176 .m = { .min = 96, .max = 140 },
177 .m1 = { .min = 18, .max = 26 },
178 .m2 = { .min = 6, .max = 16 },
179 .p = { .min = 4, .max = 128 },
180 .p1 = { .min = 2, .max = 33 },
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 4, .p2_fast = 4 },
183};
184
Keith Packarde4b36692009-06-05 19:22:17 -0700185static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200187 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200188 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400189 .m = { .min = 96, .max = 140 },
190 .m1 = { .min = 18, .max = 26 },
191 .m2 = { .min = 6, .max = 16 },
192 .p = { .min = 4, .max = 128 },
193 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700194 .p2 = { .dot_limit = 165000,
195 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700196};
Eric Anholt273e27c2011-03-30 13:01:10 -0700197
Keith Packarde4b36692009-06-05 19:22:17 -0700198static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400199 .dot = { .min = 20000, .max = 400000 },
200 .vco = { .min = 1400000, .max = 2800000 },
201 .n = { .min = 1, .max = 6 },
202 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100203 .m1 = { .min = 8, .max = 18 },
204 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400205 .p = { .min = 5, .max = 80 },
206 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700207 .p2 = { .dot_limit = 200000,
208 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700209};
210
211static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400212 .dot = { .min = 20000, .max = 400000 },
213 .vco = { .min = 1400000, .max = 2800000 },
214 .n = { .min = 1, .max = 6 },
215 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100216 .m1 = { .min = 8, .max = 18 },
217 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400218 .p = { .min = 7, .max = 98 },
219 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 .p2 = { .dot_limit = 112000,
221 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700222};
223
Eric Anholt273e27c2011-03-30 13:01:10 -0700224
Keith Packarde4b36692009-06-05 19:22:17 -0700225static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700226 .dot = { .min = 25000, .max = 270000 },
227 .vco = { .min = 1750000, .max = 3500000},
228 .n = { .min = 1, .max = 4 },
229 .m = { .min = 104, .max = 138 },
230 .m1 = { .min = 17, .max = 23 },
231 .m2 = { .min = 5, .max = 11 },
232 .p = { .min = 10, .max = 30 },
233 .p1 = { .min = 1, .max = 3},
234 .p2 = { .dot_limit = 270000,
235 .p2_slow = 10,
236 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800237 },
Keith Packarde4b36692009-06-05 19:22:17 -0700238};
239
240static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700241 .dot = { .min = 22000, .max = 400000 },
242 .vco = { .min = 1750000, .max = 3500000},
243 .n = { .min = 1, .max = 4 },
244 .m = { .min = 104, .max = 138 },
245 .m1 = { .min = 16, .max = 23 },
246 .m2 = { .min = 5, .max = 11 },
247 .p = { .min = 5, .max = 80 },
248 .p1 = { .min = 1, .max = 8},
249 .p2 = { .dot_limit = 165000,
250 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700251};
252
253static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700254 .dot = { .min = 20000, .max = 115000 },
255 .vco = { .min = 1750000, .max = 3500000 },
256 .n = { .min = 1, .max = 3 },
257 .m = { .min = 104, .max = 138 },
258 .m1 = { .min = 17, .max = 23 },
259 .m2 = { .min = 5, .max = 11 },
260 .p = { .min = 28, .max = 112 },
261 .p1 = { .min = 2, .max = 8 },
262 .p2 = { .dot_limit = 0,
263 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800264 },
Keith Packarde4b36692009-06-05 19:22:17 -0700265};
266
267static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700268 .dot = { .min = 80000, .max = 224000 },
269 .vco = { .min = 1750000, .max = 3500000 },
270 .n = { .min = 1, .max = 3 },
271 .m = { .min = 104, .max = 138 },
272 .m1 = { .min = 17, .max = 23 },
273 .m2 = { .min = 5, .max = 11 },
274 .p = { .min = 14, .max = 42 },
275 .p1 = { .min = 2, .max = 6 },
276 .p2 = { .dot_limit = 0,
277 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800278 },
Keith Packarde4b36692009-06-05 19:22:17 -0700279};
280
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500281static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .dot = { .min = 20000, .max = 400000},
283 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .n = { .min = 3, .max = 6 },
286 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .m1 = { .min = 0, .max = 0 },
289 .m2 = { .min = 0, .max = 254 },
290 .p = { .min = 5, .max = 80 },
291 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700292 .p2 = { .dot_limit = 200000,
293 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700294};
295
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500296static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .dot = { .min = 20000, .max = 400000 },
298 .vco = { .min = 1700000, .max = 3500000 },
299 .n = { .min = 3, .max = 6 },
300 .m = { .min = 2, .max = 256 },
301 .m1 = { .min = 0, .max = 0 },
302 .m2 = { .min = 0, .max = 254 },
303 .p = { .min = 7, .max = 112 },
304 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .p2 = { .dot_limit = 112000,
306 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700307};
308
Eric Anholt273e27c2011-03-30 13:01:10 -0700309/* Ironlake / Sandybridge
310 *
311 * We calculate clock using (register_value + 2) for N/M1/M2, so here
312 * the range value for them is (actual_value - 2).
313 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800314static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 5 },
318 .m = { .min = 79, .max = 127 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 5, .max = 80 },
322 .p1 = { .min = 1, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800327static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700328 .dot = { .min = 25000, .max = 350000 },
329 .vco = { .min = 1760000, .max = 3510000 },
330 .n = { .min = 1, .max = 3 },
331 .m = { .min = 79, .max = 118 },
332 .m1 = { .min = 12, .max = 22 },
333 .m2 = { .min = 5, .max = 9 },
334 .p = { .min = 28, .max = 112 },
335 .p1 = { .min = 2, .max = 8 },
336 .p2 = { .dot_limit = 225000,
337 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800338};
339
340static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700341 .dot = { .min = 25000, .max = 350000 },
342 .vco = { .min = 1760000, .max = 3510000 },
343 .n = { .min = 1, .max = 3 },
344 .m = { .min = 79, .max = 127 },
345 .m1 = { .min = 12, .max = 22 },
346 .m2 = { .min = 5, .max = 9 },
347 .p = { .min = 14, .max = 56 },
348 .p1 = { .min = 2, .max = 8 },
349 .p2 = { .dot_limit = 225000,
350 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351};
352
Eric Anholt273e27c2011-03-30 13:01:10 -0700353/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800354static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700355 .dot = { .min = 25000, .max = 350000 },
356 .vco = { .min = 1760000, .max = 3510000 },
357 .n = { .min = 1, .max = 2 },
358 .m = { .min = 79, .max = 126 },
359 .m1 = { .min = 12, .max = 22 },
360 .m2 = { .min = 5, .max = 9 },
361 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400362 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .p2 = { .dot_limit = 225000,
364 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800365};
366
367static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700368 .dot = { .min = 25000, .max = 350000 },
369 .vco = { .min = 1760000, .max = 3510000 },
370 .n = { .min = 1, .max = 3 },
371 .m = { .min = 79, .max = 126 },
372 .m1 = { .min = 12, .max = 22 },
373 .m2 = { .min = 5, .max = 9 },
374 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400375 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700376 .p2 = { .dot_limit = 225000,
377 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800378};
379
Ville Syrjälädc730512013-09-24 21:26:30 +0300380static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300381 /*
382 * These are the data rate limits (measured in fast clocks)
383 * since those are the strictest limits we have. The fast
384 * clock and actual rate limits are more relaxed, so checking
385 * them would make no difference.
386 */
387 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200388 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700389 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700390 .m1 = { .min = 2, .max = 3 },
391 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300392 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300393 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700394};
395
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300396static const intel_limit_t intel_limits_chv = {
397 /*
398 * These are the data rate limits (measured in fast clocks)
399 * since those are the strictest limits we have. The fast
400 * clock and actual rate limits are more relaxed, so checking
401 * them would make no difference.
402 */
403 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200404 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300405 .n = { .min = 1, .max = 1 },
406 .m1 = { .min = 2, .max = 2 },
407 .m2 = { .min = 24 << 22, .max = 175 << 22 },
408 .p1 = { .min = 2, .max = 4 },
409 .p2 = { .p2_slow = 1, .p2_fast = 14 },
410};
411
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200412static const intel_limit_t intel_limits_bxt = {
413 /* FIXME: find real dot limits */
414 .dot = { .min = 0, .max = INT_MAX },
415 .vco = { .min = 4800000, .max = 6480000 },
416 .n = { .min = 1, .max = 1 },
417 .m1 = { .min = 2, .max = 2 },
418 /* FIXME: find real m2 limits */
419 .m2 = { .min = 2 << 22, .max = 255 << 22 },
420 .p1 = { .min = 2, .max = 4 },
421 .p2 = { .p2_slow = 1, .p2_fast = 20 },
422};
423
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300424static void vlv_clock(int refclk, intel_clock_t *clock)
425{
426 clock->m = clock->m1 * clock->m2;
427 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200428 if (WARN_ON(clock->n == 0 || clock->p == 0))
429 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300430 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
431 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300432}
433
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300434/**
435 * Returns whether any output on the specified pipe is of the specified type
436 */
Damien Lespiau40935612014-10-29 11:16:59 +0000437bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300438{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300439 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300440 struct intel_encoder *encoder;
441
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300442 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300443 if (encoder->type == type)
444 return true;
445
446 return false;
447}
448
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200449/**
450 * Returns whether any output on the specified pipe will have the specified
451 * type after a staged modeset is complete, i.e., the same as
452 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
453 * encoder->crtc.
454 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
456 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300459 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200460 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200461 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200462 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200463
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300464 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465 if (connector_state->crtc != crtc_state->base.crtc)
466 continue;
467
468 num_connectors++;
469
470 encoder = to_intel_encoder(connector_state->best_encoder);
471 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200472 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200473 }
474
475 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200476
477 return false;
478}
479
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200480static const intel_limit_t *
481intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800482{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200483 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800484 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800485
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200486 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100487 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000488 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800489 limit = &intel_limits_ironlake_dual_lvds_100m;
490 else
491 limit = &intel_limits_ironlake_dual_lvds;
492 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000493 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800494 limit = &intel_limits_ironlake_single_lvds_100m;
495 else
496 limit = &intel_limits_ironlake_single_lvds;
497 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200498 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800499 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800500
501 return limit;
502}
503
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200504static const intel_limit_t *
505intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800506{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200507 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800508 const intel_limit_t *limit;
509
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100511 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 else
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200515 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
516 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700519 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800520 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700521 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800522
523 return limit;
524}
525
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200526static const intel_limit_t *
527intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800528{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200529 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800530 const intel_limit_t *limit;
531
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200532 if (IS_BROXTON(dev))
533 limit = &intel_limits_bxt;
534 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800536 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500538 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200539 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500540 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800541 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500542 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300543 } else if (IS_CHERRYVIEW(dev)) {
544 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700545 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300546 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100547 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100549 limit = &intel_limits_i9xx_lvds;
550 else
551 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700554 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700556 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200557 else
558 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800559 }
560 return limit;
561}
562
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500563/* m1 is reserved as 0 in Pineview, n is a ring counter */
564static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800565{
Shaohua Li21778322009-02-23 15:19:16 +0800566 clock->m = clock->m2 + 2;
567 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200568 if (WARN_ON(clock->n == 0 || clock->p == 0))
569 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300570 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
571 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800572}
573
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200574static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
575{
576 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
577}
578
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200579static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800580{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200581 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200583 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
584 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300585 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
586 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800587}
588
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300589static void chv_clock(int refclk, intel_clock_t *clock)
590{
591 clock->m = clock->m1 * clock->m2;
592 clock->p = clock->p1 * clock->p2;
593 if (WARN_ON(clock->n == 0 || clock->p == 0))
594 return;
595 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
596 clock->n << 22);
597 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
598}
599
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800600#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800601/**
602 * Returns whether the given set of divisors are valid for a given refclk with
603 * the given connectors.
604 */
605
Chris Wilson1b894b52010-12-14 20:04:54 +0000606static bool intel_PLL_is_valid(struct drm_device *dev,
607 const intel_limit_t *limit,
608 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800609{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300610 if (clock->n < limit->n.min || limit->n.max < clock->n)
611 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400617 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200619 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300620 if (clock->m1 <= clock->m2)
621 INTELPllInvalid("m1 <= m2\n");
622
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200623 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300624 if (clock->p < limit->p.min || limit->p.max < clock->p)
625 INTELPllInvalid("p out of range\n");
626 if (clock->m < limit->m.min || limit->m.max < clock->m)
627 INTELPllInvalid("m out of range\n");
628 }
629
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400631 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800637
638 return true;
639}
640
Ma Lingd4906092009-03-18 20:13:27 +0800641static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200642i9xx_find_best_dpll(const intel_limit_t *limit,
643 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800644 int target, int refclk, intel_clock_t *match_clock,
645 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800646{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200647 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300648 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 int err = target;
651
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200652 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100654 * For LVDS just rely on its current settings for dual-channel.
655 * We haven't figured out how to reliably set up different
656 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100658 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 clock.p2 = limit->p2.p2_fast;
660 else
661 clock.p2 = limit->p2.p2_slow;
662 } else {
663 if (target < limit->p2.dot_limit)
664 clock.p2 = limit->p2.p2_slow;
665 else
666 clock.p2 = limit->p2.p2_fast;
667 }
668
Akshay Joshi0206e352011-08-16 15:34:10 -0400669 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800670
Zhao Yakui42158662009-11-20 11:24:18 +0800671 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
672 clock.m1++) {
673 for (clock.m2 = limit->m2.min;
674 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200675 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800676 break;
677 for (clock.n = limit->n.min;
678 clock.n <= limit->n.max; clock.n++) {
679 for (clock.p1 = limit->p1.min;
680 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 int this_err;
682
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200683 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000684 if (!intel_PLL_is_valid(dev, limit,
685 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800687 if (match_clock &&
688 clock.p != match_clock->p)
689 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
691 this_err = abs(clock.dot - target);
692 if (this_err < err) {
693 *best_clock = clock;
694 err = this_err;
695 }
696 }
697 }
698 }
699 }
700
701 return (err != target);
702}
703
Ma Lingd4906092009-03-18 20:13:27 +0800704static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200705pnv_find_best_dpll(const intel_limit_t *limit,
706 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200707 int target, int refclk, intel_clock_t *match_clock,
708 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200709{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200710 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300711 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200712 intel_clock_t clock;
713 int err = target;
714
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200715 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200716 /*
717 * For LVDS just rely on its current settings for dual-channel.
718 * We haven't figured out how to reliably set up different
719 * single/dual channel state, if we even can.
720 */
721 if (intel_is_dual_link_lvds(dev))
722 clock.p2 = limit->p2.p2_fast;
723 else
724 clock.p2 = limit->p2.p2_slow;
725 } else {
726 if (target < limit->p2.dot_limit)
727 clock.p2 = limit->p2.p2_slow;
728 else
729 clock.p2 = limit->p2.p2_fast;
730 }
731
732 memset(best_clock, 0, sizeof(*best_clock));
733
734 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
735 clock.m1++) {
736 for (clock.m2 = limit->m2.min;
737 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 for (clock.n = limit->n.min;
739 clock.n <= limit->n.max; clock.n++) {
740 for (clock.p1 = limit->p1.min;
741 clock.p1 <= limit->p1.max; clock.p1++) {
742 int this_err;
743
744 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800745 if (!intel_PLL_is_valid(dev, limit,
746 &clock))
747 continue;
748 if (match_clock &&
749 clock.p != match_clock->p)
750 continue;
751
752 this_err = abs(clock.dot - target);
753 if (this_err < err) {
754 *best_clock = clock;
755 err = this_err;
756 }
757 }
758 }
759 }
760 }
761
762 return (err != target);
763}
764
Ma Lingd4906092009-03-18 20:13:27 +0800765static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200766g4x_find_best_dpll(const intel_limit_t *limit,
767 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800770{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200771 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300772 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800773 intel_clock_t clock;
774 int max_n;
775 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400776 /* approximately equals target * 0.00585 */
777 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800778 found = false;
779
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200780 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100781 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800782 clock.p2 = limit->p2.p2_fast;
783 else
784 clock.p2 = limit->p2.p2_slow;
785 } else {
786 if (target < limit->p2.dot_limit)
787 clock.p2 = limit->p2.p2_slow;
788 else
789 clock.p2 = limit->p2.p2_fast;
790 }
791
792 memset(best_clock, 0, sizeof(*best_clock));
793 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200794 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800795 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200796 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800797 for (clock.m1 = limit->m1.max;
798 clock.m1 >= limit->m1.min; clock.m1--) {
799 for (clock.m2 = limit->m2.max;
800 clock.m2 >= limit->m2.min; clock.m2--) {
801 for (clock.p1 = limit->p1.max;
802 clock.p1 >= limit->p1.min; clock.p1--) {
803 int this_err;
804
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200805 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000806 if (!intel_PLL_is_valid(dev, limit,
807 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800808 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000809
810 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800811 if (this_err < err_most) {
812 *best_clock = clock;
813 err_most = this_err;
814 max_n = clock.n;
815 found = true;
816 }
817 }
818 }
819 }
820 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800821 return found;
822}
Ma Lingd4906092009-03-18 20:13:27 +0800823
Imre Deakd5dd62b2015-03-17 11:40:03 +0200824/*
825 * Check if the calculated PLL configuration is more optimal compared to the
826 * best configuration and error found so far. Return the calculated error.
827 */
828static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
829 const intel_clock_t *calculated_clock,
830 const intel_clock_t *best_clock,
831 unsigned int best_error_ppm,
832 unsigned int *error_ppm)
833{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200834 /*
835 * For CHV ignore the error and consider only the P value.
836 * Prefer a bigger P value based on HW requirements.
837 */
838 if (IS_CHERRYVIEW(dev)) {
839 *error_ppm = 0;
840
841 return calculated_clock->p > best_clock->p;
842 }
843
Imre Deak24be4e42015-03-17 11:40:04 +0200844 if (WARN_ON_ONCE(!target_freq))
845 return false;
846
Imre Deakd5dd62b2015-03-17 11:40:03 +0200847 *error_ppm = div_u64(1000000ULL *
848 abs(target_freq - calculated_clock->dot),
849 target_freq);
850 /*
851 * Prefer a better P value over a better (smaller) error if the error
852 * is small. Ensure this preference for future configurations too by
853 * setting the error to 0.
854 */
855 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
856 *error_ppm = 0;
857
858 return true;
859 }
860
861 return *error_ppm + 10 < best_error_ppm;
862}
863
Zhenyu Wang2c072452009-06-05 15:38:42 +0800864static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200865vlv_find_best_dpll(const intel_limit_t *limit,
866 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200867 int target, int refclk, intel_clock_t *match_clock,
868 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700869{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200870 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300871 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300872 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300873 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300874 /* min update 19.2 MHz */
875 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300876 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700877
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300878 target *= 5; /* fast clock */
879
880 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700881
882 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300883 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300884 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300885 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300886 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300887 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700888 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300889 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200890 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300891
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
893 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300894
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300895 vlv_clock(refclk, &clock);
896
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300897 if (!intel_PLL_is_valid(dev, limit,
898 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300899 continue;
900
Imre Deakd5dd62b2015-03-17 11:40:03 +0200901 if (!vlv_PLL_is_optimal(dev, target,
902 &clock,
903 best_clock,
904 bestppm, &ppm))
905 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906
Imre Deakd5dd62b2015-03-17 11:40:03 +0200907 *best_clock = clock;
908 bestppm = ppm;
909 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700910 }
911 }
912 }
913 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700914
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300915 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700916}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700917
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300918static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200919chv_find_best_dpll(const intel_limit_t *limit,
920 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300921 int target, int refclk, intel_clock_t *match_clock,
922 intel_clock_t *best_clock)
923{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200924 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300925 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200926 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300927 intel_clock_t clock;
928 uint64_t m2;
929 int found = false;
930
931 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200932 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300933
934 /*
935 * Based on hardware doc, the n always set to 1, and m1 always
936 * set to 2. If requires to support 200Mhz refclk, we need to
937 * revisit this because n may not 1 anymore.
938 */
939 clock.n = 1, clock.m1 = 2;
940 target *= 5; /* fast clock */
941
942 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
943 for (clock.p2 = limit->p2.p2_fast;
944 clock.p2 >= limit->p2.p2_slow;
945 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200946 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300947
948 clock.p = clock.p1 * clock.p2;
949
950 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
951 clock.n) << 22, refclk * clock.m1);
952
953 if (m2 > INT_MAX/clock.m1)
954 continue;
955
956 clock.m2 = m2;
957
958 chv_clock(refclk, &clock);
959
960 if (!intel_PLL_is_valid(dev, limit, &clock))
961 continue;
962
Imre Deak9ca3ba02015-03-17 11:40:05 +0200963 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
964 best_error_ppm, &error_ppm))
965 continue;
966
967 *best_clock = clock;
968 best_error_ppm = error_ppm;
969 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300970 }
971 }
972
973 return found;
974}
975
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200976bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
977 intel_clock_t *best_clock)
978{
979 int refclk = i9xx_get_refclk(crtc_state, 0);
980
981 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
982 target_clock, refclk, NULL, best_clock);
983}
984
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300985bool intel_crtc_active(struct drm_crtc *crtc)
986{
987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
988
989 /* Be paranoid as we can arrive here with only partial
990 * state retrieved from the hardware during setup.
991 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100992 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300993 * as Haswell has gained clock readout/fastboot support.
994 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000995 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300996 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700997 *
998 * FIXME: The intel_crtc->active here should be switched to
999 * crtc->state->active once we have proper CRTC states wired up
1000 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001001 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001002 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001003 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001004}
1005
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001006enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1007 enum pipe pipe)
1008{
1009 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1011
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001012 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001013}
1014
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001015static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1016{
1017 struct drm_i915_private *dev_priv = dev->dev_private;
1018 u32 reg = PIPEDSL(pipe);
1019 u32 line1, line2;
1020 u32 line_mask;
1021
1022 if (IS_GEN2(dev))
1023 line_mask = DSL_LINEMASK_GEN2;
1024 else
1025 line_mask = DSL_LINEMASK_GEN3;
1026
1027 line1 = I915_READ(reg) & line_mask;
1028 mdelay(5);
1029 line2 = I915_READ(reg) & line_mask;
1030
1031 return line1 == line2;
1032}
1033
Keith Packardab7ad7f2010-10-03 00:33:06 -07001034/*
1035 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001036 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001037 *
1038 * After disabling a pipe, we can't wait for vblank in the usual way,
1039 * spinning on the vblank interrupt status bit, since we won't actually
1040 * see an interrupt when the pipe is disabled.
1041 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001042 * On Gen4 and above:
1043 * wait for the pipe register state bit to turn off
1044 *
1045 * Otherwise:
1046 * wait for the display line value to settle (it usually
1047 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001048 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001050static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001051{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001052 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001053 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001054 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001055 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001056
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001058 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001059
Keith Packardab7ad7f2010-10-03 00:33:06 -07001060 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001061 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1062 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001063 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001064 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001066 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001067 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001068 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001069}
1070
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001071/*
1072 * ibx_digital_port_connected - is the specified port connected?
1073 * @dev_priv: i915 private structure
1074 * @port: the port to test
1075 *
1076 * Returns true if @port is connected, false otherwise.
1077 */
1078bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1079 struct intel_digital_port *port)
1080{
1081 u32 bit;
1082
Damien Lespiauc36346e2012-12-13 16:09:03 +00001083 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001084 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001085 case PORT_B:
1086 bit = SDE_PORTB_HOTPLUG;
1087 break;
1088 case PORT_C:
1089 bit = SDE_PORTC_HOTPLUG;
1090 break;
1091 case PORT_D:
1092 bit = SDE_PORTD_HOTPLUG;
1093 break;
1094 default:
1095 return true;
1096 }
1097 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001098 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001099 case PORT_B:
1100 bit = SDE_PORTB_HOTPLUG_CPT;
1101 break;
1102 case PORT_C:
1103 bit = SDE_PORTC_HOTPLUG_CPT;
1104 break;
1105 case PORT_D:
1106 bit = SDE_PORTD_HOTPLUG_CPT;
1107 break;
1108 default:
1109 return true;
1110 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001111 }
1112
1113 return I915_READ(SDEISR) & bit;
1114}
1115
Jesse Barnesb24e7172011-01-04 15:09:30 -08001116static const char *state_string(bool enabled)
1117{
1118 return enabled ? "on" : "off";
1119}
1120
1121/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001122void assert_pll(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001124{
1125 int reg;
1126 u32 val;
1127 bool cur_state;
1128
1129 reg = DPLL(pipe);
1130 val = I915_READ(reg);
1131 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001132 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133 "PLL state assertion failure (expected %s, current %s)\n",
1134 state_string(state), state_string(cur_state));
1135}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136
Jani Nikula23538ef2013-08-27 15:12:22 +03001137/* XXX: the dsi pll is shared between MIPI DSI ports */
1138static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1139{
1140 u32 val;
1141 bool cur_state;
1142
Ville Syrjäläa5805162015-05-26 20:42:30 +03001143 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001144 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001145 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001146
1147 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001148 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001149 "DSI PLL state assertion failure (expected %s, current %s)\n",
1150 state_string(state), state_string(cur_state));
1151}
1152#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1153#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1154
Daniel Vetter55607e82013-06-16 21:42:39 +02001155struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001156intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001157{
Daniel Vettere2b78262013-06-07 23:10:03 +02001158 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1159
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001160 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001161 return NULL;
1162
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001163 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001164}
1165
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001167void assert_shared_dpll(struct drm_i915_private *dev_priv,
1168 struct intel_shared_dpll *pll,
1169 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001170{
Jesse Barnes040484a2011-01-03 12:14:26 -08001171 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001172 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001173
Chris Wilson92b27b02012-05-20 18:10:50 +01001174 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001175 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001176 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001177
Daniel Vetter53589012013-06-05 13:34:16 +02001178 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001179 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001180 "%s assertion failure (expected %s, current %s)\n",
1181 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001182}
Jesse Barnes040484a2011-01-03 12:14:26 -08001183
1184static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1185 enum pipe pipe, bool state)
1186{
1187 int reg;
1188 u32 val;
1189 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001190 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1191 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001192
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001193 if (HAS_DDI(dev_priv->dev)) {
1194 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001195 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001196 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001197 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001198 } else {
1199 reg = FDI_TX_CTL(pipe);
1200 val = I915_READ(reg);
1201 cur_state = !!(val & FDI_TX_ENABLE);
1202 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001203 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001204 "FDI TX state assertion failure (expected %s, current %s)\n",
1205 state_string(state), state_string(cur_state));
1206}
1207#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1208#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1209
1210static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
1212{
1213 int reg;
1214 u32 val;
1215 bool cur_state;
1216
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001217 reg = FDI_RX_CTL(pipe);
1218 val = I915_READ(reg);
1219 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001220 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001221 "FDI RX state assertion failure (expected %s, current %s)\n",
1222 state_string(state), state_string(cur_state));
1223}
1224#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1225#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1226
1227static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1228 enum pipe pipe)
1229{
1230 int reg;
1231 u32 val;
1232
1233 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001234 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001235 return;
1236
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001237 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001238 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001239 return;
1240
Jesse Barnes040484a2011-01-03 12:14:26 -08001241 reg = FDI_TX_CTL(pipe);
1242 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 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 -08001244}
1245
Daniel Vetter55607e82013-06-16 21:42:39 +02001246void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1247 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001248{
1249 int reg;
1250 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001251 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001252
1253 reg = FDI_RX_CTL(pipe);
1254 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001255 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001256 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001257 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1258 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001259}
1260
Daniel Vetterb680c372014-09-19 18:27:27 +02001261void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1262 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001264 struct drm_device *dev = dev_priv->dev;
1265 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001266 u32 val;
1267 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001268 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269
Jani Nikulabedd4db2014-08-22 15:04:13 +03001270 if (WARN_ON(HAS_DDI(dev)))
1271 return;
1272
1273 if (HAS_PCH_SPLIT(dev)) {
1274 u32 port_sel;
1275
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001277 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1278
1279 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1280 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1281 panel_pipe = PIPE_B;
1282 /* XXX: else fix for eDP */
1283 } else if (IS_VALLEYVIEW(dev)) {
1284 /* presumably write lock depends on pipe, not port select */
1285 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1286 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001287 } else {
1288 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001289 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1290 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001291 }
1292
1293 val = I915_READ(pp_reg);
1294 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001295 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296 locked = false;
1297
Rob Clarke2c719b2014-12-15 13:56:32 -05001298 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001299 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001300 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301}
1302
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001303static void assert_cursor(struct drm_i915_private *dev_priv,
1304 enum pipe pipe, bool state)
1305{
1306 struct drm_device *dev = dev_priv->dev;
1307 bool cur_state;
1308
Paulo Zanonid9d82082014-02-27 16:30:56 -03001309 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001310 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001311 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001312 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001313
Rob Clarke2c719b2014-12-15 13:56:32 -05001314 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001315 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1316 pipe_name(pipe), state_string(state), state_string(cur_state));
1317}
1318#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1319#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1320
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001321void assert_pipe(struct drm_i915_private *dev_priv,
1322 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001323{
1324 int reg;
1325 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001326 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001327 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1328 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001329
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001330 /* if we need the pipe quirk it must be always on */
1331 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1332 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001333 state = true;
1334
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001335 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001336 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001337 cur_state = false;
1338 } else {
1339 reg = PIPECONF(cpu_transcoder);
1340 val = I915_READ(reg);
1341 cur_state = !!(val & PIPECONF_ENABLE);
1342 }
1343
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001345 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001346 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347}
1348
Chris Wilson931872f2012-01-16 23:01:13 +00001349static void assert_plane(struct drm_i915_private *dev_priv,
1350 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001351{
1352 int reg;
1353 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001354 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001355
1356 reg = DSPCNTR(plane);
1357 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001358 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001359 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001360 "plane %c assertion failure (expected %s, current %s)\n",
1361 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001362}
1363
Chris Wilson931872f2012-01-16 23:01:13 +00001364#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1365#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1366
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe)
1369{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001370 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001371 int reg, i;
1372 u32 val;
1373 int cur_pipe;
1374
Ville Syrjälä653e1022013-06-04 13:49:05 +03001375 /* Primary planes are fixed to pipes on gen4+ */
1376 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001377 reg = DSPCNTR(pipe);
1378 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001380 "plane %c assertion failure, should be disabled but not\n",
1381 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001382 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001383 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001384
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001386 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001387 reg = DSPCNTR(i);
1388 val = I915_READ(reg);
1389 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1390 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001391 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001392 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1393 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001394 }
1395}
1396
Jesse Barnes19332d72013-03-28 09:55:38 -07001397static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe)
1399{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001400 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001401 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001402 u32 val;
1403
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001404 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001405 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001408 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1409 sprite, pipe_name(pipe));
1410 }
1411 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001412 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001413 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001415 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001416 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001417 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001418 }
1419 } else if (INTEL_INFO(dev)->gen >= 7) {
1420 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001421 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001422 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001423 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001424 plane_name(pipe), pipe_name(pipe));
1425 } else if (INTEL_INFO(dev)->gen >= 5) {
1426 reg = DVSCNTR(pipe);
1427 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001428 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001429 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1430 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001431 }
1432}
1433
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001434static void assert_vblank_disabled(struct drm_crtc *crtc)
1435{
Rob Clarke2c719b2014-12-15 13:56:32 -05001436 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001437 drm_crtc_vblank_put(crtc);
1438}
1439
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001440static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001441{
1442 u32 val;
1443 bool enabled;
1444
Rob Clarke2c719b2014-12-15 13:56:32 -05001445 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001446
Jesse Barnes92f25842011-01-04 15:09:34 -08001447 val = I915_READ(PCH_DREF_CONTROL);
1448 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1449 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001450 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001451}
1452
Daniel Vetterab9412b2013-05-03 11:49:46 +02001453static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001455{
1456 int reg;
1457 u32 val;
1458 bool enabled;
1459
Daniel Vetterab9412b2013-05-03 11:49:46 +02001460 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001461 val = I915_READ(reg);
1462 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001463 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001464 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1465 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001466}
1467
Keith Packard4e634382011-08-06 10:39:45 -07001468static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001470{
1471 if ((val & DP_PORT_EN) == 0)
1472 return false;
1473
1474 if (HAS_PCH_CPT(dev_priv->dev)) {
1475 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1476 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1477 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1478 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001479 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1480 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1481 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001482 } else {
1483 if ((val & DP_PIPE_MASK) != (pipe << 30))
1484 return false;
1485 }
1486 return true;
1487}
1488
Keith Packard1519b992011-08-06 10:35:34 -07001489static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1490 enum pipe pipe, u32 val)
1491{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001492 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001493 return false;
1494
1495 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001496 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001497 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001498 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1499 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1500 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001501 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001502 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001503 return false;
1504 }
1505 return true;
1506}
1507
1508static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1509 enum pipe pipe, u32 val)
1510{
1511 if ((val & LVDS_PORT_EN) == 0)
1512 return false;
1513
1514 if (HAS_PCH_CPT(dev_priv->dev)) {
1515 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1516 return false;
1517 } else {
1518 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
1524static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1525 enum pipe pipe, u32 val)
1526{
1527 if ((val & ADPA_DAC_ENABLE) == 0)
1528 return false;
1529 if (HAS_PCH_CPT(dev_priv->dev)) {
1530 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1531 return false;
1532 } else {
1533 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1534 return false;
1535 }
1536 return true;
1537}
1538
Jesse Barnes291906f2011-02-02 12:28:03 -08001539static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001540 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001541{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001542 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001543 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001544 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001545 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001546
Rob Clarke2c719b2014-12-15 13:56:32 -05001547 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001548 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001549 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001550}
1551
1552static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1553 enum pipe pipe, int reg)
1554{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001555 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001556 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001557 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001558 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001559
Rob Clarke2c719b2014-12-15 13:56:32 -05001560 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001561 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001562 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001563}
1564
1565static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1566 enum pipe pipe)
1567{
1568 int reg;
1569 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001570
Keith Packardf0575e92011-07-25 22:12:43 -07001571 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1572 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001574
1575 reg = PCH_ADPA;
1576 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001577 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001578 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001579 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001580
1581 reg = PCH_LVDS;
1582 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001583 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001584 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001585 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001586
Paulo Zanonie2debe92013-02-18 19:00:27 -03001587 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1588 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001590}
1591
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001592static void intel_init_dpio(struct drm_device *dev)
1593{
1594 struct drm_i915_private *dev_priv = dev->dev_private;
1595
1596 if (!IS_VALLEYVIEW(dev))
1597 return;
1598
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001599 /*
1600 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1601 * CHV x1 PHY (DP/HDMI D)
1602 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1603 */
1604 if (IS_CHERRYVIEW(dev)) {
1605 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1607 } else {
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1609 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001610}
1611
Ville Syrjäläd288f652014-10-28 13:20:22 +02001612static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001613 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001614{
Daniel Vetter426115c2013-07-11 22:13:42 +02001615 struct drm_device *dev = crtc->base.dev;
1616 struct drm_i915_private *dev_priv = dev->dev_private;
1617 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001618 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001619
Daniel Vetter426115c2013-07-11 22:13:42 +02001620 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001621
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001622 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001623 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1624
1625 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001626 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001627 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001628
Daniel Vetter426115c2013-07-11 22:13:42 +02001629 I915_WRITE(reg, dpll);
1630 POSTING_READ(reg);
1631 udelay(150);
1632
1633 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1634 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1635
Ville Syrjäläd288f652014-10-28 13:20:22 +02001636 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001637 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001638
1639 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001640 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001641 POSTING_READ(reg);
1642 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001643 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001646 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
1649}
1650
Ville Syrjäläd288f652014-10-28 13:20:22 +02001651static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001652 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001653{
1654 struct drm_device *dev = crtc->base.dev;
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 int pipe = crtc->pipe;
1657 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658 u32 tmp;
1659
1660 assert_pipe_disabled(dev_priv, crtc->pipe);
1661
1662 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1663
Ville Syrjäläa5805162015-05-26 20:42:30 +03001664 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001665
1666 /* Enable back the 10bit clock to display controller */
1667 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1668 tmp |= DPIO_DCLKP_EN;
1669 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1670
Ville Syrjälä54433e92015-05-26 20:42:31 +03001671 mutex_unlock(&dev_priv->sb_lock);
1672
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673 /*
1674 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1675 */
1676 udelay(1);
1677
1678 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001679 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001680
1681 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001682 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001683 DRM_ERROR("PLL %d failed to lock\n", pipe);
1684
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001685 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001686 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001687 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001688}
1689
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001690static int intel_num_dvo_pipes(struct drm_device *dev)
1691{
1692 struct intel_crtc *crtc;
1693 int count = 0;
1694
1695 for_each_intel_crtc(dev, crtc)
1696 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001697 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001698
1699 return count;
1700}
1701
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001702static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001703{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001704 struct drm_device *dev = crtc->base.dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001707 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001708
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001709 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001710
1711 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001712 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001713
1714 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715 if (IS_MOBILE(dev) && !IS_I830(dev))
1716 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001717
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001718 /* Enable DVO 2x clock on both PLLs if necessary */
1719 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1720 /*
1721 * It appears to be important that we don't enable this
1722 * for the current pipe before otherwise configuring the
1723 * PLL. No idea how this should be handled if multiple
1724 * DVO outputs are enabled simultaneosly.
1725 */
1726 dpll |= DPLL_DVO_2X_MODE;
1727 I915_WRITE(DPLL(!crtc->pipe),
1728 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1729 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001730
1731 /* Wait for the clocks to stabilize. */
1732 POSTING_READ(reg);
1733 udelay(150);
1734
1735 if (INTEL_INFO(dev)->gen >= 4) {
1736 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001737 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001738 } else {
1739 /* The pixel multiplier can only be updated once the
1740 * DPLL is enabled and the clocks are stable.
1741 *
1742 * So write it again.
1743 */
1744 I915_WRITE(reg, dpll);
1745 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746
1747 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001748 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001749 POSTING_READ(reg);
1750 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001751 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 POSTING_READ(reg);
1753 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001754 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001755 POSTING_READ(reg);
1756 udelay(150); /* wait for warmup */
1757}
1758
1759/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001760 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001761 * @dev_priv: i915 private structure
1762 * @pipe: pipe PLL to disable
1763 *
1764 * Disable the PLL for @pipe, making sure the pipe is off first.
1765 *
1766 * Note! This is for pre-ILK only.
1767 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001768static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001769{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001770 struct drm_device *dev = crtc->base.dev;
1771 struct drm_i915_private *dev_priv = dev->dev_private;
1772 enum pipe pipe = crtc->pipe;
1773
1774 /* Disable DVO 2x clock on both PLLs if necessary */
1775 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001776 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001777 intel_num_dvo_pipes(dev) == 1) {
1778 I915_WRITE(DPLL(PIPE_B),
1779 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1780 I915_WRITE(DPLL(PIPE_A),
1781 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1782 }
1783
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001784 /* Don't disable pipe or pipe PLLs if needed */
1785 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1786 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001787 return;
1788
1789 /* Make sure the pipe isn't still relying on us */
1790 assert_pipe_disabled(dev_priv, pipe);
1791
Daniel Vetter50b44a42013-06-05 13:34:33 +02001792 I915_WRITE(DPLL(pipe), 0);
1793 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001794}
1795
Jesse Barnesf6071162013-10-01 10:41:38 -07001796static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1797{
1798 u32 val = 0;
1799
1800 /* Make sure the pipe isn't still relying on us */
1801 assert_pipe_disabled(dev_priv, pipe);
1802
Imre Deake5cbfbf2014-01-09 17:08:16 +02001803 /*
1804 * Leave integrated clock source and reference clock enabled for pipe B.
1805 * The latter is needed for VGA hotplug / manual detection.
1806 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001807 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001808 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001809 I915_WRITE(DPLL(pipe), val);
1810 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001811
1812}
1813
1814static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1815{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001816 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001817 u32 val;
1818
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001819 /* Make sure the pipe isn't still relying on us */
1820 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001821
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001822 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001823 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001824 if (pipe != PIPE_A)
1825 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1826 I915_WRITE(DPLL(pipe), val);
1827 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001828
Ville Syrjäläa5805162015-05-26 20:42:30 +03001829 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001830
1831 /* Disable 10bit clock to display controller */
1832 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1833 val &= ~DPIO_DCLKP_EN;
1834 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1835
Ville Syrjälä61407f62014-05-27 16:32:55 +03001836 /* disable left/right clock distribution */
1837 if (pipe != PIPE_B) {
1838 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1839 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1840 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1841 } else {
1842 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1843 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1844 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1845 }
1846
Ville Syrjäläa5805162015-05-26 20:42:30 +03001847 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001848}
1849
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001850void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001851 struct intel_digital_port *dport,
1852 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001853{
1854 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001855 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001857 switch (dport->port) {
1858 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001860 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001861 break;
1862 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001863 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001864 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001865 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001866 break;
1867 case PORT_D:
1868 port_mask = DPLL_PORTD_READY_MASK;
1869 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001870 break;
1871 default:
1872 BUG();
1873 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001874
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001875 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1876 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1877 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001878}
1879
Daniel Vetterb14b1052014-04-24 23:55:13 +02001880static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1881{
1882 struct drm_device *dev = crtc->base.dev;
1883 struct drm_i915_private *dev_priv = dev->dev_private;
1884 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1885
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001886 if (WARN_ON(pll == NULL))
1887 return;
1888
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001889 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001890 if (pll->active == 0) {
1891 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1892 WARN_ON(pll->on);
1893 assert_shared_dpll_disabled(dev_priv, pll);
1894
1895 pll->mode_set(dev_priv, pll);
1896 }
1897}
1898
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001899/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001900 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001901 * @dev_priv: i915 private structure
1902 * @pipe: pipe PLL to enable
1903 *
1904 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1905 * drives the transcoder clock.
1906 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001907static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001908{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001909 struct drm_device *dev = crtc->base.dev;
1910 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001911 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001912
Daniel Vetter87a875b2013-06-05 13:34:19 +02001913 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001914 return;
1915
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001916 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001917 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001918
Damien Lespiau74dd6922014-07-29 18:06:17 +01001919 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001920 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001921 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001922
Daniel Vettercdbd2312013-06-05 13:34:03 +02001923 if (pll->active++) {
1924 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001925 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001926 return;
1927 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001928 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001929
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001930 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1931
Daniel Vetter46edb022013-06-05 13:34:12 +02001932 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001933 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001934 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001935}
1936
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001937static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001938{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001939 struct drm_device *dev = crtc->base.dev;
1940 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001941 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001942
Jesse Barnes92f25842011-01-04 15:09:34 -08001943 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001944 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001945 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001946 return;
1947
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001948 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001949 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Daniel Vetter46edb022013-06-05 13:34:12 +02001951 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1952 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001953 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001954
Chris Wilson48da64a2012-05-13 20:16:12 +01001955 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001956 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001957 return;
1958 }
1959
Daniel Vettere9d69442013-06-05 13:34:15 +02001960 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001961 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001962 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001963 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001964
Daniel Vetter46edb022013-06-05 13:34:12 +02001965 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001966 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001967 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001968
1969 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001970}
1971
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001972static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1973 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001974{
Daniel Vetter23670b322012-11-01 09:15:30 +01001975 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001976 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001978 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001979
1980 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001981 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001984 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001985 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001986
1987 /* FDI must be feeding us bits for PCH ports */
1988 assert_fdi_tx_enabled(dev_priv, pipe);
1989 assert_fdi_rx_enabled(dev_priv, pipe);
1990
Daniel Vetter23670b322012-11-01 09:15:30 +01001991 if (HAS_PCH_CPT(dev)) {
1992 /* Workaround: Set the timing override bit before enabling the
1993 * pch transcoder. */
1994 reg = TRANS_CHICKEN2(pipe);
1995 val = I915_READ(reg);
1996 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1997 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001998 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001999
Daniel Vetterab9412b2013-05-03 11:49:46 +02002000 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002001 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002002 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002003
2004 if (HAS_PCH_IBX(dev_priv->dev)) {
2005 /*
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002006 * Make the BPC in transcoder be consistent with
2007 * that in pipeconf reg. For HDMI we must use 8bpc
2008 * here for both 8bpc and 12bpc.
Jesse Barnese9bcff52011-06-24 12:19:20 -07002009 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002010 val &= ~PIPECONF_BPC_MASK;
Ville Syrjäläc5de7c62015-05-05 17:06:22 +03002011 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_HDMI))
2012 val |= PIPECONF_8BPC;
2013 else
2014 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002015 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002016
2017 val &= ~TRANS_INTERLACE_MASK;
2018 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002019 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002020 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002021 val |= TRANS_LEGACY_INTERLACED_ILK;
2022 else
2023 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002024 else
2025 val |= TRANS_PROGRESSIVE;
2026
Jesse Barnes040484a2011-01-03 12:14:26 -08002027 I915_WRITE(reg, val | TRANS_ENABLE);
2028 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002029 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002030}
2031
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002033 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002034{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036
2037 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002038 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002039
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002040 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002041 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002042 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002043
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002044 /* Workaround: set timing override bit. */
2045 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002046 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002047 I915_WRITE(_TRANSA_CHICKEN2, val);
2048
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002049 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002050 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002051
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002052 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2053 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002054 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002055 else
2056 val |= TRANS_PROGRESSIVE;
2057
Daniel Vetterab9412b2013-05-03 11:49:46 +02002058 I915_WRITE(LPT_TRANSCONF, val);
2059 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002060 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002061}
2062
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002063static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2064 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002065{
Daniel Vetter23670b322012-11-01 09:15:30 +01002066 struct drm_device *dev = dev_priv->dev;
2067 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002068
2069 /* FDI relies on the transcoder */
2070 assert_fdi_tx_disabled(dev_priv, pipe);
2071 assert_fdi_rx_disabled(dev_priv, pipe);
2072
Jesse Barnes291906f2011-02-02 12:28:03 -08002073 /* Ports must be off as well */
2074 assert_pch_ports_disabled(dev_priv, pipe);
2075
Daniel Vetterab9412b2013-05-03 11:49:46 +02002076 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002077 val = I915_READ(reg);
2078 val &= ~TRANS_ENABLE;
2079 I915_WRITE(reg, val);
2080 /* wait for PCH transcoder off, transcoder state */
2081 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002082 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002083
2084 if (!HAS_PCH_IBX(dev)) {
2085 /* Workaround: Clear the timing override chicken bit again. */
2086 reg = TRANS_CHICKEN2(pipe);
2087 val = I915_READ(reg);
2088 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2089 I915_WRITE(reg, val);
2090 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002091}
2092
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002093static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002094{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002095 u32 val;
2096
Daniel Vetterab9412b2013-05-03 11:49:46 +02002097 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002098 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002099 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002100 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002101 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002102 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002103
2104 /* Workaround: clear timing override bit. */
2105 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002106 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002107 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002108}
2109
2110/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002111 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002112 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002113 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002114 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002115 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002116 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002117static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118{
Paulo Zanoni03722642014-01-17 13:51:09 -02002119 struct drm_device *dev = crtc->base.dev;
2120 struct drm_i915_private *dev_priv = dev->dev_private;
2121 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002122 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2123 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002124 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002125 int reg;
2126 u32 val;
2127
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002128 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002129 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002130 assert_sprites_disabled(dev_priv, pipe);
2131
Paulo Zanoni681e5812012-12-06 11:12:38 -02002132 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002133 pch_transcoder = TRANSCODER_A;
2134 else
2135 pch_transcoder = pipe;
2136
Jesse Barnesb24e7172011-01-04 15:09:30 -08002137 /*
2138 * A pipe without a PLL won't actually be able to drive bits from
2139 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2140 * need the check.
2141 */
Imre Deak50360402015-01-16 00:55:16 -08002142 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002143 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002144 assert_dsi_pll_enabled(dev_priv);
2145 else
2146 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002147 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002148 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002149 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002150 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002151 assert_fdi_tx_pll_enabled(dev_priv,
2152 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002153 }
2154 /* FIXME: assert CPU port conditions for SNB+ */
2155 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002156
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002157 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002158 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002159 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002160 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2161 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002162 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002163 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002164
2165 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002166 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002167}
2168
2169/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002170 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002171 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002173 * Disable the pipe of @crtc, making sure that various hardware
2174 * specific requirements are met, if applicable, e.g. plane
2175 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176 *
2177 * Will wait until the pipe has shut down before returning.
2178 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002179static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002180{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002181 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002182 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002183 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002184 int reg;
2185 u32 val;
2186
2187 /*
2188 * Make sure planes won't keep trying to pump pixels to us,
2189 * or we might hang the display.
2190 */
2191 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002192 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002193 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002194
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002195 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002196 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002197 if ((val & PIPECONF_ENABLE) == 0)
2198 return;
2199
Ville Syrjälä67adc642014-08-15 01:21:57 +03002200 /*
2201 * Double wide has implications for planes
2202 * so best keep it disabled when not needed.
2203 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002204 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002205 val &= ~PIPECONF_DOUBLE_WIDE;
2206
2207 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002208 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2209 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002210 val &= ~PIPECONF_ENABLE;
2211
2212 I915_WRITE(reg, val);
2213 if ((val & PIPECONF_ENABLE) == 0)
2214 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002215}
2216
2217/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002218 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002219 * @plane: plane to be enabled
2220 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002221 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002222 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002223 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002224static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2225 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002226{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002227 struct drm_device *dev = plane->dev;
2228 struct drm_i915_private *dev_priv = dev->dev_private;
2229 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002230
2231 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002232 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002233 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002234
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002235 dev_priv->display.update_primary_plane(crtc, plane->fb,
2236 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002237}
2238
Chris Wilson693db182013-03-05 14:52:39 +00002239static bool need_vtd_wa(struct drm_device *dev)
2240{
2241#ifdef CONFIG_INTEL_IOMMU
2242 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2243 return true;
2244#endif
2245 return false;
2246}
2247
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002248unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002249intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2250 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002251{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002252 unsigned int tile_height;
2253 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002254
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002255 switch (fb_format_modifier) {
2256 case DRM_FORMAT_MOD_NONE:
2257 tile_height = 1;
2258 break;
2259 case I915_FORMAT_MOD_X_TILED:
2260 tile_height = IS_GEN2(dev) ? 16 : 8;
2261 break;
2262 case I915_FORMAT_MOD_Y_TILED:
2263 tile_height = 32;
2264 break;
2265 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002266 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2267 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002268 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002269 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002270 tile_height = 64;
2271 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002272 case 2:
2273 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002274 tile_height = 32;
2275 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002276 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002277 tile_height = 16;
2278 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002279 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002280 WARN_ONCE(1,
2281 "128-bit pixels are not supported for display!");
2282 tile_height = 16;
2283 break;
2284 }
2285 break;
2286 default:
2287 MISSING_CASE(fb_format_modifier);
2288 tile_height = 1;
2289 break;
2290 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002291
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002292 return tile_height;
2293}
2294
2295unsigned int
2296intel_fb_align_height(struct drm_device *dev, unsigned int height,
2297 uint32_t pixel_format, uint64_t fb_format_modifier)
2298{
2299 return ALIGN(height, intel_tile_height(dev, pixel_format,
2300 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002301}
2302
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002303static int
2304intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2305 const struct drm_plane_state *plane_state)
2306{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002307 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002308
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002309 *view = i915_ggtt_view_normal;
2310
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002311 if (!plane_state)
2312 return 0;
2313
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002314 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002315 return 0;
2316
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002317 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002318
2319 info->height = fb->height;
2320 info->pixel_format = fb->pixel_format;
2321 info->pitch = fb->pitches[0];
2322 info->fb_modifier = fb->modifier[0];
2323
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002324 return 0;
2325}
2326
Chris Wilson127bd2a2010-07-23 23:32:05 +01002327int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002328intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2329 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002330 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002331 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002332{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002333 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002334 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002335 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002336 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002337 u32 alignment;
2338 int ret;
2339
Matt Roperebcdd392014-07-09 16:22:11 -07002340 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2341
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002342 switch (fb->modifier[0]) {
2343 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002344 if (INTEL_INFO(dev)->gen >= 9)
2345 alignment = 256 * 1024;
2346 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002347 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002348 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002349 alignment = 4 * 1024;
2350 else
2351 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002352 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002353 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002354 if (INTEL_INFO(dev)->gen >= 9)
2355 alignment = 256 * 1024;
2356 else {
2357 /* pin() will align the object as required by fence */
2358 alignment = 0;
2359 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002360 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002361 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002362 case I915_FORMAT_MOD_Yf_TILED:
2363 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2364 "Y tiling bo slipped through, driver bug!\n"))
2365 return -EINVAL;
2366 alignment = 1 * 1024 * 1024;
2367 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002368 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002369 MISSING_CASE(fb->modifier[0]);
2370 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002371 }
2372
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002373 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2374 if (ret)
2375 return ret;
2376
Chris Wilson693db182013-03-05 14:52:39 +00002377 /* Note that the w/a also requires 64 PTE of padding following the
2378 * bo. We currently fill all unused PTE with the shadow page and so
2379 * we should always have valid PTE following the scanout preventing
2380 * the VT-d warning.
2381 */
2382 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2383 alignment = 256 * 1024;
2384
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002385 /*
2386 * Global gtt pte registers are special registers which actually forward
2387 * writes to a chunk of system memory. Which means that there is no risk
2388 * that the register values disappear as soon as we call
2389 * intel_runtime_pm_put(), so it is correct to wrap only the
2390 * pin/unpin/fence and not more.
2391 */
2392 intel_runtime_pm_get(dev_priv);
2393
Chris Wilsonce453d82011-02-21 14:43:56 +00002394 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002395 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002396 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002397 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002398 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002399
2400 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2401 * fence, whereas 965+ only requires a fence if using
2402 * framebuffer compression. For simplicity, we always install
2403 * a fence as the cost is not that onerous.
2404 */
Chris Wilson06d98132012-04-17 15:31:24 +01002405 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002406 if (ret)
2407 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002408
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002409 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002410
Chris Wilsonce453d82011-02-21 14:43:56 +00002411 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002412 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002413 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002414
2415err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002416 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002417err_interruptible:
2418 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002419 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002420 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002421}
2422
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002423static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2424 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002425{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002426 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002427 struct i915_ggtt_view view;
2428 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002429
Matt Roperebcdd392014-07-09 16:22:11 -07002430 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2431
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002432 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2433 WARN_ONCE(ret, "Couldn't get view from plane state!");
2434
Chris Wilson1690e1e2011-12-14 13:57:08 +01002435 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002436 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002437}
2438
Daniel Vetterc2c75132012-07-05 12:17:30 +02002439/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2440 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002441unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2442 unsigned int tiling_mode,
2443 unsigned int cpp,
2444 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002445{
Chris Wilsonbc752862013-02-21 20:04:31 +00002446 if (tiling_mode != I915_TILING_NONE) {
2447 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002448
Chris Wilsonbc752862013-02-21 20:04:31 +00002449 tile_rows = *y / 8;
2450 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002451
Chris Wilsonbc752862013-02-21 20:04:31 +00002452 tiles = *x / (512/cpp);
2453 *x %= 512/cpp;
2454
2455 return tile_rows * pitch * 8 + tiles * 4096;
2456 } else {
2457 unsigned int offset;
2458
2459 offset = *y * pitch + *x * cpp;
2460 *y = 0;
2461 *x = (offset & 4095) / cpp;
2462 return offset & -4096;
2463 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002464}
2465
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002466static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002467{
2468 switch (format) {
2469 case DISPPLANE_8BPP:
2470 return DRM_FORMAT_C8;
2471 case DISPPLANE_BGRX555:
2472 return DRM_FORMAT_XRGB1555;
2473 case DISPPLANE_BGRX565:
2474 return DRM_FORMAT_RGB565;
2475 default:
2476 case DISPPLANE_BGRX888:
2477 return DRM_FORMAT_XRGB8888;
2478 case DISPPLANE_RGBX888:
2479 return DRM_FORMAT_XBGR8888;
2480 case DISPPLANE_BGRX101010:
2481 return DRM_FORMAT_XRGB2101010;
2482 case DISPPLANE_RGBX101010:
2483 return DRM_FORMAT_XBGR2101010;
2484 }
2485}
2486
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002487static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2488{
2489 switch (format) {
2490 case PLANE_CTL_FORMAT_RGB_565:
2491 return DRM_FORMAT_RGB565;
2492 default:
2493 case PLANE_CTL_FORMAT_XRGB_8888:
2494 if (rgb_order) {
2495 if (alpha)
2496 return DRM_FORMAT_ABGR8888;
2497 else
2498 return DRM_FORMAT_XBGR8888;
2499 } else {
2500 if (alpha)
2501 return DRM_FORMAT_ARGB8888;
2502 else
2503 return DRM_FORMAT_XRGB8888;
2504 }
2505 case PLANE_CTL_FORMAT_XRGB_2101010:
2506 if (rgb_order)
2507 return DRM_FORMAT_XBGR2101010;
2508 else
2509 return DRM_FORMAT_XRGB2101010;
2510 }
2511}
2512
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002513static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002514intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2515 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002516{
2517 struct drm_device *dev = crtc->base.dev;
2518 struct drm_i915_gem_object *obj = NULL;
2519 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002520 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002521 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2522 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2523 PAGE_SIZE);
2524
2525 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002526
Chris Wilsonff2652e2014-03-10 08:07:02 +00002527 if (plane_config->size == 0)
2528 return false;
2529
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002530 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2531 base_aligned,
2532 base_aligned,
2533 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002534 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002535 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536
Damien Lespiau49af4492015-01-20 12:51:44 +00002537 obj->tiling_mode = plane_config->tiling;
2538 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002539 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002540
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002541 mode_cmd.pixel_format = fb->pixel_format;
2542 mode_cmd.width = fb->width;
2543 mode_cmd.height = fb->height;
2544 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002545 mode_cmd.modifier[0] = fb->modifier[0];
2546 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002547
2548 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002549 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002550 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002551 DRM_DEBUG_KMS("intel fb init failed\n");
2552 goto out_unref_obj;
2553 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002555
Daniel Vetterf6936e22015-03-26 12:17:05 +01002556 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002558
2559out_unref_obj:
2560 drm_gem_object_unreference(&obj->base);
2561 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002562 return false;
2563}
2564
Matt Roperafd65eb2015-02-03 13:10:04 -08002565/* Update plane->state->fb to match plane->fb after driver-internal updates */
2566static void
2567update_state_fb(struct drm_plane *plane)
2568{
2569 if (plane->fb == plane->state->fb)
2570 return;
2571
2572 if (plane->state->fb)
2573 drm_framebuffer_unreference(plane->state->fb);
2574 plane->state->fb = plane->fb;
2575 if (plane->state->fb)
2576 drm_framebuffer_reference(plane->state->fb);
2577}
2578
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002579static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002580intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2581 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002582{
2583 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002584 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002585 struct drm_crtc *c;
2586 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002587 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002588 struct drm_plane *primary = intel_crtc->base.primary;
2589 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002590
Damien Lespiau2d140302015-02-05 17:22:18 +00002591 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002592 return;
2593
Daniel Vetterf6936e22015-03-26 12:17:05 +01002594 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002595 fb = &plane_config->fb->base;
2596 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002597 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002598
Damien Lespiau2d140302015-02-05 17:22:18 +00002599 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002600
2601 /*
2602 * Failed to alloc the obj, check to see if we should share
2603 * an fb with another CRTC instead
2604 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002605 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002606 i = to_intel_crtc(c);
2607
2608 if (c == &intel_crtc->base)
2609 continue;
2610
Matt Roper2ff8fde2014-07-08 07:50:07 -07002611 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002612 continue;
2613
Daniel Vetter88595ac2015-03-26 12:42:24 +01002614 fb = c->primary->fb;
2615 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002616 continue;
2617
Daniel Vetter88595ac2015-03-26 12:42:24 +01002618 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002619 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002620 drm_framebuffer_reference(fb);
2621 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002622 }
2623 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624
2625 return;
2626
2627valid_fb:
2628 obj = intel_fb_obj(fb);
2629 if (obj->tiling_mode != I915_TILING_NONE)
2630 dev_priv->preserve_bios_swizzle = true;
2631
2632 primary->fb = fb;
2633 primary->state->crtc = &intel_crtc->base;
2634 primary->crtc = &intel_crtc->base;
2635 update_state_fb(primary);
2636 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002637}
2638
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002639static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2640 struct drm_framebuffer *fb,
2641 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002642{
2643 struct drm_device *dev = crtc->dev;
2644 struct drm_i915_private *dev_priv = dev->dev_private;
2645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002646 struct drm_plane *primary = crtc->primary;
2647 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002648 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002649 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002650 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002651 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002652 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302653 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002654
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002655 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002656 I915_WRITE(reg, 0);
2657 if (INTEL_INFO(dev)->gen >= 4)
2658 I915_WRITE(DSPSURF(plane), 0);
2659 else
2660 I915_WRITE(DSPADDR(plane), 0);
2661 POSTING_READ(reg);
2662 return;
2663 }
2664
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002665 obj = intel_fb_obj(fb);
2666 if (WARN_ON(obj == NULL))
2667 return;
2668
2669 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2670
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002671 dspcntr = DISPPLANE_GAMMA_ENABLE;
2672
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002673 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002674
2675 if (INTEL_INFO(dev)->gen < 4) {
2676 if (intel_crtc->pipe == PIPE_B)
2677 dspcntr |= DISPPLANE_SEL_PIPE_B;
2678
2679 /* pipesrc and dspsize control the size that is scaled from,
2680 * which should always be the user's requested size.
2681 */
2682 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002683 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2684 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002685 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002686 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2687 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002688 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2689 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002690 I915_WRITE(PRIMPOS(plane), 0);
2691 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002692 }
2693
Ville Syrjälä57779d02012-10-31 17:50:14 +02002694 switch (fb->pixel_format) {
2695 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002696 dspcntr |= DISPPLANE_8BPP;
2697 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002698 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002699 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002700 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701 case DRM_FORMAT_RGB565:
2702 dspcntr |= DISPPLANE_BGRX565;
2703 break;
2704 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002705 dspcntr |= DISPPLANE_BGRX888;
2706 break;
2707 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002708 dspcntr |= DISPPLANE_RGBX888;
2709 break;
2710 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002711 dspcntr |= DISPPLANE_BGRX101010;
2712 break;
2713 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002714 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002715 break;
2716 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002717 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002718 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002719
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002720 if (INTEL_INFO(dev)->gen >= 4 &&
2721 obj->tiling_mode != I915_TILING_NONE)
2722 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002723
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002724 if (IS_G4X(dev))
2725 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2726
Ville Syrjäläb98971272014-08-27 16:51:22 +03002727 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002728
Daniel Vetterc2c75132012-07-05 12:17:30 +02002729 if (INTEL_INFO(dev)->gen >= 4) {
2730 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002731 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002732 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002733 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002734 linear_offset -= intel_crtc->dspaddr_offset;
2735 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002736 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002737 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002738
Matt Roper8e7d6882015-01-21 16:35:41 -08002739 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302740 dspcntr |= DISPPLANE_ROTATE_180;
2741
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002742 x += (intel_crtc->config->pipe_src_w - 1);
2743 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302744
2745 /* Finding the last pixel of the last line of the display
2746 data and adding to linear_offset*/
2747 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002748 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2749 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302750 }
2751
2752 I915_WRITE(reg, dspcntr);
2753
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002754 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002755 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002756 I915_WRITE(DSPSURF(plane),
2757 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002758 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002759 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002760 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002761 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002762 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002763}
2764
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002765static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2766 struct drm_framebuffer *fb,
2767 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002768{
2769 struct drm_device *dev = crtc->dev;
2770 struct drm_i915_private *dev_priv = dev->dev_private;
2771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002772 struct drm_plane *primary = crtc->primary;
2773 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002774 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002775 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002776 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002777 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002778 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302779 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002780
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002781 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002782 I915_WRITE(reg, 0);
2783 I915_WRITE(DSPSURF(plane), 0);
2784 POSTING_READ(reg);
2785 return;
2786 }
2787
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002788 obj = intel_fb_obj(fb);
2789 if (WARN_ON(obj == NULL))
2790 return;
2791
2792 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2793
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002794 dspcntr = DISPPLANE_GAMMA_ENABLE;
2795
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002796 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002797
2798 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2799 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2800
Ville Syrjälä57779d02012-10-31 17:50:14 +02002801 switch (fb->pixel_format) {
2802 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002803 dspcntr |= DISPPLANE_8BPP;
2804 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 case DRM_FORMAT_RGB565:
2806 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002807 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002809 dspcntr |= DISPPLANE_BGRX888;
2810 break;
2811 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002812 dspcntr |= DISPPLANE_RGBX888;
2813 break;
2814 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 dspcntr |= DISPPLANE_BGRX101010;
2816 break;
2817 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002818 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002819 break;
2820 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002821 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822 }
2823
2824 if (obj->tiling_mode != I915_TILING_NONE)
2825 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002826
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002827 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002828 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002829
Ville Syrjäläb98971272014-08-27 16:51:22 +03002830 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002831 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002832 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002833 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002834 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002835 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002836 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302837 dspcntr |= DISPPLANE_ROTATE_180;
2838
2839 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002840 x += (intel_crtc->config->pipe_src_w - 1);
2841 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302842
2843 /* Finding the last pixel of the last line of the display
2844 data and adding to linear_offset*/
2845 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002846 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2847 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302848 }
2849 }
2850
2851 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002852
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002853 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002854 I915_WRITE(DSPSURF(plane),
2855 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002856 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002857 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2858 } else {
2859 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2860 I915_WRITE(DSPLINOFF(plane), linear_offset);
2861 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002862 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002863}
2864
Damien Lespiaub3218032015-02-27 11:15:18 +00002865u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2866 uint32_t pixel_format)
2867{
2868 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2869
2870 /*
2871 * The stride is either expressed as a multiple of 64 bytes
2872 * chunks for linear buffers or in number of tiles for tiled
2873 * buffers.
2874 */
2875 switch (fb_modifier) {
2876 case DRM_FORMAT_MOD_NONE:
2877 return 64;
2878 case I915_FORMAT_MOD_X_TILED:
2879 if (INTEL_INFO(dev)->gen == 2)
2880 return 128;
2881 return 512;
2882 case I915_FORMAT_MOD_Y_TILED:
2883 /* No need to check for old gens and Y tiling since this is
2884 * about the display engine and those will be blocked before
2885 * we get here.
2886 */
2887 return 128;
2888 case I915_FORMAT_MOD_Yf_TILED:
2889 if (bits_per_pixel == 8)
2890 return 64;
2891 else
2892 return 128;
2893 default:
2894 MISSING_CASE(fb_modifier);
2895 return 64;
2896 }
2897}
2898
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002899unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2900 struct drm_i915_gem_object *obj)
2901{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002902 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002903
2904 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002905 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002906
2907 return i915_gem_obj_ggtt_offset_view(obj, view);
2908}
2909
Chandra Kondurua1b22782015-04-07 15:28:45 -07002910/*
2911 * This function detaches (aka. unbinds) unused scalers in hardware
2912 */
2913void skl_detach_scalers(struct intel_crtc *intel_crtc)
2914{
2915 struct drm_device *dev;
2916 struct drm_i915_private *dev_priv;
2917 struct intel_crtc_scaler_state *scaler_state;
2918 int i;
2919
2920 if (!intel_crtc || !intel_crtc->config)
2921 return;
2922
2923 dev = intel_crtc->base.dev;
2924 dev_priv = dev->dev_private;
2925 scaler_state = &intel_crtc->config->scaler_state;
2926
2927 /* loop through and disable scalers that aren't in use */
2928 for (i = 0; i < intel_crtc->num_scalers; i++) {
2929 if (!scaler_state->scalers[i].in_use) {
2930 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2931 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2932 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2933 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2934 intel_crtc->base.base.id, intel_crtc->pipe, i);
2935 }
2936 }
2937}
2938
Chandra Konduru6156a452015-04-27 13:48:39 -07002939u32 skl_plane_ctl_format(uint32_t pixel_format)
2940{
Chandra Konduru6156a452015-04-27 13:48:39 -07002941 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002942 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002943 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 /*
2951 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2952 * to be already pre-multiplied. We need to add a knob (or a different
2953 * DRM_FORMAT) for user-space to configure that.
2954 */
2955 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002956 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002974 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002976
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002977 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002978}
2979
2980u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2981{
Chandra Konduru6156a452015-04-27 13:48:39 -07002982 switch (fb_modifier) {
2983 case DRM_FORMAT_MOD_NONE:
2984 break;
2985 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002986 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002988 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002990 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002991 default:
2992 MISSING_CASE(fb_modifier);
2993 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002994
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002995 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002996}
2997
2998u32 skl_plane_ctl_rotation(unsigned int rotation)
2999{
Chandra Konduru6156a452015-04-27 13:48:39 -07003000 switch (rotation) {
3001 case BIT(DRM_ROTATE_0):
3002 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303003 /*
3004 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3005 * while i915 HW rotation is clockwise, thats why this swapping.
3006 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003007 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303008 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003010 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003011 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303012 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 default:
3014 MISSING_CASE(rotation);
3015 }
3016
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003017 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003018}
3019
Damien Lespiau70d21f02013-07-03 21:06:04 +01003020static void skylake_update_primary_plane(struct drm_crtc *crtc,
3021 struct drm_framebuffer *fb,
3022 int x, int y)
3023{
3024 struct drm_device *dev = crtc->dev;
3025 struct drm_i915_private *dev_priv = dev->dev_private;
3026 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003027 struct drm_plane *plane = crtc->primary;
3028 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003029 struct drm_i915_gem_object *obj;
3030 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303031 u32 plane_ctl, stride_div, stride;
3032 u32 tile_height, plane_offset, plane_size;
3033 unsigned int rotation;
3034 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003035 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003036 struct intel_crtc_state *crtc_state = intel_crtc->config;
3037 struct intel_plane_state *plane_state;
3038 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3039 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3040 int scaler_id = -1;
3041
Chandra Konduru6156a452015-04-27 13:48:39 -07003042 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003043
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003044 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003045 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3046 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3047 POSTING_READ(PLANE_CTL(pipe, 0));
3048 return;
3049 }
3050
3051 plane_ctl = PLANE_CTL_ENABLE |
3052 PLANE_CTL_PIPE_GAMMA_ENABLE |
3053 PLANE_CTL_PIPE_CSC_ENABLE;
3054
Chandra Konduru6156a452015-04-27 13:48:39 -07003055 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3056 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003057 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303058
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303059 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003060 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003061
Damien Lespiaub3218032015-02-27 11:15:18 +00003062 obj = intel_fb_obj(fb);
3063 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3064 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303065 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3066
Chandra Konduru6156a452015-04-27 13:48:39 -07003067 /*
3068 * FIXME: intel_plane_state->src, dst aren't set when transitional
3069 * update_plane helpers are called from legacy paths.
3070 * Once full atomic crtc is available, below check can be avoided.
3071 */
3072 if (drm_rect_width(&plane_state->src)) {
3073 scaler_id = plane_state->scaler_id;
3074 src_x = plane_state->src.x1 >> 16;
3075 src_y = plane_state->src.y1 >> 16;
3076 src_w = drm_rect_width(&plane_state->src) >> 16;
3077 src_h = drm_rect_height(&plane_state->src) >> 16;
3078 dst_x = plane_state->dst.x1;
3079 dst_y = plane_state->dst.y1;
3080 dst_w = drm_rect_width(&plane_state->dst);
3081 dst_h = drm_rect_height(&plane_state->dst);
3082
3083 WARN_ON(x != src_x || y != src_y);
3084 } else {
3085 src_w = intel_crtc->config->pipe_src_w;
3086 src_h = intel_crtc->config->pipe_src_h;
3087 }
3088
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303089 if (intel_rotation_90_or_270(rotation)) {
3090 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003091 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303092 fb->modifier[0]);
3093 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003094 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303095 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003096 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303097 } else {
3098 stride = fb->pitches[0] / stride_div;
3099 x_offset = x;
3100 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003101 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303102 }
3103 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003104
Damien Lespiau70d21f02013-07-03 21:06:04 +01003105 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303106 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3107 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3108 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003109
3110 if (scaler_id >= 0) {
3111 uint32_t ps_ctrl = 0;
3112
3113 WARN_ON(!dst_w || !dst_h);
3114 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3115 crtc_state->scaler_state.scalers[scaler_id].mode;
3116 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3117 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3118 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3119 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3120 I915_WRITE(PLANE_POS(pipe, 0), 0);
3121 } else {
3122 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3123 }
3124
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003125 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003126
3127 POSTING_READ(PLANE_SURF(pipe, 0));
3128}
3129
Jesse Barnes17638cd2011-06-24 12:19:23 -07003130/* Assume fb object is pinned & idle & fenced and just update base pointers */
3131static int
3132intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3133 int x, int y, enum mode_set_atomic state)
3134{
3135 struct drm_device *dev = crtc->dev;
3136 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003137
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003138 if (dev_priv->display.disable_fbc)
3139 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003140
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003141 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3142
3143 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003144}
3145
Ville Syrjälä75147472014-11-24 18:28:11 +02003146static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003147{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003148 struct drm_crtc *crtc;
3149
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003150 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003151 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3152 enum plane plane = intel_crtc->plane;
3153
3154 intel_prepare_page_flip(dev, plane);
3155 intel_finish_page_flip_plane(dev, plane);
3156 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003157}
3158
3159static void intel_update_primary_planes(struct drm_device *dev)
3160{
3161 struct drm_i915_private *dev_priv = dev->dev_private;
3162 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003163
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
Rob Clark51fd3712013-11-19 12:10:12 -05003167 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003168 /*
3169 * FIXME: Once we have proper support for primary planes (and
3170 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003171 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003172 */
Matt Roperf4510a22014-04-01 15:22:40 -07003173 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003174 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003175 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003176 crtc->x,
3177 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003178 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003179 }
3180}
3181
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003182void intel_crtc_reset(struct intel_crtc *crtc)
3183{
3184 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3185
3186 if (!crtc->active)
3187 return;
3188
3189 intel_crtc_disable_planes(&crtc->base);
3190 dev_priv->display.crtc_disable(&crtc->base);
3191 dev_priv->display.crtc_enable(&crtc->base);
3192 intel_crtc_enable_planes(&crtc->base);
3193}
3194
Ville Syrjälä75147472014-11-24 18:28:11 +02003195void intel_prepare_reset(struct drm_device *dev)
3196{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003197 struct drm_i915_private *dev_priv = to_i915(dev);
3198 struct intel_crtc *crtc;
3199
Ville Syrjälä75147472014-11-24 18:28:11 +02003200 /* no reset support for gen2 */
3201 if (IS_GEN2(dev))
3202 return;
3203
3204 /* reset doesn't touch the display */
3205 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3206 return;
3207
3208 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003209
3210 /*
3211 * Disabling the crtcs gracefully seems nicer. Also the
3212 * g33 docs say we should at least disable all the planes.
3213 */
3214 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003215 if (!crtc->active)
3216 continue;
3217
3218 intel_crtc_disable_planes(&crtc->base);
3219 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003220 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003221}
3222
3223void intel_finish_reset(struct drm_device *dev)
3224{
3225 struct drm_i915_private *dev_priv = to_i915(dev);
3226
3227 /*
3228 * Flips in the rings will be nuked by the reset,
3229 * so complete all pending flips so that user space
3230 * will get its events and not get stuck.
3231 */
3232 intel_complete_page_flips(dev);
3233
3234 /* no reset support for gen2 */
3235 if (IS_GEN2(dev))
3236 return;
3237
3238 /* reset doesn't touch the display */
3239 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3240 /*
3241 * Flips in the rings have been nuked by the reset,
3242 * so update the base address of all primary
3243 * planes to the the last fb to make sure we're
3244 * showing the correct fb after a reset.
3245 */
3246 intel_update_primary_planes(dev);
3247 return;
3248 }
3249
3250 /*
3251 * The display has been reset as well,
3252 * so need a full re-initialization.
3253 */
3254 intel_runtime_pm_disable_interrupts(dev_priv);
3255 intel_runtime_pm_enable_interrupts(dev_priv);
3256
3257 intel_modeset_init_hw(dev);
3258
3259 spin_lock_irq(&dev_priv->irq_lock);
3260 if (dev_priv->display.hpd_irq_setup)
3261 dev_priv->display.hpd_irq_setup(dev);
3262 spin_unlock_irq(&dev_priv->irq_lock);
3263
3264 intel_modeset_setup_hw_state(dev, true);
3265
3266 intel_hpd_init(dev_priv);
3267
3268 drm_modeset_unlock_all(dev);
3269}
3270
Chris Wilson2e2f3512015-04-27 13:41:14 +01003271static void
Chris Wilson14667a42012-04-03 17:58:35 +01003272intel_finish_fb(struct drm_framebuffer *old_fb)
3273{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003274 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003275 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003276 bool was_interruptible = dev_priv->mm.interruptible;
3277 int ret;
3278
Chris Wilson14667a42012-04-03 17:58:35 +01003279 /* Big Hammer, we also need to ensure that any pending
3280 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3281 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003282 * framebuffer. Note that we rely on userspace rendering
3283 * into the buffer attached to the pipe they are waiting
3284 * on. If not, userspace generates a GPU hang with IPEHR
3285 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003286 *
3287 * This should only fail upon a hung GPU, in which case we
3288 * can safely continue.
3289 */
3290 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003291 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003292 dev_priv->mm.interruptible = was_interruptible;
3293
Chris Wilson2e2f3512015-04-27 13:41:14 +01003294 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003295}
3296
Chris Wilson7d5e3792014-03-04 13:15:08 +00003297static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3298{
3299 struct drm_device *dev = crtc->dev;
3300 struct drm_i915_private *dev_priv = dev->dev_private;
3301 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003302 bool pending;
3303
3304 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3305 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3306 return false;
3307
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003308 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003309 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003310 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003311
3312 return pending;
3313}
3314
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003315static void intel_update_pipe_size(struct intel_crtc *crtc)
3316{
3317 struct drm_device *dev = crtc->base.dev;
3318 struct drm_i915_private *dev_priv = dev->dev_private;
3319 const struct drm_display_mode *adjusted_mode;
3320
3321 if (!i915.fastboot)
3322 return;
3323
3324 /*
3325 * Update pipe size and adjust fitter if needed: the reason for this is
3326 * that in compute_mode_changes we check the native mode (not the pfit
3327 * mode) to see if we can flip rather than do a full mode set. In the
3328 * fastboot case, we'll flip, but if we don't update the pipesrc and
3329 * pfit state, we'll end up with a big fb scanned out into the wrong
3330 * sized surface.
3331 *
3332 * To fix this properly, we need to hoist the checks up into
3333 * compute_mode_changes (or above), check the actual pfit state and
3334 * whether the platform allows pfit disable with pipe active, and only
3335 * then update the pipesrc and pfit state, even on the flip path.
3336 */
3337
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003338 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003339
3340 I915_WRITE(PIPESRC(crtc->pipe),
3341 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3342 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003343 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003344 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3345 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003346 I915_WRITE(PF_CTL(crtc->pipe), 0);
3347 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3348 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3349 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003350 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3351 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003352}
3353
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003354static void intel_fdi_normal_train(struct drm_crtc *crtc)
3355{
3356 struct drm_device *dev = crtc->dev;
3357 struct drm_i915_private *dev_priv = dev->dev_private;
3358 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3359 int pipe = intel_crtc->pipe;
3360 u32 reg, temp;
3361
3362 /* enable normal train */
3363 reg = FDI_TX_CTL(pipe);
3364 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003365 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003366 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3367 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003368 } else {
3369 temp &= ~FDI_LINK_TRAIN_NONE;
3370 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003371 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003372 I915_WRITE(reg, temp);
3373
3374 reg = FDI_RX_CTL(pipe);
3375 temp = I915_READ(reg);
3376 if (HAS_PCH_CPT(dev)) {
3377 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3378 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3379 } else {
3380 temp &= ~FDI_LINK_TRAIN_NONE;
3381 temp |= FDI_LINK_TRAIN_NONE;
3382 }
3383 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3384
3385 /* wait one idle pattern time */
3386 POSTING_READ(reg);
3387 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003388
3389 /* IVB wants error correction enabled */
3390 if (IS_IVYBRIDGE(dev))
3391 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3392 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003393}
3394
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003395/* The FDI link training functions for ILK/Ibexpeak. */
3396static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3397{
3398 struct drm_device *dev = crtc->dev;
3399 struct drm_i915_private *dev_priv = dev->dev_private;
3400 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3401 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003402 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003403
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003404 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003405 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003406
Adam Jacksone1a44742010-06-25 15:32:14 -04003407 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3408 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 reg = FDI_RX_IMR(pipe);
3410 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 temp &= ~FDI_RX_SYMBOL_LOCK;
3412 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 I915_WRITE(reg, temp);
3414 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003415 udelay(150);
3416
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003417 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003418 reg = FDI_TX_CTL(pipe);
3419 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003420 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003421 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 temp &= ~FDI_LINK_TRAIN_NONE;
3423 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003424 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003425
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 reg = FDI_RX_CTL(pipe);
3427 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003428 temp &= ~FDI_LINK_TRAIN_NONE;
3429 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003430 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3431
3432 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003433 udelay(150);
3434
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003435 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003436 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3437 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3438 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003439
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003441 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3444
3445 if ((temp & FDI_RX_BIT_LOCK)) {
3446 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003447 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003448 break;
3449 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003451 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453
3454 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 reg = FDI_TX_CTL(pipe);
3456 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003457 temp &= ~FDI_LINK_TRAIN_NONE;
3458 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460
Chris Wilson5eddb702010-09-11 13:48:45 +01003461 reg = FDI_RX_CTL(pipe);
3462 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463 temp &= ~FDI_LINK_TRAIN_NONE;
3464 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 I915_WRITE(reg, temp);
3466
3467 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 udelay(150);
3469
Chris Wilson5eddb702010-09-11 13:48:45 +01003470 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003471 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3474
3475 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003476 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003477 DRM_DEBUG_KMS("FDI train 2 done.\n");
3478 break;
3479 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003480 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003481 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483
3484 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003485
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003486}
3487
Akshay Joshi0206e352011-08-16 15:34:10 -04003488static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003489 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3490 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3491 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3492 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3493};
3494
3495/* The FDI link training functions for SNB/Cougarpoint. */
3496static void gen6_fdi_link_train(struct drm_crtc *crtc)
3497{
3498 struct drm_device *dev = crtc->dev;
3499 struct drm_i915_private *dev_priv = dev->dev_private;
3500 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3501 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003502 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003503
Adam Jacksone1a44742010-06-25 15:32:14 -04003504 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3505 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 reg = FDI_RX_IMR(pipe);
3507 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003508 temp &= ~FDI_RX_SYMBOL_LOCK;
3509 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003510 I915_WRITE(reg, temp);
3511
3512 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003513 udelay(150);
3514
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003515 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003516 reg = FDI_TX_CTL(pipe);
3517 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003518 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003519 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003520 temp &= ~FDI_LINK_TRAIN_NONE;
3521 temp |= FDI_LINK_TRAIN_PATTERN_1;
3522 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3523 /* SNB-B */
3524 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003525 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526
Daniel Vetterd74cf322012-10-26 10:58:13 +02003527 I915_WRITE(FDI_RX_MISC(pipe),
3528 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3529
Chris Wilson5eddb702010-09-11 13:48:45 +01003530 reg = FDI_RX_CTL(pipe);
3531 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003532 if (HAS_PCH_CPT(dev)) {
3533 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3534 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3535 } else {
3536 temp &= ~FDI_LINK_TRAIN_NONE;
3537 temp |= FDI_LINK_TRAIN_PATTERN_1;
3538 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003539 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3540
3541 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 udelay(150);
3543
Akshay Joshi0206e352011-08-16 15:34:10 -04003544 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 reg = FDI_TX_CTL(pipe);
3546 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003547 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3548 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 I915_WRITE(reg, temp);
3550
3551 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552 udelay(500);
3553
Sean Paulfa37d392012-03-02 12:53:39 -05003554 for (retry = 0; retry < 5; retry++) {
3555 reg = FDI_RX_IIR(pipe);
3556 temp = I915_READ(reg);
3557 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3558 if (temp & FDI_RX_BIT_LOCK) {
3559 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3560 DRM_DEBUG_KMS("FDI train 1 done.\n");
3561 break;
3562 }
3563 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 }
Sean Paulfa37d392012-03-02 12:53:39 -05003565 if (retry < 5)
3566 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003567 }
3568 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003569 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003570
3571 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003572 reg = FDI_TX_CTL(pipe);
3573 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 temp &= ~FDI_LINK_TRAIN_NONE;
3575 temp |= FDI_LINK_TRAIN_PATTERN_2;
3576 if (IS_GEN6(dev)) {
3577 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3578 /* SNB-B */
3579 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3580 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003582
Chris Wilson5eddb702010-09-11 13:48:45 +01003583 reg = FDI_RX_CTL(pipe);
3584 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003585 if (HAS_PCH_CPT(dev)) {
3586 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3587 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3588 } else {
3589 temp &= ~FDI_LINK_TRAIN_NONE;
3590 temp |= FDI_LINK_TRAIN_PATTERN_2;
3591 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003592 I915_WRITE(reg, temp);
3593
3594 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003595 udelay(150);
3596
Akshay Joshi0206e352011-08-16 15:34:10 -04003597 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003598 reg = FDI_TX_CTL(pipe);
3599 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003600 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3601 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003602 I915_WRITE(reg, temp);
3603
3604 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003605 udelay(500);
3606
Sean Paulfa37d392012-03-02 12:53:39 -05003607 for (retry = 0; retry < 5; retry++) {
3608 reg = FDI_RX_IIR(pipe);
3609 temp = I915_READ(reg);
3610 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3611 if (temp & FDI_RX_SYMBOL_LOCK) {
3612 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3613 DRM_DEBUG_KMS("FDI train 2 done.\n");
3614 break;
3615 }
3616 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003617 }
Sean Paulfa37d392012-03-02 12:53:39 -05003618 if (retry < 5)
3619 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003620 }
3621 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003622 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003623
3624 DRM_DEBUG_KMS("FDI train done.\n");
3625}
3626
Jesse Barnes357555c2011-04-28 15:09:55 -07003627/* Manual link training for Ivy Bridge A0 parts */
3628static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3629{
3630 struct drm_device *dev = crtc->dev;
3631 struct drm_i915_private *dev_priv = dev->dev_private;
3632 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3633 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003634 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003635
3636 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3637 for train result */
3638 reg = FDI_RX_IMR(pipe);
3639 temp = I915_READ(reg);
3640 temp &= ~FDI_RX_SYMBOL_LOCK;
3641 temp &= ~FDI_RX_BIT_LOCK;
3642 I915_WRITE(reg, temp);
3643
3644 POSTING_READ(reg);
3645 udelay(150);
3646
Daniel Vetter01a415f2012-10-27 15:58:40 +02003647 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3648 I915_READ(FDI_RX_IIR(pipe)));
3649
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650 /* Try each vswing and preemphasis setting twice before moving on */
3651 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3652 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003653 reg = FDI_TX_CTL(pipe);
3654 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003655 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3656 temp &= ~FDI_TX_ENABLE;
3657 I915_WRITE(reg, temp);
3658
3659 reg = FDI_RX_CTL(pipe);
3660 temp = I915_READ(reg);
3661 temp &= ~FDI_LINK_TRAIN_AUTO;
3662 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3663 temp &= ~FDI_RX_ENABLE;
3664 I915_WRITE(reg, temp);
3665
3666 /* enable CPU FDI TX and PCH FDI RX */
3667 reg = FDI_TX_CTL(pipe);
3668 temp = I915_READ(reg);
3669 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003670 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003671 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003672 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003673 temp |= snb_b_fdi_train_param[j/2];
3674 temp |= FDI_COMPOSITE_SYNC;
3675 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3676
3677 I915_WRITE(FDI_RX_MISC(pipe),
3678 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3679
3680 reg = FDI_RX_CTL(pipe);
3681 temp = I915_READ(reg);
3682 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3683 temp |= FDI_COMPOSITE_SYNC;
3684 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3685
3686 POSTING_READ(reg);
3687 udelay(1); /* should be 0.5us */
3688
3689 for (i = 0; i < 4; i++) {
3690 reg = FDI_RX_IIR(pipe);
3691 temp = I915_READ(reg);
3692 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3693
3694 if (temp & FDI_RX_BIT_LOCK ||
3695 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3696 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3697 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3698 i);
3699 break;
3700 }
3701 udelay(1); /* should be 0.5us */
3702 }
3703 if (i == 4) {
3704 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3705 continue;
3706 }
3707
3708 /* Train 2 */
3709 reg = FDI_TX_CTL(pipe);
3710 temp = I915_READ(reg);
3711 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3712 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3713 I915_WRITE(reg, temp);
3714
3715 reg = FDI_RX_CTL(pipe);
3716 temp = I915_READ(reg);
3717 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3718 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003719 I915_WRITE(reg, temp);
3720
3721 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003722 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003723
Jesse Barnes139ccd32013-08-19 11:04:55 -07003724 for (i = 0; i < 4; i++) {
3725 reg = FDI_RX_IIR(pipe);
3726 temp = I915_READ(reg);
3727 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003728
Jesse Barnes139ccd32013-08-19 11:04:55 -07003729 if (temp & FDI_RX_SYMBOL_LOCK ||
3730 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3731 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3732 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3733 i);
3734 goto train_done;
3735 }
3736 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003737 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003738 if (i == 4)
3739 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003740 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003741
Jesse Barnes139ccd32013-08-19 11:04:55 -07003742train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003743 DRM_DEBUG_KMS("FDI train done.\n");
3744}
3745
Daniel Vetter88cefb62012-08-12 19:27:14 +02003746static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003747{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003748 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003749 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003750 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003751 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003752
Jesse Barnesc64e3112010-09-10 11:27:03 -07003753
Jesse Barnes0e23b992010-09-10 11:10:00 -07003754 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003755 reg = FDI_RX_CTL(pipe);
3756 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003757 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003758 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003759 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003760 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3761
3762 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003763 udelay(200);
3764
3765 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003766 temp = I915_READ(reg);
3767 I915_WRITE(reg, temp | FDI_PCDCLK);
3768
3769 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003770 udelay(200);
3771
Paulo Zanoni20749732012-11-23 15:30:38 -02003772 /* Enable CPU FDI TX PLL, always on for Ironlake */
3773 reg = FDI_TX_CTL(pipe);
3774 temp = I915_READ(reg);
3775 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3776 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003777
Paulo Zanoni20749732012-11-23 15:30:38 -02003778 POSTING_READ(reg);
3779 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003780 }
3781}
3782
Daniel Vetter88cefb62012-08-12 19:27:14 +02003783static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3784{
3785 struct drm_device *dev = intel_crtc->base.dev;
3786 struct drm_i915_private *dev_priv = dev->dev_private;
3787 int pipe = intel_crtc->pipe;
3788 u32 reg, temp;
3789
3790 /* Switch from PCDclk to Rawclk */
3791 reg = FDI_RX_CTL(pipe);
3792 temp = I915_READ(reg);
3793 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3794
3795 /* Disable CPU FDI TX PLL */
3796 reg = FDI_TX_CTL(pipe);
3797 temp = I915_READ(reg);
3798 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3799
3800 POSTING_READ(reg);
3801 udelay(100);
3802
3803 reg = FDI_RX_CTL(pipe);
3804 temp = I915_READ(reg);
3805 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3806
3807 /* Wait for the clocks to turn off. */
3808 POSTING_READ(reg);
3809 udelay(100);
3810}
3811
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003812static void ironlake_fdi_disable(struct drm_crtc *crtc)
3813{
3814 struct drm_device *dev = crtc->dev;
3815 struct drm_i915_private *dev_priv = dev->dev_private;
3816 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3817 int pipe = intel_crtc->pipe;
3818 u32 reg, temp;
3819
3820 /* disable CPU FDI tx and PCH FDI rx */
3821 reg = FDI_TX_CTL(pipe);
3822 temp = I915_READ(reg);
3823 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3824 POSTING_READ(reg);
3825
3826 reg = FDI_RX_CTL(pipe);
3827 temp = I915_READ(reg);
3828 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003829 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003830 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3831
3832 POSTING_READ(reg);
3833 udelay(100);
3834
3835 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003836 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003837 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003838
3839 /* still set train pattern 1 */
3840 reg = FDI_TX_CTL(pipe);
3841 temp = I915_READ(reg);
3842 temp &= ~FDI_LINK_TRAIN_NONE;
3843 temp |= FDI_LINK_TRAIN_PATTERN_1;
3844 I915_WRITE(reg, temp);
3845
3846 reg = FDI_RX_CTL(pipe);
3847 temp = I915_READ(reg);
3848 if (HAS_PCH_CPT(dev)) {
3849 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3850 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3851 } else {
3852 temp &= ~FDI_LINK_TRAIN_NONE;
3853 temp |= FDI_LINK_TRAIN_PATTERN_1;
3854 }
3855 /* BPC in FDI rx is consistent with that in PIPECONF */
3856 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003857 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003858 I915_WRITE(reg, temp);
3859
3860 POSTING_READ(reg);
3861 udelay(100);
3862}
3863
Chris Wilson5dce5b932014-01-20 10:17:36 +00003864bool intel_has_pending_fb_unpin(struct drm_device *dev)
3865{
3866 struct intel_crtc *crtc;
3867
3868 /* Note that we don't need to be called with mode_config.lock here
3869 * as our list of CRTC objects is static for the lifetime of the
3870 * device and so cannot disappear as we iterate. Similarly, we can
3871 * happily treat the predicates as racy, atomic checks as userspace
3872 * cannot claim and pin a new fb without at least acquring the
3873 * struct_mutex and so serialising with us.
3874 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003875 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003876 if (atomic_read(&crtc->unpin_work_count) == 0)
3877 continue;
3878
3879 if (crtc->unpin_work)
3880 intel_wait_for_vblank(dev, crtc->pipe);
3881
3882 return true;
3883 }
3884
3885 return false;
3886}
3887
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003888static void page_flip_completed(struct intel_crtc *intel_crtc)
3889{
3890 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3891 struct intel_unpin_work *work = intel_crtc->unpin_work;
3892
3893 /* ensure that the unpin work is consistent wrt ->pending. */
3894 smp_rmb();
3895 intel_crtc->unpin_work = NULL;
3896
3897 if (work->event)
3898 drm_send_vblank_event(intel_crtc->base.dev,
3899 intel_crtc->pipe,
3900 work->event);
3901
3902 drm_crtc_vblank_put(&intel_crtc->base);
3903
3904 wake_up_all(&dev_priv->pending_flip_queue);
3905 queue_work(dev_priv->wq, &work->work);
3906
3907 trace_i915_flip_complete(intel_crtc->plane,
3908 work->pending_flip_obj);
3909}
3910
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003911void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003912{
Chris Wilson0f911282012-04-17 10:05:38 +01003913 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003914 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003915
Daniel Vetter2c10d572012-12-20 21:24:07 +01003916 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003917 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3918 !intel_crtc_has_pending_flip(crtc),
3919 60*HZ) == 0)) {
3920 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003921
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003922 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003923 if (intel_crtc->unpin_work) {
3924 WARN_ONCE(1, "Removing stuck page flip\n");
3925 page_flip_completed(intel_crtc);
3926 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003927 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003928 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003929
Chris Wilson975d5682014-08-20 13:13:34 +01003930 if (crtc->primary->fb) {
3931 mutex_lock(&dev->struct_mutex);
3932 intel_finish_fb(crtc->primary->fb);
3933 mutex_unlock(&dev->struct_mutex);
3934 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003935}
3936
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003937/* Program iCLKIP clock to the desired frequency */
3938static void lpt_program_iclkip(struct drm_crtc *crtc)
3939{
3940 struct drm_device *dev = crtc->dev;
3941 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003942 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003943 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3944 u32 temp;
3945
Ville Syrjäläa5805162015-05-26 20:42:30 +03003946 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003947
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948 /* It is necessary to ungate the pixclk gate prior to programming
3949 * the divisors, and gate it back when it is done.
3950 */
3951 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3952
3953 /* Disable SSCCTL */
3954 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003955 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3956 SBI_SSCCTL_DISABLE,
3957 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958
3959 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003960 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003961 auxdiv = 1;
3962 divsel = 0x41;
3963 phaseinc = 0x20;
3964 } else {
3965 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003966 * but the adjusted_mode->crtc_clock in in KHz. To get the
3967 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003968 * convert the virtual clock precision to KHz here for higher
3969 * precision.
3970 */
3971 u32 iclk_virtual_root_freq = 172800 * 1000;
3972 u32 iclk_pi_range = 64;
3973 u32 desired_divisor, msb_divisor_value, pi_value;
3974
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003975 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003976 msb_divisor_value = desired_divisor / iclk_pi_range;
3977 pi_value = desired_divisor % iclk_pi_range;
3978
3979 auxdiv = 0;
3980 divsel = msb_divisor_value - 2;
3981 phaseinc = pi_value;
3982 }
3983
3984 /* This should not happen with any sane values */
3985 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3986 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3987 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3988 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3989
3990 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 +03003991 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003992 auxdiv,
3993 divsel,
3994 phasedir,
3995 phaseinc);
3996
3997 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003998 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003999 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4000 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4001 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4002 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4003 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4004 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004005 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004006
4007 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004008 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004009 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4010 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004011 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004012
4013 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004014 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004015 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004016 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004017
4018 /* Wait for initialization time */
4019 udelay(24);
4020
4021 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004022
Ville Syrjäläa5805162015-05-26 20:42:30 +03004023 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004024}
4025
Daniel Vetter275f01b22013-05-03 11:49:47 +02004026static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4027 enum pipe pch_transcoder)
4028{
4029 struct drm_device *dev = crtc->base.dev;
4030 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004031 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004032
4033 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4034 I915_READ(HTOTAL(cpu_transcoder)));
4035 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4036 I915_READ(HBLANK(cpu_transcoder)));
4037 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4038 I915_READ(HSYNC(cpu_transcoder)));
4039
4040 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4041 I915_READ(VTOTAL(cpu_transcoder)));
4042 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4043 I915_READ(VBLANK(cpu_transcoder)));
4044 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4045 I915_READ(VSYNC(cpu_transcoder)));
4046 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4047 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4048}
4049
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004050static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004051{
4052 struct drm_i915_private *dev_priv = dev->dev_private;
4053 uint32_t temp;
4054
4055 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004056 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004057 return;
4058
4059 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4060 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4061
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004062 temp &= ~FDI_BC_BIFURCATION_SELECT;
4063 if (enable)
4064 temp |= FDI_BC_BIFURCATION_SELECT;
4065
4066 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004067 I915_WRITE(SOUTH_CHICKEN1, temp);
4068 POSTING_READ(SOUTH_CHICKEN1);
4069}
4070
4071static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4072{
4073 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004074
4075 switch (intel_crtc->pipe) {
4076 case PIPE_A:
4077 break;
4078 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004079 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004080 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004081 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004082 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004083
4084 break;
4085 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004086 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004087
4088 break;
4089 default:
4090 BUG();
4091 }
4092}
4093
Jesse Barnesf67a5592011-01-05 10:31:48 -08004094/*
4095 * Enable PCH resources required for PCH ports:
4096 * - PCH PLLs
4097 * - FDI training & RX/TX
4098 * - update transcoder timings
4099 * - DP transcoding bits
4100 * - transcoder
4101 */
4102static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004103{
4104 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004105 struct drm_i915_private *dev_priv = dev->dev_private;
4106 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4107 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004108 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004109
Daniel Vetterab9412b2013-05-03 11:49:46 +02004110 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004111
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004112 if (IS_IVYBRIDGE(dev))
4113 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4114
Daniel Vettercd986ab2012-10-26 10:58:12 +02004115 /* Write the TU size bits before fdi link training, so that error
4116 * detection works. */
4117 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4118 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4119
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004120 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004121 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004122
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004123 /* We need to program the right clock selection before writing the pixel
4124 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004125 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004126 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004127
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004128 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004129 temp |= TRANS_DPLL_ENABLE(pipe);
4130 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004131 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004132 temp |= sel;
4133 else
4134 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004135 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004136 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004138 /* XXX: pch pll's can be enabled any time before we enable the PCH
4139 * transcoder, and we actually should do this to not upset any PCH
4140 * transcoder that already use the clock when we share it.
4141 *
4142 * Note that enable_shared_dpll tries to do the right thing, but
4143 * get_shared_dpll unconditionally resets the pll - we need that to have
4144 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004145 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004146
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004147 /* set transcoder timing, panel must allow it */
4148 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004149 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004151 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004152
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004154 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004155 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004156 reg = TRANS_DP_CTL(pipe);
4157 temp = I915_READ(reg);
4158 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004159 TRANS_DP_SYNC_MASK |
4160 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004161 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004162 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163
4164 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004165 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004166 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004167 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168
4169 switch (intel_trans_dp_port_sel(crtc)) {
4170 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004171 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004172 break;
4173 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004174 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004175 break;
4176 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004177 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004178 break;
4179 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004180 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004181 }
4182
Chris Wilson5eddb702010-09-11 13:48:45 +01004183 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004184 }
4185
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004186 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004187}
4188
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004189static void lpt_pch_enable(struct drm_crtc *crtc)
4190{
4191 struct drm_device *dev = crtc->dev;
4192 struct drm_i915_private *dev_priv = dev->dev_private;
4193 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004194 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004195
Daniel Vetterab9412b2013-05-03 11:49:46 +02004196 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004197
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004198 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004199
Paulo Zanoni0540e482012-10-31 18:12:40 -02004200 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004201 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004202
Paulo Zanoni937bb612012-10-31 18:12:47 -02004203 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004204}
4205
Daniel Vetter716c2e52014-06-25 22:02:02 +03004206void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004207{
Daniel Vettere2b78262013-06-07 23:10:03 +02004208 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004209
4210 if (pll == NULL)
4211 return;
4212
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004213 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004214 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004215 return;
4216 }
4217
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004218 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4219 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004220 WARN_ON(pll->on);
4221 WARN_ON(pll->active);
4222 }
4223
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004224 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004225}
4226
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004227struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4228 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004229{
Daniel Vettere2b78262013-06-07 23:10:03 +02004230 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004231 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004232 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004233
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004234 if (HAS_PCH_IBX(dev_priv->dev)) {
4235 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004236 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004237 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004238
Daniel Vetter46edb022013-06-05 13:34:12 +02004239 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4240 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004241
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004242 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004243
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004244 goto found;
4245 }
4246
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304247 if (IS_BROXTON(dev_priv->dev)) {
4248 /* PLL is attached to port in bxt */
4249 struct intel_encoder *encoder;
4250 struct intel_digital_port *intel_dig_port;
4251
4252 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4253 if (WARN_ON(!encoder))
4254 return NULL;
4255
4256 intel_dig_port = enc_to_dig_port(&encoder->base);
4257 /* 1:1 mapping between ports and PLLs */
4258 i = (enum intel_dpll_id)intel_dig_port->port;
4259 pll = &dev_priv->shared_dplls[i];
4260 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4261 crtc->base.base.id, pll->name);
4262 WARN_ON(pll->new_config->crtc_mask);
4263
4264 goto found;
4265 }
4266
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004267 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4268 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004269
4270 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004271 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004272 continue;
4273
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004274 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004275 &pll->new_config->hw_state,
4276 sizeof(pll->new_config->hw_state)) == 0) {
4277 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004278 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004279 pll->new_config->crtc_mask,
4280 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004281 goto found;
4282 }
4283 }
4284
4285 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004286 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4287 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004288 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004289 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4290 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004291 goto found;
4292 }
4293 }
4294
4295 return NULL;
4296
4297found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004298 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004299 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004300
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004301 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004302 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4303 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004304
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004305 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004306
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004307 return pll;
4308}
4309
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004310/**
4311 * intel_shared_dpll_start_config - start a new PLL staged config
4312 * @dev_priv: DRM device
4313 * @clear_pipes: mask of pipes that will have their PLLs freed
4314 *
4315 * Starts a new PLL staged config, copying the current config but
4316 * releasing the references of pipes specified in clear_pipes.
4317 */
4318static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4319 unsigned clear_pipes)
4320{
4321 struct intel_shared_dpll *pll;
4322 enum intel_dpll_id i;
4323
4324 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4325 pll = &dev_priv->shared_dplls[i];
4326
4327 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4328 GFP_KERNEL);
4329 if (!pll->new_config)
4330 goto cleanup;
4331
4332 pll->new_config->crtc_mask &= ~clear_pipes;
4333 }
4334
4335 return 0;
4336
4337cleanup:
4338 while (--i >= 0) {
4339 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004340 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004341 pll->new_config = NULL;
4342 }
4343
4344 return -ENOMEM;
4345}
4346
4347static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4348{
4349 struct intel_shared_dpll *pll;
4350 enum intel_dpll_id i;
4351
4352 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4353 pll = &dev_priv->shared_dplls[i];
4354
4355 WARN_ON(pll->new_config == &pll->config);
4356
4357 pll->config = *pll->new_config;
4358 kfree(pll->new_config);
4359 pll->new_config = NULL;
4360 }
4361}
4362
4363static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4364{
4365 struct intel_shared_dpll *pll;
4366 enum intel_dpll_id i;
4367
4368 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4369 pll = &dev_priv->shared_dplls[i];
4370
4371 WARN_ON(pll->new_config == &pll->config);
4372
4373 kfree(pll->new_config);
4374 pll->new_config = NULL;
4375 }
4376}
4377
Daniel Vettera1520312013-05-03 11:49:50 +02004378static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004379{
4380 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004381 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004382 u32 temp;
4383
4384 temp = I915_READ(dslreg);
4385 udelay(500);
4386 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004387 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004388 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004389 }
4390}
4391
Chandra Kondurua1b22782015-04-07 15:28:45 -07004392/**
4393 * skl_update_scaler_users - Stages update to crtc's scaler state
4394 * @intel_crtc: crtc
4395 * @crtc_state: crtc_state
4396 * @plane: plane (NULL indicates crtc is requesting update)
4397 * @plane_state: plane's state
4398 * @force_detach: request unconditional detachment of scaler
4399 *
4400 * This function updates scaler state for requested plane or crtc.
4401 * To request scaler usage update for a plane, caller shall pass plane pointer.
4402 * To request scaler usage update for crtc, caller shall pass plane pointer
4403 * as NULL.
4404 *
4405 * Return
4406 * 0 - scaler_usage updated successfully
4407 * error - requested scaling cannot be supported or other error condition
4408 */
4409int
4410skl_update_scaler_users(
4411 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4412 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4413 int force_detach)
4414{
4415 int need_scaling;
4416 int idx;
4417 int src_w, src_h, dst_w, dst_h;
4418 int *scaler_id;
4419 struct drm_framebuffer *fb;
4420 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004421 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004422
4423 if (!intel_crtc || !crtc_state)
4424 return 0;
4425
4426 scaler_state = &crtc_state->scaler_state;
4427
4428 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4429 fb = intel_plane ? plane_state->base.fb : NULL;
4430
4431 if (intel_plane) {
4432 src_w = drm_rect_width(&plane_state->src) >> 16;
4433 src_h = drm_rect_height(&plane_state->src) >> 16;
4434 dst_w = drm_rect_width(&plane_state->dst);
4435 dst_h = drm_rect_height(&plane_state->dst);
4436 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004437 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004438 } else {
4439 struct drm_display_mode *adjusted_mode =
4440 &crtc_state->base.adjusted_mode;
4441 src_w = crtc_state->pipe_src_w;
4442 src_h = crtc_state->pipe_src_h;
4443 dst_w = adjusted_mode->hdisplay;
4444 dst_h = adjusted_mode->vdisplay;
4445 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004446 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004447 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004448
4449 need_scaling = intel_rotation_90_or_270(rotation) ?
4450 (src_h != dst_w || src_w != dst_h):
4451 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004452
4453 /*
4454 * if plane is being disabled or scaler is no more required or force detach
4455 * - free scaler binded to this plane/crtc
4456 * - in order to do this, update crtc->scaler_usage
4457 *
4458 * Here scaler state in crtc_state is set free so that
4459 * scaler can be assigned to other user. Actual register
4460 * update to free the scaler is done in plane/panel-fit programming.
4461 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4462 */
4463 if (force_detach || !need_scaling || (intel_plane &&
4464 (!fb || !plane_state->visible))) {
4465 if (*scaler_id >= 0) {
4466 scaler_state->scaler_users &= ~(1 << idx);
4467 scaler_state->scalers[*scaler_id].in_use = 0;
4468
4469 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4470 "crtc_state = %p scaler_users = 0x%x\n",
4471 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4472 intel_plane ? intel_plane->base.base.id :
4473 intel_crtc->base.base.id, crtc_state,
4474 scaler_state->scaler_users);
4475 *scaler_id = -1;
4476 }
4477 return 0;
4478 }
4479
4480 /* range checks */
4481 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4482 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4483
4484 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4485 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4486 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4487 "size is out of scaler range\n",
4488 intel_plane ? "PLANE" : "CRTC",
4489 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4490 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4491 return -EINVAL;
4492 }
4493
4494 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004495 if (WARN_ON(intel_plane &&
4496 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4497 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4498 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004499 return -EINVAL;
4500 }
4501
4502 /* Check src format */
4503 if (intel_plane) {
4504 switch (fb->pixel_format) {
4505 case DRM_FORMAT_RGB565:
4506 case DRM_FORMAT_XBGR8888:
4507 case DRM_FORMAT_XRGB8888:
4508 case DRM_FORMAT_ABGR8888:
4509 case DRM_FORMAT_ARGB8888:
4510 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004511 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004512 case DRM_FORMAT_YUYV:
4513 case DRM_FORMAT_YVYU:
4514 case DRM_FORMAT_UYVY:
4515 case DRM_FORMAT_VYUY:
4516 break;
4517 default:
4518 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4519 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4520 return -EINVAL;
4521 }
4522 }
4523
4524 /* mark this plane as a scaler user in crtc_state */
4525 scaler_state->scaler_users |= (1 << idx);
4526 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4527 "crtc_state = %p scaler_users = 0x%x\n",
4528 intel_plane ? "PLANE" : "CRTC",
4529 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4530 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4531 return 0;
4532}
4533
4534static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004535{
4536 struct drm_device *dev = crtc->base.dev;
4537 struct drm_i915_private *dev_priv = dev->dev_private;
4538 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004539 struct intel_crtc_scaler_state *scaler_state =
4540 &crtc->config->scaler_state;
4541
4542 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4543
4544 /* To update pfit, first update scaler state */
4545 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4546 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4547 skl_detach_scalers(crtc);
4548 if (!enable)
4549 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004550
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004551 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004552 int id;
4553
4554 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4555 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4556 return;
4557 }
4558
4559 id = scaler_state->scaler_id;
4560 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4561 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4562 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4563 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4564
4565 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004566 }
4567}
4568
Jesse Barnesb074cec2013-04-25 12:55:02 -07004569static void ironlake_pfit_enable(struct intel_crtc *crtc)
4570{
4571 struct drm_device *dev = crtc->base.dev;
4572 struct drm_i915_private *dev_priv = dev->dev_private;
4573 int pipe = crtc->pipe;
4574
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004575 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004576 /* Force use of hard-coded filter coefficients
4577 * as some pre-programmed values are broken,
4578 * e.g. x201.
4579 */
4580 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4581 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4582 PF_PIPE_SEL_IVB(pipe));
4583 else
4584 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004585 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4586 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004587 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004588}
4589
Matt Roper4a3b8762014-12-23 10:41:51 -08004590static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004591{
4592 struct drm_device *dev = crtc->dev;
4593 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004594 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004595 struct intel_plane *intel_plane;
4596
Matt Roperaf2b6532014-04-01 15:22:32 -07004597 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4598 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004599 if (intel_plane->pipe == pipe)
4600 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004601 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004602}
4603
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004604void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004605{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004606 struct drm_device *dev = crtc->base.dev;
4607 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004608
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004609 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004610 return;
4611
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004612 /* We can only enable IPS after we enable a plane and wait for a vblank */
4613 intel_wait_for_vblank(dev, crtc->pipe);
4614
Paulo Zanonid77e4532013-09-24 13:52:55 -03004615 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004616 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004617 mutex_lock(&dev_priv->rps.hw_lock);
4618 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4619 mutex_unlock(&dev_priv->rps.hw_lock);
4620 /* Quoting Art Runyan: "its not safe to expect any particular
4621 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004622 * mailbox." Moreover, the mailbox may return a bogus state,
4623 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004624 */
4625 } else {
4626 I915_WRITE(IPS_CTL, IPS_ENABLE);
4627 /* The bit only becomes 1 in the next vblank, so this wait here
4628 * is essentially intel_wait_for_vblank. If we don't have this
4629 * and don't wait for vblanks until the end of crtc_enable, then
4630 * the HW state readout code will complain that the expected
4631 * IPS_CTL value is not the one we read. */
4632 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4633 DRM_ERROR("Timed out waiting for IPS enable\n");
4634 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004635}
4636
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004637void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004638{
4639 struct drm_device *dev = crtc->base.dev;
4640 struct drm_i915_private *dev_priv = dev->dev_private;
4641
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004642 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004643 return;
4644
4645 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004646 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004647 mutex_lock(&dev_priv->rps.hw_lock);
4648 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4649 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004650 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4651 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4652 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004653 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004654 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004655 POSTING_READ(IPS_CTL);
4656 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004657
4658 /* We need to wait for a vblank before we can disable the plane. */
4659 intel_wait_for_vblank(dev, crtc->pipe);
4660}
4661
4662/** Loads the palette/gamma unit for the CRTC with the prepared values */
4663static void intel_crtc_load_lut(struct drm_crtc *crtc)
4664{
4665 struct drm_device *dev = crtc->dev;
4666 struct drm_i915_private *dev_priv = dev->dev_private;
4667 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4668 enum pipe pipe = intel_crtc->pipe;
4669 int palreg = PALETTE(pipe);
4670 int i;
4671 bool reenable_ips = false;
4672
4673 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004674 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004675 return;
4676
Imre Deak50360402015-01-16 00:55:16 -08004677 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004678 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004679 assert_dsi_pll_enabled(dev_priv);
4680 else
4681 assert_pll_enabled(dev_priv, pipe);
4682 }
4683
4684 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304685 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004686 palreg = LGC_PALETTE(pipe);
4687
4688 /* Workaround : Do not read or write the pipe palette/gamma data while
4689 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4690 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004691 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004692 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4693 GAMMA_MODE_MODE_SPLIT)) {
4694 hsw_disable_ips(intel_crtc);
4695 reenable_ips = true;
4696 }
4697
4698 for (i = 0; i < 256; i++) {
4699 I915_WRITE(palreg + 4 * i,
4700 (intel_crtc->lut_r[i] << 16) |
4701 (intel_crtc->lut_g[i] << 8) |
4702 intel_crtc->lut_b[i]);
4703 }
4704
4705 if (reenable_ips)
4706 hsw_enable_ips(intel_crtc);
4707}
4708
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004709static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004710{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004711 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004712 struct drm_device *dev = intel_crtc->base.dev;
4713 struct drm_i915_private *dev_priv = dev->dev_private;
4714
4715 mutex_lock(&dev->struct_mutex);
4716 dev_priv->mm.interruptible = false;
4717 (void) intel_overlay_switch_off(intel_crtc->overlay);
4718 dev_priv->mm.interruptible = true;
4719 mutex_unlock(&dev->struct_mutex);
4720 }
4721
4722 /* Let userspace switch the overlay on again. In most cases userspace
4723 * has to recompute where to put it anyway.
4724 */
4725}
4726
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004727/**
4728 * intel_post_enable_primary - Perform operations after enabling primary plane
4729 * @crtc: the CRTC whose primary plane was just enabled
4730 *
4731 * Performs potentially sleeping operations that must be done after the primary
4732 * plane is enabled, such as updating FBC and IPS. Note that this may be
4733 * called due to an explicit primary plane update, or due to an implicit
4734 * re-enable that is caused when a sprite plane is updated to no longer
4735 * completely hide the primary plane.
4736 */
4737static void
4738intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004739{
4740 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004741 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004742 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4743 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004744
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004745 /*
4746 * BDW signals flip done immediately if the plane
4747 * is disabled, even if the plane enable is already
4748 * armed to occur at the next vblank :(
4749 */
4750 if (IS_BROADWELL(dev))
4751 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004752
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004753 /*
4754 * FIXME IPS should be fine as long as one plane is
4755 * enabled, but in practice it seems to have problems
4756 * when going from primary only to sprite only and vice
4757 * versa.
4758 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004759 hsw_enable_ips(intel_crtc);
4760
4761 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004762 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004763 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004764
4765 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004766 * Gen2 reports pipe underruns whenever all planes are disabled.
4767 * So don't enable underrun reporting before at least some planes
4768 * are enabled.
4769 * FIXME: Need to fix the logic to work when we turn off all planes
4770 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004771 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004772 if (IS_GEN2(dev))
4773 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4774
4775 /* Underruns don't raise interrupts, so check manually. */
4776 if (HAS_GMCH_DISPLAY(dev))
4777 i9xx_check_fifo_underruns(dev_priv);
4778}
4779
4780/**
4781 * intel_pre_disable_primary - Perform operations before disabling primary plane
4782 * @crtc: the CRTC whose primary plane is to be disabled
4783 *
4784 * Performs potentially sleeping operations that must be done before the
4785 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4786 * be called due to an explicit primary plane update, or due to an implicit
4787 * disable that is caused when a sprite plane completely hides the primary
4788 * plane.
4789 */
4790static void
4791intel_pre_disable_primary(struct drm_crtc *crtc)
4792{
4793 struct drm_device *dev = crtc->dev;
4794 struct drm_i915_private *dev_priv = dev->dev_private;
4795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4796 int pipe = intel_crtc->pipe;
4797
4798 /*
4799 * Gen2 reports pipe underruns whenever all planes are disabled.
4800 * So diasble underrun reporting before all the planes get disabled.
4801 * FIXME: Need to fix the logic to work when we turn off all planes
4802 * but leave the pipe running.
4803 */
4804 if (IS_GEN2(dev))
4805 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4806
4807 /*
4808 * Vblank time updates from the shadow to live plane control register
4809 * are blocked if the memory self-refresh mode is active at that
4810 * moment. So to make sure the plane gets truly disabled, disable
4811 * first the self-refresh mode. The self-refresh enable bit in turn
4812 * will be checked/applied by the HW only at the next frame start
4813 * event which is after the vblank start event, so we need to have a
4814 * wait-for-vblank between disabling the plane and the pipe.
4815 */
4816 if (HAS_GMCH_DISPLAY(dev))
4817 intel_set_memory_cxsr(dev_priv, false);
4818
4819 mutex_lock(&dev->struct_mutex);
4820 if (dev_priv->fbc.crtc == intel_crtc)
4821 intel_fbc_disable(dev);
4822 mutex_unlock(&dev->struct_mutex);
4823
4824 /*
4825 * FIXME IPS should be fine as long as one plane is
4826 * enabled, but in practice it seems to have problems
4827 * when going from primary only to sprite only and vice
4828 * versa.
4829 */
4830 hsw_disable_ips(intel_crtc);
4831}
4832
4833static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4834{
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004835 struct drm_device *dev = crtc->dev;
4836 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4837 int pipe = intel_crtc->pipe;
4838
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004839 intel_enable_primary_hw_plane(crtc->primary, crtc);
4840 intel_enable_sprite_planes(crtc);
4841 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004842
4843 intel_post_enable_primary(crtc);
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004844
4845 /*
4846 * FIXME: Once we grow proper nuclear flip support out of this we need
4847 * to compute the mask of flip planes precisely. For the time being
4848 * consider this a flip to a NULL plane.
4849 */
4850 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004851}
4852
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004853static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004854{
4855 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004856 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004857 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004858 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004859
4860 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004861
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004862 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004863
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004864 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004865 for_each_intel_plane(dev, intel_plane) {
4866 if (intel_plane->pipe == pipe) {
4867 struct drm_crtc *from = intel_plane->base.crtc;
4868
4869 intel_plane->disable_plane(&intel_plane->base,
4870 from ?: crtc, true);
4871 }
4872 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004873
Daniel Vetterf99d7062014-06-19 16:01:59 +02004874 /*
4875 * FIXME: Once we grow proper nuclear flip support out of this we need
4876 * to compute the mask of flip planes precisely. For the time being
4877 * consider this a flip to a NULL plane.
4878 */
4879 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004880}
4881
Jesse Barnesf67a5592011-01-05 10:31:48 -08004882static void ironlake_crtc_enable(struct drm_crtc *crtc)
4883{
4884 struct drm_device *dev = crtc->dev;
4885 struct drm_i915_private *dev_priv = dev->dev_private;
4886 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004887 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004888 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004889
Matt Roper83d65732015-02-25 13:12:16 -08004890 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004891
Jesse Barnesf67a5592011-01-05 10:31:48 -08004892 if (intel_crtc->active)
4893 return;
4894
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004895 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004896 intel_prepare_shared_dpll(intel_crtc);
4897
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004898 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304899 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004900
4901 intel_set_pipe_timings(intel_crtc);
4902
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004903 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004904 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004905 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004906 }
4907
4908 ironlake_set_pipeconf(crtc);
4909
Jesse Barnesf67a5592011-01-05 10:31:48 -08004910 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004911
Daniel Vettera72e4c92014-09-30 10:56:47 +02004912 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4913 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004914
Daniel Vetterf6736a12013-06-05 13:34:30 +02004915 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004916 if (encoder->pre_enable)
4917 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004918
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004919 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004920 /* Note: FDI PLL enabling _must_ be done before we enable the
4921 * cpu pipes, hence this is separate from all the other fdi/pch
4922 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004923 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004924 } else {
4925 assert_fdi_tx_disabled(dev_priv, pipe);
4926 assert_fdi_rx_disabled(dev_priv, pipe);
4927 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004928
Jesse Barnesb074cec2013-04-25 12:55:02 -07004929 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004930
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004931 /*
4932 * On ILK+ LUT must be loaded before the pipe is running but with
4933 * clocks enabled
4934 */
4935 intel_crtc_load_lut(crtc);
4936
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004937 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004938 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004939
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004940 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004941 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004942
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004943 assert_vblank_disabled(crtc);
4944 drm_crtc_vblank_on(crtc);
4945
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004946 for_each_encoder_on_crtc(dev, crtc, encoder)
4947 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004948
4949 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004950 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004951}
4952
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004953/* IPS only exists on ULT machines and is tied to pipe A. */
4954static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4955{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004956 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004957}
4958
Paulo Zanonie4916942013-09-20 16:21:19 -03004959/*
4960 * This implements the workaround described in the "notes" section of the mode
4961 * set sequence documentation. When going from no pipes or single pipe to
4962 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4963 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4964 */
4965static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4966{
4967 struct drm_device *dev = crtc->base.dev;
4968 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4969
4970 /* We want to get the other_active_crtc only if there's only 1 other
4971 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004972 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004973 if (!crtc_it->active || crtc_it == crtc)
4974 continue;
4975
4976 if (other_active_crtc)
4977 return;
4978
4979 other_active_crtc = crtc_it;
4980 }
4981 if (!other_active_crtc)
4982 return;
4983
4984 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4985 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4986}
4987
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004988static void haswell_crtc_enable(struct drm_crtc *crtc)
4989{
4990 struct drm_device *dev = crtc->dev;
4991 struct drm_i915_private *dev_priv = dev->dev_private;
4992 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4993 struct intel_encoder *encoder;
4994 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004995
Matt Roper83d65732015-02-25 13:12:16 -08004996 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004997
4998 if (intel_crtc->active)
4999 return;
5000
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005001 if (intel_crtc_to_shared_dpll(intel_crtc))
5002 intel_enable_shared_dpll(intel_crtc);
5003
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005004 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305005 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005006
5007 intel_set_pipe_timings(intel_crtc);
5008
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005009 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5010 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5011 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005012 }
5013
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005014 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005015 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005016 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005017 }
5018
5019 haswell_set_pipeconf(crtc);
5020
5021 intel_set_pipe_csc(crtc);
5022
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005023 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005024
Daniel Vettera72e4c92014-09-30 10:56:47 +02005025 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005026 for_each_encoder_on_crtc(dev, crtc, encoder)
5027 if (encoder->pre_enable)
5028 encoder->pre_enable(encoder);
5029
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005030 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005031 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5032 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005033 dev_priv->display.fdi_link_train(crtc);
5034 }
5035
Paulo Zanoni1f544382012-10-24 11:32:00 -02005036 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005037
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005038 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005039 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005040 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005041 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005042 else
5043 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
5045 /*
5046 * On ILK+ LUT must be loaded before the pipe is running but with
5047 * clocks enabled
5048 */
5049 intel_crtc_load_lut(crtc);
5050
Paulo Zanoni1f544382012-10-24 11:32:00 -02005051 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005052 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005053
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005054 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005055 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005056
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005057 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005058 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005059
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005060 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005061 intel_ddi_set_vc_payload_alloc(crtc, true);
5062
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005063 assert_vblank_disabled(crtc);
5064 drm_crtc_vblank_on(crtc);
5065
Jani Nikula8807e552013-08-30 19:40:32 +03005066 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005067 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005068 intel_opregion_notify_encoder(encoder, true);
5069 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005070
Paulo Zanonie4916942013-09-20 16:21:19 -03005071 /* If we change the relative order between pipe/planes enabling, we need
5072 * to change the workaround. */
5073 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005074}
5075
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005076static void ironlake_pfit_disable(struct intel_crtc *crtc)
5077{
5078 struct drm_device *dev = crtc->base.dev;
5079 struct drm_i915_private *dev_priv = dev->dev_private;
5080 int pipe = crtc->pipe;
5081
5082 /* To avoid upsetting the power well on haswell only disable the pfit if
5083 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005084 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005085 I915_WRITE(PF_CTL(pipe), 0);
5086 I915_WRITE(PF_WIN_POS(pipe), 0);
5087 I915_WRITE(PF_WIN_SZ(pipe), 0);
5088 }
5089}
5090
Jesse Barnes6be4a602010-09-10 10:26:01 -07005091static void ironlake_crtc_disable(struct drm_crtc *crtc)
5092{
5093 struct drm_device *dev = crtc->dev;
5094 struct drm_i915_private *dev_priv = dev->dev_private;
5095 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005096 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005097 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005098 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005099
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005100 if (!intel_crtc->active)
5101 return;
5102
Daniel Vetterea9d7582012-07-10 10:42:52 +02005103 for_each_encoder_on_crtc(dev, crtc, encoder)
5104 encoder->disable(encoder);
5105
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005106 drm_crtc_vblank_off(crtc);
5107 assert_vblank_disabled(crtc);
5108
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005109 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005110 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005111
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005112 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005113
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005114 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005115
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005116 if (intel_crtc->config->has_pch_encoder)
5117 ironlake_fdi_disable(crtc);
5118
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005119 for_each_encoder_on_crtc(dev, crtc, encoder)
5120 if (encoder->post_disable)
5121 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005122
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005123 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005124 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005125
Daniel Vetterd925c592013-06-05 13:34:04 +02005126 if (HAS_PCH_CPT(dev)) {
5127 /* disable TRANS_DP_CTL */
5128 reg = TRANS_DP_CTL(pipe);
5129 temp = I915_READ(reg);
5130 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5131 TRANS_DP_PORT_SEL_MASK);
5132 temp |= TRANS_DP_PORT_SEL_NONE;
5133 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005134
Daniel Vetterd925c592013-06-05 13:34:04 +02005135 /* disable DPLL_SEL */
5136 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005137 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005138 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005139 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005140
5141 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005142 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005143
5144 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005145 }
5146
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005147 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005148 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005149
5150 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005151 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005152 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005153}
5154
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005155static void haswell_crtc_disable(struct drm_crtc *crtc)
5156{
5157 struct drm_device *dev = crtc->dev;
5158 struct drm_i915_private *dev_priv = dev->dev_private;
5159 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5160 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005161 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005162
5163 if (!intel_crtc->active)
5164 return;
5165
Jani Nikula8807e552013-08-30 19:40:32 +03005166 for_each_encoder_on_crtc(dev, crtc, encoder) {
5167 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005168 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005169 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005170
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005171 drm_crtc_vblank_off(crtc);
5172 assert_vblank_disabled(crtc);
5173
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005174 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005175 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5176 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005177 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005178
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005179 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005180 intel_ddi_set_vc_payload_alloc(crtc, false);
5181
Paulo Zanoniad80a812012-10-24 16:06:19 -02005182 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005183
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005184 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005185 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005186 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005187 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005188 else
5189 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005190
Paulo Zanoni1f544382012-10-24 11:32:00 -02005191 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005193 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005194 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005195 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005196 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005197
Imre Deak97b040a2014-06-25 22:01:50 +03005198 for_each_encoder_on_crtc(dev, crtc, encoder)
5199 if (encoder->post_disable)
5200 encoder->post_disable(encoder);
5201
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005202 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005203 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005204
5205 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005206 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005207 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005208
5209 if (intel_crtc_to_shared_dpll(intel_crtc))
5210 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005211}
5212
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005213static void ironlake_crtc_off(struct drm_crtc *crtc)
5214{
5215 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005216 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005217}
5218
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005219
Jesse Barnes2dd24552013-04-25 12:55:01 -07005220static void i9xx_pfit_enable(struct intel_crtc *crtc)
5221{
5222 struct drm_device *dev = crtc->base.dev;
5223 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005224 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005225
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005226 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005227 return;
5228
Daniel Vetterc0b03412013-05-28 12:05:54 +02005229 /*
5230 * The panel fitter should only be adjusted whilst the pipe is disabled,
5231 * according to register description and PRM.
5232 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005233 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5234 assert_pipe_disabled(dev_priv, crtc->pipe);
5235
Jesse Barnesb074cec2013-04-25 12:55:02 -07005236 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5237 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005238
5239 /* Border color in case we don't scale up to the full screen. Black by
5240 * default, change to something else for debugging. */
5241 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005242}
5243
Dave Airlied05410f2014-06-05 13:22:59 +10005244static enum intel_display_power_domain port_to_power_domain(enum port port)
5245{
5246 switch (port) {
5247 case PORT_A:
5248 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5249 case PORT_B:
5250 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5251 case PORT_C:
5252 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5253 case PORT_D:
5254 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5255 default:
5256 WARN_ON_ONCE(1);
5257 return POWER_DOMAIN_PORT_OTHER;
5258 }
5259}
5260
Imre Deak77d22dc2014-03-05 16:20:52 +02005261#define for_each_power_domain(domain, mask) \
5262 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5263 if ((1 << (domain)) & (mask))
5264
Imre Deak319be8a2014-03-04 19:22:57 +02005265enum intel_display_power_domain
5266intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005267{
Imre Deak319be8a2014-03-04 19:22:57 +02005268 struct drm_device *dev = intel_encoder->base.dev;
5269 struct intel_digital_port *intel_dig_port;
5270
5271 switch (intel_encoder->type) {
5272 case INTEL_OUTPUT_UNKNOWN:
5273 /* Only DDI platforms should ever use this output type */
5274 WARN_ON_ONCE(!HAS_DDI(dev));
5275 case INTEL_OUTPUT_DISPLAYPORT:
5276 case INTEL_OUTPUT_HDMI:
5277 case INTEL_OUTPUT_EDP:
5278 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005279 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005280 case INTEL_OUTPUT_DP_MST:
5281 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5282 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005283 case INTEL_OUTPUT_ANALOG:
5284 return POWER_DOMAIN_PORT_CRT;
5285 case INTEL_OUTPUT_DSI:
5286 return POWER_DOMAIN_PORT_DSI;
5287 default:
5288 return POWER_DOMAIN_PORT_OTHER;
5289 }
5290}
5291
5292static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5293{
5294 struct drm_device *dev = crtc->dev;
5295 struct intel_encoder *intel_encoder;
5296 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5297 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005298 unsigned long mask;
5299 enum transcoder transcoder;
5300
5301 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5302
5303 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5304 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005305 if (intel_crtc->config->pch_pfit.enabled ||
5306 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005307 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5308
Imre Deak319be8a2014-03-04 19:22:57 +02005309 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5310 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5311
Imre Deak77d22dc2014-03-05 16:20:52 +02005312 return mask;
5313}
5314
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005315static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005316{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005317 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005318 struct drm_i915_private *dev_priv = dev->dev_private;
5319 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5320 struct intel_crtc *crtc;
5321
5322 /*
5323 * First get all needed power domains, then put all unneeded, to avoid
5324 * any unnecessary toggling of the power wells.
5325 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005326 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005327 enum intel_display_power_domain domain;
5328
Matt Roper83d65732015-02-25 13:12:16 -08005329 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005330 continue;
5331
Imre Deak319be8a2014-03-04 19:22:57 +02005332 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005333
5334 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5335 intel_display_power_get(dev_priv, domain);
5336 }
5337
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005338 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005339 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005340
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005341 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005342 enum intel_display_power_domain domain;
5343
5344 for_each_power_domain(domain, crtc->enabled_power_domains)
5345 intel_display_power_put(dev_priv, domain);
5346
5347 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5348 }
5349
5350 intel_display_set_init_power(dev_priv, false);
5351}
5352
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005353static void intel_update_max_cdclk(struct drm_device *dev)
5354{
5355 struct drm_i915_private *dev_priv = dev->dev_private;
5356
5357 if (IS_SKYLAKE(dev)) {
5358 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5359
5360 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5361 dev_priv->max_cdclk_freq = 675000;
5362 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5363 dev_priv->max_cdclk_freq = 540000;
5364 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5365 dev_priv->max_cdclk_freq = 450000;
5366 else
5367 dev_priv->max_cdclk_freq = 337500;
5368 } else if (IS_BROADWELL(dev)) {
5369 /*
5370 * FIXME with extra cooling we can allow
5371 * 540 MHz for ULX and 675 Mhz for ULT.
5372 * How can we know if extra cooling is
5373 * available? PCI ID, VTB, something else?
5374 */
5375 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5376 dev_priv->max_cdclk_freq = 450000;
5377 else if (IS_BDW_ULX(dev))
5378 dev_priv->max_cdclk_freq = 450000;
5379 else if (IS_BDW_ULT(dev))
5380 dev_priv->max_cdclk_freq = 540000;
5381 else
5382 dev_priv->max_cdclk_freq = 675000;
5383 } else if (IS_VALLEYVIEW(dev)) {
5384 dev_priv->max_cdclk_freq = 400000;
5385 } else {
5386 /* otherwise assume cdclk is fixed */
5387 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5388 }
5389
5390 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5391 dev_priv->max_cdclk_freq);
5392}
5393
5394static void intel_update_cdclk(struct drm_device *dev)
5395{
5396 struct drm_i915_private *dev_priv = dev->dev_private;
5397
5398 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5399 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5400 dev_priv->cdclk_freq);
5401
5402 /*
5403 * Program the gmbus_freq based on the cdclk frequency.
5404 * BSpec erroneously claims we should aim for 4MHz, but
5405 * in fact 1MHz is the correct frequency.
5406 */
5407 if (IS_VALLEYVIEW(dev)) {
5408 /*
5409 * Program the gmbus_freq based on the cdclk frequency.
5410 * BSpec erroneously claims we should aim for 4MHz, but
5411 * in fact 1MHz is the correct frequency.
5412 */
5413 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5414 }
5415
5416 if (dev_priv->max_cdclk_freq == 0)
5417 intel_update_max_cdclk(dev);
5418}
5419
Damien Lespiau70d0c572015-06-04 18:21:29 +01005420static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305421{
5422 struct drm_i915_private *dev_priv = dev->dev_private;
5423 uint32_t divider;
5424 uint32_t ratio;
5425 uint32_t current_freq;
5426 int ret;
5427
5428 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5429 switch (frequency) {
5430 case 144000:
5431 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5432 ratio = BXT_DE_PLL_RATIO(60);
5433 break;
5434 case 288000:
5435 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5436 ratio = BXT_DE_PLL_RATIO(60);
5437 break;
5438 case 384000:
5439 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5440 ratio = BXT_DE_PLL_RATIO(60);
5441 break;
5442 case 576000:
5443 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5444 ratio = BXT_DE_PLL_RATIO(60);
5445 break;
5446 case 624000:
5447 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5448 ratio = BXT_DE_PLL_RATIO(65);
5449 break;
5450 case 19200:
5451 /*
5452 * Bypass frequency with DE PLL disabled. Init ratio, divider
5453 * to suppress GCC warning.
5454 */
5455 ratio = 0;
5456 divider = 0;
5457 break;
5458 default:
5459 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5460
5461 return;
5462 }
5463
5464 mutex_lock(&dev_priv->rps.hw_lock);
5465 /* Inform power controller of upcoming frequency change */
5466 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5467 0x80000000);
5468 mutex_unlock(&dev_priv->rps.hw_lock);
5469
5470 if (ret) {
5471 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5472 ret, frequency);
5473 return;
5474 }
5475
5476 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5477 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5478 current_freq = current_freq * 500 + 1000;
5479
5480 /*
5481 * DE PLL has to be disabled when
5482 * - setting to 19.2MHz (bypass, PLL isn't used)
5483 * - before setting to 624MHz (PLL needs toggling)
5484 * - before setting to any frequency from 624MHz (PLL needs toggling)
5485 */
5486 if (frequency == 19200 || frequency == 624000 ||
5487 current_freq == 624000) {
5488 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5489 /* Timeout 200us */
5490 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5491 1))
5492 DRM_ERROR("timout waiting for DE PLL unlock\n");
5493 }
5494
5495 if (frequency != 19200) {
5496 uint32_t val;
5497
5498 val = I915_READ(BXT_DE_PLL_CTL);
5499 val &= ~BXT_DE_PLL_RATIO_MASK;
5500 val |= ratio;
5501 I915_WRITE(BXT_DE_PLL_CTL, val);
5502
5503 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5504 /* Timeout 200us */
5505 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5506 DRM_ERROR("timeout waiting for DE PLL lock\n");
5507
5508 val = I915_READ(CDCLK_CTL);
5509 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5510 val |= divider;
5511 /*
5512 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5513 * enable otherwise.
5514 */
5515 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5516 if (frequency >= 500000)
5517 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5518
5519 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5520 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5521 val |= (frequency - 1000) / 500;
5522 I915_WRITE(CDCLK_CTL, val);
5523 }
5524
5525 mutex_lock(&dev_priv->rps.hw_lock);
5526 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5527 DIV_ROUND_UP(frequency, 25000));
5528 mutex_unlock(&dev_priv->rps.hw_lock);
5529
5530 if (ret) {
5531 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5532 ret, frequency);
5533 return;
5534 }
5535
Damien Lespiaua47871b2015-06-04 18:21:34 +01005536 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305537}
5538
5539void broxton_init_cdclk(struct drm_device *dev)
5540{
5541 struct drm_i915_private *dev_priv = dev->dev_private;
5542 uint32_t val;
5543
5544 /*
5545 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5546 * or else the reset will hang because there is no PCH to respond.
5547 * Move the handshake programming to initialization sequence.
5548 * Previously was left up to BIOS.
5549 */
5550 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5551 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5552 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5553
5554 /* Enable PG1 for cdclk */
5555 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5556
5557 /* check if cd clock is enabled */
5558 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5559 DRM_DEBUG_KMS("Display already initialized\n");
5560 return;
5561 }
5562
5563 /*
5564 * FIXME:
5565 * - The initial CDCLK needs to be read from VBT.
5566 * Need to make this change after VBT has changes for BXT.
5567 * - check if setting the max (or any) cdclk freq is really necessary
5568 * here, it belongs to modeset time
5569 */
5570 broxton_set_cdclk(dev, 624000);
5571
5572 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005573 POSTING_READ(DBUF_CTL);
5574
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305575 udelay(10);
5576
5577 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5578 DRM_ERROR("DBuf power enable timeout!\n");
5579}
5580
5581void broxton_uninit_cdclk(struct drm_device *dev)
5582{
5583 struct drm_i915_private *dev_priv = dev->dev_private;
5584
5585 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005586 POSTING_READ(DBUF_CTL);
5587
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305588 udelay(10);
5589
5590 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5591 DRM_ERROR("DBuf power disable timeout!\n");
5592
5593 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5594 broxton_set_cdclk(dev, 19200);
5595
5596 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5597}
5598
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005599static const struct skl_cdclk_entry {
5600 unsigned int freq;
5601 unsigned int vco;
5602} skl_cdclk_frequencies[] = {
5603 { .freq = 308570, .vco = 8640 },
5604 { .freq = 337500, .vco = 8100 },
5605 { .freq = 432000, .vco = 8640 },
5606 { .freq = 450000, .vco = 8100 },
5607 { .freq = 540000, .vco = 8100 },
5608 { .freq = 617140, .vco = 8640 },
5609 { .freq = 675000, .vco = 8100 },
5610};
5611
5612static unsigned int skl_cdclk_decimal(unsigned int freq)
5613{
5614 return (freq - 1000) / 500;
5615}
5616
5617static unsigned int skl_cdclk_get_vco(unsigned int freq)
5618{
5619 unsigned int i;
5620
5621 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5622 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5623
5624 if (e->freq == freq)
5625 return e->vco;
5626 }
5627
5628 return 8100;
5629}
5630
5631static void
5632skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5633{
5634 unsigned int min_freq;
5635 u32 val;
5636
5637 /* select the minimum CDCLK before enabling DPLL 0 */
5638 val = I915_READ(CDCLK_CTL);
5639 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5640 val |= CDCLK_FREQ_337_308;
5641
5642 if (required_vco == 8640)
5643 min_freq = 308570;
5644 else
5645 min_freq = 337500;
5646
5647 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5648
5649 I915_WRITE(CDCLK_CTL, val);
5650 POSTING_READ(CDCLK_CTL);
5651
5652 /*
5653 * We always enable DPLL0 with the lowest link rate possible, but still
5654 * taking into account the VCO required to operate the eDP panel at the
5655 * desired frequency. The usual DP link rates operate with a VCO of
5656 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5657 * The modeset code is responsible for the selection of the exact link
5658 * rate later on, with the constraint of choosing a frequency that
5659 * works with required_vco.
5660 */
5661 val = I915_READ(DPLL_CTRL1);
5662
5663 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5664 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5665 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5666 if (required_vco == 8640)
5667 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5668 SKL_DPLL0);
5669 else
5670 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5671 SKL_DPLL0);
5672
5673 I915_WRITE(DPLL_CTRL1, val);
5674 POSTING_READ(DPLL_CTRL1);
5675
5676 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5677
5678 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5679 DRM_ERROR("DPLL0 not locked\n");
5680}
5681
5682static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5683{
5684 int ret;
5685 u32 val;
5686
5687 /* inform PCU we want to change CDCLK */
5688 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5689 mutex_lock(&dev_priv->rps.hw_lock);
5690 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5691 mutex_unlock(&dev_priv->rps.hw_lock);
5692
5693 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5694}
5695
5696static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5697{
5698 unsigned int i;
5699
5700 for (i = 0; i < 15; i++) {
5701 if (skl_cdclk_pcu_ready(dev_priv))
5702 return true;
5703 udelay(10);
5704 }
5705
5706 return false;
5707}
5708
5709static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5710{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005711 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005712 u32 freq_select, pcu_ack;
5713
5714 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5715
5716 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5717 DRM_ERROR("failed to inform PCU about cdclk change\n");
5718 return;
5719 }
5720
5721 /* set CDCLK_CTL */
5722 switch(freq) {
5723 case 450000:
5724 case 432000:
5725 freq_select = CDCLK_FREQ_450_432;
5726 pcu_ack = 1;
5727 break;
5728 case 540000:
5729 freq_select = CDCLK_FREQ_540;
5730 pcu_ack = 2;
5731 break;
5732 case 308570:
5733 case 337500:
5734 default:
5735 freq_select = CDCLK_FREQ_337_308;
5736 pcu_ack = 0;
5737 break;
5738 case 617140:
5739 case 675000:
5740 freq_select = CDCLK_FREQ_675_617;
5741 pcu_ack = 3;
5742 break;
5743 }
5744
5745 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5746 POSTING_READ(CDCLK_CTL);
5747
5748 /* inform PCU of the change */
5749 mutex_lock(&dev_priv->rps.hw_lock);
5750 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5751 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005752
5753 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005754}
5755
5756void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5757{
5758 /* disable DBUF power */
5759 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5760 POSTING_READ(DBUF_CTL);
5761
5762 udelay(10);
5763
5764 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5765 DRM_ERROR("DBuf power disable timeout\n");
5766
5767 /* disable DPLL0 */
5768 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5769 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5770 DRM_ERROR("Couldn't disable DPLL0\n");
5771
5772 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5773}
5774
5775void skl_init_cdclk(struct drm_i915_private *dev_priv)
5776{
5777 u32 val;
5778 unsigned int required_vco;
5779
5780 /* enable PCH reset handshake */
5781 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5782 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5783
5784 /* enable PG1 and Misc I/O */
5785 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5786
5787 /* DPLL0 already enabed !? */
5788 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5789 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5790 return;
5791 }
5792
5793 /* enable DPLL0 */
5794 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5795 skl_dpll0_enable(dev_priv, required_vco);
5796
5797 /* set CDCLK to the frequency the BIOS chose */
5798 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5799
5800 /* enable DBUF power */
5801 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5802 POSTING_READ(DBUF_CTL);
5803
5804 udelay(10);
5805
5806 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5807 DRM_ERROR("DBuf power enable timeout\n");
5808}
5809
Ville Syrjälädfcab172014-06-13 13:37:47 +03005810/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005811static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005812{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005813 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005814
Jesse Barnes586f49d2013-11-04 16:06:59 -08005815 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005816 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005817 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5818 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005819 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005820
Ville Syrjälädfcab172014-06-13 13:37:47 +03005821 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005822}
5823
5824/* Adjust CDclk dividers to allow high res or save power if possible */
5825static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5826{
5827 struct drm_i915_private *dev_priv = dev->dev_private;
5828 u32 val, cmd;
5829
Vandana Kannan164dfd22014-11-24 13:37:41 +05305830 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5831 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005832
Ville Syrjälädfcab172014-06-13 13:37:47 +03005833 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005834 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005835 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005836 cmd = 1;
5837 else
5838 cmd = 0;
5839
5840 mutex_lock(&dev_priv->rps.hw_lock);
5841 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5842 val &= ~DSPFREQGUAR_MASK;
5843 val |= (cmd << DSPFREQGUAR_SHIFT);
5844 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5845 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5846 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5847 50)) {
5848 DRM_ERROR("timed out waiting for CDclk change\n");
5849 }
5850 mutex_unlock(&dev_priv->rps.hw_lock);
5851
Ville Syrjälä54433e92015-05-26 20:42:31 +03005852 mutex_lock(&dev_priv->sb_lock);
5853
Ville Syrjälädfcab172014-06-13 13:37:47 +03005854 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005855 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005856
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005857 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005858
Jesse Barnes30a970c2013-11-04 13:48:12 -08005859 /* adjust cdclk divider */
5860 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005861 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862 val |= divider;
5863 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005864
5865 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5866 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5867 50))
5868 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005869 }
5870
Jesse Barnes30a970c2013-11-04 13:48:12 -08005871 /* adjust self-refresh exit latency value */
5872 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5873 val &= ~0x7f;
5874
5875 /*
5876 * For high bandwidth configs, we set a higher latency in the bunit
5877 * so that the core display fetch happens in time to avoid underruns.
5878 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005879 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 val |= 4500 / 250; /* 4.5 usec */
5881 else
5882 val |= 3000 / 250; /* 3.0 usec */
5883 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005884
Ville Syrjäläa5805162015-05-26 20:42:30 +03005885 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005886
Ville Syrjäläb6283052015-06-03 15:45:07 +03005887 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005888}
5889
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005890static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5891{
5892 struct drm_i915_private *dev_priv = dev->dev_private;
5893 u32 val, cmd;
5894
Vandana Kannan164dfd22014-11-24 13:37:41 +05305895 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5896 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005897
5898 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005899 case 333333:
5900 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005901 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005902 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005903 break;
5904 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005905 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005906 return;
5907 }
5908
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005909 /*
5910 * Specs are full of misinformation, but testing on actual
5911 * hardware has shown that we just need to write the desired
5912 * CCK divider into the Punit register.
5913 */
5914 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5915
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005916 mutex_lock(&dev_priv->rps.hw_lock);
5917 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5918 val &= ~DSPFREQGUAR_MASK_CHV;
5919 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5920 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5921 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5922 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5923 50)) {
5924 DRM_ERROR("timed out waiting for CDclk change\n");
5925 }
5926 mutex_unlock(&dev_priv->rps.hw_lock);
5927
Ville Syrjäläb6283052015-06-03 15:45:07 +03005928 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005929}
5930
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5932 int max_pixclk)
5933{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005934 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005935 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005936
Jesse Barnes30a970c2013-11-04 13:48:12 -08005937 /*
5938 * Really only a few cases to deal with, as only 4 CDclks are supported:
5939 * 200MHz
5940 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005941 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005942 * 400MHz (VLV only)
5943 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5944 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005945 *
5946 * We seem to get an unstable or solid color picture at 200MHz.
5947 * Not sure what's wrong. For now use 200MHz only when all pipes
5948 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005950 if (!IS_CHERRYVIEW(dev_priv) &&
5951 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005952 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005953 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005954 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005955 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005956 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005957 else
5958 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005959}
5960
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305961static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5962 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005963{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305964 /*
5965 * FIXME:
5966 * - remove the guardband, it's not needed on BXT
5967 * - set 19.2MHz bypass frequency if there are no active pipes
5968 */
5969 if (max_pixclk > 576000*9/10)
5970 return 624000;
5971 else if (max_pixclk > 384000*9/10)
5972 return 576000;
5973 else if (max_pixclk > 288000*9/10)
5974 return 384000;
5975 else if (max_pixclk > 144000*9/10)
5976 return 288000;
5977 else
5978 return 144000;
5979}
5980
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005981/* Compute the max pixel clock for new configuration. Uses atomic state if
5982 * that's non-NULL, look at current state otherwise. */
5983static int intel_mode_max_pixclk(struct drm_device *dev,
5984 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005985{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005986 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005987 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005988 int max_pixclk = 0;
5989
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005990 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005991 if (state)
5992 crtc_state =
5993 intel_atomic_get_crtc_state(state, intel_crtc);
5994 else
5995 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005996 if (IS_ERR(crtc_state))
5997 return PTR_ERR(crtc_state);
5998
5999 if (!crtc_state->base.enable)
6000 continue;
6001
6002 max_pixclk = max(max_pixclk,
6003 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006004 }
6005
6006 return max_pixclk;
6007}
6008
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03006009static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006010{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006011 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03006012 struct drm_crtc *crtc;
6013 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006014 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03006015 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006016
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006017 if (max_pixclk < 0)
6018 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006019
Vandana Kannanf8437dd12014-11-24 13:37:39 +05306020 if (IS_VALLEYVIEW(dev_priv))
6021 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
6022 else
6023 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
6024
6025 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006026 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006027
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03006028 /* add all active pipes to the state */
6029 for_each_crtc(state->dev, crtc) {
6030 if (!crtc->state->enable)
6031 continue;
6032
6033 crtc_state = drm_atomic_get_crtc_state(state, crtc);
6034 if (IS_ERR(crtc_state))
6035 return PTR_ERR(crtc_state);
6036 }
6037
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02006038 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03006039 for_each_crtc_in_state(state, crtc, crtc_state, i)
6040 if (crtc_state->enable)
6041 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006042
6043 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006044}
6045
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006046static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6047{
6048 unsigned int credits, default_credits;
6049
6050 if (IS_CHERRYVIEW(dev_priv))
6051 default_credits = PFI_CREDIT(12);
6052 else
6053 default_credits = PFI_CREDIT(8);
6054
Vandana Kannan164dfd22014-11-24 13:37:41 +05306055 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006056 /* CHV suggested value is 31 or 63 */
6057 if (IS_CHERRYVIEW(dev_priv))
6058 credits = PFI_CREDIT_31;
6059 else
6060 credits = PFI_CREDIT(15);
6061 } else {
6062 credits = default_credits;
6063 }
6064
6065 /*
6066 * WA - write default credits before re-programming
6067 * FIXME: should we also set the resend bit here?
6068 */
6069 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6070 default_credits);
6071
6072 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6073 credits | PFI_CREDIT_RESEND);
6074
6075 /*
6076 * FIXME is this guaranteed to clear
6077 * immediately or should we poll for it?
6078 */
6079 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6080}
6081
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006082static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006083{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006084 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006085 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006086 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006087 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006088
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006089 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
6090 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006091 if (WARN_ON(max_pixclk < 0))
6092 return;
6093
6094 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006095
Vandana Kannan164dfd22014-11-24 13:37:41 +05306096 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02006097 /*
6098 * FIXME: We can end up here with all power domains off, yet
6099 * with a CDCLK frequency other than the minimum. To account
6100 * for this take the PIPE-A power domain, which covers the HW
6101 * blocks needed for the following programming. This can be
6102 * removed once it's guaranteed that we get here either with
6103 * the minimum CDCLK set, or the required power domains
6104 * enabled.
6105 */
6106 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6107
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006108 if (IS_CHERRYVIEW(dev))
6109 cherryview_set_cdclk(dev, req_cdclk);
6110 else
6111 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006112
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006113 vlv_program_pfi_credits(dev_priv);
6114
Imre Deak738c05c2014-11-19 16:25:37 +02006115 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006116 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006117}
6118
Jesse Barnes89b667f2013-04-18 14:51:36 -07006119static void valleyview_crtc_enable(struct drm_crtc *crtc)
6120{
6121 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006122 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006123 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6124 struct intel_encoder *encoder;
6125 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006126 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006127
Matt Roper83d65732015-02-25 13:12:16 -08006128 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006129
6130 if (intel_crtc->active)
6131 return;
6132
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006133 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306134
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006135 if (!is_dsi) {
6136 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006137 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006138 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006139 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006140 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006141
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006142 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306143 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006144
6145 intel_set_pipe_timings(intel_crtc);
6146
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006147 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6148 struct drm_i915_private *dev_priv = dev->dev_private;
6149
6150 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6151 I915_WRITE(CHV_CANVAS(pipe), 0);
6152 }
6153
Daniel Vetter5b18e572014-04-24 23:55:06 +02006154 i9xx_set_pipeconf(intel_crtc);
6155
Jesse Barnes89b667f2013-04-18 14:51:36 -07006156 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006157
Daniel Vettera72e4c92014-09-30 10:56:47 +02006158 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006159
Jesse Barnes89b667f2013-04-18 14:51:36 -07006160 for_each_encoder_on_crtc(dev, crtc, encoder)
6161 if (encoder->pre_pll_enable)
6162 encoder->pre_pll_enable(encoder);
6163
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006164 if (!is_dsi) {
6165 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006166 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006167 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006168 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006169 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006170
6171 for_each_encoder_on_crtc(dev, crtc, encoder)
6172 if (encoder->pre_enable)
6173 encoder->pre_enable(encoder);
6174
Jesse Barnes2dd24552013-04-25 12:55:01 -07006175 i9xx_pfit_enable(intel_crtc);
6176
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006177 intel_crtc_load_lut(crtc);
6178
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006179 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006180 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006181
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006182 assert_vblank_disabled(crtc);
6183 drm_crtc_vblank_on(crtc);
6184
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006185 for_each_encoder_on_crtc(dev, crtc, encoder)
6186 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006187}
6188
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006189static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6190{
6191 struct drm_device *dev = crtc->base.dev;
6192 struct drm_i915_private *dev_priv = dev->dev_private;
6193
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006194 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6195 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006196}
6197
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006198static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006199{
6200 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006201 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006202 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006203 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006204 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006205
Matt Roper83d65732015-02-25 13:12:16 -08006206 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02006207
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006208 if (intel_crtc->active)
6209 return;
6210
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006211 i9xx_set_pll_dividers(intel_crtc);
6212
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006213 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306214 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006215
6216 intel_set_pipe_timings(intel_crtc);
6217
Daniel Vetter5b18e572014-04-24 23:55:06 +02006218 i9xx_set_pipeconf(intel_crtc);
6219
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006220 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006221
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006222 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006223 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006224
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006225 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006226 if (encoder->pre_enable)
6227 encoder->pre_enable(encoder);
6228
Daniel Vetterf6736a12013-06-05 13:34:30 +02006229 i9xx_enable_pll(intel_crtc);
6230
Jesse Barnes2dd24552013-04-25 12:55:01 -07006231 i9xx_pfit_enable(intel_crtc);
6232
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006233 intel_crtc_load_lut(crtc);
6234
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006235 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006236 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006237
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006238 assert_vblank_disabled(crtc);
6239 drm_crtc_vblank_on(crtc);
6240
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006241 for_each_encoder_on_crtc(dev, crtc, encoder)
6242 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006243}
6244
Daniel Vetter87476d62013-04-11 16:29:06 +02006245static void i9xx_pfit_disable(struct intel_crtc *crtc)
6246{
6247 struct drm_device *dev = crtc->base.dev;
6248 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006249
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006250 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006251 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006252
6253 assert_pipe_disabled(dev_priv, crtc->pipe);
6254
Daniel Vetter328d8e82013-05-08 10:36:31 +02006255 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6256 I915_READ(PFIT_CONTROL));
6257 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006258}
6259
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006260static void i9xx_crtc_disable(struct drm_crtc *crtc)
6261{
6262 struct drm_device *dev = crtc->dev;
6263 struct drm_i915_private *dev_priv = dev->dev_private;
6264 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006265 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006266 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006267
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006268 if (!intel_crtc->active)
6269 return;
6270
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006271 /*
6272 * On gen2 planes are double buffered but the pipe isn't, so we must
6273 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006274 * We also need to wait on all gmch platforms because of the
6275 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006276 */
Imre Deak564ed192014-06-13 14:54:21 +03006277 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006278
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006279 for_each_encoder_on_crtc(dev, crtc, encoder)
6280 encoder->disable(encoder);
6281
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006282 drm_crtc_vblank_off(crtc);
6283 assert_vblank_disabled(crtc);
6284
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006285 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006286
Daniel Vetter87476d62013-04-11 16:29:06 +02006287 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006288
Jesse Barnes89b667f2013-04-18 14:51:36 -07006289 for_each_encoder_on_crtc(dev, crtc, encoder)
6290 if (encoder->post_disable)
6291 encoder->post_disable(encoder);
6292
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006293 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006294 if (IS_CHERRYVIEW(dev))
6295 chv_disable_pll(dev_priv, pipe);
6296 else if (IS_VALLEYVIEW(dev))
6297 vlv_disable_pll(dev_priv, pipe);
6298 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006299 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006300 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006301
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006302 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006303 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006304
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006305 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006306 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006307
Daniel Vetterefa96242014-04-24 23:55:02 +02006308 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006309 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006310 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006311}
6312
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006313static void i9xx_crtc_off(struct drm_crtc *crtc)
6314{
6315}
6316
Borun Fub04c5bd2014-07-12 10:02:27 +05306317/* Master function to enable/disable CRTC and corresponding power wells */
6318void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006319{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006320 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006321 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006322 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006323 enum intel_display_power_domain domain;
6324 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006325
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006326 if (enable) {
6327 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006328 domains = get_crtc_power_domains(crtc);
6329 for_each_power_domain(domain, domains)
6330 intel_display_power_get(dev_priv, domain);
6331 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006332
6333 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006334 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006335 }
6336 } else {
6337 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006338 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006339 dev_priv->display.crtc_disable(crtc);
6340
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006341 domains = intel_crtc->enabled_power_domains;
6342 for_each_power_domain(domain, domains)
6343 intel_display_power_put(dev_priv, domain);
6344 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006345 }
6346 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306347}
6348
6349/**
6350 * Sets the power management mode of the pipe and plane.
6351 */
6352void intel_crtc_update_dpms(struct drm_crtc *crtc)
6353{
6354 struct drm_device *dev = crtc->dev;
6355 struct intel_encoder *intel_encoder;
6356 bool enable = false;
6357
6358 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6359 enable |= intel_encoder->connectors_active;
6360
6361 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006362
6363 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006364}
6365
Daniel Vetter976f8a22012-07-08 22:34:21 +02006366static void intel_crtc_disable(struct drm_crtc *crtc)
6367{
6368 struct drm_device *dev = crtc->dev;
6369 struct drm_connector *connector;
6370 struct drm_i915_private *dev_priv = dev->dev_private;
6371
6372 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006373 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006374
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006375 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006376 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006377 dev_priv->display.off(crtc);
6378
Matt Roper70a101f2015-04-08 18:56:53 -07006379 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006380
6381 /* Update computed state. */
6382 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6383 if (!connector->encoder || !connector->encoder->crtc)
6384 continue;
6385
6386 if (connector->encoder->crtc != crtc)
6387 continue;
6388
6389 connector->dpms = DRM_MODE_DPMS_OFF;
6390 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006391 }
6392}
6393
Chris Wilsonea5b2132010-08-04 13:50:23 +01006394void intel_encoder_destroy(struct drm_encoder *encoder)
6395{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006396 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006397
Chris Wilsonea5b2132010-08-04 13:50:23 +01006398 drm_encoder_cleanup(encoder);
6399 kfree(intel_encoder);
6400}
6401
Damien Lespiau92373292013-08-08 22:28:57 +01006402/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006403 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6404 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006405static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006406{
6407 if (mode == DRM_MODE_DPMS_ON) {
6408 encoder->connectors_active = true;
6409
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006410 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006411 } else {
6412 encoder->connectors_active = false;
6413
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006414 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006415 }
6416}
6417
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006418/* Cross check the actual hw state with our own modeset state tracking (and it's
6419 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006420static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006421{
6422 if (connector->get_hw_state(connector)) {
6423 struct intel_encoder *encoder = connector->encoder;
6424 struct drm_crtc *crtc;
6425 bool encoder_enabled;
6426 enum pipe pipe;
6427
6428 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6429 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006430 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006431
Dave Airlie0e32b392014-05-02 14:02:48 +10006432 /* there is no real hw state for MST connectors */
6433 if (connector->mst_port)
6434 return;
6435
Rob Clarke2c719b2014-12-15 13:56:32 -05006436 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006437 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006438 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006439 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006440
Dave Airlie36cd7442014-05-02 13:44:18 +10006441 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006442 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006443 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006444
Dave Airlie36cd7442014-05-02 13:44:18 +10006445 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006446 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6447 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006448 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006449
Dave Airlie36cd7442014-05-02 13:44:18 +10006450 crtc = encoder->base.crtc;
6451
Matt Roper83d65732015-02-25 13:12:16 -08006452 I915_STATE_WARN(!crtc->state->enable,
6453 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006454 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6455 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006456 "encoder active on the wrong pipe\n");
6457 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006458 }
6459}
6460
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006461int intel_connector_init(struct intel_connector *connector)
6462{
6463 struct drm_connector_state *connector_state;
6464
6465 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6466 if (!connector_state)
6467 return -ENOMEM;
6468
6469 connector->base.state = connector_state;
6470 return 0;
6471}
6472
6473struct intel_connector *intel_connector_alloc(void)
6474{
6475 struct intel_connector *connector;
6476
6477 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6478 if (!connector)
6479 return NULL;
6480
6481 if (intel_connector_init(connector) < 0) {
6482 kfree(connector);
6483 return NULL;
6484 }
6485
6486 return connector;
6487}
6488
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006489/* Even simpler default implementation, if there's really no special case to
6490 * consider. */
6491void intel_connector_dpms(struct drm_connector *connector, int mode)
6492{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006493 /* All the simple cases only support two dpms states. */
6494 if (mode != DRM_MODE_DPMS_ON)
6495 mode = DRM_MODE_DPMS_OFF;
6496
6497 if (mode == connector->dpms)
6498 return;
6499
6500 connector->dpms = mode;
6501
6502 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006503 if (connector->encoder)
6504 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006505
Daniel Vetterb9805142012-08-31 17:37:33 +02006506 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006507}
6508
Daniel Vetterf0947c32012-07-02 13:10:34 +02006509/* Simple connector->get_hw_state implementation for encoders that support only
6510 * one connector and no cloning and hence the encoder state determines the state
6511 * of the connector. */
6512bool intel_connector_get_hw_state(struct intel_connector *connector)
6513{
Daniel Vetter24929352012-07-02 20:28:59 +02006514 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006515 struct intel_encoder *encoder = connector->encoder;
6516
6517 return encoder->get_hw_state(encoder, &pipe);
6518}
6519
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006520static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006521{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006522 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6523 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006524
6525 return 0;
6526}
6527
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006528static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006529 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006530{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006531 struct drm_atomic_state *state = pipe_config->base.state;
6532 struct intel_crtc *other_crtc;
6533 struct intel_crtc_state *other_crtc_state;
6534
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006535 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6536 pipe_name(pipe), pipe_config->fdi_lanes);
6537 if (pipe_config->fdi_lanes > 4) {
6538 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6539 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006540 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006541 }
6542
Paulo Zanonibafb6552013-11-02 21:07:44 -07006543 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006544 if (pipe_config->fdi_lanes > 2) {
6545 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6546 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006547 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006548 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006549 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006550 }
6551 }
6552
6553 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006554 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006555
6556 /* Ivybridge 3 pipe is really complicated */
6557 switch (pipe) {
6558 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006559 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006560 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006561 if (pipe_config->fdi_lanes <= 2)
6562 return 0;
6563
6564 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6565 other_crtc_state =
6566 intel_atomic_get_crtc_state(state, other_crtc);
6567 if (IS_ERR(other_crtc_state))
6568 return PTR_ERR(other_crtc_state);
6569
6570 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006571 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6572 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006573 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006574 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006575 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006576 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006577 if (pipe_config->fdi_lanes > 2) {
6578 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6579 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006580 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006581 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006582
6583 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6584 other_crtc_state =
6585 intel_atomic_get_crtc_state(state, other_crtc);
6586 if (IS_ERR(other_crtc_state))
6587 return PTR_ERR(other_crtc_state);
6588
6589 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006590 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006591 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006592 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006593 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006594 default:
6595 BUG();
6596 }
6597}
6598
Daniel Vettere29c22c2013-02-21 00:00:16 +01006599#define RETRY 1
6600static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006601 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006602{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006603 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006604 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006605 int lane, link_bw, fdi_dotclock, ret;
6606 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006607
Daniel Vettere29c22c2013-02-21 00:00:16 +01006608retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006609 /* FDI is a binary signal running at ~2.7GHz, encoding
6610 * each output octet as 10 bits. The actual frequency
6611 * is stored as a divider into a 100MHz clock, and the
6612 * mode pixel clock is stored in units of 1KHz.
6613 * Hence the bw of each lane in terms of the mode signal
6614 * is:
6615 */
6616 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6617
Damien Lespiau241bfc32013-09-25 16:45:37 +01006618 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006619
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006620 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006621 pipe_config->pipe_bpp);
6622
6623 pipe_config->fdi_lanes = lane;
6624
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006625 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006626 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006627
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006628 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6629 intel_crtc->pipe, pipe_config);
6630 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006631 pipe_config->pipe_bpp -= 2*3;
6632 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6633 pipe_config->pipe_bpp);
6634 needs_recompute = true;
6635 pipe_config->bw_constrained = true;
6636
6637 goto retry;
6638 }
6639
6640 if (needs_recompute)
6641 return RETRY;
6642
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006643 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006644}
6645
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006646static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6647 struct intel_crtc_state *pipe_config)
6648{
6649 if (pipe_config->pipe_bpp > 24)
6650 return false;
6651
6652 /* HSW can handle pixel rate up to cdclk? */
6653 if (IS_HASWELL(dev_priv->dev))
6654 return true;
6655
6656 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006657 * We compare against max which means we must take
6658 * the increased cdclk requirement into account when
6659 * calculating the new cdclk.
6660 *
6661 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006662 */
6663 return ilk_pipe_pixel_rate(pipe_config) <=
6664 dev_priv->max_cdclk_freq * 95 / 100;
6665}
6666
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006667static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006668 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006669{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006670 struct drm_device *dev = crtc->base.dev;
6671 struct drm_i915_private *dev_priv = dev->dev_private;
6672
Jani Nikulad330a952014-01-21 11:24:25 +02006673 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006674 hsw_crtc_supports_ips(crtc) &&
6675 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006676}
6677
Daniel Vettera43f6e02013-06-07 23:10:32 +02006678static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006679 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006680{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006681 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006682 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006683 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006684 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006685
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006686 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006687 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006688 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006689
6690 /*
6691 * Enable pixel doubling when the dot clock
6692 * is > 90% of the (display) core speed.
6693 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006694 * GDG double wide on either pipe,
6695 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006696 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006697 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006698 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006699 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006700 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006701 }
6702
Damien Lespiau241bfc32013-09-25 16:45:37 +01006703 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006704 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006705 }
Chris Wilson89749352010-09-12 18:25:19 +01006706
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006707 /*
6708 * Pipe horizontal size must be even in:
6709 * - DVO ganged mode
6710 * - LVDS dual channel mode
6711 * - Double wide pipe
6712 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006713 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006714 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6715 pipe_config->pipe_src_w &= ~1;
6716
Damien Lespiau8693a822013-05-03 18:48:11 +01006717 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6718 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006719 */
6720 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6721 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006722 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006723
Damien Lespiauf5adf942013-06-24 18:29:34 +01006724 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006725 hsw_compute_ips_config(crtc, pipe_config);
6726
Daniel Vetter877d48d2013-04-19 11:24:43 +02006727 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006728 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006729
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006730 /* FIXME: remove below call once atomic mode set is place and all crtc
6731 * related checks called from atomic_crtc_check function */
6732 ret = 0;
6733 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6734 crtc, pipe_config->base.state);
6735 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6736
6737 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006738}
6739
Ville Syrjälä1652d192015-03-31 14:12:01 +03006740static int skylake_get_display_clock_speed(struct drm_device *dev)
6741{
6742 struct drm_i915_private *dev_priv = to_i915(dev);
6743 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6744 uint32_t cdctl = I915_READ(CDCLK_CTL);
6745 uint32_t linkrate;
6746
Damien Lespiau414355a2015-06-04 18:21:31 +01006747 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006748 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006749
6750 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6751 return 540000;
6752
6753 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006754 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006755
Damien Lespiau71cd8422015-04-30 16:39:17 +01006756 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6757 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006758 /* vco 8640 */
6759 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6760 case CDCLK_FREQ_450_432:
6761 return 432000;
6762 case CDCLK_FREQ_337_308:
6763 return 308570;
6764 case CDCLK_FREQ_675_617:
6765 return 617140;
6766 default:
6767 WARN(1, "Unknown cd freq selection\n");
6768 }
6769 } else {
6770 /* vco 8100 */
6771 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6772 case CDCLK_FREQ_450_432:
6773 return 450000;
6774 case CDCLK_FREQ_337_308:
6775 return 337500;
6776 case CDCLK_FREQ_675_617:
6777 return 675000;
6778 default:
6779 WARN(1, "Unknown cd freq selection\n");
6780 }
6781 }
6782
6783 /* error case, do as if DPLL0 isn't enabled */
6784 return 24000;
6785}
6786
6787static int broadwell_get_display_clock_speed(struct drm_device *dev)
6788{
6789 struct drm_i915_private *dev_priv = dev->dev_private;
6790 uint32_t lcpll = I915_READ(LCPLL_CTL);
6791 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6792
6793 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6794 return 800000;
6795 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6796 return 450000;
6797 else if (freq == LCPLL_CLK_FREQ_450)
6798 return 450000;
6799 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6800 return 540000;
6801 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6802 return 337500;
6803 else
6804 return 675000;
6805}
6806
6807static int haswell_get_display_clock_speed(struct drm_device *dev)
6808{
6809 struct drm_i915_private *dev_priv = dev->dev_private;
6810 uint32_t lcpll = I915_READ(LCPLL_CTL);
6811 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6812
6813 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6814 return 800000;
6815 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6816 return 450000;
6817 else if (freq == LCPLL_CLK_FREQ_450)
6818 return 450000;
6819 else if (IS_HSW_ULT(dev))
6820 return 337500;
6821 else
6822 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006823}
6824
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006825static int valleyview_get_display_clock_speed(struct drm_device *dev)
6826{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006827 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006828 u32 val;
6829 int divider;
6830
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006831 if (dev_priv->hpll_freq == 0)
6832 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6833
Ville Syrjäläa5805162015-05-26 20:42:30 +03006834 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006835 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006836 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006837
6838 divider = val & DISPLAY_FREQUENCY_VALUES;
6839
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006840 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6841 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6842 "cdclk change in progress\n");
6843
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006844 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006845}
6846
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006847static int ilk_get_display_clock_speed(struct drm_device *dev)
6848{
6849 return 450000;
6850}
6851
Jesse Barnese70236a2009-09-21 10:42:27 -07006852static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006853{
Jesse Barnese70236a2009-09-21 10:42:27 -07006854 return 400000;
6855}
Jesse Barnes79e53942008-11-07 14:24:08 -08006856
Jesse Barnese70236a2009-09-21 10:42:27 -07006857static int i915_get_display_clock_speed(struct drm_device *dev)
6858{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006859 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006860}
Jesse Barnes79e53942008-11-07 14:24:08 -08006861
Jesse Barnese70236a2009-09-21 10:42:27 -07006862static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6863{
6864 return 200000;
6865}
Jesse Barnes79e53942008-11-07 14:24:08 -08006866
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006867static int pnv_get_display_clock_speed(struct drm_device *dev)
6868{
6869 u16 gcfgc = 0;
6870
6871 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6872
6873 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6874 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006875 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006876 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006877 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006878 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006879 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006880 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6881 return 200000;
6882 default:
6883 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6884 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006885 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006886 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006887 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006888 }
6889}
6890
Jesse Barnese70236a2009-09-21 10:42:27 -07006891static int i915gm_get_display_clock_speed(struct drm_device *dev)
6892{
6893 u16 gcfgc = 0;
6894
6895 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6896
6897 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006898 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006899 else {
6900 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6901 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006902 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006903 default:
6904 case GC_DISPLAY_CLOCK_190_200_MHZ:
6905 return 190000;
6906 }
6907 }
6908}
Jesse Barnes79e53942008-11-07 14:24:08 -08006909
Jesse Barnese70236a2009-09-21 10:42:27 -07006910static int i865_get_display_clock_speed(struct drm_device *dev)
6911{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006912 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006913}
6914
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006915static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006916{
6917 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006918
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006919 /*
6920 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6921 * encoding is different :(
6922 * FIXME is this the right way to detect 852GM/852GMV?
6923 */
6924 if (dev->pdev->revision == 0x1)
6925 return 133333;
6926
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006927 pci_bus_read_config_word(dev->pdev->bus,
6928 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6929
Jesse Barnese70236a2009-09-21 10:42:27 -07006930 /* Assume that the hardware is in the high speed state. This
6931 * should be the default.
6932 */
6933 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6934 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006935 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006936 case GC_CLOCK_100_200:
6937 return 200000;
6938 case GC_CLOCK_166_250:
6939 return 250000;
6940 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006941 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006942 case GC_CLOCK_133_266:
6943 case GC_CLOCK_133_266_2:
6944 case GC_CLOCK_166_266:
6945 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006946 }
6947
6948 /* Shouldn't happen */
6949 return 0;
6950}
6951
6952static int i830_get_display_clock_speed(struct drm_device *dev)
6953{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006954 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006955}
6956
Ville Syrjälä34edce22015-05-22 11:22:33 +03006957static unsigned int intel_hpll_vco(struct drm_device *dev)
6958{
6959 struct drm_i915_private *dev_priv = dev->dev_private;
6960 static const unsigned int blb_vco[8] = {
6961 [0] = 3200000,
6962 [1] = 4000000,
6963 [2] = 5333333,
6964 [3] = 4800000,
6965 [4] = 6400000,
6966 };
6967 static const unsigned int pnv_vco[8] = {
6968 [0] = 3200000,
6969 [1] = 4000000,
6970 [2] = 5333333,
6971 [3] = 4800000,
6972 [4] = 2666667,
6973 };
6974 static const unsigned int cl_vco[8] = {
6975 [0] = 3200000,
6976 [1] = 4000000,
6977 [2] = 5333333,
6978 [3] = 6400000,
6979 [4] = 3333333,
6980 [5] = 3566667,
6981 [6] = 4266667,
6982 };
6983 static const unsigned int elk_vco[8] = {
6984 [0] = 3200000,
6985 [1] = 4000000,
6986 [2] = 5333333,
6987 [3] = 4800000,
6988 };
6989 static const unsigned int ctg_vco[8] = {
6990 [0] = 3200000,
6991 [1] = 4000000,
6992 [2] = 5333333,
6993 [3] = 6400000,
6994 [4] = 2666667,
6995 [5] = 4266667,
6996 };
6997 const unsigned int *vco_table;
6998 unsigned int vco;
6999 uint8_t tmp = 0;
7000
7001 /* FIXME other chipsets? */
7002 if (IS_GM45(dev))
7003 vco_table = ctg_vco;
7004 else if (IS_G4X(dev))
7005 vco_table = elk_vco;
7006 else if (IS_CRESTLINE(dev))
7007 vco_table = cl_vco;
7008 else if (IS_PINEVIEW(dev))
7009 vco_table = pnv_vco;
7010 else if (IS_G33(dev))
7011 vco_table = blb_vco;
7012 else
7013 return 0;
7014
7015 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
7016
7017 vco = vco_table[tmp & 0x7];
7018 if (vco == 0)
7019 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
7020 else
7021 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
7022
7023 return vco;
7024}
7025
7026static int gm45_get_display_clock_speed(struct drm_device *dev)
7027{
7028 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7029 uint16_t tmp = 0;
7030
7031 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7032
7033 cdclk_sel = (tmp >> 12) & 0x1;
7034
7035 switch (vco) {
7036 case 2666667:
7037 case 4000000:
7038 case 5333333:
7039 return cdclk_sel ? 333333 : 222222;
7040 case 3200000:
7041 return cdclk_sel ? 320000 : 228571;
7042 default:
7043 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
7044 return 222222;
7045 }
7046}
7047
7048static int i965gm_get_display_clock_speed(struct drm_device *dev)
7049{
7050 static const uint8_t div_3200[] = { 16, 10, 8 };
7051 static const uint8_t div_4000[] = { 20, 12, 10 };
7052 static const uint8_t div_5333[] = { 24, 16, 14 };
7053 const uint8_t *div_table;
7054 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7055 uint16_t tmp = 0;
7056
7057 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7058
7059 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7060
7061 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7062 goto fail;
7063
7064 switch (vco) {
7065 case 3200000:
7066 div_table = div_3200;
7067 break;
7068 case 4000000:
7069 div_table = div_4000;
7070 break;
7071 case 5333333:
7072 div_table = div_5333;
7073 break;
7074 default:
7075 goto fail;
7076 }
7077
7078 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7079
7080 fail:
7081 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7082 return 200000;
7083}
7084
7085static int g33_get_display_clock_speed(struct drm_device *dev)
7086{
7087 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7088 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7089 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7090 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7091 const uint8_t *div_table;
7092 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7093 uint16_t tmp = 0;
7094
7095 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7096
7097 cdclk_sel = (tmp >> 4) & 0x7;
7098
7099 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7100 goto fail;
7101
7102 switch (vco) {
7103 case 3200000:
7104 div_table = div_3200;
7105 break;
7106 case 4000000:
7107 div_table = div_4000;
7108 break;
7109 case 4800000:
7110 div_table = div_4800;
7111 break;
7112 case 5333333:
7113 div_table = div_5333;
7114 break;
7115 default:
7116 goto fail;
7117 }
7118
7119 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7120
7121 fail:
7122 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7123 return 190476;
7124}
7125
Zhenyu Wang2c072452009-06-05 15:38:42 +08007126static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007127intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007128{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007129 while (*num > DATA_LINK_M_N_MASK ||
7130 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007131 *num >>= 1;
7132 *den >>= 1;
7133 }
7134}
7135
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007136static void compute_m_n(unsigned int m, unsigned int n,
7137 uint32_t *ret_m, uint32_t *ret_n)
7138{
7139 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7140 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7141 intel_reduce_m_n_ratio(ret_m, ret_n);
7142}
7143
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007144void
7145intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7146 int pixel_clock, int link_clock,
7147 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007148{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007149 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007150
7151 compute_m_n(bits_per_pixel * pixel_clock,
7152 link_clock * nlanes * 8,
7153 &m_n->gmch_m, &m_n->gmch_n);
7154
7155 compute_m_n(pixel_clock, link_clock,
7156 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007157}
7158
Chris Wilsona7615032011-01-12 17:04:08 +00007159static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7160{
Jani Nikulad330a952014-01-21 11:24:25 +02007161 if (i915.panel_use_ssc >= 0)
7162 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007163 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007164 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007165}
7166
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007167static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7168 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007169{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007170 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007171 struct drm_i915_private *dev_priv = dev->dev_private;
7172 int refclk;
7173
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007174 WARN_ON(!crtc_state->base.state);
7175
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007176 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007177 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007178 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007179 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007180 refclk = dev_priv->vbt.lvds_ssc_freq;
7181 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007182 } else if (!IS_GEN2(dev)) {
7183 refclk = 96000;
7184 } else {
7185 refclk = 48000;
7186 }
7187
7188 return refclk;
7189}
7190
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007191static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007192{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007193 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007194}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007195
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007196static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7197{
7198 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007199}
7200
Daniel Vetterf47709a2013-03-28 10:42:02 +01007201static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007202 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007203 intel_clock_t *reduced_clock)
7204{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007205 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007206 u32 fp, fp2 = 0;
7207
7208 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007209 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007210 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007211 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007212 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007213 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007214 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007215 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007216 }
7217
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007218 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007219
Daniel Vetterf47709a2013-03-28 10:42:02 +01007220 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007221 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007222 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007223 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007224 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007225 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007226 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007227 }
7228}
7229
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007230static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7231 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007232{
7233 u32 reg_val;
7234
7235 /*
7236 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7237 * and set it to a reasonable value instead.
7238 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007239 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007240 reg_val &= 0xffffff00;
7241 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007242 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007243
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007244 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007245 reg_val &= 0x8cffffff;
7246 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007247 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007248
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007249 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007250 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007251 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007252
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007253 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007254 reg_val &= 0x00ffffff;
7255 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007256 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007257}
7258
Daniel Vetterb5518422013-05-03 11:49:48 +02007259static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7260 struct intel_link_m_n *m_n)
7261{
7262 struct drm_device *dev = crtc->base.dev;
7263 struct drm_i915_private *dev_priv = dev->dev_private;
7264 int pipe = crtc->pipe;
7265
Daniel Vettere3b95f12013-05-03 11:49:49 +02007266 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7267 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7268 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7269 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007270}
7271
7272static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007273 struct intel_link_m_n *m_n,
7274 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007275{
7276 struct drm_device *dev = crtc->base.dev;
7277 struct drm_i915_private *dev_priv = dev->dev_private;
7278 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007279 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007280
7281 if (INTEL_INFO(dev)->gen >= 5) {
7282 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7283 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7284 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7285 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007286 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7287 * for gen < 8) and if DRRS is supported (to make sure the
7288 * registers are not unnecessarily accessed).
7289 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307290 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007291 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007292 I915_WRITE(PIPE_DATA_M2(transcoder),
7293 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7294 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7295 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7296 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7297 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007298 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007299 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7300 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7301 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7302 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007303 }
7304}
7305
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307306void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007307{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307308 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7309
7310 if (m_n == M1_N1) {
7311 dp_m_n = &crtc->config->dp_m_n;
7312 dp_m2_n2 = &crtc->config->dp_m2_n2;
7313 } else if (m_n == M2_N2) {
7314
7315 /*
7316 * M2_N2 registers are not supported. Hence m2_n2 divider value
7317 * needs to be programmed into M1_N1.
7318 */
7319 dp_m_n = &crtc->config->dp_m2_n2;
7320 } else {
7321 DRM_ERROR("Unsupported divider value\n");
7322 return;
7323 }
7324
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007325 if (crtc->config->has_pch_encoder)
7326 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007327 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307328 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007329}
7330
Ville Syrjäläd288f652014-10-28 13:20:22 +02007331static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007332 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007333{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007334 u32 dpll, dpll_md;
7335
7336 /*
7337 * Enable DPIO clock input. We should never disable the reference
7338 * clock for pipe B, since VGA hotplug / manual detection depends
7339 * on it.
7340 */
7341 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7342 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7343 /* We should never disable this, set it here for state tracking */
7344 if (crtc->pipe == PIPE_B)
7345 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7346 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007347 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007348
Ville Syrjäläd288f652014-10-28 13:20:22 +02007349 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007350 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007351 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007352}
7353
Ville Syrjäläd288f652014-10-28 13:20:22 +02007354static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007355 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007356{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007357 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007358 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007359 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007360 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007361 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007362 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007363
Ville Syrjäläa5805162015-05-26 20:42:30 +03007364 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007365
Ville Syrjäläd288f652014-10-28 13:20:22 +02007366 bestn = pipe_config->dpll.n;
7367 bestm1 = pipe_config->dpll.m1;
7368 bestm2 = pipe_config->dpll.m2;
7369 bestp1 = pipe_config->dpll.p1;
7370 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007371
Jesse Barnes89b667f2013-04-18 14:51:36 -07007372 /* See eDP HDMI DPIO driver vbios notes doc */
7373
7374 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007375 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007376 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007377
7378 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007379 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007380
7381 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007382 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007383 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007384 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007385
7386 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007387 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007388
7389 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007390 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7391 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7392 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007393 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007394
7395 /*
7396 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7397 * but we don't support that).
7398 * Note: don't use the DAC post divider as it seems unstable.
7399 */
7400 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007401 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007402
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007403 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007404 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007405
Jesse Barnes89b667f2013-04-18 14:51:36 -07007406 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007407 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007408 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7409 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007410 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007411 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007412 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007413 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007414 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007415
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007416 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007417 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007418 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007419 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007420 0x0df40000);
7421 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007422 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007423 0x0df70000);
7424 } else { /* HDMI or VGA */
7425 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007426 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007427 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007428 0x0df70000);
7429 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007430 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007431 0x0df40000);
7432 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007433
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007434 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007435 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007436 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7437 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007438 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007439 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007440
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007441 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007442 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007443}
7444
Ville Syrjäläd288f652014-10-28 13:20:22 +02007445static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007446 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007447{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007448 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007449 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7450 DPLL_VCO_ENABLE;
7451 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007452 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007453
Ville Syrjäläd288f652014-10-28 13:20:22 +02007454 pipe_config->dpll_hw_state.dpll_md =
7455 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007456}
7457
Ville Syrjäläd288f652014-10-28 13:20:22 +02007458static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007459 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007460{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007461 struct drm_device *dev = crtc->base.dev;
7462 struct drm_i915_private *dev_priv = dev->dev_private;
7463 int pipe = crtc->pipe;
7464 int dpll_reg = DPLL(crtc->pipe);
7465 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307466 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007467 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307468 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307469 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007470
Ville Syrjäläd288f652014-10-28 13:20:22 +02007471 bestn = pipe_config->dpll.n;
7472 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7473 bestm1 = pipe_config->dpll.m1;
7474 bestm2 = pipe_config->dpll.m2 >> 22;
7475 bestp1 = pipe_config->dpll.p1;
7476 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307477 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307478 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307479 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007480
7481 /*
7482 * Enable Refclk and SSC
7483 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007484 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007485 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007486
Ville Syrjäläa5805162015-05-26 20:42:30 +03007487 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007488
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007489 /* p1 and p2 divider */
7490 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7491 5 << DPIO_CHV_S1_DIV_SHIFT |
7492 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7493 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7494 1 << DPIO_CHV_K_DIV_SHIFT);
7495
7496 /* Feedback post-divider - m2 */
7497 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7498
7499 /* Feedback refclk divider - n and m1 */
7500 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7501 DPIO_CHV_M1_DIV_BY_2 |
7502 1 << DPIO_CHV_N_DIV_SHIFT);
7503
7504 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307505 if (bestm2_frac)
7506 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007507
7508 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307509 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7510 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7511 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7512 if (bestm2_frac)
7513 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7514 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007515
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307516 /* Program digital lock detect threshold */
7517 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7518 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7519 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7520 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7521 if (!bestm2_frac)
7522 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7523 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7524
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007525 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307526 if (vco == 5400000) {
7527 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7528 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7529 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7530 tribuf_calcntr = 0x9;
7531 } else if (vco <= 6200000) {
7532 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7533 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7534 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7535 tribuf_calcntr = 0x9;
7536 } else if (vco <= 6480000) {
7537 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7538 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7539 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7540 tribuf_calcntr = 0x8;
7541 } else {
7542 /* Not supported. Apply the same limits as in the max case */
7543 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7544 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7545 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7546 tribuf_calcntr = 0;
7547 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007548 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7549
Ville Syrjälä968040b2015-03-11 22:52:08 +02007550 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307551 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7552 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7553 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7554
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007555 /* AFC Recal */
7556 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7557 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7558 DPIO_AFC_RECAL);
7559
Ville Syrjäläa5805162015-05-26 20:42:30 +03007560 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007561}
7562
Ville Syrjäläd288f652014-10-28 13:20:22 +02007563/**
7564 * vlv_force_pll_on - forcibly enable just the PLL
7565 * @dev_priv: i915 private structure
7566 * @pipe: pipe PLL to enable
7567 * @dpll: PLL configuration
7568 *
7569 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7570 * in cases where we need the PLL enabled even when @pipe is not going to
7571 * be enabled.
7572 */
7573void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7574 const struct dpll *dpll)
7575{
7576 struct intel_crtc *crtc =
7577 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007578 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007579 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007580 .pixel_multiplier = 1,
7581 .dpll = *dpll,
7582 };
7583
7584 if (IS_CHERRYVIEW(dev)) {
7585 chv_update_pll(crtc, &pipe_config);
7586 chv_prepare_pll(crtc, &pipe_config);
7587 chv_enable_pll(crtc, &pipe_config);
7588 } else {
7589 vlv_update_pll(crtc, &pipe_config);
7590 vlv_prepare_pll(crtc, &pipe_config);
7591 vlv_enable_pll(crtc, &pipe_config);
7592 }
7593}
7594
7595/**
7596 * vlv_force_pll_off - forcibly disable just the PLL
7597 * @dev_priv: i915 private structure
7598 * @pipe: pipe PLL to disable
7599 *
7600 * Disable the PLL for @pipe. To be used in cases where we need
7601 * the PLL enabled even when @pipe is not going to be enabled.
7602 */
7603void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7604{
7605 if (IS_CHERRYVIEW(dev))
7606 chv_disable_pll(to_i915(dev), pipe);
7607 else
7608 vlv_disable_pll(to_i915(dev), pipe);
7609}
7610
Daniel Vetterf47709a2013-03-28 10:42:02 +01007611static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007612 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007613 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007614 int num_connectors)
7615{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007616 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007617 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007618 u32 dpll;
7619 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007620 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007621
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007622 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307623
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007624 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7625 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007626
7627 dpll = DPLL_VGA_MODE_DIS;
7628
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007629 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007630 dpll |= DPLLB_MODE_LVDS;
7631 else
7632 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007633
Daniel Vetteref1b4602013-06-01 17:17:04 +02007634 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007635 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007636 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007637 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007638
7639 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007640 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007641
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007642 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007643 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007644
7645 /* compute bitmask from p1 value */
7646 if (IS_PINEVIEW(dev))
7647 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7648 else {
7649 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7650 if (IS_G4X(dev) && reduced_clock)
7651 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7652 }
7653 switch (clock->p2) {
7654 case 5:
7655 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7656 break;
7657 case 7:
7658 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7659 break;
7660 case 10:
7661 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7662 break;
7663 case 14:
7664 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7665 break;
7666 }
7667 if (INTEL_INFO(dev)->gen >= 4)
7668 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7669
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007670 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007671 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007672 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007673 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7674 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7675 else
7676 dpll |= PLL_REF_INPUT_DREFCLK;
7677
7678 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007679 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007680
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007681 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007682 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007683 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007684 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007685 }
7686}
7687
Daniel Vetterf47709a2013-03-28 10:42:02 +01007688static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007689 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007690 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007691 int num_connectors)
7692{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007693 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007694 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007695 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007696 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007697
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007698 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307699
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007700 dpll = DPLL_VGA_MODE_DIS;
7701
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007702 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007703 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7704 } else {
7705 if (clock->p1 == 2)
7706 dpll |= PLL_P1_DIVIDE_BY_TWO;
7707 else
7708 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7709 if (clock->p2 == 4)
7710 dpll |= PLL_P2_DIVIDE_BY_4;
7711 }
7712
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007713 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007714 dpll |= DPLL_DVO_2X_MODE;
7715
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007716 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007717 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7718 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7719 else
7720 dpll |= PLL_REF_INPUT_DREFCLK;
7721
7722 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007723 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007724}
7725
Daniel Vetter8a654f32013-06-01 17:16:22 +02007726static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007727{
7728 struct drm_device *dev = intel_crtc->base.dev;
7729 struct drm_i915_private *dev_priv = dev->dev_private;
7730 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007731 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007732 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007733 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007734 uint32_t crtc_vtotal, crtc_vblank_end;
7735 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007736
7737 /* We need to be careful not to changed the adjusted mode, for otherwise
7738 * the hw state checker will get angry at the mismatch. */
7739 crtc_vtotal = adjusted_mode->crtc_vtotal;
7740 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007741
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007742 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007743 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007744 crtc_vtotal -= 1;
7745 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007746
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007747 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007748 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7749 else
7750 vsyncshift = adjusted_mode->crtc_hsync_start -
7751 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007752 if (vsyncshift < 0)
7753 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007754 }
7755
7756 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007757 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007758
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007759 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007760 (adjusted_mode->crtc_hdisplay - 1) |
7761 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007762 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007763 (adjusted_mode->crtc_hblank_start - 1) |
7764 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007765 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007766 (adjusted_mode->crtc_hsync_start - 1) |
7767 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7768
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007769 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007770 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007771 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007772 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007773 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007774 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007775 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007776 (adjusted_mode->crtc_vsync_start - 1) |
7777 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7778
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007779 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7780 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7781 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7782 * bits. */
7783 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7784 (pipe == PIPE_B || pipe == PIPE_C))
7785 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7786
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007787 /* pipesrc controls the size that is scaled from, which should
7788 * always be the user's requested size.
7789 */
7790 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007791 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7792 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007793}
7794
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007795static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007796 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007797{
7798 struct drm_device *dev = crtc->base.dev;
7799 struct drm_i915_private *dev_priv = dev->dev_private;
7800 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7801 uint32_t tmp;
7802
7803 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007804 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7805 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007806 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007807 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7808 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007809 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007810 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7811 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007812
7813 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007814 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7815 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007816 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007817 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7818 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007819 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007820 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7821 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007822
7823 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007824 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7825 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7826 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007827 }
7828
7829 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007830 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7831 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7832
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007833 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7834 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007835}
7836
Daniel Vetterf6a83282014-02-11 15:28:57 -08007837void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007838 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007839{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007840 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7841 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7842 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7843 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007844
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007845 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7846 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7847 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7848 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007849
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007850 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007851
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007852 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7853 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007854}
7855
Daniel Vetter84b046f2013-02-19 18:48:54 +01007856static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7857{
7858 struct drm_device *dev = intel_crtc->base.dev;
7859 struct drm_i915_private *dev_priv = dev->dev_private;
7860 uint32_t pipeconf;
7861
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007862 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007863
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007864 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7865 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7866 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007867
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007868 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007869 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007870
Daniel Vetterff9ce462013-04-24 14:57:17 +02007871 /* only g4x and later have fancy bpc/dither controls */
7872 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007873 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007874 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007875 pipeconf |= PIPECONF_DITHER_EN |
7876 PIPECONF_DITHER_TYPE_SP;
7877
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007878 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007879 case 18:
7880 pipeconf |= PIPECONF_6BPC;
7881 break;
7882 case 24:
7883 pipeconf |= PIPECONF_8BPC;
7884 break;
7885 case 30:
7886 pipeconf |= PIPECONF_10BPC;
7887 break;
7888 default:
7889 /* Case prevented by intel_choose_pipe_bpp_dither. */
7890 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007891 }
7892 }
7893
7894 if (HAS_PIPE_CXSR(dev)) {
7895 if (intel_crtc->lowfreq_avail) {
7896 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7897 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7898 } else {
7899 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007900 }
7901 }
7902
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007903 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007904 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007905 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007906 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7907 else
7908 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7909 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007910 pipeconf |= PIPECONF_PROGRESSIVE;
7911
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007912 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007913 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007914
Daniel Vetter84b046f2013-02-19 18:48:54 +01007915 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7916 POSTING_READ(PIPECONF(intel_crtc->pipe));
7917}
7918
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007919static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7920 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007921{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007922 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007923 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007924 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007925 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007926 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007927 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007928 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007929 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007930 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007931 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007932 struct drm_connector_state *connector_state;
7933 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007934
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007935 memset(&crtc_state->dpll_hw_state, 0,
7936 sizeof(crtc_state->dpll_hw_state));
7937
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007938 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007939 if (connector_state->crtc != &crtc->base)
7940 continue;
7941
7942 encoder = to_intel_encoder(connector_state->best_encoder);
7943
Chris Wilson5eddb702010-09-11 13:48:45 +01007944 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007945 case INTEL_OUTPUT_LVDS:
7946 is_lvds = true;
7947 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007948 case INTEL_OUTPUT_DSI:
7949 is_dsi = true;
7950 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007951 default:
7952 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007953 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007954
Eric Anholtc751ce42010-03-25 11:48:48 -07007955 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007956 }
7957
Jani Nikulaf2335332013-09-13 11:03:09 +03007958 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007959 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007960
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007961 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007962 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007963
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007964 /*
7965 * Returns a set of divisors for the desired target clock with
7966 * the given refclk, or FALSE. The returned values represent
7967 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7968 * 2) / p1 / p2.
7969 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007970 limit = intel_limit(crtc_state, refclk);
7971 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007972 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007973 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007974 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007975 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7976 return -EINVAL;
7977 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007978
Jani Nikulaf2335332013-09-13 11:03:09 +03007979 if (is_lvds && dev_priv->lvds_downclock_avail) {
7980 /*
7981 * Ensure we match the reduced clock's P to the target
7982 * clock. If the clocks don't match, we can't switch
7983 * the display clock by using the FP0/FP1. In such case
7984 * we will disable the LVDS downclock feature.
7985 */
7986 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007987 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007988 dev_priv->lvds_downclock,
7989 refclk, &clock,
7990 &reduced_clock);
7991 }
7992 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007993 crtc_state->dpll.n = clock.n;
7994 crtc_state->dpll.m1 = clock.m1;
7995 crtc_state->dpll.m2 = clock.m2;
7996 crtc_state->dpll.p1 = clock.p1;
7997 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007998 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007999
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008000 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008001 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05308002 has_reduced_clock ? &reduced_clock : NULL,
8003 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03008004 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008005 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008006 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008007 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008008 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008009 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02008010 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02008011 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03008012 }
Eric Anholtf564048e2011-03-30 13:01:02 -07008013
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008014 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07008015}
8016
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008017static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008018 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008019{
8020 struct drm_device *dev = crtc->base.dev;
8021 struct drm_i915_private *dev_priv = dev->dev_private;
8022 uint32_t tmp;
8023
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02008024 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
8025 return;
8026
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008027 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02008028 if (!(tmp & PFIT_ENABLE))
8029 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008030
Daniel Vetter06922822013-07-11 13:35:40 +02008031 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008032 if (INTEL_INFO(dev)->gen < 4) {
8033 if (crtc->pipe != PIPE_B)
8034 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008035 } else {
8036 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
8037 return;
8038 }
8039
Daniel Vetter06922822013-07-11 13:35:40 +02008040 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008041 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
8042 if (INTEL_INFO(dev)->gen < 5)
8043 pipe_config->gmch_pfit.lvds_border_bits =
8044 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
8045}
8046
Jesse Barnesacbec812013-09-20 11:29:32 -07008047static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008048 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008049{
8050 struct drm_device *dev = crtc->base.dev;
8051 struct drm_i915_private *dev_priv = dev->dev_private;
8052 int pipe = pipe_config->cpu_transcoder;
8053 intel_clock_t clock;
8054 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008055 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008056
Shobhit Kumarf573de52014-07-30 20:32:37 +05308057 /* In case of MIPI DPLL will not even be used */
8058 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8059 return;
8060
Ville Syrjäläa5805162015-05-26 20:42:30 +03008061 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008062 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008063 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008064
8065 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8066 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8067 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8068 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8069 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8070
Ville Syrjäläf6466282013-10-14 14:50:31 +03008071 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008072
Ville Syrjäläf6466282013-10-14 14:50:31 +03008073 /* clock.dot is the fast clock */
8074 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07008075}
8076
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008077static void
8078i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8079 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008080{
8081 struct drm_device *dev = crtc->base.dev;
8082 struct drm_i915_private *dev_priv = dev->dev_private;
8083 u32 val, base, offset;
8084 int pipe = crtc->pipe, plane = crtc->plane;
8085 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008086 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008087 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008088 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008089
Damien Lespiau42a7b082015-02-05 19:35:13 +00008090 val = I915_READ(DSPCNTR(plane));
8091 if (!(val & DISPLAY_PLANE_ENABLE))
8092 return;
8093
Damien Lespiaud9806c92015-01-21 14:07:19 +00008094 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008095 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008096 DRM_DEBUG_KMS("failed to alloc fb\n");
8097 return;
8098 }
8099
Damien Lespiau1b842c82015-01-21 13:50:54 +00008100 fb = &intel_fb->base;
8101
Daniel Vetter18c52472015-02-10 17:16:09 +00008102 if (INTEL_INFO(dev)->gen >= 4) {
8103 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008104 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008105 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8106 }
8107 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008108
8109 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008110 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008111 fb->pixel_format = fourcc;
8112 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008113
8114 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008115 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008116 offset = I915_READ(DSPTILEOFF(plane));
8117 else
8118 offset = I915_READ(DSPLINOFF(plane));
8119 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8120 } else {
8121 base = I915_READ(DSPADDR(plane));
8122 }
8123 plane_config->base = base;
8124
8125 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008126 fb->width = ((val >> 16) & 0xfff) + 1;
8127 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008128
8129 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008130 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008131
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008132 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008133 fb->pixel_format,
8134 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008135
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008136 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008137
Damien Lespiau2844a922015-01-20 12:51:48 +00008138 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8139 pipe_name(pipe), plane, fb->width, fb->height,
8140 fb->bits_per_pixel, base, fb->pitches[0],
8141 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008142
Damien Lespiau2d140302015-02-05 17:22:18 +00008143 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008144}
8145
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008146static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008147 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008148{
8149 struct drm_device *dev = crtc->base.dev;
8150 struct drm_i915_private *dev_priv = dev->dev_private;
8151 int pipe = pipe_config->cpu_transcoder;
8152 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8153 intel_clock_t clock;
8154 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8155 int refclk = 100000;
8156
Ville Syrjäläa5805162015-05-26 20:42:30 +03008157 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008158 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8159 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8160 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8161 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008162 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008163
8164 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8165 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8166 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8167 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8168 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8169
8170 chv_clock(refclk, &clock);
8171
8172 /* clock.dot is the fast clock */
8173 pipe_config->port_clock = clock.dot / 5;
8174}
8175
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008176static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008177 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008178{
8179 struct drm_device *dev = crtc->base.dev;
8180 struct drm_i915_private *dev_priv = dev->dev_private;
8181 uint32_t tmp;
8182
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008183 if (!intel_display_power_is_enabled(dev_priv,
8184 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008185 return false;
8186
Daniel Vettere143a212013-07-04 12:01:15 +02008187 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008188 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008189
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008190 tmp = I915_READ(PIPECONF(crtc->pipe));
8191 if (!(tmp & PIPECONF_ENABLE))
8192 return false;
8193
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008194 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8195 switch (tmp & PIPECONF_BPC_MASK) {
8196 case PIPECONF_6BPC:
8197 pipe_config->pipe_bpp = 18;
8198 break;
8199 case PIPECONF_8BPC:
8200 pipe_config->pipe_bpp = 24;
8201 break;
8202 case PIPECONF_10BPC:
8203 pipe_config->pipe_bpp = 30;
8204 break;
8205 default:
8206 break;
8207 }
8208 }
8209
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008210 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8211 pipe_config->limited_color_range = true;
8212
Ville Syrjälä282740f2013-09-04 18:30:03 +03008213 if (INTEL_INFO(dev)->gen < 4)
8214 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8215
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008216 intel_get_pipe_timings(crtc, pipe_config);
8217
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008218 i9xx_get_pfit_config(crtc, pipe_config);
8219
Daniel Vetter6c49f242013-06-06 12:45:25 +02008220 if (INTEL_INFO(dev)->gen >= 4) {
8221 tmp = I915_READ(DPLL_MD(crtc->pipe));
8222 pipe_config->pixel_multiplier =
8223 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8224 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008225 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008226 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8227 tmp = I915_READ(DPLL(crtc->pipe));
8228 pipe_config->pixel_multiplier =
8229 ((tmp & SDVO_MULTIPLIER_MASK)
8230 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8231 } else {
8232 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8233 * port and will be fixed up in the encoder->get_config
8234 * function. */
8235 pipe_config->pixel_multiplier = 1;
8236 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008237 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8238 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008239 /*
8240 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8241 * on 830. Filter it out here so that we don't
8242 * report errors due to that.
8243 */
8244 if (IS_I830(dev))
8245 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8246
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008247 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8248 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008249 } else {
8250 /* Mask out read-only status bits. */
8251 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8252 DPLL_PORTC_READY_MASK |
8253 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008254 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008255
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008256 if (IS_CHERRYVIEW(dev))
8257 chv_crtc_clock_get(crtc, pipe_config);
8258 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008259 vlv_crtc_clock_get(crtc, pipe_config);
8260 else
8261 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008262
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008263 return true;
8264}
8265
Paulo Zanonidde86e22012-12-01 12:04:25 -02008266static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008267{
8268 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008269 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008270 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008271 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008272 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008273 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008274 bool has_ck505 = false;
8275 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008276
8277 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008278 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008279 switch (encoder->type) {
8280 case INTEL_OUTPUT_LVDS:
8281 has_panel = true;
8282 has_lvds = true;
8283 break;
8284 case INTEL_OUTPUT_EDP:
8285 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008286 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008287 has_cpu_edp = true;
8288 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008289 default:
8290 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008291 }
8292 }
8293
Keith Packard99eb6a02011-09-26 14:29:12 -07008294 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008295 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008296 can_ssc = has_ck505;
8297 } else {
8298 has_ck505 = false;
8299 can_ssc = true;
8300 }
8301
Imre Deak2de69052013-05-08 13:14:04 +03008302 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8303 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008304
8305 /* Ironlake: try to setup display ref clock before DPLL
8306 * enabling. This is only under driver's control after
8307 * PCH B stepping, previous chipset stepping should be
8308 * ignoring this setting.
8309 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008310 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008311
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008312 /* As we must carefully and slowly disable/enable each source in turn,
8313 * compute the final state we want first and check if we need to
8314 * make any changes at all.
8315 */
8316 final = val;
8317 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008318 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008319 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008320 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008321 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8322
8323 final &= ~DREF_SSC_SOURCE_MASK;
8324 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8325 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008326
Keith Packard199e5d72011-09-22 12:01:57 -07008327 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008328 final |= DREF_SSC_SOURCE_ENABLE;
8329
8330 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8331 final |= DREF_SSC1_ENABLE;
8332
8333 if (has_cpu_edp) {
8334 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8335 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8336 else
8337 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8338 } else
8339 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8340 } else {
8341 final |= DREF_SSC_SOURCE_DISABLE;
8342 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8343 }
8344
8345 if (final == val)
8346 return;
8347
8348 /* Always enable nonspread source */
8349 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8350
8351 if (has_ck505)
8352 val |= DREF_NONSPREAD_CK505_ENABLE;
8353 else
8354 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8355
8356 if (has_panel) {
8357 val &= ~DREF_SSC_SOURCE_MASK;
8358 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008359
Keith Packard199e5d72011-09-22 12:01:57 -07008360 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008361 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008362 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008363 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008364 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008365 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008366
8367 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008368 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008369 POSTING_READ(PCH_DREF_CONTROL);
8370 udelay(200);
8371
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008372 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008373
8374 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008375 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008376 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008377 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008378 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008379 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008380 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008381 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008382 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008383
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008384 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008385 POSTING_READ(PCH_DREF_CONTROL);
8386 udelay(200);
8387 } else {
8388 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8389
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008390 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008391
8392 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008393 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008394
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008395 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008396 POSTING_READ(PCH_DREF_CONTROL);
8397 udelay(200);
8398
8399 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008400 val &= ~DREF_SSC_SOURCE_MASK;
8401 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008402
8403 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008404 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008405
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008406 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008407 POSTING_READ(PCH_DREF_CONTROL);
8408 udelay(200);
8409 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008410
8411 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008412}
8413
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008414static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008415{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008416 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008417
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008418 tmp = I915_READ(SOUTH_CHICKEN2);
8419 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8420 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008421
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008422 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8423 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8424 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008425
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008426 tmp = I915_READ(SOUTH_CHICKEN2);
8427 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8428 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008429
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008430 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8431 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8432 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008433}
8434
8435/* WaMPhyProgramming:hsw */
8436static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8437{
8438 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008439
8440 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8441 tmp &= ~(0xFF << 24);
8442 tmp |= (0x12 << 24);
8443 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8444
Paulo Zanonidde86e22012-12-01 12:04:25 -02008445 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8446 tmp |= (1 << 11);
8447 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8448
8449 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8450 tmp |= (1 << 11);
8451 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8452
Paulo Zanonidde86e22012-12-01 12:04:25 -02008453 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8454 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8455 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8456
8457 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8458 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8459 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8460
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008461 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8462 tmp &= ~(7 << 13);
8463 tmp |= (5 << 13);
8464 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008465
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008466 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8467 tmp &= ~(7 << 13);
8468 tmp |= (5 << 13);
8469 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008470
8471 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8472 tmp &= ~0xFF;
8473 tmp |= 0x1C;
8474 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8475
8476 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8477 tmp &= ~0xFF;
8478 tmp |= 0x1C;
8479 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8480
8481 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8482 tmp &= ~(0xFF << 16);
8483 tmp |= (0x1C << 16);
8484 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8485
8486 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8487 tmp &= ~(0xFF << 16);
8488 tmp |= (0x1C << 16);
8489 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8490
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008491 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8492 tmp |= (1 << 27);
8493 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008494
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008495 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8496 tmp |= (1 << 27);
8497 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008498
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008499 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8500 tmp &= ~(0xF << 28);
8501 tmp |= (4 << 28);
8502 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008503
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008504 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8505 tmp &= ~(0xF << 28);
8506 tmp |= (4 << 28);
8507 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008508}
8509
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008510/* Implements 3 different sequences from BSpec chapter "Display iCLK
8511 * Programming" based on the parameters passed:
8512 * - Sequence to enable CLKOUT_DP
8513 * - Sequence to enable CLKOUT_DP without spread
8514 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8515 */
8516static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8517 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008518{
8519 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008520 uint32_t reg, tmp;
8521
8522 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8523 with_spread = true;
8524 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8525 with_fdi, "LP PCH doesn't have FDI\n"))
8526 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008527
Ville Syrjäläa5805162015-05-26 20:42:30 +03008528 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008529
8530 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8531 tmp &= ~SBI_SSCCTL_DISABLE;
8532 tmp |= SBI_SSCCTL_PATHALT;
8533 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8534
8535 udelay(24);
8536
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008537 if (with_spread) {
8538 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8539 tmp &= ~SBI_SSCCTL_PATHALT;
8540 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008541
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008542 if (with_fdi) {
8543 lpt_reset_fdi_mphy(dev_priv);
8544 lpt_program_fdi_mphy(dev_priv);
8545 }
8546 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008547
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008548 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8549 SBI_GEN0 : SBI_DBUFF0;
8550 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8551 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8552 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008553
Ville Syrjäläa5805162015-05-26 20:42:30 +03008554 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008555}
8556
Paulo Zanoni47701c32013-07-23 11:19:25 -03008557/* Sequence to disable CLKOUT_DP */
8558static void lpt_disable_clkout_dp(struct drm_device *dev)
8559{
8560 struct drm_i915_private *dev_priv = dev->dev_private;
8561 uint32_t reg, tmp;
8562
Ville Syrjäläa5805162015-05-26 20:42:30 +03008563 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008564
8565 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8566 SBI_GEN0 : SBI_DBUFF0;
8567 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8568 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8569 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8570
8571 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8572 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8573 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8574 tmp |= SBI_SSCCTL_PATHALT;
8575 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8576 udelay(32);
8577 }
8578 tmp |= SBI_SSCCTL_DISABLE;
8579 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8580 }
8581
Ville Syrjäläa5805162015-05-26 20:42:30 +03008582 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008583}
8584
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008585static void lpt_init_pch_refclk(struct drm_device *dev)
8586{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008587 struct intel_encoder *encoder;
8588 bool has_vga = false;
8589
Damien Lespiaub2784e12014-08-05 11:29:37 +01008590 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008591 switch (encoder->type) {
8592 case INTEL_OUTPUT_ANALOG:
8593 has_vga = true;
8594 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008595 default:
8596 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008597 }
8598 }
8599
Paulo Zanoni47701c32013-07-23 11:19:25 -03008600 if (has_vga)
8601 lpt_enable_clkout_dp(dev, true, true);
8602 else
8603 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008604}
8605
Paulo Zanonidde86e22012-12-01 12:04:25 -02008606/*
8607 * Initialize reference clocks when the driver loads
8608 */
8609void intel_init_pch_refclk(struct drm_device *dev)
8610{
8611 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8612 ironlake_init_pch_refclk(dev);
8613 else if (HAS_PCH_LPT(dev))
8614 lpt_init_pch_refclk(dev);
8615}
8616
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008617static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008618{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008619 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008620 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008621 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008622 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008623 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008624 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008625 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008626 bool is_lvds = false;
8627
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008628 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008629 if (connector_state->crtc != crtc_state->base.crtc)
8630 continue;
8631
8632 encoder = to_intel_encoder(connector_state->best_encoder);
8633
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008634 switch (encoder->type) {
8635 case INTEL_OUTPUT_LVDS:
8636 is_lvds = true;
8637 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008638 default:
8639 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008640 }
8641 num_connectors++;
8642 }
8643
8644 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008645 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008646 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008647 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008648 }
8649
8650 return 120000;
8651}
8652
Daniel Vetter6ff93602013-04-19 11:24:36 +02008653static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008654{
8655 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8656 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8657 int pipe = intel_crtc->pipe;
8658 uint32_t val;
8659
Daniel Vetter78114072013-06-13 00:54:57 +02008660 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008661
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008662 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008663 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008664 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008665 break;
8666 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008667 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008668 break;
8669 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008670 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008671 break;
8672 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008673 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008674 break;
8675 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008676 /* Case prevented by intel_choose_pipe_bpp_dither. */
8677 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008678 }
8679
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008680 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008681 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8682
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008683 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008684 val |= PIPECONF_INTERLACED_ILK;
8685 else
8686 val |= PIPECONF_PROGRESSIVE;
8687
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008688 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008689 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008690
Paulo Zanonic8203562012-09-12 10:06:29 -03008691 I915_WRITE(PIPECONF(pipe), val);
8692 POSTING_READ(PIPECONF(pipe));
8693}
8694
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008695/*
8696 * Set up the pipe CSC unit.
8697 *
8698 * Currently only full range RGB to limited range RGB conversion
8699 * is supported, but eventually this should handle various
8700 * RGB<->YCbCr scenarios as well.
8701 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008702static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008703{
8704 struct drm_device *dev = crtc->dev;
8705 struct drm_i915_private *dev_priv = dev->dev_private;
8706 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8707 int pipe = intel_crtc->pipe;
8708 uint16_t coeff = 0x7800; /* 1.0 */
8709
8710 /*
8711 * TODO: Check what kind of values actually come out of the pipe
8712 * with these coeff/postoff values and adjust to get the best
8713 * accuracy. Perhaps we even need to take the bpc value into
8714 * consideration.
8715 */
8716
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008717 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008718 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8719
8720 /*
8721 * GY/GU and RY/RU should be the other way around according
8722 * to BSpec, but reality doesn't agree. Just set them up in
8723 * a way that results in the correct picture.
8724 */
8725 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8726 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8727
8728 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8729 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8730
8731 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8732 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8733
8734 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8735 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8736 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8737
8738 if (INTEL_INFO(dev)->gen > 6) {
8739 uint16_t postoff = 0;
8740
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008741 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008742 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008743
8744 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8745 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8746 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8747
8748 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8749 } else {
8750 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8751
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008752 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008753 mode |= CSC_BLACK_SCREEN_OFFSET;
8754
8755 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8756 }
8757}
8758
Daniel Vetter6ff93602013-04-19 11:24:36 +02008759static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008760{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008761 struct drm_device *dev = crtc->dev;
8762 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008763 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008764 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008765 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008766 uint32_t val;
8767
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008768 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008769
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008770 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008771 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8772
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008773 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008774 val |= PIPECONF_INTERLACED_ILK;
8775 else
8776 val |= PIPECONF_PROGRESSIVE;
8777
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008778 I915_WRITE(PIPECONF(cpu_transcoder), val);
8779 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008780
8781 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8782 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008783
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308784 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008785 val = 0;
8786
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008787 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008788 case 18:
8789 val |= PIPEMISC_DITHER_6_BPC;
8790 break;
8791 case 24:
8792 val |= PIPEMISC_DITHER_8_BPC;
8793 break;
8794 case 30:
8795 val |= PIPEMISC_DITHER_10_BPC;
8796 break;
8797 case 36:
8798 val |= PIPEMISC_DITHER_12_BPC;
8799 break;
8800 default:
8801 /* Case prevented by pipe_config_set_bpp. */
8802 BUG();
8803 }
8804
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008805 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008806 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8807
8808 I915_WRITE(PIPEMISC(pipe), val);
8809 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008810}
8811
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008812static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008813 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008814 intel_clock_t *clock,
8815 bool *has_reduced_clock,
8816 intel_clock_t *reduced_clock)
8817{
8818 struct drm_device *dev = crtc->dev;
8819 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008820 int refclk;
8821 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008822 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008823
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008824 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008825
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008826 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008827
8828 /*
8829 * Returns a set of divisors for the desired target clock with the given
8830 * refclk, or FALSE. The returned values represent the clock equation:
8831 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8832 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008833 limit = intel_limit(crtc_state, refclk);
8834 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008835 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008836 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008837 if (!ret)
8838 return false;
8839
8840 if (is_lvds && dev_priv->lvds_downclock_avail) {
8841 /*
8842 * Ensure we match the reduced clock's P to the target clock.
8843 * If the clocks don't match, we can't switch the display clock
8844 * by using the FP0/FP1. In such case we will disable the LVDS
8845 * downclock feature.
8846 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008847 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008848 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008849 dev_priv->lvds_downclock,
8850 refclk, clock,
8851 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008852 }
8853
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008854 return true;
8855}
8856
Paulo Zanonid4b19312012-11-29 11:29:32 -02008857int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8858{
8859 /*
8860 * Account for spread spectrum to avoid
8861 * oversubscribing the link. Max center spread
8862 * is 2.5%; use 5% for safety's sake.
8863 */
8864 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008865 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008866}
8867
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008868static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008869{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008870 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008871}
8872
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008873static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008874 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008875 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008876 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008877{
8878 struct drm_crtc *crtc = &intel_crtc->base;
8879 struct drm_device *dev = crtc->dev;
8880 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008881 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008882 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008883 struct drm_connector_state *connector_state;
8884 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008885 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008886 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008887 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008888
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008889 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008890 if (connector_state->crtc != crtc_state->base.crtc)
8891 continue;
8892
8893 encoder = to_intel_encoder(connector_state->best_encoder);
8894
8895 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008896 case INTEL_OUTPUT_LVDS:
8897 is_lvds = true;
8898 break;
8899 case INTEL_OUTPUT_SDVO:
8900 case INTEL_OUTPUT_HDMI:
8901 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008902 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008903 default:
8904 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008905 }
8906
8907 num_connectors++;
8908 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008909
Chris Wilsonc1858122010-12-03 21:35:48 +00008910 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008911 factor = 21;
8912 if (is_lvds) {
8913 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008914 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008915 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008916 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008917 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008918 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008919
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008920 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008921 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008922
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008923 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8924 *fp2 |= FP_CB_TUNE;
8925
Chris Wilson5eddb702010-09-11 13:48:45 +01008926 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008927
Eric Anholta07d6782011-03-30 13:01:08 -07008928 if (is_lvds)
8929 dpll |= DPLLB_MODE_LVDS;
8930 else
8931 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008932
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008933 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008934 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008935
8936 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008937 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008938 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008939 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008940
Eric Anholta07d6782011-03-30 13:01:08 -07008941 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008942 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008943 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008944 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008945
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008946 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008947 case 5:
8948 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8949 break;
8950 case 7:
8951 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8952 break;
8953 case 10:
8954 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8955 break;
8956 case 14:
8957 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8958 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008959 }
8960
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008961 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008962 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008963 else
8964 dpll |= PLL_REF_INPUT_DREFCLK;
8965
Daniel Vetter959e16d2013-06-05 13:34:21 +02008966 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008967}
8968
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008969static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8970 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008971{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008972 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008973 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008974 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008975 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008976 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008977 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008978
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008979 memset(&crtc_state->dpll_hw_state, 0,
8980 sizeof(crtc_state->dpll_hw_state));
8981
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008982 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008983
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008984 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8985 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8986
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008987 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008988 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008989 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008990 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8991 return -EINVAL;
8992 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008993 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008994 if (!crtc_state->clock_set) {
8995 crtc_state->dpll.n = clock.n;
8996 crtc_state->dpll.m1 = clock.m1;
8997 crtc_state->dpll.m2 = clock.m2;
8998 crtc_state->dpll.p1 = clock.p1;
8999 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01009000 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009001
Paulo Zanoni5dc52982012-10-05 12:05:56 -03009002 /* 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 +02009003 if (crtc_state->has_pch_encoder) {
9004 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009005 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02009006 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009007
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009008 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02009009 &fp, &reduced_clock,
9010 has_reduced_clock ? &fp2 : NULL);
9011
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009012 crtc_state->dpll_hw_state.dpll = dpll;
9013 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009014 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009015 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009016 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009017 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02009018
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009019 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01009020 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03009021 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009022 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07009023 return -EINVAL;
9024 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02009025 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009026
Rodrigo Viviab585de2015-03-24 12:40:09 -07009027 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009028 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02009029 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009030 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02009031
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009032 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009033}
9034
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009035static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
9036 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02009037{
9038 struct drm_device *dev = crtc->base.dev;
9039 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009040 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02009041
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009042 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
9043 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
9044 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
9045 & ~TU_SIZE_MASK;
9046 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9047 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9048 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9049}
9050
9051static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9052 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009053 struct intel_link_m_n *m_n,
9054 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009055{
9056 struct drm_device *dev = crtc->base.dev;
9057 struct drm_i915_private *dev_priv = dev->dev_private;
9058 enum pipe pipe = crtc->pipe;
9059
9060 if (INTEL_INFO(dev)->gen >= 5) {
9061 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9062 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9063 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9064 & ~TU_SIZE_MASK;
9065 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9066 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9067 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009068 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9069 * gen < 8) and if DRRS is supported (to make sure the
9070 * registers are not unnecessarily read).
9071 */
9072 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009073 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009074 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9075 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9076 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9077 & ~TU_SIZE_MASK;
9078 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9079 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9080 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9081 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009082 } else {
9083 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9084 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9085 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9086 & ~TU_SIZE_MASK;
9087 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9088 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9089 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9090 }
9091}
9092
9093void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009094 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009095{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009096 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009097 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9098 else
9099 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009100 &pipe_config->dp_m_n,
9101 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009102}
9103
Daniel Vetter72419202013-04-04 13:28:53 +02009104static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009105 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009106{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009107 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009108 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009109}
9110
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009111static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009112 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009113{
9114 struct drm_device *dev = crtc->base.dev;
9115 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009116 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9117 uint32_t ps_ctrl = 0;
9118 int id = -1;
9119 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009120
Chandra Kondurua1b22782015-04-07 15:28:45 -07009121 /* find scaler attached to this pipe */
9122 for (i = 0; i < crtc->num_scalers; i++) {
9123 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9124 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9125 id = i;
9126 pipe_config->pch_pfit.enabled = true;
9127 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9128 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9129 break;
9130 }
9131 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009132
Chandra Kondurua1b22782015-04-07 15:28:45 -07009133 scaler_state->scaler_id = id;
9134 if (id >= 0) {
9135 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9136 } else {
9137 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009138 }
9139}
9140
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009141static void
9142skylake_get_initial_plane_config(struct intel_crtc *crtc,
9143 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009144{
9145 struct drm_device *dev = crtc->base.dev;
9146 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009147 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009148 int pipe = crtc->pipe;
9149 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009150 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009151 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009152 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009153
Damien Lespiaud9806c92015-01-21 14:07:19 +00009154 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009155 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009156 DRM_DEBUG_KMS("failed to alloc fb\n");
9157 return;
9158 }
9159
Damien Lespiau1b842c82015-01-21 13:50:54 +00009160 fb = &intel_fb->base;
9161
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009162 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009163 if (!(val & PLANE_CTL_ENABLE))
9164 goto error;
9165
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009166 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9167 fourcc = skl_format_to_fourcc(pixel_format,
9168 val & PLANE_CTL_ORDER_RGBX,
9169 val & PLANE_CTL_ALPHA_MASK);
9170 fb->pixel_format = fourcc;
9171 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9172
Damien Lespiau40f46282015-02-27 11:15:21 +00009173 tiling = val & PLANE_CTL_TILED_MASK;
9174 switch (tiling) {
9175 case PLANE_CTL_TILED_LINEAR:
9176 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9177 break;
9178 case PLANE_CTL_TILED_X:
9179 plane_config->tiling = I915_TILING_X;
9180 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9181 break;
9182 case PLANE_CTL_TILED_Y:
9183 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9184 break;
9185 case PLANE_CTL_TILED_YF:
9186 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9187 break;
9188 default:
9189 MISSING_CASE(tiling);
9190 goto error;
9191 }
9192
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009193 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9194 plane_config->base = base;
9195
9196 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9197
9198 val = I915_READ(PLANE_SIZE(pipe, 0));
9199 fb->height = ((val >> 16) & 0xfff) + 1;
9200 fb->width = ((val >> 0) & 0x1fff) + 1;
9201
9202 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009203 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9204 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009205 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9206
9207 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009208 fb->pixel_format,
9209 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009210
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009211 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009212
9213 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9214 pipe_name(pipe), fb->width, fb->height,
9215 fb->bits_per_pixel, base, fb->pitches[0],
9216 plane_config->size);
9217
Damien Lespiau2d140302015-02-05 17:22:18 +00009218 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009219 return;
9220
9221error:
9222 kfree(fb);
9223}
9224
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009225static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009226 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009227{
9228 struct drm_device *dev = crtc->base.dev;
9229 struct drm_i915_private *dev_priv = dev->dev_private;
9230 uint32_t tmp;
9231
9232 tmp = I915_READ(PF_CTL(crtc->pipe));
9233
9234 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009235 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009236 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9237 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009238
9239 /* We currently do not free assignements of panel fitters on
9240 * ivb/hsw (since we don't use the higher upscaling modes which
9241 * differentiates them) so just WARN about this case for now. */
9242 if (IS_GEN7(dev)) {
9243 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9244 PF_PIPE_SEL_IVB(crtc->pipe));
9245 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009246 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009247}
9248
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009249static void
9250ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9251 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009252{
9253 struct drm_device *dev = crtc->base.dev;
9254 struct drm_i915_private *dev_priv = dev->dev_private;
9255 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009256 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009257 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009258 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009259 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009260 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009261
Damien Lespiau42a7b082015-02-05 19:35:13 +00009262 val = I915_READ(DSPCNTR(pipe));
9263 if (!(val & DISPLAY_PLANE_ENABLE))
9264 return;
9265
Damien Lespiaud9806c92015-01-21 14:07:19 +00009266 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009267 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009268 DRM_DEBUG_KMS("failed to alloc fb\n");
9269 return;
9270 }
9271
Damien Lespiau1b842c82015-01-21 13:50:54 +00009272 fb = &intel_fb->base;
9273
Daniel Vetter18c52472015-02-10 17:16:09 +00009274 if (INTEL_INFO(dev)->gen >= 4) {
9275 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009276 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009277 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9278 }
9279 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009280
9281 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009282 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009283 fb->pixel_format = fourcc;
9284 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009285
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009286 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009287 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009288 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009289 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009290 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009291 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009292 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009293 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009294 }
9295 plane_config->base = base;
9296
9297 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009298 fb->width = ((val >> 16) & 0xfff) + 1;
9299 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009300
9301 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009302 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009303
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009304 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009305 fb->pixel_format,
9306 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009307
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009308 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009309
Damien Lespiau2844a922015-01-20 12:51:48 +00009310 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9311 pipe_name(pipe), fb->width, fb->height,
9312 fb->bits_per_pixel, base, fb->pitches[0],
9313 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009314
Damien Lespiau2d140302015-02-05 17:22:18 +00009315 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009316}
9317
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009318static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009319 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009320{
9321 struct drm_device *dev = crtc->base.dev;
9322 struct drm_i915_private *dev_priv = dev->dev_private;
9323 uint32_t tmp;
9324
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009325 if (!intel_display_power_is_enabled(dev_priv,
9326 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009327 return false;
9328
Daniel Vettere143a212013-07-04 12:01:15 +02009329 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009330 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009331
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009332 tmp = I915_READ(PIPECONF(crtc->pipe));
9333 if (!(tmp & PIPECONF_ENABLE))
9334 return false;
9335
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009336 switch (tmp & PIPECONF_BPC_MASK) {
9337 case PIPECONF_6BPC:
9338 pipe_config->pipe_bpp = 18;
9339 break;
9340 case PIPECONF_8BPC:
9341 pipe_config->pipe_bpp = 24;
9342 break;
9343 case PIPECONF_10BPC:
9344 pipe_config->pipe_bpp = 30;
9345 break;
9346 case PIPECONF_12BPC:
9347 pipe_config->pipe_bpp = 36;
9348 break;
9349 default:
9350 break;
9351 }
9352
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009353 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9354 pipe_config->limited_color_range = true;
9355
Daniel Vetterab9412b2013-05-03 11:49:46 +02009356 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009357 struct intel_shared_dpll *pll;
9358
Daniel Vetter88adfff2013-03-28 10:42:01 +01009359 pipe_config->has_pch_encoder = true;
9360
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009361 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9362 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9363 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009364
9365 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009366
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009367 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009368 pipe_config->shared_dpll =
9369 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009370 } else {
9371 tmp = I915_READ(PCH_DPLL_SEL);
9372 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9373 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9374 else
9375 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9376 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009377
9378 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9379
9380 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9381 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009382
9383 tmp = pipe_config->dpll_hw_state.dpll;
9384 pipe_config->pixel_multiplier =
9385 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9386 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009387
9388 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009389 } else {
9390 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009391 }
9392
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009393 intel_get_pipe_timings(crtc, pipe_config);
9394
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009395 ironlake_get_pfit_config(crtc, pipe_config);
9396
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009397 return true;
9398}
9399
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009400static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9401{
9402 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009403 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009404
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009405 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009406 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009407 pipe_name(crtc->pipe));
9408
Rob Clarke2c719b2014-12-15 13:56:32 -05009409 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9410 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9411 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9412 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9413 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9414 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009415 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009416 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009417 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009418 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009419 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009420 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009421 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009422 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009423 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009424
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009425 /*
9426 * In theory we can still leave IRQs enabled, as long as only the HPD
9427 * interrupts remain enabled. We used to check for that, but since it's
9428 * gen-specific and since we only disable LCPLL after we fully disable
9429 * the interrupts, the check below should be enough.
9430 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009431 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009432}
9433
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009434static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9435{
9436 struct drm_device *dev = dev_priv->dev;
9437
9438 if (IS_HASWELL(dev))
9439 return I915_READ(D_COMP_HSW);
9440 else
9441 return I915_READ(D_COMP_BDW);
9442}
9443
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009444static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9445{
9446 struct drm_device *dev = dev_priv->dev;
9447
9448 if (IS_HASWELL(dev)) {
9449 mutex_lock(&dev_priv->rps.hw_lock);
9450 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9451 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009452 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009453 mutex_unlock(&dev_priv->rps.hw_lock);
9454 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009455 I915_WRITE(D_COMP_BDW, val);
9456 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009457 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009458}
9459
9460/*
9461 * This function implements pieces of two sequences from BSpec:
9462 * - Sequence for display software to disable LCPLL
9463 * - Sequence for display software to allow package C8+
9464 * The steps implemented here are just the steps that actually touch the LCPLL
9465 * register. Callers should take care of disabling all the display engine
9466 * functions, doing the mode unset, fixing interrupts, etc.
9467 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009468static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9469 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009470{
9471 uint32_t val;
9472
9473 assert_can_disable_lcpll(dev_priv);
9474
9475 val = I915_READ(LCPLL_CTL);
9476
9477 if (switch_to_fclk) {
9478 val |= LCPLL_CD_SOURCE_FCLK;
9479 I915_WRITE(LCPLL_CTL, val);
9480
9481 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9482 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9483 DRM_ERROR("Switching to FCLK failed\n");
9484
9485 val = I915_READ(LCPLL_CTL);
9486 }
9487
9488 val |= LCPLL_PLL_DISABLE;
9489 I915_WRITE(LCPLL_CTL, val);
9490 POSTING_READ(LCPLL_CTL);
9491
9492 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9493 DRM_ERROR("LCPLL still locked\n");
9494
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009495 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009496 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009497 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009498 ndelay(100);
9499
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009500 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9501 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009502 DRM_ERROR("D_COMP RCOMP still in progress\n");
9503
9504 if (allow_power_down) {
9505 val = I915_READ(LCPLL_CTL);
9506 val |= LCPLL_POWER_DOWN_ALLOW;
9507 I915_WRITE(LCPLL_CTL, val);
9508 POSTING_READ(LCPLL_CTL);
9509 }
9510}
9511
9512/*
9513 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9514 * source.
9515 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009516static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009517{
9518 uint32_t val;
9519
9520 val = I915_READ(LCPLL_CTL);
9521
9522 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9523 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9524 return;
9525
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009526 /*
9527 * Make sure we're not on PC8 state before disabling PC8, otherwise
9528 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009529 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009530 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009531
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009532 if (val & LCPLL_POWER_DOWN_ALLOW) {
9533 val &= ~LCPLL_POWER_DOWN_ALLOW;
9534 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009535 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009536 }
9537
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009538 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009539 val |= D_COMP_COMP_FORCE;
9540 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009541 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009542
9543 val = I915_READ(LCPLL_CTL);
9544 val &= ~LCPLL_PLL_DISABLE;
9545 I915_WRITE(LCPLL_CTL, val);
9546
9547 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9548 DRM_ERROR("LCPLL not locked yet\n");
9549
9550 if (val & LCPLL_CD_SOURCE_FCLK) {
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) == 0, 1))
9557 DRM_ERROR("Switching back to LCPLL failed\n");
9558 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009559
Mika Kuoppala59bad942015-01-16 11:34:40 +02009560 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009561 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009562}
9563
Paulo Zanoni765dab672014-03-07 20:08:18 -03009564/*
9565 * Package states C8 and deeper are really deep PC states that can only be
9566 * reached when all the devices on the system allow it, so even if the graphics
9567 * device allows PC8+, it doesn't mean the system will actually get to these
9568 * states. Our driver only allows PC8+ when going into runtime PM.
9569 *
9570 * The requirements for PC8+ are that all the outputs are disabled, the power
9571 * well is disabled and most interrupts are disabled, and these are also
9572 * requirements for runtime PM. When these conditions are met, we manually do
9573 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9574 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9575 * hang the machine.
9576 *
9577 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9578 * the state of some registers, so when we come back from PC8+ we need to
9579 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9580 * need to take care of the registers kept by RC6. Notice that this happens even
9581 * if we don't put the device in PCI D3 state (which is what currently happens
9582 * because of the runtime PM support).
9583 *
9584 * For more, read "Display Sequences for Package C8" on the hardware
9585 * documentation.
9586 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009587void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009588{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009589 struct drm_device *dev = dev_priv->dev;
9590 uint32_t val;
9591
Paulo Zanonic67a4702013-08-19 13:18:09 -03009592 DRM_DEBUG_KMS("Enabling package C8+\n");
9593
Paulo Zanonic67a4702013-08-19 13:18:09 -03009594 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9595 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9596 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9597 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9598 }
9599
9600 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009601 hsw_disable_lcpll(dev_priv, true, true);
9602}
9603
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009604void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009605{
9606 struct drm_device *dev = dev_priv->dev;
9607 uint32_t val;
9608
Paulo Zanonic67a4702013-08-19 13:18:09 -03009609 DRM_DEBUG_KMS("Disabling package C8+\n");
9610
9611 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009612 lpt_init_pch_refclk(dev);
9613
9614 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9615 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9616 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9617 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9618 }
9619
9620 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009621}
9622
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009623static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309624{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009625 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309626 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009627 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309628 int req_cdclk;
9629
9630 /* see the comment in valleyview_modeset_global_resources */
9631 if (WARN_ON(max_pixclk < 0))
9632 return;
9633
9634 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9635
9636 if (req_cdclk != dev_priv->cdclk_freq)
9637 broxton_set_cdclk(dev, req_cdclk);
9638}
9639
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009640/* compute the max rate for new configuration */
9641static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9642{
9643 struct drm_device *dev = dev_priv->dev;
9644 struct intel_crtc *intel_crtc;
9645 struct drm_crtc *crtc;
9646 int max_pixel_rate = 0;
9647 int pixel_rate;
9648
9649 for_each_crtc(dev, crtc) {
9650 if (!crtc->state->enable)
9651 continue;
9652
9653 intel_crtc = to_intel_crtc(crtc);
9654 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9655
9656 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9657 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9658 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9659
9660 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9661 }
9662
9663 return max_pixel_rate;
9664}
9665
9666static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9667{
9668 struct drm_i915_private *dev_priv = dev->dev_private;
9669 uint32_t val, data;
9670 int ret;
9671
9672 if (WARN((I915_READ(LCPLL_CTL) &
9673 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9674 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9675 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9676 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9677 "trying to change cdclk frequency with cdclk not enabled\n"))
9678 return;
9679
9680 mutex_lock(&dev_priv->rps.hw_lock);
9681 ret = sandybridge_pcode_write(dev_priv,
9682 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9683 mutex_unlock(&dev_priv->rps.hw_lock);
9684 if (ret) {
9685 DRM_ERROR("failed to inform pcode about cdclk change\n");
9686 return;
9687 }
9688
9689 val = I915_READ(LCPLL_CTL);
9690 val |= LCPLL_CD_SOURCE_FCLK;
9691 I915_WRITE(LCPLL_CTL, val);
9692
9693 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9694 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9695 DRM_ERROR("Switching to FCLK failed\n");
9696
9697 val = I915_READ(LCPLL_CTL);
9698 val &= ~LCPLL_CLK_FREQ_MASK;
9699
9700 switch (cdclk) {
9701 case 450000:
9702 val |= LCPLL_CLK_FREQ_450;
9703 data = 0;
9704 break;
9705 case 540000:
9706 val |= LCPLL_CLK_FREQ_54O_BDW;
9707 data = 1;
9708 break;
9709 case 337500:
9710 val |= LCPLL_CLK_FREQ_337_5_BDW;
9711 data = 2;
9712 break;
9713 case 675000:
9714 val |= LCPLL_CLK_FREQ_675_BDW;
9715 data = 3;
9716 break;
9717 default:
9718 WARN(1, "invalid cdclk frequency\n");
9719 return;
9720 }
9721
9722 I915_WRITE(LCPLL_CTL, val);
9723
9724 val = I915_READ(LCPLL_CTL);
9725 val &= ~LCPLL_CD_SOURCE_FCLK;
9726 I915_WRITE(LCPLL_CTL, val);
9727
9728 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9729 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9730 DRM_ERROR("Switching back to LCPLL failed\n");
9731
9732 mutex_lock(&dev_priv->rps.hw_lock);
9733 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9734 mutex_unlock(&dev_priv->rps.hw_lock);
9735
9736 intel_update_cdclk(dev);
9737
9738 WARN(cdclk != dev_priv->cdclk_freq,
9739 "cdclk requested %d kHz but got %d kHz\n",
9740 cdclk, dev_priv->cdclk_freq);
9741}
9742
9743static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9744 int max_pixel_rate)
9745{
9746 int cdclk;
9747
9748 /*
9749 * FIXME should also account for plane ratio
9750 * once 64bpp pixel formats are supported.
9751 */
9752 if (max_pixel_rate > 540000)
9753 cdclk = 675000;
9754 else if (max_pixel_rate > 450000)
9755 cdclk = 540000;
9756 else if (max_pixel_rate > 337500)
9757 cdclk = 450000;
9758 else
9759 cdclk = 337500;
9760
9761 /*
9762 * FIXME move the cdclk caclulation to
9763 * compute_config() so we can fail gracegully.
9764 */
9765 if (cdclk > dev_priv->max_cdclk_freq) {
9766 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9767 cdclk, dev_priv->max_cdclk_freq);
9768 cdclk = dev_priv->max_cdclk_freq;
9769 }
9770
9771 return cdclk;
9772}
9773
9774static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9775{
9776 struct drm_i915_private *dev_priv = to_i915(state->dev);
9777 struct drm_crtc *crtc;
9778 struct drm_crtc_state *crtc_state;
9779 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9780 int cdclk, i;
9781
9782 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9783
9784 if (cdclk == dev_priv->cdclk_freq)
9785 return 0;
9786
9787 /* add all active pipes to the state */
9788 for_each_crtc(state->dev, crtc) {
9789 if (!crtc->state->enable)
9790 continue;
9791
9792 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9793 if (IS_ERR(crtc_state))
9794 return PTR_ERR(crtc_state);
9795 }
9796
9797 /* disable/enable all currently active pipes while we change cdclk */
9798 for_each_crtc_in_state(state, crtc, crtc_state, i)
9799 if (crtc_state->enable)
9800 crtc_state->mode_changed = true;
9801
9802 return 0;
9803}
9804
9805static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9806{
9807 struct drm_device *dev = state->dev;
9808 struct drm_i915_private *dev_priv = dev->dev_private;
9809 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9810 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9811
9812 if (req_cdclk != dev_priv->cdclk_freq)
9813 broadwell_set_cdclk(dev, req_cdclk);
9814}
9815
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009816static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9817 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009818{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009819 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009820 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009821
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009822 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009823
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009824 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009825}
9826
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309827static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9828 enum port port,
9829 struct intel_crtc_state *pipe_config)
9830{
9831 switch (port) {
9832 case PORT_A:
9833 pipe_config->ddi_pll_sel = SKL_DPLL0;
9834 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9835 break;
9836 case PORT_B:
9837 pipe_config->ddi_pll_sel = SKL_DPLL1;
9838 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9839 break;
9840 case PORT_C:
9841 pipe_config->ddi_pll_sel = SKL_DPLL2;
9842 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9843 break;
9844 default:
9845 DRM_ERROR("Incorrect port type\n");
9846 }
9847}
9848
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009849static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9850 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009851 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009852{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009853 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009854
9855 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9856 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9857
9858 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009859 case SKL_DPLL0:
9860 /*
9861 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9862 * of the shared DPLL framework and thus needs to be read out
9863 * separately
9864 */
9865 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9866 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9867 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009868 case SKL_DPLL1:
9869 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9870 break;
9871 case SKL_DPLL2:
9872 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9873 break;
9874 case SKL_DPLL3:
9875 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9876 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009877 }
9878}
9879
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009880static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9881 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009882 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009883{
9884 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9885
9886 switch (pipe_config->ddi_pll_sel) {
9887 case PORT_CLK_SEL_WRPLL1:
9888 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9889 break;
9890 case PORT_CLK_SEL_WRPLL2:
9891 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9892 break;
9893 }
9894}
9895
Daniel Vetter26804af2014-06-25 22:01:55 +03009896static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009897 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009898{
9899 struct drm_device *dev = crtc->base.dev;
9900 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009901 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009902 enum port port;
9903 uint32_t tmp;
9904
9905 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9906
9907 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9908
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009909 if (IS_SKYLAKE(dev))
9910 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309911 else if (IS_BROXTON(dev))
9912 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009913 else
9914 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009915
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009916 if (pipe_config->shared_dpll >= 0) {
9917 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9918
9919 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9920 &pipe_config->dpll_hw_state));
9921 }
9922
Daniel Vetter26804af2014-06-25 22:01:55 +03009923 /*
9924 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9925 * DDI E. So just check whether this pipe is wired to DDI E and whether
9926 * the PCH transcoder is on.
9927 */
Damien Lespiauca370452013-12-03 13:56:24 +00009928 if (INTEL_INFO(dev)->gen < 9 &&
9929 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009930 pipe_config->has_pch_encoder = true;
9931
9932 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9933 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9934 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9935
9936 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9937 }
9938}
9939
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009940static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009941 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009942{
9943 struct drm_device *dev = crtc->base.dev;
9944 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009945 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009946 uint32_t tmp;
9947
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009948 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009949 POWER_DOMAIN_PIPE(crtc->pipe)))
9950 return false;
9951
Daniel Vettere143a212013-07-04 12:01:15 +02009952 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009953 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9954
Daniel Vettereccb1402013-05-22 00:50:22 +02009955 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9956 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9957 enum pipe trans_edp_pipe;
9958 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9959 default:
9960 WARN(1, "unknown pipe linked to edp transcoder\n");
9961 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9962 case TRANS_DDI_EDP_INPUT_A_ON:
9963 trans_edp_pipe = PIPE_A;
9964 break;
9965 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9966 trans_edp_pipe = PIPE_B;
9967 break;
9968 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9969 trans_edp_pipe = PIPE_C;
9970 break;
9971 }
9972
9973 if (trans_edp_pipe == crtc->pipe)
9974 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9975 }
9976
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009977 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009978 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009979 return false;
9980
Daniel Vettereccb1402013-05-22 00:50:22 +02009981 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009982 if (!(tmp & PIPECONF_ENABLE))
9983 return false;
9984
Daniel Vetter26804af2014-06-25 22:01:55 +03009985 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009986
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009987 intel_get_pipe_timings(crtc, pipe_config);
9988
Chandra Kondurua1b22782015-04-07 15:28:45 -07009989 if (INTEL_INFO(dev)->gen >= 9) {
9990 skl_init_scalers(dev, crtc, pipe_config);
9991 }
9992
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009993 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009994
9995 if (INTEL_INFO(dev)->gen >= 9) {
9996 pipe_config->scaler_state.scaler_id = -1;
9997 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9998 }
9999
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010000 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010001 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010002 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010003 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010004 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -080010005 else
10006 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +000010007 }
Daniel Vetter88adfff2013-03-28 10:42:01 +010010008
Jesse Barnese59150d2014-01-07 13:30:45 -080010009 if (IS_HASWELL(dev))
10010 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
10011 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030010012
Clint Taylorebb69c92014-09-30 10:30:22 -070010013 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
10014 pipe_config->pixel_multiplier =
10015 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
10016 } else {
10017 pipe_config->pixel_multiplier = 1;
10018 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020010019
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010010020 return true;
10021}
10022
Chris Wilson560b85b2010-08-07 11:01:38 +010010023static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
10024{
10025 struct drm_device *dev = crtc->dev;
10026 struct drm_i915_private *dev_priv = dev->dev_private;
10027 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +030010028 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +010010029
Ville Syrjälädc41c152014-08-13 11:57:05 +030010030 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010031 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
10032 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +030010033 unsigned int stride = roundup_pow_of_two(width) * 4;
10034
10035 switch (stride) {
10036 default:
10037 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
10038 width, stride);
10039 stride = 256;
10040 /* fallthrough */
10041 case 256:
10042 case 512:
10043 case 1024:
10044 case 2048:
10045 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010046 }
10047
Ville Syrjälädc41c152014-08-13 11:57:05 +030010048 cntl |= CURSOR_ENABLE |
10049 CURSOR_GAMMA_ENABLE |
10050 CURSOR_FORMAT_ARGB |
10051 CURSOR_STRIDE(stride);
10052
10053 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010054 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010055
Ville Syrjälädc41c152014-08-13 11:57:05 +030010056 if (intel_crtc->cursor_cntl != 0 &&
10057 (intel_crtc->cursor_base != base ||
10058 intel_crtc->cursor_size != size ||
10059 intel_crtc->cursor_cntl != cntl)) {
10060 /* On these chipsets we can only modify the base/size/stride
10061 * whilst the cursor is disabled.
10062 */
10063 I915_WRITE(_CURACNTR, 0);
10064 POSTING_READ(_CURACNTR);
10065 intel_crtc->cursor_cntl = 0;
10066 }
10067
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010068 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +030010069 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010070 intel_crtc->cursor_base = base;
10071 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010072
10073 if (intel_crtc->cursor_size != size) {
10074 I915_WRITE(CURSIZE, size);
10075 intel_crtc->cursor_size = size;
10076 }
10077
Chris Wilson4b0e3332014-05-30 16:35:26 +030010078 if (intel_crtc->cursor_cntl != cntl) {
10079 I915_WRITE(_CURACNTR, cntl);
10080 POSTING_READ(_CURACNTR);
10081 intel_crtc->cursor_cntl = cntl;
10082 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010083}
10084
10085static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
10086{
10087 struct drm_device *dev = crtc->dev;
10088 struct drm_i915_private *dev_priv = dev->dev_private;
10089 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10090 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010091 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +010010092
Chris Wilson4b0e3332014-05-30 16:35:26 +030010093 cntl = 0;
10094 if (base) {
10095 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -080010096 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010097 case 64:
10098 cntl |= CURSOR_MODE_64_ARGB_AX;
10099 break;
10100 case 128:
10101 cntl |= CURSOR_MODE_128_ARGB_AX;
10102 break;
10103 case 256:
10104 cntl |= CURSOR_MODE_256_ARGB_AX;
10105 break;
10106 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010107 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010108 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010109 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010110 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010111
10112 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10113 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010114 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010115
Matt Roper8e7d6882015-01-21 16:35:41 -080010116 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010117 cntl |= CURSOR_ROTATE_180;
10118
Chris Wilson4b0e3332014-05-30 16:35:26 +030010119 if (intel_crtc->cursor_cntl != cntl) {
10120 I915_WRITE(CURCNTR(pipe), cntl);
10121 POSTING_READ(CURCNTR(pipe));
10122 intel_crtc->cursor_cntl = cntl;
10123 }
10124
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010125 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010126 I915_WRITE(CURBASE(pipe), base);
10127 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010128
10129 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010130}
10131
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010132/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010133static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10134 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010135{
10136 struct drm_device *dev = crtc->dev;
10137 struct drm_i915_private *dev_priv = dev->dev_private;
10138 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10139 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010140 int x = crtc->cursor_x;
10141 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010142 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010143
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010144 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010145 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010146
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010147 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010148 base = 0;
10149
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010150 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010151 base = 0;
10152
10153 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010154 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010155 base = 0;
10156
10157 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10158 x = -x;
10159 }
10160 pos |= x << CURSOR_X_SHIFT;
10161
10162 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010163 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010164 base = 0;
10165
10166 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10167 y = -y;
10168 }
10169 pos |= y << CURSOR_Y_SHIFT;
10170
Chris Wilson4b0e3332014-05-30 16:35:26 +030010171 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010172 return;
10173
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010174 I915_WRITE(CURPOS(pipe), pos);
10175
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010176 /* ILK+ do this automagically */
10177 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010178 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010179 base += (intel_crtc->base.cursor->state->crtc_h *
10180 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010181 }
10182
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010183 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010184 i845_update_cursor(crtc, base);
10185 else
10186 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010187}
10188
Ville Syrjälädc41c152014-08-13 11:57:05 +030010189static bool cursor_size_ok(struct drm_device *dev,
10190 uint32_t width, uint32_t height)
10191{
10192 if (width == 0 || height == 0)
10193 return false;
10194
10195 /*
10196 * 845g/865g are special in that they are only limited by
10197 * the width of their cursors, the height is arbitrary up to
10198 * the precision of the register. Everything else requires
10199 * square cursors, limited to a few power-of-two sizes.
10200 */
10201 if (IS_845G(dev) || IS_I865G(dev)) {
10202 if ((width & 63) != 0)
10203 return false;
10204
10205 if (width > (IS_845G(dev) ? 64 : 512))
10206 return false;
10207
10208 if (height > 1023)
10209 return false;
10210 } else {
10211 switch (width | height) {
10212 case 256:
10213 case 128:
10214 if (IS_GEN2(dev))
10215 return false;
10216 case 64:
10217 break;
10218 default:
10219 return false;
10220 }
10221 }
10222
10223 return true;
10224}
10225
Jesse Barnes79e53942008-11-07 14:24:08 -080010226static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010227 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010228{
James Simmons72034252010-08-03 01:33:19 +010010229 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010230 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010231
James Simmons72034252010-08-03 01:33:19 +010010232 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010233 intel_crtc->lut_r[i] = red[i] >> 8;
10234 intel_crtc->lut_g[i] = green[i] >> 8;
10235 intel_crtc->lut_b[i] = blue[i] >> 8;
10236 }
10237
10238 intel_crtc_load_lut(crtc);
10239}
10240
Jesse Barnes79e53942008-11-07 14:24:08 -080010241/* VESA 640x480x72Hz mode to set on the pipe */
10242static struct drm_display_mode load_detect_mode = {
10243 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10244 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10245};
10246
Daniel Vettera8bb6812014-02-10 18:00:39 +010010247struct drm_framebuffer *
10248__intel_framebuffer_create(struct drm_device *dev,
10249 struct drm_mode_fb_cmd2 *mode_cmd,
10250 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010251{
10252 struct intel_framebuffer *intel_fb;
10253 int ret;
10254
10255 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10256 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010257 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010258 return ERR_PTR(-ENOMEM);
10259 }
10260
10261 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010262 if (ret)
10263 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010264
10265 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010266err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010267 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010268 kfree(intel_fb);
10269
10270 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010271}
10272
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010273static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010274intel_framebuffer_create(struct drm_device *dev,
10275 struct drm_mode_fb_cmd2 *mode_cmd,
10276 struct drm_i915_gem_object *obj)
10277{
10278 struct drm_framebuffer *fb;
10279 int ret;
10280
10281 ret = i915_mutex_lock_interruptible(dev);
10282 if (ret)
10283 return ERR_PTR(ret);
10284 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10285 mutex_unlock(&dev->struct_mutex);
10286
10287 return fb;
10288}
10289
Chris Wilsond2dff872011-04-19 08:36:26 +010010290static u32
10291intel_framebuffer_pitch_for_width(int width, int bpp)
10292{
10293 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10294 return ALIGN(pitch, 64);
10295}
10296
10297static u32
10298intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10299{
10300 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010301 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010302}
10303
10304static struct drm_framebuffer *
10305intel_framebuffer_create_for_mode(struct drm_device *dev,
10306 struct drm_display_mode *mode,
10307 int depth, int bpp)
10308{
10309 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010310 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010311
10312 obj = i915_gem_alloc_object(dev,
10313 intel_framebuffer_size_for_mode(mode, bpp));
10314 if (obj == NULL)
10315 return ERR_PTR(-ENOMEM);
10316
10317 mode_cmd.width = mode->hdisplay;
10318 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010319 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10320 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010321 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010322
10323 return intel_framebuffer_create(dev, &mode_cmd, obj);
10324}
10325
10326static struct drm_framebuffer *
10327mode_fits_in_fbdev(struct drm_device *dev,
10328 struct drm_display_mode *mode)
10329{
Daniel Vetter4520f532013-10-09 09:18:51 +020010330#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010331 struct drm_i915_private *dev_priv = dev->dev_private;
10332 struct drm_i915_gem_object *obj;
10333 struct drm_framebuffer *fb;
10334
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010335 if (!dev_priv->fbdev)
10336 return NULL;
10337
10338 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010339 return NULL;
10340
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010341 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010342 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010343
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010344 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010345 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10346 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010347 return NULL;
10348
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010349 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010350 return NULL;
10351
10352 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010353#else
10354 return NULL;
10355#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010356}
10357
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010358static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10359 struct drm_crtc *crtc,
10360 struct drm_display_mode *mode,
10361 struct drm_framebuffer *fb,
10362 int x, int y)
10363{
10364 struct drm_plane_state *plane_state;
10365 int hdisplay, vdisplay;
10366 int ret;
10367
10368 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10369 if (IS_ERR(plane_state))
10370 return PTR_ERR(plane_state);
10371
10372 if (mode)
10373 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10374 else
10375 hdisplay = vdisplay = 0;
10376
10377 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10378 if (ret)
10379 return ret;
10380 drm_atomic_set_fb_for_plane(plane_state, fb);
10381 plane_state->crtc_x = 0;
10382 plane_state->crtc_y = 0;
10383 plane_state->crtc_w = hdisplay;
10384 plane_state->crtc_h = vdisplay;
10385 plane_state->src_x = x << 16;
10386 plane_state->src_y = y << 16;
10387 plane_state->src_w = hdisplay << 16;
10388 plane_state->src_h = vdisplay << 16;
10389
10390 return 0;
10391}
10392
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010393bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010394 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010395 struct intel_load_detect_pipe *old,
10396 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010397{
10398 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010399 struct intel_encoder *intel_encoder =
10400 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010401 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010402 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010403 struct drm_crtc *crtc = NULL;
10404 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010405 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010406 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010407 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010408 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010409 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010410 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010411
Chris Wilsond2dff872011-04-19 08:36:26 +010010412 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010413 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010414 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010415
Rob Clark51fd3712013-11-19 12:10:12 -050010416retry:
10417 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10418 if (ret)
10419 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010420
Jesse Barnes79e53942008-11-07 14:24:08 -080010421 /*
10422 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010423 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010424 * - if the connector already has an assigned crtc, use it (but make
10425 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010426 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010427 * - try to find the first unused crtc that can drive this connector,
10428 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010429 */
10430
10431 /* See if we already have a CRTC for this connector */
10432 if (encoder->crtc) {
10433 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010434
Rob Clark51fd3712013-11-19 12:10:12 -050010435 ret = drm_modeset_lock(&crtc->mutex, ctx);
10436 if (ret)
10437 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010438 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10439 if (ret)
10440 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010441
Daniel Vetter24218aa2012-08-12 19:27:11 +020010442 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010443 old->load_detect_temp = false;
10444
10445 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010446 if (connector->dpms != DRM_MODE_DPMS_ON)
10447 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010448
Chris Wilson71731882011-04-19 23:10:58 +010010449 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010450 }
10451
10452 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010453 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010454 i++;
10455 if (!(encoder->possible_crtcs & (1 << i)))
10456 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010457 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010458 continue;
10459 /* This can occur when applying the pipe A quirk on resume. */
10460 if (to_intel_crtc(possible_crtc)->new_enabled)
10461 continue;
10462
10463 crtc = possible_crtc;
10464 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010465 }
10466
10467 /*
10468 * If we didn't find an unused CRTC, don't use any.
10469 */
10470 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010471 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010472 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010473 }
10474
Rob Clark51fd3712013-11-19 12:10:12 -050010475 ret = drm_modeset_lock(&crtc->mutex, ctx);
10476 if (ret)
10477 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010478 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10479 if (ret)
10480 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010481 intel_encoder->new_crtc = to_intel_crtc(crtc);
10482 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010483
10484 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010485 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010486 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010487 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010488 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010489
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010490 state = drm_atomic_state_alloc(dev);
10491 if (!state)
10492 return false;
10493
10494 state->acquire_ctx = ctx;
10495
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010496 connector_state = drm_atomic_get_connector_state(state, connector);
10497 if (IS_ERR(connector_state)) {
10498 ret = PTR_ERR(connector_state);
10499 goto fail;
10500 }
10501
10502 connector_state->crtc = crtc;
10503 connector_state->best_encoder = &intel_encoder->base;
10504
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010505 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10506 if (IS_ERR(crtc_state)) {
10507 ret = PTR_ERR(crtc_state);
10508 goto fail;
10509 }
10510
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010511 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010512
Chris Wilson64927112011-04-20 07:25:26 +010010513 if (!mode)
10514 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010515
Chris Wilsond2dff872011-04-19 08:36:26 +010010516 /* We need a framebuffer large enough to accommodate all accesses
10517 * that the plane may generate whilst we perform load detection.
10518 * We can not rely on the fbcon either being present (we get called
10519 * during its initialisation to detect all boot displays, or it may
10520 * not even exist) or that it is large enough to satisfy the
10521 * requested mode.
10522 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010523 fb = mode_fits_in_fbdev(dev, mode);
10524 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010525 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010526 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10527 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010528 } else
10529 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010530 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010531 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010532 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010533 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010534
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010535 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10536 if (ret)
10537 goto fail;
10538
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010539 drm_mode_copy(&crtc_state->base.mode, mode);
10540
10541 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010542 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010543 if (old->release_fb)
10544 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010545 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010546 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010547 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010548
Jesse Barnes79e53942008-11-07 14:24:08 -080010549 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010550 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010551 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010552
10553 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010554 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010555fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010556 drm_atomic_state_free(state);
10557 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010558
Rob Clark51fd3712013-11-19 12:10:12 -050010559 if (ret == -EDEADLK) {
10560 drm_modeset_backoff(ctx);
10561 goto retry;
10562 }
10563
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010564 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010565}
10566
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010567void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010568 struct intel_load_detect_pipe *old,
10569 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010570{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010571 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010572 struct intel_encoder *intel_encoder =
10573 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010574 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010575 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010576 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010577 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010578 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010579 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010580 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010581
Chris Wilsond2dff872011-04-19 08:36:26 +010010582 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010583 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010584 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010585
Chris Wilson8261b192011-04-19 23:18:09 +010010586 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010587 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010588 if (!state)
10589 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010590
10591 state->acquire_ctx = ctx;
10592
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010593 connector_state = drm_atomic_get_connector_state(state, connector);
10594 if (IS_ERR(connector_state))
10595 goto fail;
10596
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010597 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10598 if (IS_ERR(crtc_state))
10599 goto fail;
10600
Daniel Vetterfc303102012-07-09 10:40:58 +020010601 to_intel_connector(connector)->new_encoder = NULL;
10602 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010603 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010604
10605 connector_state->best_encoder = NULL;
10606 connector_state->crtc = NULL;
10607
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010608 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010609
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010610 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10611 0, 0);
10612 if (ret)
10613 goto fail;
10614
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010615 ret = intel_set_mode(crtc, state);
10616 if (ret)
10617 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010618
Daniel Vetter36206362012-12-10 20:42:17 +010010619 if (old->release_fb) {
10620 drm_framebuffer_unregister_private(old->release_fb);
10621 drm_framebuffer_unreference(old->release_fb);
10622 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010623
Chris Wilson0622a532011-04-21 09:32:11 +010010624 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010625 }
10626
Eric Anholtc751ce42010-03-25 11:48:48 -070010627 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010628 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10629 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010630
10631 return;
10632fail:
10633 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10634 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010635}
10636
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010637static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010638 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010639{
10640 struct drm_i915_private *dev_priv = dev->dev_private;
10641 u32 dpll = pipe_config->dpll_hw_state.dpll;
10642
10643 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010644 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010645 else if (HAS_PCH_SPLIT(dev))
10646 return 120000;
10647 else if (!IS_GEN2(dev))
10648 return 96000;
10649 else
10650 return 48000;
10651}
10652
Jesse Barnes79e53942008-11-07 14:24:08 -080010653/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010654static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010655 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010656{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010657 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010658 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010659 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010660 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010661 u32 fp;
10662 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010663 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010664
10665 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010666 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010667 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010668 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010669
10670 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010671 if (IS_PINEVIEW(dev)) {
10672 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10673 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010674 } else {
10675 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10676 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10677 }
10678
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010679 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010680 if (IS_PINEVIEW(dev))
10681 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10682 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010683 else
10684 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010685 DPLL_FPA01_P1_POST_DIV_SHIFT);
10686
10687 switch (dpll & DPLL_MODE_MASK) {
10688 case DPLLB_MODE_DAC_SERIAL:
10689 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10690 5 : 10;
10691 break;
10692 case DPLLB_MODE_LVDS:
10693 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10694 7 : 14;
10695 break;
10696 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010697 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010698 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010699 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010700 }
10701
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010702 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010703 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010704 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010705 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010706 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010707 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010708 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010709
10710 if (is_lvds) {
10711 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10712 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010713
10714 if (lvds & LVDS_CLKB_POWER_UP)
10715 clock.p2 = 7;
10716 else
10717 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010718 } else {
10719 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10720 clock.p1 = 2;
10721 else {
10722 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10723 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10724 }
10725 if (dpll & PLL_P2_DIVIDE_BY_4)
10726 clock.p2 = 4;
10727 else
10728 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010729 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010730
10731 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010732 }
10733
Ville Syrjälä18442d02013-09-13 16:00:08 +030010734 /*
10735 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010736 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010737 * encoder's get_config() function.
10738 */
10739 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010740}
10741
Ville Syrjälä6878da02013-09-13 15:59:11 +030010742int intel_dotclock_calculate(int link_freq,
10743 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010744{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010745 /*
10746 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010747 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010748 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010749 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010750 *
10751 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010752 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010753 */
10754
Ville Syrjälä6878da02013-09-13 15:59:11 +030010755 if (!m_n->link_n)
10756 return 0;
10757
10758 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10759}
10760
Ville Syrjälä18442d02013-09-13 16:00:08 +030010761static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010762 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010763{
10764 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010765
10766 /* read out port_clock from the DPLL */
10767 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010768
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010769 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010770 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010771 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010772 * agree once we know their relationship in the encoder's
10773 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010774 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010775 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010776 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10777 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010778}
10779
10780/** Returns the currently programmed mode of the given pipe. */
10781struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10782 struct drm_crtc *crtc)
10783{
Jesse Barnes548f2452011-02-17 10:40:53 -080010784 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010786 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010787 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010788 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010789 int htot = I915_READ(HTOTAL(cpu_transcoder));
10790 int hsync = I915_READ(HSYNC(cpu_transcoder));
10791 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10792 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010793 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010794
10795 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10796 if (!mode)
10797 return NULL;
10798
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010799 /*
10800 * Construct a pipe_config sufficient for getting the clock info
10801 * back out of crtc_clock_get.
10802 *
10803 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10804 * to use a real value here instead.
10805 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010806 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010807 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010808 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10809 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10810 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010811 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10812
Ville Syrjälä773ae032013-09-23 17:48:20 +030010813 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010814 mode->hdisplay = (htot & 0xffff) + 1;
10815 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10816 mode->hsync_start = (hsync & 0xffff) + 1;
10817 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10818 mode->vdisplay = (vtot & 0xffff) + 1;
10819 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10820 mode->vsync_start = (vsync & 0xffff) + 1;
10821 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10822
10823 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010824
10825 return mode;
10826}
10827
Jesse Barnes652c3932009-08-17 13:31:43 -070010828static void intel_decrease_pllclock(struct drm_crtc *crtc)
10829{
10830 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010831 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010832 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010833
Sonika Jindalbaff2962014-07-22 11:16:35 +053010834 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010835 return;
10836
10837 if (!dev_priv->lvds_downclock_avail)
10838 return;
10839
10840 /*
10841 * Since this is called by a timer, we should never get here in
10842 * the manual case.
10843 */
10844 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010845 int pipe = intel_crtc->pipe;
10846 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010847 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010848
Zhao Yakui44d98a62009-10-09 11:39:40 +080010849 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010850
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010851 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010852
Chris Wilson074b5e12012-05-02 12:07:06 +010010853 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010854 dpll |= DISPLAY_RATE_SELECT_FPA1;
10855 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010856 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010857 dpll = I915_READ(dpll_reg);
10858 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010859 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010860 }
10861
10862}
10863
Chris Wilsonf047e392012-07-21 12:31:41 +010010864void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010865{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010866 struct drm_i915_private *dev_priv = dev->dev_private;
10867
Chris Wilsonf62a0072014-02-21 17:55:39 +000010868 if (dev_priv->mm.busy)
10869 return;
10870
Paulo Zanoni43694d62014-03-07 20:08:08 -030010871 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010872 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010873 if (INTEL_INFO(dev)->gen >= 6)
10874 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010875 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010876}
10877
10878void intel_mark_idle(struct drm_device *dev)
10879{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010880 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010881 struct drm_crtc *crtc;
10882
Chris Wilsonf62a0072014-02-21 17:55:39 +000010883 if (!dev_priv->mm.busy)
10884 return;
10885
10886 dev_priv->mm.busy = false;
10887
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010888 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010889 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010890 continue;
10891
10892 intel_decrease_pllclock(crtc);
10893 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010894
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010895 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010896 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010897
Paulo Zanoni43694d62014-03-07 20:08:08 -030010898 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010899}
10900
Jesse Barnes79e53942008-11-07 14:24:08 -080010901static void intel_crtc_destroy(struct drm_crtc *crtc)
10902{
10903 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010904 struct drm_device *dev = crtc->dev;
10905 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010906
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010907 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010908 work = intel_crtc->unpin_work;
10909 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010910 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010911
10912 if (work) {
10913 cancel_work_sync(&work->work);
10914 kfree(work);
10915 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010916
10917 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010918
Jesse Barnes79e53942008-11-07 14:24:08 -080010919 kfree(intel_crtc);
10920}
10921
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010922static void intel_unpin_work_fn(struct work_struct *__work)
10923{
10924 struct intel_unpin_work *work =
10925 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010926 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010927 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010928
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010929 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010930 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010931 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010932
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010933 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010934
10935 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010936 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010937 mutex_unlock(&dev->struct_mutex);
10938
Daniel Vetterf99d7062014-06-19 16:01:59 +020010939 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010940 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010941
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010942 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10943 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10944
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010945 kfree(work);
10946}
10947
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010948static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010949 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010950{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010951 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10952 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010953 unsigned long flags;
10954
10955 /* Ignore early vblank irqs */
10956 if (intel_crtc == NULL)
10957 return;
10958
Daniel Vetterf3260382014-09-15 14:55:23 +020010959 /*
10960 * This is called both by irq handlers and the reset code (to complete
10961 * lost pageflips) so needs the full irqsave spinlocks.
10962 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010963 spin_lock_irqsave(&dev->event_lock, flags);
10964 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010965
10966 /* Ensure we don't miss a work->pending update ... */
10967 smp_rmb();
10968
10969 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010970 spin_unlock_irqrestore(&dev->event_lock, flags);
10971 return;
10972 }
10973
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010974 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010975
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010976 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010977}
10978
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010979void intel_finish_page_flip(struct drm_device *dev, int pipe)
10980{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010981 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010982 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10983
Mario Kleiner49b14a52010-12-09 07:00:07 +010010984 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010985}
10986
10987void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10988{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010989 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010990 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10991
Mario Kleiner49b14a52010-12-09 07:00:07 +010010992 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010993}
10994
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010995/* Is 'a' after or equal to 'b'? */
10996static bool g4x_flip_count_after_eq(u32 a, u32 b)
10997{
10998 return !((a - b) & 0x80000000);
10999}
11000
11001static bool page_flip_finished(struct intel_crtc *crtc)
11002{
11003 struct drm_device *dev = crtc->base.dev;
11004 struct drm_i915_private *dev_priv = dev->dev_private;
11005
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030011006 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
11007 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
11008 return true;
11009
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011010 /*
11011 * The relevant registers doen't exist on pre-ctg.
11012 * As the flip done interrupt doesn't trigger for mmio
11013 * flips on gmch platforms, a flip count check isn't
11014 * really needed there. But since ctg has the registers,
11015 * include it in the check anyway.
11016 */
11017 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
11018 return true;
11019
11020 /*
11021 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
11022 * used the same base address. In that case the mmio flip might
11023 * have completed, but the CS hasn't even executed the flip yet.
11024 *
11025 * A flip count check isn't enough as the CS might have updated
11026 * the base address just after start of vblank, but before we
11027 * managed to process the interrupt. This means we'd complete the
11028 * CS flip too soon.
11029 *
11030 * Combining both checks should get us a good enough result. It may
11031 * still happen that the CS flip has been executed, but has not
11032 * yet actually completed. But in case the base address is the same
11033 * anyway, we don't really care.
11034 */
11035 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
11036 crtc->unpin_work->gtt_offset &&
11037 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
11038 crtc->unpin_work->flip_count);
11039}
11040
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011041void intel_prepare_page_flip(struct drm_device *dev, int plane)
11042{
Jani Nikulafbee40d2014-03-31 14:27:18 +030011043 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011044 struct intel_crtc *intel_crtc =
11045 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11046 unsigned long flags;
11047
Daniel Vetterf3260382014-09-15 14:55:23 +020011048
11049 /*
11050 * This is called both by irq handlers and the reset code (to complete
11051 * lost pageflips) so needs the full irqsave spinlocks.
11052 *
11053 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000011054 * generate a page-flip completion irq, i.e. every modeset
11055 * is also accompanied by a spurious intel_prepare_page_flip().
11056 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011057 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011058 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011059 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011060 spin_unlock_irqrestore(&dev->event_lock, flags);
11061}
11062
Robin Schroereba905b2014-05-18 02:24:50 +020011063static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011064{
11065 /* Ensure that the work item is consistent when activating it ... */
11066 smp_wmb();
11067 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
11068 /* and that it is marked active as soon as the irq could fire. */
11069 smp_wmb();
11070}
11071
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011072static int intel_gen2_queue_flip(struct drm_device *dev,
11073 struct drm_crtc *crtc,
11074 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011075 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011076 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011077 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011078{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011079 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011080 u32 flip_mask;
11081 int ret;
11082
Daniel Vetter6d90c952012-04-26 23:28:05 +020011083 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011084 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011085 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011086
11087 /* Can't queue multiple flips, so wait for the previous
11088 * one to finish before executing the next.
11089 */
11090 if (intel_crtc->plane)
11091 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11092 else
11093 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011094 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11095 intel_ring_emit(ring, MI_NOOP);
11096 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11097 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11098 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011099 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011100 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011101
11102 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011103 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011104 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011105}
11106
11107static int intel_gen3_queue_flip(struct drm_device *dev,
11108 struct drm_crtc *crtc,
11109 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011110 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011111 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011112 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011113{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011114 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011115 u32 flip_mask;
11116 int ret;
11117
Daniel Vetter6d90c952012-04-26 23:28:05 +020011118 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011119 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011120 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011121
11122 if (intel_crtc->plane)
11123 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11124 else
11125 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011126 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11127 intel_ring_emit(ring, MI_NOOP);
11128 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11129 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11130 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011131 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011132 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011133
Chris Wilsone7d841c2012-12-03 11:36:30 +000011134 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011135 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011136 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011137}
11138
11139static int intel_gen4_queue_flip(struct drm_device *dev,
11140 struct drm_crtc *crtc,
11141 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011142 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011143 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011144 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011145{
11146 struct drm_i915_private *dev_priv = dev->dev_private;
11147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11148 uint32_t pf, pipesrc;
11149 int ret;
11150
Daniel Vetter6d90c952012-04-26 23:28:05 +020011151 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011152 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011153 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011154
11155 /* i965+ uses the linear or tiled offsets from the
11156 * Display Registers (which do not change across a page-flip)
11157 * so we need only reprogram the base address.
11158 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011159 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11160 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11161 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011162 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011163 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011164
11165 /* XXX Enabling the panel-fitter across page-flip is so far
11166 * untested on non-native modes, so ignore it for now.
11167 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11168 */
11169 pf = 0;
11170 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011171 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011172
11173 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011174 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011175 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011176}
11177
11178static int intel_gen6_queue_flip(struct drm_device *dev,
11179 struct drm_crtc *crtc,
11180 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011181 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011182 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011183 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011184{
11185 struct drm_i915_private *dev_priv = dev->dev_private;
11186 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11187 uint32_t pf, pipesrc;
11188 int ret;
11189
Daniel Vetter6d90c952012-04-26 23:28:05 +020011190 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011191 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011192 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011193
Daniel Vetter6d90c952012-04-26 23:28:05 +020011194 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11195 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11196 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011197 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011198
Chris Wilson99d9acd2012-04-17 20:37:00 +010011199 /* Contrary to the suggestions in the documentation,
11200 * "Enable Panel Fitter" does not seem to be required when page
11201 * flipping with a non-native mode, and worse causes a normal
11202 * modeset to fail.
11203 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11204 */
11205 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011206 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011207 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011208
11209 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011210 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011211 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011212}
11213
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011214static int intel_gen7_queue_flip(struct drm_device *dev,
11215 struct drm_crtc *crtc,
11216 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011217 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011218 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011219 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011220{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011221 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011222 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011223 int len, ret;
11224
Robin Schroereba905b2014-05-18 02:24:50 +020011225 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011226 case PLANE_A:
11227 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11228 break;
11229 case PLANE_B:
11230 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11231 break;
11232 case PLANE_C:
11233 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11234 break;
11235 default:
11236 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011237 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011238 }
11239
Chris Wilsonffe74d72013-08-26 20:58:12 +010011240 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011241 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011242 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011243 /*
11244 * On Gen 8, SRM is now taking an extra dword to accommodate
11245 * 48bits addresses, and we need a NOOP for the batch size to
11246 * stay even.
11247 */
11248 if (IS_GEN8(dev))
11249 len += 2;
11250 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011251
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011252 /*
11253 * BSpec MI_DISPLAY_FLIP for IVB:
11254 * "The full packet must be contained within the same cache line."
11255 *
11256 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11257 * cacheline, if we ever start emitting more commands before
11258 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11259 * then do the cacheline alignment, and finally emit the
11260 * MI_DISPLAY_FLIP.
11261 */
11262 ret = intel_ring_cacheline_align(ring);
11263 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011264 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011265
Chris Wilsonffe74d72013-08-26 20:58:12 +010011266 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011267 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011268 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011269
Chris Wilsonffe74d72013-08-26 20:58:12 +010011270 /* Unmask the flip-done completion message. Note that the bspec says that
11271 * we should do this for both the BCS and RCS, and that we must not unmask
11272 * more than one flip event at any time (or ensure that one flip message
11273 * can be sent by waiting for flip-done prior to queueing new flips).
11274 * Experimentation says that BCS works despite DERRMR masking all
11275 * flip-done completion events and that unmasking all planes at once
11276 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11277 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11278 */
11279 if (ring->id == RCS) {
11280 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11281 intel_ring_emit(ring, DERRMR);
11282 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11283 DERRMR_PIPEB_PRI_FLIP_DONE |
11284 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011285 if (IS_GEN8(dev))
11286 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11287 MI_SRM_LRM_GLOBAL_GTT);
11288 else
11289 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11290 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011291 intel_ring_emit(ring, DERRMR);
11292 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011293 if (IS_GEN8(dev)) {
11294 intel_ring_emit(ring, 0);
11295 intel_ring_emit(ring, MI_NOOP);
11296 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011297 }
11298
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011299 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011300 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011301 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011302 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011303
11304 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011305 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011306 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011307}
11308
Sourab Gupta84c33a62014-06-02 16:47:17 +053011309static bool use_mmio_flip(struct intel_engine_cs *ring,
11310 struct drm_i915_gem_object *obj)
11311{
11312 /*
11313 * This is not being used for older platforms, because
11314 * non-availability of flip done interrupt forces us to use
11315 * CS flips. Older platforms derive flip done using some clever
11316 * tricks involving the flip_pending status bits and vblank irqs.
11317 * So using MMIO flips there would disrupt this mechanism.
11318 */
11319
Chris Wilson8e09bf82014-07-08 10:40:30 +010011320 if (ring == NULL)
11321 return true;
11322
Sourab Gupta84c33a62014-06-02 16:47:17 +053011323 if (INTEL_INFO(ring->dev)->gen < 5)
11324 return false;
11325
11326 if (i915.use_mmio_flip < 0)
11327 return false;
11328 else if (i915.use_mmio_flip > 0)
11329 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011330 else if (i915.enable_execlists)
11331 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011332 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011333 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011334}
11335
Damien Lespiauff944562014-11-20 14:58:16 +000011336static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11337{
11338 struct drm_device *dev = intel_crtc->base.dev;
11339 struct drm_i915_private *dev_priv = dev->dev_private;
11340 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011341 const enum pipe pipe = intel_crtc->pipe;
11342 u32 ctl, stride;
11343
11344 ctl = I915_READ(PLANE_CTL(pipe, 0));
11345 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011346 switch (fb->modifier[0]) {
11347 case DRM_FORMAT_MOD_NONE:
11348 break;
11349 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011350 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011351 break;
11352 case I915_FORMAT_MOD_Y_TILED:
11353 ctl |= PLANE_CTL_TILED_Y;
11354 break;
11355 case I915_FORMAT_MOD_Yf_TILED:
11356 ctl |= PLANE_CTL_TILED_YF;
11357 break;
11358 default:
11359 MISSING_CASE(fb->modifier[0]);
11360 }
Damien Lespiauff944562014-11-20 14:58:16 +000011361
11362 /*
11363 * The stride is either expressed as a multiple of 64 bytes chunks for
11364 * linear buffers or in number of tiles for tiled buffers.
11365 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011366 stride = fb->pitches[0] /
11367 intel_fb_stride_alignment(dev, fb->modifier[0],
11368 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011369
11370 /*
11371 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11372 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11373 */
11374 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11375 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11376
11377 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11378 POSTING_READ(PLANE_SURF(pipe, 0));
11379}
11380
11381static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011382{
11383 struct drm_device *dev = intel_crtc->base.dev;
11384 struct drm_i915_private *dev_priv = dev->dev_private;
11385 struct intel_framebuffer *intel_fb =
11386 to_intel_framebuffer(intel_crtc->base.primary->fb);
11387 struct drm_i915_gem_object *obj = intel_fb->obj;
11388 u32 dspcntr;
11389 u32 reg;
11390
Sourab Gupta84c33a62014-06-02 16:47:17 +053011391 reg = DSPCNTR(intel_crtc->plane);
11392 dspcntr = I915_READ(reg);
11393
Damien Lespiauc5d97472014-10-25 00:11:11 +010011394 if (obj->tiling_mode != I915_TILING_NONE)
11395 dspcntr |= DISPPLANE_TILED;
11396 else
11397 dspcntr &= ~DISPPLANE_TILED;
11398
Sourab Gupta84c33a62014-06-02 16:47:17 +053011399 I915_WRITE(reg, dspcntr);
11400
11401 I915_WRITE(DSPSURF(intel_crtc->plane),
11402 intel_crtc->unpin_work->gtt_offset);
11403 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011404
Damien Lespiauff944562014-11-20 14:58:16 +000011405}
11406
11407/*
11408 * XXX: This is the temporary way to update the plane registers until we get
11409 * around to using the usual plane update functions for MMIO flips
11410 */
11411static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11412{
11413 struct drm_device *dev = intel_crtc->base.dev;
11414 bool atomic_update;
11415 u32 start_vbl_count;
11416
11417 intel_mark_page_flip_active(intel_crtc);
11418
11419 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11420
11421 if (INTEL_INFO(dev)->gen >= 9)
11422 skl_do_mmio_flip(intel_crtc);
11423 else
11424 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11425 ilk_do_mmio_flip(intel_crtc);
11426
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011427 if (atomic_update)
11428 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011429}
11430
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011431static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011432{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011433 struct intel_mmio_flip *mmio_flip =
11434 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011435
Daniel Vettereed29a52015-05-21 14:21:25 +020011436 if (mmio_flip->req)
11437 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011438 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011439 false, NULL,
11440 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011441
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011442 intel_do_mmio_flip(mmio_flip->crtc);
11443
Daniel Vettereed29a52015-05-21 14:21:25 +020011444 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011445 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011446}
11447
11448static int intel_queue_mmio_flip(struct drm_device *dev,
11449 struct drm_crtc *crtc,
11450 struct drm_framebuffer *fb,
11451 struct drm_i915_gem_object *obj,
11452 struct intel_engine_cs *ring,
11453 uint32_t flags)
11454{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011455 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011456
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011457 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11458 if (mmio_flip == NULL)
11459 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011460
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011461 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011462 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011463 mmio_flip->crtc = to_intel_crtc(crtc);
11464
11465 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11466 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011467
Sourab Gupta84c33a62014-06-02 16:47:17 +053011468 return 0;
11469}
11470
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011471static int intel_default_queue_flip(struct drm_device *dev,
11472 struct drm_crtc *crtc,
11473 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011474 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011475 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011476 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011477{
11478 return -ENODEV;
11479}
11480
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011481static bool __intel_pageflip_stall_check(struct drm_device *dev,
11482 struct drm_crtc *crtc)
11483{
11484 struct drm_i915_private *dev_priv = dev->dev_private;
11485 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11486 struct intel_unpin_work *work = intel_crtc->unpin_work;
11487 u32 addr;
11488
11489 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11490 return true;
11491
11492 if (!work->enable_stall_check)
11493 return false;
11494
11495 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011496 if (work->flip_queued_req &&
11497 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011498 return false;
11499
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011500 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011501 }
11502
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011503 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011504 return false;
11505
11506 /* Potential stall - if we see that the flip has happened,
11507 * assume a missed interrupt. */
11508 if (INTEL_INFO(dev)->gen >= 4)
11509 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11510 else
11511 addr = I915_READ(DSPADDR(intel_crtc->plane));
11512
11513 /* There is a potential issue here with a false positive after a flip
11514 * to the same address. We could address this by checking for a
11515 * non-incrementing frame counter.
11516 */
11517 return addr == work->gtt_offset;
11518}
11519
11520void intel_check_page_flip(struct drm_device *dev, int pipe)
11521{
11522 struct drm_i915_private *dev_priv = dev->dev_private;
11523 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11524 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011525 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011526
Dave Gordon6c51d462015-03-06 15:34:26 +000011527 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011528
11529 if (crtc == NULL)
11530 return;
11531
Daniel Vetterf3260382014-09-15 14:55:23 +020011532 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011533 work = intel_crtc->unpin_work;
11534 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011535 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011536 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011537 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011538 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011539 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011540 if (work != NULL &&
11541 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11542 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011543 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011544}
11545
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011546static int intel_crtc_page_flip(struct drm_crtc *crtc,
11547 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011548 struct drm_pending_vblank_event *event,
11549 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011550{
11551 struct drm_device *dev = crtc->dev;
11552 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011553 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011554 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011555 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011556 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011557 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011558 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011559 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011560 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011561 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011562
Matt Roper2ff8fde2014-07-08 07:50:07 -070011563 /*
11564 * drm_mode_page_flip_ioctl() should already catch this, but double
11565 * check to be safe. In the future we may enable pageflipping from
11566 * a disabled primary plane.
11567 */
11568 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11569 return -EBUSY;
11570
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011571 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011572 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011573 return -EINVAL;
11574
11575 /*
11576 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11577 * Note that pitch changes could also affect these register.
11578 */
11579 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011580 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11581 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011582 return -EINVAL;
11583
Chris Wilsonf900db42014-02-20 09:26:13 +000011584 if (i915_terminally_wedged(&dev_priv->gpu_error))
11585 goto out_hang;
11586
Daniel Vetterb14c5672013-09-19 12:18:32 +020011587 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011588 if (work == NULL)
11589 return -ENOMEM;
11590
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011591 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011592 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011593 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011594 INIT_WORK(&work->work, intel_unpin_work_fn);
11595
Daniel Vetter87b6b102014-05-15 15:33:46 +020011596 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011597 if (ret)
11598 goto free_work;
11599
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011600 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011601 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011602 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011603 /* Before declaring the flip queue wedged, check if
11604 * the hardware completed the operation behind our backs.
11605 */
11606 if (__intel_pageflip_stall_check(dev, crtc)) {
11607 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11608 page_flip_completed(intel_crtc);
11609 } else {
11610 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011611 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011612
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011613 drm_crtc_vblank_put(crtc);
11614 kfree(work);
11615 return -EBUSY;
11616 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011617 }
11618 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011619 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011620
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011621 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11622 flush_workqueue(dev_priv->wq);
11623
Jesse Barnes75dfca82010-02-10 15:09:44 -080011624 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011625 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011626 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011627
Matt Roperf4510a22014-04-01 15:22:40 -070011628 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011629 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011630
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011631 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011632
Chris Wilson89ed88b2015-02-16 14:31:49 +000011633 ret = i915_mutex_lock_interruptible(dev);
11634 if (ret)
11635 goto cleanup;
11636
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011637 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011638 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011639
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011640 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011641 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011642
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011643 if (IS_VALLEYVIEW(dev)) {
11644 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011645 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011646 /* vlv: DISPLAY_FLIP fails to change tiling */
11647 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011648 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011649 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011650 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011651 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011652 if (ring == NULL || ring->id != RCS)
11653 ring = &dev_priv->ring[BCS];
11654 } else {
11655 ring = &dev_priv->ring[RCS];
11656 }
11657
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011658 mmio_flip = use_mmio_flip(ring, obj);
11659
11660 /* When using CS flips, we want to emit semaphores between rings.
11661 * However, when using mmio flips we will create a task to do the
11662 * synchronisation, so all we want here is to pin the framebuffer
11663 * into the display plane and skip any waits.
11664 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011665 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011666 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011667 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011668 if (ret)
11669 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011670
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011671 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11672 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011673
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011674 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011675 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11676 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011677 if (ret)
11678 goto cleanup_unpin;
11679
John Harrisonf06cc1b2014-11-24 18:49:37 +000011680 i915_gem_request_assign(&work->flip_queued_req,
11681 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011682 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011683 if (obj->last_write_req) {
11684 ret = i915_gem_check_olr(obj->last_write_req);
11685 if (ret)
11686 goto cleanup_unpin;
11687 }
11688
Sourab Gupta84c33a62014-06-02 16:47:17 +053011689 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011690 page_flip_flags);
11691 if (ret)
11692 goto cleanup_unpin;
11693
John Harrisonf06cc1b2014-11-24 18:49:37 +000011694 i915_gem_request_assign(&work->flip_queued_req,
11695 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011696 }
11697
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011698 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011699 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011700
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011701 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011702 INTEL_FRONTBUFFER_PRIMARY(pipe));
11703
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011704 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011705 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011706 mutex_unlock(&dev->struct_mutex);
11707
Jesse Barnese5510fa2010-07-01 16:48:37 -070011708 trace_i915_flip_request(intel_crtc->plane, obj);
11709
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011710 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011711
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011712cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011713 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011714cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011715 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011716 mutex_unlock(&dev->struct_mutex);
11717cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011718 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011719 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011720
Chris Wilson89ed88b2015-02-16 14:31:49 +000011721 drm_gem_object_unreference_unlocked(&obj->base);
11722 drm_framebuffer_unreference(work->old_fb);
11723
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011724 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011725 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011726 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011727
Daniel Vetter87b6b102014-05-15 15:33:46 +020011728 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011729free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011730 kfree(work);
11731
Chris Wilsonf900db42014-02-20 09:26:13 +000011732 if (ret == -EIO) {
11733out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011734 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011735 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011736 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011737 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011738 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011739 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011740 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011741 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011742}
11743
Jani Nikula65b38e02015-04-13 11:26:56 +030011744static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011745 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11746 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011747 .atomic_begin = intel_begin_crtc_commit,
11748 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011749};
11750
Daniel Vetter9a935852012-07-05 22:34:27 +020011751/**
11752 * intel_modeset_update_staged_output_state
11753 *
11754 * Updates the staged output configuration state, e.g. after we've read out the
11755 * current hw state.
11756 */
11757static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11758{
Ville Syrjälä76688512014-01-10 11:28:06 +020011759 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011760 struct intel_encoder *encoder;
11761 struct intel_connector *connector;
11762
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011763 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011764 connector->new_encoder =
11765 to_intel_encoder(connector->base.encoder);
11766 }
11767
Damien Lespiaub2784e12014-08-05 11:29:37 +010011768 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011769 encoder->new_crtc =
11770 to_intel_crtc(encoder->base.crtc);
11771 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011772
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011773 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011774 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011775 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011776}
11777
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011778/* Transitional helper to copy current connector/encoder state to
11779 * connector->state. This is needed so that code that is partially
11780 * converted to atomic does the right thing.
11781 */
11782static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11783{
11784 struct intel_connector *connector;
11785
11786 for_each_intel_connector(dev, connector) {
11787 if (connector->base.encoder) {
11788 connector->base.state->best_encoder =
11789 connector->base.encoder;
11790 connector->base.state->crtc =
11791 connector->base.encoder->crtc;
11792 } else {
11793 connector->base.state->best_encoder = NULL;
11794 connector->base.state->crtc = NULL;
11795 }
11796 }
11797}
11798
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011799/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011800 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011801static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011802{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011803 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011804 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011805 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011806
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011807 for_each_intel_connector(state->dev, connector) {
11808 connector->base.encoder = connector->base.state->best_encoder;
11809 if (connector->base.encoder)
11810 connector->base.encoder->crtc =
11811 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011812 }
11813
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011814 /* Update crtc of disabled encoders */
11815 for_each_intel_encoder(state->dev, encoder) {
11816 int num_connectors = 0;
11817
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011818 for_each_intel_connector(state->dev, connector)
11819 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011820 num_connectors++;
11821
11822 if (num_connectors == 0)
11823 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011824 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011825
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011826 for_each_intel_crtc(state->dev, crtc) {
11827 crtc->base.enabled = crtc->base.state->enable;
11828 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011829 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011830
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011831 /* Copy the new configuration to the staged state, to keep the few
11832 * pieces of code that haven't been converted yet happy */
11833 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011834}
11835
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011836static void
Robin Schroereba905b2014-05-18 02:24:50 +020011837connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011838 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011839{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011840 int bpp = pipe_config->pipe_bpp;
11841
11842 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11843 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011844 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011845
11846 /* Don't use an invalid EDID bpc value */
11847 if (connector->base.display_info.bpc &&
11848 connector->base.display_info.bpc * 3 < bpp) {
11849 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11850 bpp, connector->base.display_info.bpc*3);
11851 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11852 }
11853
11854 /* Clamp bpp to 8 on screens without EDID 1.4 */
11855 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11856 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11857 bpp);
11858 pipe_config->pipe_bpp = 24;
11859 }
11860}
11861
11862static int
11863compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011864 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011865{
11866 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011867 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011868 struct drm_connector *connector;
11869 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011870 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011871
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011872 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011873 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011874 else if (INTEL_INFO(dev)->gen >= 5)
11875 bpp = 12*3;
11876 else
11877 bpp = 8*3;
11878
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011879
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011880 pipe_config->pipe_bpp = bpp;
11881
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011882 state = pipe_config->base.state;
11883
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011884 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011885 for_each_connector_in_state(state, connector, connector_state, i) {
11886 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011887 continue;
11888
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011889 connected_sink_compute_bpp(to_intel_connector(connector),
11890 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011891 }
11892
11893 return bpp;
11894}
11895
Daniel Vetter644db712013-09-19 14:53:58 +020011896static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11897{
11898 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11899 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011900 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011901 mode->crtc_hdisplay, mode->crtc_hsync_start,
11902 mode->crtc_hsync_end, mode->crtc_htotal,
11903 mode->crtc_vdisplay, mode->crtc_vsync_start,
11904 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11905}
11906
Daniel Vetterc0b03412013-05-28 12:05:54 +020011907static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011908 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011909 const char *context)
11910{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011911 struct drm_device *dev = crtc->base.dev;
11912 struct drm_plane *plane;
11913 struct intel_plane *intel_plane;
11914 struct intel_plane_state *state;
11915 struct drm_framebuffer *fb;
11916
11917 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11918 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011919
11920 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11921 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11922 pipe_config->pipe_bpp, pipe_config->dither);
11923 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11924 pipe_config->has_pch_encoder,
11925 pipe_config->fdi_lanes,
11926 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11927 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11928 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011929 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11930 pipe_config->has_dp_encoder,
11931 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11932 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11933 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011934
11935 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11936 pipe_config->has_dp_encoder,
11937 pipe_config->dp_m2_n2.gmch_m,
11938 pipe_config->dp_m2_n2.gmch_n,
11939 pipe_config->dp_m2_n2.link_m,
11940 pipe_config->dp_m2_n2.link_n,
11941 pipe_config->dp_m2_n2.tu);
11942
Daniel Vetter55072d12014-11-20 16:10:28 +010011943 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11944 pipe_config->has_audio,
11945 pipe_config->has_infoframe);
11946
Daniel Vetterc0b03412013-05-28 12:05:54 +020011947 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011948 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011949 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011950 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11951 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011952 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011953 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11954 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011955 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11956 crtc->num_scalers,
11957 pipe_config->scaler_state.scaler_users,
11958 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011959 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11960 pipe_config->gmch_pfit.control,
11961 pipe_config->gmch_pfit.pgm_ratios,
11962 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011963 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011964 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011965 pipe_config->pch_pfit.size,
11966 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011967 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011968 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011969
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011970 if (IS_BROXTON(dev)) {
11971 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11972 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11973 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11974 pipe_config->ddi_pll_sel,
11975 pipe_config->dpll_hw_state.ebb0,
11976 pipe_config->dpll_hw_state.pll0,
11977 pipe_config->dpll_hw_state.pll1,
11978 pipe_config->dpll_hw_state.pll2,
11979 pipe_config->dpll_hw_state.pll3,
11980 pipe_config->dpll_hw_state.pll6,
11981 pipe_config->dpll_hw_state.pll8,
11982 pipe_config->dpll_hw_state.pcsdw12);
11983 } else if (IS_SKYLAKE(dev)) {
11984 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11985 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11986 pipe_config->ddi_pll_sel,
11987 pipe_config->dpll_hw_state.ctrl1,
11988 pipe_config->dpll_hw_state.cfgcr1,
11989 pipe_config->dpll_hw_state.cfgcr2);
11990 } else if (HAS_DDI(dev)) {
11991 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11992 pipe_config->ddi_pll_sel,
11993 pipe_config->dpll_hw_state.wrpll);
11994 } else {
11995 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11996 "fp0: 0x%x, fp1: 0x%x\n",
11997 pipe_config->dpll_hw_state.dpll,
11998 pipe_config->dpll_hw_state.dpll_md,
11999 pipe_config->dpll_hw_state.fp0,
12000 pipe_config->dpll_hw_state.fp1);
12001 }
12002
Chandra Konduru6a60cd82015-04-07 15:28:40 -070012003 DRM_DEBUG_KMS("planes on this crtc\n");
12004 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
12005 intel_plane = to_intel_plane(plane);
12006 if (intel_plane->pipe != crtc->pipe)
12007 continue;
12008
12009 state = to_intel_plane_state(plane->state);
12010 fb = state->base.fb;
12011 if (!fb) {
12012 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
12013 "disabled, scaler_id = %d\n",
12014 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12015 plane->base.id, intel_plane->pipe,
12016 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
12017 drm_plane_index(plane), state->scaler_id);
12018 continue;
12019 }
12020
12021 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
12022 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
12023 plane->base.id, intel_plane->pipe,
12024 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
12025 drm_plane_index(plane));
12026 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
12027 fb->base.id, fb->width, fb->height, fb->pixel_format);
12028 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
12029 state->scaler_id,
12030 state->src.x1 >> 16, state->src.y1 >> 16,
12031 drm_rect_width(&state->src) >> 16,
12032 drm_rect_height(&state->src) >> 16,
12033 state->dst.x1, state->dst.y1,
12034 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
12035 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020012036}
12037
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012038static bool encoders_cloneable(const struct intel_encoder *a,
12039 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012040{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012041 /* masks could be asymmetric, so check both ways */
12042 return a == b || (a->cloneable & (1 << b->type) &&
12043 b->cloneable & (1 << a->type));
12044}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012045
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012046static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12047 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012048 struct intel_encoder *encoder)
12049{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012050 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012051 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012052 struct drm_connector_state *connector_state;
12053 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012054
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012055 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012056 if (connector_state->crtc != &crtc->base)
12057 continue;
12058
12059 source_encoder =
12060 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012061 if (!encoders_cloneable(encoder, source_encoder))
12062 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012063 }
12064
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012065 return true;
12066}
12067
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012068static bool check_encoder_cloning(struct drm_atomic_state *state,
12069 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012070{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012071 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012072 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012073 struct drm_connector_state *connector_state;
12074 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012075
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012076 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012077 if (connector_state->crtc != &crtc->base)
12078 continue;
12079
12080 encoder = to_intel_encoder(connector_state->best_encoder);
12081 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012082 return false;
12083 }
12084
12085 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012086}
12087
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012088static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012089{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012090 struct drm_device *dev = state->dev;
12091 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012092 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012093 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012094 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012095 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012096
12097 /*
12098 * Walk the connector list instead of the encoder
12099 * list to detect the problem on ddi platforms
12100 * where there's just one encoder per digital port.
12101 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012102 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012103 if (!connector_state->best_encoder)
12104 continue;
12105
12106 encoder = to_intel_encoder(connector_state->best_encoder);
12107
12108 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012109
12110 switch (encoder->type) {
12111 unsigned int port_mask;
12112 case INTEL_OUTPUT_UNKNOWN:
12113 if (WARN_ON(!HAS_DDI(dev)))
12114 break;
12115 case INTEL_OUTPUT_DISPLAYPORT:
12116 case INTEL_OUTPUT_HDMI:
12117 case INTEL_OUTPUT_EDP:
12118 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12119
12120 /* the same port mustn't appear more than once */
12121 if (used_ports & port_mask)
12122 return false;
12123
12124 used_ports |= port_mask;
12125 default:
12126 break;
12127 }
12128 }
12129
12130 return true;
12131}
12132
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012133static void
12134clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12135{
12136 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012137 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012138 struct intel_dpll_hw_state dpll_hw_state;
12139 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012140 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012141
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012142 /* FIXME: before the switch to atomic started, a new pipe_config was
12143 * kzalloc'd. Code that depends on any field being zero should be
12144 * fixed, so that the crtc_state can be safely duplicated. For now,
12145 * only fields that are know to not cause problems are preserved. */
12146
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012147 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012148 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012149 shared_dpll = crtc_state->shared_dpll;
12150 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012151 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012152
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012153 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012154
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012155 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012156 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012157 crtc_state->shared_dpll = shared_dpll;
12158 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012159 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012160}
12161
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012162static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012163intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012164 struct drm_atomic_state *state,
12165 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012166{
Daniel Vetter7758a112012-07-08 19:40:39 +020012167 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012168 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012169 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012170 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012171 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012172 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012173
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012174 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012175 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012176 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012177 }
12178
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012179 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012180 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012181 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012182 }
12183
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012184 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012185
Daniel Vettere143a212013-07-04 12:01:15 +020012186 pipe_config->cpu_transcoder =
12187 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012188
Imre Deak2960bc92013-07-30 13:36:32 +030012189 /*
12190 * Sanitize sync polarity flags based on requested ones. If neither
12191 * positive or negative polarity is requested, treat this as meaning
12192 * negative polarity.
12193 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012194 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012195 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012196 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012197
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012198 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012199 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012200 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012201
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012202 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12203 * plane pixel format and any sink constraints into account. Returns the
12204 * source plane bpp so that dithering can be selected on mismatches
12205 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012206 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12207 pipe_config);
12208 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012209 goto fail;
12210
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012211 /*
12212 * Determine the real pipe dimensions. Note that stereo modes can
12213 * increase the actual pipe size due to the frame doubling and
12214 * insertion of additional space for blanks between the frame. This
12215 * is stored in the crtc timings. We use the requested mode to do this
12216 * computation to clearly distinguish it from the adjusted mode, which
12217 * can be changed by the connectors in the below retry loop.
12218 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012219 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012220 &pipe_config->pipe_src_w,
12221 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012222
Daniel Vettere29c22c2013-02-21 00:00:16 +010012223encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012224 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012225 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012226 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012227
Daniel Vetter135c81b2013-07-21 21:37:09 +020012228 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012229 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12230 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012231
Daniel Vetter7758a112012-07-08 19:40:39 +020012232 /* Pass our mode to the connectors and the CRTC to give them a chance to
12233 * adjust it according to limitations or connector properties, and also
12234 * a chance to reject the mode entirely.
12235 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012236 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012237 if (connector_state->crtc != crtc)
12238 continue;
12239
12240 encoder = to_intel_encoder(connector_state->best_encoder);
12241
Daniel Vetterefea6e82013-07-21 21:36:59 +020012242 if (!(encoder->compute_config(encoder, pipe_config))) {
12243 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012244 goto fail;
12245 }
12246 }
12247
Daniel Vetterff9a6752013-06-01 17:16:21 +020012248 /* Set default port clock if not overwritten by the encoder. Needs to be
12249 * done afterwards in case the encoder adjusts the mode. */
12250 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012251 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012252 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012253
Daniel Vettera43f6e02013-06-07 23:10:32 +020012254 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012255 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012256 DRM_DEBUG_KMS("CRTC fixup failed\n");
12257 goto fail;
12258 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012259
12260 if (ret == RETRY) {
12261 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12262 ret = -EINVAL;
12263 goto fail;
12264 }
12265
12266 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12267 retry = false;
12268 goto encoder_retry;
12269 }
12270
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012271 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012272 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012273 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012274
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012275 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020012276fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012277 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012278}
12279
Daniel Vetterea9d7582012-07-10 10:42:52 +020012280static bool intel_crtc_in_use(struct drm_crtc *crtc)
12281{
12282 struct drm_encoder *encoder;
12283 struct drm_device *dev = crtc->dev;
12284
12285 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12286 if (encoder->crtc == crtc)
12287 return true;
12288
12289 return false;
12290}
12291
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012292static bool
12293needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012294{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012295 return state->mode_changed || state->active_changed;
12296}
12297
12298static void
12299intel_modeset_update_state(struct drm_atomic_state *state)
12300{
12301 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012302 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012303 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012304 struct drm_crtc *crtc;
12305 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012306 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012307 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012308
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012309 intel_shared_dpll_commit(dev_priv);
12310
Damien Lespiaub2784e12014-08-05 11:29:37 +010012311 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012312 if (!intel_encoder->base.crtc)
12313 continue;
12314
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012315 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12316 if (crtc != intel_encoder->base.crtc)
12317 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012318
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012319 if (crtc_state->enable && needs_modeset(crtc_state))
12320 intel_encoder->connectors_active = false;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012321
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012322 break;
12323 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012324 }
12325
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012326 drm_atomic_helper_swap_state(state->dev, state);
12327 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012328
Ville Syrjälä76688512014-01-10 11:28:06 +020012329 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012330 for_each_crtc(dev, crtc) {
12331 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020012332 }
12333
12334 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12335 if (!connector->encoder || !connector->encoder->crtc)
12336 continue;
12337
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012338 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12339 if (crtc != connector->encoder->crtc)
12340 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012341
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012342 if (crtc->state->enable && needs_modeset(crtc->state)) {
12343 struct drm_property *dpms_property =
12344 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012345
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012346 connector->dpms = DRM_MODE_DPMS_ON;
12347 drm_object_property_set_value(&connector->base,
12348 dpms_property,
12349 DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012350
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012351 intel_encoder = to_intel_encoder(connector->encoder);
12352 intel_encoder->connectors_active = true;
12353 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012354
Ander Conselvan de Oliveirabd4b4822015-05-29 14:28:09 +030012355 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012356 }
12357 }
12358
12359}
12360
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012361static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012362{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012363 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012364
12365 if (clock1 == clock2)
12366 return true;
12367
12368 if (!clock1 || !clock2)
12369 return false;
12370
12371 diff = abs(clock1 - clock2);
12372
12373 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12374 return true;
12375
12376 return false;
12377}
12378
Daniel Vetter25c5b262012-07-08 22:08:04 +020012379#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12380 list_for_each_entry((intel_crtc), \
12381 &(dev)->mode_config.crtc_list, \
12382 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012383 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012384
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012385static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012386intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012387 struct intel_crtc_state *current_config,
12388 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012389{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012390#define PIPE_CONF_CHECK_X(name) \
12391 if (current_config->name != pipe_config->name) { \
12392 DRM_ERROR("mismatch in " #name " " \
12393 "(expected 0x%08x, found 0x%08x)\n", \
12394 current_config->name, \
12395 pipe_config->name); \
12396 return false; \
12397 }
12398
Daniel Vetter08a24032013-04-19 11:25:34 +020012399#define PIPE_CONF_CHECK_I(name) \
12400 if (current_config->name != pipe_config->name) { \
12401 DRM_ERROR("mismatch in " #name " " \
12402 "(expected %i, found %i)\n", \
12403 current_config->name, \
12404 pipe_config->name); \
12405 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012406 }
12407
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012408/* This is required for BDW+ where there is only one set of registers for
12409 * switching between high and low RR.
12410 * This macro can be used whenever a comparison has to be made between one
12411 * hw state and multiple sw state variables.
12412 */
12413#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12414 if ((current_config->name != pipe_config->name) && \
12415 (current_config->alt_name != pipe_config->name)) { \
12416 DRM_ERROR("mismatch in " #name " " \
12417 "(expected %i or %i, found %i)\n", \
12418 current_config->name, \
12419 current_config->alt_name, \
12420 pipe_config->name); \
12421 return false; \
12422 }
12423
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012424#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12425 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012426 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012427 "(expected %i, found %i)\n", \
12428 current_config->name & (mask), \
12429 pipe_config->name & (mask)); \
12430 return false; \
12431 }
12432
Ville Syrjälä5e550652013-09-06 23:29:07 +030012433#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12434 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12435 DRM_ERROR("mismatch in " #name " " \
12436 "(expected %i, found %i)\n", \
12437 current_config->name, \
12438 pipe_config->name); \
12439 return false; \
12440 }
12441
Daniel Vetterbb760062013-06-06 14:55:52 +020012442#define PIPE_CONF_QUIRK(quirk) \
12443 ((current_config->quirks | pipe_config->quirks) & (quirk))
12444
Daniel Vettereccb1402013-05-22 00:50:22 +020012445 PIPE_CONF_CHECK_I(cpu_transcoder);
12446
Daniel Vetter08a24032013-04-19 11:25:34 +020012447 PIPE_CONF_CHECK_I(has_pch_encoder);
12448 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012449 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12450 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12451 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12452 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12453 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012454
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012455 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012456
12457 if (INTEL_INFO(dev)->gen < 8) {
12458 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12459 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12460 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12461 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12462 PIPE_CONF_CHECK_I(dp_m_n.tu);
12463
12464 if (current_config->has_drrs) {
12465 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12466 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12467 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12468 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12469 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12470 }
12471 } else {
12472 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12473 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12474 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12475 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12476 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12477 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012478
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012479 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12480 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12481 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12482 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12483 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12484 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012485
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012486 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12487 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12488 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12489 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12490 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12491 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012492
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012493 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012494 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012495 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12496 IS_VALLEYVIEW(dev))
12497 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012498 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012499
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012500 PIPE_CONF_CHECK_I(has_audio);
12501
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012502 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012503 DRM_MODE_FLAG_INTERLACE);
12504
Daniel Vetterbb760062013-06-06 14:55:52 +020012505 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012506 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012507 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012508 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012509 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012510 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012511 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012512 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012513 DRM_MODE_FLAG_NVSYNC);
12514 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012515
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012516 PIPE_CONF_CHECK_I(pipe_src_w);
12517 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012518
Daniel Vetter99535992014-04-13 12:00:33 +020012519 /*
12520 * FIXME: BIOS likes to set up a cloned config with lvds+external
12521 * screen. Since we don't yet re-compute the pipe config when moving
12522 * just the lvds port away to another pipe the sw tracking won't match.
12523 *
12524 * Proper atomic modesets with recomputed global state will fix this.
12525 * Until then just don't check gmch state for inherited modes.
12526 */
12527 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12528 PIPE_CONF_CHECK_I(gmch_pfit.control);
12529 /* pfit ratios are autocomputed by the hw on gen4+ */
12530 if (INTEL_INFO(dev)->gen < 4)
12531 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12532 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12533 }
12534
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012535 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12536 if (current_config->pch_pfit.enabled) {
12537 PIPE_CONF_CHECK_I(pch_pfit.pos);
12538 PIPE_CONF_CHECK_I(pch_pfit.size);
12539 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012540
Chandra Kondurua1b22782015-04-07 15:28:45 -070012541 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12542
Jesse Barnese59150d2014-01-07 13:30:45 -080012543 /* BDW+ don't expose a synchronous way to read the state */
12544 if (IS_HASWELL(dev))
12545 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012546
Ville Syrjälä282740f2013-09-04 18:30:03 +030012547 PIPE_CONF_CHECK_I(double_wide);
12548
Daniel Vetter26804af2014-06-25 22:01:55 +030012549 PIPE_CONF_CHECK_X(ddi_pll_sel);
12550
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012551 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012552 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012553 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012554 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12555 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012556 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012557 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12558 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12559 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012560
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012561 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12562 PIPE_CONF_CHECK_I(pipe_bpp);
12563
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012564 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012565 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012566
Daniel Vetter66e985c2013-06-05 13:34:20 +020012567#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012568#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012569#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012570#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012571#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012572#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012573
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012574 return true;
12575}
12576
Damien Lespiau08db6652014-11-04 17:06:52 +000012577static void check_wm_state(struct drm_device *dev)
12578{
12579 struct drm_i915_private *dev_priv = dev->dev_private;
12580 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12581 struct intel_crtc *intel_crtc;
12582 int plane;
12583
12584 if (INTEL_INFO(dev)->gen < 9)
12585 return;
12586
12587 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12588 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12589
12590 for_each_intel_crtc(dev, intel_crtc) {
12591 struct skl_ddb_entry *hw_entry, *sw_entry;
12592 const enum pipe pipe = intel_crtc->pipe;
12593
12594 if (!intel_crtc->active)
12595 continue;
12596
12597 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012598 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012599 hw_entry = &hw_ddb.plane[pipe][plane];
12600 sw_entry = &sw_ddb->plane[pipe][plane];
12601
12602 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12603 continue;
12604
12605 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12606 "(expected (%u,%u), found (%u,%u))\n",
12607 pipe_name(pipe), plane + 1,
12608 sw_entry->start, sw_entry->end,
12609 hw_entry->start, hw_entry->end);
12610 }
12611
12612 /* cursor */
12613 hw_entry = &hw_ddb.cursor[pipe];
12614 sw_entry = &sw_ddb->cursor[pipe];
12615
12616 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12617 continue;
12618
12619 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12620 "(expected (%u,%u), found (%u,%u))\n",
12621 pipe_name(pipe),
12622 sw_entry->start, sw_entry->end,
12623 hw_entry->start, hw_entry->end);
12624 }
12625}
12626
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012627static void
12628check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012629{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012630 struct intel_connector *connector;
12631
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012632 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012633 /* This also checks the encoder/connector hw state with the
12634 * ->get_hw_state callbacks. */
12635 intel_connector_check_state(connector);
12636
Rob Clarke2c719b2014-12-15 13:56:32 -050012637 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012638 "connector's staged encoder doesn't match current encoder\n");
12639 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012640}
12641
12642static void
12643check_encoder_state(struct drm_device *dev)
12644{
12645 struct intel_encoder *encoder;
12646 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012647
Damien Lespiaub2784e12014-08-05 11:29:37 +010012648 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012649 bool enabled = false;
12650 bool active = false;
12651 enum pipe pipe, tracked_pipe;
12652
12653 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12654 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012655 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012656
Rob Clarke2c719b2014-12-15 13:56:32 -050012657 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012658 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012659 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012660 "encoder's active_connectors set, but no crtc\n");
12661
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012662 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012663 if (connector->base.encoder != &encoder->base)
12664 continue;
12665 enabled = true;
12666 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12667 active = true;
12668 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012669 /*
12670 * for MST connectors if we unplug the connector is gone
12671 * away but the encoder is still connected to a crtc
12672 * until a modeset happens in response to the hotplug.
12673 */
12674 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12675 continue;
12676
Rob Clarke2c719b2014-12-15 13:56:32 -050012677 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012678 "encoder's enabled state mismatch "
12679 "(expected %i, found %i)\n",
12680 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012681 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012682 "active encoder with no crtc\n");
12683
Rob Clarke2c719b2014-12-15 13:56:32 -050012684 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012685 "encoder's computed active state doesn't match tracked active state "
12686 "(expected %i, found %i)\n", active, encoder->connectors_active);
12687
12688 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012689 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012690 "encoder's hw state doesn't match sw tracking "
12691 "(expected %i, found %i)\n",
12692 encoder->connectors_active, active);
12693
12694 if (!encoder->base.crtc)
12695 continue;
12696
12697 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012698 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012699 "active encoder's pipe doesn't match"
12700 "(expected %i, found %i)\n",
12701 tracked_pipe, pipe);
12702
12703 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012704}
12705
12706static void
12707check_crtc_state(struct drm_device *dev)
12708{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012709 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012710 struct intel_crtc *crtc;
12711 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012712 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012713
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012714 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012715 bool enabled = false;
12716 bool active = false;
12717
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012718 memset(&pipe_config, 0, sizeof(pipe_config));
12719
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012720 DRM_DEBUG_KMS("[CRTC:%d]\n",
12721 crtc->base.base.id);
12722
Matt Roper83d65732015-02-25 13:12:16 -080012723 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012724 "active crtc, but not enabled in sw tracking\n");
12725
Damien Lespiaub2784e12014-08-05 11:29:37 +010012726 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012727 if (encoder->base.crtc != &crtc->base)
12728 continue;
12729 enabled = true;
12730 if (encoder->connectors_active)
12731 active = true;
12732 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012733
Rob Clarke2c719b2014-12-15 13:56:32 -050012734 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012735 "crtc's computed active state doesn't match tracked active state "
12736 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012737 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012738 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012739 "(expected %i, found %i)\n", enabled,
12740 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012741
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012742 active = dev_priv->display.get_pipe_config(crtc,
12743 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012744
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012745 /* hw state is inconsistent with the pipe quirk */
12746 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12747 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012748 active = crtc->active;
12749
Damien Lespiaub2784e12014-08-05 11:29:37 +010012750 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012751 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012752 if (encoder->base.crtc != &crtc->base)
12753 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012754 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012755 encoder->get_config(encoder, &pipe_config);
12756 }
12757
Rob Clarke2c719b2014-12-15 13:56:32 -050012758 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012759 "crtc active state doesn't match with hw state "
12760 "(expected %i, found %i)\n", crtc->active, active);
12761
Daniel Vetterc0b03412013-05-28 12:05:54 +020012762 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012763 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012764 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012765 intel_dump_pipe_config(crtc, &pipe_config,
12766 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012767 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012768 "[sw state]");
12769 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012770 }
12771}
12772
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012773static void
12774check_shared_dpll_state(struct drm_device *dev)
12775{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012776 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012777 struct intel_crtc *crtc;
12778 struct intel_dpll_hw_state dpll_hw_state;
12779 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012780
12781 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12782 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12783 int enabled_crtcs = 0, active_crtcs = 0;
12784 bool active;
12785
12786 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12787
12788 DRM_DEBUG_KMS("%s\n", pll->name);
12789
12790 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12791
Rob Clarke2c719b2014-12-15 13:56:32 -050012792 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012793 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012794 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012795 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012796 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012797 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012798 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012799 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012800 "pll on state mismatch (expected %i, found %i)\n",
12801 pll->on, active);
12802
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012803 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012804 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012805 enabled_crtcs++;
12806 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12807 active_crtcs++;
12808 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012809 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012810 "pll active crtcs mismatch (expected %i, found %i)\n",
12811 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012812 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012813 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012814 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012815
Rob Clarke2c719b2014-12-15 13:56:32 -050012816 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012817 sizeof(dpll_hw_state)),
12818 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012819 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012820}
12821
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012822void
12823intel_modeset_check_state(struct drm_device *dev)
12824{
Damien Lespiau08db6652014-11-04 17:06:52 +000012825 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012826 check_connector_state(dev);
12827 check_encoder_state(dev);
12828 check_crtc_state(dev);
12829 check_shared_dpll_state(dev);
12830}
12831
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012832void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012833 int dotclock)
12834{
12835 /*
12836 * FDI already provided one idea for the dotclock.
12837 * Yell if the encoder disagrees.
12838 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012839 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012840 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012841 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012842}
12843
Ville Syrjälä80715b22014-05-15 20:23:23 +030012844static void update_scanline_offset(struct intel_crtc *crtc)
12845{
12846 struct drm_device *dev = crtc->base.dev;
12847
12848 /*
12849 * The scanline counter increments at the leading edge of hsync.
12850 *
12851 * On most platforms it starts counting from vtotal-1 on the
12852 * first active line. That means the scanline counter value is
12853 * always one less than what we would expect. Ie. just after
12854 * start of vblank, which also occurs at start of hsync (on the
12855 * last active line), the scanline counter will read vblank_start-1.
12856 *
12857 * On gen2 the scanline counter starts counting from 1 instead
12858 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12859 * to keep the value positive), instead of adding one.
12860 *
12861 * On HSW+ the behaviour of the scanline counter depends on the output
12862 * type. For DP ports it behaves like most other platforms, but on HDMI
12863 * there's an extra 1 line difference. So we need to add two instead of
12864 * one to the value.
12865 */
12866 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012867 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012868 int vtotal;
12869
12870 vtotal = mode->crtc_vtotal;
12871 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12872 vtotal /= 2;
12873
12874 crtc->scanline_offset = vtotal - 1;
12875 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012876 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012877 crtc->scanline_offset = 2;
12878 } else
12879 crtc->scanline_offset = 1;
12880}
12881
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012882static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012883intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012884 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012885{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012886 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012887 int ret = 0;
12888
12889 ret = drm_atomic_add_affected_connectors(state, crtc);
12890 if (ret)
12891 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012892
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012893 ret = drm_atomic_helper_check_modeset(state->dev, state);
12894 if (ret)
12895 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012896
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012897 /*
12898 * Note this needs changes when we start tracking multiple modes
12899 * and crtcs. At that point we'll need to compute the whole config
12900 * (i.e. one pipe_config for each crtc) rather than just the one
12901 * for this crtc.
12902 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012903 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12904 if (IS_ERR(pipe_config))
12905 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012906
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012907 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012908 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012909
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012910 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012911 if (ret)
12912 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012913
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012914 /* Check things that can only be changed through modeset */
12915 if (pipe_config->has_audio !=
12916 to_intel_crtc(crtc)->config->has_audio)
12917 pipe_config->base.mode_changed = true;
12918
12919 /*
12920 * Note we have an issue here with infoframes: current code
12921 * only updates them on the full mode set path per hw
12922 * requirements. So here we should be checking for any
12923 * required changes and forcing a mode set.
12924 */
12925
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012926 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12927
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012928 ret = drm_atomic_helper_check_planes(state->dev, state);
12929 if (ret)
12930 return ERR_PTR(ret);
12931
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012932 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012933}
12934
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012935static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012936{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012937 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012938 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012939 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012940 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012941 struct intel_crtc_state *intel_crtc_state;
12942 struct drm_crtc *crtc;
12943 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012944 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012945 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012946
12947 if (!dev_priv->display.crtc_compute_clock)
12948 return 0;
12949
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012950 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12951 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012952 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012953
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012954 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012955 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012956 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012957 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012958 }
12959
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012960 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12961 if (ret)
12962 goto done;
12963
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012964 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12965 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012966 continue;
12967
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012968 intel_crtc = to_intel_crtc(crtc);
12969 intel_crtc_state = to_intel_crtc_state(crtc_state);
12970
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012971 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012972 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012973 if (ret) {
12974 intel_shared_dpll_abort_config(dev_priv);
12975 goto done;
12976 }
12977 }
12978
12979done:
12980 return ret;
12981}
12982
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012983/* Code that should eventually be part of atomic_check() */
12984static int __intel_set_mode_checks(struct drm_atomic_state *state)
12985{
12986 struct drm_device *dev = state->dev;
12987 int ret;
12988
12989 /*
12990 * See if the config requires any additional preparation, e.g.
12991 * to adjust global state with pipes off. We need to do this
12992 * here so we can get the modeset_pipe updated config for the new
12993 * mode set on this crtc. For other crtcs we need to use the
12994 * adjusted_mode bits in the crtc directly.
12995 */
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012996 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12997 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12998 ret = valleyview_modeset_global_pipes(state);
12999 else
13000 ret = broadwell_modeset_global_pipes(state);
13001
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013002 if (ret)
13003 return ret;
13004 }
13005
13006 ret = __intel_set_mode_setup_plls(state);
13007 if (ret)
13008 return ret;
13009
13010 return 0;
13011}
13012
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013013static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013014 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020013015{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013016 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030013017 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030013018 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013019 struct drm_crtc *crtc;
13020 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013021 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013022 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020013023
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030013024 ret = __intel_set_mode_checks(state);
13025 if (ret < 0)
13026 return ret;
13027
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013028 ret = drm_atomic_helper_prepare_planes(dev, state);
13029 if (ret)
13030 return ret;
13031
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013032 for_each_crtc_in_state(state, crtc, crtc_state, i) {
13033 if (!needs_modeset(crtc_state))
13034 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010013035
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013036 if (!crtc_state->enable) {
13037 intel_crtc_disable(crtc);
13038 } else if (crtc->state->enable) {
13039 intel_crtc_disable_planes(crtc);
13040 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030013041 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020013042 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013043
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020013044 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
13045 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013046 *
13047 * Note we'll need to fix this up when we start tracking multiple
13048 * pipes; here we assume a single modeset_pipe and only track the
13049 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020013050 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013051 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013052 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020013053
13054 /*
13055 * Calculate and store various constants which
13056 * are later needed by vblank and swap-completion
13057 * timestamping. They are derived from true hwmode.
13058 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013059 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013060 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013061 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013062
Daniel Vetterea9d7582012-07-10 10:42:52 +020013063 /* Only after disabling all output pipelines that will be changed can we
13064 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013065 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013066
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013067 /* The state has been swaped above, so state actually contains the
13068 * old state now. */
13069
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030013070 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013071
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013072 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013073
13074 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013075 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013076 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013077 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013078
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013079 update_scanline_offset(to_intel_crtc(crtc));
13080
13081 dev_priv->display.crtc_enable(crtc);
13082 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013083 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013084
Daniel Vettera6778b32012-07-02 09:56:42 +020013085 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013086
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013087 drm_atomic_helper_cleanup_planes(dev, state);
13088
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013089 drm_atomic_state_free(state);
13090
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013091 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013092}
13093
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013094static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013095 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013096{
13097 int ret;
13098
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013099 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013100
13101 if (ret == 0)
13102 intel_modeset_check_state(crtc->dev);
13103
13104 return ret;
13105}
13106
Damien Lespiaue7457a92013-08-08 22:28:59 +010013107static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013108 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013109{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013110 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013111 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013112
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013113 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013114 if (IS_ERR(pipe_config)) {
13115 ret = PTR_ERR(pipe_config);
13116 goto out;
13117 }
Daniel Vetterf30da182013-04-11 20:22:50 +020013118
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013119 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013120 if (ret)
13121 goto out;
13122
13123out:
13124 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013125}
13126
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013127void intel_crtc_restore_mode(struct drm_crtc *crtc)
13128{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013129 struct drm_device *dev = crtc->dev;
13130 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013131 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013132 struct intel_encoder *encoder;
13133 struct intel_connector *connector;
13134 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013135 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013136 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013137
13138 state = drm_atomic_state_alloc(dev);
13139 if (!state) {
13140 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13141 crtc->base.id);
13142 return;
13143 }
13144
13145 state->acquire_ctx = dev->mode_config.acquire_ctx;
13146
13147 /* The force restore path in the HW readout code relies on the staged
13148 * config still keeping the user requested config while the actual
13149 * state has been overwritten by the configuration read from HW. We
13150 * need to copy the staged config to the atomic state, otherwise the
13151 * mode set will just reapply the state the HW is already in. */
13152 for_each_intel_encoder(dev, encoder) {
13153 if (&encoder->new_crtc->base != crtc)
13154 continue;
13155
13156 for_each_intel_connector(dev, connector) {
13157 if (connector->new_encoder != encoder)
13158 continue;
13159
13160 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13161 if (IS_ERR(connector_state)) {
13162 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13163 connector->base.base.id,
13164 connector->base.name,
13165 PTR_ERR(connector_state));
13166 continue;
13167 }
13168
13169 connector_state->crtc = crtc;
13170 connector_state->best_encoder = &encoder->base;
13171 }
13172 }
13173
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013174 for_each_intel_crtc(dev, intel_crtc) {
13175 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13176 continue;
13177
13178 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13179 if (IS_ERR(crtc_state)) {
13180 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13181 intel_crtc->base.base.id,
13182 PTR_ERR(crtc_state));
13183 continue;
13184 }
13185
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013186 crtc_state->base.active = crtc_state->base.enable =
13187 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013188
13189 if (&intel_crtc->base == crtc)
13190 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013191 }
13192
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013193 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13194 crtc->primary->fb, crtc->x, crtc->y);
13195
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013196 ret = intel_set_mode(crtc, state);
13197 if (ret)
13198 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013199}
13200
Daniel Vetter25c5b262012-07-08 22:08:04 +020013201#undef for_each_intel_crtc_masked
13202
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013203static bool intel_connector_in_mode_set(struct intel_connector *connector,
13204 struct drm_mode_set *set)
13205{
13206 int ro;
13207
13208 for (ro = 0; ro < set->num_connectors; ro++)
13209 if (set->connectors[ro] == &connector->base)
13210 return true;
13211
13212 return false;
13213}
13214
Daniel Vetter2e431052012-07-04 22:42:15 +020013215static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013216intel_modeset_stage_output_state(struct drm_device *dev,
13217 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013218 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013219{
Daniel Vetter9a935852012-07-05 22:34:27 +020013220 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013221 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013222 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013223 struct drm_crtc *crtc;
13224 struct drm_crtc_state *crtc_state;
13225 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013226
Damien Lespiau9abdda72013-02-13 13:29:23 +000013227 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013228 * of connectors. For paranoia, double-check this. */
13229 WARN_ON(!set->fb && (set->num_connectors != 0));
13230 WARN_ON(set->fb && (set->num_connectors == 0));
13231
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013232 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013233 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13234
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013235 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13236 continue;
13237
13238 connector_state =
13239 drm_atomic_get_connector_state(state, &connector->base);
13240 if (IS_ERR(connector_state))
13241 return PTR_ERR(connector_state);
13242
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013243 if (in_mode_set) {
13244 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013245 connector_state->best_encoder =
13246 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013247 }
13248
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013249 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013250 continue;
13251
Daniel Vetter9a935852012-07-05 22:34:27 +020013252 /* If we disable the crtc, disable all its connectors. Also, if
13253 * the connector is on the changing crtc but not on the new
13254 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013255 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013256 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013257
13258 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13259 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013260 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013261 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013262 }
13263 /* connector->new_encoder is now updated for all connectors. */
13264
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013265 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13266 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013267
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013268 if (!connector_state->best_encoder) {
13269 ret = drm_atomic_set_crtc_for_connector(connector_state,
13270 NULL);
13271 if (ret)
13272 return ret;
13273
Daniel Vetter50f56112012-07-02 09:35:43 +020013274 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013275 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013276
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013277 if (intel_connector_in_mode_set(connector, set)) {
13278 struct drm_crtc *crtc = connector->base.state->crtc;
13279
13280 /* If this connector was in a previous crtc, add it
13281 * to the state. We might need to disable it. */
13282 if (crtc) {
13283 crtc_state =
13284 drm_atomic_get_crtc_state(state, crtc);
13285 if (IS_ERR(crtc_state))
13286 return PTR_ERR(crtc_state);
13287 }
13288
13289 ret = drm_atomic_set_crtc_for_connector(connector_state,
13290 set->crtc);
13291 if (ret)
13292 return ret;
13293 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013294
13295 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013296 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13297 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013298 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013299 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013300
Daniel Vetter9a935852012-07-05 22:34:27 +020013301 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13302 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013303 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013304 connector_state->crtc->base.id);
13305
13306 if (connector_state->best_encoder != &connector->encoder->base)
13307 connector->encoder =
13308 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013309 }
13310
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013311 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013312 bool has_connectors;
13313
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013314 ret = drm_atomic_add_affected_connectors(state, crtc);
13315 if (ret)
13316 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013317
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013318 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13319 if (has_connectors != crtc_state->enable)
13320 crtc_state->enable =
13321 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013322 }
13323
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013324 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13325 set->fb, set->x, set->y);
13326 if (ret)
13327 return ret;
13328
13329 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13330 if (IS_ERR(crtc_state))
13331 return PTR_ERR(crtc_state);
13332
13333 if (set->mode)
13334 drm_mode_copy(&crtc_state->mode, set->mode);
13335
13336 if (set->num_connectors)
13337 crtc_state->active = true;
13338
Daniel Vetter2e431052012-07-04 22:42:15 +020013339 return 0;
13340}
13341
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013342static bool primary_plane_visible(struct drm_crtc *crtc)
13343{
13344 struct intel_plane_state *plane_state =
13345 to_intel_plane_state(crtc->primary->state);
13346
13347 return plane_state->visible;
13348}
13349
Daniel Vetter2e431052012-07-04 22:42:15 +020013350static int intel_crtc_set_config(struct drm_mode_set *set)
13351{
13352 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013353 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013354 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013355 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020013356 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013357
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013358 BUG_ON(!set);
13359 BUG_ON(!set->crtc);
13360 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013361
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013362 /* Enforce sane interface api - has been abused by the fb helper. */
13363 BUG_ON(!set->mode && set->fb);
13364 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013365
Daniel Vetter2e431052012-07-04 22:42:15 +020013366 if (set->fb) {
13367 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13368 set->crtc->base.id, set->fb->base.id,
13369 (int)set->num_connectors, set->x, set->y);
13370 } else {
13371 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013372 }
13373
13374 dev = set->crtc->dev;
13375
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013376 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013377 if (!state)
13378 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013379
13380 state->acquire_ctx = dev->mode_config.acquire_ctx;
13381
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013382 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013383 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013384 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013385
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013386 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080013387 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080013388 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013389 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080013390 }
Jesse Barnes50f52752014-11-07 13:11:00 -080013391
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013392 intel_update_pipe_size(to_intel_crtc(set->crtc));
13393
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013394 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013395
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013396 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013397
13398 if (ret == 0 &&
13399 pipe_config->base.enable &&
13400 pipe_config->base.planes_changed &&
13401 !needs_modeset(&pipe_config->base)) {
13402 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013403
13404 /*
13405 * We need to make sure the primary plane is re-enabled if it
13406 * has previously been turned off.
13407 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013408 if (ret == 0 && !primary_plane_was_visible &&
13409 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070013410 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030013411 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013412 }
13413
Jesse Barnes7ca51a32014-01-07 13:50:49 -080013414 /*
13415 * In the fastboot case this may be our only check of the
13416 * state after boot. It would be better to only do it on
13417 * the first update, but we don't have a nice way of doing that
13418 * (and really, set_config isn't used much for high freq page
13419 * flipping, so increasing its cost here shouldn't be a big
13420 * deal).
13421 */
Jani Nikulad330a952014-01-21 11:24:25 +020013422 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080013423 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020013424 }
13425
Chris Wilson2d05eae2013-05-03 17:36:25 +010013426 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013427 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13428 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013429 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013430
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013431out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013432 if (ret)
13433 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013434 return ret;
13435}
13436
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013437static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013438 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013439 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013440 .destroy = intel_crtc_destroy,
13441 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013442 .atomic_duplicate_state = intel_crtc_duplicate_state,
13443 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013444};
13445
Daniel Vetter53589012013-06-05 13:34:16 +020013446static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13447 struct intel_shared_dpll *pll,
13448 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013449{
Daniel Vetter53589012013-06-05 13:34:16 +020013450 uint32_t val;
13451
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013452 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013453 return false;
13454
Daniel Vetter53589012013-06-05 13:34:16 +020013455 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013456 hw_state->dpll = val;
13457 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13458 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013459
13460 return val & DPLL_VCO_ENABLE;
13461}
13462
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013463static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13464 struct intel_shared_dpll *pll)
13465{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013466 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13467 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013468}
13469
Daniel Vettere7b903d2013-06-05 13:34:14 +020013470static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13471 struct intel_shared_dpll *pll)
13472{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013473 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013474 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013475
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013476 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013477
13478 /* Wait for the clocks to stabilize. */
13479 POSTING_READ(PCH_DPLL(pll->id));
13480 udelay(150);
13481
13482 /* The pixel multiplier can only be updated once the
13483 * DPLL is enabled and the clocks are stable.
13484 *
13485 * So write it again.
13486 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013487 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013488 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013489 udelay(200);
13490}
13491
13492static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13493 struct intel_shared_dpll *pll)
13494{
13495 struct drm_device *dev = dev_priv->dev;
13496 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013497
13498 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013499 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013500 if (intel_crtc_to_shared_dpll(crtc) == pll)
13501 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13502 }
13503
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013504 I915_WRITE(PCH_DPLL(pll->id), 0);
13505 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013506 udelay(200);
13507}
13508
Daniel Vetter46edb022013-06-05 13:34:12 +020013509static char *ibx_pch_dpll_names[] = {
13510 "PCH DPLL A",
13511 "PCH DPLL B",
13512};
13513
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013514static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013515{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013516 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013517 int i;
13518
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013519 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013520
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013521 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013522 dev_priv->shared_dplls[i].id = i;
13523 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013524 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013525 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13526 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013527 dev_priv->shared_dplls[i].get_hw_state =
13528 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013529 }
13530}
13531
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013532static void intel_shared_dpll_init(struct drm_device *dev)
13533{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013534 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013535
Ville Syrjäläb6283052015-06-03 15:45:07 +030013536 intel_update_cdclk(dev);
13537
Daniel Vetter9cd86932014-06-25 22:01:57 +030013538 if (HAS_DDI(dev))
13539 intel_ddi_pll_init(dev);
13540 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013541 ibx_pch_dpll_init(dev);
13542 else
13543 dev_priv->num_shared_dpll = 0;
13544
13545 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013546}
13547
Matt Roper6beb8c232014-12-01 15:40:14 -080013548/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013549 * intel_wm_need_update - Check whether watermarks need updating
13550 * @plane: drm plane
13551 * @state: new plane state
13552 *
13553 * Check current plane state versus the new one to determine whether
13554 * watermarks need to be recalculated.
13555 *
13556 * Returns true or false.
13557 */
13558bool intel_wm_need_update(struct drm_plane *plane,
13559 struct drm_plane_state *state)
13560{
13561 /* Update watermarks on tiling changes. */
13562 if (!plane->state->fb || !state->fb ||
13563 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13564 plane->state->rotation != state->rotation)
13565 return true;
13566
13567 return false;
13568}
13569
13570/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013571 * intel_prepare_plane_fb - Prepare fb for usage on plane
13572 * @plane: drm plane to prepare for
13573 * @fb: framebuffer to prepare for presentation
13574 *
13575 * Prepares a framebuffer for usage on a display plane. Generally this
13576 * involves pinning the underlying object and updating the frontbuffer tracking
13577 * bits. Some older platforms need special physical address handling for
13578 * cursor planes.
13579 *
13580 * Returns 0 on success, negative error code on failure.
13581 */
13582int
13583intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013584 struct drm_framebuffer *fb,
13585 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013586{
13587 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013588 struct intel_plane *intel_plane = to_intel_plane(plane);
13589 enum pipe pipe = intel_plane->pipe;
13590 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13591 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13592 unsigned frontbuffer_bits = 0;
13593 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013594
Matt Roperea2c67b2014-12-23 10:41:52 -080013595 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013596 return 0;
13597
Matt Roper6beb8c232014-12-01 15:40:14 -080013598 switch (plane->type) {
13599 case DRM_PLANE_TYPE_PRIMARY:
13600 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13601 break;
13602 case DRM_PLANE_TYPE_CURSOR:
13603 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13604 break;
13605 case DRM_PLANE_TYPE_OVERLAY:
13606 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13607 break;
13608 }
Matt Roper465c1202014-05-29 08:06:54 -070013609
Matt Roper4c345742014-07-09 16:22:10 -070013610 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013611
Matt Roper6beb8c232014-12-01 15:40:14 -080013612 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13613 INTEL_INFO(dev)->cursor_needs_physical) {
13614 int align = IS_I830(dev) ? 16 * 1024 : 256;
13615 ret = i915_gem_object_attach_phys(obj, align);
13616 if (ret)
13617 DRM_DEBUG_KMS("failed to attach phys object\n");
13618 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013619 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013620 }
13621
13622 if (ret == 0)
13623 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13624
13625 mutex_unlock(&dev->struct_mutex);
13626
13627 return ret;
13628}
13629
Matt Roper38f3ce32014-12-02 07:45:25 -080013630/**
13631 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13632 * @plane: drm plane to clean up for
13633 * @fb: old framebuffer that was on plane
13634 *
13635 * Cleans up a framebuffer that has just been removed from a plane.
13636 */
13637void
13638intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013639 struct drm_framebuffer *fb,
13640 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013641{
13642 struct drm_device *dev = plane->dev;
13643 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13644
13645 if (WARN_ON(!obj))
13646 return;
13647
13648 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13649 !INTEL_INFO(dev)->cursor_needs_physical) {
13650 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013651 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013652 mutex_unlock(&dev->struct_mutex);
13653 }
Matt Roper465c1202014-05-29 08:06:54 -070013654}
13655
Chandra Konduru6156a452015-04-27 13:48:39 -070013656int
13657skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13658{
13659 int max_scale;
13660 struct drm_device *dev;
13661 struct drm_i915_private *dev_priv;
13662 int crtc_clock, cdclk;
13663
13664 if (!intel_crtc || !crtc_state)
13665 return DRM_PLANE_HELPER_NO_SCALING;
13666
13667 dev = intel_crtc->base.dev;
13668 dev_priv = dev->dev_private;
13669 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13670 cdclk = dev_priv->display.get_display_clock_speed(dev);
13671
13672 if (!crtc_clock || !cdclk)
13673 return DRM_PLANE_HELPER_NO_SCALING;
13674
13675 /*
13676 * skl max scale is lower of:
13677 * close to 3 but not 3, -1 is for that purpose
13678 * or
13679 * cdclk/crtc_clock
13680 */
13681 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13682
13683 return max_scale;
13684}
13685
Matt Roper465c1202014-05-29 08:06:54 -070013686static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013687intel_check_primary_plane(struct drm_plane *plane,
13688 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013689{
Matt Roper32b7eee2014-12-24 07:59:06 -080013690 struct drm_device *dev = plane->dev;
13691 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013692 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013693 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013694 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013695 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013696 struct drm_rect *dest = &state->dst;
13697 struct drm_rect *src = &state->src;
13698 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013699 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013700 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13701 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013702 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013703
Matt Roperea2c67b2014-12-23 10:41:52 -080013704 crtc = crtc ? crtc : plane->crtc;
13705 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013706 crtc_state = state->base.state ?
13707 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013708
Chandra Konduru6156a452015-04-27 13:48:39 -070013709 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013710 /* use scaler when colorkey is not required */
13711 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13712 min_scale = 1;
13713 max_scale = skl_max_scale(intel_crtc, crtc_state);
13714 }
Sonika Jindald8106362015-04-10 14:37:28 +053013715 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013716 }
Sonika Jindald8106362015-04-10 14:37:28 +053013717
Matt Roperc59cb172014-12-01 15:40:16 -080013718 ret = drm_plane_helper_check_update(plane, crtc, fb,
13719 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013720 min_scale,
13721 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013722 can_position, true,
13723 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013724 if (ret)
13725 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013726
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013727 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013728 struct intel_plane_state *old_state =
13729 to_intel_plane_state(plane->state);
13730
Matt Roper32b7eee2014-12-24 07:59:06 -080013731 intel_crtc->atomic.wait_for_flips = true;
13732
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013733 /*
13734 * FBC does not work on some platforms for rotated
13735 * planes, so disable it when rotation is not 0 and
13736 * update it when rotation is set back to 0.
13737 *
13738 * FIXME: This is redundant with the fbc update done in
13739 * the primary plane enable function except that that
13740 * one is done too late. We eventually need to unify
13741 * this.
13742 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013743 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013744 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013745 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013746 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013747 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013748 }
13749
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013750 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013751 /*
13752 * BDW signals flip done immediately if the plane
13753 * is disabled, even if the plane enable is already
13754 * armed to occur at the next vblank :(
13755 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013756 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013757 intel_crtc->atomic.wait_vblank = true;
13758 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013759
Matt Roper32b7eee2014-12-24 07:59:06 -080013760 intel_crtc->atomic.fb_bits |=
13761 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13762
13763 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013764
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013765 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013766 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013767 }
13768
Chandra Konduru6156a452015-04-27 13:48:39 -070013769 if (INTEL_INFO(dev)->gen >= 9) {
13770 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13771 to_intel_plane(plane), state, 0);
13772 if (ret)
13773 return ret;
13774 }
13775
Matt Roperc59cb172014-12-01 15:40:16 -080013776 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013777}
13778
Sonika Jindal48404c12014-08-22 14:06:04 +053013779static void
13780intel_commit_primary_plane(struct drm_plane *plane,
13781 struct intel_plane_state *state)
13782{
Matt Roper2b875c22014-12-01 15:40:13 -080013783 struct drm_crtc *crtc = state->base.crtc;
13784 struct drm_framebuffer *fb = state->base.fb;
13785 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013786 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013787 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013788 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013789
Matt Roperea2c67b2014-12-23 10:41:52 -080013790 crtc = crtc ? crtc : plane->crtc;
13791 intel_crtc = to_intel_crtc(crtc);
13792
Matt Ropercf4c7c12014-12-04 10:27:42 -080013793 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013794 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013795 crtc->y = src->y1 >> 16;
13796
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013797 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013798 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013799 /* FIXME: kill this fastboot hack */
13800 intel_update_pipe_size(intel_crtc);
13801
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013802 dev_priv->display.update_primary_plane(crtc, plane->fb,
13803 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013804 }
13805}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013806
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013807static void
13808intel_disable_primary_plane(struct drm_plane *plane,
13809 struct drm_crtc *crtc,
13810 bool force)
13811{
13812 struct drm_device *dev = plane->dev;
13813 struct drm_i915_private *dev_priv = dev->dev_private;
13814
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013815 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13816}
13817
Matt Roper32b7eee2014-12-24 07:59:06 -080013818static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13819{
13820 struct drm_device *dev = crtc->dev;
13821 struct drm_i915_private *dev_priv = dev->dev_private;
13822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013823 struct intel_plane *intel_plane;
13824 struct drm_plane *p;
13825 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013826
Matt Roperea2c67b2014-12-23 10:41:52 -080013827 /* Track fb's for any planes being disabled */
13828 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13829 intel_plane = to_intel_plane(p);
13830
13831 if (intel_crtc->atomic.disabled_planes &
13832 (1 << drm_plane_index(p))) {
13833 switch (p->type) {
13834 case DRM_PLANE_TYPE_PRIMARY:
13835 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13836 break;
13837 case DRM_PLANE_TYPE_CURSOR:
13838 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13839 break;
13840 case DRM_PLANE_TYPE_OVERLAY:
13841 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13842 break;
13843 }
13844
13845 mutex_lock(&dev->struct_mutex);
13846 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13847 mutex_unlock(&dev->struct_mutex);
13848 }
13849 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013850
Matt Roper32b7eee2014-12-24 07:59:06 -080013851 if (intel_crtc->atomic.wait_for_flips)
13852 intel_crtc_wait_for_pending_flips(crtc);
13853
13854 if (intel_crtc->atomic.disable_fbc)
13855 intel_fbc_disable(dev);
13856
13857 if (intel_crtc->atomic.pre_disable_primary)
13858 intel_pre_disable_primary(crtc);
13859
13860 if (intel_crtc->atomic.update_wm)
13861 intel_update_watermarks(crtc);
13862
13863 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013864
13865 /* Perform vblank evasion around commit operation */
13866 if (intel_crtc->active)
13867 intel_crtc->atomic.evade =
13868 intel_pipe_update_start(intel_crtc,
13869 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013870}
13871
13872static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13873{
13874 struct drm_device *dev = crtc->dev;
13875 struct drm_i915_private *dev_priv = dev->dev_private;
13876 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13877 struct drm_plane *p;
13878
Matt Roperc34c9ee2014-12-23 10:41:50 -080013879 if (intel_crtc->atomic.evade)
13880 intel_pipe_update_end(intel_crtc,
13881 intel_crtc->atomic.start_vbl_count);
13882
Matt Roper32b7eee2014-12-24 07:59:06 -080013883 intel_runtime_pm_put(dev_priv);
13884
13885 if (intel_crtc->atomic.wait_vblank)
13886 intel_wait_for_vblank(dev, intel_crtc->pipe);
13887
13888 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13889
13890 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013891 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013892 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013893 mutex_unlock(&dev->struct_mutex);
13894 }
Matt Roper465c1202014-05-29 08:06:54 -070013895
Matt Roper32b7eee2014-12-24 07:59:06 -080013896 if (intel_crtc->atomic.post_enable_primary)
13897 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013898
Matt Roper32b7eee2014-12-24 07:59:06 -080013899 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13900 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13901 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13902 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013903
Matt Roper32b7eee2014-12-24 07:59:06 -080013904 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013905}
13906
Matt Ropercf4c7c12014-12-04 10:27:42 -080013907/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013908 * intel_plane_destroy - destroy a plane
13909 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013910 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013911 * Common destruction function for all types of planes (primary, cursor,
13912 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013913 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013914void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013915{
13916 struct intel_plane *intel_plane = to_intel_plane(plane);
13917 drm_plane_cleanup(plane);
13918 kfree(intel_plane);
13919}
13920
Matt Roper65a3fea2015-01-21 16:35:42 -080013921const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013922 .update_plane = drm_atomic_helper_update_plane,
13923 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013924 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013925 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013926 .atomic_get_property = intel_plane_atomic_get_property,
13927 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013928 .atomic_duplicate_state = intel_plane_duplicate_state,
13929 .atomic_destroy_state = intel_plane_destroy_state,
13930
Matt Roper465c1202014-05-29 08:06:54 -070013931};
13932
13933static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13934 int pipe)
13935{
13936 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013937 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013938 const uint32_t *intel_primary_formats;
13939 int num_formats;
13940
13941 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13942 if (primary == NULL)
13943 return NULL;
13944
Matt Roper8e7d6882015-01-21 16:35:41 -080013945 state = intel_create_plane_state(&primary->base);
13946 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013947 kfree(primary);
13948 return NULL;
13949 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013950 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013951
Matt Roper465c1202014-05-29 08:06:54 -070013952 primary->can_scale = false;
13953 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013954 if (INTEL_INFO(dev)->gen >= 9) {
13955 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013956 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013957 }
Matt Roper465c1202014-05-29 08:06:54 -070013958 primary->pipe = pipe;
13959 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013960 primary->check_plane = intel_check_primary_plane;
13961 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013962 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013963 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013964 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13965 primary->plane = !pipe;
13966
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013967 if (INTEL_INFO(dev)->gen >= 9) {
13968 intel_primary_formats = skl_primary_formats;
13969 num_formats = ARRAY_SIZE(skl_primary_formats);
13970 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013971 intel_primary_formats = i965_primary_formats;
13972 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013973 } else {
13974 intel_primary_formats = i8xx_primary_formats;
13975 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013976 }
13977
13978 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013979 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013980 intel_primary_formats, num_formats,
13981 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013982
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013983 if (INTEL_INFO(dev)->gen >= 4)
13984 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013985
Matt Roperea2c67b2014-12-23 10:41:52 -080013986 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13987
Matt Roper465c1202014-05-29 08:06:54 -070013988 return &primary->base;
13989}
13990
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013991void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13992{
13993 if (!dev->mode_config.rotation_property) {
13994 unsigned long flags = BIT(DRM_ROTATE_0) |
13995 BIT(DRM_ROTATE_180);
13996
13997 if (INTEL_INFO(dev)->gen >= 9)
13998 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13999
14000 dev->mode_config.rotation_property =
14001 drm_mode_create_rotation_property(dev, flags);
14002 }
14003 if (dev->mode_config.rotation_property)
14004 drm_object_attach_property(&plane->base.base,
14005 dev->mode_config.rotation_property,
14006 plane->base.state->rotation);
14007}
14008
Matt Roper3d7d6512014-06-10 08:28:13 -070014009static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030014010intel_check_cursor_plane(struct drm_plane *plane,
14011 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070014012{
Matt Roper2b875c22014-12-01 15:40:13 -080014013 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014014 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080014015 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014016 struct drm_rect *dest = &state->dst;
14017 struct drm_rect *src = &state->src;
14018 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014019 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080014020 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014021 unsigned stride;
14022 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014023
Matt Roperea2c67b2014-12-23 10:41:52 -080014024 crtc = crtc ? crtc : plane->crtc;
14025 intel_crtc = to_intel_crtc(crtc);
14026
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014027 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030014028 src, dest, clip,
14029 DRM_PLANE_HELPER_NO_SCALING,
14030 DRM_PLANE_HELPER_NO_SCALING,
14031 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014032 if (ret)
14033 return ret;
14034
14035
14036 /* if we want to turn off the cursor ignore width and height */
14037 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080014038 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014039
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014040 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080014041 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
14042 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
14043 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014044 return -EINVAL;
14045 }
14046
Matt Roperea2c67b2014-12-23 10:41:52 -080014047 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
14048 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014049 DRM_DEBUG_KMS("buffer is too small\n");
14050 return -ENOMEM;
14051 }
14052
Ville Syrjälä3a656b52015-03-09 21:08:37 +020014053 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014054 DRM_DEBUG_KMS("cursor cannot be tiled\n");
14055 ret = -EINVAL;
14056 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014057
Matt Roper32b7eee2014-12-24 07:59:06 -080014058finish:
14059 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020014060 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080014061 intel_crtc->atomic.update_wm = true;
14062
14063 intel_crtc->atomic.fb_bits |=
14064 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
14065 }
14066
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014067 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014068}
14069
Matt Roperf4a2cf22014-12-01 15:40:12 -080014070static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014071intel_disable_cursor_plane(struct drm_plane *plane,
14072 struct drm_crtc *crtc,
14073 bool force)
14074{
14075 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14076
14077 if (!force) {
14078 plane->fb = NULL;
14079 intel_crtc->cursor_bo = NULL;
14080 intel_crtc->cursor_addr = 0;
14081 }
14082
14083 intel_crtc_update_cursor(crtc, false);
14084}
14085
14086static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030014087intel_commit_cursor_plane(struct drm_plane *plane,
14088 struct intel_plane_state *state)
14089{
Matt Roper2b875c22014-12-01 15:40:13 -080014090 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014091 struct drm_device *dev = plane->dev;
14092 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014093 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014094 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014095
Matt Roperea2c67b2014-12-23 10:41:52 -080014096 crtc = crtc ? crtc : plane->crtc;
14097 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014098
Matt Roperea2c67b2014-12-23 10:41:52 -080014099 plane->fb = state->base.fb;
14100 crtc->cursor_x = state->base.crtc_x;
14101 crtc->cursor_y = state->base.crtc_y;
14102
Gustavo Padovana912f122014-12-01 15:40:10 -080014103 if (intel_crtc->cursor_bo == obj)
14104 goto update;
14105
Matt Roperf4a2cf22014-12-01 15:40:12 -080014106 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014107 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014108 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014109 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014110 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014111 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014112
Gustavo Padovana912f122014-12-01 15:40:10 -080014113 intel_crtc->cursor_addr = addr;
14114 intel_crtc->cursor_bo = obj;
14115update:
Gustavo Padovana912f122014-12-01 15:40:10 -080014116
Matt Roper32b7eee2014-12-24 07:59:06 -080014117 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014118 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014119}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014120
Matt Roper3d7d6512014-06-10 08:28:13 -070014121static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14122 int pipe)
14123{
14124 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014125 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014126
14127 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14128 if (cursor == NULL)
14129 return NULL;
14130
Matt Roper8e7d6882015-01-21 16:35:41 -080014131 state = intel_create_plane_state(&cursor->base);
14132 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014133 kfree(cursor);
14134 return NULL;
14135 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014136 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014137
Matt Roper3d7d6512014-06-10 08:28:13 -070014138 cursor->can_scale = false;
14139 cursor->max_downscale = 1;
14140 cursor->pipe = pipe;
14141 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080014142 cursor->check_plane = intel_check_cursor_plane;
14143 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014144 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014145
14146 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014147 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014148 intel_cursor_formats,
14149 ARRAY_SIZE(intel_cursor_formats),
14150 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014151
14152 if (INTEL_INFO(dev)->gen >= 4) {
14153 if (!dev->mode_config.rotation_property)
14154 dev->mode_config.rotation_property =
14155 drm_mode_create_rotation_property(dev,
14156 BIT(DRM_ROTATE_0) |
14157 BIT(DRM_ROTATE_180));
14158 if (dev->mode_config.rotation_property)
14159 drm_object_attach_property(&cursor->base.base,
14160 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014161 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014162 }
14163
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014164 if (INTEL_INFO(dev)->gen >=9)
14165 state->scaler_id = -1;
14166
Matt Roperea2c67b2014-12-23 10:41:52 -080014167 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14168
Matt Roper3d7d6512014-06-10 08:28:13 -070014169 return &cursor->base;
14170}
14171
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014172static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14173 struct intel_crtc_state *crtc_state)
14174{
14175 int i;
14176 struct intel_scaler *intel_scaler;
14177 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14178
14179 for (i = 0; i < intel_crtc->num_scalers; i++) {
14180 intel_scaler = &scaler_state->scalers[i];
14181 intel_scaler->in_use = 0;
14182 intel_scaler->id = i;
14183
14184 intel_scaler->mode = PS_SCALER_MODE_DYN;
14185 }
14186
14187 scaler_state->scaler_id = -1;
14188}
14189
Hannes Ederb358d0a2008-12-18 21:18:47 +010014190static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014191{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014192 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014193 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014194 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014195 struct drm_plane *primary = NULL;
14196 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014197 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014198
Daniel Vetter955382f2013-09-19 14:05:45 +020014199 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014200 if (intel_crtc == NULL)
14201 return;
14202
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014203 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14204 if (!crtc_state)
14205 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014206 intel_crtc->config = crtc_state;
14207 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014208 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014209
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014210 /* initialize shared scalers */
14211 if (INTEL_INFO(dev)->gen >= 9) {
14212 if (pipe == PIPE_C)
14213 intel_crtc->num_scalers = 1;
14214 else
14215 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14216
14217 skl_init_scalers(dev, intel_crtc, crtc_state);
14218 }
14219
Matt Roper465c1202014-05-29 08:06:54 -070014220 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014221 if (!primary)
14222 goto fail;
14223
14224 cursor = intel_cursor_plane_create(dev, pipe);
14225 if (!cursor)
14226 goto fail;
14227
Matt Roper465c1202014-05-29 08:06:54 -070014228 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014229 cursor, &intel_crtc_funcs);
14230 if (ret)
14231 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014232
14233 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014234 for (i = 0; i < 256; i++) {
14235 intel_crtc->lut_r[i] = i;
14236 intel_crtc->lut_g[i] = i;
14237 intel_crtc->lut_b[i] = i;
14238 }
14239
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014240 /*
14241 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014242 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014243 */
Jesse Barnes80824002009-09-10 15:28:06 -070014244 intel_crtc->pipe = pipe;
14245 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014246 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014247 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014248 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014249 }
14250
Chris Wilson4b0e3332014-05-30 16:35:26 +030014251 intel_crtc->cursor_base = ~0;
14252 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014253 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014254
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014255 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14256 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14257 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14258 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14259
Jesse Barnes79e53942008-11-07 14:24:08 -080014260 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014261
14262 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014263 return;
14264
14265fail:
14266 if (primary)
14267 drm_plane_cleanup(primary);
14268 if (cursor)
14269 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014270 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014271 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014272}
14273
Jesse Barnes752aa882013-10-31 18:55:49 +020014274enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14275{
14276 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014277 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014278
Rob Clark51fd3712013-11-19 12:10:12 -050014279 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014280
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014281 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014282 return INVALID_PIPE;
14283
14284 return to_intel_crtc(encoder->crtc)->pipe;
14285}
14286
Carl Worth08d7b3d2009-04-29 14:43:54 -070014287int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014288 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014289{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014290 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014291 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014292 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014293
Rob Clark7707e652014-07-17 23:30:04 -040014294 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014295
Rob Clark7707e652014-07-17 23:30:04 -040014296 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014297 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014298 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014299 }
14300
Rob Clark7707e652014-07-17 23:30:04 -040014301 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014302 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014303
Daniel Vetterc05422d2009-08-11 16:05:30 +020014304 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014305}
14306
Daniel Vetter66a92782012-07-12 20:08:18 +020014307static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014308{
Daniel Vetter66a92782012-07-12 20:08:18 +020014309 struct drm_device *dev = encoder->base.dev;
14310 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014311 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014312 int entry = 0;
14313
Damien Lespiaub2784e12014-08-05 11:29:37 +010014314 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014315 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014316 index_mask |= (1 << entry);
14317
Jesse Barnes79e53942008-11-07 14:24:08 -080014318 entry++;
14319 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014320
Jesse Barnes79e53942008-11-07 14:24:08 -080014321 return index_mask;
14322}
14323
Chris Wilson4d302442010-12-14 19:21:29 +000014324static bool has_edp_a(struct drm_device *dev)
14325{
14326 struct drm_i915_private *dev_priv = dev->dev_private;
14327
14328 if (!IS_MOBILE(dev))
14329 return false;
14330
14331 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14332 return false;
14333
Damien Lespiaue3589902014-02-07 19:12:50 +000014334 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014335 return false;
14336
14337 return true;
14338}
14339
Jesse Barnes84b4e042014-06-25 08:24:29 -070014340static bool intel_crt_present(struct drm_device *dev)
14341{
14342 struct drm_i915_private *dev_priv = dev->dev_private;
14343
Damien Lespiau884497e2013-12-03 13:56:23 +000014344 if (INTEL_INFO(dev)->gen >= 9)
14345 return false;
14346
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014347 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014348 return false;
14349
14350 if (IS_CHERRYVIEW(dev))
14351 return false;
14352
14353 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14354 return false;
14355
14356 return true;
14357}
14358
Jesse Barnes79e53942008-11-07 14:24:08 -080014359static void intel_setup_outputs(struct drm_device *dev)
14360{
Eric Anholt725e30a2009-01-22 13:01:02 -080014361 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014362 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014363 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014364
Daniel Vetterc9093352013-06-06 22:22:47 +020014365 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014366
Jesse Barnes84b4e042014-06-25 08:24:29 -070014367 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014368 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014369
Vandana Kannanc776eb22014-08-19 12:05:01 +053014370 if (IS_BROXTON(dev)) {
14371 /*
14372 * FIXME: Broxton doesn't support port detection via the
14373 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14374 * detect the ports.
14375 */
14376 intel_ddi_init(dev, PORT_A);
14377 intel_ddi_init(dev, PORT_B);
14378 intel_ddi_init(dev, PORT_C);
14379 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014380 int found;
14381
Jesse Barnesde31fac2015-03-06 15:53:32 -080014382 /*
14383 * Haswell uses DDI functions to detect digital outputs.
14384 * On SKL pre-D0 the strap isn't connected, so we assume
14385 * it's there.
14386 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014387 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014388 /* WaIgnoreDDIAStrap: skl */
14389 if (found ||
14390 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014391 intel_ddi_init(dev, PORT_A);
14392
14393 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14394 * register */
14395 found = I915_READ(SFUSE_STRAP);
14396
14397 if (found & SFUSE_STRAP_DDIB_DETECTED)
14398 intel_ddi_init(dev, PORT_B);
14399 if (found & SFUSE_STRAP_DDIC_DETECTED)
14400 intel_ddi_init(dev, PORT_C);
14401 if (found & SFUSE_STRAP_DDID_DETECTED)
14402 intel_ddi_init(dev, PORT_D);
14403 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014404 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014405 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014406
14407 if (has_edp_a(dev))
14408 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014409
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014410 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014411 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014412 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014413 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014414 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014415 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014416 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014417 }
14418
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014419 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014420 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014421
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014422 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014423 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014424
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014425 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014426 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014427
Daniel Vetter270b3042012-10-27 15:52:05 +020014428 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014429 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014430 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014431 /*
14432 * The DP_DETECTED bit is the latched state of the DDC
14433 * SDA pin at boot. However since eDP doesn't require DDC
14434 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14435 * eDP ports may have been muxed to an alternate function.
14436 * Thus we can't rely on the DP_DETECTED bit alone to detect
14437 * eDP ports. Consult the VBT as well as DP_DETECTED to
14438 * detect eDP ports.
14439 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014440 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14441 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014442 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14443 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014444 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14445 intel_dp_is_edp(dev, PORT_B))
14446 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014447
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014448 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14449 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014450 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14451 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014452 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14453 intel_dp_is_edp(dev, PORT_C))
14454 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014455
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014456 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014457 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014458 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14459 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014460 /* eDP not supported on port D, so don't check VBT */
14461 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14462 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014463 }
14464
Jani Nikula3cfca972013-08-27 15:12:26 +030014465 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014466 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014467 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014468
Paulo Zanonie2debe92013-02-18 19:00:27 -030014469 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014470 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014471 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014472 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14473 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014474 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014475 }
Ma Ling27185ae2009-08-24 13:50:23 +080014476
Imre Deake7281ea2013-05-08 13:14:08 +030014477 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014478 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014479 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014480
14481 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014482
Paulo Zanonie2debe92013-02-18 19:00:27 -030014483 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014484 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014485 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014486 }
Ma Ling27185ae2009-08-24 13:50:23 +080014487
Paulo Zanonie2debe92013-02-18 19:00:27 -030014488 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014489
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014490 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14491 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014492 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014493 }
Imre Deake7281ea2013-05-08 13:14:08 +030014494 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014495 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014496 }
Ma Ling27185ae2009-08-24 13:50:23 +080014497
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014498 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014499 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014500 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014501 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014502 intel_dvo_init(dev);
14503
Zhenyu Wang103a1962009-11-27 11:44:36 +080014504 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014505 intel_tv_init(dev);
14506
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014507 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014508
Damien Lespiaub2784e12014-08-05 11:29:37 +010014509 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014510 encoder->base.possible_crtcs = encoder->crtc_mask;
14511 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014512 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014513 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014514
Paulo Zanonidde86e22012-12-01 12:04:25 -020014515 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014516
14517 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014518}
14519
14520static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14521{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014522 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014523 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014524
Daniel Vetteref2d6332014-02-10 18:00:38 +010014525 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014526 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014527 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014528 drm_gem_object_unreference(&intel_fb->obj->base);
14529 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014530 kfree(intel_fb);
14531}
14532
14533static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014534 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014535 unsigned int *handle)
14536{
14537 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014538 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014539
Chris Wilson05394f32010-11-08 19:18:58 +000014540 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014541}
14542
14543static const struct drm_framebuffer_funcs intel_fb_funcs = {
14544 .destroy = intel_user_framebuffer_destroy,
14545 .create_handle = intel_user_framebuffer_create_handle,
14546};
14547
Damien Lespiaub3218032015-02-27 11:15:18 +000014548static
14549u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14550 uint32_t pixel_format)
14551{
14552 u32 gen = INTEL_INFO(dev)->gen;
14553
14554 if (gen >= 9) {
14555 /* "The stride in bytes must not exceed the of the size of 8K
14556 * pixels and 32K bytes."
14557 */
14558 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14559 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14560 return 32*1024;
14561 } else if (gen >= 4) {
14562 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14563 return 16*1024;
14564 else
14565 return 32*1024;
14566 } else if (gen >= 3) {
14567 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14568 return 8*1024;
14569 else
14570 return 16*1024;
14571 } else {
14572 /* XXX DSPC is limited to 4k tiled */
14573 return 8*1024;
14574 }
14575}
14576
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014577static int intel_framebuffer_init(struct drm_device *dev,
14578 struct intel_framebuffer *intel_fb,
14579 struct drm_mode_fb_cmd2 *mode_cmd,
14580 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014581{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014582 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014583 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014584 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014585
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014586 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14587
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014588 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14589 /* Enforce that fb modifier and tiling mode match, but only for
14590 * X-tiled. This is needed for FBC. */
14591 if (!!(obj->tiling_mode == I915_TILING_X) !=
14592 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14593 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14594 return -EINVAL;
14595 }
14596 } else {
14597 if (obj->tiling_mode == I915_TILING_X)
14598 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14599 else if (obj->tiling_mode == I915_TILING_Y) {
14600 DRM_DEBUG("No Y tiling for legacy addfb\n");
14601 return -EINVAL;
14602 }
14603 }
14604
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014605 /* Passed in modifier sanity checking. */
14606 switch (mode_cmd->modifier[0]) {
14607 case I915_FORMAT_MOD_Y_TILED:
14608 case I915_FORMAT_MOD_Yf_TILED:
14609 if (INTEL_INFO(dev)->gen < 9) {
14610 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14611 mode_cmd->modifier[0]);
14612 return -EINVAL;
14613 }
14614 case DRM_FORMAT_MOD_NONE:
14615 case I915_FORMAT_MOD_X_TILED:
14616 break;
14617 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014618 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14619 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014620 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014621 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014622
Damien Lespiaub3218032015-02-27 11:15:18 +000014623 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14624 mode_cmd->pixel_format);
14625 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14626 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14627 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014628 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014629 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014630
Damien Lespiaub3218032015-02-27 11:15:18 +000014631 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14632 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014633 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014634 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14635 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014636 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014637 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014638 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014639 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014640
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014641 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014642 mode_cmd->pitches[0] != obj->stride) {
14643 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14644 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014645 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014646 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014647
Ville Syrjälä57779d02012-10-31 17:50:14 +020014648 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014649 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014650 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014651 case DRM_FORMAT_RGB565:
14652 case DRM_FORMAT_XRGB8888:
14653 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014654 break;
14655 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014656 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014657 DRM_DEBUG("unsupported pixel format: %s\n",
14658 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014659 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014660 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014661 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014662 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014663 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14664 DRM_DEBUG("unsupported pixel format: %s\n",
14665 drm_get_format_name(mode_cmd->pixel_format));
14666 return -EINVAL;
14667 }
14668 break;
14669 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014670 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014671 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014672 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014673 DRM_DEBUG("unsupported pixel format: %s\n",
14674 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014675 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014676 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014677 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014678 case DRM_FORMAT_ABGR2101010:
14679 if (!IS_VALLEYVIEW(dev)) {
14680 DRM_DEBUG("unsupported pixel format: %s\n",
14681 drm_get_format_name(mode_cmd->pixel_format));
14682 return -EINVAL;
14683 }
14684 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014685 case DRM_FORMAT_YUYV:
14686 case DRM_FORMAT_UYVY:
14687 case DRM_FORMAT_YVYU:
14688 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014689 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014690 DRM_DEBUG("unsupported pixel format: %s\n",
14691 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014692 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014693 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014694 break;
14695 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014696 DRM_DEBUG("unsupported pixel format: %s\n",
14697 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014698 return -EINVAL;
14699 }
14700
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014701 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14702 if (mode_cmd->offsets[0] != 0)
14703 return -EINVAL;
14704
Damien Lespiauec2c9812015-01-20 12:51:45 +000014705 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014706 mode_cmd->pixel_format,
14707 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014708 /* FIXME drm helper for size checks (especially planar formats)? */
14709 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14710 return -EINVAL;
14711
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014712 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14713 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014714 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014715
Jesse Barnes79e53942008-11-07 14:24:08 -080014716 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14717 if (ret) {
14718 DRM_ERROR("framebuffer init failed %d\n", ret);
14719 return ret;
14720 }
14721
Jesse Barnes79e53942008-11-07 14:24:08 -080014722 return 0;
14723}
14724
Jesse Barnes79e53942008-11-07 14:24:08 -080014725static struct drm_framebuffer *
14726intel_user_framebuffer_create(struct drm_device *dev,
14727 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014728 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014729{
Chris Wilson05394f32010-11-08 19:18:58 +000014730 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014731
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014732 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14733 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014734 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014735 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014736
Chris Wilsond2dff872011-04-19 08:36:26 +010014737 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014738}
14739
Daniel Vetter4520f532013-10-09 09:18:51 +020014740#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014741static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014742{
14743}
14744#endif
14745
Jesse Barnes79e53942008-11-07 14:24:08 -080014746static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014747 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014748 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014749 .atomic_check = intel_atomic_check,
14750 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014751};
14752
Jesse Barnese70236a2009-09-21 10:42:27 -070014753/* Set up chip specific display functions */
14754static void intel_init_display(struct drm_device *dev)
14755{
14756 struct drm_i915_private *dev_priv = dev->dev_private;
14757
Daniel Vetteree9300b2013-06-03 22:40:22 +020014758 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14759 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014760 else if (IS_CHERRYVIEW(dev))
14761 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014762 else if (IS_VALLEYVIEW(dev))
14763 dev_priv->display.find_dpll = vlv_find_best_dpll;
14764 else if (IS_PINEVIEW(dev))
14765 dev_priv->display.find_dpll = pnv_find_best_dpll;
14766 else
14767 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14768
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014769 if (INTEL_INFO(dev)->gen >= 9) {
14770 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014771 dev_priv->display.get_initial_plane_config =
14772 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014773 dev_priv->display.crtc_compute_clock =
14774 haswell_crtc_compute_clock;
14775 dev_priv->display.crtc_enable = haswell_crtc_enable;
14776 dev_priv->display.crtc_disable = haswell_crtc_disable;
14777 dev_priv->display.off = ironlake_crtc_off;
14778 dev_priv->display.update_primary_plane =
14779 skylake_update_primary_plane;
14780 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014781 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014782 dev_priv->display.get_initial_plane_config =
14783 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014784 dev_priv->display.crtc_compute_clock =
14785 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014786 dev_priv->display.crtc_enable = haswell_crtc_enable;
14787 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014788 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014789 dev_priv->display.update_primary_plane =
14790 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014791 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014792 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014793 dev_priv->display.get_initial_plane_config =
14794 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014795 dev_priv->display.crtc_compute_clock =
14796 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014797 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14798 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014799 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014800 dev_priv->display.update_primary_plane =
14801 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014802 } else if (IS_VALLEYVIEW(dev)) {
14803 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014804 dev_priv->display.get_initial_plane_config =
14805 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014806 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014807 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14808 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14809 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014810 dev_priv->display.update_primary_plane =
14811 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014812 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014813 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014814 dev_priv->display.get_initial_plane_config =
14815 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014816 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014817 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14818 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014819 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014820 dev_priv->display.update_primary_plane =
14821 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014822 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014823
Jesse Barnese70236a2009-09-21 10:42:27 -070014824 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014825 if (IS_SKYLAKE(dev))
14826 dev_priv->display.get_display_clock_speed =
14827 skylake_get_display_clock_speed;
14828 else if (IS_BROADWELL(dev))
14829 dev_priv->display.get_display_clock_speed =
14830 broadwell_get_display_clock_speed;
14831 else if (IS_HASWELL(dev))
14832 dev_priv->display.get_display_clock_speed =
14833 haswell_get_display_clock_speed;
14834 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014835 dev_priv->display.get_display_clock_speed =
14836 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014837 else if (IS_GEN5(dev))
14838 dev_priv->display.get_display_clock_speed =
14839 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014840 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014841 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014842 dev_priv->display.get_display_clock_speed =
14843 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014844 else if (IS_GM45(dev))
14845 dev_priv->display.get_display_clock_speed =
14846 gm45_get_display_clock_speed;
14847 else if (IS_CRESTLINE(dev))
14848 dev_priv->display.get_display_clock_speed =
14849 i965gm_get_display_clock_speed;
14850 else if (IS_PINEVIEW(dev))
14851 dev_priv->display.get_display_clock_speed =
14852 pnv_get_display_clock_speed;
14853 else if (IS_G33(dev) || IS_G4X(dev))
14854 dev_priv->display.get_display_clock_speed =
14855 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014856 else if (IS_I915G(dev))
14857 dev_priv->display.get_display_clock_speed =
14858 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014859 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014860 dev_priv->display.get_display_clock_speed =
14861 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014862 else if (IS_PINEVIEW(dev))
14863 dev_priv->display.get_display_clock_speed =
14864 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014865 else if (IS_I915GM(dev))
14866 dev_priv->display.get_display_clock_speed =
14867 i915gm_get_display_clock_speed;
14868 else if (IS_I865G(dev))
14869 dev_priv->display.get_display_clock_speed =
14870 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014871 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014872 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014873 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014874 else { /* 830 */
14875 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014876 dev_priv->display.get_display_clock_speed =
14877 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014878 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014879
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014880 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014881 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014882 } else if (IS_GEN6(dev)) {
14883 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014884 } else if (IS_IVYBRIDGE(dev)) {
14885 /* FIXME: detect B0+ stepping and use auto training */
14886 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014887 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014888 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030014889 if (IS_BROADWELL(dev))
14890 dev_priv->display.modeset_global_resources =
14891 broadwell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014892 } else if (IS_VALLEYVIEW(dev)) {
14893 dev_priv->display.modeset_global_resources =
14894 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014895 } else if (IS_BROXTON(dev)) {
14896 dev_priv->display.modeset_global_resources =
14897 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014898 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014899
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014900 switch (INTEL_INFO(dev)->gen) {
14901 case 2:
14902 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14903 break;
14904
14905 case 3:
14906 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14907 break;
14908
14909 case 4:
14910 case 5:
14911 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14912 break;
14913
14914 case 6:
14915 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14916 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014917 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014918 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014919 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14920 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014921 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014922 /* Drop through - unsupported since execlist only. */
14923 default:
14924 /* Default just returns -ENODEV to indicate unsupported */
14925 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014926 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014927
14928 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014929
14930 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014931}
14932
Jesse Barnesb690e962010-07-19 13:53:12 -070014933/*
14934 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14935 * resume, or other times. This quirk makes sure that's the case for
14936 * affected systems.
14937 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014938static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014939{
14940 struct drm_i915_private *dev_priv = dev->dev_private;
14941
14942 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014943 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014944}
14945
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014946static void quirk_pipeb_force(struct drm_device *dev)
14947{
14948 struct drm_i915_private *dev_priv = dev->dev_private;
14949
14950 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14951 DRM_INFO("applying pipe b force quirk\n");
14952}
14953
Keith Packard435793d2011-07-12 14:56:22 -070014954/*
14955 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14956 */
14957static void quirk_ssc_force_disable(struct drm_device *dev)
14958{
14959 struct drm_i915_private *dev_priv = dev->dev_private;
14960 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014961 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014962}
14963
Carsten Emde4dca20e2012-03-15 15:56:26 +010014964/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014965 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14966 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014967 */
14968static void quirk_invert_brightness(struct drm_device *dev)
14969{
14970 struct drm_i915_private *dev_priv = dev->dev_private;
14971 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014972 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014973}
14974
Scot Doyle9c72cc62014-07-03 23:27:50 +000014975/* Some VBT's incorrectly indicate no backlight is present */
14976static void quirk_backlight_present(struct drm_device *dev)
14977{
14978 struct drm_i915_private *dev_priv = dev->dev_private;
14979 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14980 DRM_INFO("applying backlight present quirk\n");
14981}
14982
Jesse Barnesb690e962010-07-19 13:53:12 -070014983struct intel_quirk {
14984 int device;
14985 int subsystem_vendor;
14986 int subsystem_device;
14987 void (*hook)(struct drm_device *dev);
14988};
14989
Egbert Eich5f85f172012-10-14 15:46:38 +020014990/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14991struct intel_dmi_quirk {
14992 void (*hook)(struct drm_device *dev);
14993 const struct dmi_system_id (*dmi_id_list)[];
14994};
14995
14996static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14997{
14998 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14999 return 1;
15000}
15001
15002static const struct intel_dmi_quirk intel_dmi_quirks[] = {
15003 {
15004 .dmi_id_list = &(const struct dmi_system_id[]) {
15005 {
15006 .callback = intel_dmi_reverse_brightness,
15007 .ident = "NCR Corporation",
15008 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
15009 DMI_MATCH(DMI_PRODUCT_NAME, ""),
15010 },
15011 },
15012 { } /* terminating entry */
15013 },
15014 .hook = quirk_invert_brightness,
15015 },
15016};
15017
Ben Widawskyc43b5632012-04-16 14:07:40 -070015018static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070015019 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
15020 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
15021
Jesse Barnesb690e962010-07-19 13:53:12 -070015022 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
15023 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
15024
Ville Syrjälä5f080c02014-08-15 01:22:06 +030015025 /* 830 needs to leave pipe A & dpll A up */
15026 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
15027
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030015028 /* 830 needs to leave pipe B & dpll B up */
15029 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
15030
Keith Packard435793d2011-07-12 14:56:22 -070015031 /* Lenovo U160 cannot use SSC on LVDS */
15032 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020015033
15034 /* Sony Vaio Y cannot use SSC on LVDS */
15035 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010015036
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010015037 /* Acer Aspire 5734Z must invert backlight brightness */
15038 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
15039
15040 /* Acer/eMachines G725 */
15041 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
15042
15043 /* Acer/eMachines e725 */
15044 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
15045
15046 /* Acer/Packard Bell NCL20 */
15047 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
15048
15049 /* Acer Aspire 4736Z */
15050 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020015051
15052 /* Acer Aspire 5336 */
15053 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000015054
15055 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
15056 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000015057
Scot Doyledfb3d47b2014-08-21 16:08:02 +000015058 /* Acer C720 Chromebook (Core i3 4005U) */
15059 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
15060
jens steinb2a96012014-10-28 20:25:53 +010015061 /* Apple Macbook 2,1 (Core 2 T7400) */
15062 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15063
Scot Doyled4967d82014-07-03 23:27:52 +000015064 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15065 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015066
15067 /* HP Chromebook 14 (Celeron 2955U) */
15068 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015069
15070 /* Dell Chromebook 11 */
15071 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015072};
15073
15074static void intel_init_quirks(struct drm_device *dev)
15075{
15076 struct pci_dev *d = dev->pdev;
15077 int i;
15078
15079 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15080 struct intel_quirk *q = &intel_quirks[i];
15081
15082 if (d->device == q->device &&
15083 (d->subsystem_vendor == q->subsystem_vendor ||
15084 q->subsystem_vendor == PCI_ANY_ID) &&
15085 (d->subsystem_device == q->subsystem_device ||
15086 q->subsystem_device == PCI_ANY_ID))
15087 q->hook(dev);
15088 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015089 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15090 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15091 intel_dmi_quirks[i].hook(dev);
15092 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015093}
15094
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015095/* Disable the VGA plane that we never use */
15096static void i915_disable_vga(struct drm_device *dev)
15097{
15098 struct drm_i915_private *dev_priv = dev->dev_private;
15099 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015100 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015101
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015102 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015103 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015104 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015105 sr1 = inb(VGA_SR_DATA);
15106 outb(sr1 | 1<<5, VGA_SR_DATA);
15107 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15108 udelay(300);
15109
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015110 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015111 POSTING_READ(vga_reg);
15112}
15113
Daniel Vetterf8175862012-04-10 15:50:11 +020015114void intel_modeset_init_hw(struct drm_device *dev)
15115{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015116 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015117 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015118 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015119 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015120}
15121
Jesse Barnes79e53942008-11-07 14:24:08 -080015122void intel_modeset_init(struct drm_device *dev)
15123{
Jesse Barnes652c3932009-08-17 13:31:43 -070015124 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015125 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015126 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015127 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015128
15129 drm_mode_config_init(dev);
15130
15131 dev->mode_config.min_width = 0;
15132 dev->mode_config.min_height = 0;
15133
Dave Airlie019d96c2011-09-29 16:20:42 +010015134 dev->mode_config.preferred_depth = 24;
15135 dev->mode_config.prefer_shadow = 1;
15136
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015137 dev->mode_config.allow_fb_modifiers = true;
15138
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015139 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015140
Jesse Barnesb690e962010-07-19 13:53:12 -070015141 intel_init_quirks(dev);
15142
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015143 intel_init_pm(dev);
15144
Ben Widawskye3c74752013-04-05 13:12:39 -070015145 if (INTEL_INFO(dev)->num_pipes == 0)
15146 return;
15147
Jesse Barnese70236a2009-09-21 10:42:27 -070015148 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015149 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015150
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015151 if (IS_GEN2(dev)) {
15152 dev->mode_config.max_width = 2048;
15153 dev->mode_config.max_height = 2048;
15154 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015155 dev->mode_config.max_width = 4096;
15156 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015157 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015158 dev->mode_config.max_width = 8192;
15159 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015160 }
Damien Lespiau068be562014-03-28 14:17:49 +000015161
Ville Syrjälädc41c152014-08-13 11:57:05 +030015162 if (IS_845G(dev) || IS_I865G(dev)) {
15163 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15164 dev->mode_config.cursor_height = 1023;
15165 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015166 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15167 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15168 } else {
15169 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15170 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15171 }
15172
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015173 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015174
Zhao Yakui28c97732009-10-09 11:39:41 +080015175 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015176 INTEL_INFO(dev)->num_pipes,
15177 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015178
Damien Lespiau055e3932014-08-18 13:49:10 +010015179 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015180 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015181 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015182 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015183 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015184 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015185 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015186 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015187 }
15188
Jesse Barnesf42bb702013-12-16 16:34:23 -080015189 intel_init_dpio(dev);
15190
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015191 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015192
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015193 /* Just disable it once at startup */
15194 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015195 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015196
15197 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015198 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015199
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015200 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015201 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015202 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015203
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015204 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015205 if (!crtc->active)
15206 continue;
15207
Jesse Barnes46f297f2014-03-07 08:57:48 -080015208 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015209 * Note that reserving the BIOS fb up front prevents us
15210 * from stuffing other stolen allocations like the ring
15211 * on top. This prevents some ugliness at boot time, and
15212 * can even allow for smooth boot transitions if the BIOS
15213 * fb is large enough for the active pipe configuration.
15214 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015215 if (dev_priv->display.get_initial_plane_config) {
15216 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015217 &crtc->plane_config);
15218 /*
15219 * If the fb is shared between multiple heads, we'll
15220 * just get the first one.
15221 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015222 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015223 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015224 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015225}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015226
Daniel Vetter7fad7982012-07-04 17:51:47 +020015227static void intel_enable_pipe_a(struct drm_device *dev)
15228{
15229 struct intel_connector *connector;
15230 struct drm_connector *crt = NULL;
15231 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015232 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015233
15234 /* We can't just switch on the pipe A, we need to set things up with a
15235 * proper mode and output configuration. As a gross hack, enable pipe A
15236 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015237 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015238 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15239 crt = &connector->base;
15240 break;
15241 }
15242 }
15243
15244 if (!crt)
15245 return;
15246
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015247 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015248 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015249}
15250
Daniel Vetterfa555832012-10-10 23:14:00 +020015251static bool
15252intel_check_plane_mapping(struct intel_crtc *crtc)
15253{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015254 struct drm_device *dev = crtc->base.dev;
15255 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015256 u32 reg, val;
15257
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015258 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015259 return true;
15260
15261 reg = DSPCNTR(!crtc->plane);
15262 val = I915_READ(reg);
15263
15264 if ((val & DISPLAY_PLANE_ENABLE) &&
15265 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15266 return false;
15267
15268 return true;
15269}
15270
Daniel Vetter24929352012-07-02 20:28:59 +020015271static void intel_sanitize_crtc(struct intel_crtc *crtc)
15272{
15273 struct drm_device *dev = crtc->base.dev;
15274 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015275 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020015276
Daniel Vetter24929352012-07-02 20:28:59 +020015277 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015278 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015279 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15280
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015281 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015282 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015283 if (crtc->active) {
15284 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015285 drm_crtc_vblank_on(&crtc->base);
15286 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015287
Daniel Vetter24929352012-07-02 20:28:59 +020015288 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015289 * disable the crtc (and hence change the state) if it is wrong. Note
15290 * that gen4+ has a fixed plane -> pipe mapping. */
15291 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015292 struct intel_connector *connector;
15293 bool plane;
15294
Daniel Vetter24929352012-07-02 20:28:59 +020015295 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15296 crtc->base.base.id);
15297
15298 /* Pipe has the wrong plane attached and the plane is active.
15299 * Temporarily change the plane mapping and disable everything
15300 * ... */
15301 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015302 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015303 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030015304 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015305 dev_priv->display.crtc_disable(&crtc->base);
15306 crtc->plane = plane;
15307
15308 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015309 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015310 if (connector->encoder->base.crtc != &crtc->base)
15311 continue;
15312
Egbert Eich7f1950f2014-04-25 10:56:22 +020015313 connector->base.dpms = DRM_MODE_DPMS_OFF;
15314 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015315 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015316 /* multiple connectors may have the same encoder:
15317 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015318 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020015319 if (connector->encoder->base.crtc == &crtc->base) {
15320 connector->encoder->base.crtc = NULL;
15321 connector->encoder->connectors_active = false;
15322 }
Daniel Vetter24929352012-07-02 20:28:59 +020015323
15324 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080015325 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015326 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015327 crtc->base.enabled = false;
15328 }
Daniel Vetter24929352012-07-02 20:28:59 +020015329
Daniel Vetter7fad7982012-07-04 17:51:47 +020015330 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15331 crtc->pipe == PIPE_A && !crtc->active) {
15332 /* BIOS forgot to enable pipe A, this mostly happens after
15333 * resume. Force-enable the pipe to fix this, the update_dpms
15334 * call below we restore the pipe to the right state, but leave
15335 * the required bits on. */
15336 intel_enable_pipe_a(dev);
15337 }
15338
Daniel Vetter24929352012-07-02 20:28:59 +020015339 /* Adjust the state of the output pipe according to whether we
15340 * have active connectors/encoders. */
15341 intel_crtc_update_dpms(&crtc->base);
15342
Matt Roper83d65732015-02-25 13:12:16 -080015343 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020015344 struct intel_encoder *encoder;
15345
15346 /* This can happen either due to bugs in the get_hw_state
15347 * functions or because the pipe is force-enabled due to the
15348 * pipe A quirk. */
15349 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15350 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015351 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015352 crtc->active ? "enabled" : "disabled");
15353
Matt Roper83d65732015-02-25 13:12:16 -080015354 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015355 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015356 crtc->base.enabled = crtc->active;
15357
15358 /* Because we only establish the connector -> encoder ->
15359 * crtc links if something is active, this means the
15360 * crtc is now deactivated. Break the links. connector
15361 * -> encoder links are only establish when things are
15362 * actually up, hence no need to break them. */
15363 WARN_ON(crtc->active);
15364
15365 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15366 WARN_ON(encoder->connectors_active);
15367 encoder->base.crtc = NULL;
15368 }
15369 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015370
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015371 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015372 /*
15373 * We start out with underrun reporting disabled to avoid races.
15374 * For correct bookkeeping mark this on active crtcs.
15375 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015376 * Also on gmch platforms we dont have any hardware bits to
15377 * disable the underrun reporting. Which means we need to start
15378 * out with underrun reporting disabled also on inactive pipes,
15379 * since otherwise we'll complain about the garbage we read when
15380 * e.g. coming up after runtime pm.
15381 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015382 * No protection against concurrent access is required - at
15383 * worst a fifo underrun happens which also sets this to false.
15384 */
15385 crtc->cpu_fifo_underrun_disabled = true;
15386 crtc->pch_fifo_underrun_disabled = true;
15387 }
Daniel Vetter24929352012-07-02 20:28:59 +020015388}
15389
15390static void intel_sanitize_encoder(struct intel_encoder *encoder)
15391{
15392 struct intel_connector *connector;
15393 struct drm_device *dev = encoder->base.dev;
15394
15395 /* We need to check both for a crtc link (meaning that the
15396 * encoder is active and trying to read from a pipe) and the
15397 * pipe itself being active. */
15398 bool has_active_crtc = encoder->base.crtc &&
15399 to_intel_crtc(encoder->base.crtc)->active;
15400
15401 if (encoder->connectors_active && !has_active_crtc) {
15402 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15403 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015404 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015405
15406 /* Connector is active, but has no active pipe. This is
15407 * fallout from our resume register restoring. Disable
15408 * the encoder manually again. */
15409 if (encoder->base.crtc) {
15410 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15411 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015412 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015413 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015414 if (encoder->post_disable)
15415 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015416 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015417 encoder->base.crtc = NULL;
15418 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015419
15420 /* Inconsistent output/port/pipe state happens presumably due to
15421 * a bug in one of the get_hw_state functions. Or someplace else
15422 * in our code, like the register restore mess on resume. Clamp
15423 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015424 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015425 if (connector->encoder != encoder)
15426 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015427 connector->base.dpms = DRM_MODE_DPMS_OFF;
15428 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015429 }
15430 }
15431 /* Enabled encoders without active connectors will be fixed in
15432 * the crtc fixup. */
15433}
15434
Imre Deak04098752014-02-18 00:02:16 +020015435void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015436{
15437 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015438 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015439
Imre Deak04098752014-02-18 00:02:16 +020015440 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15441 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15442 i915_disable_vga(dev);
15443 }
15444}
15445
15446void i915_redisable_vga(struct drm_device *dev)
15447{
15448 struct drm_i915_private *dev_priv = dev->dev_private;
15449
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015450 /* This function can be called both from intel_modeset_setup_hw_state or
15451 * at a very early point in our resume sequence, where the power well
15452 * structures are not yet restored. Since this function is at a very
15453 * paranoid "someone might have enabled VGA while we were not looking"
15454 * level, just check if the power well is enabled instead of trying to
15455 * follow the "don't touch the power well if we don't need it" policy
15456 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015457 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015458 return;
15459
Imre Deak04098752014-02-18 00:02:16 +020015460 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015461}
15462
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015463static bool primary_get_hw_state(struct intel_crtc *crtc)
15464{
15465 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15466
15467 if (!crtc->active)
15468 return false;
15469
15470 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15471}
15472
Daniel Vetter30e984d2013-06-05 13:34:17 +020015473static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015474{
15475 struct drm_i915_private *dev_priv = dev->dev_private;
15476 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015477 struct intel_crtc *crtc;
15478 struct intel_encoder *encoder;
15479 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015480 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015481
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015482 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015483 struct drm_plane *primary = crtc->base.primary;
15484 struct intel_plane_state *plane_state;
15485
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015486 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015487
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015488 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015489
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015490 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015491 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015492
Matt Roper83d65732015-02-25 13:12:16 -080015493 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015494 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015495 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015496
15497 plane_state = to_intel_plane_state(primary->state);
15498 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015499
15500 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15501 crtc->base.base.id,
15502 crtc->active ? "enabled" : "disabled");
15503 }
15504
Daniel Vetter53589012013-06-05 13:34:16 +020015505 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15506 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15507
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015508 pll->on = pll->get_hw_state(dev_priv, pll,
15509 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015510 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015511 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015512 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015513 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015514 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015515 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015516 }
Daniel Vetter53589012013-06-05 13:34:16 +020015517 }
Daniel Vetter53589012013-06-05 13:34:16 +020015518
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015519 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015520 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015521
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015522 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015523 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015524 }
15525
Damien Lespiaub2784e12014-08-05 11:29:37 +010015526 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015527 pipe = 0;
15528
15529 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015530 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15531 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015532 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015533 } else {
15534 encoder->base.crtc = NULL;
15535 }
15536
15537 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015538 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015539 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015540 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015541 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015542 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015543 }
15544
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015545 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015546 if (connector->get_hw_state(connector)) {
15547 connector->base.dpms = DRM_MODE_DPMS_ON;
15548 connector->encoder->connectors_active = true;
15549 connector->base.encoder = &connector->encoder->base;
15550 } else {
15551 connector->base.dpms = DRM_MODE_DPMS_OFF;
15552 connector->base.encoder = NULL;
15553 }
15554 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15555 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015556 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015557 connector->base.encoder ? "enabled" : "disabled");
15558 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015559}
15560
15561/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15562 * and i915 state tracking structures. */
15563void intel_modeset_setup_hw_state(struct drm_device *dev,
15564 bool force_restore)
15565{
15566 struct drm_i915_private *dev_priv = dev->dev_private;
15567 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015568 struct intel_crtc *crtc;
15569 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015570 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015571
15572 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015573
Jesse Barnesbabea612013-06-26 18:57:38 +030015574 /*
15575 * Now that we have the config, copy it to each CRTC struct
15576 * Note that this could go away if we move to using crtc_config
15577 * checking everywhere.
15578 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015579 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015580 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015581 intel_mode_from_pipe_config(&crtc->base.mode,
15582 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015583 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15584 crtc->base.base.id);
15585 drm_mode_debug_printmodeline(&crtc->base.mode);
15586 }
15587 }
15588
Daniel Vetter24929352012-07-02 20:28:59 +020015589 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015590 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015591 intel_sanitize_encoder(encoder);
15592 }
15593
Damien Lespiau055e3932014-08-18 13:49:10 +010015594 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015595 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15596 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015597 intel_dump_pipe_config(crtc, crtc->config,
15598 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015599 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015600
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015601 intel_modeset_update_connector_atomic_state(dev);
15602
Daniel Vetter35c95372013-07-17 06:55:04 +020015603 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15604 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15605
15606 if (!pll->on || pll->active)
15607 continue;
15608
15609 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15610
15611 pll->disable(dev_priv, pll);
15612 pll->on = false;
15613 }
15614
Pradeep Bhat30789992014-11-04 17:06:45 +000015615 if (IS_GEN9(dev))
15616 skl_wm_get_hw_state(dev);
15617 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015618 ilk_wm_get_hw_state(dev);
15619
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015620 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015621 i915_redisable_vga(dev);
15622
Daniel Vetterf30da182013-04-11 20:22:50 +020015623 /*
15624 * We need to use raw interfaces for restoring state to avoid
15625 * checking (bogus) intermediate states.
15626 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015627 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015628 struct drm_crtc *crtc =
15629 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015630
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015631 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015632 }
15633 } else {
15634 intel_modeset_update_staged_output_state(dev);
15635 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015636
15637 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015638}
15639
15640void intel_modeset_gem_init(struct drm_device *dev)
15641{
Jesse Barnes92122782014-10-09 12:57:42 -070015642 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015643 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015644 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015645 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015646
Imre Deakae484342014-03-31 15:10:44 +030015647 mutex_lock(&dev->struct_mutex);
15648 intel_init_gt_powersave(dev);
15649 mutex_unlock(&dev->struct_mutex);
15650
Jesse Barnes92122782014-10-09 12:57:42 -070015651 /*
15652 * There may be no VBT; and if the BIOS enabled SSC we can
15653 * just keep using it to avoid unnecessary flicker. Whereas if the
15654 * BIOS isn't using it, don't assume it will work even if the VBT
15655 * indicates as much.
15656 */
15657 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15658 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15659 DREF_SSC1_ENABLE);
15660
Chris Wilson1833b132012-05-09 11:56:28 +010015661 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015662
15663 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015664
15665 /*
15666 * Make sure any fbs we allocated at startup are properly
15667 * pinned & fenced. When we do the allocation it's too early
15668 * for this.
15669 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015670 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015671 obj = intel_fb_obj(c->primary->fb);
15672 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015673 continue;
15674
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015675 mutex_lock(&dev->struct_mutex);
15676 ret = intel_pin_and_fence_fb_obj(c->primary,
15677 c->primary->fb,
15678 c->primary->state,
15679 NULL);
15680 mutex_unlock(&dev->struct_mutex);
15681 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015682 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15683 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015684 drm_framebuffer_unreference(c->primary->fb);
15685 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015686 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015687 }
15688 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015689
15690 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015691}
15692
Imre Deak4932e2c2014-02-11 17:12:48 +020015693void intel_connector_unregister(struct intel_connector *intel_connector)
15694{
15695 struct drm_connector *connector = &intel_connector->base;
15696
15697 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015698 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015699}
15700
Jesse Barnes79e53942008-11-07 14:24:08 -080015701void intel_modeset_cleanup(struct drm_device *dev)
15702{
Jesse Barnes652c3932009-08-17 13:31:43 -070015703 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015704 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015705
Imre Deak2eb52522014-11-19 15:30:05 +020015706 intel_disable_gt_powersave(dev);
15707
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015708 intel_backlight_unregister(dev);
15709
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015710 /*
15711 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015712 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015713 * experience fancy races otherwise.
15714 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015715 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015716
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015717 /*
15718 * Due to the hpd irq storm handling the hotplug work can re-arm the
15719 * poll handlers. Hence disable polling after hpd handling is shut down.
15720 */
Keith Packardf87ea762010-10-03 19:36:26 -070015721 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015722
Jesse Barnes652c3932009-08-17 13:31:43 -070015723 mutex_lock(&dev->struct_mutex);
15724
Jesse Barnes723bfd72010-10-07 16:01:13 -070015725 intel_unregister_dsm_handler();
15726
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015727 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015728
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015729 mutex_unlock(&dev->struct_mutex);
15730
Chris Wilson1630fe72011-07-08 12:22:42 +010015731 /* flush any delayed tasks or pending work */
15732 flush_scheduled_work();
15733
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015734 /* destroy the backlight and sysfs files before encoders/connectors */
15735 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015736 struct intel_connector *intel_connector;
15737
15738 intel_connector = to_intel_connector(connector);
15739 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015740 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015741
Jesse Barnes79e53942008-11-07 14:24:08 -080015742 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015743
15744 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015745
15746 mutex_lock(&dev->struct_mutex);
15747 intel_cleanup_gt_powersave(dev);
15748 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015749}
15750
Dave Airlie28d52042009-09-21 14:33:58 +100015751/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015752 * Return which encoder is currently attached for connector.
15753 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015754struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015755{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015756 return &intel_attached_encoder(connector)->base;
15757}
Jesse Barnes79e53942008-11-07 14:24:08 -080015758
Chris Wilsondf0e9242010-09-09 16:20:55 +010015759void intel_connector_attach_encoder(struct intel_connector *connector,
15760 struct intel_encoder *encoder)
15761{
15762 connector->encoder = encoder;
15763 drm_mode_connector_attach_encoder(&connector->base,
15764 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015765}
Dave Airlie28d52042009-09-21 14:33:58 +100015766
15767/*
15768 * set vga decode state - true == enable VGA decode
15769 */
15770int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15771{
15772 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015773 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015774 u16 gmch_ctrl;
15775
Chris Wilson75fa0412014-02-07 18:37:02 -020015776 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15777 DRM_ERROR("failed to read control word\n");
15778 return -EIO;
15779 }
15780
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015781 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15782 return 0;
15783
Dave Airlie28d52042009-09-21 14:33:58 +100015784 if (state)
15785 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15786 else
15787 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015788
15789 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15790 DRM_ERROR("failed to write control word\n");
15791 return -EIO;
15792 }
15793
Dave Airlie28d52042009-09-21 14:33:58 +100015794 return 0;
15795}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015796
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015797struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015798
15799 u32 power_well_driver;
15800
Chris Wilson63b66e52013-08-08 15:12:06 +020015801 int num_transcoders;
15802
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015803 struct intel_cursor_error_state {
15804 u32 control;
15805 u32 position;
15806 u32 base;
15807 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015808 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015809
15810 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015811 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015812 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015813 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015814 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015815
15816 struct intel_plane_error_state {
15817 u32 control;
15818 u32 stride;
15819 u32 size;
15820 u32 pos;
15821 u32 addr;
15822 u32 surface;
15823 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015824 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015825
15826 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015827 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015828 enum transcoder cpu_transcoder;
15829
15830 u32 conf;
15831
15832 u32 htotal;
15833 u32 hblank;
15834 u32 hsync;
15835 u32 vtotal;
15836 u32 vblank;
15837 u32 vsync;
15838 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015839};
15840
15841struct intel_display_error_state *
15842intel_display_capture_error_state(struct drm_device *dev)
15843{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015844 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015845 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015846 int transcoders[] = {
15847 TRANSCODER_A,
15848 TRANSCODER_B,
15849 TRANSCODER_C,
15850 TRANSCODER_EDP,
15851 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015852 int i;
15853
Chris Wilson63b66e52013-08-08 15:12:06 +020015854 if (INTEL_INFO(dev)->num_pipes == 0)
15855 return NULL;
15856
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015857 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015858 if (error == NULL)
15859 return NULL;
15860
Imre Deak190be112013-11-25 17:15:31 +020015861 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015862 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15863
Damien Lespiau055e3932014-08-18 13:49:10 +010015864 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015865 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015866 __intel_display_power_is_enabled(dev_priv,
15867 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015868 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015869 continue;
15870
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015871 error->cursor[i].control = I915_READ(CURCNTR(i));
15872 error->cursor[i].position = I915_READ(CURPOS(i));
15873 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015874
15875 error->plane[i].control = I915_READ(DSPCNTR(i));
15876 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015877 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015878 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015879 error->plane[i].pos = I915_READ(DSPPOS(i));
15880 }
Paulo Zanonica291362013-03-06 20:03:14 -030015881 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15882 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015883 if (INTEL_INFO(dev)->gen >= 4) {
15884 error->plane[i].surface = I915_READ(DSPSURF(i));
15885 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15886 }
15887
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015888 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015889
Sonika Jindal3abfce72014-07-21 15:23:43 +053015890 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030015891 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015892 }
15893
15894 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15895 if (HAS_DDI(dev_priv->dev))
15896 error->num_transcoders++; /* Account for eDP. */
15897
15898 for (i = 0; i < error->num_transcoders; i++) {
15899 enum transcoder cpu_transcoder = transcoders[i];
15900
Imre Deakddf9c532013-11-27 22:02:02 +020015901 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015902 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015903 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015904 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015905 continue;
15906
Chris Wilson63b66e52013-08-08 15:12:06 +020015907 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15908
15909 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15910 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15911 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15912 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15913 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15914 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15915 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015916 }
15917
15918 return error;
15919}
15920
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015921#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15922
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015923void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015924intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015925 struct drm_device *dev,
15926 struct intel_display_error_state *error)
15927{
Damien Lespiau055e3932014-08-18 13:49:10 +010015928 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015929 int i;
15930
Chris Wilson63b66e52013-08-08 15:12:06 +020015931 if (!error)
15932 return;
15933
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015934 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015935 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015936 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015937 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015938 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015939 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015940 err_printf(m, " Power: %s\n",
15941 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015942 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015943 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015944
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015945 err_printf(m, "Plane [%d]:\n", i);
15946 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15947 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015948 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015949 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15950 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015951 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015952 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015953 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015954 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015955 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15956 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015957 }
15958
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015959 err_printf(m, "Cursor [%d]:\n", i);
15960 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15961 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15962 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015963 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015964
15965 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015966 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015967 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015968 err_printf(m, " Power: %s\n",
15969 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015970 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15971 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15972 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15973 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15974 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15975 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15976 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15977 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015978}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015979
15980void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15981{
15982 struct intel_crtc *crtc;
15983
15984 for_each_intel_crtc(dev, crtc) {
15985 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015986
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015987 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015988
15989 work = crtc->unpin_work;
15990
15991 if (work && work->event &&
15992 work->event->base.file_priv == file) {
15993 kfree(work->event);
15994 work->event = NULL;
15995 }
15996
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015997 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015998 }
15999}