blob: 657a33366e923396eccb7e92c019b50260b811a1 [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
1143 mutex_lock(&dev_priv->dpio_lock);
1144 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
1145 mutex_unlock(&dev_priv->dpio_lock);
1146
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
1664 mutex_lock(&dev_priv->dpio_lock);
1665
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
1671 /*
1672 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1673 */
1674 udelay(1);
1675
1676 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001677 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001678
1679 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001680 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001681 DRM_ERROR("PLL %d failed to lock\n", pipe);
1682
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001683 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001684 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001685 POSTING_READ(DPLL_MD(pipe));
1686
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001687 mutex_unlock(&dev_priv->dpio_lock);
1688}
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
1829 mutex_lock(&dev_priv->dpio_lock);
1830
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äd7520482014-04-09 13:28:59 +03001847 mutex_unlock(&dev_priv->dpio_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 /*
2006 * make the BPC in transcoder be consistent with
2007 * that in pipeconf reg.
2008 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002009 val &= ~PIPECONF_BPC_MASK;
2010 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002011 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002012
2013 val &= ~TRANS_INTERLACE_MASK;
2014 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002015 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002016 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002017 val |= TRANS_LEGACY_INTERLACED_ILK;
2018 else
2019 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002020 else
2021 val |= TRANS_PROGRESSIVE;
2022
Jesse Barnes040484a2011-01-03 12:14:26 -08002023 I915_WRITE(reg, val | TRANS_ENABLE);
2024 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002025 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002026}
2027
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002029 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002030{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032
2033 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002034 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002037 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002038 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002039
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002040 /* Workaround: set timing override bit. */
2041 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002042 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002043 I915_WRITE(_TRANSA_CHICKEN2, val);
2044
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002045 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002046 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002047
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002048 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2049 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002050 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002051 else
2052 val |= TRANS_PROGRESSIVE;
2053
Daniel Vetterab9412b2013-05-03 11:49:46 +02002054 I915_WRITE(LPT_TRANSCONF, val);
2055 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002056 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002057}
2058
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002059static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2060 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002061{
Daniel Vetter23670b322012-11-01 09:15:30 +01002062 struct drm_device *dev = dev_priv->dev;
2063 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002064
2065 /* FDI relies on the transcoder */
2066 assert_fdi_tx_disabled(dev_priv, pipe);
2067 assert_fdi_rx_disabled(dev_priv, pipe);
2068
Jesse Barnes291906f2011-02-02 12:28:03 -08002069 /* Ports must be off as well */
2070 assert_pch_ports_disabled(dev_priv, pipe);
2071
Daniel Vetterab9412b2013-05-03 11:49:46 +02002072 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002073 val = I915_READ(reg);
2074 val &= ~TRANS_ENABLE;
2075 I915_WRITE(reg, val);
2076 /* wait for PCH transcoder off, transcoder state */
2077 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002078 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002079
2080 if (!HAS_PCH_IBX(dev)) {
2081 /* Workaround: Clear the timing override chicken bit again. */
2082 reg = TRANS_CHICKEN2(pipe);
2083 val = I915_READ(reg);
2084 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2085 I915_WRITE(reg, val);
2086 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002087}
2088
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002089static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002090{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002091 u32 val;
2092
Daniel Vetterab9412b2013-05-03 11:49:46 +02002093 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002094 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002095 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002096 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002097 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002098 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002099
2100 /* Workaround: clear timing override bit. */
2101 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002102 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002103 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002104}
2105
2106/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002107 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002108 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002109 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002110 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002111 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002112 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002113static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002114{
Paulo Zanoni03722642014-01-17 13:51:09 -02002115 struct drm_device *dev = crtc->base.dev;
2116 struct drm_i915_private *dev_priv = dev->dev_private;
2117 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002118 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2119 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002120 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121 int reg;
2122 u32 val;
2123
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002124 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002125 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002126 assert_sprites_disabled(dev_priv, pipe);
2127
Paulo Zanoni681e5812012-12-06 11:12:38 -02002128 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002129 pch_transcoder = TRANSCODER_A;
2130 else
2131 pch_transcoder = pipe;
2132
Jesse Barnesb24e7172011-01-04 15:09:30 -08002133 /*
2134 * A pipe without a PLL won't actually be able to drive bits from
2135 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2136 * need the check.
2137 */
Imre Deak50360402015-01-16 00:55:16 -08002138 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002139 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002140 assert_dsi_pll_enabled(dev_priv);
2141 else
2142 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002143 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002144 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002145 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002146 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002147 assert_fdi_tx_pll_enabled(dev_priv,
2148 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002149 }
2150 /* FIXME: assert CPU port conditions for SNB+ */
2151 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002152
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002153 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002154 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002155 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002156 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2157 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002158 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002159 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002160
2161 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002162 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163}
2164
2165/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002166 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002167 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002168 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002169 * Disable the pipe of @crtc, making sure that various hardware
2170 * specific requirements are met, if applicable, e.g. plane
2171 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172 *
2173 * Will wait until the pipe has shut down before returning.
2174 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002175static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002177 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002178 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002179 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002180 int reg;
2181 u32 val;
2182
2183 /*
2184 * Make sure planes won't keep trying to pump pixels to us,
2185 * or we might hang the display.
2186 */
2187 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002188 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002189 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002190
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002191 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002192 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002193 if ((val & PIPECONF_ENABLE) == 0)
2194 return;
2195
Ville Syrjälä67adc642014-08-15 01:21:57 +03002196 /*
2197 * Double wide has implications for planes
2198 * so best keep it disabled when not needed.
2199 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002200 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002201 val &= ~PIPECONF_DOUBLE_WIDE;
2202
2203 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002204 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2205 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002206 val &= ~PIPECONF_ENABLE;
2207
2208 I915_WRITE(reg, val);
2209 if ((val & PIPECONF_ENABLE) == 0)
2210 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002211}
2212
Keith Packardd74362c2011-07-28 14:47:14 -07002213/*
2214 * Plane regs are double buffered, going from enabled->disabled needs a
2215 * trigger in order to latch. The display address reg provides this.
2216 */
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002217void intel_flush_primary_plane(struct drm_i915_private *dev_priv,
2218 enum plane plane)
Keith Packardd74362c2011-07-28 14:47:14 -07002219{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00002220 struct drm_device *dev = dev_priv->dev;
2221 u32 reg = INTEL_INFO(dev)->gen >= 4 ? DSPSURF(plane) : DSPADDR(plane);
Ville Syrjälä1dba99f2013-10-01 18:02:18 +03002222
2223 I915_WRITE(reg, I915_READ(reg));
2224 POSTING_READ(reg);
Keith Packardd74362c2011-07-28 14:47:14 -07002225}
2226
Jesse Barnesb24e7172011-01-04 15:09:30 -08002227/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002228 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002229 * @plane: plane to be enabled
2230 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002231 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002232 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002233 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002234static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2235 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002236{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002237 struct drm_device *dev = plane->dev;
2238 struct drm_i915_private *dev_priv = dev->dev_private;
2239 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002240
2241 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002242 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002243 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002244
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002245 dev_priv->display.update_primary_plane(crtc, plane->fb,
2246 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002247}
2248
Chris Wilson693db182013-03-05 14:52:39 +00002249static bool need_vtd_wa(struct drm_device *dev)
2250{
2251#ifdef CONFIG_INTEL_IOMMU
2252 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2253 return true;
2254#endif
2255 return false;
2256}
2257
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002258unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002259intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2260 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002261{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002262 unsigned int tile_height;
2263 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002264
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002265 switch (fb_format_modifier) {
2266 case DRM_FORMAT_MOD_NONE:
2267 tile_height = 1;
2268 break;
2269 case I915_FORMAT_MOD_X_TILED:
2270 tile_height = IS_GEN2(dev) ? 16 : 8;
2271 break;
2272 case I915_FORMAT_MOD_Y_TILED:
2273 tile_height = 32;
2274 break;
2275 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002276 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2277 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002278 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002279 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002280 tile_height = 64;
2281 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002282 case 2:
2283 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002284 tile_height = 32;
2285 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002286 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002287 tile_height = 16;
2288 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002289 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002290 WARN_ONCE(1,
2291 "128-bit pixels are not supported for display!");
2292 tile_height = 16;
2293 break;
2294 }
2295 break;
2296 default:
2297 MISSING_CASE(fb_format_modifier);
2298 tile_height = 1;
2299 break;
2300 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002301
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002302 return tile_height;
2303}
2304
2305unsigned int
2306intel_fb_align_height(struct drm_device *dev, unsigned int height,
2307 uint32_t pixel_format, uint64_t fb_format_modifier)
2308{
2309 return ALIGN(height, intel_tile_height(dev, pixel_format,
2310 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002311}
2312
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002313static int
2314intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2315 const struct drm_plane_state *plane_state)
2316{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002317 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002318
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002319 *view = i915_ggtt_view_normal;
2320
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002321 if (!plane_state)
2322 return 0;
2323
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002324 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002325 return 0;
2326
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002327 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002328
2329 info->height = fb->height;
2330 info->pixel_format = fb->pixel_format;
2331 info->pitch = fb->pitches[0];
2332 info->fb_modifier = fb->modifier[0];
2333
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002334 return 0;
2335}
2336
Chris Wilson127bd2a2010-07-23 23:32:05 +01002337int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002338intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2339 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002340 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002341 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002342{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002343 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002344 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002345 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002346 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002347 u32 alignment;
2348 int ret;
2349
Matt Roperebcdd392014-07-09 16:22:11 -07002350 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2351
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002352 switch (fb->modifier[0]) {
2353 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002354 if (INTEL_INFO(dev)->gen >= 9)
2355 alignment = 256 * 1024;
2356 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002357 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002358 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002359 alignment = 4 * 1024;
2360 else
2361 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002362 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002363 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002364 if (INTEL_INFO(dev)->gen >= 9)
2365 alignment = 256 * 1024;
2366 else {
2367 /* pin() will align the object as required by fence */
2368 alignment = 0;
2369 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002370 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002371 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002372 case I915_FORMAT_MOD_Yf_TILED:
2373 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2374 "Y tiling bo slipped through, driver bug!\n"))
2375 return -EINVAL;
2376 alignment = 1 * 1024 * 1024;
2377 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002378 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002379 MISSING_CASE(fb->modifier[0]);
2380 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002381 }
2382
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002383 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2384 if (ret)
2385 return ret;
2386
Chris Wilson693db182013-03-05 14:52:39 +00002387 /* Note that the w/a also requires 64 PTE of padding following the
2388 * bo. We currently fill all unused PTE with the shadow page and so
2389 * we should always have valid PTE following the scanout preventing
2390 * the VT-d warning.
2391 */
2392 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2393 alignment = 256 * 1024;
2394
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002395 /*
2396 * Global gtt pte registers are special registers which actually forward
2397 * writes to a chunk of system memory. Which means that there is no risk
2398 * that the register values disappear as soon as we call
2399 * intel_runtime_pm_put(), so it is correct to wrap only the
2400 * pin/unpin/fence and not more.
2401 */
2402 intel_runtime_pm_get(dev_priv);
2403
Chris Wilsonce453d82011-02-21 14:43:56 +00002404 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002405 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002406 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002407 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002408 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002409
2410 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2411 * fence, whereas 965+ only requires a fence if using
2412 * framebuffer compression. For simplicity, we always install
2413 * a fence as the cost is not that onerous.
2414 */
Chris Wilson06d98132012-04-17 15:31:24 +01002415 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002416 if (ret)
2417 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002418
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002419 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002420
Chris Wilsonce453d82011-02-21 14:43:56 +00002421 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002422 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002423 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002424
2425err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002426 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002427err_interruptible:
2428 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002429 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002430 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002431}
2432
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002433static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2434 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002435{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002436 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002437 struct i915_ggtt_view view;
2438 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002439
Matt Roperebcdd392014-07-09 16:22:11 -07002440 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2441
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002442 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2443 WARN_ONCE(ret, "Couldn't get view from plane state!");
2444
Chris Wilson1690e1e2011-12-14 13:57:08 +01002445 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002446 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002447}
2448
Daniel Vetterc2c75132012-07-05 12:17:30 +02002449/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2450 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002451unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2452 unsigned int tiling_mode,
2453 unsigned int cpp,
2454 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002455{
Chris Wilsonbc752862013-02-21 20:04:31 +00002456 if (tiling_mode != I915_TILING_NONE) {
2457 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002458
Chris Wilsonbc752862013-02-21 20:04:31 +00002459 tile_rows = *y / 8;
2460 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002461
Chris Wilsonbc752862013-02-21 20:04:31 +00002462 tiles = *x / (512/cpp);
2463 *x %= 512/cpp;
2464
2465 return tile_rows * pitch * 8 + tiles * 4096;
2466 } else {
2467 unsigned int offset;
2468
2469 offset = *y * pitch + *x * cpp;
2470 *y = 0;
2471 *x = (offset & 4095) / cpp;
2472 return offset & -4096;
2473 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002474}
2475
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002476static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002477{
2478 switch (format) {
2479 case DISPPLANE_8BPP:
2480 return DRM_FORMAT_C8;
2481 case DISPPLANE_BGRX555:
2482 return DRM_FORMAT_XRGB1555;
2483 case DISPPLANE_BGRX565:
2484 return DRM_FORMAT_RGB565;
2485 default:
2486 case DISPPLANE_BGRX888:
2487 return DRM_FORMAT_XRGB8888;
2488 case DISPPLANE_RGBX888:
2489 return DRM_FORMAT_XBGR8888;
2490 case DISPPLANE_BGRX101010:
2491 return DRM_FORMAT_XRGB2101010;
2492 case DISPPLANE_RGBX101010:
2493 return DRM_FORMAT_XBGR2101010;
2494 }
2495}
2496
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002497static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2498{
2499 switch (format) {
2500 case PLANE_CTL_FORMAT_RGB_565:
2501 return DRM_FORMAT_RGB565;
2502 default:
2503 case PLANE_CTL_FORMAT_XRGB_8888:
2504 if (rgb_order) {
2505 if (alpha)
2506 return DRM_FORMAT_ABGR8888;
2507 else
2508 return DRM_FORMAT_XBGR8888;
2509 } else {
2510 if (alpha)
2511 return DRM_FORMAT_ARGB8888;
2512 else
2513 return DRM_FORMAT_XRGB8888;
2514 }
2515 case PLANE_CTL_FORMAT_XRGB_2101010:
2516 if (rgb_order)
2517 return DRM_FORMAT_XBGR2101010;
2518 else
2519 return DRM_FORMAT_XRGB2101010;
2520 }
2521}
2522
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002523static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002524intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2525 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002526{
2527 struct drm_device *dev = crtc->base.dev;
2528 struct drm_i915_gem_object *obj = NULL;
2529 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002530 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002531 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2532 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2533 PAGE_SIZE);
2534
2535 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536
Chris Wilsonff2652e2014-03-10 08:07:02 +00002537 if (plane_config->size == 0)
2538 return false;
2539
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002540 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2541 base_aligned,
2542 base_aligned,
2543 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002544 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002545 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002546
Damien Lespiau49af4492015-01-20 12:51:44 +00002547 obj->tiling_mode = plane_config->tiling;
2548 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002549 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002550
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002551 mode_cmd.pixel_format = fb->pixel_format;
2552 mode_cmd.width = fb->width;
2553 mode_cmd.height = fb->height;
2554 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002555 mode_cmd.modifier[0] = fb->modifier[0];
2556 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002557
2558 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002559 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002560 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002561 DRM_DEBUG_KMS("intel fb init failed\n");
2562 goto out_unref_obj;
2563 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002564 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002565
Daniel Vetterf6936e22015-03-26 12:17:05 +01002566 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002567 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002568
2569out_unref_obj:
2570 drm_gem_object_unreference(&obj->base);
2571 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002572 return false;
2573}
2574
Matt Roperafd65eb2015-02-03 13:10:04 -08002575/* Update plane->state->fb to match plane->fb after driver-internal updates */
2576static void
2577update_state_fb(struct drm_plane *plane)
2578{
2579 if (plane->fb == plane->state->fb)
2580 return;
2581
2582 if (plane->state->fb)
2583 drm_framebuffer_unreference(plane->state->fb);
2584 plane->state->fb = plane->fb;
2585 if (plane->state->fb)
2586 drm_framebuffer_reference(plane->state->fb);
2587}
2588
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002589static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002590intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2591 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002592{
2593 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002594 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002595 struct drm_crtc *c;
2596 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002597 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002598 struct drm_plane *primary = intel_crtc->base.primary;
2599 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002600
Damien Lespiau2d140302015-02-05 17:22:18 +00002601 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602 return;
2603
Daniel Vetterf6936e22015-03-26 12:17:05 +01002604 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002605 fb = &plane_config->fb->base;
2606 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002607 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002608
Damien Lespiau2d140302015-02-05 17:22:18 +00002609 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002610
2611 /*
2612 * Failed to alloc the obj, check to see if we should share
2613 * an fb with another CRTC instead
2614 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002615 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002616 i = to_intel_crtc(c);
2617
2618 if (c == &intel_crtc->base)
2619 continue;
2620
Matt Roper2ff8fde2014-07-08 07:50:07 -07002621 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002622 continue;
2623
Daniel Vetter88595ac2015-03-26 12:42:24 +01002624 fb = c->primary->fb;
2625 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002626 continue;
2627
Daniel Vetter88595ac2015-03-26 12:42:24 +01002628 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002629 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002630 drm_framebuffer_reference(fb);
2631 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002632 }
2633 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002634
2635 return;
2636
2637valid_fb:
2638 obj = intel_fb_obj(fb);
2639 if (obj->tiling_mode != I915_TILING_NONE)
2640 dev_priv->preserve_bios_swizzle = true;
2641
2642 primary->fb = fb;
2643 primary->state->crtc = &intel_crtc->base;
2644 primary->crtc = &intel_crtc->base;
2645 update_state_fb(primary);
2646 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002647}
2648
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002649static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2650 struct drm_framebuffer *fb,
2651 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002652{
2653 struct drm_device *dev = crtc->dev;
2654 struct drm_i915_private *dev_priv = dev->dev_private;
2655 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002656 struct drm_plane *primary = crtc->primary;
2657 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002658 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002659 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002660 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002661 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002662 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302663 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002664
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002665 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002666 I915_WRITE(reg, 0);
2667 if (INTEL_INFO(dev)->gen >= 4)
2668 I915_WRITE(DSPSURF(plane), 0);
2669 else
2670 I915_WRITE(DSPADDR(plane), 0);
2671 POSTING_READ(reg);
2672 return;
2673 }
2674
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002675 obj = intel_fb_obj(fb);
2676 if (WARN_ON(obj == NULL))
2677 return;
2678
2679 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2680
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002681 dspcntr = DISPPLANE_GAMMA_ENABLE;
2682
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002683 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002684
2685 if (INTEL_INFO(dev)->gen < 4) {
2686 if (intel_crtc->pipe == PIPE_B)
2687 dspcntr |= DISPPLANE_SEL_PIPE_B;
2688
2689 /* pipesrc and dspsize control the size that is scaled from,
2690 * which should always be the user's requested size.
2691 */
2692 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002693 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2694 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002695 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002696 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2697 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002698 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2699 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002700 I915_WRITE(PRIMPOS(plane), 0);
2701 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002702 }
2703
Ville Syrjälä57779d02012-10-31 17:50:14 +02002704 switch (fb->pixel_format) {
2705 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002706 dspcntr |= DISPPLANE_8BPP;
2707 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002708 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002709 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002710 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002711 case DRM_FORMAT_RGB565:
2712 dspcntr |= DISPPLANE_BGRX565;
2713 break;
2714 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002715 dspcntr |= DISPPLANE_BGRX888;
2716 break;
2717 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002718 dspcntr |= DISPPLANE_RGBX888;
2719 break;
2720 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002721 dspcntr |= DISPPLANE_BGRX101010;
2722 break;
2723 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002724 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002725 break;
2726 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002727 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002728 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002729
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002730 if (INTEL_INFO(dev)->gen >= 4 &&
2731 obj->tiling_mode != I915_TILING_NONE)
2732 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002733
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002734 if (IS_G4X(dev))
2735 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2736
Ville Syrjäläb98971272014-08-27 16:51:22 +03002737 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002738
Daniel Vetterc2c75132012-07-05 12:17:30 +02002739 if (INTEL_INFO(dev)->gen >= 4) {
2740 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002741 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002742 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002743 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002744 linear_offset -= intel_crtc->dspaddr_offset;
2745 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002746 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002747 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002748
Matt Roper8e7d6882015-01-21 16:35:41 -08002749 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302750 dspcntr |= DISPPLANE_ROTATE_180;
2751
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002752 x += (intel_crtc->config->pipe_src_w - 1);
2753 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302754
2755 /* Finding the last pixel of the last line of the display
2756 data and adding to linear_offset*/
2757 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002758 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2759 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302760 }
2761
2762 I915_WRITE(reg, dspcntr);
2763
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002764 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002765 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002766 I915_WRITE(DSPSURF(plane),
2767 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002768 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002769 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002770 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002771 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002772 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773}
2774
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002775static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2776 struct drm_framebuffer *fb,
2777 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002778{
2779 struct drm_device *dev = crtc->dev;
2780 struct drm_i915_private *dev_priv = dev->dev_private;
2781 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002782 struct drm_plane *primary = crtc->primary;
2783 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002784 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002785 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002786 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002787 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002788 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302789 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002790
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002791 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002792 I915_WRITE(reg, 0);
2793 I915_WRITE(DSPSURF(plane), 0);
2794 POSTING_READ(reg);
2795 return;
2796 }
2797
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002798 obj = intel_fb_obj(fb);
2799 if (WARN_ON(obj == NULL))
2800 return;
2801
2802 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2803
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002804 dspcntr = DISPPLANE_GAMMA_ENABLE;
2805
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002806 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002807
2808 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2809 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2810
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 switch (fb->pixel_format) {
2812 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002813 dspcntr |= DISPPLANE_8BPP;
2814 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002815 case DRM_FORMAT_RGB565:
2816 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002817 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002818 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002819 dspcntr |= DISPPLANE_BGRX888;
2820 break;
2821 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002822 dspcntr |= DISPPLANE_RGBX888;
2823 break;
2824 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002825 dspcntr |= DISPPLANE_BGRX101010;
2826 break;
2827 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002828 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002829 break;
2830 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002831 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002832 }
2833
2834 if (obj->tiling_mode != I915_TILING_NONE)
2835 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002836
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002837 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002838 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002839
Ville Syrjäläb98971272014-08-27 16:51:22 +03002840 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002841 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002842 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002843 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002844 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002845 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002846 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302847 dspcntr |= DISPPLANE_ROTATE_180;
2848
2849 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002850 x += (intel_crtc->config->pipe_src_w - 1);
2851 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302852
2853 /* Finding the last pixel of the last line of the display
2854 data and adding to linear_offset*/
2855 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002856 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2857 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302858 }
2859 }
2860
2861 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002862
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002863 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002864 I915_WRITE(DSPSURF(plane),
2865 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002866 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002867 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2868 } else {
2869 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2870 I915_WRITE(DSPLINOFF(plane), linear_offset);
2871 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002872 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002873}
2874
Damien Lespiaub3218032015-02-27 11:15:18 +00002875u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2876 uint32_t pixel_format)
2877{
2878 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2879
2880 /*
2881 * The stride is either expressed as a multiple of 64 bytes
2882 * chunks for linear buffers or in number of tiles for tiled
2883 * buffers.
2884 */
2885 switch (fb_modifier) {
2886 case DRM_FORMAT_MOD_NONE:
2887 return 64;
2888 case I915_FORMAT_MOD_X_TILED:
2889 if (INTEL_INFO(dev)->gen == 2)
2890 return 128;
2891 return 512;
2892 case I915_FORMAT_MOD_Y_TILED:
2893 /* No need to check for old gens and Y tiling since this is
2894 * about the display engine and those will be blocked before
2895 * we get here.
2896 */
2897 return 128;
2898 case I915_FORMAT_MOD_Yf_TILED:
2899 if (bits_per_pixel == 8)
2900 return 64;
2901 else
2902 return 128;
2903 default:
2904 MISSING_CASE(fb_modifier);
2905 return 64;
2906 }
2907}
2908
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002909unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2910 struct drm_i915_gem_object *obj)
2911{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002912 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002913
2914 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002915 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002916
2917 return i915_gem_obj_ggtt_offset_view(obj, view);
2918}
2919
Chandra Kondurua1b22782015-04-07 15:28:45 -07002920/*
2921 * This function detaches (aka. unbinds) unused scalers in hardware
2922 */
2923void skl_detach_scalers(struct intel_crtc *intel_crtc)
2924{
2925 struct drm_device *dev;
2926 struct drm_i915_private *dev_priv;
2927 struct intel_crtc_scaler_state *scaler_state;
2928 int i;
2929
2930 if (!intel_crtc || !intel_crtc->config)
2931 return;
2932
2933 dev = intel_crtc->base.dev;
2934 dev_priv = dev->dev_private;
2935 scaler_state = &intel_crtc->config->scaler_state;
2936
2937 /* loop through and disable scalers that aren't in use */
2938 for (i = 0; i < intel_crtc->num_scalers; i++) {
2939 if (!scaler_state->scalers[i].in_use) {
2940 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2941 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2942 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2943 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2944 intel_crtc->base.base.id, intel_crtc->pipe, i);
2945 }
2946 }
2947}
2948
Chandra Konduru6156a452015-04-27 13:48:39 -07002949u32 skl_plane_ctl_format(uint32_t pixel_format)
2950{
Chandra Konduru6156a452015-04-27 13:48:39 -07002951 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002952 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002953 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002957 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002958 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002959 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 /*
2961 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2962 * to be already pre-multiplied. We need to add a knob (or a different
2963 * DRM_FORMAT) for user-space to configure that.
2964 */
2965 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002968 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002969 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002970 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002976 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002978 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002979 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002980 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002981 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002984 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002986
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002987 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002988}
2989
2990u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2991{
Chandra Konduru6156a452015-04-27 13:48:39 -07002992 switch (fb_modifier) {
2993 case DRM_FORMAT_MOD_NONE:
2994 break;
2995 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002996 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002997 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002998 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002999 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003000 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07003001 default:
3002 MISSING_CASE(fb_modifier);
3003 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01003004
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003005 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003006}
3007
3008u32 skl_plane_ctl_rotation(unsigned int rotation)
3009{
Chandra Konduru6156a452015-04-27 13:48:39 -07003010 switch (rotation) {
3011 case BIT(DRM_ROTATE_0):
3012 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303013 /*
3014 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3015 * while i915 HW rotation is clockwise, thats why this swapping.
3016 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003017 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303018 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003019 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003020 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003021 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303022 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003023 default:
3024 MISSING_CASE(rotation);
3025 }
3026
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003027 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003028}
3029
Damien Lespiau70d21f02013-07-03 21:06:04 +01003030static void skylake_update_primary_plane(struct drm_crtc *crtc,
3031 struct drm_framebuffer *fb,
3032 int x, int y)
3033{
3034 struct drm_device *dev = crtc->dev;
3035 struct drm_i915_private *dev_priv = dev->dev_private;
3036 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003037 struct drm_plane *plane = crtc->primary;
3038 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003039 struct drm_i915_gem_object *obj;
3040 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303041 u32 plane_ctl, stride_div, stride;
3042 u32 tile_height, plane_offset, plane_size;
3043 unsigned int rotation;
3044 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003045 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003046 struct intel_crtc_state *crtc_state = intel_crtc->config;
3047 struct intel_plane_state *plane_state;
3048 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3049 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3050 int scaler_id = -1;
3051
Chandra Konduru6156a452015-04-27 13:48:39 -07003052 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003053
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003054 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003055 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3056 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3057 POSTING_READ(PLANE_CTL(pipe, 0));
3058 return;
3059 }
3060
3061 plane_ctl = PLANE_CTL_ENABLE |
3062 PLANE_CTL_PIPE_GAMMA_ENABLE |
3063 PLANE_CTL_PIPE_CSC_ENABLE;
3064
Chandra Konduru6156a452015-04-27 13:48:39 -07003065 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3066 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003067 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303068
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303069 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003070 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003071
Damien Lespiaub3218032015-02-27 11:15:18 +00003072 obj = intel_fb_obj(fb);
3073 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3074 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303075 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3076
Chandra Konduru6156a452015-04-27 13:48:39 -07003077 /*
3078 * FIXME: intel_plane_state->src, dst aren't set when transitional
3079 * update_plane helpers are called from legacy paths.
3080 * Once full atomic crtc is available, below check can be avoided.
3081 */
3082 if (drm_rect_width(&plane_state->src)) {
3083 scaler_id = plane_state->scaler_id;
3084 src_x = plane_state->src.x1 >> 16;
3085 src_y = plane_state->src.y1 >> 16;
3086 src_w = drm_rect_width(&plane_state->src) >> 16;
3087 src_h = drm_rect_height(&plane_state->src) >> 16;
3088 dst_x = plane_state->dst.x1;
3089 dst_y = plane_state->dst.y1;
3090 dst_w = drm_rect_width(&plane_state->dst);
3091 dst_h = drm_rect_height(&plane_state->dst);
3092
3093 WARN_ON(x != src_x || y != src_y);
3094 } else {
3095 src_w = intel_crtc->config->pipe_src_w;
3096 src_h = intel_crtc->config->pipe_src_h;
3097 }
3098
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303099 if (intel_rotation_90_or_270(rotation)) {
3100 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003101 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303102 fb->modifier[0]);
3103 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003104 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303105 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003106 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303107 } else {
3108 stride = fb->pitches[0] / stride_div;
3109 x_offset = x;
3110 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003111 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303112 }
3113 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003114
Damien Lespiau70d21f02013-07-03 21:06:04 +01003115 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303116 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3117 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3118 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003119
3120 if (scaler_id >= 0) {
3121 uint32_t ps_ctrl = 0;
3122
3123 WARN_ON(!dst_w || !dst_h);
3124 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3125 crtc_state->scaler_state.scalers[scaler_id].mode;
3126 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3127 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3128 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3129 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3130 I915_WRITE(PLANE_POS(pipe, 0), 0);
3131 } else {
3132 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3133 }
3134
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003135 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003136
3137 POSTING_READ(PLANE_SURF(pipe, 0));
3138}
3139
Jesse Barnes17638cd2011-06-24 12:19:23 -07003140/* Assume fb object is pinned & idle & fenced and just update base pointers */
3141static int
3142intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3143 int x, int y, enum mode_set_atomic state)
3144{
3145 struct drm_device *dev = crtc->dev;
3146 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003147
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003148 if (dev_priv->display.disable_fbc)
3149 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003150
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003151 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3152
3153 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003154}
3155
Ville Syrjälä75147472014-11-24 18:28:11 +02003156static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003157{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003158 struct drm_crtc *crtc;
3159
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003160 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3162 enum plane plane = intel_crtc->plane;
3163
3164 intel_prepare_page_flip(dev, plane);
3165 intel_finish_page_flip_plane(dev, plane);
3166 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003167}
3168
3169static void intel_update_primary_planes(struct drm_device *dev)
3170{
3171 struct drm_i915_private *dev_priv = dev->dev_private;
3172 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003173
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003174 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3176
Rob Clark51fd3712013-11-19 12:10:12 -05003177 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003178 /*
3179 * FIXME: Once we have proper support for primary planes (and
3180 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003181 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003182 */
Matt Roperf4510a22014-04-01 15:22:40 -07003183 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003184 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003185 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003186 crtc->x,
3187 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003188 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003189 }
3190}
3191
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003192void intel_crtc_reset(struct intel_crtc *crtc)
3193{
3194 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3195
3196 if (!crtc->active)
3197 return;
3198
3199 intel_crtc_disable_planes(&crtc->base);
3200 dev_priv->display.crtc_disable(&crtc->base);
3201 dev_priv->display.crtc_enable(&crtc->base);
3202 intel_crtc_enable_planes(&crtc->base);
3203}
3204
Ville Syrjälä75147472014-11-24 18:28:11 +02003205void intel_prepare_reset(struct drm_device *dev)
3206{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003207 struct drm_i915_private *dev_priv = to_i915(dev);
3208 struct intel_crtc *crtc;
3209
Ville Syrjälä75147472014-11-24 18:28:11 +02003210 /* no reset support for gen2 */
3211 if (IS_GEN2(dev))
3212 return;
3213
3214 /* reset doesn't touch the display */
3215 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3216 return;
3217
3218 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003219
3220 /*
3221 * Disabling the crtcs gracefully seems nicer. Also the
3222 * g33 docs say we should at least disable all the planes.
3223 */
3224 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003225 if (!crtc->active)
3226 continue;
3227
3228 intel_crtc_disable_planes(&crtc->base);
3229 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003230 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003231}
3232
3233void intel_finish_reset(struct drm_device *dev)
3234{
3235 struct drm_i915_private *dev_priv = to_i915(dev);
3236
3237 /*
3238 * Flips in the rings will be nuked by the reset,
3239 * so complete all pending flips so that user space
3240 * will get its events and not get stuck.
3241 */
3242 intel_complete_page_flips(dev);
3243
3244 /* no reset support for gen2 */
3245 if (IS_GEN2(dev))
3246 return;
3247
3248 /* reset doesn't touch the display */
3249 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3250 /*
3251 * Flips in the rings have been nuked by the reset,
3252 * so update the base address of all primary
3253 * planes to the the last fb to make sure we're
3254 * showing the correct fb after a reset.
3255 */
3256 intel_update_primary_planes(dev);
3257 return;
3258 }
3259
3260 /*
3261 * The display has been reset as well,
3262 * so need a full re-initialization.
3263 */
3264 intel_runtime_pm_disable_interrupts(dev_priv);
3265 intel_runtime_pm_enable_interrupts(dev_priv);
3266
3267 intel_modeset_init_hw(dev);
3268
3269 spin_lock_irq(&dev_priv->irq_lock);
3270 if (dev_priv->display.hpd_irq_setup)
3271 dev_priv->display.hpd_irq_setup(dev);
3272 spin_unlock_irq(&dev_priv->irq_lock);
3273
3274 intel_modeset_setup_hw_state(dev, true);
3275
3276 intel_hpd_init(dev_priv);
3277
3278 drm_modeset_unlock_all(dev);
3279}
3280
Chris Wilson2e2f3512015-04-27 13:41:14 +01003281static void
Chris Wilson14667a42012-04-03 17:58:35 +01003282intel_finish_fb(struct drm_framebuffer *old_fb)
3283{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003284 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003285 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003286 bool was_interruptible = dev_priv->mm.interruptible;
3287 int ret;
3288
Chris Wilson14667a42012-04-03 17:58:35 +01003289 /* Big Hammer, we also need to ensure that any pending
3290 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3291 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003292 * framebuffer. Note that we rely on userspace rendering
3293 * into the buffer attached to the pipe they are waiting
3294 * on. If not, userspace generates a GPU hang with IPEHR
3295 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003296 *
3297 * This should only fail upon a hung GPU, in which case we
3298 * can safely continue.
3299 */
3300 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003301 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003302 dev_priv->mm.interruptible = was_interruptible;
3303
Chris Wilson2e2f3512015-04-27 13:41:14 +01003304 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003305}
3306
Chris Wilson7d5e3792014-03-04 13:15:08 +00003307static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3308{
3309 struct drm_device *dev = crtc->dev;
3310 struct drm_i915_private *dev_priv = dev->dev_private;
3311 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003312 bool pending;
3313
3314 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3315 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3316 return false;
3317
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003318 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003319 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003320 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003321
3322 return pending;
3323}
3324
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003325static void intel_update_pipe_size(struct intel_crtc *crtc)
3326{
3327 struct drm_device *dev = crtc->base.dev;
3328 struct drm_i915_private *dev_priv = dev->dev_private;
3329 const struct drm_display_mode *adjusted_mode;
3330
3331 if (!i915.fastboot)
3332 return;
3333
3334 /*
3335 * Update pipe size and adjust fitter if needed: the reason for this is
3336 * that in compute_mode_changes we check the native mode (not the pfit
3337 * mode) to see if we can flip rather than do a full mode set. In the
3338 * fastboot case, we'll flip, but if we don't update the pipesrc and
3339 * pfit state, we'll end up with a big fb scanned out into the wrong
3340 * sized surface.
3341 *
3342 * To fix this properly, we need to hoist the checks up into
3343 * compute_mode_changes (or above), check the actual pfit state and
3344 * whether the platform allows pfit disable with pipe active, and only
3345 * then update the pipesrc and pfit state, even on the flip path.
3346 */
3347
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003348 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003349
3350 I915_WRITE(PIPESRC(crtc->pipe),
3351 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3352 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003353 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003354 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3355 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003356 I915_WRITE(PF_CTL(crtc->pipe), 0);
3357 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3358 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3359 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003360 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3361 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003362}
3363
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003364static void intel_fdi_normal_train(struct drm_crtc *crtc)
3365{
3366 struct drm_device *dev = crtc->dev;
3367 struct drm_i915_private *dev_priv = dev->dev_private;
3368 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3369 int pipe = intel_crtc->pipe;
3370 u32 reg, temp;
3371
3372 /* enable normal train */
3373 reg = FDI_TX_CTL(pipe);
3374 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003375 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003376 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3377 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003378 } else {
3379 temp &= ~FDI_LINK_TRAIN_NONE;
3380 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003381 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003382 I915_WRITE(reg, temp);
3383
3384 reg = FDI_RX_CTL(pipe);
3385 temp = I915_READ(reg);
3386 if (HAS_PCH_CPT(dev)) {
3387 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3388 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3389 } else {
3390 temp &= ~FDI_LINK_TRAIN_NONE;
3391 temp |= FDI_LINK_TRAIN_NONE;
3392 }
3393 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3394
3395 /* wait one idle pattern time */
3396 POSTING_READ(reg);
3397 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003398
3399 /* IVB wants error correction enabled */
3400 if (IS_IVYBRIDGE(dev))
3401 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3402 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003403}
3404
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003405/* The FDI link training functions for ILK/Ibexpeak. */
3406static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3407{
3408 struct drm_device *dev = crtc->dev;
3409 struct drm_i915_private *dev_priv = dev->dev_private;
3410 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3411 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003412 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003414 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003415 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003416
Adam Jacksone1a44742010-06-25 15:32:14 -04003417 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3418 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_RX_IMR(pipe);
3420 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003421 temp &= ~FDI_RX_SYMBOL_LOCK;
3422 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003423 I915_WRITE(reg, temp);
3424 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003425 udelay(150);
3426
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003428 reg = FDI_TX_CTL(pipe);
3429 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003430 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003431 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432 temp &= ~FDI_LINK_TRAIN_NONE;
3433 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 reg = FDI_RX_CTL(pipe);
3437 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003438 temp &= ~FDI_LINK_TRAIN_NONE;
3439 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3441
3442 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003443 udelay(150);
3444
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003445 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003446 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3447 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3448 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003449
Chris Wilson5eddb702010-09-11 13:48:45 +01003450 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003451 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3454
3455 if ((temp & FDI_RX_BIT_LOCK)) {
3456 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003458 break;
3459 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003461 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003462 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003463
3464 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003465 reg = FDI_TX_CTL(pipe);
3466 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 temp &= ~FDI_LINK_TRAIN_NONE;
3468 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470
Chris Wilson5eddb702010-09-11 13:48:45 +01003471 reg = FDI_RX_CTL(pipe);
3472 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 temp &= ~FDI_LINK_TRAIN_NONE;
3474 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003475 I915_WRITE(reg, temp);
3476
3477 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003478 udelay(150);
3479
Chris Wilson5eddb702010-09-11 13:48:45 +01003480 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003481 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003482 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003483 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3484
3485 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003486 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003487 DRM_DEBUG_KMS("FDI train 2 done.\n");
3488 break;
3489 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003491 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003492 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003493
3494 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003495
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003496}
3497
Akshay Joshi0206e352011-08-16 15:34:10 -04003498static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3500 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3501 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3502 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3503};
3504
3505/* The FDI link training functions for SNB/Cougarpoint. */
3506static void gen6_fdi_link_train(struct drm_crtc *crtc)
3507{
3508 struct drm_device *dev = crtc->dev;
3509 struct drm_i915_private *dev_priv = dev->dev_private;
3510 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3511 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003512 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513
Adam Jacksone1a44742010-06-25 15:32:14 -04003514 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3515 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003516 reg = FDI_RX_IMR(pipe);
3517 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003518 temp &= ~FDI_RX_SYMBOL_LOCK;
3519 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003520 I915_WRITE(reg, temp);
3521
3522 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003523 udelay(150);
3524
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003525 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 reg = FDI_TX_CTL(pipe);
3527 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003528 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003529 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003530 temp &= ~FDI_LINK_TRAIN_NONE;
3531 temp |= FDI_LINK_TRAIN_PATTERN_1;
3532 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3533 /* SNB-B */
3534 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003536
Daniel Vetterd74cf322012-10-26 10:58:13 +02003537 I915_WRITE(FDI_RX_MISC(pipe),
3538 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3539
Chris Wilson5eddb702010-09-11 13:48:45 +01003540 reg = FDI_RX_CTL(pipe);
3541 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003542 if (HAS_PCH_CPT(dev)) {
3543 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3544 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3545 } else {
3546 temp &= ~FDI_LINK_TRAIN_NONE;
3547 temp |= FDI_LINK_TRAIN_PATTERN_1;
3548 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003549 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3550
3551 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003552 udelay(150);
3553
Akshay Joshi0206e352011-08-16 15:34:10 -04003554 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003555 reg = FDI_TX_CTL(pipe);
3556 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003557 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3558 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003559 I915_WRITE(reg, temp);
3560
3561 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003562 udelay(500);
3563
Sean Paulfa37d392012-03-02 12:53:39 -05003564 for (retry = 0; retry < 5; retry++) {
3565 reg = FDI_RX_IIR(pipe);
3566 temp = I915_READ(reg);
3567 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3568 if (temp & FDI_RX_BIT_LOCK) {
3569 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3570 DRM_DEBUG_KMS("FDI train 1 done.\n");
3571 break;
3572 }
3573 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 }
Sean Paulfa37d392012-03-02 12:53:39 -05003575 if (retry < 5)
3576 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003577 }
3578 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003579 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003580
3581 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003582 reg = FDI_TX_CTL(pipe);
3583 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 temp &= ~FDI_LINK_TRAIN_NONE;
3585 temp |= FDI_LINK_TRAIN_PATTERN_2;
3586 if (IS_GEN6(dev)) {
3587 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3588 /* SNB-B */
3589 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3590 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003591 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592
Chris Wilson5eddb702010-09-11 13:48:45 +01003593 reg = FDI_RX_CTL(pipe);
3594 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003595 if (HAS_PCH_CPT(dev)) {
3596 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3597 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3598 } else {
3599 temp &= ~FDI_LINK_TRAIN_NONE;
3600 temp |= FDI_LINK_TRAIN_PATTERN_2;
3601 }
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(150);
3606
Akshay Joshi0206e352011-08-16 15:34:10 -04003607 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003608 reg = FDI_TX_CTL(pipe);
3609 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003610 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3611 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003612 I915_WRITE(reg, temp);
3613
3614 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003615 udelay(500);
3616
Sean Paulfa37d392012-03-02 12:53:39 -05003617 for (retry = 0; retry < 5; retry++) {
3618 reg = FDI_RX_IIR(pipe);
3619 temp = I915_READ(reg);
3620 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3621 if (temp & FDI_RX_SYMBOL_LOCK) {
3622 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3623 DRM_DEBUG_KMS("FDI train 2 done.\n");
3624 break;
3625 }
3626 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003627 }
Sean Paulfa37d392012-03-02 12:53:39 -05003628 if (retry < 5)
3629 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003630 }
3631 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003632 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003633
3634 DRM_DEBUG_KMS("FDI train done.\n");
3635}
3636
Jesse Barnes357555c2011-04-28 15:09:55 -07003637/* Manual link training for Ivy Bridge A0 parts */
3638static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3639{
3640 struct drm_device *dev = crtc->dev;
3641 struct drm_i915_private *dev_priv = dev->dev_private;
3642 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3643 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003644 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003645
3646 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3647 for train result */
3648 reg = FDI_RX_IMR(pipe);
3649 temp = I915_READ(reg);
3650 temp &= ~FDI_RX_SYMBOL_LOCK;
3651 temp &= ~FDI_RX_BIT_LOCK;
3652 I915_WRITE(reg, temp);
3653
3654 POSTING_READ(reg);
3655 udelay(150);
3656
Daniel Vetter01a415f2012-10-27 15:58:40 +02003657 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3658 I915_READ(FDI_RX_IIR(pipe)));
3659
Jesse Barnes139ccd32013-08-19 11:04:55 -07003660 /* Try each vswing and preemphasis setting twice before moving on */
3661 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3662 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003663 reg = FDI_TX_CTL(pipe);
3664 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003665 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3666 temp &= ~FDI_TX_ENABLE;
3667 I915_WRITE(reg, temp);
3668
3669 reg = FDI_RX_CTL(pipe);
3670 temp = I915_READ(reg);
3671 temp &= ~FDI_LINK_TRAIN_AUTO;
3672 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3673 temp &= ~FDI_RX_ENABLE;
3674 I915_WRITE(reg, temp);
3675
3676 /* enable CPU FDI TX and PCH FDI RX */
3677 reg = FDI_TX_CTL(pipe);
3678 temp = I915_READ(reg);
3679 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003680 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003681 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003682 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003683 temp |= snb_b_fdi_train_param[j/2];
3684 temp |= FDI_COMPOSITE_SYNC;
3685 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3686
3687 I915_WRITE(FDI_RX_MISC(pipe),
3688 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3689
3690 reg = FDI_RX_CTL(pipe);
3691 temp = I915_READ(reg);
3692 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3693 temp |= FDI_COMPOSITE_SYNC;
3694 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3695
3696 POSTING_READ(reg);
3697 udelay(1); /* should be 0.5us */
3698
3699 for (i = 0; i < 4; i++) {
3700 reg = FDI_RX_IIR(pipe);
3701 temp = I915_READ(reg);
3702 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3703
3704 if (temp & FDI_RX_BIT_LOCK ||
3705 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3706 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3707 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3708 i);
3709 break;
3710 }
3711 udelay(1); /* should be 0.5us */
3712 }
3713 if (i == 4) {
3714 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3715 continue;
3716 }
3717
3718 /* Train 2 */
3719 reg = FDI_TX_CTL(pipe);
3720 temp = I915_READ(reg);
3721 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3722 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3723 I915_WRITE(reg, temp);
3724
3725 reg = FDI_RX_CTL(pipe);
3726 temp = I915_READ(reg);
3727 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3728 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003729 I915_WRITE(reg, temp);
3730
3731 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003732 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003733
Jesse Barnes139ccd32013-08-19 11:04:55 -07003734 for (i = 0; i < 4; i++) {
3735 reg = FDI_RX_IIR(pipe);
3736 temp = I915_READ(reg);
3737 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003738
Jesse Barnes139ccd32013-08-19 11:04:55 -07003739 if (temp & FDI_RX_SYMBOL_LOCK ||
3740 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3741 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3742 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3743 i);
3744 goto train_done;
3745 }
3746 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003747 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003748 if (i == 4)
3749 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003750 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003751
Jesse Barnes139ccd32013-08-19 11:04:55 -07003752train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003753 DRM_DEBUG_KMS("FDI train done.\n");
3754}
3755
Daniel Vetter88cefb62012-08-12 19:27:14 +02003756static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003757{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003758 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003759 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003760 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003761 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003762
Jesse Barnesc64e3112010-09-10 11:27:03 -07003763
Jesse Barnes0e23b992010-09-10 11:10:00 -07003764 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003765 reg = FDI_RX_CTL(pipe);
3766 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003767 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003768 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003769 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003770 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3771
3772 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003773 udelay(200);
3774
3775 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003776 temp = I915_READ(reg);
3777 I915_WRITE(reg, temp | FDI_PCDCLK);
3778
3779 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003780 udelay(200);
3781
Paulo Zanoni20749732012-11-23 15:30:38 -02003782 /* Enable CPU FDI TX PLL, always on for Ironlake */
3783 reg = FDI_TX_CTL(pipe);
3784 temp = I915_READ(reg);
3785 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3786 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003787
Paulo Zanoni20749732012-11-23 15:30:38 -02003788 POSTING_READ(reg);
3789 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003790 }
3791}
3792
Daniel Vetter88cefb62012-08-12 19:27:14 +02003793static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3794{
3795 struct drm_device *dev = intel_crtc->base.dev;
3796 struct drm_i915_private *dev_priv = dev->dev_private;
3797 int pipe = intel_crtc->pipe;
3798 u32 reg, temp;
3799
3800 /* Switch from PCDclk to Rawclk */
3801 reg = FDI_RX_CTL(pipe);
3802 temp = I915_READ(reg);
3803 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3804
3805 /* Disable CPU FDI TX PLL */
3806 reg = FDI_TX_CTL(pipe);
3807 temp = I915_READ(reg);
3808 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3809
3810 POSTING_READ(reg);
3811 udelay(100);
3812
3813 reg = FDI_RX_CTL(pipe);
3814 temp = I915_READ(reg);
3815 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3816
3817 /* Wait for the clocks to turn off. */
3818 POSTING_READ(reg);
3819 udelay(100);
3820}
3821
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003822static void ironlake_fdi_disable(struct drm_crtc *crtc)
3823{
3824 struct drm_device *dev = crtc->dev;
3825 struct drm_i915_private *dev_priv = dev->dev_private;
3826 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3827 int pipe = intel_crtc->pipe;
3828 u32 reg, temp;
3829
3830 /* disable CPU FDI tx and PCH FDI rx */
3831 reg = FDI_TX_CTL(pipe);
3832 temp = I915_READ(reg);
3833 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3834 POSTING_READ(reg);
3835
3836 reg = FDI_RX_CTL(pipe);
3837 temp = I915_READ(reg);
3838 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003839 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003840 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3841
3842 POSTING_READ(reg);
3843 udelay(100);
3844
3845 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003846 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003847 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003848
3849 /* still set train pattern 1 */
3850 reg = FDI_TX_CTL(pipe);
3851 temp = I915_READ(reg);
3852 temp &= ~FDI_LINK_TRAIN_NONE;
3853 temp |= FDI_LINK_TRAIN_PATTERN_1;
3854 I915_WRITE(reg, temp);
3855
3856 reg = FDI_RX_CTL(pipe);
3857 temp = I915_READ(reg);
3858 if (HAS_PCH_CPT(dev)) {
3859 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3860 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3861 } else {
3862 temp &= ~FDI_LINK_TRAIN_NONE;
3863 temp |= FDI_LINK_TRAIN_PATTERN_1;
3864 }
3865 /* BPC in FDI rx is consistent with that in PIPECONF */
3866 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003867 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003868 I915_WRITE(reg, temp);
3869
3870 POSTING_READ(reg);
3871 udelay(100);
3872}
3873
Chris Wilson5dce5b932014-01-20 10:17:36 +00003874bool intel_has_pending_fb_unpin(struct drm_device *dev)
3875{
3876 struct intel_crtc *crtc;
3877
3878 /* Note that we don't need to be called with mode_config.lock here
3879 * as our list of CRTC objects is static for the lifetime of the
3880 * device and so cannot disappear as we iterate. Similarly, we can
3881 * happily treat the predicates as racy, atomic checks as userspace
3882 * cannot claim and pin a new fb without at least acquring the
3883 * struct_mutex and so serialising with us.
3884 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003885 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003886 if (atomic_read(&crtc->unpin_work_count) == 0)
3887 continue;
3888
3889 if (crtc->unpin_work)
3890 intel_wait_for_vblank(dev, crtc->pipe);
3891
3892 return true;
3893 }
3894
3895 return false;
3896}
3897
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003898static void page_flip_completed(struct intel_crtc *intel_crtc)
3899{
3900 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3901 struct intel_unpin_work *work = intel_crtc->unpin_work;
3902
3903 /* ensure that the unpin work is consistent wrt ->pending. */
3904 smp_rmb();
3905 intel_crtc->unpin_work = NULL;
3906
3907 if (work->event)
3908 drm_send_vblank_event(intel_crtc->base.dev,
3909 intel_crtc->pipe,
3910 work->event);
3911
3912 drm_crtc_vblank_put(&intel_crtc->base);
3913
3914 wake_up_all(&dev_priv->pending_flip_queue);
3915 queue_work(dev_priv->wq, &work->work);
3916
3917 trace_i915_flip_complete(intel_crtc->plane,
3918 work->pending_flip_obj);
3919}
3920
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003921void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003922{
Chris Wilson0f911282012-04-17 10:05:38 +01003923 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003924 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003925
Daniel Vetter2c10d572012-12-20 21:24:07 +01003926 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003927 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3928 !intel_crtc_has_pending_flip(crtc),
3929 60*HZ) == 0)) {
3930 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003931
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003932 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003933 if (intel_crtc->unpin_work) {
3934 WARN_ONCE(1, "Removing stuck page flip\n");
3935 page_flip_completed(intel_crtc);
3936 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003937 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003938 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003939
Chris Wilson975d5682014-08-20 13:13:34 +01003940 if (crtc->primary->fb) {
3941 mutex_lock(&dev->struct_mutex);
3942 intel_finish_fb(crtc->primary->fb);
3943 mutex_unlock(&dev->struct_mutex);
3944 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003945}
3946
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947/* Program iCLKIP clock to the desired frequency */
3948static void lpt_program_iclkip(struct drm_crtc *crtc)
3949{
3950 struct drm_device *dev = crtc->dev;
3951 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003952 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003953 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3954 u32 temp;
3955
Daniel Vetter09153002012-12-12 14:06:44 +01003956 mutex_lock(&dev_priv->dpio_lock);
3957
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003958 /* It is necessary to ungate the pixclk gate prior to programming
3959 * the divisors, and gate it back when it is done.
3960 */
3961 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3962
3963 /* Disable SSCCTL */
3964 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003965 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3966 SBI_SSCCTL_DISABLE,
3967 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003968
3969 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003970 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003971 auxdiv = 1;
3972 divsel = 0x41;
3973 phaseinc = 0x20;
3974 } else {
3975 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003976 * but the adjusted_mode->crtc_clock in in KHz. To get the
3977 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003978 * convert the virtual clock precision to KHz here for higher
3979 * precision.
3980 */
3981 u32 iclk_virtual_root_freq = 172800 * 1000;
3982 u32 iclk_pi_range = 64;
3983 u32 desired_divisor, msb_divisor_value, pi_value;
3984
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003985 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003986 msb_divisor_value = desired_divisor / iclk_pi_range;
3987 pi_value = desired_divisor % iclk_pi_range;
3988
3989 auxdiv = 0;
3990 divsel = msb_divisor_value - 2;
3991 phaseinc = pi_value;
3992 }
3993
3994 /* This should not happen with any sane values */
3995 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3996 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3997 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3998 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3999
4000 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 +03004001 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004002 auxdiv,
4003 divsel,
4004 phasedir,
4005 phaseinc);
4006
4007 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004008 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004009 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
4010 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
4011 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
4012 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
4013 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4014 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004015 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004016
4017 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004018 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004019 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4020 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004021 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004022
4023 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004024 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004025 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004026 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004027
4028 /* Wait for initialization time */
4029 udelay(24);
4030
4031 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004032
4033 mutex_unlock(&dev_priv->dpio_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004034}
4035
Daniel Vetter275f01b22013-05-03 11:49:47 +02004036static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4037 enum pipe pch_transcoder)
4038{
4039 struct drm_device *dev = crtc->base.dev;
4040 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004041 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004042
4043 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4044 I915_READ(HTOTAL(cpu_transcoder)));
4045 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4046 I915_READ(HBLANK(cpu_transcoder)));
4047 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4048 I915_READ(HSYNC(cpu_transcoder)));
4049
4050 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4051 I915_READ(VTOTAL(cpu_transcoder)));
4052 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4053 I915_READ(VBLANK(cpu_transcoder)));
4054 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4055 I915_READ(VSYNC(cpu_transcoder)));
4056 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4057 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4058}
4059
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004060static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004061{
4062 struct drm_i915_private *dev_priv = dev->dev_private;
4063 uint32_t temp;
4064
4065 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004066 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004067 return;
4068
4069 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4070 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4071
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004072 temp &= ~FDI_BC_BIFURCATION_SELECT;
4073 if (enable)
4074 temp |= FDI_BC_BIFURCATION_SELECT;
4075
4076 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004077 I915_WRITE(SOUTH_CHICKEN1, temp);
4078 POSTING_READ(SOUTH_CHICKEN1);
4079}
4080
4081static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4082{
4083 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004084
4085 switch (intel_crtc->pipe) {
4086 case PIPE_A:
4087 break;
4088 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004089 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004090 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004091 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004092 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004093
4094 break;
4095 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004096 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004097
4098 break;
4099 default:
4100 BUG();
4101 }
4102}
4103
Jesse Barnesf67a5592011-01-05 10:31:48 -08004104/*
4105 * Enable PCH resources required for PCH ports:
4106 * - PCH PLLs
4107 * - FDI training & RX/TX
4108 * - update transcoder timings
4109 * - DP transcoding bits
4110 * - transcoder
4111 */
4112static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004113{
4114 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004115 struct drm_i915_private *dev_priv = dev->dev_private;
4116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4117 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004118 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004119
Daniel Vetterab9412b2013-05-03 11:49:46 +02004120 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004121
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004122 if (IS_IVYBRIDGE(dev))
4123 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4124
Daniel Vettercd986ab2012-10-26 10:58:12 +02004125 /* Write the TU size bits before fdi link training, so that error
4126 * detection works. */
4127 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4128 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4129
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004130 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004131 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004132
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004133 /* We need to program the right clock selection before writing the pixel
4134 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004135 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004136 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004137
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004138 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004139 temp |= TRANS_DPLL_ENABLE(pipe);
4140 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004141 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004142 temp |= sel;
4143 else
4144 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004145 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004148 /* XXX: pch pll's can be enabled any time before we enable the PCH
4149 * transcoder, and we actually should do this to not upset any PCH
4150 * transcoder that already use the clock when we share it.
4151 *
4152 * Note that enable_shared_dpll tries to do the right thing, but
4153 * get_shared_dpll unconditionally resets the pll - we need that to have
4154 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004155 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004156
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004157 /* set transcoder timing, panel must allow it */
4158 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004159 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004161 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004162
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004164 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004165 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004166 reg = TRANS_DP_CTL(pipe);
4167 temp = I915_READ(reg);
4168 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004169 TRANS_DP_SYNC_MASK |
4170 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004171 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004172 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004173
4174 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004175 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004176 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004177 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004178
4179 switch (intel_trans_dp_port_sel(crtc)) {
4180 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004181 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004182 break;
4183 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004184 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004185 break;
4186 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004187 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004188 break;
4189 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004190 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004191 }
4192
Chris Wilson5eddb702010-09-11 13:48:45 +01004193 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004194 }
4195
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004196 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004197}
4198
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004199static void lpt_pch_enable(struct drm_crtc *crtc)
4200{
4201 struct drm_device *dev = crtc->dev;
4202 struct drm_i915_private *dev_priv = dev->dev_private;
4203 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004204 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004205
Daniel Vetterab9412b2013-05-03 11:49:46 +02004206 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004207
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004208 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004209
Paulo Zanoni0540e482012-10-31 18:12:40 -02004210 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004211 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004212
Paulo Zanoni937bb612012-10-31 18:12:47 -02004213 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004214}
4215
Daniel Vetter716c2e52014-06-25 22:02:02 +03004216void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004217{
Daniel Vettere2b78262013-06-07 23:10:03 +02004218 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004219
4220 if (pll == NULL)
4221 return;
4222
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004223 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004224 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004225 return;
4226 }
4227
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004228 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4229 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004230 WARN_ON(pll->on);
4231 WARN_ON(pll->active);
4232 }
4233
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004234 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004235}
4236
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004237struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4238 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004239{
Daniel Vettere2b78262013-06-07 23:10:03 +02004240 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004241 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004242 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004243
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004244 if (HAS_PCH_IBX(dev_priv->dev)) {
4245 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004246 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004247 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004248
Daniel Vetter46edb022013-06-05 13:34:12 +02004249 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4250 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004251
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004252 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004253
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004254 goto found;
4255 }
4256
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304257 if (IS_BROXTON(dev_priv->dev)) {
4258 /* PLL is attached to port in bxt */
4259 struct intel_encoder *encoder;
4260 struct intel_digital_port *intel_dig_port;
4261
4262 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4263 if (WARN_ON(!encoder))
4264 return NULL;
4265
4266 intel_dig_port = enc_to_dig_port(&encoder->base);
4267 /* 1:1 mapping between ports and PLLs */
4268 i = (enum intel_dpll_id)intel_dig_port->port;
4269 pll = &dev_priv->shared_dplls[i];
4270 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4271 crtc->base.base.id, pll->name);
4272 WARN_ON(pll->new_config->crtc_mask);
4273
4274 goto found;
4275 }
4276
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004277 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4278 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004279
4280 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004281 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004282 continue;
4283
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004284 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004285 &pll->new_config->hw_state,
4286 sizeof(pll->new_config->hw_state)) == 0) {
4287 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004288 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004289 pll->new_config->crtc_mask,
4290 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004291 goto found;
4292 }
4293 }
4294
4295 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004296 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4297 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004298 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004299 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4300 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004301 goto found;
4302 }
4303 }
4304
4305 return NULL;
4306
4307found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004308 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004309 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004310
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004311 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004312 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4313 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004314
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004315 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004316
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004317 return pll;
4318}
4319
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004320/**
4321 * intel_shared_dpll_start_config - start a new PLL staged config
4322 * @dev_priv: DRM device
4323 * @clear_pipes: mask of pipes that will have their PLLs freed
4324 *
4325 * Starts a new PLL staged config, copying the current config but
4326 * releasing the references of pipes specified in clear_pipes.
4327 */
4328static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4329 unsigned clear_pipes)
4330{
4331 struct intel_shared_dpll *pll;
4332 enum intel_dpll_id i;
4333
4334 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4335 pll = &dev_priv->shared_dplls[i];
4336
4337 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4338 GFP_KERNEL);
4339 if (!pll->new_config)
4340 goto cleanup;
4341
4342 pll->new_config->crtc_mask &= ~clear_pipes;
4343 }
4344
4345 return 0;
4346
4347cleanup:
4348 while (--i >= 0) {
4349 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004350 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004351 pll->new_config = NULL;
4352 }
4353
4354 return -ENOMEM;
4355}
4356
4357static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4358{
4359 struct intel_shared_dpll *pll;
4360 enum intel_dpll_id i;
4361
4362 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4363 pll = &dev_priv->shared_dplls[i];
4364
4365 WARN_ON(pll->new_config == &pll->config);
4366
4367 pll->config = *pll->new_config;
4368 kfree(pll->new_config);
4369 pll->new_config = NULL;
4370 }
4371}
4372
4373static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4374{
4375 struct intel_shared_dpll *pll;
4376 enum intel_dpll_id i;
4377
4378 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4379 pll = &dev_priv->shared_dplls[i];
4380
4381 WARN_ON(pll->new_config == &pll->config);
4382
4383 kfree(pll->new_config);
4384 pll->new_config = NULL;
4385 }
4386}
4387
Daniel Vettera1520312013-05-03 11:49:50 +02004388static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004389{
4390 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004391 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004392 u32 temp;
4393
4394 temp = I915_READ(dslreg);
4395 udelay(500);
4396 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004397 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004398 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004399 }
4400}
4401
Chandra Kondurua1b22782015-04-07 15:28:45 -07004402/**
4403 * skl_update_scaler_users - Stages update to crtc's scaler state
4404 * @intel_crtc: crtc
4405 * @crtc_state: crtc_state
4406 * @plane: plane (NULL indicates crtc is requesting update)
4407 * @plane_state: plane's state
4408 * @force_detach: request unconditional detachment of scaler
4409 *
4410 * This function updates scaler state for requested plane or crtc.
4411 * To request scaler usage update for a plane, caller shall pass plane pointer.
4412 * To request scaler usage update for crtc, caller shall pass plane pointer
4413 * as NULL.
4414 *
4415 * Return
4416 * 0 - scaler_usage updated successfully
4417 * error - requested scaling cannot be supported or other error condition
4418 */
4419int
4420skl_update_scaler_users(
4421 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4422 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4423 int force_detach)
4424{
4425 int need_scaling;
4426 int idx;
4427 int src_w, src_h, dst_w, dst_h;
4428 int *scaler_id;
4429 struct drm_framebuffer *fb;
4430 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004431 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004432
4433 if (!intel_crtc || !crtc_state)
4434 return 0;
4435
4436 scaler_state = &crtc_state->scaler_state;
4437
4438 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4439 fb = intel_plane ? plane_state->base.fb : NULL;
4440
4441 if (intel_plane) {
4442 src_w = drm_rect_width(&plane_state->src) >> 16;
4443 src_h = drm_rect_height(&plane_state->src) >> 16;
4444 dst_w = drm_rect_width(&plane_state->dst);
4445 dst_h = drm_rect_height(&plane_state->dst);
4446 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004447 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004448 } else {
4449 struct drm_display_mode *adjusted_mode =
4450 &crtc_state->base.adjusted_mode;
4451 src_w = crtc_state->pipe_src_w;
4452 src_h = crtc_state->pipe_src_h;
4453 dst_w = adjusted_mode->hdisplay;
4454 dst_h = adjusted_mode->vdisplay;
4455 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004456 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004457 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004458
4459 need_scaling = intel_rotation_90_or_270(rotation) ?
4460 (src_h != dst_w || src_w != dst_h):
4461 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004462
4463 /*
4464 * if plane is being disabled or scaler is no more required or force detach
4465 * - free scaler binded to this plane/crtc
4466 * - in order to do this, update crtc->scaler_usage
4467 *
4468 * Here scaler state in crtc_state is set free so that
4469 * scaler can be assigned to other user. Actual register
4470 * update to free the scaler is done in plane/panel-fit programming.
4471 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4472 */
4473 if (force_detach || !need_scaling || (intel_plane &&
4474 (!fb || !plane_state->visible))) {
4475 if (*scaler_id >= 0) {
4476 scaler_state->scaler_users &= ~(1 << idx);
4477 scaler_state->scalers[*scaler_id].in_use = 0;
4478
4479 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4480 "crtc_state = %p scaler_users = 0x%x\n",
4481 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4482 intel_plane ? intel_plane->base.base.id :
4483 intel_crtc->base.base.id, crtc_state,
4484 scaler_state->scaler_users);
4485 *scaler_id = -1;
4486 }
4487 return 0;
4488 }
4489
4490 /* range checks */
4491 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4492 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4493
4494 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4495 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4496 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4497 "size is out of scaler range\n",
4498 intel_plane ? "PLANE" : "CRTC",
4499 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4500 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4501 return -EINVAL;
4502 }
4503
4504 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004505 if (WARN_ON(intel_plane &&
4506 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4507 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4508 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004509 return -EINVAL;
4510 }
4511
4512 /* Check src format */
4513 if (intel_plane) {
4514 switch (fb->pixel_format) {
4515 case DRM_FORMAT_RGB565:
4516 case DRM_FORMAT_XBGR8888:
4517 case DRM_FORMAT_XRGB8888:
4518 case DRM_FORMAT_ABGR8888:
4519 case DRM_FORMAT_ARGB8888:
4520 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004521 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004522 case DRM_FORMAT_YUYV:
4523 case DRM_FORMAT_YVYU:
4524 case DRM_FORMAT_UYVY:
4525 case DRM_FORMAT_VYUY:
4526 break;
4527 default:
4528 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4529 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4530 return -EINVAL;
4531 }
4532 }
4533
4534 /* mark this plane as a scaler user in crtc_state */
4535 scaler_state->scaler_users |= (1 << idx);
4536 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4537 "crtc_state = %p scaler_users = 0x%x\n",
4538 intel_plane ? "PLANE" : "CRTC",
4539 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4540 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4541 return 0;
4542}
4543
4544static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004545{
4546 struct drm_device *dev = crtc->base.dev;
4547 struct drm_i915_private *dev_priv = dev->dev_private;
4548 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004549 struct intel_crtc_scaler_state *scaler_state =
4550 &crtc->config->scaler_state;
4551
4552 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4553
4554 /* To update pfit, first update scaler state */
4555 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4556 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4557 skl_detach_scalers(crtc);
4558 if (!enable)
4559 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004560
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004561 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004562 int id;
4563
4564 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4565 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4566 return;
4567 }
4568
4569 id = scaler_state->scaler_id;
4570 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4571 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4572 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4573 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4574
4575 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004576 }
4577}
4578
Jesse Barnesb074cec2013-04-25 12:55:02 -07004579static void ironlake_pfit_enable(struct intel_crtc *crtc)
4580{
4581 struct drm_device *dev = crtc->base.dev;
4582 struct drm_i915_private *dev_priv = dev->dev_private;
4583 int pipe = crtc->pipe;
4584
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004585 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004586 /* Force use of hard-coded filter coefficients
4587 * as some pre-programmed values are broken,
4588 * e.g. x201.
4589 */
4590 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4591 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4592 PF_PIPE_SEL_IVB(pipe));
4593 else
4594 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004595 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4596 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004597 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004598}
4599
Matt Roper4a3b8762014-12-23 10:41:51 -08004600static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004601{
4602 struct drm_device *dev = crtc->dev;
4603 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004604 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004605 struct intel_plane *intel_plane;
4606
Matt Roperaf2b6532014-04-01 15:22:32 -07004607 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4608 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004609 if (intel_plane->pipe == pipe)
4610 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004611 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004612}
4613
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004614void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004615{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004616 struct drm_device *dev = crtc->base.dev;
4617 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004618
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004619 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004620 return;
4621
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004622 /* We can only enable IPS after we enable a plane and wait for a vblank */
4623 intel_wait_for_vblank(dev, crtc->pipe);
4624
Paulo Zanonid77e4532013-09-24 13:52:55 -03004625 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004626 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004627 mutex_lock(&dev_priv->rps.hw_lock);
4628 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4629 mutex_unlock(&dev_priv->rps.hw_lock);
4630 /* Quoting Art Runyan: "its not safe to expect any particular
4631 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004632 * mailbox." Moreover, the mailbox may return a bogus state,
4633 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004634 */
4635 } else {
4636 I915_WRITE(IPS_CTL, IPS_ENABLE);
4637 /* The bit only becomes 1 in the next vblank, so this wait here
4638 * is essentially intel_wait_for_vblank. If we don't have this
4639 * and don't wait for vblanks until the end of crtc_enable, then
4640 * the HW state readout code will complain that the expected
4641 * IPS_CTL value is not the one we read. */
4642 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4643 DRM_ERROR("Timed out waiting for IPS enable\n");
4644 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004645}
4646
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004647void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004648{
4649 struct drm_device *dev = crtc->base.dev;
4650 struct drm_i915_private *dev_priv = dev->dev_private;
4651
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004652 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004653 return;
4654
4655 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004656 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004657 mutex_lock(&dev_priv->rps.hw_lock);
4658 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4659 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004660 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4661 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4662 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004663 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004664 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004665 POSTING_READ(IPS_CTL);
4666 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004667
4668 /* We need to wait for a vblank before we can disable the plane. */
4669 intel_wait_for_vblank(dev, crtc->pipe);
4670}
4671
4672/** Loads the palette/gamma unit for the CRTC with the prepared values */
4673static void intel_crtc_load_lut(struct drm_crtc *crtc)
4674{
4675 struct drm_device *dev = crtc->dev;
4676 struct drm_i915_private *dev_priv = dev->dev_private;
4677 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4678 enum pipe pipe = intel_crtc->pipe;
4679 int palreg = PALETTE(pipe);
4680 int i;
4681 bool reenable_ips = false;
4682
4683 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004684 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004685 return;
4686
Imre Deak50360402015-01-16 00:55:16 -08004687 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004688 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004689 assert_dsi_pll_enabled(dev_priv);
4690 else
4691 assert_pll_enabled(dev_priv, pipe);
4692 }
4693
4694 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304695 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004696 palreg = LGC_PALETTE(pipe);
4697
4698 /* Workaround : Do not read or write the pipe palette/gamma data while
4699 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4700 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004701 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004702 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4703 GAMMA_MODE_MODE_SPLIT)) {
4704 hsw_disable_ips(intel_crtc);
4705 reenable_ips = true;
4706 }
4707
4708 for (i = 0; i < 256; i++) {
4709 I915_WRITE(palreg + 4 * i,
4710 (intel_crtc->lut_r[i] << 16) |
4711 (intel_crtc->lut_g[i] << 8) |
4712 intel_crtc->lut_b[i]);
4713 }
4714
4715 if (reenable_ips)
4716 hsw_enable_ips(intel_crtc);
4717}
4718
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004719static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004720{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004721 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004722 struct drm_device *dev = intel_crtc->base.dev;
4723 struct drm_i915_private *dev_priv = dev->dev_private;
4724
4725 mutex_lock(&dev->struct_mutex);
4726 dev_priv->mm.interruptible = false;
4727 (void) intel_overlay_switch_off(intel_crtc->overlay);
4728 dev_priv->mm.interruptible = true;
4729 mutex_unlock(&dev->struct_mutex);
4730 }
4731
4732 /* Let userspace switch the overlay on again. In most cases userspace
4733 * has to recompute where to put it anyway.
4734 */
4735}
4736
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004737/**
4738 * intel_post_enable_primary - Perform operations after enabling primary plane
4739 * @crtc: the CRTC whose primary plane was just enabled
4740 *
4741 * Performs potentially sleeping operations that must be done after the primary
4742 * plane is enabled, such as updating FBC and IPS. Note that this may be
4743 * called due to an explicit primary plane update, or due to an implicit
4744 * re-enable that is caused when a sprite plane is updated to no longer
4745 * completely hide the primary plane.
4746 */
4747static void
4748intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004749{
4750 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004751 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004752 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4753 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004754
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004755 /*
4756 * BDW signals flip done immediately if the plane
4757 * is disabled, even if the plane enable is already
4758 * armed to occur at the next vblank :(
4759 */
4760 if (IS_BROADWELL(dev))
4761 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004762
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004763 /*
4764 * FIXME IPS should be fine as long as one plane is
4765 * enabled, but in practice it seems to have problems
4766 * when going from primary only to sprite only and vice
4767 * versa.
4768 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004769 hsw_enable_ips(intel_crtc);
4770
4771 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004772 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004773 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004774
4775 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004776 * Gen2 reports pipe underruns whenever all planes are disabled.
4777 * So don't enable underrun reporting before at least some planes
4778 * are enabled.
4779 * FIXME: Need to fix the logic to work when we turn off all planes
4780 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004781 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004782 if (IS_GEN2(dev))
4783 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4784
4785 /* Underruns don't raise interrupts, so check manually. */
4786 if (HAS_GMCH_DISPLAY(dev))
4787 i9xx_check_fifo_underruns(dev_priv);
4788}
4789
4790/**
4791 * intel_pre_disable_primary - Perform operations before disabling primary plane
4792 * @crtc: the CRTC whose primary plane is to be disabled
4793 *
4794 * Performs potentially sleeping operations that must be done before the
4795 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4796 * be called due to an explicit primary plane update, or due to an implicit
4797 * disable that is caused when a sprite plane completely hides the primary
4798 * plane.
4799 */
4800static void
4801intel_pre_disable_primary(struct drm_crtc *crtc)
4802{
4803 struct drm_device *dev = crtc->dev;
4804 struct drm_i915_private *dev_priv = dev->dev_private;
4805 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4806 int pipe = intel_crtc->pipe;
4807
4808 /*
4809 * Gen2 reports pipe underruns whenever all planes are disabled.
4810 * So diasble underrun reporting before all the planes get disabled.
4811 * FIXME: Need to fix the logic to work when we turn off all planes
4812 * but leave the pipe running.
4813 */
4814 if (IS_GEN2(dev))
4815 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4816
4817 /*
4818 * Vblank time updates from the shadow to live plane control register
4819 * are blocked if the memory self-refresh mode is active at that
4820 * moment. So to make sure the plane gets truly disabled, disable
4821 * first the self-refresh mode. The self-refresh enable bit in turn
4822 * will be checked/applied by the HW only at the next frame start
4823 * event which is after the vblank start event, so we need to have a
4824 * wait-for-vblank between disabling the plane and the pipe.
4825 */
4826 if (HAS_GMCH_DISPLAY(dev))
4827 intel_set_memory_cxsr(dev_priv, false);
4828
4829 mutex_lock(&dev->struct_mutex);
4830 if (dev_priv->fbc.crtc == intel_crtc)
4831 intel_fbc_disable(dev);
4832 mutex_unlock(&dev->struct_mutex);
4833
4834 /*
4835 * FIXME IPS should be fine as long as one plane is
4836 * enabled, but in practice it seems to have problems
4837 * when going from primary only to sprite only and vice
4838 * versa.
4839 */
4840 hsw_disable_ips(intel_crtc);
4841}
4842
4843static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4844{
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004845 intel_enable_primary_hw_plane(crtc->primary, crtc);
4846 intel_enable_sprite_planes(crtc);
4847 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004848
4849 intel_post_enable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004850}
4851
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004852static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004853{
4854 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004855 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004856 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004857 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004858
4859 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004860
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004861 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004862
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004863 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004864 for_each_intel_plane(dev, intel_plane) {
4865 if (intel_plane->pipe == pipe) {
4866 struct drm_crtc *from = intel_plane->base.crtc;
4867
4868 intel_plane->disable_plane(&intel_plane->base,
4869 from ?: crtc, true);
4870 }
4871 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004872
Daniel Vetterf99d7062014-06-19 16:01:59 +02004873 /*
4874 * FIXME: Once we grow proper nuclear flip support out of this we need
4875 * to compute the mask of flip planes precisely. For the time being
4876 * consider this a flip to a NULL plane.
4877 */
4878 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004879}
4880
Jesse Barnesf67a5592011-01-05 10:31:48 -08004881static void ironlake_crtc_enable(struct drm_crtc *crtc)
4882{
4883 struct drm_device *dev = crtc->dev;
4884 struct drm_i915_private *dev_priv = dev->dev_private;
4885 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004886 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004887 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004888
Matt Roper83d65732015-02-25 13:12:16 -08004889 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004890
Jesse Barnesf67a5592011-01-05 10:31:48 -08004891 if (intel_crtc->active)
4892 return;
4893
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004894 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004895 intel_prepare_shared_dpll(intel_crtc);
4896
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004897 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304898 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004899
4900 intel_set_pipe_timings(intel_crtc);
4901
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004902 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004903 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004904 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004905 }
4906
4907 ironlake_set_pipeconf(crtc);
4908
Jesse Barnesf67a5592011-01-05 10:31:48 -08004909 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004910
Daniel Vettera72e4c92014-09-30 10:56:47 +02004911 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4912 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004913
Daniel Vetterf6736a12013-06-05 13:34:30 +02004914 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004915 if (encoder->pre_enable)
4916 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004917
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004918 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004919 /* Note: FDI PLL enabling _must_ be done before we enable the
4920 * cpu pipes, hence this is separate from all the other fdi/pch
4921 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004922 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004923 } else {
4924 assert_fdi_tx_disabled(dev_priv, pipe);
4925 assert_fdi_rx_disabled(dev_priv, pipe);
4926 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004927
Jesse Barnesb074cec2013-04-25 12:55:02 -07004928 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004929
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004930 /*
4931 * On ILK+ LUT must be loaded before the pipe is running but with
4932 * clocks enabled
4933 */
4934 intel_crtc_load_lut(crtc);
4935
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004936 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004937 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004938
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004939 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004940 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004941
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004942 assert_vblank_disabled(crtc);
4943 drm_crtc_vblank_on(crtc);
4944
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004945 for_each_encoder_on_crtc(dev, crtc, encoder)
4946 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004947
4948 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004949 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004950}
4951
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004952/* IPS only exists on ULT machines and is tied to pipe A. */
4953static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4954{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004955 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004956}
4957
Paulo Zanonie4916942013-09-20 16:21:19 -03004958/*
4959 * This implements the workaround described in the "notes" section of the mode
4960 * set sequence documentation. When going from no pipes or single pipe to
4961 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4962 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4963 */
4964static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4965{
4966 struct drm_device *dev = crtc->base.dev;
4967 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4968
4969 /* We want to get the other_active_crtc only if there's only 1 other
4970 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004971 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004972 if (!crtc_it->active || crtc_it == crtc)
4973 continue;
4974
4975 if (other_active_crtc)
4976 return;
4977
4978 other_active_crtc = crtc_it;
4979 }
4980 if (!other_active_crtc)
4981 return;
4982
4983 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4984 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4985}
4986
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004987static void haswell_crtc_enable(struct drm_crtc *crtc)
4988{
4989 struct drm_device *dev = crtc->dev;
4990 struct drm_i915_private *dev_priv = dev->dev_private;
4991 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4992 struct intel_encoder *encoder;
4993 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004994
Matt Roper83d65732015-02-25 13:12:16 -08004995 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004996
4997 if (intel_crtc->active)
4998 return;
4999
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005000 if (intel_crtc_to_shared_dpll(intel_crtc))
5001 intel_enable_shared_dpll(intel_crtc);
5002
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005003 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05305004 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02005005
5006 intel_set_pipe_timings(intel_crtc);
5007
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005008 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
5009 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
5010 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07005011 }
5012
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005013 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005014 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005015 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005016 }
5017
5018 haswell_set_pipeconf(crtc);
5019
5020 intel_set_pipe_csc(crtc);
5021
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005022 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005023
Daniel Vettera72e4c92014-09-30 10:56:47 +02005024 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025 for_each_encoder_on_crtc(dev, crtc, encoder)
5026 if (encoder->pre_enable)
5027 encoder->pre_enable(encoder);
5028
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005029 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005030 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5031 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005032 dev_priv->display.fdi_link_train(crtc);
5033 }
5034
Paulo Zanoni1f544382012-10-24 11:32:00 -02005035 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005037 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005038 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005039 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005040 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005041 else
5042 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005043
5044 /*
5045 * On ILK+ LUT must be loaded before the pipe is running but with
5046 * clocks enabled
5047 */
5048 intel_crtc_load_lut(crtc);
5049
Paulo Zanoni1f544382012-10-24 11:32:00 -02005050 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005051 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005053 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005054 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005055
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005056 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005057 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005058
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005059 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005060 intel_ddi_set_vc_payload_alloc(crtc, true);
5061
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005062 assert_vblank_disabled(crtc);
5063 drm_crtc_vblank_on(crtc);
5064
Jani Nikula8807e552013-08-30 19:40:32 +03005065 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005066 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005067 intel_opregion_notify_encoder(encoder, true);
5068 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005069
Paulo Zanonie4916942013-09-20 16:21:19 -03005070 /* If we change the relative order between pipe/planes enabling, we need
5071 * to change the workaround. */
5072 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005073}
5074
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005075static void ironlake_pfit_disable(struct intel_crtc *crtc)
5076{
5077 struct drm_device *dev = crtc->base.dev;
5078 struct drm_i915_private *dev_priv = dev->dev_private;
5079 int pipe = crtc->pipe;
5080
5081 /* To avoid upsetting the power well on haswell only disable the pfit if
5082 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005083 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005084 I915_WRITE(PF_CTL(pipe), 0);
5085 I915_WRITE(PF_WIN_POS(pipe), 0);
5086 I915_WRITE(PF_WIN_SZ(pipe), 0);
5087 }
5088}
5089
Jesse Barnes6be4a602010-09-10 10:26:01 -07005090static void ironlake_crtc_disable(struct drm_crtc *crtc)
5091{
5092 struct drm_device *dev = crtc->dev;
5093 struct drm_i915_private *dev_priv = dev->dev_private;
5094 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005095 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005096 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005097 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005098
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005099 if (!intel_crtc->active)
5100 return;
5101
Daniel Vetterea9d7582012-07-10 10:42:52 +02005102 for_each_encoder_on_crtc(dev, crtc, encoder)
5103 encoder->disable(encoder);
5104
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005105 drm_crtc_vblank_off(crtc);
5106 assert_vblank_disabled(crtc);
5107
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005108 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005109 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005110
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005111 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005112
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005113 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005114
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005115 if (intel_crtc->config->has_pch_encoder)
5116 ironlake_fdi_disable(crtc);
5117
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005118 for_each_encoder_on_crtc(dev, crtc, encoder)
5119 if (encoder->post_disable)
5120 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005121
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005122 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005123 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005124
Daniel Vetterd925c592013-06-05 13:34:04 +02005125 if (HAS_PCH_CPT(dev)) {
5126 /* disable TRANS_DP_CTL */
5127 reg = TRANS_DP_CTL(pipe);
5128 temp = I915_READ(reg);
5129 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5130 TRANS_DP_PORT_SEL_MASK);
5131 temp |= TRANS_DP_PORT_SEL_NONE;
5132 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005133
Daniel Vetterd925c592013-06-05 13:34:04 +02005134 /* disable DPLL_SEL */
5135 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005136 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005137 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005138 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005139
5140 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005141 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005142
5143 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005144 }
5145
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005146 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005147 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005148
5149 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005150 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005151 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005152}
5153
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005154static void haswell_crtc_disable(struct drm_crtc *crtc)
5155{
5156 struct drm_device *dev = crtc->dev;
5157 struct drm_i915_private *dev_priv = dev->dev_private;
5158 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5159 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005160 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005161
5162 if (!intel_crtc->active)
5163 return;
5164
Jani Nikula8807e552013-08-30 19:40:32 +03005165 for_each_encoder_on_crtc(dev, crtc, encoder) {
5166 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005167 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005168 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005169
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005170 drm_crtc_vblank_off(crtc);
5171 assert_vblank_disabled(crtc);
5172
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005173 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005174 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5175 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005176 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005177
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005178 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005179 intel_ddi_set_vc_payload_alloc(crtc, false);
5180
Paulo Zanoniad80a812012-10-24 16:06:19 -02005181 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005182
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005183 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005184 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005185 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005186 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005187 else
5188 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005189
Paulo Zanoni1f544382012-10-24 11:32:00 -02005190 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005191
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005192 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005193 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005194 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005195 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005196
Imre Deak97b040a2014-06-25 22:01:50 +03005197 for_each_encoder_on_crtc(dev, crtc, encoder)
5198 if (encoder->post_disable)
5199 encoder->post_disable(encoder);
5200
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005201 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005202 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005203
5204 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005205 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005206 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005207
5208 if (intel_crtc_to_shared_dpll(intel_crtc))
5209 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005210}
5211
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005212static void ironlake_crtc_off(struct drm_crtc *crtc)
5213{
5214 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005215 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005216}
5217
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005218
Jesse Barnes2dd24552013-04-25 12:55:01 -07005219static void i9xx_pfit_enable(struct intel_crtc *crtc)
5220{
5221 struct drm_device *dev = crtc->base.dev;
5222 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005223 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005224
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005225 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005226 return;
5227
Daniel Vetterc0b03412013-05-28 12:05:54 +02005228 /*
5229 * The panel fitter should only be adjusted whilst the pipe is disabled,
5230 * according to register description and PRM.
5231 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005232 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5233 assert_pipe_disabled(dev_priv, crtc->pipe);
5234
Jesse Barnesb074cec2013-04-25 12:55:02 -07005235 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5236 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005237
5238 /* Border color in case we don't scale up to the full screen. Black by
5239 * default, change to something else for debugging. */
5240 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005241}
5242
Dave Airlied05410f2014-06-05 13:22:59 +10005243static enum intel_display_power_domain port_to_power_domain(enum port port)
5244{
5245 switch (port) {
5246 case PORT_A:
5247 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5248 case PORT_B:
5249 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5250 case PORT_C:
5251 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5252 case PORT_D:
5253 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5254 default:
5255 WARN_ON_ONCE(1);
5256 return POWER_DOMAIN_PORT_OTHER;
5257 }
5258}
5259
Imre Deak77d22dc2014-03-05 16:20:52 +02005260#define for_each_power_domain(domain, mask) \
5261 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5262 if ((1 << (domain)) & (mask))
5263
Imre Deak319be8a2014-03-04 19:22:57 +02005264enum intel_display_power_domain
5265intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005266{
Imre Deak319be8a2014-03-04 19:22:57 +02005267 struct drm_device *dev = intel_encoder->base.dev;
5268 struct intel_digital_port *intel_dig_port;
5269
5270 switch (intel_encoder->type) {
5271 case INTEL_OUTPUT_UNKNOWN:
5272 /* Only DDI platforms should ever use this output type */
5273 WARN_ON_ONCE(!HAS_DDI(dev));
5274 case INTEL_OUTPUT_DISPLAYPORT:
5275 case INTEL_OUTPUT_HDMI:
5276 case INTEL_OUTPUT_EDP:
5277 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005278 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005279 case INTEL_OUTPUT_DP_MST:
5280 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5281 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005282 case INTEL_OUTPUT_ANALOG:
5283 return POWER_DOMAIN_PORT_CRT;
5284 case INTEL_OUTPUT_DSI:
5285 return POWER_DOMAIN_PORT_DSI;
5286 default:
5287 return POWER_DOMAIN_PORT_OTHER;
5288 }
5289}
5290
5291static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5292{
5293 struct drm_device *dev = crtc->dev;
5294 struct intel_encoder *intel_encoder;
5295 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5296 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005297 unsigned long mask;
5298 enum transcoder transcoder;
5299
5300 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5301
5302 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5303 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005304 if (intel_crtc->config->pch_pfit.enabled ||
5305 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005306 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5307
Imre Deak319be8a2014-03-04 19:22:57 +02005308 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5309 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5310
Imre Deak77d22dc2014-03-05 16:20:52 +02005311 return mask;
5312}
5313
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005314static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005315{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005316 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005317 struct drm_i915_private *dev_priv = dev->dev_private;
5318 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5319 struct intel_crtc *crtc;
5320
5321 /*
5322 * First get all needed power domains, then put all unneeded, to avoid
5323 * any unnecessary toggling of the power wells.
5324 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005325 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005326 enum intel_display_power_domain domain;
5327
Matt Roper83d65732015-02-25 13:12:16 -08005328 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005329 continue;
5330
Imre Deak319be8a2014-03-04 19:22:57 +02005331 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005332
5333 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5334 intel_display_power_get(dev_priv, domain);
5335 }
5336
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005337 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005338 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005339
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005340 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005341 enum intel_display_power_domain domain;
5342
5343 for_each_power_domain(domain, crtc->enabled_power_domains)
5344 intel_display_power_put(dev_priv, domain);
5345
5346 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5347 }
5348
5349 intel_display_set_init_power(dev_priv, false);
5350}
5351
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305352void broxton_set_cdclk(struct drm_device *dev, int frequency)
5353{
5354 struct drm_i915_private *dev_priv = dev->dev_private;
5355 uint32_t divider;
5356 uint32_t ratio;
5357 uint32_t current_freq;
5358 int ret;
5359
5360 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5361 switch (frequency) {
5362 case 144000:
5363 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5364 ratio = BXT_DE_PLL_RATIO(60);
5365 break;
5366 case 288000:
5367 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5368 ratio = BXT_DE_PLL_RATIO(60);
5369 break;
5370 case 384000:
5371 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5372 ratio = BXT_DE_PLL_RATIO(60);
5373 break;
5374 case 576000:
5375 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5376 ratio = BXT_DE_PLL_RATIO(60);
5377 break;
5378 case 624000:
5379 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5380 ratio = BXT_DE_PLL_RATIO(65);
5381 break;
5382 case 19200:
5383 /*
5384 * Bypass frequency with DE PLL disabled. Init ratio, divider
5385 * to suppress GCC warning.
5386 */
5387 ratio = 0;
5388 divider = 0;
5389 break;
5390 default:
5391 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5392
5393 return;
5394 }
5395
5396 mutex_lock(&dev_priv->rps.hw_lock);
5397 /* Inform power controller of upcoming frequency change */
5398 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5399 0x80000000);
5400 mutex_unlock(&dev_priv->rps.hw_lock);
5401
5402 if (ret) {
5403 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5404 ret, frequency);
5405 return;
5406 }
5407
5408 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5409 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5410 current_freq = current_freq * 500 + 1000;
5411
5412 /*
5413 * DE PLL has to be disabled when
5414 * - setting to 19.2MHz (bypass, PLL isn't used)
5415 * - before setting to 624MHz (PLL needs toggling)
5416 * - before setting to any frequency from 624MHz (PLL needs toggling)
5417 */
5418 if (frequency == 19200 || frequency == 624000 ||
5419 current_freq == 624000) {
5420 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5421 /* Timeout 200us */
5422 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5423 1))
5424 DRM_ERROR("timout waiting for DE PLL unlock\n");
5425 }
5426
5427 if (frequency != 19200) {
5428 uint32_t val;
5429
5430 val = I915_READ(BXT_DE_PLL_CTL);
5431 val &= ~BXT_DE_PLL_RATIO_MASK;
5432 val |= ratio;
5433 I915_WRITE(BXT_DE_PLL_CTL, val);
5434
5435 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5436 /* Timeout 200us */
5437 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5438 DRM_ERROR("timeout waiting for DE PLL lock\n");
5439
5440 val = I915_READ(CDCLK_CTL);
5441 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5442 val |= divider;
5443 /*
5444 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5445 * enable otherwise.
5446 */
5447 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5448 if (frequency >= 500000)
5449 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5450
5451 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5452 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5453 val |= (frequency - 1000) / 500;
5454 I915_WRITE(CDCLK_CTL, val);
5455 }
5456
5457 mutex_lock(&dev_priv->rps.hw_lock);
5458 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5459 DIV_ROUND_UP(frequency, 25000));
5460 mutex_unlock(&dev_priv->rps.hw_lock);
5461
5462 if (ret) {
5463 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5464 ret, frequency);
5465 return;
5466 }
5467
5468 dev_priv->cdclk_freq = frequency;
5469}
5470
5471void broxton_init_cdclk(struct drm_device *dev)
5472{
5473 struct drm_i915_private *dev_priv = dev->dev_private;
5474 uint32_t val;
5475
5476 /*
5477 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5478 * or else the reset will hang because there is no PCH to respond.
5479 * Move the handshake programming to initialization sequence.
5480 * Previously was left up to BIOS.
5481 */
5482 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5483 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5484 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5485
5486 /* Enable PG1 for cdclk */
5487 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5488
5489 /* check if cd clock is enabled */
5490 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5491 DRM_DEBUG_KMS("Display already initialized\n");
5492 return;
5493 }
5494
5495 /*
5496 * FIXME:
5497 * - The initial CDCLK needs to be read from VBT.
5498 * Need to make this change after VBT has changes for BXT.
5499 * - check if setting the max (or any) cdclk freq is really necessary
5500 * here, it belongs to modeset time
5501 */
5502 broxton_set_cdclk(dev, 624000);
5503
5504 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005505 POSTING_READ(DBUF_CTL);
5506
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305507 udelay(10);
5508
5509 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5510 DRM_ERROR("DBuf power enable timeout!\n");
5511}
5512
5513void broxton_uninit_cdclk(struct drm_device *dev)
5514{
5515 struct drm_i915_private *dev_priv = dev->dev_private;
5516
5517 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005518 POSTING_READ(DBUF_CTL);
5519
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305520 udelay(10);
5521
5522 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5523 DRM_ERROR("DBuf power disable timeout!\n");
5524
5525 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5526 broxton_set_cdclk(dev, 19200);
5527
5528 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5529}
5530
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005531static const struct skl_cdclk_entry {
5532 unsigned int freq;
5533 unsigned int vco;
5534} skl_cdclk_frequencies[] = {
5535 { .freq = 308570, .vco = 8640 },
5536 { .freq = 337500, .vco = 8100 },
5537 { .freq = 432000, .vco = 8640 },
5538 { .freq = 450000, .vco = 8100 },
5539 { .freq = 540000, .vco = 8100 },
5540 { .freq = 617140, .vco = 8640 },
5541 { .freq = 675000, .vco = 8100 },
5542};
5543
5544static unsigned int skl_cdclk_decimal(unsigned int freq)
5545{
5546 return (freq - 1000) / 500;
5547}
5548
5549static unsigned int skl_cdclk_get_vco(unsigned int freq)
5550{
5551 unsigned int i;
5552
5553 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5554 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5555
5556 if (e->freq == freq)
5557 return e->vco;
5558 }
5559
5560 return 8100;
5561}
5562
5563static void
5564skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5565{
5566 unsigned int min_freq;
5567 u32 val;
5568
5569 /* select the minimum CDCLK before enabling DPLL 0 */
5570 val = I915_READ(CDCLK_CTL);
5571 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5572 val |= CDCLK_FREQ_337_308;
5573
5574 if (required_vco == 8640)
5575 min_freq = 308570;
5576 else
5577 min_freq = 337500;
5578
5579 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5580
5581 I915_WRITE(CDCLK_CTL, val);
5582 POSTING_READ(CDCLK_CTL);
5583
5584 /*
5585 * We always enable DPLL0 with the lowest link rate possible, but still
5586 * taking into account the VCO required to operate the eDP panel at the
5587 * desired frequency. The usual DP link rates operate with a VCO of
5588 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5589 * The modeset code is responsible for the selection of the exact link
5590 * rate later on, with the constraint of choosing a frequency that
5591 * works with required_vco.
5592 */
5593 val = I915_READ(DPLL_CTRL1);
5594
5595 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5596 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5597 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5598 if (required_vco == 8640)
5599 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5600 SKL_DPLL0);
5601 else
5602 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5603 SKL_DPLL0);
5604
5605 I915_WRITE(DPLL_CTRL1, val);
5606 POSTING_READ(DPLL_CTRL1);
5607
5608 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5609
5610 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5611 DRM_ERROR("DPLL0 not locked\n");
5612}
5613
5614static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5615{
5616 int ret;
5617 u32 val;
5618
5619 /* inform PCU we want to change CDCLK */
5620 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5621 mutex_lock(&dev_priv->rps.hw_lock);
5622 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5623 mutex_unlock(&dev_priv->rps.hw_lock);
5624
5625 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5626}
5627
5628static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5629{
5630 unsigned int i;
5631
5632 for (i = 0; i < 15; i++) {
5633 if (skl_cdclk_pcu_ready(dev_priv))
5634 return true;
5635 udelay(10);
5636 }
5637
5638 return false;
5639}
5640
5641static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5642{
5643 u32 freq_select, pcu_ack;
5644
5645 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5646
5647 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5648 DRM_ERROR("failed to inform PCU about cdclk change\n");
5649 return;
5650 }
5651
5652 /* set CDCLK_CTL */
5653 switch(freq) {
5654 case 450000:
5655 case 432000:
5656 freq_select = CDCLK_FREQ_450_432;
5657 pcu_ack = 1;
5658 break;
5659 case 540000:
5660 freq_select = CDCLK_FREQ_540;
5661 pcu_ack = 2;
5662 break;
5663 case 308570:
5664 case 337500:
5665 default:
5666 freq_select = CDCLK_FREQ_337_308;
5667 pcu_ack = 0;
5668 break;
5669 case 617140:
5670 case 675000:
5671 freq_select = CDCLK_FREQ_675_617;
5672 pcu_ack = 3;
5673 break;
5674 }
5675
5676 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5677 POSTING_READ(CDCLK_CTL);
5678
5679 /* inform PCU of the change */
5680 mutex_lock(&dev_priv->rps.hw_lock);
5681 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5682 mutex_unlock(&dev_priv->rps.hw_lock);
5683}
5684
5685void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5686{
5687 /* disable DBUF power */
5688 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5689 POSTING_READ(DBUF_CTL);
5690
5691 udelay(10);
5692
5693 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5694 DRM_ERROR("DBuf power disable timeout\n");
5695
5696 /* disable DPLL0 */
5697 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5698 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5699 DRM_ERROR("Couldn't disable DPLL0\n");
5700
5701 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5702}
5703
5704void skl_init_cdclk(struct drm_i915_private *dev_priv)
5705{
5706 u32 val;
5707 unsigned int required_vco;
5708
5709 /* enable PCH reset handshake */
5710 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5711 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5712
5713 /* enable PG1 and Misc I/O */
5714 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5715
5716 /* DPLL0 already enabed !? */
5717 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5718 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5719 return;
5720 }
5721
5722 /* enable DPLL0 */
5723 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5724 skl_dpll0_enable(dev_priv, required_vco);
5725
5726 /* set CDCLK to the frequency the BIOS chose */
5727 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5728
5729 /* enable DBUF power */
5730 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5731 POSTING_READ(DBUF_CTL);
5732
5733 udelay(10);
5734
5735 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5736 DRM_ERROR("DBuf power enable timeout\n");
5737}
5738
Ville Syrjälädfcab172014-06-13 13:37:47 +03005739/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005740static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005741{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005742 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005743
Jesse Barnes586f49d2013-11-04 16:06:59 -08005744 /* Obtain SKU information */
5745 mutex_lock(&dev_priv->dpio_lock);
5746 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5747 CCK_FUSE_HPLL_FREQ_MASK;
5748 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005749
Ville Syrjälädfcab172014-06-13 13:37:47 +03005750 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005751}
5752
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005753static void vlv_update_cdclk(struct drm_device *dev)
5754{
5755 struct drm_i915_private *dev_priv = dev->dev_private;
5756
Vandana Kannan164dfd22014-11-24 13:37:41 +05305757 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005758 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305759 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005760
5761 /*
5762 * Program the gmbus_freq based on the cdclk frequency.
5763 * BSpec erroneously claims we should aim for 4MHz, but
5764 * in fact 1MHz is the correct frequency.
5765 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305766 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005767}
5768
Jesse Barnes30a970c2013-11-04 13:48:12 -08005769/* Adjust CDclk dividers to allow high res or save power if possible */
5770static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5771{
5772 struct drm_i915_private *dev_priv = dev->dev_private;
5773 u32 val, cmd;
5774
Vandana Kannan164dfd22014-11-24 13:37:41 +05305775 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5776 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005777
Ville Syrjälädfcab172014-06-13 13:37:47 +03005778 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005779 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005780 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781 cmd = 1;
5782 else
5783 cmd = 0;
5784
5785 mutex_lock(&dev_priv->rps.hw_lock);
5786 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5787 val &= ~DSPFREQGUAR_MASK;
5788 val |= (cmd << DSPFREQGUAR_SHIFT);
5789 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5790 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5791 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5792 50)) {
5793 DRM_ERROR("timed out waiting for CDclk change\n");
5794 }
5795 mutex_unlock(&dev_priv->rps.hw_lock);
5796
Ville Syrjälädfcab172014-06-13 13:37:47 +03005797 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005798 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005799
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005800 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005801
5802 mutex_lock(&dev_priv->dpio_lock);
5803 /* adjust cdclk divider */
5804 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005805 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005806 val |= divider;
5807 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005808
5809 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5810 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5811 50))
5812 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005813 mutex_unlock(&dev_priv->dpio_lock);
5814 }
5815
5816 mutex_lock(&dev_priv->dpio_lock);
5817 /* adjust self-refresh exit latency value */
5818 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5819 val &= ~0x7f;
5820
5821 /*
5822 * For high bandwidth configs, we set a higher latency in the bunit
5823 * so that the core display fetch happens in time to avoid underruns.
5824 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005825 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005826 val |= 4500 / 250; /* 4.5 usec */
5827 else
5828 val |= 3000 / 250; /* 3.0 usec */
5829 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
5830 mutex_unlock(&dev_priv->dpio_lock);
5831
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005832 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005833}
5834
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005835static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5836{
5837 struct drm_i915_private *dev_priv = dev->dev_private;
5838 u32 val, cmd;
5839
Vandana Kannan164dfd22014-11-24 13:37:41 +05305840 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5841 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005842
5843 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005844 case 333333:
5845 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005846 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005847 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005848 break;
5849 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005850 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005851 return;
5852 }
5853
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005854 /*
5855 * Specs are full of misinformation, but testing on actual
5856 * hardware has shown that we just need to write the desired
5857 * CCK divider into the Punit register.
5858 */
5859 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5860
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005861 mutex_lock(&dev_priv->rps.hw_lock);
5862 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5863 val &= ~DSPFREQGUAR_MASK_CHV;
5864 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5865 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5866 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5867 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5868 50)) {
5869 DRM_ERROR("timed out waiting for CDclk change\n");
5870 }
5871 mutex_unlock(&dev_priv->rps.hw_lock);
5872
5873 vlv_update_cdclk(dev);
5874}
5875
Jesse Barnes30a970c2013-11-04 13:48:12 -08005876static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5877 int max_pixclk)
5878{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005879 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005880 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005881
Jesse Barnes30a970c2013-11-04 13:48:12 -08005882 /*
5883 * Really only a few cases to deal with, as only 4 CDclks are supported:
5884 * 200MHz
5885 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005886 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005887 * 400MHz (VLV only)
5888 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5889 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005890 *
5891 * We seem to get an unstable or solid color picture at 200MHz.
5892 * Not sure what's wrong. For now use 200MHz only when all pipes
5893 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005894 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005895 if (!IS_CHERRYVIEW(dev_priv) &&
5896 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005897 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005898 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005899 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005900 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005901 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005902 else
5903 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005904}
5905
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305906static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5907 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005908{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305909 /*
5910 * FIXME:
5911 * - remove the guardband, it's not needed on BXT
5912 * - set 19.2MHz bypass frequency if there are no active pipes
5913 */
5914 if (max_pixclk > 576000*9/10)
5915 return 624000;
5916 else if (max_pixclk > 384000*9/10)
5917 return 576000;
5918 else if (max_pixclk > 288000*9/10)
5919 return 384000;
5920 else if (max_pixclk > 144000*9/10)
5921 return 288000;
5922 else
5923 return 144000;
5924}
5925
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005926/* Compute the max pixel clock for new configuration. Uses atomic state if
5927 * that's non-NULL, look at current state otherwise. */
5928static int intel_mode_max_pixclk(struct drm_device *dev,
5929 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005930{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005931 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005932 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005933 int max_pixclk = 0;
5934
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005935 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005936 if (state)
5937 crtc_state =
5938 intel_atomic_get_crtc_state(state, intel_crtc);
5939 else
5940 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005941 if (IS_ERR(crtc_state))
5942 return PTR_ERR(crtc_state);
5943
5944 if (!crtc_state->base.enable)
5945 continue;
5946
5947 max_pixclk = max(max_pixclk,
5948 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005949 }
5950
5951 return max_pixclk;
5952}
5953
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005954static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005955{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005956 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005957 struct drm_crtc *crtc;
5958 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005959 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005960 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005961
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005962 if (max_pixclk < 0)
5963 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005964
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305965 if (IS_VALLEYVIEW(dev_priv))
5966 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5967 else
5968 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5969
5970 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005971 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005972
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005973 /* add all active pipes to the state */
5974 for_each_crtc(state->dev, crtc) {
5975 if (!crtc->state->enable)
5976 continue;
5977
5978 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5979 if (IS_ERR(crtc_state))
5980 return PTR_ERR(crtc_state);
5981 }
5982
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005983 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005984 for_each_crtc_in_state(state, crtc, crtc_state, i)
5985 if (crtc_state->enable)
5986 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005987
5988 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005989}
5990
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005991static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5992{
5993 unsigned int credits, default_credits;
5994
5995 if (IS_CHERRYVIEW(dev_priv))
5996 default_credits = PFI_CREDIT(12);
5997 else
5998 default_credits = PFI_CREDIT(8);
5999
Vandana Kannan164dfd22014-11-24 13:37:41 +05306000 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006001 /* CHV suggested value is 31 or 63 */
6002 if (IS_CHERRYVIEW(dev_priv))
6003 credits = PFI_CREDIT_31;
6004 else
6005 credits = PFI_CREDIT(15);
6006 } else {
6007 credits = default_credits;
6008 }
6009
6010 /*
6011 * WA - write default credits before re-programming
6012 * FIXME: should we also set the resend bit here?
6013 */
6014 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6015 default_credits);
6016
6017 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6018 credits | PFI_CREDIT_RESEND);
6019
6020 /*
6021 * FIXME is this guaranteed to clear
6022 * immediately or should we poll for it?
6023 */
6024 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6025}
6026
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006027static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006028{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006029 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006030 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006031 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006032 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006033
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006034 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
6035 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006036 if (WARN_ON(max_pixclk < 0))
6037 return;
6038
6039 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006040
Vandana Kannan164dfd22014-11-24 13:37:41 +05306041 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02006042 /*
6043 * FIXME: We can end up here with all power domains off, yet
6044 * with a CDCLK frequency other than the minimum. To account
6045 * for this take the PIPE-A power domain, which covers the HW
6046 * blocks needed for the following programming. This can be
6047 * removed once it's guaranteed that we get here either with
6048 * the minimum CDCLK set, or the required power domains
6049 * enabled.
6050 */
6051 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6052
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006053 if (IS_CHERRYVIEW(dev))
6054 cherryview_set_cdclk(dev, req_cdclk);
6055 else
6056 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006057
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006058 vlv_program_pfi_credits(dev_priv);
6059
Imre Deak738c05c2014-11-19 16:25:37 +02006060 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006061 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006062}
6063
Jesse Barnes89b667f2013-04-18 14:51:36 -07006064static void valleyview_crtc_enable(struct drm_crtc *crtc)
6065{
6066 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006067 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6069 struct intel_encoder *encoder;
6070 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006071 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006072
Matt Roper83d65732015-02-25 13:12:16 -08006073 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006074
6075 if (intel_crtc->active)
6076 return;
6077
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006078 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306079
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006080 if (!is_dsi) {
6081 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006082 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006083 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006084 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006085 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006086
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006087 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306088 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006089
6090 intel_set_pipe_timings(intel_crtc);
6091
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006092 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6093 struct drm_i915_private *dev_priv = dev->dev_private;
6094
6095 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6096 I915_WRITE(CHV_CANVAS(pipe), 0);
6097 }
6098
Daniel Vetter5b18e572014-04-24 23:55:06 +02006099 i9xx_set_pipeconf(intel_crtc);
6100
Jesse Barnes89b667f2013-04-18 14:51:36 -07006101 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006102
Daniel Vettera72e4c92014-09-30 10:56:47 +02006103 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006104
Jesse Barnes89b667f2013-04-18 14:51:36 -07006105 for_each_encoder_on_crtc(dev, crtc, encoder)
6106 if (encoder->pre_pll_enable)
6107 encoder->pre_pll_enable(encoder);
6108
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006109 if (!is_dsi) {
6110 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006111 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006112 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006113 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006114 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006115
6116 for_each_encoder_on_crtc(dev, crtc, encoder)
6117 if (encoder->pre_enable)
6118 encoder->pre_enable(encoder);
6119
Jesse Barnes2dd24552013-04-25 12:55:01 -07006120 i9xx_pfit_enable(intel_crtc);
6121
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006122 intel_crtc_load_lut(crtc);
6123
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006124 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006125 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006126
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006127 assert_vblank_disabled(crtc);
6128 drm_crtc_vblank_on(crtc);
6129
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006130 for_each_encoder_on_crtc(dev, crtc, encoder)
6131 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006132}
6133
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006134static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6135{
6136 struct drm_device *dev = crtc->base.dev;
6137 struct drm_i915_private *dev_priv = dev->dev_private;
6138
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006139 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6140 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006141}
6142
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006143static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006144{
6145 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006146 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006148 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006149 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006150
Matt Roper83d65732015-02-25 13:12:16 -08006151 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02006152
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006153 if (intel_crtc->active)
6154 return;
6155
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006156 i9xx_set_pll_dividers(intel_crtc);
6157
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006158 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306159 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006160
6161 intel_set_pipe_timings(intel_crtc);
6162
Daniel Vetter5b18e572014-04-24 23:55:06 +02006163 i9xx_set_pipeconf(intel_crtc);
6164
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006165 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006166
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006167 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006168 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006169
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006170 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006171 if (encoder->pre_enable)
6172 encoder->pre_enable(encoder);
6173
Daniel Vetterf6736a12013-06-05 13:34:30 +02006174 i9xx_enable_pll(intel_crtc);
6175
Jesse Barnes2dd24552013-04-25 12:55:01 -07006176 i9xx_pfit_enable(intel_crtc);
6177
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006178 intel_crtc_load_lut(crtc);
6179
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006180 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006181 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006182
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006183 assert_vblank_disabled(crtc);
6184 drm_crtc_vblank_on(crtc);
6185
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006186 for_each_encoder_on_crtc(dev, crtc, encoder)
6187 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006188}
6189
Daniel Vetter87476d62013-04-11 16:29:06 +02006190static void i9xx_pfit_disable(struct intel_crtc *crtc)
6191{
6192 struct drm_device *dev = crtc->base.dev;
6193 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006194
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006195 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006196 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006197
6198 assert_pipe_disabled(dev_priv, crtc->pipe);
6199
Daniel Vetter328d8e82013-05-08 10:36:31 +02006200 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6201 I915_READ(PFIT_CONTROL));
6202 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006203}
6204
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006205static void i9xx_crtc_disable(struct drm_crtc *crtc)
6206{
6207 struct drm_device *dev = crtc->dev;
6208 struct drm_i915_private *dev_priv = dev->dev_private;
6209 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006210 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006211 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006212
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006213 if (!intel_crtc->active)
6214 return;
6215
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006216 /*
6217 * On gen2 planes are double buffered but the pipe isn't, so we must
6218 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006219 * We also need to wait on all gmch platforms because of the
6220 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006221 */
Imre Deak564ed192014-06-13 14:54:21 +03006222 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006223
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006224 for_each_encoder_on_crtc(dev, crtc, encoder)
6225 encoder->disable(encoder);
6226
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006227 drm_crtc_vblank_off(crtc);
6228 assert_vblank_disabled(crtc);
6229
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006230 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006231
Daniel Vetter87476d62013-04-11 16:29:06 +02006232 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006233
Jesse Barnes89b667f2013-04-18 14:51:36 -07006234 for_each_encoder_on_crtc(dev, crtc, encoder)
6235 if (encoder->post_disable)
6236 encoder->post_disable(encoder);
6237
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006238 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006239 if (IS_CHERRYVIEW(dev))
6240 chv_disable_pll(dev_priv, pipe);
6241 else if (IS_VALLEYVIEW(dev))
6242 vlv_disable_pll(dev_priv, pipe);
6243 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006244 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006245 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006246
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006247 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006248 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006249
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006250 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006251 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006252
Daniel Vetterefa96242014-04-24 23:55:02 +02006253 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006254 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006255 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006256}
6257
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006258static void i9xx_crtc_off(struct drm_crtc *crtc)
6259{
6260}
6261
Borun Fub04c5bd2014-07-12 10:02:27 +05306262/* Master function to enable/disable CRTC and corresponding power wells */
6263void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006264{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006265 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006266 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006267 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006268 enum intel_display_power_domain domain;
6269 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006270
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006271 if (enable) {
6272 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006273 domains = get_crtc_power_domains(crtc);
6274 for_each_power_domain(domain, domains)
6275 intel_display_power_get(dev_priv, domain);
6276 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006277
6278 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006279 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006280 }
6281 } else {
6282 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006283 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006284 dev_priv->display.crtc_disable(crtc);
6285
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006286 domains = intel_crtc->enabled_power_domains;
6287 for_each_power_domain(domain, domains)
6288 intel_display_power_put(dev_priv, domain);
6289 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006290 }
6291 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306292}
6293
6294/**
6295 * Sets the power management mode of the pipe and plane.
6296 */
6297void intel_crtc_update_dpms(struct drm_crtc *crtc)
6298{
6299 struct drm_device *dev = crtc->dev;
6300 struct intel_encoder *intel_encoder;
6301 bool enable = false;
6302
6303 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6304 enable |= intel_encoder->connectors_active;
6305
6306 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006307
6308 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006309}
6310
Daniel Vetter976f8a22012-07-08 22:34:21 +02006311static void intel_crtc_disable(struct drm_crtc *crtc)
6312{
6313 struct drm_device *dev = crtc->dev;
6314 struct drm_connector *connector;
6315 struct drm_i915_private *dev_priv = dev->dev_private;
6316
6317 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006318 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006319
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006320 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006321 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006322 dev_priv->display.off(crtc);
6323
Matt Roper70a101f2015-04-08 18:56:53 -07006324 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006325
6326 /* Update computed state. */
6327 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6328 if (!connector->encoder || !connector->encoder->crtc)
6329 continue;
6330
6331 if (connector->encoder->crtc != crtc)
6332 continue;
6333
6334 connector->dpms = DRM_MODE_DPMS_OFF;
6335 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006336 }
6337}
6338
Chris Wilsonea5b2132010-08-04 13:50:23 +01006339void intel_encoder_destroy(struct drm_encoder *encoder)
6340{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006341 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006342
Chris Wilsonea5b2132010-08-04 13:50:23 +01006343 drm_encoder_cleanup(encoder);
6344 kfree(intel_encoder);
6345}
6346
Damien Lespiau92373292013-08-08 22:28:57 +01006347/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006348 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6349 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006350static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006351{
6352 if (mode == DRM_MODE_DPMS_ON) {
6353 encoder->connectors_active = true;
6354
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006355 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006356 } else {
6357 encoder->connectors_active = false;
6358
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006359 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006360 }
6361}
6362
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006363/* Cross check the actual hw state with our own modeset state tracking (and it's
6364 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006365static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006366{
6367 if (connector->get_hw_state(connector)) {
6368 struct intel_encoder *encoder = connector->encoder;
6369 struct drm_crtc *crtc;
6370 bool encoder_enabled;
6371 enum pipe pipe;
6372
6373 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6374 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006375 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006376
Dave Airlie0e32b392014-05-02 14:02:48 +10006377 /* there is no real hw state for MST connectors */
6378 if (connector->mst_port)
6379 return;
6380
Rob Clarke2c719b2014-12-15 13:56:32 -05006381 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006382 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006383 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006384 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006385
Dave Airlie36cd7442014-05-02 13:44:18 +10006386 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006387 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006388 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006389
Dave Airlie36cd7442014-05-02 13:44:18 +10006390 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006391 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6392 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006393 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006394
Dave Airlie36cd7442014-05-02 13:44:18 +10006395 crtc = encoder->base.crtc;
6396
Matt Roper83d65732015-02-25 13:12:16 -08006397 I915_STATE_WARN(!crtc->state->enable,
6398 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006399 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6400 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006401 "encoder active on the wrong pipe\n");
6402 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006403 }
6404}
6405
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006406int intel_connector_init(struct intel_connector *connector)
6407{
6408 struct drm_connector_state *connector_state;
6409
6410 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6411 if (!connector_state)
6412 return -ENOMEM;
6413
6414 connector->base.state = connector_state;
6415 return 0;
6416}
6417
6418struct intel_connector *intel_connector_alloc(void)
6419{
6420 struct intel_connector *connector;
6421
6422 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6423 if (!connector)
6424 return NULL;
6425
6426 if (intel_connector_init(connector) < 0) {
6427 kfree(connector);
6428 return NULL;
6429 }
6430
6431 return connector;
6432}
6433
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006434/* Even simpler default implementation, if there's really no special case to
6435 * consider. */
6436void intel_connector_dpms(struct drm_connector *connector, int mode)
6437{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006438 /* All the simple cases only support two dpms states. */
6439 if (mode != DRM_MODE_DPMS_ON)
6440 mode = DRM_MODE_DPMS_OFF;
6441
6442 if (mode == connector->dpms)
6443 return;
6444
6445 connector->dpms = mode;
6446
6447 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006448 if (connector->encoder)
6449 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006450
Daniel Vetterb9805142012-08-31 17:37:33 +02006451 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006452}
6453
Daniel Vetterf0947c32012-07-02 13:10:34 +02006454/* Simple connector->get_hw_state implementation for encoders that support only
6455 * one connector and no cloning and hence the encoder state determines the state
6456 * of the connector. */
6457bool intel_connector_get_hw_state(struct intel_connector *connector)
6458{
Daniel Vetter24929352012-07-02 20:28:59 +02006459 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006460 struct intel_encoder *encoder = connector->encoder;
6461
6462 return encoder->get_hw_state(encoder, &pipe);
6463}
6464
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006465static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006466{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006467 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6468 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006469
6470 return 0;
6471}
6472
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006473static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006474 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006475{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006476 struct drm_atomic_state *state = pipe_config->base.state;
6477 struct intel_crtc *other_crtc;
6478 struct intel_crtc_state *other_crtc_state;
6479
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6481 pipe_name(pipe), pipe_config->fdi_lanes);
6482 if (pipe_config->fdi_lanes > 4) {
6483 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6484 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006486 }
6487
Paulo Zanonibafb6552013-11-02 21:07:44 -07006488 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006489 if (pipe_config->fdi_lanes > 2) {
6490 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6491 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006492 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006493 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006494 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006495 }
6496 }
6497
6498 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006499 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006500
6501 /* Ivybridge 3 pipe is really complicated */
6502 switch (pipe) {
6503 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006504 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006505 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006506 if (pipe_config->fdi_lanes <= 2)
6507 return 0;
6508
6509 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6510 other_crtc_state =
6511 intel_atomic_get_crtc_state(state, other_crtc);
6512 if (IS_ERR(other_crtc_state))
6513 return PTR_ERR(other_crtc_state);
6514
6515 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006516 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6517 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006518 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006519 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006520 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006521 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006522 if (pipe_config->fdi_lanes > 2) {
6523 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6524 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006525 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006526 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006527
6528 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6529 other_crtc_state =
6530 intel_atomic_get_crtc_state(state, other_crtc);
6531 if (IS_ERR(other_crtc_state))
6532 return PTR_ERR(other_crtc_state);
6533
6534 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006535 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006536 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006537 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006538 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006539 default:
6540 BUG();
6541 }
6542}
6543
Daniel Vettere29c22c2013-02-21 00:00:16 +01006544#define RETRY 1
6545static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006546 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006547{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006548 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006549 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006550 int lane, link_bw, fdi_dotclock, ret;
6551 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006552
Daniel Vettere29c22c2013-02-21 00:00:16 +01006553retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006554 /* FDI is a binary signal running at ~2.7GHz, encoding
6555 * each output octet as 10 bits. The actual frequency
6556 * is stored as a divider into a 100MHz clock, and the
6557 * mode pixel clock is stored in units of 1KHz.
6558 * Hence the bw of each lane in terms of the mode signal
6559 * is:
6560 */
6561 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6562
Damien Lespiau241bfc32013-09-25 16:45:37 +01006563 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006564
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006565 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006566 pipe_config->pipe_bpp);
6567
6568 pipe_config->fdi_lanes = lane;
6569
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006570 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006571 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006572
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006573 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6574 intel_crtc->pipe, pipe_config);
6575 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006576 pipe_config->pipe_bpp -= 2*3;
6577 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6578 pipe_config->pipe_bpp);
6579 needs_recompute = true;
6580 pipe_config->bw_constrained = true;
6581
6582 goto retry;
6583 }
6584
6585 if (needs_recompute)
6586 return RETRY;
6587
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006588 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006589}
6590
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006591static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006592 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006593{
Jani Nikulad330a952014-01-21 11:24:25 +02006594 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006595 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006596 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006597}
6598
Daniel Vettera43f6e02013-06-07 23:10:32 +02006599static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006600 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006601{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006602 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006603 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006604 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006605 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006606
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006607 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006608 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006609 int clock_limit =
6610 dev_priv->display.get_display_clock_speed(dev);
6611
6612 /*
6613 * Enable pixel doubling when the dot clock
6614 * is > 90% of the (display) core speed.
6615 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006616 * GDG double wide on either pipe,
6617 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006618 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006619 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006620 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006621 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006622 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006623 }
6624
Damien Lespiau241bfc32013-09-25 16:45:37 +01006625 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006626 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006627 }
Chris Wilson89749352010-09-12 18:25:19 +01006628
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006629 /*
6630 * Pipe horizontal size must be even in:
6631 * - DVO ganged mode
6632 * - LVDS dual channel mode
6633 * - Double wide pipe
6634 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006635 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006636 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6637 pipe_config->pipe_src_w &= ~1;
6638
Damien Lespiau8693a822013-05-03 18:48:11 +01006639 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6640 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006641 */
6642 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6643 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006644 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006645
Damien Lespiauf5adf942013-06-24 18:29:34 +01006646 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006647 hsw_compute_ips_config(crtc, pipe_config);
6648
Daniel Vetter877d48d2013-04-19 11:24:43 +02006649 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006650 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006651
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006652 /* FIXME: remove below call once atomic mode set is place and all crtc
6653 * related checks called from atomic_crtc_check function */
6654 ret = 0;
6655 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6656 crtc, pipe_config->base.state);
6657 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6658
6659 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006660}
6661
Ville Syrjälä1652d192015-03-31 14:12:01 +03006662static int skylake_get_display_clock_speed(struct drm_device *dev)
6663{
6664 struct drm_i915_private *dev_priv = to_i915(dev);
6665 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6666 uint32_t cdctl = I915_READ(CDCLK_CTL);
6667 uint32_t linkrate;
6668
6669 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6670 WARN(1, "LCPLL1 not enabled\n");
6671 return 24000; /* 24MHz is the cd freq with NSSC ref */
6672 }
6673
6674 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6675 return 540000;
6676
6677 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006678 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006679
Damien Lespiau71cd8422015-04-30 16:39:17 +01006680 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6681 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006682 /* vco 8640 */
6683 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6684 case CDCLK_FREQ_450_432:
6685 return 432000;
6686 case CDCLK_FREQ_337_308:
6687 return 308570;
6688 case CDCLK_FREQ_675_617:
6689 return 617140;
6690 default:
6691 WARN(1, "Unknown cd freq selection\n");
6692 }
6693 } else {
6694 /* vco 8100 */
6695 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6696 case CDCLK_FREQ_450_432:
6697 return 450000;
6698 case CDCLK_FREQ_337_308:
6699 return 337500;
6700 case CDCLK_FREQ_675_617:
6701 return 675000;
6702 default:
6703 WARN(1, "Unknown cd freq selection\n");
6704 }
6705 }
6706
6707 /* error case, do as if DPLL0 isn't enabled */
6708 return 24000;
6709}
6710
6711static int broadwell_get_display_clock_speed(struct drm_device *dev)
6712{
6713 struct drm_i915_private *dev_priv = dev->dev_private;
6714 uint32_t lcpll = I915_READ(LCPLL_CTL);
6715 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6716
6717 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6718 return 800000;
6719 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6720 return 450000;
6721 else if (freq == LCPLL_CLK_FREQ_450)
6722 return 450000;
6723 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6724 return 540000;
6725 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6726 return 337500;
6727 else
6728 return 675000;
6729}
6730
6731static int haswell_get_display_clock_speed(struct drm_device *dev)
6732{
6733 struct drm_i915_private *dev_priv = dev->dev_private;
6734 uint32_t lcpll = I915_READ(LCPLL_CTL);
6735 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6736
6737 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6738 return 800000;
6739 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6740 return 450000;
6741 else if (freq == LCPLL_CLK_FREQ_450)
6742 return 450000;
6743 else if (IS_HSW_ULT(dev))
6744 return 337500;
6745 else
6746 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006747}
6748
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006749static int valleyview_get_display_clock_speed(struct drm_device *dev)
6750{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006751 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006752 u32 val;
6753 int divider;
6754
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006755 if (dev_priv->hpll_freq == 0)
6756 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6757
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006758 mutex_lock(&dev_priv->dpio_lock);
6759 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
6760 mutex_unlock(&dev_priv->dpio_lock);
6761
6762 divider = val & DISPLAY_FREQUENCY_VALUES;
6763
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006764 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6765 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6766 "cdclk change in progress\n");
6767
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006768 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006769}
6770
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006771static int ilk_get_display_clock_speed(struct drm_device *dev)
6772{
6773 return 450000;
6774}
6775
Jesse Barnese70236a2009-09-21 10:42:27 -07006776static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006777{
Jesse Barnese70236a2009-09-21 10:42:27 -07006778 return 400000;
6779}
Jesse Barnes79e53942008-11-07 14:24:08 -08006780
Jesse Barnese70236a2009-09-21 10:42:27 -07006781static int i915_get_display_clock_speed(struct drm_device *dev)
6782{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006783 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006784}
Jesse Barnes79e53942008-11-07 14:24:08 -08006785
Jesse Barnese70236a2009-09-21 10:42:27 -07006786static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6787{
6788 return 200000;
6789}
Jesse Barnes79e53942008-11-07 14:24:08 -08006790
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006791static int pnv_get_display_clock_speed(struct drm_device *dev)
6792{
6793 u16 gcfgc = 0;
6794
6795 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6796
6797 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6798 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006799 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006800 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006801 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006802 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006803 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006804 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6805 return 200000;
6806 default:
6807 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6808 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006809 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006810 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006811 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006812 }
6813}
6814
Jesse Barnese70236a2009-09-21 10:42:27 -07006815static int i915gm_get_display_clock_speed(struct drm_device *dev)
6816{
6817 u16 gcfgc = 0;
6818
6819 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6820
6821 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006822 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006823 else {
6824 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6825 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006826 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006827 default:
6828 case GC_DISPLAY_CLOCK_190_200_MHZ:
6829 return 190000;
6830 }
6831 }
6832}
Jesse Barnes79e53942008-11-07 14:24:08 -08006833
Jesse Barnese70236a2009-09-21 10:42:27 -07006834static int i865_get_display_clock_speed(struct drm_device *dev)
6835{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006836 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006837}
6838
6839static int i855_get_display_clock_speed(struct drm_device *dev)
6840{
6841 u16 hpllcc = 0;
6842 /* Assume that the hardware is in the high speed state. This
6843 * should be the default.
6844 */
6845 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6846 case GC_CLOCK_133_200:
6847 case GC_CLOCK_100_200:
6848 return 200000;
6849 case GC_CLOCK_166_250:
6850 return 250000;
6851 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006852 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006853 }
6854
6855 /* Shouldn't happen */
6856 return 0;
6857}
6858
6859static int i830_get_display_clock_speed(struct drm_device *dev)
6860{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006861 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006862}
6863
Zhenyu Wang2c072452009-06-05 15:38:42 +08006864static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006865intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006866{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006867 while (*num > DATA_LINK_M_N_MASK ||
6868 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006869 *num >>= 1;
6870 *den >>= 1;
6871 }
6872}
6873
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006874static void compute_m_n(unsigned int m, unsigned int n,
6875 uint32_t *ret_m, uint32_t *ret_n)
6876{
6877 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6878 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6879 intel_reduce_m_n_ratio(ret_m, ret_n);
6880}
6881
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006882void
6883intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6884 int pixel_clock, int link_clock,
6885 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006886{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006887 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006888
6889 compute_m_n(bits_per_pixel * pixel_clock,
6890 link_clock * nlanes * 8,
6891 &m_n->gmch_m, &m_n->gmch_n);
6892
6893 compute_m_n(pixel_clock, link_clock,
6894 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006895}
6896
Chris Wilsona7615032011-01-12 17:04:08 +00006897static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6898{
Jani Nikulad330a952014-01-21 11:24:25 +02006899 if (i915.panel_use_ssc >= 0)
6900 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006901 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006902 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006903}
6904
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006905static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6906 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006907{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006908 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006909 struct drm_i915_private *dev_priv = dev->dev_private;
6910 int refclk;
6911
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006912 WARN_ON(!crtc_state->base.state);
6913
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006914 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006915 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006916 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006917 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006918 refclk = dev_priv->vbt.lvds_ssc_freq;
6919 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006920 } else if (!IS_GEN2(dev)) {
6921 refclk = 96000;
6922 } else {
6923 refclk = 48000;
6924 }
6925
6926 return refclk;
6927}
6928
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006929static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006930{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006931 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006932}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006933
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006934static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6935{
6936 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006937}
6938
Daniel Vetterf47709a2013-03-28 10:42:02 +01006939static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006940 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006941 intel_clock_t *reduced_clock)
6942{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006943 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006944 u32 fp, fp2 = 0;
6945
6946 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006947 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006948 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006949 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006950 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006951 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006952 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006953 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006954 }
6955
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006956 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006957
Daniel Vetterf47709a2013-03-28 10:42:02 +01006958 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006959 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006960 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006961 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006962 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006963 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006964 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006965 }
6966}
6967
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006968static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6969 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006970{
6971 u32 reg_val;
6972
6973 /*
6974 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6975 * and set it to a reasonable value instead.
6976 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006977 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006978 reg_val &= 0xffffff00;
6979 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006980 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006981
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006982 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006983 reg_val &= 0x8cffffff;
6984 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006985 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006986
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006987 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006988 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006989 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006990
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006991 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006992 reg_val &= 0x00ffffff;
6993 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006994 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006995}
6996
Daniel Vetterb5518422013-05-03 11:49:48 +02006997static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
6998 struct intel_link_m_n *m_n)
6999{
7000 struct drm_device *dev = crtc->base.dev;
7001 struct drm_i915_private *dev_priv = dev->dev_private;
7002 int pipe = crtc->pipe;
7003
Daniel Vettere3b95f12013-05-03 11:49:49 +02007004 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7005 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7006 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7007 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007008}
7009
7010static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007011 struct intel_link_m_n *m_n,
7012 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007013{
7014 struct drm_device *dev = crtc->base.dev;
7015 struct drm_i915_private *dev_priv = dev->dev_private;
7016 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007017 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007018
7019 if (INTEL_INFO(dev)->gen >= 5) {
7020 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7021 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7022 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7023 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007024 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7025 * for gen < 8) and if DRRS is supported (to make sure the
7026 * registers are not unnecessarily accessed).
7027 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307028 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007029 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007030 I915_WRITE(PIPE_DATA_M2(transcoder),
7031 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7032 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7033 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7034 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7035 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007036 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007037 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7038 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7039 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7040 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007041 }
7042}
7043
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307044void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007045{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307046 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7047
7048 if (m_n == M1_N1) {
7049 dp_m_n = &crtc->config->dp_m_n;
7050 dp_m2_n2 = &crtc->config->dp_m2_n2;
7051 } else if (m_n == M2_N2) {
7052
7053 /*
7054 * M2_N2 registers are not supported. Hence m2_n2 divider value
7055 * needs to be programmed into M1_N1.
7056 */
7057 dp_m_n = &crtc->config->dp_m2_n2;
7058 } else {
7059 DRM_ERROR("Unsupported divider value\n");
7060 return;
7061 }
7062
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007063 if (crtc->config->has_pch_encoder)
7064 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007065 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307066 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007067}
7068
Ville Syrjäläd288f652014-10-28 13:20:22 +02007069static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007070 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007071{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007072 u32 dpll, dpll_md;
7073
7074 /*
7075 * Enable DPIO clock input. We should never disable the reference
7076 * clock for pipe B, since VGA hotplug / manual detection depends
7077 * on it.
7078 */
7079 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7080 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7081 /* We should never disable this, set it here for state tracking */
7082 if (crtc->pipe == PIPE_B)
7083 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7084 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007085 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007086
Ville Syrjäläd288f652014-10-28 13:20:22 +02007087 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007088 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007089 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007090}
7091
Ville Syrjäläd288f652014-10-28 13:20:22 +02007092static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007093 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007094{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007095 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007096 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007097 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007098 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007099 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007100 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007101
Daniel Vetter09153002012-12-12 14:06:44 +01007102 mutex_lock(&dev_priv->dpio_lock);
7103
Ville Syrjäläd288f652014-10-28 13:20:22 +02007104 bestn = pipe_config->dpll.n;
7105 bestm1 = pipe_config->dpll.m1;
7106 bestm2 = pipe_config->dpll.m2;
7107 bestp1 = pipe_config->dpll.p1;
7108 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007109
Jesse Barnes89b667f2013-04-18 14:51:36 -07007110 /* See eDP HDMI DPIO driver vbios notes doc */
7111
7112 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007113 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007114 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007115
7116 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007117 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007118
7119 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007120 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007121 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007122 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007123
7124 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007125 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007126
7127 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007128 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7129 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7130 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007131 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007132
7133 /*
7134 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7135 * but we don't support that).
7136 * Note: don't use the DAC post divider as it seems unstable.
7137 */
7138 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007139 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007140
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007141 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007142 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007143
Jesse Barnes89b667f2013-04-18 14:51:36 -07007144 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007145 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007146 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7147 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007148 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007149 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007150 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007151 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007152 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007153
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007154 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007155 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007156 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007157 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007158 0x0df40000);
7159 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007160 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007161 0x0df70000);
7162 } else { /* HDMI or VGA */
7163 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007164 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007165 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007166 0x0df70000);
7167 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007168 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007169 0x0df40000);
7170 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007171
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007172 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007173 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007174 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7175 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007176 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007177 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007178
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007179 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Daniel Vetter09153002012-12-12 14:06:44 +01007180 mutex_unlock(&dev_priv->dpio_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007181}
7182
Ville Syrjäläd288f652014-10-28 13:20:22 +02007183static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007184 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007185{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007186 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007187 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7188 DPLL_VCO_ENABLE;
7189 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007190 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007191
Ville Syrjäläd288f652014-10-28 13:20:22 +02007192 pipe_config->dpll_hw_state.dpll_md =
7193 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007194}
7195
Ville Syrjäläd288f652014-10-28 13:20:22 +02007196static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007197 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007198{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007199 struct drm_device *dev = crtc->base.dev;
7200 struct drm_i915_private *dev_priv = dev->dev_private;
7201 int pipe = crtc->pipe;
7202 int dpll_reg = DPLL(crtc->pipe);
7203 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307204 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007205 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307206 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307207 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007208
Ville Syrjäläd288f652014-10-28 13:20:22 +02007209 bestn = pipe_config->dpll.n;
7210 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7211 bestm1 = pipe_config->dpll.m1;
7212 bestm2 = pipe_config->dpll.m2 >> 22;
7213 bestp1 = pipe_config->dpll.p1;
7214 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307215 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307216 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307217 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007218
7219 /*
7220 * Enable Refclk and SSC
7221 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007222 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007223 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007224
7225 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007226
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007227 /* p1 and p2 divider */
7228 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7229 5 << DPIO_CHV_S1_DIV_SHIFT |
7230 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7231 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7232 1 << DPIO_CHV_K_DIV_SHIFT);
7233
7234 /* Feedback post-divider - m2 */
7235 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7236
7237 /* Feedback refclk divider - n and m1 */
7238 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7239 DPIO_CHV_M1_DIV_BY_2 |
7240 1 << DPIO_CHV_N_DIV_SHIFT);
7241
7242 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307243 if (bestm2_frac)
7244 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007245
7246 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307247 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7248 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7249 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7250 if (bestm2_frac)
7251 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7252 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007253
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307254 /* Program digital lock detect threshold */
7255 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7256 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7257 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7258 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7259 if (!bestm2_frac)
7260 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7261 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7262
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007263 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307264 if (vco == 5400000) {
7265 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7266 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7267 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7268 tribuf_calcntr = 0x9;
7269 } else if (vco <= 6200000) {
7270 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7271 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7272 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7273 tribuf_calcntr = 0x9;
7274 } else if (vco <= 6480000) {
7275 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7276 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7277 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7278 tribuf_calcntr = 0x8;
7279 } else {
7280 /* Not supported. Apply the same limits as in the max case */
7281 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7282 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7283 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7284 tribuf_calcntr = 0;
7285 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007286 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7287
Ville Syrjälä968040b2015-03-11 22:52:08 +02007288 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307289 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7290 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7291 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7292
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007293 /* AFC Recal */
7294 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7295 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7296 DPIO_AFC_RECAL);
7297
7298 mutex_unlock(&dev_priv->dpio_lock);
7299}
7300
Ville Syrjäläd288f652014-10-28 13:20:22 +02007301/**
7302 * vlv_force_pll_on - forcibly enable just the PLL
7303 * @dev_priv: i915 private structure
7304 * @pipe: pipe PLL to enable
7305 * @dpll: PLL configuration
7306 *
7307 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7308 * in cases where we need the PLL enabled even when @pipe is not going to
7309 * be enabled.
7310 */
7311void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7312 const struct dpll *dpll)
7313{
7314 struct intel_crtc *crtc =
7315 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007316 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007317 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007318 .pixel_multiplier = 1,
7319 .dpll = *dpll,
7320 };
7321
7322 if (IS_CHERRYVIEW(dev)) {
7323 chv_update_pll(crtc, &pipe_config);
7324 chv_prepare_pll(crtc, &pipe_config);
7325 chv_enable_pll(crtc, &pipe_config);
7326 } else {
7327 vlv_update_pll(crtc, &pipe_config);
7328 vlv_prepare_pll(crtc, &pipe_config);
7329 vlv_enable_pll(crtc, &pipe_config);
7330 }
7331}
7332
7333/**
7334 * vlv_force_pll_off - forcibly disable just the PLL
7335 * @dev_priv: i915 private structure
7336 * @pipe: pipe PLL to disable
7337 *
7338 * Disable the PLL for @pipe. To be used in cases where we need
7339 * the PLL enabled even when @pipe is not going to be enabled.
7340 */
7341void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7342{
7343 if (IS_CHERRYVIEW(dev))
7344 chv_disable_pll(to_i915(dev), pipe);
7345 else
7346 vlv_disable_pll(to_i915(dev), pipe);
7347}
7348
Daniel Vetterf47709a2013-03-28 10:42:02 +01007349static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007350 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007351 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007352 int num_connectors)
7353{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007354 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007355 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007356 u32 dpll;
7357 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007358 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007359
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007360 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307361
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007362 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7363 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007364
7365 dpll = DPLL_VGA_MODE_DIS;
7366
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007367 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007368 dpll |= DPLLB_MODE_LVDS;
7369 else
7370 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007371
Daniel Vetteref1b4602013-06-01 17:17:04 +02007372 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007373 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007374 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007375 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007376
7377 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007378 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007379
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007380 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007381 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007382
7383 /* compute bitmask from p1 value */
7384 if (IS_PINEVIEW(dev))
7385 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7386 else {
7387 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7388 if (IS_G4X(dev) && reduced_clock)
7389 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7390 }
7391 switch (clock->p2) {
7392 case 5:
7393 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7394 break;
7395 case 7:
7396 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7397 break;
7398 case 10:
7399 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7400 break;
7401 case 14:
7402 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7403 break;
7404 }
7405 if (INTEL_INFO(dev)->gen >= 4)
7406 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7407
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007408 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007409 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007410 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007411 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7412 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7413 else
7414 dpll |= PLL_REF_INPUT_DREFCLK;
7415
7416 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007417 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007418
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007419 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007420 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007421 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007422 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007423 }
7424}
7425
Daniel Vetterf47709a2013-03-28 10:42:02 +01007426static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007427 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007428 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007429 int num_connectors)
7430{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007431 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007432 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007433 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007434 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007435
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007436 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307437
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007438 dpll = DPLL_VGA_MODE_DIS;
7439
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007440 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007441 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7442 } else {
7443 if (clock->p1 == 2)
7444 dpll |= PLL_P1_DIVIDE_BY_TWO;
7445 else
7446 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7447 if (clock->p2 == 4)
7448 dpll |= PLL_P2_DIVIDE_BY_4;
7449 }
7450
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007451 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007452 dpll |= DPLL_DVO_2X_MODE;
7453
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007454 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007455 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7456 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7457 else
7458 dpll |= PLL_REF_INPUT_DREFCLK;
7459
7460 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007461 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007462}
7463
Daniel Vetter8a654f32013-06-01 17:16:22 +02007464static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007465{
7466 struct drm_device *dev = intel_crtc->base.dev;
7467 struct drm_i915_private *dev_priv = dev->dev_private;
7468 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007469 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007470 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007471 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007472 uint32_t crtc_vtotal, crtc_vblank_end;
7473 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007474
7475 /* We need to be careful not to changed the adjusted mode, for otherwise
7476 * the hw state checker will get angry at the mismatch. */
7477 crtc_vtotal = adjusted_mode->crtc_vtotal;
7478 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007479
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007480 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007481 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007482 crtc_vtotal -= 1;
7483 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007484
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007485 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007486 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7487 else
7488 vsyncshift = adjusted_mode->crtc_hsync_start -
7489 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007490 if (vsyncshift < 0)
7491 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007492 }
7493
7494 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007495 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007496
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007497 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007498 (adjusted_mode->crtc_hdisplay - 1) |
7499 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007500 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007501 (adjusted_mode->crtc_hblank_start - 1) |
7502 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007503 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007504 (adjusted_mode->crtc_hsync_start - 1) |
7505 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7506
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007507 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007508 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007509 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007510 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007511 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007512 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007513 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007514 (adjusted_mode->crtc_vsync_start - 1) |
7515 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7516
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007517 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7518 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7519 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7520 * bits. */
7521 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7522 (pipe == PIPE_B || pipe == PIPE_C))
7523 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7524
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007525 /* pipesrc controls the size that is scaled from, which should
7526 * always be the user's requested size.
7527 */
7528 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007529 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7530 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007531}
7532
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007533static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007534 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007535{
7536 struct drm_device *dev = crtc->base.dev;
7537 struct drm_i915_private *dev_priv = dev->dev_private;
7538 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7539 uint32_t tmp;
7540
7541 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007542 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7543 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007544 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007545 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7546 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007547 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007548 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7549 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007550
7551 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007552 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7553 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007554 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007555 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7556 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007557 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007558 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7559 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007560
7561 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007562 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7563 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7564 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007565 }
7566
7567 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007568 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7569 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7570
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007571 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7572 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007573}
7574
Daniel Vetterf6a83282014-02-11 15:28:57 -08007575void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007576 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007577{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007578 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7579 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7580 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7581 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007582
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007583 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7584 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7585 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7586 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007587
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007588 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007589
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007590 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7591 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007592}
7593
Daniel Vetter84b046f2013-02-19 18:48:54 +01007594static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7595{
7596 struct drm_device *dev = intel_crtc->base.dev;
7597 struct drm_i915_private *dev_priv = dev->dev_private;
7598 uint32_t pipeconf;
7599
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007600 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007601
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007602 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7603 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7604 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007605
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007606 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007607 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007608
Daniel Vetterff9ce462013-04-24 14:57:17 +02007609 /* only g4x and later have fancy bpc/dither controls */
7610 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007611 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007612 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007613 pipeconf |= PIPECONF_DITHER_EN |
7614 PIPECONF_DITHER_TYPE_SP;
7615
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007616 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007617 case 18:
7618 pipeconf |= PIPECONF_6BPC;
7619 break;
7620 case 24:
7621 pipeconf |= PIPECONF_8BPC;
7622 break;
7623 case 30:
7624 pipeconf |= PIPECONF_10BPC;
7625 break;
7626 default:
7627 /* Case prevented by intel_choose_pipe_bpp_dither. */
7628 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007629 }
7630 }
7631
7632 if (HAS_PIPE_CXSR(dev)) {
7633 if (intel_crtc->lowfreq_avail) {
7634 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7635 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7636 } else {
7637 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007638 }
7639 }
7640
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007641 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007642 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007643 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007644 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7645 else
7646 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7647 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007648 pipeconf |= PIPECONF_PROGRESSIVE;
7649
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007650 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007651 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007652
Daniel Vetter84b046f2013-02-19 18:48:54 +01007653 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7654 POSTING_READ(PIPECONF(intel_crtc->pipe));
7655}
7656
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007657static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7658 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007659{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007660 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007661 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007662 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007663 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007664 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007665 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007666 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007667 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007668 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007669 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007670 struct drm_connector_state *connector_state;
7671 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007672
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007673 memset(&crtc_state->dpll_hw_state, 0,
7674 sizeof(crtc_state->dpll_hw_state));
7675
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007676 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007677 if (connector_state->crtc != &crtc->base)
7678 continue;
7679
7680 encoder = to_intel_encoder(connector_state->best_encoder);
7681
Chris Wilson5eddb702010-09-11 13:48:45 +01007682 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007683 case INTEL_OUTPUT_LVDS:
7684 is_lvds = true;
7685 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007686 case INTEL_OUTPUT_DSI:
7687 is_dsi = true;
7688 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007689 default:
7690 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007691 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007692
Eric Anholtc751ce42010-03-25 11:48:48 -07007693 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007694 }
7695
Jani Nikulaf2335332013-09-13 11:03:09 +03007696 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007697 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007698
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007699 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007700 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007701
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007702 /*
7703 * Returns a set of divisors for the desired target clock with
7704 * the given refclk, or FALSE. The returned values represent
7705 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7706 * 2) / p1 / p2.
7707 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007708 limit = intel_limit(crtc_state, refclk);
7709 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007710 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007711 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007712 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007713 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7714 return -EINVAL;
7715 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007716
Jani Nikulaf2335332013-09-13 11:03:09 +03007717 if (is_lvds && dev_priv->lvds_downclock_avail) {
7718 /*
7719 * Ensure we match the reduced clock's P to the target
7720 * clock. If the clocks don't match, we can't switch
7721 * the display clock by using the FP0/FP1. In such case
7722 * we will disable the LVDS downclock feature.
7723 */
7724 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007725 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007726 dev_priv->lvds_downclock,
7727 refclk, &clock,
7728 &reduced_clock);
7729 }
7730 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007731 crtc_state->dpll.n = clock.n;
7732 crtc_state->dpll.m1 = clock.m1;
7733 crtc_state->dpll.m2 = clock.m2;
7734 crtc_state->dpll.p1 = clock.p1;
7735 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007736 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007737
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007738 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007739 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307740 has_reduced_clock ? &reduced_clock : NULL,
7741 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007742 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007743 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007744 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007745 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007746 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007747 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007748 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007749 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007750 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007751
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007752 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007753}
7754
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007755static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007756 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007757{
7758 struct drm_device *dev = crtc->base.dev;
7759 struct drm_i915_private *dev_priv = dev->dev_private;
7760 uint32_t tmp;
7761
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007762 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7763 return;
7764
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007765 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007766 if (!(tmp & PFIT_ENABLE))
7767 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007768
Daniel Vetter06922822013-07-11 13:35:40 +02007769 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007770 if (INTEL_INFO(dev)->gen < 4) {
7771 if (crtc->pipe != PIPE_B)
7772 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007773 } else {
7774 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7775 return;
7776 }
7777
Daniel Vetter06922822013-07-11 13:35:40 +02007778 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007779 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7780 if (INTEL_INFO(dev)->gen < 5)
7781 pipe_config->gmch_pfit.lvds_border_bits =
7782 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7783}
7784
Jesse Barnesacbec812013-09-20 11:29:32 -07007785static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007786 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007787{
7788 struct drm_device *dev = crtc->base.dev;
7789 struct drm_i915_private *dev_priv = dev->dev_private;
7790 int pipe = pipe_config->cpu_transcoder;
7791 intel_clock_t clock;
7792 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007793 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007794
Shobhit Kumarf573de52014-07-30 20:32:37 +05307795 /* In case of MIPI DPLL will not even be used */
7796 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7797 return;
7798
Jesse Barnesacbec812013-09-20 11:29:32 -07007799 mutex_lock(&dev_priv->dpio_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007800 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Jesse Barnesacbec812013-09-20 11:29:32 -07007801 mutex_unlock(&dev_priv->dpio_lock);
7802
7803 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7804 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7805 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7806 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7807 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7808
Ville Syrjäläf6466282013-10-14 14:50:31 +03007809 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007810
Ville Syrjäläf6466282013-10-14 14:50:31 +03007811 /* clock.dot is the fast clock */
7812 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007813}
7814
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007815static void
7816i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7817 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007818{
7819 struct drm_device *dev = crtc->base.dev;
7820 struct drm_i915_private *dev_priv = dev->dev_private;
7821 u32 val, base, offset;
7822 int pipe = crtc->pipe, plane = crtc->plane;
7823 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007824 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007825 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007826 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007827
Damien Lespiau42a7b082015-02-05 19:35:13 +00007828 val = I915_READ(DSPCNTR(plane));
7829 if (!(val & DISPLAY_PLANE_ENABLE))
7830 return;
7831
Damien Lespiaud9806c92015-01-21 14:07:19 +00007832 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007833 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007834 DRM_DEBUG_KMS("failed to alloc fb\n");
7835 return;
7836 }
7837
Damien Lespiau1b842c82015-01-21 13:50:54 +00007838 fb = &intel_fb->base;
7839
Daniel Vetter18c52472015-02-10 17:16:09 +00007840 if (INTEL_INFO(dev)->gen >= 4) {
7841 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007842 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007843 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7844 }
7845 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007846
7847 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007848 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007849 fb->pixel_format = fourcc;
7850 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007851
7852 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007853 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007854 offset = I915_READ(DSPTILEOFF(plane));
7855 else
7856 offset = I915_READ(DSPLINOFF(plane));
7857 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7858 } else {
7859 base = I915_READ(DSPADDR(plane));
7860 }
7861 plane_config->base = base;
7862
7863 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007864 fb->width = ((val >> 16) & 0xfff) + 1;
7865 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007866
7867 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007868 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007869
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007870 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007871 fb->pixel_format,
7872 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007873
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007874 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007875
Damien Lespiau2844a922015-01-20 12:51:48 +00007876 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7877 pipe_name(pipe), plane, fb->width, fb->height,
7878 fb->bits_per_pixel, base, fb->pitches[0],
7879 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007880
Damien Lespiau2d140302015-02-05 17:22:18 +00007881 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007882}
7883
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007884static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007885 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007886{
7887 struct drm_device *dev = crtc->base.dev;
7888 struct drm_i915_private *dev_priv = dev->dev_private;
7889 int pipe = pipe_config->cpu_transcoder;
7890 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7891 intel_clock_t clock;
7892 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7893 int refclk = 100000;
7894
7895 mutex_lock(&dev_priv->dpio_lock);
7896 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7897 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7898 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7899 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
7900 mutex_unlock(&dev_priv->dpio_lock);
7901
7902 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7903 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7904 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7905 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7906 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7907
7908 chv_clock(refclk, &clock);
7909
7910 /* clock.dot is the fast clock */
7911 pipe_config->port_clock = clock.dot / 5;
7912}
7913
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007914static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007915 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007916{
7917 struct drm_device *dev = crtc->base.dev;
7918 struct drm_i915_private *dev_priv = dev->dev_private;
7919 uint32_t tmp;
7920
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007921 if (!intel_display_power_is_enabled(dev_priv,
7922 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007923 return false;
7924
Daniel Vettere143a212013-07-04 12:01:15 +02007925 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007926 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007927
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007928 tmp = I915_READ(PIPECONF(crtc->pipe));
7929 if (!(tmp & PIPECONF_ENABLE))
7930 return false;
7931
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007932 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7933 switch (tmp & PIPECONF_BPC_MASK) {
7934 case PIPECONF_6BPC:
7935 pipe_config->pipe_bpp = 18;
7936 break;
7937 case PIPECONF_8BPC:
7938 pipe_config->pipe_bpp = 24;
7939 break;
7940 case PIPECONF_10BPC:
7941 pipe_config->pipe_bpp = 30;
7942 break;
7943 default:
7944 break;
7945 }
7946 }
7947
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007948 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7949 pipe_config->limited_color_range = true;
7950
Ville Syrjälä282740f2013-09-04 18:30:03 +03007951 if (INTEL_INFO(dev)->gen < 4)
7952 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7953
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007954 intel_get_pipe_timings(crtc, pipe_config);
7955
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007956 i9xx_get_pfit_config(crtc, pipe_config);
7957
Daniel Vetter6c49f242013-06-06 12:45:25 +02007958 if (INTEL_INFO(dev)->gen >= 4) {
7959 tmp = I915_READ(DPLL_MD(crtc->pipe));
7960 pipe_config->pixel_multiplier =
7961 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7962 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007963 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007964 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7965 tmp = I915_READ(DPLL(crtc->pipe));
7966 pipe_config->pixel_multiplier =
7967 ((tmp & SDVO_MULTIPLIER_MASK)
7968 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7969 } else {
7970 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7971 * port and will be fixed up in the encoder->get_config
7972 * function. */
7973 pipe_config->pixel_multiplier = 1;
7974 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007975 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7976 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007977 /*
7978 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7979 * on 830. Filter it out here so that we don't
7980 * report errors due to that.
7981 */
7982 if (IS_I830(dev))
7983 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7984
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007985 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7986 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007987 } else {
7988 /* Mask out read-only status bits. */
7989 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7990 DPLL_PORTC_READY_MASK |
7991 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007992 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007993
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007994 if (IS_CHERRYVIEW(dev))
7995 chv_crtc_clock_get(crtc, pipe_config);
7996 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07007997 vlv_crtc_clock_get(crtc, pipe_config);
7998 else
7999 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008000
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008001 return true;
8002}
8003
Paulo Zanonidde86e22012-12-01 12:04:25 -02008004static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008005{
8006 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008007 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008008 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008009 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008010 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008011 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008012 bool has_ck505 = false;
8013 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008014
8015 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008016 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008017 switch (encoder->type) {
8018 case INTEL_OUTPUT_LVDS:
8019 has_panel = true;
8020 has_lvds = true;
8021 break;
8022 case INTEL_OUTPUT_EDP:
8023 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008024 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008025 has_cpu_edp = true;
8026 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008027 default:
8028 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008029 }
8030 }
8031
Keith Packard99eb6a02011-09-26 14:29:12 -07008032 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008033 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008034 can_ssc = has_ck505;
8035 } else {
8036 has_ck505 = false;
8037 can_ssc = true;
8038 }
8039
Imre Deak2de69052013-05-08 13:14:04 +03008040 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8041 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008042
8043 /* Ironlake: try to setup display ref clock before DPLL
8044 * enabling. This is only under driver's control after
8045 * PCH B stepping, previous chipset stepping should be
8046 * ignoring this setting.
8047 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008048 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008049
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008050 /* As we must carefully and slowly disable/enable each source in turn,
8051 * compute the final state we want first and check if we need to
8052 * make any changes at all.
8053 */
8054 final = val;
8055 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008056 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008057 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008058 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008059 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8060
8061 final &= ~DREF_SSC_SOURCE_MASK;
8062 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8063 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008064
Keith Packard199e5d72011-09-22 12:01:57 -07008065 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008066 final |= DREF_SSC_SOURCE_ENABLE;
8067
8068 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8069 final |= DREF_SSC1_ENABLE;
8070
8071 if (has_cpu_edp) {
8072 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8073 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8074 else
8075 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8076 } else
8077 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8078 } else {
8079 final |= DREF_SSC_SOURCE_DISABLE;
8080 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8081 }
8082
8083 if (final == val)
8084 return;
8085
8086 /* Always enable nonspread source */
8087 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8088
8089 if (has_ck505)
8090 val |= DREF_NONSPREAD_CK505_ENABLE;
8091 else
8092 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8093
8094 if (has_panel) {
8095 val &= ~DREF_SSC_SOURCE_MASK;
8096 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008097
Keith Packard199e5d72011-09-22 12:01:57 -07008098 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008099 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008100 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008101 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008102 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008103 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008104
8105 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008106 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008107 POSTING_READ(PCH_DREF_CONTROL);
8108 udelay(200);
8109
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008110 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008111
8112 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008113 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008114 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008115 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008116 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008117 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008118 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008119 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008120 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008121
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008122 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008123 POSTING_READ(PCH_DREF_CONTROL);
8124 udelay(200);
8125 } else {
8126 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8127
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008128 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008129
8130 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008131 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008132
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008133 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008134 POSTING_READ(PCH_DREF_CONTROL);
8135 udelay(200);
8136
8137 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008138 val &= ~DREF_SSC_SOURCE_MASK;
8139 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008140
8141 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008142 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008143
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008144 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008145 POSTING_READ(PCH_DREF_CONTROL);
8146 udelay(200);
8147 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008148
8149 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008150}
8151
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008152static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008153{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008154 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008155
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008156 tmp = I915_READ(SOUTH_CHICKEN2);
8157 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8158 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008159
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008160 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8161 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8162 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008163
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008164 tmp = I915_READ(SOUTH_CHICKEN2);
8165 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8166 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008167
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008168 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8169 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8170 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008171}
8172
8173/* WaMPhyProgramming:hsw */
8174static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8175{
8176 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008177
8178 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8179 tmp &= ~(0xFF << 24);
8180 tmp |= (0x12 << 24);
8181 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8182
Paulo Zanonidde86e22012-12-01 12:04:25 -02008183 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8184 tmp |= (1 << 11);
8185 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8186
8187 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8188 tmp |= (1 << 11);
8189 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8190
Paulo Zanonidde86e22012-12-01 12:04:25 -02008191 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8192 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8193 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8194
8195 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8196 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8197 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8198
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008199 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8200 tmp &= ~(7 << 13);
8201 tmp |= (5 << 13);
8202 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008203
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008204 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8205 tmp &= ~(7 << 13);
8206 tmp |= (5 << 13);
8207 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008208
8209 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8210 tmp &= ~0xFF;
8211 tmp |= 0x1C;
8212 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8213
8214 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8215 tmp &= ~0xFF;
8216 tmp |= 0x1C;
8217 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8218
8219 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8220 tmp &= ~(0xFF << 16);
8221 tmp |= (0x1C << 16);
8222 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8223
8224 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8225 tmp &= ~(0xFF << 16);
8226 tmp |= (0x1C << 16);
8227 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8228
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008229 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8230 tmp |= (1 << 27);
8231 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008232
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008233 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8234 tmp |= (1 << 27);
8235 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008236
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008237 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8238 tmp &= ~(0xF << 28);
8239 tmp |= (4 << 28);
8240 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008241
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008242 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8243 tmp &= ~(0xF << 28);
8244 tmp |= (4 << 28);
8245 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008246}
8247
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008248/* Implements 3 different sequences from BSpec chapter "Display iCLK
8249 * Programming" based on the parameters passed:
8250 * - Sequence to enable CLKOUT_DP
8251 * - Sequence to enable CLKOUT_DP without spread
8252 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8253 */
8254static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8255 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008256{
8257 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008258 uint32_t reg, tmp;
8259
8260 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8261 with_spread = true;
8262 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8263 with_fdi, "LP PCH doesn't have FDI\n"))
8264 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008265
8266 mutex_lock(&dev_priv->dpio_lock);
8267
8268 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8269 tmp &= ~SBI_SSCCTL_DISABLE;
8270 tmp |= SBI_SSCCTL_PATHALT;
8271 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8272
8273 udelay(24);
8274
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008275 if (with_spread) {
8276 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8277 tmp &= ~SBI_SSCCTL_PATHALT;
8278 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008279
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008280 if (with_fdi) {
8281 lpt_reset_fdi_mphy(dev_priv);
8282 lpt_program_fdi_mphy(dev_priv);
8283 }
8284 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008285
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008286 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8287 SBI_GEN0 : SBI_DBUFF0;
8288 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8289 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8290 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008291
8292 mutex_unlock(&dev_priv->dpio_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008293}
8294
Paulo Zanoni47701c32013-07-23 11:19:25 -03008295/* Sequence to disable CLKOUT_DP */
8296static void lpt_disable_clkout_dp(struct drm_device *dev)
8297{
8298 struct drm_i915_private *dev_priv = dev->dev_private;
8299 uint32_t reg, tmp;
8300
8301 mutex_lock(&dev_priv->dpio_lock);
8302
8303 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8304 SBI_GEN0 : SBI_DBUFF0;
8305 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8306 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8307 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8308
8309 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8310 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8311 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8312 tmp |= SBI_SSCCTL_PATHALT;
8313 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8314 udelay(32);
8315 }
8316 tmp |= SBI_SSCCTL_DISABLE;
8317 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8318 }
8319
8320 mutex_unlock(&dev_priv->dpio_lock);
8321}
8322
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008323static void lpt_init_pch_refclk(struct drm_device *dev)
8324{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008325 struct intel_encoder *encoder;
8326 bool has_vga = false;
8327
Damien Lespiaub2784e12014-08-05 11:29:37 +01008328 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008329 switch (encoder->type) {
8330 case INTEL_OUTPUT_ANALOG:
8331 has_vga = true;
8332 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008333 default:
8334 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008335 }
8336 }
8337
Paulo Zanoni47701c32013-07-23 11:19:25 -03008338 if (has_vga)
8339 lpt_enable_clkout_dp(dev, true, true);
8340 else
8341 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008342}
8343
Paulo Zanonidde86e22012-12-01 12:04:25 -02008344/*
8345 * Initialize reference clocks when the driver loads
8346 */
8347void intel_init_pch_refclk(struct drm_device *dev)
8348{
8349 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8350 ironlake_init_pch_refclk(dev);
8351 else if (HAS_PCH_LPT(dev))
8352 lpt_init_pch_refclk(dev);
8353}
8354
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008355static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008356{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008357 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008358 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008359 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008360 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008361 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008362 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008363 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008364 bool is_lvds = false;
8365
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008366 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008367 if (connector_state->crtc != crtc_state->base.crtc)
8368 continue;
8369
8370 encoder = to_intel_encoder(connector_state->best_encoder);
8371
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008372 switch (encoder->type) {
8373 case INTEL_OUTPUT_LVDS:
8374 is_lvds = true;
8375 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008376 default:
8377 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008378 }
8379 num_connectors++;
8380 }
8381
8382 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008383 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008384 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008385 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008386 }
8387
8388 return 120000;
8389}
8390
Daniel Vetter6ff93602013-04-19 11:24:36 +02008391static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008392{
8393 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8394 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8395 int pipe = intel_crtc->pipe;
8396 uint32_t val;
8397
Daniel Vetter78114072013-06-13 00:54:57 +02008398 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008399
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008400 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008401 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008402 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008403 break;
8404 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008405 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008406 break;
8407 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008408 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008409 break;
8410 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008411 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008412 break;
8413 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008414 /* Case prevented by intel_choose_pipe_bpp_dither. */
8415 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008416 }
8417
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008418 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008419 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8420
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008421 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008422 val |= PIPECONF_INTERLACED_ILK;
8423 else
8424 val |= PIPECONF_PROGRESSIVE;
8425
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008426 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008427 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008428
Paulo Zanonic8203562012-09-12 10:06:29 -03008429 I915_WRITE(PIPECONF(pipe), val);
8430 POSTING_READ(PIPECONF(pipe));
8431}
8432
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008433/*
8434 * Set up the pipe CSC unit.
8435 *
8436 * Currently only full range RGB to limited range RGB conversion
8437 * is supported, but eventually this should handle various
8438 * RGB<->YCbCr scenarios as well.
8439 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008440static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008441{
8442 struct drm_device *dev = crtc->dev;
8443 struct drm_i915_private *dev_priv = dev->dev_private;
8444 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8445 int pipe = intel_crtc->pipe;
8446 uint16_t coeff = 0x7800; /* 1.0 */
8447
8448 /*
8449 * TODO: Check what kind of values actually come out of the pipe
8450 * with these coeff/postoff values and adjust to get the best
8451 * accuracy. Perhaps we even need to take the bpc value into
8452 * consideration.
8453 */
8454
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008455 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008456 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8457
8458 /*
8459 * GY/GU and RY/RU should be the other way around according
8460 * to BSpec, but reality doesn't agree. Just set them up in
8461 * a way that results in the correct picture.
8462 */
8463 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8464 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8465
8466 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8467 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8468
8469 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8470 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8471
8472 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8473 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8474 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8475
8476 if (INTEL_INFO(dev)->gen > 6) {
8477 uint16_t postoff = 0;
8478
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008479 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008480 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008481
8482 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8483 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8484 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8485
8486 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8487 } else {
8488 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8489
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008490 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008491 mode |= CSC_BLACK_SCREEN_OFFSET;
8492
8493 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8494 }
8495}
8496
Daniel Vetter6ff93602013-04-19 11:24:36 +02008497static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008498{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008499 struct drm_device *dev = crtc->dev;
8500 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008501 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008502 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008503 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008504 uint32_t val;
8505
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008506 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008507
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008508 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008509 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8510
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008511 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008512 val |= PIPECONF_INTERLACED_ILK;
8513 else
8514 val |= PIPECONF_PROGRESSIVE;
8515
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008516 I915_WRITE(PIPECONF(cpu_transcoder), val);
8517 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008518
8519 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8520 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008521
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308522 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008523 val = 0;
8524
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008525 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008526 case 18:
8527 val |= PIPEMISC_DITHER_6_BPC;
8528 break;
8529 case 24:
8530 val |= PIPEMISC_DITHER_8_BPC;
8531 break;
8532 case 30:
8533 val |= PIPEMISC_DITHER_10_BPC;
8534 break;
8535 case 36:
8536 val |= PIPEMISC_DITHER_12_BPC;
8537 break;
8538 default:
8539 /* Case prevented by pipe_config_set_bpp. */
8540 BUG();
8541 }
8542
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008543 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008544 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8545
8546 I915_WRITE(PIPEMISC(pipe), val);
8547 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008548}
8549
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008550static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008551 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008552 intel_clock_t *clock,
8553 bool *has_reduced_clock,
8554 intel_clock_t *reduced_clock)
8555{
8556 struct drm_device *dev = crtc->dev;
8557 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008558 int refclk;
8559 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008560 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008561
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008562 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008563
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008564 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008565
8566 /*
8567 * Returns a set of divisors for the desired target clock with the given
8568 * refclk, or FALSE. The returned values represent the clock equation:
8569 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8570 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008571 limit = intel_limit(crtc_state, refclk);
8572 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008573 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008574 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008575 if (!ret)
8576 return false;
8577
8578 if (is_lvds && dev_priv->lvds_downclock_avail) {
8579 /*
8580 * Ensure we match the reduced clock's P to the target clock.
8581 * If the clocks don't match, we can't switch the display clock
8582 * by using the FP0/FP1. In such case we will disable the LVDS
8583 * downclock feature.
8584 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008585 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008586 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008587 dev_priv->lvds_downclock,
8588 refclk, clock,
8589 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008590 }
8591
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008592 return true;
8593}
8594
Paulo Zanonid4b19312012-11-29 11:29:32 -02008595int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8596{
8597 /*
8598 * Account for spread spectrum to avoid
8599 * oversubscribing the link. Max center spread
8600 * is 2.5%; use 5% for safety's sake.
8601 */
8602 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008603 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008604}
8605
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008606static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008607{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008608 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008609}
8610
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008611static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008612 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008613 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008614 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008615{
8616 struct drm_crtc *crtc = &intel_crtc->base;
8617 struct drm_device *dev = crtc->dev;
8618 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008619 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008620 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008621 struct drm_connector_state *connector_state;
8622 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008623 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008624 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008625 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008626
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008627 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008628 if (connector_state->crtc != crtc_state->base.crtc)
8629 continue;
8630
8631 encoder = to_intel_encoder(connector_state->best_encoder);
8632
8633 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008634 case INTEL_OUTPUT_LVDS:
8635 is_lvds = true;
8636 break;
8637 case INTEL_OUTPUT_SDVO:
8638 case INTEL_OUTPUT_HDMI:
8639 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008640 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008641 default:
8642 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008643 }
8644
8645 num_connectors++;
8646 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008647
Chris Wilsonc1858122010-12-03 21:35:48 +00008648 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008649 factor = 21;
8650 if (is_lvds) {
8651 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008652 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008653 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008654 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008655 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008656 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008657
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008658 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008659 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008660
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008661 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8662 *fp2 |= FP_CB_TUNE;
8663
Chris Wilson5eddb702010-09-11 13:48:45 +01008664 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008665
Eric Anholta07d6782011-03-30 13:01:08 -07008666 if (is_lvds)
8667 dpll |= DPLLB_MODE_LVDS;
8668 else
8669 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008670
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008671 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008672 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008673
8674 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008675 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008676 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008677 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008678
Eric Anholta07d6782011-03-30 13:01:08 -07008679 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008680 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008681 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008682 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008683
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008684 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008685 case 5:
8686 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8687 break;
8688 case 7:
8689 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8690 break;
8691 case 10:
8692 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8693 break;
8694 case 14:
8695 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8696 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008697 }
8698
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008699 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008700 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008701 else
8702 dpll |= PLL_REF_INPUT_DREFCLK;
8703
Daniel Vetter959e16d2013-06-05 13:34:21 +02008704 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008705}
8706
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008707static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8708 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008709{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008710 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008711 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008712 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008713 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008714 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008715 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008716
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008717 memset(&crtc_state->dpll_hw_state, 0,
8718 sizeof(crtc_state->dpll_hw_state));
8719
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008720 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008721
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008722 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8723 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8724
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008725 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008726 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008727 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008728 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8729 return -EINVAL;
8730 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008731 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008732 if (!crtc_state->clock_set) {
8733 crtc_state->dpll.n = clock.n;
8734 crtc_state->dpll.m1 = clock.m1;
8735 crtc_state->dpll.m2 = clock.m2;
8736 crtc_state->dpll.p1 = clock.p1;
8737 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008738 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008739
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008740 /* 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 +02008741 if (crtc_state->has_pch_encoder) {
8742 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008743 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008744 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008745
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008746 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008747 &fp, &reduced_clock,
8748 has_reduced_clock ? &fp2 : NULL);
8749
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008750 crtc_state->dpll_hw_state.dpll = dpll;
8751 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008752 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008753 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008754 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008755 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008756
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008757 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008758 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008759 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008760 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008761 return -EINVAL;
8762 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008763 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008764
Rodrigo Viviab585de2015-03-24 12:40:09 -07008765 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008766 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008767 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008768 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008769
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008770 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008771}
8772
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008773static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8774 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008775{
8776 struct drm_device *dev = crtc->base.dev;
8777 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008778 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008779
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008780 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8781 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8782 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8783 & ~TU_SIZE_MASK;
8784 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8785 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8786 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8787}
8788
8789static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8790 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008791 struct intel_link_m_n *m_n,
8792 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008793{
8794 struct drm_device *dev = crtc->base.dev;
8795 struct drm_i915_private *dev_priv = dev->dev_private;
8796 enum pipe pipe = crtc->pipe;
8797
8798 if (INTEL_INFO(dev)->gen >= 5) {
8799 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8800 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8801 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8802 & ~TU_SIZE_MASK;
8803 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8804 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8805 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008806 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8807 * gen < 8) and if DRRS is supported (to make sure the
8808 * registers are not unnecessarily read).
8809 */
8810 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008811 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008812 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8813 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8814 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8815 & ~TU_SIZE_MASK;
8816 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8817 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8818 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8819 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008820 } else {
8821 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8822 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8823 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8824 & ~TU_SIZE_MASK;
8825 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8826 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8827 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8828 }
8829}
8830
8831void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008832 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008833{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008834 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008835 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8836 else
8837 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008838 &pipe_config->dp_m_n,
8839 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008840}
8841
Daniel Vetter72419202013-04-04 13:28:53 +02008842static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008843 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008844{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008845 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008846 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008847}
8848
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008849static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008850 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008851{
8852 struct drm_device *dev = crtc->base.dev;
8853 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008854 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8855 uint32_t ps_ctrl = 0;
8856 int id = -1;
8857 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008858
Chandra Kondurua1b22782015-04-07 15:28:45 -07008859 /* find scaler attached to this pipe */
8860 for (i = 0; i < crtc->num_scalers; i++) {
8861 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8862 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8863 id = i;
8864 pipe_config->pch_pfit.enabled = true;
8865 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8866 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8867 break;
8868 }
8869 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008870
Chandra Kondurua1b22782015-04-07 15:28:45 -07008871 scaler_state->scaler_id = id;
8872 if (id >= 0) {
8873 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8874 } else {
8875 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008876 }
8877}
8878
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008879static void
8880skylake_get_initial_plane_config(struct intel_crtc *crtc,
8881 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008882{
8883 struct drm_device *dev = crtc->base.dev;
8884 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008885 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008886 int pipe = crtc->pipe;
8887 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008888 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008889 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008890 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008891
Damien Lespiaud9806c92015-01-21 14:07:19 +00008892 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008893 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008894 DRM_DEBUG_KMS("failed to alloc fb\n");
8895 return;
8896 }
8897
Damien Lespiau1b842c82015-01-21 13:50:54 +00008898 fb = &intel_fb->base;
8899
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008900 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008901 if (!(val & PLANE_CTL_ENABLE))
8902 goto error;
8903
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008904 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8905 fourcc = skl_format_to_fourcc(pixel_format,
8906 val & PLANE_CTL_ORDER_RGBX,
8907 val & PLANE_CTL_ALPHA_MASK);
8908 fb->pixel_format = fourcc;
8909 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8910
Damien Lespiau40f46282015-02-27 11:15:21 +00008911 tiling = val & PLANE_CTL_TILED_MASK;
8912 switch (tiling) {
8913 case PLANE_CTL_TILED_LINEAR:
8914 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8915 break;
8916 case PLANE_CTL_TILED_X:
8917 plane_config->tiling = I915_TILING_X;
8918 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8919 break;
8920 case PLANE_CTL_TILED_Y:
8921 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8922 break;
8923 case PLANE_CTL_TILED_YF:
8924 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8925 break;
8926 default:
8927 MISSING_CASE(tiling);
8928 goto error;
8929 }
8930
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008931 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8932 plane_config->base = base;
8933
8934 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8935
8936 val = I915_READ(PLANE_SIZE(pipe, 0));
8937 fb->height = ((val >> 16) & 0xfff) + 1;
8938 fb->width = ((val >> 0) & 0x1fff) + 1;
8939
8940 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008941 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8942 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008943 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8944
8945 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008946 fb->pixel_format,
8947 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008948
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008949 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008950
8951 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8952 pipe_name(pipe), fb->width, fb->height,
8953 fb->bits_per_pixel, base, fb->pitches[0],
8954 plane_config->size);
8955
Damien Lespiau2d140302015-02-05 17:22:18 +00008956 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008957 return;
8958
8959error:
8960 kfree(fb);
8961}
8962
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008963static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008964 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008965{
8966 struct drm_device *dev = crtc->base.dev;
8967 struct drm_i915_private *dev_priv = dev->dev_private;
8968 uint32_t tmp;
8969
8970 tmp = I915_READ(PF_CTL(crtc->pipe));
8971
8972 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008973 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008974 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8975 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008976
8977 /* We currently do not free assignements of panel fitters on
8978 * ivb/hsw (since we don't use the higher upscaling modes which
8979 * differentiates them) so just WARN about this case for now. */
8980 if (IS_GEN7(dev)) {
8981 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8982 PF_PIPE_SEL_IVB(crtc->pipe));
8983 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008984 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008985}
8986
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008987static void
8988ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8989 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008990{
8991 struct drm_device *dev = crtc->base.dev;
8992 struct drm_i915_private *dev_priv = dev->dev_private;
8993 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008994 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008995 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008996 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008997 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008998 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008999
Damien Lespiau42a7b082015-02-05 19:35:13 +00009000 val = I915_READ(DSPCNTR(pipe));
9001 if (!(val & DISPLAY_PLANE_ENABLE))
9002 return;
9003
Damien Lespiaud9806c92015-01-21 14:07:19 +00009004 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009005 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009006 DRM_DEBUG_KMS("failed to alloc fb\n");
9007 return;
9008 }
9009
Damien Lespiau1b842c82015-01-21 13:50:54 +00009010 fb = &intel_fb->base;
9011
Daniel Vetter18c52472015-02-10 17:16:09 +00009012 if (INTEL_INFO(dev)->gen >= 4) {
9013 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009014 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009015 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9016 }
9017 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009018
9019 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009020 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009021 fb->pixel_format = fourcc;
9022 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009023
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009024 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009025 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009026 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009027 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009028 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009029 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009030 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009031 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009032 }
9033 plane_config->base = base;
9034
9035 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009036 fb->width = ((val >> 16) & 0xfff) + 1;
9037 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009038
9039 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009040 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009041
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009042 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009043 fb->pixel_format,
9044 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009045
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009046 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009047
Damien Lespiau2844a922015-01-20 12:51:48 +00009048 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9049 pipe_name(pipe), fb->width, fb->height,
9050 fb->bits_per_pixel, base, fb->pitches[0],
9051 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009052
Damien Lespiau2d140302015-02-05 17:22:18 +00009053 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009054}
9055
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009056static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009057 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009058{
9059 struct drm_device *dev = crtc->base.dev;
9060 struct drm_i915_private *dev_priv = dev->dev_private;
9061 uint32_t tmp;
9062
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009063 if (!intel_display_power_is_enabled(dev_priv,
9064 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009065 return false;
9066
Daniel Vettere143a212013-07-04 12:01:15 +02009067 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009068 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009069
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009070 tmp = I915_READ(PIPECONF(crtc->pipe));
9071 if (!(tmp & PIPECONF_ENABLE))
9072 return false;
9073
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009074 switch (tmp & PIPECONF_BPC_MASK) {
9075 case PIPECONF_6BPC:
9076 pipe_config->pipe_bpp = 18;
9077 break;
9078 case PIPECONF_8BPC:
9079 pipe_config->pipe_bpp = 24;
9080 break;
9081 case PIPECONF_10BPC:
9082 pipe_config->pipe_bpp = 30;
9083 break;
9084 case PIPECONF_12BPC:
9085 pipe_config->pipe_bpp = 36;
9086 break;
9087 default:
9088 break;
9089 }
9090
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009091 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9092 pipe_config->limited_color_range = true;
9093
Daniel Vetterab9412b2013-05-03 11:49:46 +02009094 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009095 struct intel_shared_dpll *pll;
9096
Daniel Vetter88adfff2013-03-28 10:42:01 +01009097 pipe_config->has_pch_encoder = true;
9098
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009099 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9100 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9101 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009102
9103 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009104
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009105 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009106 pipe_config->shared_dpll =
9107 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009108 } else {
9109 tmp = I915_READ(PCH_DPLL_SEL);
9110 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9111 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9112 else
9113 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9114 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009115
9116 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9117
9118 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9119 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009120
9121 tmp = pipe_config->dpll_hw_state.dpll;
9122 pipe_config->pixel_multiplier =
9123 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9124 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009125
9126 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009127 } else {
9128 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009129 }
9130
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009131 intel_get_pipe_timings(crtc, pipe_config);
9132
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009133 ironlake_get_pfit_config(crtc, pipe_config);
9134
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009135 return true;
9136}
9137
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009138static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9139{
9140 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009141 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009142
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009143 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009144 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009145 pipe_name(crtc->pipe));
9146
Rob Clarke2c719b2014-12-15 13:56:32 -05009147 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9148 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9149 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9150 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9151 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9152 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009153 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009154 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009155 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009156 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009157 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009158 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009159 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009160 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009161 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009162
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009163 /*
9164 * In theory we can still leave IRQs enabled, as long as only the HPD
9165 * interrupts remain enabled. We used to check for that, but since it's
9166 * gen-specific and since we only disable LCPLL after we fully disable
9167 * the interrupts, the check below should be enough.
9168 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009169 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009170}
9171
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009172static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9173{
9174 struct drm_device *dev = dev_priv->dev;
9175
9176 if (IS_HASWELL(dev))
9177 return I915_READ(D_COMP_HSW);
9178 else
9179 return I915_READ(D_COMP_BDW);
9180}
9181
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009182static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9183{
9184 struct drm_device *dev = dev_priv->dev;
9185
9186 if (IS_HASWELL(dev)) {
9187 mutex_lock(&dev_priv->rps.hw_lock);
9188 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9189 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009190 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009191 mutex_unlock(&dev_priv->rps.hw_lock);
9192 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009193 I915_WRITE(D_COMP_BDW, val);
9194 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009195 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009196}
9197
9198/*
9199 * This function implements pieces of two sequences from BSpec:
9200 * - Sequence for display software to disable LCPLL
9201 * - Sequence for display software to allow package C8+
9202 * The steps implemented here are just the steps that actually touch the LCPLL
9203 * register. Callers should take care of disabling all the display engine
9204 * functions, doing the mode unset, fixing interrupts, etc.
9205 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009206static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9207 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009208{
9209 uint32_t val;
9210
9211 assert_can_disable_lcpll(dev_priv);
9212
9213 val = I915_READ(LCPLL_CTL);
9214
9215 if (switch_to_fclk) {
9216 val |= LCPLL_CD_SOURCE_FCLK;
9217 I915_WRITE(LCPLL_CTL, val);
9218
9219 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9220 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9221 DRM_ERROR("Switching to FCLK failed\n");
9222
9223 val = I915_READ(LCPLL_CTL);
9224 }
9225
9226 val |= LCPLL_PLL_DISABLE;
9227 I915_WRITE(LCPLL_CTL, val);
9228 POSTING_READ(LCPLL_CTL);
9229
9230 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9231 DRM_ERROR("LCPLL still locked\n");
9232
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009233 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009234 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009235 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009236 ndelay(100);
9237
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009238 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9239 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009240 DRM_ERROR("D_COMP RCOMP still in progress\n");
9241
9242 if (allow_power_down) {
9243 val = I915_READ(LCPLL_CTL);
9244 val |= LCPLL_POWER_DOWN_ALLOW;
9245 I915_WRITE(LCPLL_CTL, val);
9246 POSTING_READ(LCPLL_CTL);
9247 }
9248}
9249
9250/*
9251 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9252 * source.
9253 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009254static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009255{
9256 uint32_t val;
9257
9258 val = I915_READ(LCPLL_CTL);
9259
9260 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9261 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9262 return;
9263
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009264 /*
9265 * Make sure we're not on PC8 state before disabling PC8, otherwise
9266 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009267 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009268 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009269
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009270 if (val & LCPLL_POWER_DOWN_ALLOW) {
9271 val &= ~LCPLL_POWER_DOWN_ALLOW;
9272 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009273 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009274 }
9275
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009276 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009277 val |= D_COMP_COMP_FORCE;
9278 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009279 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009280
9281 val = I915_READ(LCPLL_CTL);
9282 val &= ~LCPLL_PLL_DISABLE;
9283 I915_WRITE(LCPLL_CTL, val);
9284
9285 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9286 DRM_ERROR("LCPLL not locked yet\n");
9287
9288 if (val & LCPLL_CD_SOURCE_FCLK) {
9289 val = I915_READ(LCPLL_CTL);
9290 val &= ~LCPLL_CD_SOURCE_FCLK;
9291 I915_WRITE(LCPLL_CTL, val);
9292
9293 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9294 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9295 DRM_ERROR("Switching back to LCPLL failed\n");
9296 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009297
Mika Kuoppala59bad942015-01-16 11:34:40 +02009298 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009299}
9300
Paulo Zanoni765dab672014-03-07 20:08:18 -03009301/*
9302 * Package states C8 and deeper are really deep PC states that can only be
9303 * reached when all the devices on the system allow it, so even if the graphics
9304 * device allows PC8+, it doesn't mean the system will actually get to these
9305 * states. Our driver only allows PC8+ when going into runtime PM.
9306 *
9307 * The requirements for PC8+ are that all the outputs are disabled, the power
9308 * well is disabled and most interrupts are disabled, and these are also
9309 * requirements for runtime PM. When these conditions are met, we manually do
9310 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9311 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9312 * hang the machine.
9313 *
9314 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9315 * the state of some registers, so when we come back from PC8+ we need to
9316 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9317 * need to take care of the registers kept by RC6. Notice that this happens even
9318 * if we don't put the device in PCI D3 state (which is what currently happens
9319 * because of the runtime PM support).
9320 *
9321 * For more, read "Display Sequences for Package C8" on the hardware
9322 * documentation.
9323 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009324void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009325{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009326 struct drm_device *dev = dev_priv->dev;
9327 uint32_t val;
9328
Paulo Zanonic67a4702013-08-19 13:18:09 -03009329 DRM_DEBUG_KMS("Enabling package C8+\n");
9330
Paulo Zanonic67a4702013-08-19 13:18:09 -03009331 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9332 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9333 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9334 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9335 }
9336
9337 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009338 hsw_disable_lcpll(dev_priv, true, true);
9339}
9340
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009341void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009342{
9343 struct drm_device *dev = dev_priv->dev;
9344 uint32_t val;
9345
Paulo Zanonic67a4702013-08-19 13:18:09 -03009346 DRM_DEBUG_KMS("Disabling package C8+\n");
9347
9348 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009349 lpt_init_pch_refclk(dev);
9350
9351 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9352 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9353 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9354 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9355 }
9356
9357 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009358}
9359
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009360static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309361{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009362 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309363 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009364 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309365 int req_cdclk;
9366
9367 /* see the comment in valleyview_modeset_global_resources */
9368 if (WARN_ON(max_pixclk < 0))
9369 return;
9370
9371 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9372
9373 if (req_cdclk != dev_priv->cdclk_freq)
9374 broxton_set_cdclk(dev, req_cdclk);
9375}
9376
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009377static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9378 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009379{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009380 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009381 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009382
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009383 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009384
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009385 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009386}
9387
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309388static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9389 enum port port,
9390 struct intel_crtc_state *pipe_config)
9391{
9392 switch (port) {
9393 case PORT_A:
9394 pipe_config->ddi_pll_sel = SKL_DPLL0;
9395 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9396 break;
9397 case PORT_B:
9398 pipe_config->ddi_pll_sel = SKL_DPLL1;
9399 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9400 break;
9401 case PORT_C:
9402 pipe_config->ddi_pll_sel = SKL_DPLL2;
9403 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9404 break;
9405 default:
9406 DRM_ERROR("Incorrect port type\n");
9407 }
9408}
9409
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009410static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9411 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009412 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009413{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009414 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009415
9416 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9417 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9418
9419 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009420 case SKL_DPLL0:
9421 /*
9422 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9423 * of the shared DPLL framework and thus needs to be read out
9424 * separately
9425 */
9426 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9427 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9428 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009429 case SKL_DPLL1:
9430 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9431 break;
9432 case SKL_DPLL2:
9433 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9434 break;
9435 case SKL_DPLL3:
9436 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9437 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009438 }
9439}
9440
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009441static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9442 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009443 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009444{
9445 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9446
9447 switch (pipe_config->ddi_pll_sel) {
9448 case PORT_CLK_SEL_WRPLL1:
9449 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9450 break;
9451 case PORT_CLK_SEL_WRPLL2:
9452 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9453 break;
9454 }
9455}
9456
Daniel Vetter26804af2014-06-25 22:01:55 +03009457static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009458 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009459{
9460 struct drm_device *dev = crtc->base.dev;
9461 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009462 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009463 enum port port;
9464 uint32_t tmp;
9465
9466 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9467
9468 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9469
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009470 if (IS_SKYLAKE(dev))
9471 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309472 else if (IS_BROXTON(dev))
9473 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009474 else
9475 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009476
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009477 if (pipe_config->shared_dpll >= 0) {
9478 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9479
9480 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9481 &pipe_config->dpll_hw_state));
9482 }
9483
Daniel Vetter26804af2014-06-25 22:01:55 +03009484 /*
9485 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9486 * DDI E. So just check whether this pipe is wired to DDI E and whether
9487 * the PCH transcoder is on.
9488 */
Damien Lespiauca370452013-12-03 13:56:24 +00009489 if (INTEL_INFO(dev)->gen < 9 &&
9490 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009491 pipe_config->has_pch_encoder = true;
9492
9493 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9494 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9495 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9496
9497 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9498 }
9499}
9500
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009501static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009502 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009503{
9504 struct drm_device *dev = crtc->base.dev;
9505 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009506 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009507 uint32_t tmp;
9508
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009509 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009510 POWER_DOMAIN_PIPE(crtc->pipe)))
9511 return false;
9512
Daniel Vettere143a212013-07-04 12:01:15 +02009513 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009514 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9515
Daniel Vettereccb1402013-05-22 00:50:22 +02009516 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9517 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9518 enum pipe trans_edp_pipe;
9519 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9520 default:
9521 WARN(1, "unknown pipe linked to edp transcoder\n");
9522 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9523 case TRANS_DDI_EDP_INPUT_A_ON:
9524 trans_edp_pipe = PIPE_A;
9525 break;
9526 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9527 trans_edp_pipe = PIPE_B;
9528 break;
9529 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9530 trans_edp_pipe = PIPE_C;
9531 break;
9532 }
9533
9534 if (trans_edp_pipe == crtc->pipe)
9535 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9536 }
9537
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009538 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009539 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009540 return false;
9541
Daniel Vettereccb1402013-05-22 00:50:22 +02009542 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009543 if (!(tmp & PIPECONF_ENABLE))
9544 return false;
9545
Daniel Vetter26804af2014-06-25 22:01:55 +03009546 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009547
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009548 intel_get_pipe_timings(crtc, pipe_config);
9549
Chandra Kondurua1b22782015-04-07 15:28:45 -07009550 if (INTEL_INFO(dev)->gen >= 9) {
9551 skl_init_scalers(dev, crtc, pipe_config);
9552 }
9553
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009554 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ceda2015-05-11 14:35:47 -07009555
9556 if (INTEL_INFO(dev)->gen >= 9) {
9557 pipe_config->scaler_state.scaler_id = -1;
9558 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9559 }
9560
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009561 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009562 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009563 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009564 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009565 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009566 else
9567 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009568 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009569
Jesse Barnese59150d2014-01-07 13:30:45 -08009570 if (IS_HASWELL(dev))
9571 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9572 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009573
Clint Taylorebb69c92014-09-30 10:30:22 -07009574 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9575 pipe_config->pixel_multiplier =
9576 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9577 } else {
9578 pipe_config->pixel_multiplier = 1;
9579 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009580
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009581 return true;
9582}
9583
Chris Wilson560b85b2010-08-07 11:01:38 +01009584static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9585{
9586 struct drm_device *dev = crtc->dev;
9587 struct drm_i915_private *dev_priv = dev->dev_private;
9588 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009589 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009590
Ville Syrjälädc41c152014-08-13 11:57:05 +03009591 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009592 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9593 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009594 unsigned int stride = roundup_pow_of_two(width) * 4;
9595
9596 switch (stride) {
9597 default:
9598 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9599 width, stride);
9600 stride = 256;
9601 /* fallthrough */
9602 case 256:
9603 case 512:
9604 case 1024:
9605 case 2048:
9606 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009607 }
9608
Ville Syrjälädc41c152014-08-13 11:57:05 +03009609 cntl |= CURSOR_ENABLE |
9610 CURSOR_GAMMA_ENABLE |
9611 CURSOR_FORMAT_ARGB |
9612 CURSOR_STRIDE(stride);
9613
9614 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009615 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009616
Ville Syrjälädc41c152014-08-13 11:57:05 +03009617 if (intel_crtc->cursor_cntl != 0 &&
9618 (intel_crtc->cursor_base != base ||
9619 intel_crtc->cursor_size != size ||
9620 intel_crtc->cursor_cntl != cntl)) {
9621 /* On these chipsets we can only modify the base/size/stride
9622 * whilst the cursor is disabled.
9623 */
9624 I915_WRITE(_CURACNTR, 0);
9625 POSTING_READ(_CURACNTR);
9626 intel_crtc->cursor_cntl = 0;
9627 }
9628
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009629 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009630 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009631 intel_crtc->cursor_base = base;
9632 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009633
9634 if (intel_crtc->cursor_size != size) {
9635 I915_WRITE(CURSIZE, size);
9636 intel_crtc->cursor_size = size;
9637 }
9638
Chris Wilson4b0e3332014-05-30 16:35:26 +03009639 if (intel_crtc->cursor_cntl != cntl) {
9640 I915_WRITE(_CURACNTR, cntl);
9641 POSTING_READ(_CURACNTR);
9642 intel_crtc->cursor_cntl = cntl;
9643 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009644}
9645
9646static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9647{
9648 struct drm_device *dev = crtc->dev;
9649 struct drm_i915_private *dev_priv = dev->dev_private;
9650 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9651 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009652 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009653
Chris Wilson4b0e3332014-05-30 16:35:26 +03009654 cntl = 0;
9655 if (base) {
9656 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009657 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309658 case 64:
9659 cntl |= CURSOR_MODE_64_ARGB_AX;
9660 break;
9661 case 128:
9662 cntl |= CURSOR_MODE_128_ARGB_AX;
9663 break;
9664 case 256:
9665 cntl |= CURSOR_MODE_256_ARGB_AX;
9666 break;
9667 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009668 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309669 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009670 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009671 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009672
9673 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9674 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009675 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009676
Matt Roper8e7d6882015-01-21 16:35:41 -08009677 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009678 cntl |= CURSOR_ROTATE_180;
9679
Chris Wilson4b0e3332014-05-30 16:35:26 +03009680 if (intel_crtc->cursor_cntl != cntl) {
9681 I915_WRITE(CURCNTR(pipe), cntl);
9682 POSTING_READ(CURCNTR(pipe));
9683 intel_crtc->cursor_cntl = cntl;
9684 }
9685
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009686 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009687 I915_WRITE(CURBASE(pipe), base);
9688 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009689
9690 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009691}
9692
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009693/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009694static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9695 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009696{
9697 struct drm_device *dev = crtc->dev;
9698 struct drm_i915_private *dev_priv = dev->dev_private;
9699 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9700 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009701 int x = crtc->cursor_x;
9702 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009703 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009704
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009705 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009706 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009707
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009708 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009709 base = 0;
9710
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009711 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009712 base = 0;
9713
9714 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009715 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009716 base = 0;
9717
9718 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9719 x = -x;
9720 }
9721 pos |= x << CURSOR_X_SHIFT;
9722
9723 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009724 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009725 base = 0;
9726
9727 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9728 y = -y;
9729 }
9730 pos |= y << CURSOR_Y_SHIFT;
9731
Chris Wilson4b0e3332014-05-30 16:35:26 +03009732 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009733 return;
9734
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009735 I915_WRITE(CURPOS(pipe), pos);
9736
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009737 /* ILK+ do this automagically */
9738 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009739 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009740 base += (intel_crtc->base.cursor->state->crtc_h *
9741 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009742 }
9743
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009744 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009745 i845_update_cursor(crtc, base);
9746 else
9747 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009748}
9749
Ville Syrjälädc41c152014-08-13 11:57:05 +03009750static bool cursor_size_ok(struct drm_device *dev,
9751 uint32_t width, uint32_t height)
9752{
9753 if (width == 0 || height == 0)
9754 return false;
9755
9756 /*
9757 * 845g/865g are special in that they are only limited by
9758 * the width of their cursors, the height is arbitrary up to
9759 * the precision of the register. Everything else requires
9760 * square cursors, limited to a few power-of-two sizes.
9761 */
9762 if (IS_845G(dev) || IS_I865G(dev)) {
9763 if ((width & 63) != 0)
9764 return false;
9765
9766 if (width > (IS_845G(dev) ? 64 : 512))
9767 return false;
9768
9769 if (height > 1023)
9770 return false;
9771 } else {
9772 switch (width | height) {
9773 case 256:
9774 case 128:
9775 if (IS_GEN2(dev))
9776 return false;
9777 case 64:
9778 break;
9779 default:
9780 return false;
9781 }
9782 }
9783
9784 return true;
9785}
9786
Jesse Barnes79e53942008-11-07 14:24:08 -08009787static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009788 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009789{
James Simmons72034252010-08-03 01:33:19 +01009790 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009792
James Simmons72034252010-08-03 01:33:19 +01009793 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009794 intel_crtc->lut_r[i] = red[i] >> 8;
9795 intel_crtc->lut_g[i] = green[i] >> 8;
9796 intel_crtc->lut_b[i] = blue[i] >> 8;
9797 }
9798
9799 intel_crtc_load_lut(crtc);
9800}
9801
Jesse Barnes79e53942008-11-07 14:24:08 -08009802/* VESA 640x480x72Hz mode to set on the pipe */
9803static struct drm_display_mode load_detect_mode = {
9804 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9805 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9806};
9807
Daniel Vettera8bb6812014-02-10 18:00:39 +01009808struct drm_framebuffer *
9809__intel_framebuffer_create(struct drm_device *dev,
9810 struct drm_mode_fb_cmd2 *mode_cmd,
9811 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009812{
9813 struct intel_framebuffer *intel_fb;
9814 int ret;
9815
9816 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9817 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009818 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009819 return ERR_PTR(-ENOMEM);
9820 }
9821
9822 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009823 if (ret)
9824 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009825
9826 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009827err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009828 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009829 kfree(intel_fb);
9830
9831 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009832}
9833
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009834static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009835intel_framebuffer_create(struct drm_device *dev,
9836 struct drm_mode_fb_cmd2 *mode_cmd,
9837 struct drm_i915_gem_object *obj)
9838{
9839 struct drm_framebuffer *fb;
9840 int ret;
9841
9842 ret = i915_mutex_lock_interruptible(dev);
9843 if (ret)
9844 return ERR_PTR(ret);
9845 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9846 mutex_unlock(&dev->struct_mutex);
9847
9848 return fb;
9849}
9850
Chris Wilsond2dff872011-04-19 08:36:26 +01009851static u32
9852intel_framebuffer_pitch_for_width(int width, int bpp)
9853{
9854 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9855 return ALIGN(pitch, 64);
9856}
9857
9858static u32
9859intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9860{
9861 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009862 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009863}
9864
9865static struct drm_framebuffer *
9866intel_framebuffer_create_for_mode(struct drm_device *dev,
9867 struct drm_display_mode *mode,
9868 int depth, int bpp)
9869{
9870 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009871 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009872
9873 obj = i915_gem_alloc_object(dev,
9874 intel_framebuffer_size_for_mode(mode, bpp));
9875 if (obj == NULL)
9876 return ERR_PTR(-ENOMEM);
9877
9878 mode_cmd.width = mode->hdisplay;
9879 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009880 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9881 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009882 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009883
9884 return intel_framebuffer_create(dev, &mode_cmd, obj);
9885}
9886
9887static struct drm_framebuffer *
9888mode_fits_in_fbdev(struct drm_device *dev,
9889 struct drm_display_mode *mode)
9890{
Daniel Vetter4520f532013-10-09 09:18:51 +02009891#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009892 struct drm_i915_private *dev_priv = dev->dev_private;
9893 struct drm_i915_gem_object *obj;
9894 struct drm_framebuffer *fb;
9895
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009896 if (!dev_priv->fbdev)
9897 return NULL;
9898
9899 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009900 return NULL;
9901
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009902 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009903 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009904
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009905 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009906 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9907 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009908 return NULL;
9909
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009910 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009911 return NULL;
9912
9913 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009914#else
9915 return NULL;
9916#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009917}
9918
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009919static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9920 struct drm_crtc *crtc,
9921 struct drm_display_mode *mode,
9922 struct drm_framebuffer *fb,
9923 int x, int y)
9924{
9925 struct drm_plane_state *plane_state;
9926 int hdisplay, vdisplay;
9927 int ret;
9928
9929 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9930 if (IS_ERR(plane_state))
9931 return PTR_ERR(plane_state);
9932
9933 if (mode)
9934 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9935 else
9936 hdisplay = vdisplay = 0;
9937
9938 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9939 if (ret)
9940 return ret;
9941 drm_atomic_set_fb_for_plane(plane_state, fb);
9942 plane_state->crtc_x = 0;
9943 plane_state->crtc_y = 0;
9944 plane_state->crtc_w = hdisplay;
9945 plane_state->crtc_h = vdisplay;
9946 plane_state->src_x = x << 16;
9947 plane_state->src_y = y << 16;
9948 plane_state->src_w = hdisplay << 16;
9949 plane_state->src_h = vdisplay << 16;
9950
9951 return 0;
9952}
9953
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009954bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009955 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009956 struct intel_load_detect_pipe *old,
9957 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009958{
9959 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009960 struct intel_encoder *intel_encoder =
9961 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009962 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009963 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009964 struct drm_crtc *crtc = NULL;
9965 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009966 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009967 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009968 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009969 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009970 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009971 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009972
Chris Wilsond2dff872011-04-19 08:36:26 +01009973 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009974 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009975 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009976
Rob Clark51fd3712013-11-19 12:10:12 -05009977retry:
9978 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9979 if (ret)
9980 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009981
Jesse Barnes79e53942008-11-07 14:24:08 -08009982 /*
9983 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009984 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009985 * - if the connector already has an assigned crtc, use it (but make
9986 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009987 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009988 * - try to find the first unused crtc that can drive this connector,
9989 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009990 */
9991
9992 /* See if we already have a CRTC for this connector */
9993 if (encoder->crtc) {
9994 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009995
Rob Clark51fd3712013-11-19 12:10:12 -05009996 ret = drm_modeset_lock(&crtc->mutex, ctx);
9997 if (ret)
9998 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +01009999 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10000 if (ret)
10001 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010002
Daniel Vetter24218aa2012-08-12 19:27:11 +020010003 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010004 old->load_detect_temp = false;
10005
10006 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010007 if (connector->dpms != DRM_MODE_DPMS_ON)
10008 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010009
Chris Wilson71731882011-04-19 23:10:58 +010010010 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010011 }
10012
10013 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010014 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010015 i++;
10016 if (!(encoder->possible_crtcs & (1 << i)))
10017 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010018 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010019 continue;
10020 /* This can occur when applying the pipe A quirk on resume. */
10021 if (to_intel_crtc(possible_crtc)->new_enabled)
10022 continue;
10023
10024 crtc = possible_crtc;
10025 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010026 }
10027
10028 /*
10029 * If we didn't find an unused CRTC, don't use any.
10030 */
10031 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010032 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010033 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010034 }
10035
Rob Clark51fd3712013-11-19 12:10:12 -050010036 ret = drm_modeset_lock(&crtc->mutex, ctx);
10037 if (ret)
10038 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010039 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10040 if (ret)
10041 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010042 intel_encoder->new_crtc = to_intel_crtc(crtc);
10043 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010044
10045 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010046 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010047 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010048 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010049 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010050
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010051 state = drm_atomic_state_alloc(dev);
10052 if (!state)
10053 return false;
10054
10055 state->acquire_ctx = ctx;
10056
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010057 connector_state = drm_atomic_get_connector_state(state, connector);
10058 if (IS_ERR(connector_state)) {
10059 ret = PTR_ERR(connector_state);
10060 goto fail;
10061 }
10062
10063 connector_state->crtc = crtc;
10064 connector_state->best_encoder = &intel_encoder->base;
10065
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010066 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10067 if (IS_ERR(crtc_state)) {
10068 ret = PTR_ERR(crtc_state);
10069 goto fail;
10070 }
10071
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010072 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010073
Chris Wilson64927112011-04-20 07:25:26 +010010074 if (!mode)
10075 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010076
Chris Wilsond2dff872011-04-19 08:36:26 +010010077 /* We need a framebuffer large enough to accommodate all accesses
10078 * that the plane may generate whilst we perform load detection.
10079 * We can not rely on the fbcon either being present (we get called
10080 * during its initialisation to detect all boot displays, or it may
10081 * not even exist) or that it is large enough to satisfy the
10082 * requested mode.
10083 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010084 fb = mode_fits_in_fbdev(dev, mode);
10085 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010086 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010087 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10088 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010089 } else
10090 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010091 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010092 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010093 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010094 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010095
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010096 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10097 if (ret)
10098 goto fail;
10099
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010100 drm_mode_copy(&crtc_state->base.mode, mode);
10101
10102 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010103 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010104 if (old->release_fb)
10105 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010106 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010107 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010108 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010109
Jesse Barnes79e53942008-11-07 14:24:08 -080010110 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010111 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010112 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010113
10114 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010115 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010116fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010117 drm_atomic_state_free(state);
10118 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010119
Rob Clark51fd3712013-11-19 12:10:12 -050010120 if (ret == -EDEADLK) {
10121 drm_modeset_backoff(ctx);
10122 goto retry;
10123 }
10124
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010125 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010126}
10127
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010128void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010129 struct intel_load_detect_pipe *old,
10130 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010131{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010132 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010133 struct intel_encoder *intel_encoder =
10134 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010135 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010136 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010137 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010138 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010139 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010140 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010141 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010142
Chris Wilsond2dff872011-04-19 08:36:26 +010010143 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010144 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010145 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010146
Chris Wilson8261b192011-04-19 23:18:09 +010010147 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010148 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010149 if (!state)
10150 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010151
10152 state->acquire_ctx = ctx;
10153
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010154 connector_state = drm_atomic_get_connector_state(state, connector);
10155 if (IS_ERR(connector_state))
10156 goto fail;
10157
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010158 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10159 if (IS_ERR(crtc_state))
10160 goto fail;
10161
Daniel Vetterfc303102012-07-09 10:40:58 +020010162 to_intel_connector(connector)->new_encoder = NULL;
10163 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010164 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010165
10166 connector_state->best_encoder = NULL;
10167 connector_state->crtc = NULL;
10168
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010169 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010170
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010171 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10172 0, 0);
10173 if (ret)
10174 goto fail;
10175
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010176 ret = intel_set_mode(crtc, state);
10177 if (ret)
10178 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010179
Daniel Vetter36206362012-12-10 20:42:17 +010010180 if (old->release_fb) {
10181 drm_framebuffer_unregister_private(old->release_fb);
10182 drm_framebuffer_unreference(old->release_fb);
10183 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010184
Chris Wilson0622a532011-04-21 09:32:11 +010010185 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010186 }
10187
Eric Anholtc751ce42010-03-25 11:48:48 -070010188 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010189 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10190 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010191
10192 return;
10193fail:
10194 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10195 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010196}
10197
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010198static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010199 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010200{
10201 struct drm_i915_private *dev_priv = dev->dev_private;
10202 u32 dpll = pipe_config->dpll_hw_state.dpll;
10203
10204 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010205 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010206 else if (HAS_PCH_SPLIT(dev))
10207 return 120000;
10208 else if (!IS_GEN2(dev))
10209 return 96000;
10210 else
10211 return 48000;
10212}
10213
Jesse Barnes79e53942008-11-07 14:24:08 -080010214/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010215static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010216 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010217{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010218 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010219 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010220 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010221 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010222 u32 fp;
10223 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010224 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010225
10226 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010227 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010228 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010229 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010230
10231 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010232 if (IS_PINEVIEW(dev)) {
10233 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10234 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010235 } else {
10236 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10237 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10238 }
10239
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010240 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010241 if (IS_PINEVIEW(dev))
10242 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10243 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010244 else
10245 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010246 DPLL_FPA01_P1_POST_DIV_SHIFT);
10247
10248 switch (dpll & DPLL_MODE_MASK) {
10249 case DPLLB_MODE_DAC_SERIAL:
10250 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10251 5 : 10;
10252 break;
10253 case DPLLB_MODE_LVDS:
10254 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10255 7 : 14;
10256 break;
10257 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010258 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010259 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010260 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010261 }
10262
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010263 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010264 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010265 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010266 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010267 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010268 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010269 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010270
10271 if (is_lvds) {
10272 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10273 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010274
10275 if (lvds & LVDS_CLKB_POWER_UP)
10276 clock.p2 = 7;
10277 else
10278 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010279 } else {
10280 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10281 clock.p1 = 2;
10282 else {
10283 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10284 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10285 }
10286 if (dpll & PLL_P2_DIVIDE_BY_4)
10287 clock.p2 = 4;
10288 else
10289 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010290 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010291
10292 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010293 }
10294
Ville Syrjälä18442d02013-09-13 16:00:08 +030010295 /*
10296 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010297 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010298 * encoder's get_config() function.
10299 */
10300 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010301}
10302
Ville Syrjälä6878da02013-09-13 15:59:11 +030010303int intel_dotclock_calculate(int link_freq,
10304 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010305{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010306 /*
10307 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010308 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010309 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010310 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010311 *
10312 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010313 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010314 */
10315
Ville Syrjälä6878da02013-09-13 15:59:11 +030010316 if (!m_n->link_n)
10317 return 0;
10318
10319 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10320}
10321
Ville Syrjälä18442d02013-09-13 16:00:08 +030010322static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010323 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010324{
10325 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010326
10327 /* read out port_clock from the DPLL */
10328 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010329
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010330 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010331 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010332 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010333 * agree once we know their relationship in the encoder's
10334 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010335 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010336 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010337 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10338 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010339}
10340
10341/** Returns the currently programmed mode of the given pipe. */
10342struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10343 struct drm_crtc *crtc)
10344{
Jesse Barnes548f2452011-02-17 10:40:53 -080010345 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010346 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010347 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010348 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010349 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010350 int htot = I915_READ(HTOTAL(cpu_transcoder));
10351 int hsync = I915_READ(HSYNC(cpu_transcoder));
10352 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10353 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010354 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010355
10356 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10357 if (!mode)
10358 return NULL;
10359
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010360 /*
10361 * Construct a pipe_config sufficient for getting the clock info
10362 * back out of crtc_clock_get.
10363 *
10364 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10365 * to use a real value here instead.
10366 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010367 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010368 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010369 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10370 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10371 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010372 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10373
Ville Syrjälä773ae032013-09-23 17:48:20 +030010374 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010375 mode->hdisplay = (htot & 0xffff) + 1;
10376 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10377 mode->hsync_start = (hsync & 0xffff) + 1;
10378 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10379 mode->vdisplay = (vtot & 0xffff) + 1;
10380 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10381 mode->vsync_start = (vsync & 0xffff) + 1;
10382 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10383
10384 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010385
10386 return mode;
10387}
10388
Jesse Barnes652c3932009-08-17 13:31:43 -070010389static void intel_decrease_pllclock(struct drm_crtc *crtc)
10390{
10391 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010392 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010393 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010394
Sonika Jindalbaff2962014-07-22 11:16:35 +053010395 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010396 return;
10397
10398 if (!dev_priv->lvds_downclock_avail)
10399 return;
10400
10401 /*
10402 * Since this is called by a timer, we should never get here in
10403 * the manual case.
10404 */
10405 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010406 int pipe = intel_crtc->pipe;
10407 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010408 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010409
Zhao Yakui44d98a62009-10-09 11:39:40 +080010410 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010411
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010412 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010413
Chris Wilson074b5e12012-05-02 12:07:06 +010010414 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010415 dpll |= DISPLAY_RATE_SELECT_FPA1;
10416 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010417 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010418 dpll = I915_READ(dpll_reg);
10419 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010420 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010421 }
10422
10423}
10424
Chris Wilsonf047e392012-07-21 12:31:41 +010010425void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010426{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010427 struct drm_i915_private *dev_priv = dev->dev_private;
10428
Chris Wilsonf62a0072014-02-21 17:55:39 +000010429 if (dev_priv->mm.busy)
10430 return;
10431
Paulo Zanoni43694d62014-03-07 20:08:08 -030010432 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010433 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010434 if (INTEL_INFO(dev)->gen >= 6)
10435 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010436 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010437}
10438
10439void intel_mark_idle(struct drm_device *dev)
10440{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010441 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010442 struct drm_crtc *crtc;
10443
Chris Wilsonf62a0072014-02-21 17:55:39 +000010444 if (!dev_priv->mm.busy)
10445 return;
10446
10447 dev_priv->mm.busy = false;
10448
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010449 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010450 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010451 continue;
10452
10453 intel_decrease_pllclock(crtc);
10454 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010455
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010456 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010457 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010458
Paulo Zanoni43694d62014-03-07 20:08:08 -030010459 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010460}
10461
Jesse Barnes79e53942008-11-07 14:24:08 -080010462static void intel_crtc_destroy(struct drm_crtc *crtc)
10463{
10464 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010465 struct drm_device *dev = crtc->dev;
10466 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010467
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010468 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010469 work = intel_crtc->unpin_work;
10470 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010471 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010472
10473 if (work) {
10474 cancel_work_sync(&work->work);
10475 kfree(work);
10476 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010477
10478 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010479
Jesse Barnes79e53942008-11-07 14:24:08 -080010480 kfree(intel_crtc);
10481}
10482
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010483static void intel_unpin_work_fn(struct work_struct *__work)
10484{
10485 struct intel_unpin_work *work =
10486 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010487 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010488 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010489
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010490 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010491 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010492 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010493
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010494 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010495
10496 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010497 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010498 mutex_unlock(&dev->struct_mutex);
10499
Daniel Vetterf99d7062014-06-19 16:01:59 +020010500 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010501 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010502
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010503 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10504 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10505
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010506 kfree(work);
10507}
10508
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010509static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010510 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010511{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010512 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10513 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010514 unsigned long flags;
10515
10516 /* Ignore early vblank irqs */
10517 if (intel_crtc == NULL)
10518 return;
10519
Daniel Vetterf3260382014-09-15 14:55:23 +020010520 /*
10521 * This is called both by irq handlers and the reset code (to complete
10522 * lost pageflips) so needs the full irqsave spinlocks.
10523 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010524 spin_lock_irqsave(&dev->event_lock, flags);
10525 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010526
10527 /* Ensure we don't miss a work->pending update ... */
10528 smp_rmb();
10529
10530 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010531 spin_unlock_irqrestore(&dev->event_lock, flags);
10532 return;
10533 }
10534
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010535 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010536
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010537 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010538}
10539
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010540void intel_finish_page_flip(struct drm_device *dev, int pipe)
10541{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010542 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010543 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10544
Mario Kleiner49b14a52010-12-09 07:00:07 +010010545 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010546}
10547
10548void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10549{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010550 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010551 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10552
Mario Kleiner49b14a52010-12-09 07:00:07 +010010553 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010554}
10555
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010556/* Is 'a' after or equal to 'b'? */
10557static bool g4x_flip_count_after_eq(u32 a, u32 b)
10558{
10559 return !((a - b) & 0x80000000);
10560}
10561
10562static bool page_flip_finished(struct intel_crtc *crtc)
10563{
10564 struct drm_device *dev = crtc->base.dev;
10565 struct drm_i915_private *dev_priv = dev->dev_private;
10566
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010567 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10568 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10569 return true;
10570
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010571 /*
10572 * The relevant registers doen't exist on pre-ctg.
10573 * As the flip done interrupt doesn't trigger for mmio
10574 * flips on gmch platforms, a flip count check isn't
10575 * really needed there. But since ctg has the registers,
10576 * include it in the check anyway.
10577 */
10578 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10579 return true;
10580
10581 /*
10582 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10583 * used the same base address. In that case the mmio flip might
10584 * have completed, but the CS hasn't even executed the flip yet.
10585 *
10586 * A flip count check isn't enough as the CS might have updated
10587 * the base address just after start of vblank, but before we
10588 * managed to process the interrupt. This means we'd complete the
10589 * CS flip too soon.
10590 *
10591 * Combining both checks should get us a good enough result. It may
10592 * still happen that the CS flip has been executed, but has not
10593 * yet actually completed. But in case the base address is the same
10594 * anyway, we don't really care.
10595 */
10596 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10597 crtc->unpin_work->gtt_offset &&
10598 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10599 crtc->unpin_work->flip_count);
10600}
10601
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010602void intel_prepare_page_flip(struct drm_device *dev, int plane)
10603{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010604 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010605 struct intel_crtc *intel_crtc =
10606 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10607 unsigned long flags;
10608
Daniel Vetterf3260382014-09-15 14:55:23 +020010609
10610 /*
10611 * This is called both by irq handlers and the reset code (to complete
10612 * lost pageflips) so needs the full irqsave spinlocks.
10613 *
10614 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010615 * generate a page-flip completion irq, i.e. every modeset
10616 * is also accompanied by a spurious intel_prepare_page_flip().
10617 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010618 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010619 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010620 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010621 spin_unlock_irqrestore(&dev->event_lock, flags);
10622}
10623
Robin Schroereba905b2014-05-18 02:24:50 +020010624static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010625{
10626 /* Ensure that the work item is consistent when activating it ... */
10627 smp_wmb();
10628 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10629 /* and that it is marked active as soon as the irq could fire. */
10630 smp_wmb();
10631}
10632
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010633static int intel_gen2_queue_flip(struct drm_device *dev,
10634 struct drm_crtc *crtc,
10635 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010636 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010637 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010638 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010639{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010640 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010641 u32 flip_mask;
10642 int ret;
10643
Daniel Vetter6d90c952012-04-26 23:28:05 +020010644 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010645 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010646 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010647
10648 /* Can't queue multiple flips, so wait for the previous
10649 * one to finish before executing the next.
10650 */
10651 if (intel_crtc->plane)
10652 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10653 else
10654 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010655 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10656 intel_ring_emit(ring, MI_NOOP);
10657 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10658 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10659 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010660 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010661 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010662
10663 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010664 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010665 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010666}
10667
10668static int intel_gen3_queue_flip(struct drm_device *dev,
10669 struct drm_crtc *crtc,
10670 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010671 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010672 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010673 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010674{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010675 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010676 u32 flip_mask;
10677 int ret;
10678
Daniel Vetter6d90c952012-04-26 23:28:05 +020010679 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010680 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010681 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010682
10683 if (intel_crtc->plane)
10684 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10685 else
10686 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010687 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10688 intel_ring_emit(ring, MI_NOOP);
10689 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10690 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10691 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010692 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010693 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010694
Chris Wilsone7d841c2012-12-03 11:36:30 +000010695 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010696 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010697 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010698}
10699
10700static int intel_gen4_queue_flip(struct drm_device *dev,
10701 struct drm_crtc *crtc,
10702 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010703 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010704 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010705 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010706{
10707 struct drm_i915_private *dev_priv = dev->dev_private;
10708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10709 uint32_t pf, pipesrc;
10710 int ret;
10711
Daniel Vetter6d90c952012-04-26 23:28:05 +020010712 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010713 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010714 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010715
10716 /* i965+ uses the linear or tiled offsets from the
10717 * Display Registers (which do not change across a page-flip)
10718 * so we need only reprogram the base address.
10719 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010720 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10721 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10722 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010723 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010724 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010725
10726 /* XXX Enabling the panel-fitter across page-flip is so far
10727 * untested on non-native modes, so ignore it for now.
10728 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10729 */
10730 pf = 0;
10731 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010732 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010733
10734 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010735 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010736 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010737}
10738
10739static int intel_gen6_queue_flip(struct drm_device *dev,
10740 struct drm_crtc *crtc,
10741 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010742 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010743 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010744 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010745{
10746 struct drm_i915_private *dev_priv = dev->dev_private;
10747 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10748 uint32_t pf, pipesrc;
10749 int ret;
10750
Daniel Vetter6d90c952012-04-26 23:28:05 +020010751 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010752 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010753 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010754
Daniel Vetter6d90c952012-04-26 23:28:05 +020010755 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10756 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10757 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010758 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010759
Chris Wilson99d9acd2012-04-17 20:37:00 +010010760 /* Contrary to the suggestions in the documentation,
10761 * "Enable Panel Fitter" does not seem to be required when page
10762 * flipping with a non-native mode, and worse causes a normal
10763 * modeset to fail.
10764 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10765 */
10766 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010767 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010768 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010769
10770 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010771 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010772 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010773}
10774
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010775static int intel_gen7_queue_flip(struct drm_device *dev,
10776 struct drm_crtc *crtc,
10777 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010778 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010779 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010780 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010781{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010782 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010783 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010784 int len, ret;
10785
Robin Schroereba905b2014-05-18 02:24:50 +020010786 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010787 case PLANE_A:
10788 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10789 break;
10790 case PLANE_B:
10791 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10792 break;
10793 case PLANE_C:
10794 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10795 break;
10796 default:
10797 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010798 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010799 }
10800
Chris Wilsonffe74d72013-08-26 20:58:12 +010010801 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010802 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010803 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010804 /*
10805 * On Gen 8, SRM is now taking an extra dword to accommodate
10806 * 48bits addresses, and we need a NOOP for the batch size to
10807 * stay even.
10808 */
10809 if (IS_GEN8(dev))
10810 len += 2;
10811 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010812
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010813 /*
10814 * BSpec MI_DISPLAY_FLIP for IVB:
10815 * "The full packet must be contained within the same cache line."
10816 *
10817 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10818 * cacheline, if we ever start emitting more commands before
10819 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10820 * then do the cacheline alignment, and finally emit the
10821 * MI_DISPLAY_FLIP.
10822 */
10823 ret = intel_ring_cacheline_align(ring);
10824 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010825 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010826
Chris Wilsonffe74d72013-08-26 20:58:12 +010010827 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010828 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010829 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010830
Chris Wilsonffe74d72013-08-26 20:58:12 +010010831 /* Unmask the flip-done completion message. Note that the bspec says that
10832 * we should do this for both the BCS and RCS, and that we must not unmask
10833 * more than one flip event at any time (or ensure that one flip message
10834 * can be sent by waiting for flip-done prior to queueing new flips).
10835 * Experimentation says that BCS works despite DERRMR masking all
10836 * flip-done completion events and that unmasking all planes at once
10837 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10838 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10839 */
10840 if (ring->id == RCS) {
10841 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10842 intel_ring_emit(ring, DERRMR);
10843 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10844 DERRMR_PIPEB_PRI_FLIP_DONE |
10845 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010846 if (IS_GEN8(dev))
10847 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10848 MI_SRM_LRM_GLOBAL_GTT);
10849 else
10850 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10851 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010852 intel_ring_emit(ring, DERRMR);
10853 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010854 if (IS_GEN8(dev)) {
10855 intel_ring_emit(ring, 0);
10856 intel_ring_emit(ring, MI_NOOP);
10857 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010858 }
10859
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010860 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010861 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010862 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010863 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010864
10865 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010866 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010867 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010868}
10869
Sourab Gupta84c33a62014-06-02 16:47:17 +053010870static bool use_mmio_flip(struct intel_engine_cs *ring,
10871 struct drm_i915_gem_object *obj)
10872{
10873 /*
10874 * This is not being used for older platforms, because
10875 * non-availability of flip done interrupt forces us to use
10876 * CS flips. Older platforms derive flip done using some clever
10877 * tricks involving the flip_pending status bits and vblank irqs.
10878 * So using MMIO flips there would disrupt this mechanism.
10879 */
10880
Chris Wilson8e09bf82014-07-08 10:40:30 +010010881 if (ring == NULL)
10882 return true;
10883
Sourab Gupta84c33a62014-06-02 16:47:17 +053010884 if (INTEL_INFO(ring->dev)->gen < 5)
10885 return false;
10886
10887 if (i915.use_mmio_flip < 0)
10888 return false;
10889 else if (i915.use_mmio_flip > 0)
10890 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010891 else if (i915.enable_execlists)
10892 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010893 else
Chris Wilsonb4716182015-04-27 13:41:17 +010010894 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010895}
10896
Damien Lespiauff944562014-11-20 14:58:16 +000010897static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10898{
10899 struct drm_device *dev = intel_crtc->base.dev;
10900 struct drm_i915_private *dev_priv = dev->dev_private;
10901 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010902 const enum pipe pipe = intel_crtc->pipe;
10903 u32 ctl, stride;
10904
10905 ctl = I915_READ(PLANE_CTL(pipe, 0));
10906 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010907 switch (fb->modifier[0]) {
10908 case DRM_FORMAT_MOD_NONE:
10909 break;
10910 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010911 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010912 break;
10913 case I915_FORMAT_MOD_Y_TILED:
10914 ctl |= PLANE_CTL_TILED_Y;
10915 break;
10916 case I915_FORMAT_MOD_Yf_TILED:
10917 ctl |= PLANE_CTL_TILED_YF;
10918 break;
10919 default:
10920 MISSING_CASE(fb->modifier[0]);
10921 }
Damien Lespiauff944562014-11-20 14:58:16 +000010922
10923 /*
10924 * The stride is either expressed as a multiple of 64 bytes chunks for
10925 * linear buffers or in number of tiles for tiled buffers.
10926 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010927 stride = fb->pitches[0] /
10928 intel_fb_stride_alignment(dev, fb->modifier[0],
10929 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010930
10931 /*
10932 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10933 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10934 */
10935 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10936 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10937
10938 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10939 POSTING_READ(PLANE_SURF(pipe, 0));
10940}
10941
10942static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010943{
10944 struct drm_device *dev = intel_crtc->base.dev;
10945 struct drm_i915_private *dev_priv = dev->dev_private;
10946 struct intel_framebuffer *intel_fb =
10947 to_intel_framebuffer(intel_crtc->base.primary->fb);
10948 struct drm_i915_gem_object *obj = intel_fb->obj;
10949 u32 dspcntr;
10950 u32 reg;
10951
Sourab Gupta84c33a62014-06-02 16:47:17 +053010952 reg = DSPCNTR(intel_crtc->plane);
10953 dspcntr = I915_READ(reg);
10954
Damien Lespiauc5d97472014-10-25 00:11:11 +010010955 if (obj->tiling_mode != I915_TILING_NONE)
10956 dspcntr |= DISPPLANE_TILED;
10957 else
10958 dspcntr &= ~DISPPLANE_TILED;
10959
Sourab Gupta84c33a62014-06-02 16:47:17 +053010960 I915_WRITE(reg, dspcntr);
10961
10962 I915_WRITE(DSPSURF(intel_crtc->plane),
10963 intel_crtc->unpin_work->gtt_offset);
10964 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010965
Damien Lespiauff944562014-11-20 14:58:16 +000010966}
10967
10968/*
10969 * XXX: This is the temporary way to update the plane registers until we get
10970 * around to using the usual plane update functions for MMIO flips
10971 */
10972static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10973{
10974 struct drm_device *dev = intel_crtc->base.dev;
10975 bool atomic_update;
10976 u32 start_vbl_count;
10977
10978 intel_mark_page_flip_active(intel_crtc);
10979
10980 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10981
10982 if (INTEL_INFO(dev)->gen >= 9)
10983 skl_do_mmio_flip(intel_crtc);
10984 else
10985 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10986 ilk_do_mmio_flip(intel_crtc);
10987
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010988 if (atomic_update)
10989 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010990}
10991
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010992static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010993{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010994 struct intel_mmio_flip *mmio_flip =
10995 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010996
Daniel Vettereed29a52015-05-21 14:21:25 +020010997 if (mmio_flip->req)
10998 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010999 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011000 false, NULL,
11001 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011002
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011003 intel_do_mmio_flip(mmio_flip->crtc);
11004
Daniel Vettereed29a52015-05-21 14:21:25 +020011005 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011006 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011007}
11008
11009static int intel_queue_mmio_flip(struct drm_device *dev,
11010 struct drm_crtc *crtc,
11011 struct drm_framebuffer *fb,
11012 struct drm_i915_gem_object *obj,
11013 struct intel_engine_cs *ring,
11014 uint32_t flags)
11015{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011016 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011017
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011018 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11019 if (mmio_flip == NULL)
11020 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011021
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011022 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011023 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011024 mmio_flip->crtc = to_intel_crtc(crtc);
11025
11026 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11027 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011028
Sourab Gupta84c33a62014-06-02 16:47:17 +053011029 return 0;
11030}
11031
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011032static int intel_default_queue_flip(struct drm_device *dev,
11033 struct drm_crtc *crtc,
11034 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011035 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011036 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011037 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011038{
11039 return -ENODEV;
11040}
11041
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011042static bool __intel_pageflip_stall_check(struct drm_device *dev,
11043 struct drm_crtc *crtc)
11044{
11045 struct drm_i915_private *dev_priv = dev->dev_private;
11046 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11047 struct intel_unpin_work *work = intel_crtc->unpin_work;
11048 u32 addr;
11049
11050 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11051 return true;
11052
11053 if (!work->enable_stall_check)
11054 return false;
11055
11056 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011057 if (work->flip_queued_req &&
11058 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011059 return false;
11060
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011061 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011062 }
11063
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011064 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011065 return false;
11066
11067 /* Potential stall - if we see that the flip has happened,
11068 * assume a missed interrupt. */
11069 if (INTEL_INFO(dev)->gen >= 4)
11070 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11071 else
11072 addr = I915_READ(DSPADDR(intel_crtc->plane));
11073
11074 /* There is a potential issue here with a false positive after a flip
11075 * to the same address. We could address this by checking for a
11076 * non-incrementing frame counter.
11077 */
11078 return addr == work->gtt_offset;
11079}
11080
11081void intel_check_page_flip(struct drm_device *dev, int pipe)
11082{
11083 struct drm_i915_private *dev_priv = dev->dev_private;
11084 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011086 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011087
Dave Gordon6c51d462015-03-06 15:34:26 +000011088 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011089
11090 if (crtc == NULL)
11091 return;
11092
Daniel Vetterf3260382014-09-15 14:55:23 +020011093 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011094 work = intel_crtc->unpin_work;
11095 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011096 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011097 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011098 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011099 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011100 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011101 if (work != NULL &&
11102 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11103 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011104 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011105}
11106
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011107static int intel_crtc_page_flip(struct drm_crtc *crtc,
11108 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011109 struct drm_pending_vblank_event *event,
11110 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011111{
11112 struct drm_device *dev = crtc->dev;
11113 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011114 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011115 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011116 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011117 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011118 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011119 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011120 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011121 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011122 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011123
Matt Roper2ff8fde2014-07-08 07:50:07 -070011124 /*
11125 * drm_mode_page_flip_ioctl() should already catch this, but double
11126 * check to be safe. In the future we may enable pageflipping from
11127 * a disabled primary plane.
11128 */
11129 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11130 return -EBUSY;
11131
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011132 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011133 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011134 return -EINVAL;
11135
11136 /*
11137 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11138 * Note that pitch changes could also affect these register.
11139 */
11140 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011141 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11142 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011143 return -EINVAL;
11144
Chris Wilsonf900db42014-02-20 09:26:13 +000011145 if (i915_terminally_wedged(&dev_priv->gpu_error))
11146 goto out_hang;
11147
Daniel Vetterb14c5672013-09-19 12:18:32 +020011148 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011149 if (work == NULL)
11150 return -ENOMEM;
11151
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011152 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011153 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011154 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011155 INIT_WORK(&work->work, intel_unpin_work_fn);
11156
Daniel Vetter87b6b102014-05-15 15:33:46 +020011157 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011158 if (ret)
11159 goto free_work;
11160
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011161 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011162 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011163 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011164 /* Before declaring the flip queue wedged, check if
11165 * the hardware completed the operation behind our backs.
11166 */
11167 if (__intel_pageflip_stall_check(dev, crtc)) {
11168 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11169 page_flip_completed(intel_crtc);
11170 } else {
11171 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011172 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011173
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011174 drm_crtc_vblank_put(crtc);
11175 kfree(work);
11176 return -EBUSY;
11177 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011178 }
11179 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011180 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011181
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011182 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11183 flush_workqueue(dev_priv->wq);
11184
Jesse Barnes75dfca82010-02-10 15:09:44 -080011185 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011186 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011187 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011188
Matt Roperf4510a22014-04-01 15:22:40 -070011189 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011190 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011191
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011192 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011193
Chris Wilson89ed88b2015-02-16 14:31:49 +000011194 ret = i915_mutex_lock_interruptible(dev);
11195 if (ret)
11196 goto cleanup;
11197
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011198 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011199 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011200
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011201 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011202 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011203
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011204 if (IS_VALLEYVIEW(dev)) {
11205 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011206 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011207 /* vlv: DISPLAY_FLIP fails to change tiling */
11208 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011209 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011210 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011211 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011212 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011213 if (ring == NULL || ring->id != RCS)
11214 ring = &dev_priv->ring[BCS];
11215 } else {
11216 ring = &dev_priv->ring[RCS];
11217 }
11218
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011219 mmio_flip = use_mmio_flip(ring, obj);
11220
11221 /* When using CS flips, we want to emit semaphores between rings.
11222 * However, when using mmio flips we will create a task to do the
11223 * synchronisation, so all we want here is to pin the framebuffer
11224 * into the display plane and skip any waits.
11225 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011226 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011227 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011228 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011229 if (ret)
11230 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011231
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011232 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11233 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011234
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011235 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011236 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11237 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011238 if (ret)
11239 goto cleanup_unpin;
11240
John Harrisonf06cc1b2014-11-24 18:49:37 +000011241 i915_gem_request_assign(&work->flip_queued_req,
11242 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011243 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011244 if (obj->last_write_req) {
11245 ret = i915_gem_check_olr(obj->last_write_req);
11246 if (ret)
11247 goto cleanup_unpin;
11248 }
11249
Sourab Gupta84c33a62014-06-02 16:47:17 +053011250 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011251 page_flip_flags);
11252 if (ret)
11253 goto cleanup_unpin;
11254
John Harrisonf06cc1b2014-11-24 18:49:37 +000011255 i915_gem_request_assign(&work->flip_queued_req,
11256 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011257 }
11258
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011259 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011260 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011261
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011262 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011263 INTEL_FRONTBUFFER_PRIMARY(pipe));
11264
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011265 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011266 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011267 mutex_unlock(&dev->struct_mutex);
11268
Jesse Barnese5510fa2010-07-01 16:48:37 -070011269 trace_i915_flip_request(intel_crtc->plane, obj);
11270
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011271 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011272
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011273cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011274 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011275cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011276 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011277 mutex_unlock(&dev->struct_mutex);
11278cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011279 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011280 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011281
Chris Wilson89ed88b2015-02-16 14:31:49 +000011282 drm_gem_object_unreference_unlocked(&obj->base);
11283 drm_framebuffer_unreference(work->old_fb);
11284
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011285 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011286 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011287 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011288
Daniel Vetter87b6b102014-05-15 15:33:46 +020011289 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011290free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011291 kfree(work);
11292
Chris Wilsonf900db42014-02-20 09:26:13 +000011293 if (ret == -EIO) {
11294out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011295 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011296 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011297 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011298 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011299 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011300 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011301 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011302 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011303}
11304
Jani Nikula65b38e02015-04-13 11:26:56 +030011305static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011306 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11307 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011308 .atomic_begin = intel_begin_crtc_commit,
11309 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011310};
11311
Daniel Vetter9a935852012-07-05 22:34:27 +020011312/**
11313 * intel_modeset_update_staged_output_state
11314 *
11315 * Updates the staged output configuration state, e.g. after we've read out the
11316 * current hw state.
11317 */
11318static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11319{
Ville Syrjälä76688512014-01-10 11:28:06 +020011320 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011321 struct intel_encoder *encoder;
11322 struct intel_connector *connector;
11323
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011324 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011325 connector->new_encoder =
11326 to_intel_encoder(connector->base.encoder);
11327 }
11328
Damien Lespiaub2784e12014-08-05 11:29:37 +010011329 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011330 encoder->new_crtc =
11331 to_intel_crtc(encoder->base.crtc);
11332 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011333
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011334 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011335 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011336 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011337}
11338
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011339/* Transitional helper to copy current connector/encoder state to
11340 * connector->state. This is needed so that code that is partially
11341 * converted to atomic does the right thing.
11342 */
11343static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11344{
11345 struct intel_connector *connector;
11346
11347 for_each_intel_connector(dev, connector) {
11348 if (connector->base.encoder) {
11349 connector->base.state->best_encoder =
11350 connector->base.encoder;
11351 connector->base.state->crtc =
11352 connector->base.encoder->crtc;
11353 } else {
11354 connector->base.state->best_encoder = NULL;
11355 connector->base.state->crtc = NULL;
11356 }
11357 }
11358}
11359
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011360/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011361 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011362static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011363{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011364 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011365 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011366 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011367
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011368 for_each_intel_connector(state->dev, connector) {
11369 connector->base.encoder = connector->base.state->best_encoder;
11370 if (connector->base.encoder)
11371 connector->base.encoder->crtc =
11372 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011373 }
11374
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011375 /* Update crtc of disabled encoders */
11376 for_each_intel_encoder(state->dev, encoder) {
11377 int num_connectors = 0;
11378
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011379 for_each_intel_connector(state->dev, connector)
11380 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011381 num_connectors++;
11382
11383 if (num_connectors == 0)
11384 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011385 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011386
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011387 for_each_intel_crtc(state->dev, crtc) {
11388 crtc->base.enabled = crtc->base.state->enable;
11389 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011390 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011391
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011392 /* Copy the new configuration to the staged state, to keep the few
11393 * pieces of code that haven't been converted yet happy */
11394 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011395}
11396
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011397static void
Robin Schroereba905b2014-05-18 02:24:50 +020011398connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011399 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011400{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011401 int bpp = pipe_config->pipe_bpp;
11402
11403 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11404 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011405 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011406
11407 /* Don't use an invalid EDID bpc value */
11408 if (connector->base.display_info.bpc &&
11409 connector->base.display_info.bpc * 3 < bpp) {
11410 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11411 bpp, connector->base.display_info.bpc*3);
11412 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11413 }
11414
11415 /* Clamp bpp to 8 on screens without EDID 1.4 */
11416 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11417 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11418 bpp);
11419 pipe_config->pipe_bpp = 24;
11420 }
11421}
11422
11423static int
11424compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011425 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011426{
11427 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011428 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011429 struct drm_connector *connector;
11430 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011431 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011432
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011433 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011434 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011435 else if (INTEL_INFO(dev)->gen >= 5)
11436 bpp = 12*3;
11437 else
11438 bpp = 8*3;
11439
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011440
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011441 pipe_config->pipe_bpp = bpp;
11442
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011443 state = pipe_config->base.state;
11444
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011445 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011446 for_each_connector_in_state(state, connector, connector_state, i) {
11447 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011448 continue;
11449
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011450 connected_sink_compute_bpp(to_intel_connector(connector),
11451 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011452 }
11453
11454 return bpp;
11455}
11456
Daniel Vetter644db712013-09-19 14:53:58 +020011457static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11458{
11459 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11460 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011461 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011462 mode->crtc_hdisplay, mode->crtc_hsync_start,
11463 mode->crtc_hsync_end, mode->crtc_htotal,
11464 mode->crtc_vdisplay, mode->crtc_vsync_start,
11465 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11466}
11467
Daniel Vetterc0b03412013-05-28 12:05:54 +020011468static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011469 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011470 const char *context)
11471{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011472 struct drm_device *dev = crtc->base.dev;
11473 struct drm_plane *plane;
11474 struct intel_plane *intel_plane;
11475 struct intel_plane_state *state;
11476 struct drm_framebuffer *fb;
11477
11478 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11479 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011480
11481 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11482 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11483 pipe_config->pipe_bpp, pipe_config->dither);
11484 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11485 pipe_config->has_pch_encoder,
11486 pipe_config->fdi_lanes,
11487 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11488 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11489 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011490 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11491 pipe_config->has_dp_encoder,
11492 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11493 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11494 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011495
11496 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11497 pipe_config->has_dp_encoder,
11498 pipe_config->dp_m2_n2.gmch_m,
11499 pipe_config->dp_m2_n2.gmch_n,
11500 pipe_config->dp_m2_n2.link_m,
11501 pipe_config->dp_m2_n2.link_n,
11502 pipe_config->dp_m2_n2.tu);
11503
Daniel Vetter55072d12014-11-20 16:10:28 +010011504 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11505 pipe_config->has_audio,
11506 pipe_config->has_infoframe);
11507
Daniel Vetterc0b03412013-05-28 12:05:54 +020011508 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011509 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011510 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011511 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11512 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011513 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011514 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11515 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011516 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11517 crtc->num_scalers,
11518 pipe_config->scaler_state.scaler_users,
11519 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011520 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11521 pipe_config->gmch_pfit.control,
11522 pipe_config->gmch_pfit.pgm_ratios,
11523 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011524 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011525 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011526 pipe_config->pch_pfit.size,
11527 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011528 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011529 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011530
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011531 if (IS_BROXTON(dev)) {
11532 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11533 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11534 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11535 pipe_config->ddi_pll_sel,
11536 pipe_config->dpll_hw_state.ebb0,
11537 pipe_config->dpll_hw_state.pll0,
11538 pipe_config->dpll_hw_state.pll1,
11539 pipe_config->dpll_hw_state.pll2,
11540 pipe_config->dpll_hw_state.pll3,
11541 pipe_config->dpll_hw_state.pll6,
11542 pipe_config->dpll_hw_state.pll8,
11543 pipe_config->dpll_hw_state.pcsdw12);
11544 } else if (IS_SKYLAKE(dev)) {
11545 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11546 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11547 pipe_config->ddi_pll_sel,
11548 pipe_config->dpll_hw_state.ctrl1,
11549 pipe_config->dpll_hw_state.cfgcr1,
11550 pipe_config->dpll_hw_state.cfgcr2);
11551 } else if (HAS_DDI(dev)) {
11552 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11553 pipe_config->ddi_pll_sel,
11554 pipe_config->dpll_hw_state.wrpll);
11555 } else {
11556 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11557 "fp0: 0x%x, fp1: 0x%x\n",
11558 pipe_config->dpll_hw_state.dpll,
11559 pipe_config->dpll_hw_state.dpll_md,
11560 pipe_config->dpll_hw_state.fp0,
11561 pipe_config->dpll_hw_state.fp1);
11562 }
11563
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011564 DRM_DEBUG_KMS("planes on this crtc\n");
11565 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11566 intel_plane = to_intel_plane(plane);
11567 if (intel_plane->pipe != crtc->pipe)
11568 continue;
11569
11570 state = to_intel_plane_state(plane->state);
11571 fb = state->base.fb;
11572 if (!fb) {
11573 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11574 "disabled, scaler_id = %d\n",
11575 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11576 plane->base.id, intel_plane->pipe,
11577 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11578 drm_plane_index(plane), state->scaler_id);
11579 continue;
11580 }
11581
11582 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11583 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11584 plane->base.id, intel_plane->pipe,
11585 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11586 drm_plane_index(plane));
11587 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11588 fb->base.id, fb->width, fb->height, fb->pixel_format);
11589 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11590 state->scaler_id,
11591 state->src.x1 >> 16, state->src.y1 >> 16,
11592 drm_rect_width(&state->src) >> 16,
11593 drm_rect_height(&state->src) >> 16,
11594 state->dst.x1, state->dst.y1,
11595 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11596 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011597}
11598
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011599static bool encoders_cloneable(const struct intel_encoder *a,
11600 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011601{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011602 /* masks could be asymmetric, so check both ways */
11603 return a == b || (a->cloneable & (1 << b->type) &&
11604 b->cloneable & (1 << a->type));
11605}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011606
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011607static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11608 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011609 struct intel_encoder *encoder)
11610{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011611 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011612 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011613 struct drm_connector_state *connector_state;
11614 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011615
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011616 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011617 if (connector_state->crtc != &crtc->base)
11618 continue;
11619
11620 source_encoder =
11621 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011622 if (!encoders_cloneable(encoder, source_encoder))
11623 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011624 }
11625
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011626 return true;
11627}
11628
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011629static bool check_encoder_cloning(struct drm_atomic_state *state,
11630 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011631{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011632 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011633 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011634 struct drm_connector_state *connector_state;
11635 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011636
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011637 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011638 if (connector_state->crtc != &crtc->base)
11639 continue;
11640
11641 encoder = to_intel_encoder(connector_state->best_encoder);
11642 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011643 return false;
11644 }
11645
11646 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011647}
11648
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011649static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011650{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011651 struct drm_device *dev = state->dev;
11652 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011653 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011654 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011655 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011656 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011657
11658 /*
11659 * Walk the connector list instead of the encoder
11660 * list to detect the problem on ddi platforms
11661 * where there's just one encoder per digital port.
11662 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011663 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011664 if (!connector_state->best_encoder)
11665 continue;
11666
11667 encoder = to_intel_encoder(connector_state->best_encoder);
11668
11669 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011670
11671 switch (encoder->type) {
11672 unsigned int port_mask;
11673 case INTEL_OUTPUT_UNKNOWN:
11674 if (WARN_ON(!HAS_DDI(dev)))
11675 break;
11676 case INTEL_OUTPUT_DISPLAYPORT:
11677 case INTEL_OUTPUT_HDMI:
11678 case INTEL_OUTPUT_EDP:
11679 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11680
11681 /* the same port mustn't appear more than once */
11682 if (used_ports & port_mask)
11683 return false;
11684
11685 used_ports |= port_mask;
11686 default:
11687 break;
11688 }
11689 }
11690
11691 return true;
11692}
11693
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011694static void
11695clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11696{
11697 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011698 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011699 struct intel_dpll_hw_state dpll_hw_state;
11700 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011701 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011702
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011703 /* FIXME: before the switch to atomic started, a new pipe_config was
11704 * kzalloc'd. Code that depends on any field being zero should be
11705 * fixed, so that the crtc_state can be safely duplicated. For now,
11706 * only fields that are know to not cause problems are preserved. */
11707
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011708 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011709 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011710 shared_dpll = crtc_state->shared_dpll;
11711 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011712 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011713
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011714 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011715
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011716 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011717 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011718 crtc_state->shared_dpll = shared_dpll;
11719 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011720 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011721}
11722
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011723static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011724intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011725 struct drm_atomic_state *state,
11726 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011727{
Daniel Vetter7758a112012-07-08 19:40:39 +020011728 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011729 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011730 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011731 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011732 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011733 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011734
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011735 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011736 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011737 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011738 }
11739
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011740 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011741 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011742 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011743 }
11744
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011745 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011746
Daniel Vettere143a212013-07-04 12:01:15 +020011747 pipe_config->cpu_transcoder =
11748 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011749
Imre Deak2960bc92013-07-30 13:36:32 +030011750 /*
11751 * Sanitize sync polarity flags based on requested ones. If neither
11752 * positive or negative polarity is requested, treat this as meaning
11753 * negative polarity.
11754 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011755 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011756 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011757 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011758
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011759 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011760 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011761 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011762
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011763 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11764 * plane pixel format and any sink constraints into account. Returns the
11765 * source plane bpp so that dithering can be selected on mismatches
11766 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011767 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11768 pipe_config);
11769 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011770 goto fail;
11771
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011772 /*
11773 * Determine the real pipe dimensions. Note that stereo modes can
11774 * increase the actual pipe size due to the frame doubling and
11775 * insertion of additional space for blanks between the frame. This
11776 * is stored in the crtc timings. We use the requested mode to do this
11777 * computation to clearly distinguish it from the adjusted mode, which
11778 * can be changed by the connectors in the below retry loop.
11779 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011780 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011781 &pipe_config->pipe_src_w,
11782 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011783
Daniel Vettere29c22c2013-02-21 00:00:16 +010011784encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011785 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011786 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011787 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011788
Daniel Vetter135c81b2013-07-21 21:37:09 +020011789 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011790 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11791 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011792
Daniel Vetter7758a112012-07-08 19:40:39 +020011793 /* Pass our mode to the connectors and the CRTC to give them a chance to
11794 * adjust it according to limitations or connector properties, and also
11795 * a chance to reject the mode entirely.
11796 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011797 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011798 if (connector_state->crtc != crtc)
11799 continue;
11800
11801 encoder = to_intel_encoder(connector_state->best_encoder);
11802
Daniel Vetterefea6e82013-07-21 21:36:59 +020011803 if (!(encoder->compute_config(encoder, pipe_config))) {
11804 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011805 goto fail;
11806 }
11807 }
11808
Daniel Vetterff9a6752013-06-01 17:16:21 +020011809 /* Set default port clock if not overwritten by the encoder. Needs to be
11810 * done afterwards in case the encoder adjusts the mode. */
11811 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011812 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011813 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011814
Daniel Vettera43f6e02013-06-07 23:10:32 +020011815 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011816 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011817 DRM_DEBUG_KMS("CRTC fixup failed\n");
11818 goto fail;
11819 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011820
11821 if (ret == RETRY) {
11822 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11823 ret = -EINVAL;
11824 goto fail;
11825 }
11826
11827 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11828 retry = false;
11829 goto encoder_retry;
11830 }
11831
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011832 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011833 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011834 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011835
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011836 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020011837fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011838 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011839}
11840
Daniel Vetterea9d7582012-07-10 10:42:52 +020011841static bool intel_crtc_in_use(struct drm_crtc *crtc)
11842{
11843 struct drm_encoder *encoder;
11844 struct drm_device *dev = crtc->dev;
11845
11846 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11847 if (encoder->crtc == crtc)
11848 return true;
11849
11850 return false;
11851}
11852
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011853static bool
11854needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011855{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011856 return state->mode_changed || state->active_changed;
11857}
11858
11859static void
11860intel_modeset_update_state(struct drm_atomic_state *state)
11861{
11862 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011863 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011864 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011865 struct drm_crtc *crtc;
11866 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011867 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011868 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011869
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011870 intel_shared_dpll_commit(dev_priv);
11871
Damien Lespiaub2784e12014-08-05 11:29:37 +010011872 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011873 if (!intel_encoder->base.crtc)
11874 continue;
11875
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011876 for_each_crtc_in_state(state, crtc, crtc_state, i)
11877 if (crtc == intel_encoder->base.crtc)
11878 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011879
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011880 if (crtc != intel_encoder->base.crtc)
11881 continue;
11882
11883 if (crtc_state->enable && needs_modeset(crtc_state))
Daniel Vetterea9d7582012-07-10 10:42:52 +020011884 intel_encoder->connectors_active = false;
11885 }
11886
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011887 drm_atomic_helper_swap_state(state->dev, state);
11888 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011889
Ville Syrjälä76688512014-01-10 11:28:06 +020011890 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011891 for_each_crtc(dev, crtc) {
11892 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011893 }
11894
11895 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11896 if (!connector->encoder || !connector->encoder->crtc)
11897 continue;
11898
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011899 for_each_crtc_in_state(state, crtc, crtc_state, i)
11900 if (crtc == connector->encoder->crtc)
11901 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011902
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011903 if (crtc != connector->encoder->crtc)
11904 continue;
11905
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011906 if (crtc->state->enable && needs_modeset(crtc->state)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011907 struct drm_property *dpms_property =
11908 dev->mode_config.dpms_property;
11909
Daniel Vetterea9d7582012-07-10 10:42:52 +020011910 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011911 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011912 dpms_property,
11913 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011914
11915 intel_encoder = to_intel_encoder(connector->encoder);
11916 intel_encoder->connectors_active = true;
11917 }
11918 }
11919
11920}
11921
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011922static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011923{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011924 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011925
11926 if (clock1 == clock2)
11927 return true;
11928
11929 if (!clock1 || !clock2)
11930 return false;
11931
11932 diff = abs(clock1 - clock2);
11933
11934 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11935 return true;
11936
11937 return false;
11938}
11939
Daniel Vetter25c5b262012-07-08 22:08:04 +020011940#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11941 list_for_each_entry((intel_crtc), \
11942 &(dev)->mode_config.crtc_list, \
11943 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011944 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011945
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011946static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011947intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011948 struct intel_crtc_state *current_config,
11949 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011950{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011951#define PIPE_CONF_CHECK_X(name) \
11952 if (current_config->name != pipe_config->name) { \
11953 DRM_ERROR("mismatch in " #name " " \
11954 "(expected 0x%08x, found 0x%08x)\n", \
11955 current_config->name, \
11956 pipe_config->name); \
11957 return false; \
11958 }
11959
Daniel Vetter08a24032013-04-19 11:25:34 +020011960#define PIPE_CONF_CHECK_I(name) \
11961 if (current_config->name != pipe_config->name) { \
11962 DRM_ERROR("mismatch in " #name " " \
11963 "(expected %i, found %i)\n", \
11964 current_config->name, \
11965 pipe_config->name); \
11966 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011967 }
11968
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011969/* This is required for BDW+ where there is only one set of registers for
11970 * switching between high and low RR.
11971 * This macro can be used whenever a comparison has to be made between one
11972 * hw state and multiple sw state variables.
11973 */
11974#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11975 if ((current_config->name != pipe_config->name) && \
11976 (current_config->alt_name != pipe_config->name)) { \
11977 DRM_ERROR("mismatch in " #name " " \
11978 "(expected %i or %i, found %i)\n", \
11979 current_config->name, \
11980 current_config->alt_name, \
11981 pipe_config->name); \
11982 return false; \
11983 }
11984
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011985#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11986 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011987 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011988 "(expected %i, found %i)\n", \
11989 current_config->name & (mask), \
11990 pipe_config->name & (mask)); \
11991 return false; \
11992 }
11993
Ville Syrjälä5e550652013-09-06 23:29:07 +030011994#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11995 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11996 DRM_ERROR("mismatch in " #name " " \
11997 "(expected %i, found %i)\n", \
11998 current_config->name, \
11999 pipe_config->name); \
12000 return false; \
12001 }
12002
Daniel Vetterbb760062013-06-06 14:55:52 +020012003#define PIPE_CONF_QUIRK(quirk) \
12004 ((current_config->quirks | pipe_config->quirks) & (quirk))
12005
Daniel Vettereccb1402013-05-22 00:50:22 +020012006 PIPE_CONF_CHECK_I(cpu_transcoder);
12007
Daniel Vetter08a24032013-04-19 11:25:34 +020012008 PIPE_CONF_CHECK_I(has_pch_encoder);
12009 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012010 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12011 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12012 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12013 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12014 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012015
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012016 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012017
12018 if (INTEL_INFO(dev)->gen < 8) {
12019 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12020 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12021 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12022 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12023 PIPE_CONF_CHECK_I(dp_m_n.tu);
12024
12025 if (current_config->has_drrs) {
12026 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12027 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12028 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12029 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12030 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12031 }
12032 } else {
12033 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12034 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12035 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12036 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12037 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12038 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012039
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012040 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12041 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12042 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12043 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12044 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12045 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012046
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012047 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12048 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12049 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12050 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12051 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12052 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012053
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012054 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012055 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012056 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12057 IS_VALLEYVIEW(dev))
12058 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012059 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012060
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012061 PIPE_CONF_CHECK_I(has_audio);
12062
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012063 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012064 DRM_MODE_FLAG_INTERLACE);
12065
Daniel Vetterbb760062013-06-06 14:55:52 +020012066 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012067 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012068 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012069 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012070 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012071 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012072 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012073 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012074 DRM_MODE_FLAG_NVSYNC);
12075 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012076
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012077 PIPE_CONF_CHECK_I(pipe_src_w);
12078 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012079
Daniel Vetter99535992014-04-13 12:00:33 +020012080 /*
12081 * FIXME: BIOS likes to set up a cloned config with lvds+external
12082 * screen. Since we don't yet re-compute the pipe config when moving
12083 * just the lvds port away to another pipe the sw tracking won't match.
12084 *
12085 * Proper atomic modesets with recomputed global state will fix this.
12086 * Until then just don't check gmch state for inherited modes.
12087 */
12088 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12089 PIPE_CONF_CHECK_I(gmch_pfit.control);
12090 /* pfit ratios are autocomputed by the hw on gen4+ */
12091 if (INTEL_INFO(dev)->gen < 4)
12092 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12093 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12094 }
12095
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012096 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12097 if (current_config->pch_pfit.enabled) {
12098 PIPE_CONF_CHECK_I(pch_pfit.pos);
12099 PIPE_CONF_CHECK_I(pch_pfit.size);
12100 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012101
Chandra Kondurua1b22782015-04-07 15:28:45 -070012102 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12103
Jesse Barnese59150d2014-01-07 13:30:45 -080012104 /* BDW+ don't expose a synchronous way to read the state */
12105 if (IS_HASWELL(dev))
12106 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012107
Ville Syrjälä282740f2013-09-04 18:30:03 +030012108 PIPE_CONF_CHECK_I(double_wide);
12109
Daniel Vetter26804af2014-06-25 22:01:55 +030012110 PIPE_CONF_CHECK_X(ddi_pll_sel);
12111
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012112 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012113 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012114 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012115 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12116 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012117 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012118 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12119 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12120 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012121
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012122 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12123 PIPE_CONF_CHECK_I(pipe_bpp);
12124
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012125 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012126 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012127
Daniel Vetter66e985c2013-06-05 13:34:20 +020012128#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012129#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012130#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012131#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012132#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012133#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012134
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012135 return true;
12136}
12137
Damien Lespiau08db6652014-11-04 17:06:52 +000012138static void check_wm_state(struct drm_device *dev)
12139{
12140 struct drm_i915_private *dev_priv = dev->dev_private;
12141 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12142 struct intel_crtc *intel_crtc;
12143 int plane;
12144
12145 if (INTEL_INFO(dev)->gen < 9)
12146 return;
12147
12148 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12149 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12150
12151 for_each_intel_crtc(dev, intel_crtc) {
12152 struct skl_ddb_entry *hw_entry, *sw_entry;
12153 const enum pipe pipe = intel_crtc->pipe;
12154
12155 if (!intel_crtc->active)
12156 continue;
12157
12158 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012159 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012160 hw_entry = &hw_ddb.plane[pipe][plane];
12161 sw_entry = &sw_ddb->plane[pipe][plane];
12162
12163 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12164 continue;
12165
12166 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12167 "(expected (%u,%u), found (%u,%u))\n",
12168 pipe_name(pipe), plane + 1,
12169 sw_entry->start, sw_entry->end,
12170 hw_entry->start, hw_entry->end);
12171 }
12172
12173 /* cursor */
12174 hw_entry = &hw_ddb.cursor[pipe];
12175 sw_entry = &sw_ddb->cursor[pipe];
12176
12177 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12178 continue;
12179
12180 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12181 "(expected (%u,%u), found (%u,%u))\n",
12182 pipe_name(pipe),
12183 sw_entry->start, sw_entry->end,
12184 hw_entry->start, hw_entry->end);
12185 }
12186}
12187
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012188static void
12189check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012190{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012191 struct intel_connector *connector;
12192
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012193 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012194 /* This also checks the encoder/connector hw state with the
12195 * ->get_hw_state callbacks. */
12196 intel_connector_check_state(connector);
12197
Rob Clarke2c719b2014-12-15 13:56:32 -050012198 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012199 "connector's staged encoder doesn't match current encoder\n");
12200 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012201}
12202
12203static void
12204check_encoder_state(struct drm_device *dev)
12205{
12206 struct intel_encoder *encoder;
12207 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012208
Damien Lespiaub2784e12014-08-05 11:29:37 +010012209 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012210 bool enabled = false;
12211 bool active = false;
12212 enum pipe pipe, tracked_pipe;
12213
12214 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12215 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012216 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012217
Rob Clarke2c719b2014-12-15 13:56:32 -050012218 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012219 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012220 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012221 "encoder's active_connectors set, but no crtc\n");
12222
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012223 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012224 if (connector->base.encoder != &encoder->base)
12225 continue;
12226 enabled = true;
12227 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12228 active = true;
12229 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012230 /*
12231 * for MST connectors if we unplug the connector is gone
12232 * away but the encoder is still connected to a crtc
12233 * until a modeset happens in response to the hotplug.
12234 */
12235 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12236 continue;
12237
Rob Clarke2c719b2014-12-15 13:56:32 -050012238 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012239 "encoder's enabled state mismatch "
12240 "(expected %i, found %i)\n",
12241 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012242 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012243 "active encoder with no crtc\n");
12244
Rob Clarke2c719b2014-12-15 13:56:32 -050012245 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012246 "encoder's computed active state doesn't match tracked active state "
12247 "(expected %i, found %i)\n", active, encoder->connectors_active);
12248
12249 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012250 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012251 "encoder's hw state doesn't match sw tracking "
12252 "(expected %i, found %i)\n",
12253 encoder->connectors_active, active);
12254
12255 if (!encoder->base.crtc)
12256 continue;
12257
12258 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012259 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012260 "active encoder's pipe doesn't match"
12261 "(expected %i, found %i)\n",
12262 tracked_pipe, pipe);
12263
12264 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012265}
12266
12267static void
12268check_crtc_state(struct drm_device *dev)
12269{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012270 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012271 struct intel_crtc *crtc;
12272 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012273 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012274
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012275 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012276 bool enabled = false;
12277 bool active = false;
12278
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012279 memset(&pipe_config, 0, sizeof(pipe_config));
12280
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012281 DRM_DEBUG_KMS("[CRTC:%d]\n",
12282 crtc->base.base.id);
12283
Matt Roper83d65732015-02-25 13:12:16 -080012284 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012285 "active crtc, but not enabled in sw tracking\n");
12286
Damien Lespiaub2784e12014-08-05 11:29:37 +010012287 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012288 if (encoder->base.crtc != &crtc->base)
12289 continue;
12290 enabled = true;
12291 if (encoder->connectors_active)
12292 active = true;
12293 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012294
Rob Clarke2c719b2014-12-15 13:56:32 -050012295 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012296 "crtc's computed active state doesn't match tracked active state "
12297 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012298 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012299 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012300 "(expected %i, found %i)\n", enabled,
12301 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012302
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012303 active = dev_priv->display.get_pipe_config(crtc,
12304 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012305
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012306 /* hw state is inconsistent with the pipe quirk */
12307 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12308 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012309 active = crtc->active;
12310
Damien Lespiaub2784e12014-08-05 11:29:37 +010012311 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012312 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012313 if (encoder->base.crtc != &crtc->base)
12314 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012315 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012316 encoder->get_config(encoder, &pipe_config);
12317 }
12318
Rob Clarke2c719b2014-12-15 13:56:32 -050012319 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012320 "crtc active state doesn't match with hw state "
12321 "(expected %i, found %i)\n", crtc->active, active);
12322
Daniel Vetterc0b03412013-05-28 12:05:54 +020012323 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012324 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012325 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012326 intel_dump_pipe_config(crtc, &pipe_config,
12327 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012328 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012329 "[sw state]");
12330 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012331 }
12332}
12333
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012334static void
12335check_shared_dpll_state(struct drm_device *dev)
12336{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012337 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012338 struct intel_crtc *crtc;
12339 struct intel_dpll_hw_state dpll_hw_state;
12340 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012341
12342 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12343 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12344 int enabled_crtcs = 0, active_crtcs = 0;
12345 bool active;
12346
12347 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12348
12349 DRM_DEBUG_KMS("%s\n", pll->name);
12350
12351 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12352
Rob Clarke2c719b2014-12-15 13:56:32 -050012353 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012354 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012355 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012356 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012357 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012358 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012359 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012360 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012361 "pll on state mismatch (expected %i, found %i)\n",
12362 pll->on, active);
12363
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012364 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012365 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012366 enabled_crtcs++;
12367 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12368 active_crtcs++;
12369 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012370 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012371 "pll active crtcs mismatch (expected %i, found %i)\n",
12372 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012373 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012374 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012375 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012376
Rob Clarke2c719b2014-12-15 13:56:32 -050012377 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012378 sizeof(dpll_hw_state)),
12379 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012380 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012381}
12382
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012383void
12384intel_modeset_check_state(struct drm_device *dev)
12385{
Damien Lespiau08db6652014-11-04 17:06:52 +000012386 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012387 check_connector_state(dev);
12388 check_encoder_state(dev);
12389 check_crtc_state(dev);
12390 check_shared_dpll_state(dev);
12391}
12392
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012393void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012394 int dotclock)
12395{
12396 /*
12397 * FDI already provided one idea for the dotclock.
12398 * Yell if the encoder disagrees.
12399 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012400 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012401 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012402 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012403}
12404
Ville Syrjälä80715b22014-05-15 20:23:23 +030012405static void update_scanline_offset(struct intel_crtc *crtc)
12406{
12407 struct drm_device *dev = crtc->base.dev;
12408
12409 /*
12410 * The scanline counter increments at the leading edge of hsync.
12411 *
12412 * On most platforms it starts counting from vtotal-1 on the
12413 * first active line. That means the scanline counter value is
12414 * always one less than what we would expect. Ie. just after
12415 * start of vblank, which also occurs at start of hsync (on the
12416 * last active line), the scanline counter will read vblank_start-1.
12417 *
12418 * On gen2 the scanline counter starts counting from 1 instead
12419 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12420 * to keep the value positive), instead of adding one.
12421 *
12422 * On HSW+ the behaviour of the scanline counter depends on the output
12423 * type. For DP ports it behaves like most other platforms, but on HDMI
12424 * there's an extra 1 line difference. So we need to add two instead of
12425 * one to the value.
12426 */
12427 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012428 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012429 int vtotal;
12430
12431 vtotal = mode->crtc_vtotal;
12432 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12433 vtotal /= 2;
12434
12435 crtc->scanline_offset = vtotal - 1;
12436 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012437 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012438 crtc->scanline_offset = 2;
12439 } else
12440 crtc->scanline_offset = 1;
12441}
12442
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012443static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012444intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012445 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012446{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012447 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012448 int ret = 0;
12449
12450 ret = drm_atomic_add_affected_connectors(state, crtc);
12451 if (ret)
12452 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012453
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012454 ret = drm_atomic_helper_check_modeset(state->dev, state);
12455 if (ret)
12456 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012457
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012458 /*
12459 * Note this needs changes when we start tracking multiple modes
12460 * and crtcs. At that point we'll need to compute the whole config
12461 * (i.e. one pipe_config for each crtc) rather than just the one
12462 * for this crtc.
12463 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012464 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12465 if (IS_ERR(pipe_config))
12466 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012467
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012468 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012469 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012470
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012471 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012472 if (ret)
12473 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012474
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012475 /* Check things that can only be changed through modeset */
12476 if (pipe_config->has_audio !=
12477 to_intel_crtc(crtc)->config->has_audio)
12478 pipe_config->base.mode_changed = true;
12479
12480 /*
12481 * Note we have an issue here with infoframes: current code
12482 * only updates them on the full mode set path per hw
12483 * requirements. So here we should be checking for any
12484 * required changes and forcing a mode set.
12485 */
12486
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012487 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12488
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012489 ret = drm_atomic_helper_check_planes(state->dev, state);
12490 if (ret)
12491 return ERR_PTR(ret);
12492
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012493 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012494}
12495
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012496static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012497{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012498 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012499 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012500 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012501 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012502 struct intel_crtc_state *intel_crtc_state;
12503 struct drm_crtc *crtc;
12504 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012505 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012506 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012507
12508 if (!dev_priv->display.crtc_compute_clock)
12509 return 0;
12510
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012511 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12512 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012513 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012514
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012515 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012516 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012517 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012518 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012519 }
12520
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012521 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12522 if (ret)
12523 goto done;
12524
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012525 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12526 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012527 continue;
12528
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012529 intel_crtc = to_intel_crtc(crtc);
12530 intel_crtc_state = to_intel_crtc_state(crtc_state);
12531
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012532 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012533 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012534 if (ret) {
12535 intel_shared_dpll_abort_config(dev_priv);
12536 goto done;
12537 }
12538 }
12539
12540done:
12541 return ret;
12542}
12543
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012544/* Code that should eventually be part of atomic_check() */
12545static int __intel_set_mode_checks(struct drm_atomic_state *state)
12546{
12547 struct drm_device *dev = state->dev;
12548 int ret;
12549
12550 /*
12551 * See if the config requires any additional preparation, e.g.
12552 * to adjust global state with pipes off. We need to do this
12553 * here so we can get the modeset_pipe updated config for the new
12554 * mode set on this crtc. For other crtcs we need to use the
12555 * adjusted_mode bits in the crtc directly.
12556 */
12557 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12558 ret = valleyview_modeset_global_pipes(state);
12559 if (ret)
12560 return ret;
12561 }
12562
12563 ret = __intel_set_mode_setup_plls(state);
12564 if (ret)
12565 return ret;
12566
12567 return 0;
12568}
12569
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012570static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012571 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012572{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012573 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012574 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012575 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012576 struct drm_crtc *crtc;
12577 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012578 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012579 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012580
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012581 ret = __intel_set_mode_checks(state);
12582 if (ret < 0)
12583 return ret;
12584
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012585 ret = drm_atomic_helper_prepare_planes(dev, state);
12586 if (ret)
12587 return ret;
12588
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012589 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12590 if (!needs_modeset(crtc_state))
12591 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012592
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012593 if (!crtc_state->enable) {
12594 intel_crtc_disable(crtc);
12595 } else if (crtc->state->enable) {
12596 intel_crtc_disable_planes(crtc);
12597 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012598 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012599 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012600
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012601 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12602 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012603 *
12604 * Note we'll need to fix this up when we start tracking multiple
12605 * pipes; here we assume a single modeset_pipe and only track the
12606 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012607 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012608 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012609 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012610
12611 /*
12612 * Calculate and store various constants which
12613 * are later needed by vblank and swap-completion
12614 * timestamping. They are derived from true hwmode.
12615 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012616 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012617 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012618 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012619
Daniel Vetterea9d7582012-07-10 10:42:52 +020012620 /* Only after disabling all output pipelines that will be changed can we
12621 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012622 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012623
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012624 /* The state has been swaped above, so state actually contains the
12625 * old state now. */
12626
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012627 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012628
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012629 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012630
12631 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012632 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012633 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012634 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012635
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012636 update_scanline_offset(to_intel_crtc(crtc));
12637
12638 dev_priv->display.crtc_enable(crtc);
12639 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012640 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012641
Daniel Vettera6778b32012-07-02 09:56:42 +020012642 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012643
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012644 drm_atomic_helper_cleanup_planes(dev, state);
12645
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012646 drm_atomic_state_free(state);
12647
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012648 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012649}
12650
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012651static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012652 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012653{
12654 int ret;
12655
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012656 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012657
12658 if (ret == 0)
12659 intel_modeset_check_state(crtc->dev);
12660
12661 return ret;
12662}
12663
Damien Lespiaue7457a92013-08-08 22:28:59 +010012664static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012665 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012666{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012667 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012668 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012669
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012670 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012671 if (IS_ERR(pipe_config)) {
12672 ret = PTR_ERR(pipe_config);
12673 goto out;
12674 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012675
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012676 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012677 if (ret)
12678 goto out;
12679
12680out:
12681 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012682}
12683
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012684void intel_crtc_restore_mode(struct drm_crtc *crtc)
12685{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012686 struct drm_device *dev = crtc->dev;
12687 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012688 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012689 struct intel_encoder *encoder;
12690 struct intel_connector *connector;
12691 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012692 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012693 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012694
12695 state = drm_atomic_state_alloc(dev);
12696 if (!state) {
12697 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12698 crtc->base.id);
12699 return;
12700 }
12701
12702 state->acquire_ctx = dev->mode_config.acquire_ctx;
12703
12704 /* The force restore path in the HW readout code relies on the staged
12705 * config still keeping the user requested config while the actual
12706 * state has been overwritten by the configuration read from HW. We
12707 * need to copy the staged config to the atomic state, otherwise the
12708 * mode set will just reapply the state the HW is already in. */
12709 for_each_intel_encoder(dev, encoder) {
12710 if (&encoder->new_crtc->base != crtc)
12711 continue;
12712
12713 for_each_intel_connector(dev, connector) {
12714 if (connector->new_encoder != encoder)
12715 continue;
12716
12717 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12718 if (IS_ERR(connector_state)) {
12719 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12720 connector->base.base.id,
12721 connector->base.name,
12722 PTR_ERR(connector_state));
12723 continue;
12724 }
12725
12726 connector_state->crtc = crtc;
12727 connector_state->best_encoder = &encoder->base;
12728 }
12729 }
12730
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012731 for_each_intel_crtc(dev, intel_crtc) {
12732 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12733 continue;
12734
12735 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12736 if (IS_ERR(crtc_state)) {
12737 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12738 intel_crtc->base.base.id,
12739 PTR_ERR(crtc_state));
12740 continue;
12741 }
12742
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012743 crtc_state->base.active = crtc_state->base.enable =
12744 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012745
12746 if (&intel_crtc->base == crtc)
12747 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012748 }
12749
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012750 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12751 crtc->primary->fb, crtc->x, crtc->y);
12752
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012753 ret = intel_set_mode(crtc, state);
12754 if (ret)
12755 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012756}
12757
Daniel Vetter25c5b262012-07-08 22:08:04 +020012758#undef for_each_intel_crtc_masked
12759
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012760static bool intel_connector_in_mode_set(struct intel_connector *connector,
12761 struct drm_mode_set *set)
12762{
12763 int ro;
12764
12765 for (ro = 0; ro < set->num_connectors; ro++)
12766 if (set->connectors[ro] == &connector->base)
12767 return true;
12768
12769 return false;
12770}
12771
Daniel Vetter2e431052012-07-04 22:42:15 +020012772static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012773intel_modeset_stage_output_state(struct drm_device *dev,
12774 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012775 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012776{
Daniel Vetter9a935852012-07-05 22:34:27 +020012777 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012778 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012779 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012780 struct drm_crtc *crtc;
12781 struct drm_crtc_state *crtc_state;
12782 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020012783
Damien Lespiau9abdda72013-02-13 13:29:23 +000012784 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012785 * of connectors. For paranoia, double-check this. */
12786 WARN_ON(!set->fb && (set->num_connectors != 0));
12787 WARN_ON(set->fb && (set->num_connectors == 0));
12788
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012789 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012790 bool in_mode_set = intel_connector_in_mode_set(connector, set);
12791
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012792 if (!in_mode_set && connector->base.state->crtc != set->crtc)
12793 continue;
12794
12795 connector_state =
12796 drm_atomic_get_connector_state(state, &connector->base);
12797 if (IS_ERR(connector_state))
12798 return PTR_ERR(connector_state);
12799
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012800 if (in_mode_set) {
12801 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012802 connector_state->best_encoder =
12803 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020012804 }
12805
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012806 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012807 continue;
12808
Daniel Vetter9a935852012-07-05 22:34:27 +020012809 /* If we disable the crtc, disable all its connectors. Also, if
12810 * the connector is on the changing crtc but not on the new
12811 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012812 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012813 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020012814
12815 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12816 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012817 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012818 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012819 }
12820 /* connector->new_encoder is now updated for all connectors. */
12821
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012822 for_each_connector_in_state(state, drm_connector, connector_state, i) {
12823 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020012824
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012825 if (!connector_state->best_encoder) {
12826 ret = drm_atomic_set_crtc_for_connector(connector_state,
12827 NULL);
12828 if (ret)
12829 return ret;
12830
Daniel Vetter50f56112012-07-02 09:35:43 +020012831 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012832 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012833
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012834 if (intel_connector_in_mode_set(connector, set)) {
12835 struct drm_crtc *crtc = connector->base.state->crtc;
12836
12837 /* If this connector was in a previous crtc, add it
12838 * to the state. We might need to disable it. */
12839 if (crtc) {
12840 crtc_state =
12841 drm_atomic_get_crtc_state(state, crtc);
12842 if (IS_ERR(crtc_state))
12843 return PTR_ERR(crtc_state);
12844 }
12845
12846 ret = drm_atomic_set_crtc_for_connector(connector_state,
12847 set->crtc);
12848 if (ret)
12849 return ret;
12850 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012851
12852 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012853 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
12854 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012855 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012856 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012857
Daniel Vetter9a935852012-07-05 22:34:27 +020012858 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12859 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012860 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012861 connector_state->crtc->base.id);
12862
12863 if (connector_state->best_encoder != &connector->encoder->base)
12864 connector->encoder =
12865 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020012866 }
12867
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012868 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012869 bool has_connectors;
12870
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012871 ret = drm_atomic_add_affected_connectors(state, crtc);
12872 if (ret)
12873 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012874
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012875 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
12876 if (has_connectors != crtc_state->enable)
12877 crtc_state->enable =
12878 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020012879 }
12880
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012881 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12882 set->fb, set->x, set->y);
12883 if (ret)
12884 return ret;
12885
12886 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
12887 if (IS_ERR(crtc_state))
12888 return PTR_ERR(crtc_state);
12889
12890 if (set->mode)
12891 drm_mode_copy(&crtc_state->mode, set->mode);
12892
12893 if (set->num_connectors)
12894 crtc_state->active = true;
12895
Daniel Vetter2e431052012-07-04 22:42:15 +020012896 return 0;
12897}
12898
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012899static bool primary_plane_visible(struct drm_crtc *crtc)
12900{
12901 struct intel_plane_state *plane_state =
12902 to_intel_plane_state(crtc->primary->state);
12903
12904 return plane_state->visible;
12905}
12906
Daniel Vetter2e431052012-07-04 22:42:15 +020012907static int intel_crtc_set_config(struct drm_mode_set *set)
12908{
12909 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012910 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012911 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012912 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020012913 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012914
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012915 BUG_ON(!set);
12916 BUG_ON(!set->crtc);
12917 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012918
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012919 /* Enforce sane interface api - has been abused by the fb helper. */
12920 BUG_ON(!set->mode && set->fb);
12921 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012922
Daniel Vetter2e431052012-07-04 22:42:15 +020012923 if (set->fb) {
12924 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12925 set->crtc->base.id, set->fb->base.id,
12926 (int)set->num_connectors, set->x, set->y);
12927 } else {
12928 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012929 }
12930
12931 dev = set->crtc->dev;
12932
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012933 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012934 if (!state)
12935 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012936
12937 state->acquire_ctx = dev->mode_config.acquire_ctx;
12938
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012939 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012940 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012941 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020012942
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012943 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080012944 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012945 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012946 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080012947 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012948
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012949 intel_update_pipe_size(to_intel_crtc(set->crtc));
12950
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012951 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012952
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012953 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012954
12955 if (ret == 0 &&
12956 pipe_config->base.enable &&
12957 pipe_config->base.planes_changed &&
12958 !needs_modeset(&pipe_config->base)) {
12959 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012960
12961 /*
12962 * We need to make sure the primary plane is re-enabled if it
12963 * has previously been turned off.
12964 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012965 if (ret == 0 && !primary_plane_was_visible &&
12966 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070012967 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012968 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012969 }
12970
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012971 /*
12972 * In the fastboot case this may be our only check of the
12973 * state after boot. It would be better to only do it on
12974 * the first update, but we don't have a nice way of doing that
12975 * (and really, set_config isn't used much for high freq page
12976 * flipping, so increasing its cost here shouldn't be a big
12977 * deal).
12978 */
Jani Nikulad330a952014-01-21 11:24:25 +020012979 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012980 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012981 }
12982
Chris Wilson2d05eae2013-05-03 17:36:25 +010012983 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012984 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12985 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010012986 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012987
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012988out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012989 if (ret)
12990 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020012991 return ret;
12992}
12993
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012994static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012995 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012996 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012997 .destroy = intel_crtc_destroy,
12998 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080012999 .atomic_duplicate_state = intel_crtc_duplicate_state,
13000 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013001};
13002
Daniel Vetter53589012013-06-05 13:34:16 +020013003static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13004 struct intel_shared_dpll *pll,
13005 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013006{
Daniel Vetter53589012013-06-05 13:34:16 +020013007 uint32_t val;
13008
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013009 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013010 return false;
13011
Daniel Vetter53589012013-06-05 13:34:16 +020013012 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013013 hw_state->dpll = val;
13014 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13015 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013016
13017 return val & DPLL_VCO_ENABLE;
13018}
13019
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013020static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13021 struct intel_shared_dpll *pll)
13022{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013023 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13024 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013025}
13026
Daniel Vettere7b903d2013-06-05 13:34:14 +020013027static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13028 struct intel_shared_dpll *pll)
13029{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013030 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013031 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013032
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013033 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013034
13035 /* Wait for the clocks to stabilize. */
13036 POSTING_READ(PCH_DPLL(pll->id));
13037 udelay(150);
13038
13039 /* The pixel multiplier can only be updated once the
13040 * DPLL is enabled and the clocks are stable.
13041 *
13042 * So write it again.
13043 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013044 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013045 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013046 udelay(200);
13047}
13048
13049static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13050 struct intel_shared_dpll *pll)
13051{
13052 struct drm_device *dev = dev_priv->dev;
13053 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013054
13055 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013056 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013057 if (intel_crtc_to_shared_dpll(crtc) == pll)
13058 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13059 }
13060
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013061 I915_WRITE(PCH_DPLL(pll->id), 0);
13062 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013063 udelay(200);
13064}
13065
Daniel Vetter46edb022013-06-05 13:34:12 +020013066static char *ibx_pch_dpll_names[] = {
13067 "PCH DPLL A",
13068 "PCH DPLL B",
13069};
13070
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013071static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013072{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013073 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013074 int i;
13075
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013076 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013077
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013078 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013079 dev_priv->shared_dplls[i].id = i;
13080 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013081 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013082 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13083 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013084 dev_priv->shared_dplls[i].get_hw_state =
13085 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013086 }
13087}
13088
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013089static void intel_shared_dpll_init(struct drm_device *dev)
13090{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013091 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013092
Daniel Vetter9cd86932014-06-25 22:01:57 +030013093 if (HAS_DDI(dev))
13094 intel_ddi_pll_init(dev);
13095 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013096 ibx_pch_dpll_init(dev);
13097 else
13098 dev_priv->num_shared_dpll = 0;
13099
13100 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013101}
13102
Matt Roper6beb8c232014-12-01 15:40:14 -080013103/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013104 * intel_wm_need_update - Check whether watermarks need updating
13105 * @plane: drm plane
13106 * @state: new plane state
13107 *
13108 * Check current plane state versus the new one to determine whether
13109 * watermarks need to be recalculated.
13110 *
13111 * Returns true or false.
13112 */
13113bool intel_wm_need_update(struct drm_plane *plane,
13114 struct drm_plane_state *state)
13115{
13116 /* Update watermarks on tiling changes. */
13117 if (!plane->state->fb || !state->fb ||
13118 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13119 plane->state->rotation != state->rotation)
13120 return true;
13121
13122 return false;
13123}
13124
13125/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013126 * intel_prepare_plane_fb - Prepare fb for usage on plane
13127 * @plane: drm plane to prepare for
13128 * @fb: framebuffer to prepare for presentation
13129 *
13130 * Prepares a framebuffer for usage on a display plane. Generally this
13131 * involves pinning the underlying object and updating the frontbuffer tracking
13132 * bits. Some older platforms need special physical address handling for
13133 * cursor planes.
13134 *
13135 * Returns 0 on success, negative error code on failure.
13136 */
13137int
13138intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013139 struct drm_framebuffer *fb,
13140 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013141{
13142 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013143 struct intel_plane *intel_plane = to_intel_plane(plane);
13144 enum pipe pipe = intel_plane->pipe;
13145 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13146 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13147 unsigned frontbuffer_bits = 0;
13148 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013149
Matt Roperea2c67b2014-12-23 10:41:52 -080013150 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013151 return 0;
13152
Matt Roper6beb8c232014-12-01 15:40:14 -080013153 switch (plane->type) {
13154 case DRM_PLANE_TYPE_PRIMARY:
13155 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13156 break;
13157 case DRM_PLANE_TYPE_CURSOR:
13158 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13159 break;
13160 case DRM_PLANE_TYPE_OVERLAY:
13161 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13162 break;
13163 }
Matt Roper465c1202014-05-29 08:06:54 -070013164
Matt Roper4c345742014-07-09 16:22:10 -070013165 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013166
Matt Roper6beb8c232014-12-01 15:40:14 -080013167 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13168 INTEL_INFO(dev)->cursor_needs_physical) {
13169 int align = IS_I830(dev) ? 16 * 1024 : 256;
13170 ret = i915_gem_object_attach_phys(obj, align);
13171 if (ret)
13172 DRM_DEBUG_KMS("failed to attach phys object\n");
13173 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013174 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013175 }
13176
13177 if (ret == 0)
13178 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13179
13180 mutex_unlock(&dev->struct_mutex);
13181
13182 return ret;
13183}
13184
Matt Roper38f3ce32014-12-02 07:45:25 -080013185/**
13186 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13187 * @plane: drm plane to clean up for
13188 * @fb: old framebuffer that was on plane
13189 *
13190 * Cleans up a framebuffer that has just been removed from a plane.
13191 */
13192void
13193intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013194 struct drm_framebuffer *fb,
13195 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013196{
13197 struct drm_device *dev = plane->dev;
13198 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13199
13200 if (WARN_ON(!obj))
13201 return;
13202
13203 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13204 !INTEL_INFO(dev)->cursor_needs_physical) {
13205 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013206 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013207 mutex_unlock(&dev->struct_mutex);
13208 }
Matt Roper465c1202014-05-29 08:06:54 -070013209}
13210
Chandra Konduru6156a452015-04-27 13:48:39 -070013211int
13212skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13213{
13214 int max_scale;
13215 struct drm_device *dev;
13216 struct drm_i915_private *dev_priv;
13217 int crtc_clock, cdclk;
13218
13219 if (!intel_crtc || !crtc_state)
13220 return DRM_PLANE_HELPER_NO_SCALING;
13221
13222 dev = intel_crtc->base.dev;
13223 dev_priv = dev->dev_private;
13224 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13225 cdclk = dev_priv->display.get_display_clock_speed(dev);
13226
13227 if (!crtc_clock || !cdclk)
13228 return DRM_PLANE_HELPER_NO_SCALING;
13229
13230 /*
13231 * skl max scale is lower of:
13232 * close to 3 but not 3, -1 is for that purpose
13233 * or
13234 * cdclk/crtc_clock
13235 */
13236 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13237
13238 return max_scale;
13239}
13240
Matt Roper465c1202014-05-29 08:06:54 -070013241static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013242intel_check_primary_plane(struct drm_plane *plane,
13243 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013244{
Matt Roper32b7eee2014-12-24 07:59:06 -080013245 struct drm_device *dev = plane->dev;
13246 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013247 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013248 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013249 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013250 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013251 struct drm_rect *dest = &state->dst;
13252 struct drm_rect *src = &state->src;
13253 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013254 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013255 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13256 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013257 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013258
Matt Roperea2c67b2014-12-23 10:41:52 -080013259 crtc = crtc ? crtc : plane->crtc;
13260 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013261 crtc_state = state->base.state ?
13262 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013263
Chandra Konduru6156a452015-04-27 13:48:39 -070013264 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013265 /* use scaler when colorkey is not required */
13266 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13267 min_scale = 1;
13268 max_scale = skl_max_scale(intel_crtc, crtc_state);
13269 }
Sonika Jindald8106362015-04-10 14:37:28 +053013270 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013271 }
Sonika Jindald8106362015-04-10 14:37:28 +053013272
Matt Roperc59cb172014-12-01 15:40:16 -080013273 ret = drm_plane_helper_check_update(plane, crtc, fb,
13274 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013275 min_scale,
13276 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013277 can_position, true,
13278 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013279 if (ret)
13280 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013281
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013282 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013283 struct intel_plane_state *old_state =
13284 to_intel_plane_state(plane->state);
13285
Matt Roper32b7eee2014-12-24 07:59:06 -080013286 intel_crtc->atomic.wait_for_flips = true;
13287
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013288 /*
13289 * FBC does not work on some platforms for rotated
13290 * planes, so disable it when rotation is not 0 and
13291 * update it when rotation is set back to 0.
13292 *
13293 * FIXME: This is redundant with the fbc update done in
13294 * the primary plane enable function except that that
13295 * one is done too late. We eventually need to unify
13296 * this.
13297 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013298 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013299 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013300 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013301 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013302 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013303 }
13304
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013305 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013306 /*
13307 * BDW signals flip done immediately if the plane
13308 * is disabled, even if the plane enable is already
13309 * armed to occur at the next vblank :(
13310 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013311 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013312 intel_crtc->atomic.wait_vblank = true;
13313 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013314
Matt Roper32b7eee2014-12-24 07:59:06 -080013315 intel_crtc->atomic.fb_bits |=
13316 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13317
13318 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013319
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013320 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013321 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013322 }
13323
Chandra Konduru6156a452015-04-27 13:48:39 -070013324 if (INTEL_INFO(dev)->gen >= 9) {
13325 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13326 to_intel_plane(plane), state, 0);
13327 if (ret)
13328 return ret;
13329 }
13330
Matt Roperc59cb172014-12-01 15:40:16 -080013331 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013332}
13333
Sonika Jindal48404c12014-08-22 14:06:04 +053013334static void
13335intel_commit_primary_plane(struct drm_plane *plane,
13336 struct intel_plane_state *state)
13337{
Matt Roper2b875c22014-12-01 15:40:13 -080013338 struct drm_crtc *crtc = state->base.crtc;
13339 struct drm_framebuffer *fb = state->base.fb;
13340 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013341 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013342 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013343 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013344
Matt Roperea2c67b2014-12-23 10:41:52 -080013345 crtc = crtc ? crtc : plane->crtc;
13346 intel_crtc = to_intel_crtc(crtc);
13347
Matt Ropercf4c7c12014-12-04 10:27:42 -080013348 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013349 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013350 crtc->y = src->y1 >> 16;
13351
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013352 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013353 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013354 /* FIXME: kill this fastboot hack */
13355 intel_update_pipe_size(intel_crtc);
13356
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013357 dev_priv->display.update_primary_plane(crtc, plane->fb,
13358 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013359 }
13360}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013361
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013362static void
13363intel_disable_primary_plane(struct drm_plane *plane,
13364 struct drm_crtc *crtc,
13365 bool force)
13366{
13367 struct drm_device *dev = plane->dev;
13368 struct drm_i915_private *dev_priv = dev->dev_private;
13369
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013370 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13371}
13372
Matt Roper32b7eee2014-12-24 07:59:06 -080013373static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13374{
13375 struct drm_device *dev = crtc->dev;
13376 struct drm_i915_private *dev_priv = dev->dev_private;
13377 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013378 struct intel_plane *intel_plane;
13379 struct drm_plane *p;
13380 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013381
Matt Roperea2c67b2014-12-23 10:41:52 -080013382 /* Track fb's for any planes being disabled */
13383 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13384 intel_plane = to_intel_plane(p);
13385
13386 if (intel_crtc->atomic.disabled_planes &
13387 (1 << drm_plane_index(p))) {
13388 switch (p->type) {
13389 case DRM_PLANE_TYPE_PRIMARY:
13390 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13391 break;
13392 case DRM_PLANE_TYPE_CURSOR:
13393 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13394 break;
13395 case DRM_PLANE_TYPE_OVERLAY:
13396 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13397 break;
13398 }
13399
13400 mutex_lock(&dev->struct_mutex);
13401 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13402 mutex_unlock(&dev->struct_mutex);
13403 }
13404 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013405
Matt Roper32b7eee2014-12-24 07:59:06 -080013406 if (intel_crtc->atomic.wait_for_flips)
13407 intel_crtc_wait_for_pending_flips(crtc);
13408
13409 if (intel_crtc->atomic.disable_fbc)
13410 intel_fbc_disable(dev);
13411
13412 if (intel_crtc->atomic.pre_disable_primary)
13413 intel_pre_disable_primary(crtc);
13414
13415 if (intel_crtc->atomic.update_wm)
13416 intel_update_watermarks(crtc);
13417
13418 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013419
13420 /* Perform vblank evasion around commit operation */
13421 if (intel_crtc->active)
13422 intel_crtc->atomic.evade =
13423 intel_pipe_update_start(intel_crtc,
13424 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013425}
13426
13427static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13428{
13429 struct drm_device *dev = crtc->dev;
13430 struct drm_i915_private *dev_priv = dev->dev_private;
13431 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13432 struct drm_plane *p;
13433
Matt Roperc34c9ee2014-12-23 10:41:50 -080013434 if (intel_crtc->atomic.evade)
13435 intel_pipe_update_end(intel_crtc,
13436 intel_crtc->atomic.start_vbl_count);
13437
Matt Roper32b7eee2014-12-24 07:59:06 -080013438 intel_runtime_pm_put(dev_priv);
13439
13440 if (intel_crtc->atomic.wait_vblank)
13441 intel_wait_for_vblank(dev, intel_crtc->pipe);
13442
13443 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13444
13445 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013446 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013447 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013448 mutex_unlock(&dev->struct_mutex);
13449 }
Matt Roper465c1202014-05-29 08:06:54 -070013450
Matt Roper32b7eee2014-12-24 07:59:06 -080013451 if (intel_crtc->atomic.post_enable_primary)
13452 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013453
Matt Roper32b7eee2014-12-24 07:59:06 -080013454 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13455 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13456 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13457 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013458
Matt Roper32b7eee2014-12-24 07:59:06 -080013459 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013460}
13461
Matt Ropercf4c7c12014-12-04 10:27:42 -080013462/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013463 * intel_plane_destroy - destroy a plane
13464 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013465 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013466 * Common destruction function for all types of planes (primary, cursor,
13467 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013468 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013469void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013470{
13471 struct intel_plane *intel_plane = to_intel_plane(plane);
13472 drm_plane_cleanup(plane);
13473 kfree(intel_plane);
13474}
13475
Matt Roper65a3fea2015-01-21 16:35:42 -080013476const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013477 .update_plane = drm_atomic_helper_update_plane,
13478 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013479 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013480 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013481 .atomic_get_property = intel_plane_atomic_get_property,
13482 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013483 .atomic_duplicate_state = intel_plane_duplicate_state,
13484 .atomic_destroy_state = intel_plane_destroy_state,
13485
Matt Roper465c1202014-05-29 08:06:54 -070013486};
13487
13488static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13489 int pipe)
13490{
13491 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013492 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013493 const uint32_t *intel_primary_formats;
13494 int num_formats;
13495
13496 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13497 if (primary == NULL)
13498 return NULL;
13499
Matt Roper8e7d6882015-01-21 16:35:41 -080013500 state = intel_create_plane_state(&primary->base);
13501 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013502 kfree(primary);
13503 return NULL;
13504 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013505 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013506
Matt Roper465c1202014-05-29 08:06:54 -070013507 primary->can_scale = false;
13508 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013509 if (INTEL_INFO(dev)->gen >= 9) {
13510 primary->can_scale = true;
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013511 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013512 }
Matt Roper465c1202014-05-29 08:06:54 -070013513 primary->pipe = pipe;
13514 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013515 primary->check_plane = intel_check_primary_plane;
13516 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013517 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013518 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013519 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13520 primary->plane = !pipe;
13521
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013522 if (INTEL_INFO(dev)->gen >= 9) {
13523 intel_primary_formats = skl_primary_formats;
13524 num_formats = ARRAY_SIZE(skl_primary_formats);
13525 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013526 intel_primary_formats = i965_primary_formats;
13527 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013528 } else {
13529 intel_primary_formats = i8xx_primary_formats;
13530 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013531 }
13532
13533 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013534 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013535 intel_primary_formats, num_formats,
13536 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013537
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013538 if (INTEL_INFO(dev)->gen >= 4)
13539 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013540
Matt Roperea2c67b2014-12-23 10:41:52 -080013541 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13542
Matt Roper465c1202014-05-29 08:06:54 -070013543 return &primary->base;
13544}
13545
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013546void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13547{
13548 if (!dev->mode_config.rotation_property) {
13549 unsigned long flags = BIT(DRM_ROTATE_0) |
13550 BIT(DRM_ROTATE_180);
13551
13552 if (INTEL_INFO(dev)->gen >= 9)
13553 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13554
13555 dev->mode_config.rotation_property =
13556 drm_mode_create_rotation_property(dev, flags);
13557 }
13558 if (dev->mode_config.rotation_property)
13559 drm_object_attach_property(&plane->base.base,
13560 dev->mode_config.rotation_property,
13561 plane->base.state->rotation);
13562}
13563
Matt Roper3d7d6512014-06-10 08:28:13 -070013564static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013565intel_check_cursor_plane(struct drm_plane *plane,
13566 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013567{
Matt Roper2b875c22014-12-01 15:40:13 -080013568 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013569 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013570 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013571 struct drm_rect *dest = &state->dst;
13572 struct drm_rect *src = &state->src;
13573 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013574 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013575 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013576 unsigned stride;
13577 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013578
Matt Roperea2c67b2014-12-23 10:41:52 -080013579 crtc = crtc ? crtc : plane->crtc;
13580 intel_crtc = to_intel_crtc(crtc);
13581
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013582 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013583 src, dest, clip,
13584 DRM_PLANE_HELPER_NO_SCALING,
13585 DRM_PLANE_HELPER_NO_SCALING,
13586 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013587 if (ret)
13588 return ret;
13589
13590
13591 /* if we want to turn off the cursor ignore width and height */
13592 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013593 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013594
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013595 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013596 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13597 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13598 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013599 return -EINVAL;
13600 }
13601
Matt Roperea2c67b2014-12-23 10:41:52 -080013602 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13603 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013604 DRM_DEBUG_KMS("buffer is too small\n");
13605 return -ENOMEM;
13606 }
13607
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013608 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013609 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13610 ret = -EINVAL;
13611 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013612
Matt Roper32b7eee2014-12-24 07:59:06 -080013613finish:
13614 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013615 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013616 intel_crtc->atomic.update_wm = true;
13617
13618 intel_crtc->atomic.fb_bits |=
13619 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13620 }
13621
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013622 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013623}
13624
Matt Roperf4a2cf22014-12-01 15:40:12 -080013625static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013626intel_disable_cursor_plane(struct drm_plane *plane,
13627 struct drm_crtc *crtc,
13628 bool force)
13629{
13630 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13631
13632 if (!force) {
13633 plane->fb = NULL;
13634 intel_crtc->cursor_bo = NULL;
13635 intel_crtc->cursor_addr = 0;
13636 }
13637
13638 intel_crtc_update_cursor(crtc, false);
13639}
13640
13641static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013642intel_commit_cursor_plane(struct drm_plane *plane,
13643 struct intel_plane_state *state)
13644{
Matt Roper2b875c22014-12-01 15:40:13 -080013645 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013646 struct drm_device *dev = plane->dev;
13647 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013648 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013649 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013650
Matt Roperea2c67b2014-12-23 10:41:52 -080013651 crtc = crtc ? crtc : plane->crtc;
13652 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013653
Matt Roperea2c67b2014-12-23 10:41:52 -080013654 plane->fb = state->base.fb;
13655 crtc->cursor_x = state->base.crtc_x;
13656 crtc->cursor_y = state->base.crtc_y;
13657
Gustavo Padovana912f122014-12-01 15:40:10 -080013658 if (intel_crtc->cursor_bo == obj)
13659 goto update;
13660
Matt Roperf4a2cf22014-12-01 15:40:12 -080013661 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013662 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013663 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013664 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013665 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013666 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013667
Gustavo Padovana912f122014-12-01 15:40:10 -080013668 intel_crtc->cursor_addr = addr;
13669 intel_crtc->cursor_bo = obj;
13670update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013671
Matt Roper32b7eee2014-12-24 07:59:06 -080013672 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013673 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013674}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013675
Matt Roper3d7d6512014-06-10 08:28:13 -070013676static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13677 int pipe)
13678{
13679 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013680 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013681
13682 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13683 if (cursor == NULL)
13684 return NULL;
13685
Matt Roper8e7d6882015-01-21 16:35:41 -080013686 state = intel_create_plane_state(&cursor->base);
13687 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013688 kfree(cursor);
13689 return NULL;
13690 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013691 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013692
Matt Roper3d7d6512014-06-10 08:28:13 -070013693 cursor->can_scale = false;
13694 cursor->max_downscale = 1;
13695 cursor->pipe = pipe;
13696 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013697 cursor->check_plane = intel_check_cursor_plane;
13698 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013699 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013700
13701 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013702 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013703 intel_cursor_formats,
13704 ARRAY_SIZE(intel_cursor_formats),
13705 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013706
13707 if (INTEL_INFO(dev)->gen >= 4) {
13708 if (!dev->mode_config.rotation_property)
13709 dev->mode_config.rotation_property =
13710 drm_mode_create_rotation_property(dev,
13711 BIT(DRM_ROTATE_0) |
13712 BIT(DRM_ROTATE_180));
13713 if (dev->mode_config.rotation_property)
13714 drm_object_attach_property(&cursor->base.base,
13715 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013716 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013717 }
13718
Chandra Konduruaf99ceda2015-05-11 14:35:47 -070013719 if (INTEL_INFO(dev)->gen >=9)
13720 state->scaler_id = -1;
13721
Matt Roperea2c67b2014-12-23 10:41:52 -080013722 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13723
Matt Roper3d7d6512014-06-10 08:28:13 -070013724 return &cursor->base;
13725}
13726
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013727static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13728 struct intel_crtc_state *crtc_state)
13729{
13730 int i;
13731 struct intel_scaler *intel_scaler;
13732 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13733
13734 for (i = 0; i < intel_crtc->num_scalers; i++) {
13735 intel_scaler = &scaler_state->scalers[i];
13736 intel_scaler->in_use = 0;
13737 intel_scaler->id = i;
13738
13739 intel_scaler->mode = PS_SCALER_MODE_DYN;
13740 }
13741
13742 scaler_state->scaler_id = -1;
13743}
13744
Hannes Ederb358d0a2008-12-18 21:18:47 +010013745static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013746{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013747 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013748 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013749 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013750 struct drm_plane *primary = NULL;
13751 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013752 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013753
Daniel Vetter955382f2013-09-19 14:05:45 +020013754 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013755 if (intel_crtc == NULL)
13756 return;
13757
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013758 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13759 if (!crtc_state)
13760 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013761 intel_crtc->config = crtc_state;
13762 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013763 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013764
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013765 /* initialize shared scalers */
13766 if (INTEL_INFO(dev)->gen >= 9) {
13767 if (pipe == PIPE_C)
13768 intel_crtc->num_scalers = 1;
13769 else
13770 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13771
13772 skl_init_scalers(dev, intel_crtc, crtc_state);
13773 }
13774
Matt Roper465c1202014-05-29 08:06:54 -070013775 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013776 if (!primary)
13777 goto fail;
13778
13779 cursor = intel_cursor_plane_create(dev, pipe);
13780 if (!cursor)
13781 goto fail;
13782
Matt Roper465c1202014-05-29 08:06:54 -070013783 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013784 cursor, &intel_crtc_funcs);
13785 if (ret)
13786 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013787
13788 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013789 for (i = 0; i < 256; i++) {
13790 intel_crtc->lut_r[i] = i;
13791 intel_crtc->lut_g[i] = i;
13792 intel_crtc->lut_b[i] = i;
13793 }
13794
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013795 /*
13796 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013797 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013798 */
Jesse Barnes80824002009-09-10 15:28:06 -070013799 intel_crtc->pipe = pipe;
13800 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013801 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013802 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013803 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013804 }
13805
Chris Wilson4b0e3332014-05-30 16:35:26 +030013806 intel_crtc->cursor_base = ~0;
13807 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013808 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013809
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013810 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13811 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13812 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13813 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13814
Jesse Barnes79e53942008-11-07 14:24:08 -080013815 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013816
13817 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013818 return;
13819
13820fail:
13821 if (primary)
13822 drm_plane_cleanup(primary);
13823 if (cursor)
13824 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013825 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013826 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013827}
13828
Jesse Barnes752aa882013-10-31 18:55:49 +020013829enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13830{
13831 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013832 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013833
Rob Clark51fd3712013-11-19 12:10:12 -050013834 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013835
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013836 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013837 return INVALID_PIPE;
13838
13839 return to_intel_crtc(encoder->crtc)->pipe;
13840}
13841
Carl Worth08d7b3d2009-04-29 14:43:54 -070013842int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013843 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013844{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013845 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013846 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013847 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013848
Rob Clark7707e652014-07-17 23:30:04 -040013849 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013850
Rob Clark7707e652014-07-17 23:30:04 -040013851 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013852 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013853 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013854 }
13855
Rob Clark7707e652014-07-17 23:30:04 -040013856 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013857 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013858
Daniel Vetterc05422d2009-08-11 16:05:30 +020013859 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013860}
13861
Daniel Vetter66a92782012-07-12 20:08:18 +020013862static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013863{
Daniel Vetter66a92782012-07-12 20:08:18 +020013864 struct drm_device *dev = encoder->base.dev;
13865 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013866 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013867 int entry = 0;
13868
Damien Lespiaub2784e12014-08-05 11:29:37 +010013869 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013870 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013871 index_mask |= (1 << entry);
13872
Jesse Barnes79e53942008-11-07 14:24:08 -080013873 entry++;
13874 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013875
Jesse Barnes79e53942008-11-07 14:24:08 -080013876 return index_mask;
13877}
13878
Chris Wilson4d302442010-12-14 19:21:29 +000013879static bool has_edp_a(struct drm_device *dev)
13880{
13881 struct drm_i915_private *dev_priv = dev->dev_private;
13882
13883 if (!IS_MOBILE(dev))
13884 return false;
13885
13886 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13887 return false;
13888
Damien Lespiaue3589902014-02-07 19:12:50 +000013889 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013890 return false;
13891
13892 return true;
13893}
13894
Jesse Barnes84b4e042014-06-25 08:24:29 -070013895static bool intel_crt_present(struct drm_device *dev)
13896{
13897 struct drm_i915_private *dev_priv = dev->dev_private;
13898
Damien Lespiau884497e2013-12-03 13:56:23 +000013899 if (INTEL_INFO(dev)->gen >= 9)
13900 return false;
13901
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013902 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013903 return false;
13904
13905 if (IS_CHERRYVIEW(dev))
13906 return false;
13907
13908 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13909 return false;
13910
13911 return true;
13912}
13913
Jesse Barnes79e53942008-11-07 14:24:08 -080013914static void intel_setup_outputs(struct drm_device *dev)
13915{
Eric Anholt725e30a2009-01-22 13:01:02 -080013916 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013917 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013918 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013919
Daniel Vetterc9093352013-06-06 22:22:47 +020013920 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013921
Jesse Barnes84b4e042014-06-25 08:24:29 -070013922 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013923 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013924
Vandana Kannanc776eb22014-08-19 12:05:01 +053013925 if (IS_BROXTON(dev)) {
13926 /*
13927 * FIXME: Broxton doesn't support port detection via the
13928 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13929 * detect the ports.
13930 */
13931 intel_ddi_init(dev, PORT_A);
13932 intel_ddi_init(dev, PORT_B);
13933 intel_ddi_init(dev, PORT_C);
13934 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013935 int found;
13936
Jesse Barnesde31fac2015-03-06 15:53:32 -080013937 /*
13938 * Haswell uses DDI functions to detect digital outputs.
13939 * On SKL pre-D0 the strap isn't connected, so we assume
13940 * it's there.
13941 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013942 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013943 /* WaIgnoreDDIAStrap: skl */
13944 if (found ||
13945 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013946 intel_ddi_init(dev, PORT_A);
13947
13948 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13949 * register */
13950 found = I915_READ(SFUSE_STRAP);
13951
13952 if (found & SFUSE_STRAP_DDIB_DETECTED)
13953 intel_ddi_init(dev, PORT_B);
13954 if (found & SFUSE_STRAP_DDIC_DETECTED)
13955 intel_ddi_init(dev, PORT_C);
13956 if (found & SFUSE_STRAP_DDID_DETECTED)
13957 intel_ddi_init(dev, PORT_D);
13958 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013959 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013960 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013961
13962 if (has_edp_a(dev))
13963 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013964
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013965 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013966 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013967 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013968 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013969 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013970 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013971 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013972 }
13973
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013974 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013975 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013976
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013977 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013978 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013979
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013980 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013981 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013982
Daniel Vetter270b3042012-10-27 15:52:05 +020013983 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013984 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013985 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013986 /*
13987 * The DP_DETECTED bit is the latched state of the DDC
13988 * SDA pin at boot. However since eDP doesn't require DDC
13989 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13990 * eDP ports may have been muxed to an alternate function.
13991 * Thus we can't rely on the DP_DETECTED bit alone to detect
13992 * eDP ports. Consult the VBT as well as DP_DETECTED to
13993 * detect eDP ports.
13994 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013995 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13996 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030013997 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
13998 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013999 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14000 intel_dp_is_edp(dev, PORT_B))
14001 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014002
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014003 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14004 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014005 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14006 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014007 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14008 intel_dp_is_edp(dev, PORT_C))
14009 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014010
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014011 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014012 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014013 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14014 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014015 /* eDP not supported on port D, so don't check VBT */
14016 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14017 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014018 }
14019
Jani Nikula3cfca972013-08-27 15:12:26 +030014020 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014021 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014022 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014023
Paulo Zanonie2debe92013-02-18 19:00:27 -030014024 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014025 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014026 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014027 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14028 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014029 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014030 }
Ma Ling27185ae2009-08-24 13:50:23 +080014031
Imre Deake7281ea2013-05-08 13:14:08 +030014032 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014033 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014034 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014035
14036 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014037
Paulo Zanonie2debe92013-02-18 19:00:27 -030014038 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014039 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014040 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014041 }
Ma Ling27185ae2009-08-24 13:50:23 +080014042
Paulo Zanonie2debe92013-02-18 19:00:27 -030014043 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014044
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014045 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14046 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014047 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014048 }
Imre Deake7281ea2013-05-08 13:14:08 +030014049 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014050 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014051 }
Ma Ling27185ae2009-08-24 13:50:23 +080014052
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014053 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014054 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014055 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014056 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014057 intel_dvo_init(dev);
14058
Zhenyu Wang103a1962009-11-27 11:44:36 +080014059 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014060 intel_tv_init(dev);
14061
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014062 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014063
Damien Lespiaub2784e12014-08-05 11:29:37 +010014064 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014065 encoder->base.possible_crtcs = encoder->crtc_mask;
14066 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014067 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014068 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014069
Paulo Zanonidde86e22012-12-01 12:04:25 -020014070 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014071
14072 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014073}
14074
14075static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14076{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014077 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014078 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014079
Daniel Vetteref2d6332014-02-10 18:00:38 +010014080 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014081 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014082 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014083 drm_gem_object_unreference(&intel_fb->obj->base);
14084 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014085 kfree(intel_fb);
14086}
14087
14088static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014089 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014090 unsigned int *handle)
14091{
14092 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014093 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014094
Chris Wilson05394f32010-11-08 19:18:58 +000014095 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014096}
14097
14098static const struct drm_framebuffer_funcs intel_fb_funcs = {
14099 .destroy = intel_user_framebuffer_destroy,
14100 .create_handle = intel_user_framebuffer_create_handle,
14101};
14102
Damien Lespiaub3218032015-02-27 11:15:18 +000014103static
14104u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14105 uint32_t pixel_format)
14106{
14107 u32 gen = INTEL_INFO(dev)->gen;
14108
14109 if (gen >= 9) {
14110 /* "The stride in bytes must not exceed the of the size of 8K
14111 * pixels and 32K bytes."
14112 */
14113 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14114 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14115 return 32*1024;
14116 } else if (gen >= 4) {
14117 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14118 return 16*1024;
14119 else
14120 return 32*1024;
14121 } else if (gen >= 3) {
14122 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14123 return 8*1024;
14124 else
14125 return 16*1024;
14126 } else {
14127 /* XXX DSPC is limited to 4k tiled */
14128 return 8*1024;
14129 }
14130}
14131
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014132static int intel_framebuffer_init(struct drm_device *dev,
14133 struct intel_framebuffer *intel_fb,
14134 struct drm_mode_fb_cmd2 *mode_cmd,
14135 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014136{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014137 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014138 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014139 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014140
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014141 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14142
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014143 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14144 /* Enforce that fb modifier and tiling mode match, but only for
14145 * X-tiled. This is needed for FBC. */
14146 if (!!(obj->tiling_mode == I915_TILING_X) !=
14147 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14148 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14149 return -EINVAL;
14150 }
14151 } else {
14152 if (obj->tiling_mode == I915_TILING_X)
14153 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14154 else if (obj->tiling_mode == I915_TILING_Y) {
14155 DRM_DEBUG("No Y tiling for legacy addfb\n");
14156 return -EINVAL;
14157 }
14158 }
14159
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014160 /* Passed in modifier sanity checking. */
14161 switch (mode_cmd->modifier[0]) {
14162 case I915_FORMAT_MOD_Y_TILED:
14163 case I915_FORMAT_MOD_Yf_TILED:
14164 if (INTEL_INFO(dev)->gen < 9) {
14165 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14166 mode_cmd->modifier[0]);
14167 return -EINVAL;
14168 }
14169 case DRM_FORMAT_MOD_NONE:
14170 case I915_FORMAT_MOD_X_TILED:
14171 break;
14172 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014173 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14174 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014175 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014176 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014177
Damien Lespiaub3218032015-02-27 11:15:18 +000014178 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14179 mode_cmd->pixel_format);
14180 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14181 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14182 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014183 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014184 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014185
Damien Lespiaub3218032015-02-27 11:15:18 +000014186 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14187 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014188 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014189 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14190 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014191 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014192 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014193 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014194 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014195
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014196 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014197 mode_cmd->pitches[0] != obj->stride) {
14198 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14199 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014200 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014201 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014202
Ville Syrjälä57779d02012-10-31 17:50:14 +020014203 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014204 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014205 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014206 case DRM_FORMAT_RGB565:
14207 case DRM_FORMAT_XRGB8888:
14208 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014209 break;
14210 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014211 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014212 DRM_DEBUG("unsupported pixel format: %s\n",
14213 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014214 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014215 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014216 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014217 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014218 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14219 DRM_DEBUG("unsupported pixel format: %s\n",
14220 drm_get_format_name(mode_cmd->pixel_format));
14221 return -EINVAL;
14222 }
14223 break;
14224 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014225 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014226 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014227 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014228 DRM_DEBUG("unsupported pixel format: %s\n",
14229 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014230 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014231 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014232 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014233 case DRM_FORMAT_ABGR2101010:
14234 if (!IS_VALLEYVIEW(dev)) {
14235 DRM_DEBUG("unsupported pixel format: %s\n",
14236 drm_get_format_name(mode_cmd->pixel_format));
14237 return -EINVAL;
14238 }
14239 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014240 case DRM_FORMAT_YUYV:
14241 case DRM_FORMAT_UYVY:
14242 case DRM_FORMAT_YVYU:
14243 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014244 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014245 DRM_DEBUG("unsupported pixel format: %s\n",
14246 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014247 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014248 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014249 break;
14250 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014251 DRM_DEBUG("unsupported pixel format: %s\n",
14252 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014253 return -EINVAL;
14254 }
14255
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014256 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14257 if (mode_cmd->offsets[0] != 0)
14258 return -EINVAL;
14259
Damien Lespiauec2c9812015-01-20 12:51:45 +000014260 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014261 mode_cmd->pixel_format,
14262 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014263 /* FIXME drm helper for size checks (especially planar formats)? */
14264 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14265 return -EINVAL;
14266
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014267 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14268 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014269 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014270
Jesse Barnes79e53942008-11-07 14:24:08 -080014271 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14272 if (ret) {
14273 DRM_ERROR("framebuffer init failed %d\n", ret);
14274 return ret;
14275 }
14276
Jesse Barnes79e53942008-11-07 14:24:08 -080014277 return 0;
14278}
14279
Jesse Barnes79e53942008-11-07 14:24:08 -080014280static struct drm_framebuffer *
14281intel_user_framebuffer_create(struct drm_device *dev,
14282 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014283 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014284{
Chris Wilson05394f32010-11-08 19:18:58 +000014285 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014286
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014287 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14288 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014289 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014290 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014291
Chris Wilsond2dff872011-04-19 08:36:26 +010014292 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014293}
14294
Daniel Vetter4520f532013-10-09 09:18:51 +020014295#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014296static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014297{
14298}
14299#endif
14300
Jesse Barnes79e53942008-11-07 14:24:08 -080014301static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014302 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014303 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014304 .atomic_check = intel_atomic_check,
14305 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014306};
14307
Jesse Barnese70236a2009-09-21 10:42:27 -070014308/* Set up chip specific display functions */
14309static void intel_init_display(struct drm_device *dev)
14310{
14311 struct drm_i915_private *dev_priv = dev->dev_private;
14312
Daniel Vetteree9300b2013-06-03 22:40:22 +020014313 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14314 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014315 else if (IS_CHERRYVIEW(dev))
14316 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014317 else if (IS_VALLEYVIEW(dev))
14318 dev_priv->display.find_dpll = vlv_find_best_dpll;
14319 else if (IS_PINEVIEW(dev))
14320 dev_priv->display.find_dpll = pnv_find_best_dpll;
14321 else
14322 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14323
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014324 if (INTEL_INFO(dev)->gen >= 9) {
14325 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014326 dev_priv->display.get_initial_plane_config =
14327 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014328 dev_priv->display.crtc_compute_clock =
14329 haswell_crtc_compute_clock;
14330 dev_priv->display.crtc_enable = haswell_crtc_enable;
14331 dev_priv->display.crtc_disable = haswell_crtc_disable;
14332 dev_priv->display.off = ironlake_crtc_off;
14333 dev_priv->display.update_primary_plane =
14334 skylake_update_primary_plane;
14335 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014336 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014337 dev_priv->display.get_initial_plane_config =
14338 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014339 dev_priv->display.crtc_compute_clock =
14340 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014341 dev_priv->display.crtc_enable = haswell_crtc_enable;
14342 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014343 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014344 dev_priv->display.update_primary_plane =
14345 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014346 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014347 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014348 dev_priv->display.get_initial_plane_config =
14349 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014350 dev_priv->display.crtc_compute_clock =
14351 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014352 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14353 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014354 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014355 dev_priv->display.update_primary_plane =
14356 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014357 } else if (IS_VALLEYVIEW(dev)) {
14358 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014359 dev_priv->display.get_initial_plane_config =
14360 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014361 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014362 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14363 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14364 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014365 dev_priv->display.update_primary_plane =
14366 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014367 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014368 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014369 dev_priv->display.get_initial_plane_config =
14370 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014371 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014372 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14373 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014374 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014375 dev_priv->display.update_primary_plane =
14376 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014377 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014378
Jesse Barnese70236a2009-09-21 10:42:27 -070014379 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014380 if (IS_SKYLAKE(dev))
14381 dev_priv->display.get_display_clock_speed =
14382 skylake_get_display_clock_speed;
14383 else if (IS_BROADWELL(dev))
14384 dev_priv->display.get_display_clock_speed =
14385 broadwell_get_display_clock_speed;
14386 else if (IS_HASWELL(dev))
14387 dev_priv->display.get_display_clock_speed =
14388 haswell_get_display_clock_speed;
14389 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014390 dev_priv->display.get_display_clock_speed =
14391 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014392 else if (IS_GEN5(dev))
14393 dev_priv->display.get_display_clock_speed =
14394 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014395 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14396 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014397 dev_priv->display.get_display_clock_speed =
14398 i945_get_display_clock_speed;
14399 else if (IS_I915G(dev))
14400 dev_priv->display.get_display_clock_speed =
14401 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014402 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014403 dev_priv->display.get_display_clock_speed =
14404 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014405 else if (IS_PINEVIEW(dev))
14406 dev_priv->display.get_display_clock_speed =
14407 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014408 else if (IS_I915GM(dev))
14409 dev_priv->display.get_display_clock_speed =
14410 i915gm_get_display_clock_speed;
14411 else if (IS_I865G(dev))
14412 dev_priv->display.get_display_clock_speed =
14413 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014414 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014415 dev_priv->display.get_display_clock_speed =
14416 i855_get_display_clock_speed;
14417 else /* 852, 830 */
14418 dev_priv->display.get_display_clock_speed =
14419 i830_get_display_clock_speed;
14420
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014421 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014422 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014423 } else if (IS_GEN6(dev)) {
14424 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014425 } else if (IS_IVYBRIDGE(dev)) {
14426 /* FIXME: detect B0+ stepping and use auto training */
14427 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014428 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014429 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014430 } else if (IS_VALLEYVIEW(dev)) {
14431 dev_priv->display.modeset_global_resources =
14432 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014433 } else if (IS_BROXTON(dev)) {
14434 dev_priv->display.modeset_global_resources =
14435 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014436 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014437
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014438 switch (INTEL_INFO(dev)->gen) {
14439 case 2:
14440 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14441 break;
14442
14443 case 3:
14444 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14445 break;
14446
14447 case 4:
14448 case 5:
14449 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14450 break;
14451
14452 case 6:
14453 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14454 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014455 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014456 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014457 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14458 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014459 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014460 /* Drop through - unsupported since execlist only. */
14461 default:
14462 /* Default just returns -ENODEV to indicate unsupported */
14463 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014464 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014465
14466 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014467
14468 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014469}
14470
Jesse Barnesb690e962010-07-19 13:53:12 -070014471/*
14472 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14473 * resume, or other times. This quirk makes sure that's the case for
14474 * affected systems.
14475 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014476static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014477{
14478 struct drm_i915_private *dev_priv = dev->dev_private;
14479
14480 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014481 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014482}
14483
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014484static void quirk_pipeb_force(struct drm_device *dev)
14485{
14486 struct drm_i915_private *dev_priv = dev->dev_private;
14487
14488 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14489 DRM_INFO("applying pipe b force quirk\n");
14490}
14491
Keith Packard435793d2011-07-12 14:56:22 -070014492/*
14493 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14494 */
14495static void quirk_ssc_force_disable(struct drm_device *dev)
14496{
14497 struct drm_i915_private *dev_priv = dev->dev_private;
14498 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014499 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014500}
14501
Carsten Emde4dca20e2012-03-15 15:56:26 +010014502/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014503 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14504 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014505 */
14506static void quirk_invert_brightness(struct drm_device *dev)
14507{
14508 struct drm_i915_private *dev_priv = dev->dev_private;
14509 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014510 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014511}
14512
Scot Doyle9c72cc62014-07-03 23:27:50 +000014513/* Some VBT's incorrectly indicate no backlight is present */
14514static void quirk_backlight_present(struct drm_device *dev)
14515{
14516 struct drm_i915_private *dev_priv = dev->dev_private;
14517 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14518 DRM_INFO("applying backlight present quirk\n");
14519}
14520
Jesse Barnesb690e962010-07-19 13:53:12 -070014521struct intel_quirk {
14522 int device;
14523 int subsystem_vendor;
14524 int subsystem_device;
14525 void (*hook)(struct drm_device *dev);
14526};
14527
Egbert Eich5f85f172012-10-14 15:46:38 +020014528/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14529struct intel_dmi_quirk {
14530 void (*hook)(struct drm_device *dev);
14531 const struct dmi_system_id (*dmi_id_list)[];
14532};
14533
14534static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14535{
14536 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14537 return 1;
14538}
14539
14540static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14541 {
14542 .dmi_id_list = &(const struct dmi_system_id[]) {
14543 {
14544 .callback = intel_dmi_reverse_brightness,
14545 .ident = "NCR Corporation",
14546 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14547 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14548 },
14549 },
14550 { } /* terminating entry */
14551 },
14552 .hook = quirk_invert_brightness,
14553 },
14554};
14555
Ben Widawskyc43b5632012-04-16 14:07:40 -070014556static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014557 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14558 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14559
Jesse Barnesb690e962010-07-19 13:53:12 -070014560 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14561 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14562
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014563 /* 830 needs to leave pipe A & dpll A up */
14564 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14565
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014566 /* 830 needs to leave pipe B & dpll B up */
14567 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14568
Keith Packard435793d2011-07-12 14:56:22 -070014569 /* Lenovo U160 cannot use SSC on LVDS */
14570 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014571
14572 /* Sony Vaio Y cannot use SSC on LVDS */
14573 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014574
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014575 /* Acer Aspire 5734Z must invert backlight brightness */
14576 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14577
14578 /* Acer/eMachines G725 */
14579 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14580
14581 /* Acer/eMachines e725 */
14582 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14583
14584 /* Acer/Packard Bell NCL20 */
14585 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14586
14587 /* Acer Aspire 4736Z */
14588 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014589
14590 /* Acer Aspire 5336 */
14591 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014592
14593 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14594 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014595
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014596 /* Acer C720 Chromebook (Core i3 4005U) */
14597 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14598
jens steinb2a96012014-10-28 20:25:53 +010014599 /* Apple Macbook 2,1 (Core 2 T7400) */
14600 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14601
Scot Doyled4967d82014-07-03 23:27:52 +000014602 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14603 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014604
14605 /* HP Chromebook 14 (Celeron 2955U) */
14606 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014607
14608 /* Dell Chromebook 11 */
14609 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014610};
14611
14612static void intel_init_quirks(struct drm_device *dev)
14613{
14614 struct pci_dev *d = dev->pdev;
14615 int i;
14616
14617 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14618 struct intel_quirk *q = &intel_quirks[i];
14619
14620 if (d->device == q->device &&
14621 (d->subsystem_vendor == q->subsystem_vendor ||
14622 q->subsystem_vendor == PCI_ANY_ID) &&
14623 (d->subsystem_device == q->subsystem_device ||
14624 q->subsystem_device == PCI_ANY_ID))
14625 q->hook(dev);
14626 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014627 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14628 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14629 intel_dmi_quirks[i].hook(dev);
14630 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014631}
14632
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014633/* Disable the VGA plane that we never use */
14634static void i915_disable_vga(struct drm_device *dev)
14635{
14636 struct drm_i915_private *dev_priv = dev->dev_private;
14637 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014638 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014639
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014640 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014641 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014642 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014643 sr1 = inb(VGA_SR_DATA);
14644 outb(sr1 | 1<<5, VGA_SR_DATA);
14645 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14646 udelay(300);
14647
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014648 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014649 POSTING_READ(vga_reg);
14650}
14651
Daniel Vetterf8175862012-04-10 15:50:11 +020014652void intel_modeset_init_hw(struct drm_device *dev)
14653{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014654 intel_prepare_ddi(dev);
14655
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014656 if (IS_VALLEYVIEW(dev))
14657 vlv_update_cdclk(dev);
14658
Daniel Vetterf8175862012-04-10 15:50:11 +020014659 intel_init_clock_gating(dev);
14660
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014661 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014662}
14663
Jesse Barnes79e53942008-11-07 14:24:08 -080014664void intel_modeset_init(struct drm_device *dev)
14665{
Jesse Barnes652c3932009-08-17 13:31:43 -070014666 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014667 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014668 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014669 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014670
14671 drm_mode_config_init(dev);
14672
14673 dev->mode_config.min_width = 0;
14674 dev->mode_config.min_height = 0;
14675
Dave Airlie019d96c2011-09-29 16:20:42 +010014676 dev->mode_config.preferred_depth = 24;
14677 dev->mode_config.prefer_shadow = 1;
14678
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014679 dev->mode_config.allow_fb_modifiers = true;
14680
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014681 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014682
Jesse Barnesb690e962010-07-19 13:53:12 -070014683 intel_init_quirks(dev);
14684
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014685 intel_init_pm(dev);
14686
Ben Widawskye3c74752013-04-05 13:12:39 -070014687 if (INTEL_INFO(dev)->num_pipes == 0)
14688 return;
14689
Jesse Barnese70236a2009-09-21 10:42:27 -070014690 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014691 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014692
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014693 if (IS_GEN2(dev)) {
14694 dev->mode_config.max_width = 2048;
14695 dev->mode_config.max_height = 2048;
14696 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014697 dev->mode_config.max_width = 4096;
14698 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014699 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014700 dev->mode_config.max_width = 8192;
14701 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014702 }
Damien Lespiau068be562014-03-28 14:17:49 +000014703
Ville Syrjälädc41c152014-08-13 11:57:05 +030014704 if (IS_845G(dev) || IS_I865G(dev)) {
14705 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14706 dev->mode_config.cursor_height = 1023;
14707 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014708 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14709 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14710 } else {
14711 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14712 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14713 }
14714
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014715 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014716
Zhao Yakui28c97732009-10-09 11:39:41 +080014717 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014718 INTEL_INFO(dev)->num_pipes,
14719 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014720
Damien Lespiau055e3932014-08-18 13:49:10 +010014721 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014722 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014723 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014724 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014725 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014726 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014727 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014728 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014729 }
14730
Jesse Barnesf42bb702013-12-16 16:34:23 -080014731 intel_init_dpio(dev);
14732
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014733 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014734
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014735 /* Just disable it once at startup */
14736 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014737 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014738
14739 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014740 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014741
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014742 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014743 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014744 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014745
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014746 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014747 if (!crtc->active)
14748 continue;
14749
Jesse Barnes46f297f2014-03-07 08:57:48 -080014750 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014751 * Note that reserving the BIOS fb up front prevents us
14752 * from stuffing other stolen allocations like the ring
14753 * on top. This prevents some ugliness at boot time, and
14754 * can even allow for smooth boot transitions if the BIOS
14755 * fb is large enough for the active pipe configuration.
14756 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014757 if (dev_priv->display.get_initial_plane_config) {
14758 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014759 &crtc->plane_config);
14760 /*
14761 * If the fb is shared between multiple heads, we'll
14762 * just get the first one.
14763 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014764 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014765 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014766 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014767}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014768
Daniel Vetter7fad7982012-07-04 17:51:47 +020014769static void intel_enable_pipe_a(struct drm_device *dev)
14770{
14771 struct intel_connector *connector;
14772 struct drm_connector *crt = NULL;
14773 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014774 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014775
14776 /* We can't just switch on the pipe A, we need to set things up with a
14777 * proper mode and output configuration. As a gross hack, enable pipe A
14778 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014779 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014780 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14781 crt = &connector->base;
14782 break;
14783 }
14784 }
14785
14786 if (!crt)
14787 return;
14788
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014789 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014790 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014791}
14792
Daniel Vetterfa555832012-10-10 23:14:00 +020014793static bool
14794intel_check_plane_mapping(struct intel_crtc *crtc)
14795{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014796 struct drm_device *dev = crtc->base.dev;
14797 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014798 u32 reg, val;
14799
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014800 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014801 return true;
14802
14803 reg = DSPCNTR(!crtc->plane);
14804 val = I915_READ(reg);
14805
14806 if ((val & DISPLAY_PLANE_ENABLE) &&
14807 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14808 return false;
14809
14810 return true;
14811}
14812
Daniel Vetter24929352012-07-02 20:28:59 +020014813static void intel_sanitize_crtc(struct intel_crtc *crtc)
14814{
14815 struct drm_device *dev = crtc->base.dev;
14816 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014817 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014818
Daniel Vetter24929352012-07-02 20:28:59 +020014819 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014820 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014821 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14822
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014823 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014824 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014825 if (crtc->active) {
14826 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014827 drm_crtc_vblank_on(&crtc->base);
14828 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014829
Daniel Vetter24929352012-07-02 20:28:59 +020014830 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014831 * disable the crtc (and hence change the state) if it is wrong. Note
14832 * that gen4+ has a fixed plane -> pipe mapping. */
14833 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014834 struct intel_connector *connector;
14835 bool plane;
14836
Daniel Vetter24929352012-07-02 20:28:59 +020014837 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14838 crtc->base.base.id);
14839
14840 /* Pipe has the wrong plane attached and the plane is active.
14841 * Temporarily change the plane mapping and disable everything
14842 * ... */
14843 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014844 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014845 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014846 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014847 dev_priv->display.crtc_disable(&crtc->base);
14848 crtc->plane = plane;
14849
14850 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014851 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014852 if (connector->encoder->base.crtc != &crtc->base)
14853 continue;
14854
Egbert Eich7f1950f2014-04-25 10:56:22 +020014855 connector->base.dpms = DRM_MODE_DPMS_OFF;
14856 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014857 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014858 /* multiple connectors may have the same encoder:
14859 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014860 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014861 if (connector->encoder->base.crtc == &crtc->base) {
14862 connector->encoder->base.crtc = NULL;
14863 connector->encoder->connectors_active = false;
14864 }
Daniel Vetter24929352012-07-02 20:28:59 +020014865
14866 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014867 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014868 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014869 crtc->base.enabled = false;
14870 }
Daniel Vetter24929352012-07-02 20:28:59 +020014871
Daniel Vetter7fad7982012-07-04 17:51:47 +020014872 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14873 crtc->pipe == PIPE_A && !crtc->active) {
14874 /* BIOS forgot to enable pipe A, this mostly happens after
14875 * resume. Force-enable the pipe to fix this, the update_dpms
14876 * call below we restore the pipe to the right state, but leave
14877 * the required bits on. */
14878 intel_enable_pipe_a(dev);
14879 }
14880
Daniel Vetter24929352012-07-02 20:28:59 +020014881 /* Adjust the state of the output pipe according to whether we
14882 * have active connectors/encoders. */
14883 intel_crtc_update_dpms(&crtc->base);
14884
Matt Roper83d65732015-02-25 13:12:16 -080014885 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014886 struct intel_encoder *encoder;
14887
14888 /* This can happen either due to bugs in the get_hw_state
14889 * functions or because the pipe is force-enabled due to the
14890 * pipe A quirk. */
14891 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14892 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014893 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014894 crtc->active ? "enabled" : "disabled");
14895
Matt Roper83d65732015-02-25 13:12:16 -080014896 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014897 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014898 crtc->base.enabled = crtc->active;
14899
14900 /* Because we only establish the connector -> encoder ->
14901 * crtc links if something is active, this means the
14902 * crtc is now deactivated. Break the links. connector
14903 * -> encoder links are only establish when things are
14904 * actually up, hence no need to break them. */
14905 WARN_ON(crtc->active);
14906
14907 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14908 WARN_ON(encoder->connectors_active);
14909 encoder->base.crtc = NULL;
14910 }
14911 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014912
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014913 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014914 /*
14915 * We start out with underrun reporting disabled to avoid races.
14916 * For correct bookkeeping mark this on active crtcs.
14917 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014918 * Also on gmch platforms we dont have any hardware bits to
14919 * disable the underrun reporting. Which means we need to start
14920 * out with underrun reporting disabled also on inactive pipes,
14921 * since otherwise we'll complain about the garbage we read when
14922 * e.g. coming up after runtime pm.
14923 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014924 * No protection against concurrent access is required - at
14925 * worst a fifo underrun happens which also sets this to false.
14926 */
14927 crtc->cpu_fifo_underrun_disabled = true;
14928 crtc->pch_fifo_underrun_disabled = true;
14929 }
Daniel Vetter24929352012-07-02 20:28:59 +020014930}
14931
14932static void intel_sanitize_encoder(struct intel_encoder *encoder)
14933{
14934 struct intel_connector *connector;
14935 struct drm_device *dev = encoder->base.dev;
14936
14937 /* We need to check both for a crtc link (meaning that the
14938 * encoder is active and trying to read from a pipe) and the
14939 * pipe itself being active. */
14940 bool has_active_crtc = encoder->base.crtc &&
14941 to_intel_crtc(encoder->base.crtc)->active;
14942
14943 if (encoder->connectors_active && !has_active_crtc) {
14944 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14945 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014946 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014947
14948 /* Connector is active, but has no active pipe. This is
14949 * fallout from our resume register restoring. Disable
14950 * the encoder manually again. */
14951 if (encoder->base.crtc) {
14952 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14953 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014954 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014955 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014956 if (encoder->post_disable)
14957 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014958 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014959 encoder->base.crtc = NULL;
14960 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014961
14962 /* Inconsistent output/port/pipe state happens presumably due to
14963 * a bug in one of the get_hw_state functions. Or someplace else
14964 * in our code, like the register restore mess on resume. Clamp
14965 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014966 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014967 if (connector->encoder != encoder)
14968 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014969 connector->base.dpms = DRM_MODE_DPMS_OFF;
14970 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014971 }
14972 }
14973 /* Enabled encoders without active connectors will be fixed in
14974 * the crtc fixup. */
14975}
14976
Imre Deak04098752014-02-18 00:02:16 +020014977void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014978{
14979 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014980 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014981
Imre Deak04098752014-02-18 00:02:16 +020014982 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14983 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14984 i915_disable_vga(dev);
14985 }
14986}
14987
14988void i915_redisable_vga(struct drm_device *dev)
14989{
14990 struct drm_i915_private *dev_priv = dev->dev_private;
14991
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014992 /* This function can be called both from intel_modeset_setup_hw_state or
14993 * at a very early point in our resume sequence, where the power well
14994 * structures are not yet restored. Since this function is at a very
14995 * paranoid "someone might have enabled VGA while we were not looking"
14996 * level, just check if the power well is enabled instead of trying to
14997 * follow the "don't touch the power well if we don't need it" policy
14998 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020014999 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015000 return;
15001
Imre Deak04098752014-02-18 00:02:16 +020015002 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015003}
15004
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015005static bool primary_get_hw_state(struct intel_crtc *crtc)
15006{
15007 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15008
15009 if (!crtc->active)
15010 return false;
15011
15012 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15013}
15014
Daniel Vetter30e984d2013-06-05 13:34:17 +020015015static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015016{
15017 struct drm_i915_private *dev_priv = dev->dev_private;
15018 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015019 struct intel_crtc *crtc;
15020 struct intel_encoder *encoder;
15021 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015022 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015023
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015024 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015025 struct drm_plane *primary = crtc->base.primary;
15026 struct intel_plane_state *plane_state;
15027
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015028 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015029
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015030 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015031
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015032 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015033 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015034
Matt Roper83d65732015-02-25 13:12:16 -080015035 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015036 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015037 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015038
15039 plane_state = to_intel_plane_state(primary->state);
15040 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015041
15042 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15043 crtc->base.base.id,
15044 crtc->active ? "enabled" : "disabled");
15045 }
15046
Daniel Vetter53589012013-06-05 13:34:16 +020015047 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15048 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15049
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015050 pll->on = pll->get_hw_state(dev_priv, pll,
15051 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015052 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015053 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015054 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015055 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015056 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015057 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015058 }
Daniel Vetter53589012013-06-05 13:34:16 +020015059 }
Daniel Vetter53589012013-06-05 13:34:16 +020015060
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015061 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015062 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015063
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015064 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015065 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015066 }
15067
Damien Lespiaub2784e12014-08-05 11:29:37 +010015068 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015069 pipe = 0;
15070
15071 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015072 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15073 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015074 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015075 } else {
15076 encoder->base.crtc = NULL;
15077 }
15078
15079 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015080 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015081 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015082 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015083 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015084 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015085 }
15086
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015087 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015088 if (connector->get_hw_state(connector)) {
15089 connector->base.dpms = DRM_MODE_DPMS_ON;
15090 connector->encoder->connectors_active = true;
15091 connector->base.encoder = &connector->encoder->base;
15092 } else {
15093 connector->base.dpms = DRM_MODE_DPMS_OFF;
15094 connector->base.encoder = NULL;
15095 }
15096 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15097 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015098 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015099 connector->base.encoder ? "enabled" : "disabled");
15100 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015101}
15102
15103/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15104 * and i915 state tracking structures. */
15105void intel_modeset_setup_hw_state(struct drm_device *dev,
15106 bool force_restore)
15107{
15108 struct drm_i915_private *dev_priv = dev->dev_private;
15109 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015110 struct intel_crtc *crtc;
15111 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015112 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015113
15114 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015115
Jesse Barnesbabea612013-06-26 18:57:38 +030015116 /*
15117 * Now that we have the config, copy it to each CRTC struct
15118 * Note that this could go away if we move to using crtc_config
15119 * checking everywhere.
15120 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015121 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015122 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015123 intel_mode_from_pipe_config(&crtc->base.mode,
15124 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015125 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15126 crtc->base.base.id);
15127 drm_mode_debug_printmodeline(&crtc->base.mode);
15128 }
15129 }
15130
Daniel Vetter24929352012-07-02 20:28:59 +020015131 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015132 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015133 intel_sanitize_encoder(encoder);
15134 }
15135
Damien Lespiau055e3932014-08-18 13:49:10 +010015136 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015137 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15138 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015139 intel_dump_pipe_config(crtc, crtc->config,
15140 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015141 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015142
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015143 intel_modeset_update_connector_atomic_state(dev);
15144
Daniel Vetter35c95372013-07-17 06:55:04 +020015145 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15146 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15147
15148 if (!pll->on || pll->active)
15149 continue;
15150
15151 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15152
15153 pll->disable(dev_priv, pll);
15154 pll->on = false;
15155 }
15156
Pradeep Bhat30789992014-11-04 17:06:45 +000015157 if (IS_GEN9(dev))
15158 skl_wm_get_hw_state(dev);
15159 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015160 ilk_wm_get_hw_state(dev);
15161
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015162 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015163 i915_redisable_vga(dev);
15164
Daniel Vetterf30da182013-04-11 20:22:50 +020015165 /*
15166 * We need to use raw interfaces for restoring state to avoid
15167 * checking (bogus) intermediate states.
15168 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015169 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015170 struct drm_crtc *crtc =
15171 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015172
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015173 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015174 }
15175 } else {
15176 intel_modeset_update_staged_output_state(dev);
15177 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015178
15179 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015180}
15181
15182void intel_modeset_gem_init(struct drm_device *dev)
15183{
Jesse Barnes92122782014-10-09 12:57:42 -070015184 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015185 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015186 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015187 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015188
Imre Deakae484342014-03-31 15:10:44 +030015189 mutex_lock(&dev->struct_mutex);
15190 intel_init_gt_powersave(dev);
15191 mutex_unlock(&dev->struct_mutex);
15192
Jesse Barnes92122782014-10-09 12:57:42 -070015193 /*
15194 * There may be no VBT; and if the BIOS enabled SSC we can
15195 * just keep using it to avoid unnecessary flicker. Whereas if the
15196 * BIOS isn't using it, don't assume it will work even if the VBT
15197 * indicates as much.
15198 */
15199 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15200 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15201 DREF_SSC1_ENABLE);
15202
Chris Wilson1833b132012-05-09 11:56:28 +010015203 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015204
15205 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015206
15207 /*
15208 * Make sure any fbs we allocated at startup are properly
15209 * pinned & fenced. When we do the allocation it's too early
15210 * for this.
15211 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015212 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015213 obj = intel_fb_obj(c->primary->fb);
15214 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015215 continue;
15216
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015217 mutex_lock(&dev->struct_mutex);
15218 ret = intel_pin_and_fence_fb_obj(c->primary,
15219 c->primary->fb,
15220 c->primary->state,
15221 NULL);
15222 mutex_unlock(&dev->struct_mutex);
15223 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015224 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15225 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015226 drm_framebuffer_unreference(c->primary->fb);
15227 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015228 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015229 }
15230 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015231
15232 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015233}
15234
Imre Deak4932e2c2014-02-11 17:12:48 +020015235void intel_connector_unregister(struct intel_connector *intel_connector)
15236{
15237 struct drm_connector *connector = &intel_connector->base;
15238
15239 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015240 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015241}
15242
Jesse Barnes79e53942008-11-07 14:24:08 -080015243void intel_modeset_cleanup(struct drm_device *dev)
15244{
Jesse Barnes652c3932009-08-17 13:31:43 -070015245 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015246 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015247
Imre Deak2eb52522014-11-19 15:30:05 +020015248 intel_disable_gt_powersave(dev);
15249
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015250 intel_backlight_unregister(dev);
15251
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015252 /*
15253 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015254 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015255 * experience fancy races otherwise.
15256 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015257 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015258
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015259 /*
15260 * Due to the hpd irq storm handling the hotplug work can re-arm the
15261 * poll handlers. Hence disable polling after hpd handling is shut down.
15262 */
Keith Packardf87ea762010-10-03 19:36:26 -070015263 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015264
Jesse Barnes652c3932009-08-17 13:31:43 -070015265 mutex_lock(&dev->struct_mutex);
15266
Jesse Barnes723bfd72010-10-07 16:01:13 -070015267 intel_unregister_dsm_handler();
15268
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015269 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015270
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015271 mutex_unlock(&dev->struct_mutex);
15272
Chris Wilson1630fe72011-07-08 12:22:42 +010015273 /* flush any delayed tasks or pending work */
15274 flush_scheduled_work();
15275
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015276 /* destroy the backlight and sysfs files before encoders/connectors */
15277 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015278 struct intel_connector *intel_connector;
15279
15280 intel_connector = to_intel_connector(connector);
15281 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015282 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015283
Jesse Barnes79e53942008-11-07 14:24:08 -080015284 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015285
15286 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015287
15288 mutex_lock(&dev->struct_mutex);
15289 intel_cleanup_gt_powersave(dev);
15290 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015291}
15292
Dave Airlie28d52042009-09-21 14:33:58 +100015293/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015294 * Return which encoder is currently attached for connector.
15295 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015296struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015297{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015298 return &intel_attached_encoder(connector)->base;
15299}
Jesse Barnes79e53942008-11-07 14:24:08 -080015300
Chris Wilsondf0e9242010-09-09 16:20:55 +010015301void intel_connector_attach_encoder(struct intel_connector *connector,
15302 struct intel_encoder *encoder)
15303{
15304 connector->encoder = encoder;
15305 drm_mode_connector_attach_encoder(&connector->base,
15306 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015307}
Dave Airlie28d52042009-09-21 14:33:58 +100015308
15309/*
15310 * set vga decode state - true == enable VGA decode
15311 */
15312int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15313{
15314 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015315 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015316 u16 gmch_ctrl;
15317
Chris Wilson75fa0412014-02-07 18:37:02 -020015318 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15319 DRM_ERROR("failed to read control word\n");
15320 return -EIO;
15321 }
15322
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015323 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15324 return 0;
15325
Dave Airlie28d52042009-09-21 14:33:58 +100015326 if (state)
15327 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15328 else
15329 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015330
15331 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15332 DRM_ERROR("failed to write control word\n");
15333 return -EIO;
15334 }
15335
Dave Airlie28d52042009-09-21 14:33:58 +100015336 return 0;
15337}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015338
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015339struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015340
15341 u32 power_well_driver;
15342
Chris Wilson63b66e52013-08-08 15:12:06 +020015343 int num_transcoders;
15344
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015345 struct intel_cursor_error_state {
15346 u32 control;
15347 u32 position;
15348 u32 base;
15349 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015350 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015351
15352 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015353 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015354 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015355 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015356 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015357
15358 struct intel_plane_error_state {
15359 u32 control;
15360 u32 stride;
15361 u32 size;
15362 u32 pos;
15363 u32 addr;
15364 u32 surface;
15365 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015366 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015367
15368 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015369 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015370 enum transcoder cpu_transcoder;
15371
15372 u32 conf;
15373
15374 u32 htotal;
15375 u32 hblank;
15376 u32 hsync;
15377 u32 vtotal;
15378 u32 vblank;
15379 u32 vsync;
15380 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015381};
15382
15383struct intel_display_error_state *
15384intel_display_capture_error_state(struct drm_device *dev)
15385{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015386 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015387 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015388 int transcoders[] = {
15389 TRANSCODER_A,
15390 TRANSCODER_B,
15391 TRANSCODER_C,
15392 TRANSCODER_EDP,
15393 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015394 int i;
15395
Chris Wilson63b66e52013-08-08 15:12:06 +020015396 if (INTEL_INFO(dev)->num_pipes == 0)
15397 return NULL;
15398
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015399 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015400 if (error == NULL)
15401 return NULL;
15402
Imre Deak190be112013-11-25 17:15:31 +020015403 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015404 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15405
Damien Lespiau055e3932014-08-18 13:49:10 +010015406 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015407 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015408 __intel_display_power_is_enabled(dev_priv,
15409 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015410 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015411 continue;
15412
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015413 error->cursor[i].control = I915_READ(CURCNTR(i));
15414 error->cursor[i].position = I915_READ(CURPOS(i));
15415 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015416
15417 error->plane[i].control = I915_READ(DSPCNTR(i));
15418 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015419 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015420 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015421 error->plane[i].pos = I915_READ(DSPPOS(i));
15422 }
Paulo Zanonica291362013-03-06 20:03:14 -030015423 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15424 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015425 if (INTEL_INFO(dev)->gen >= 4) {
15426 error->plane[i].surface = I915_READ(DSPSURF(i));
15427 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15428 }
15429
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015430 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015431
Sonika Jindal3abfce72014-07-21 15:23:43 +053015432 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015433 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015434 }
15435
15436 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15437 if (HAS_DDI(dev_priv->dev))
15438 error->num_transcoders++; /* Account for eDP. */
15439
15440 for (i = 0; i < error->num_transcoders; i++) {
15441 enum transcoder cpu_transcoder = transcoders[i];
15442
Imre Deakddf9c532013-11-27 22:02:02 +020015443 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015444 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015445 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015446 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015447 continue;
15448
Chris Wilson63b66e52013-08-08 15:12:06 +020015449 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15450
15451 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15452 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15453 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15454 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15455 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15456 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15457 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015458 }
15459
15460 return error;
15461}
15462
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015463#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15464
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015465void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015466intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015467 struct drm_device *dev,
15468 struct intel_display_error_state *error)
15469{
Damien Lespiau055e3932014-08-18 13:49:10 +010015470 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015471 int i;
15472
Chris Wilson63b66e52013-08-08 15:12:06 +020015473 if (!error)
15474 return;
15475
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015476 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015477 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015478 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015479 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015480 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015481 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015482 err_printf(m, " Power: %s\n",
15483 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015484 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015485 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015486
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015487 err_printf(m, "Plane [%d]:\n", i);
15488 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15489 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015490 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015491 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15492 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015493 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015494 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015495 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015496 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015497 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15498 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015499 }
15500
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015501 err_printf(m, "Cursor [%d]:\n", i);
15502 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15503 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15504 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015505 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015506
15507 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015508 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015509 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015510 err_printf(m, " Power: %s\n",
15511 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015512 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15513 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15514 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15515 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15516 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15517 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15518 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15519 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015520}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015521
15522void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15523{
15524 struct intel_crtc *crtc;
15525
15526 for_each_intel_crtc(dev, crtc) {
15527 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015528
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015529 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015530
15531 work = crtc->unpin_work;
15532
15533 if (work && work->event &&
15534 work->event->base.file_priv == file) {
15535 kfree(work->event);
15536 work->event = NULL;
15537 }
15538
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015539 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015540 }
15541}