blob: 73a46149564ab731abe6abf09219aaad9c96e36f [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Matt Roper465c1202014-05-29 08:06:54 -070075};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Chris Wilson6b383a72010-09-13 13:54:26 +010082static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080083
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020085 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030086static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030089static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020090 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080091static int intel_framebuffer_init(struct drm_device *dev,
92 struct intel_framebuffer *ifb,
93 struct drm_mode_fb_cmd2 *mode_cmd,
94 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020095static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
96static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020097static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070098 struct intel_link_m_n *m_n,
99 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200100static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200101static void haswell_set_pipeconf(struct drm_crtc *crtc);
102static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200103static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200104 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200105static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200106 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800107static void intel_begin_crtc_commit(struct drm_crtc *crtc);
108static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700109static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
110 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200111static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
112 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300113static void intel_crtc_enable_planes(struct drm_crtc *crtc);
114static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100115
Dave Airlie0e32b392014-05-02 14:02:48 +1000116static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
117{
118 if (!connector->mst_port)
119 return connector->encoder;
120 else
121 return &connector->mst_port->mst_encoders[pipe]->base;
122}
123
Jesse Barnes79e53942008-11-07 14:24:08 -0800124typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126} intel_range_t;
127
128typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400129 int dot_limit;
130 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800131} intel_p2_t;
132
Ma Lingd4906092009-03-18 20:13:27 +0800133typedef struct intel_limit intel_limit_t;
134struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400135 intel_range_t dot, vco, n, m, m1, m2, p, p1;
136 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Daniel Vetterd2acd212012-10-20 20:57:43 +0200139int
140intel_pch_rawclk(struct drm_device *dev)
141{
142 struct drm_i915_private *dev_priv = dev->dev_private;
143
144 WARN_ON(!HAS_PCH_SPLIT(dev));
145
146 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
147}
148
Chris Wilson021357a2010-09-07 20:54:59 +0100149static inline u32 /* units of 100MHz */
150intel_fdi_link_freq(struct drm_device *dev)
151{
Chris Wilson8b99e682010-10-13 09:59:17 +0100152 if (IS_GEN5(dev)) {
153 struct drm_i915_private *dev_priv = dev->dev_private;
154 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
155 } else
156 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100157}
158
Daniel Vetter5d536e22013-07-06 12:52:06 +0200159static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200161 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200162 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
Daniel Vetter5d536e22013-07-06 12:52:06 +0200172static const intel_limit_t intel_limits_i8xx_dvo = {
173 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200174 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200175 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200176 .m = { .min = 96, .max = 140 },
177 .m1 = { .min = 18, .max = 26 },
178 .m2 = { .min = 6, .max = 16 },
179 .p = { .min = 4, .max = 128 },
180 .p1 = { .min = 2, .max = 33 },
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 4, .p2_fast = 4 },
183};
184
Keith Packarde4b36692009-06-05 19:22:17 -0700185static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200187 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200188 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400189 .m = { .min = 96, .max = 140 },
190 .m1 = { .min = 18, .max = 26 },
191 .m2 = { .min = 6, .max = 16 },
192 .p = { .min = 4, .max = 128 },
193 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700194 .p2 = { .dot_limit = 165000,
195 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700196};
Eric Anholt273e27c2011-03-30 13:01:10 -0700197
Keith Packarde4b36692009-06-05 19:22:17 -0700198static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400199 .dot = { .min = 20000, .max = 400000 },
200 .vco = { .min = 1400000, .max = 2800000 },
201 .n = { .min = 1, .max = 6 },
202 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100203 .m1 = { .min = 8, .max = 18 },
204 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400205 .p = { .min = 5, .max = 80 },
206 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700207 .p2 = { .dot_limit = 200000,
208 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700209};
210
211static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400212 .dot = { .min = 20000, .max = 400000 },
213 .vco = { .min = 1400000, .max = 2800000 },
214 .n = { .min = 1, .max = 6 },
215 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100216 .m1 = { .min = 8, .max = 18 },
217 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400218 .p = { .min = 7, .max = 98 },
219 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 .p2 = { .dot_limit = 112000,
221 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700222};
223
Eric Anholt273e27c2011-03-30 13:01:10 -0700224
Keith Packarde4b36692009-06-05 19:22:17 -0700225static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700226 .dot = { .min = 25000, .max = 270000 },
227 .vco = { .min = 1750000, .max = 3500000},
228 .n = { .min = 1, .max = 4 },
229 .m = { .min = 104, .max = 138 },
230 .m1 = { .min = 17, .max = 23 },
231 .m2 = { .min = 5, .max = 11 },
232 .p = { .min = 10, .max = 30 },
233 .p1 = { .min = 1, .max = 3},
234 .p2 = { .dot_limit = 270000,
235 .p2_slow = 10,
236 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800237 },
Keith Packarde4b36692009-06-05 19:22:17 -0700238};
239
240static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700241 .dot = { .min = 22000, .max = 400000 },
242 .vco = { .min = 1750000, .max = 3500000},
243 .n = { .min = 1, .max = 4 },
244 .m = { .min = 104, .max = 138 },
245 .m1 = { .min = 16, .max = 23 },
246 .m2 = { .min = 5, .max = 11 },
247 .p = { .min = 5, .max = 80 },
248 .p1 = { .min = 1, .max = 8},
249 .p2 = { .dot_limit = 165000,
250 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700251};
252
253static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700254 .dot = { .min = 20000, .max = 115000 },
255 .vco = { .min = 1750000, .max = 3500000 },
256 .n = { .min = 1, .max = 3 },
257 .m = { .min = 104, .max = 138 },
258 .m1 = { .min = 17, .max = 23 },
259 .m2 = { .min = 5, .max = 11 },
260 .p = { .min = 28, .max = 112 },
261 .p1 = { .min = 2, .max = 8 },
262 .p2 = { .dot_limit = 0,
263 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800264 },
Keith Packarde4b36692009-06-05 19:22:17 -0700265};
266
267static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700268 .dot = { .min = 80000, .max = 224000 },
269 .vco = { .min = 1750000, .max = 3500000 },
270 .n = { .min = 1, .max = 3 },
271 .m = { .min = 104, .max = 138 },
272 .m1 = { .min = 17, .max = 23 },
273 .m2 = { .min = 5, .max = 11 },
274 .p = { .min = 14, .max = 42 },
275 .p1 = { .min = 2, .max = 6 },
276 .p2 = { .dot_limit = 0,
277 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800278 },
Keith Packarde4b36692009-06-05 19:22:17 -0700279};
280
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500281static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .dot = { .min = 20000, .max = 400000},
283 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .n = { .min = 3, .max = 6 },
286 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .m1 = { .min = 0, .max = 0 },
289 .m2 = { .min = 0, .max = 254 },
290 .p = { .min = 5, .max = 80 },
291 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700292 .p2 = { .dot_limit = 200000,
293 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700294};
295
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500296static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .dot = { .min = 20000, .max = 400000 },
298 .vco = { .min = 1700000, .max = 3500000 },
299 .n = { .min = 3, .max = 6 },
300 .m = { .min = 2, .max = 256 },
301 .m1 = { .min = 0, .max = 0 },
302 .m2 = { .min = 0, .max = 254 },
303 .p = { .min = 7, .max = 112 },
304 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .p2 = { .dot_limit = 112000,
306 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700307};
308
Eric Anholt273e27c2011-03-30 13:01:10 -0700309/* Ironlake / Sandybridge
310 *
311 * We calculate clock using (register_value + 2) for N/M1/M2, so here
312 * the range value for them is (actual_value - 2).
313 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800314static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 5 },
318 .m = { .min = 79, .max = 127 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 5, .max = 80 },
322 .p1 = { .min = 1, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800327static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700328 .dot = { .min = 25000, .max = 350000 },
329 .vco = { .min = 1760000, .max = 3510000 },
330 .n = { .min = 1, .max = 3 },
331 .m = { .min = 79, .max = 118 },
332 .m1 = { .min = 12, .max = 22 },
333 .m2 = { .min = 5, .max = 9 },
334 .p = { .min = 28, .max = 112 },
335 .p1 = { .min = 2, .max = 8 },
336 .p2 = { .dot_limit = 225000,
337 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800338};
339
340static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700341 .dot = { .min = 25000, .max = 350000 },
342 .vco = { .min = 1760000, .max = 3510000 },
343 .n = { .min = 1, .max = 3 },
344 .m = { .min = 79, .max = 127 },
345 .m1 = { .min = 12, .max = 22 },
346 .m2 = { .min = 5, .max = 9 },
347 .p = { .min = 14, .max = 56 },
348 .p1 = { .min = 2, .max = 8 },
349 .p2 = { .dot_limit = 225000,
350 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351};
352
Eric Anholt273e27c2011-03-30 13:01:10 -0700353/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800354static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700355 .dot = { .min = 25000, .max = 350000 },
356 .vco = { .min = 1760000, .max = 3510000 },
357 .n = { .min = 1, .max = 2 },
358 .m = { .min = 79, .max = 126 },
359 .m1 = { .min = 12, .max = 22 },
360 .m2 = { .min = 5, .max = 9 },
361 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400362 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .p2 = { .dot_limit = 225000,
364 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800365};
366
367static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700368 .dot = { .min = 25000, .max = 350000 },
369 .vco = { .min = 1760000, .max = 3510000 },
370 .n = { .min = 1, .max = 3 },
371 .m = { .min = 79, .max = 126 },
372 .m1 = { .min = 12, .max = 22 },
373 .m2 = { .min = 5, .max = 9 },
374 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400375 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700376 .p2 = { .dot_limit = 225000,
377 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800378};
379
Ville Syrjälädc730512013-09-24 21:26:30 +0300380static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300381 /*
382 * These are the data rate limits (measured in fast clocks)
383 * since those are the strictest limits we have. The fast
384 * clock and actual rate limits are more relaxed, so checking
385 * them would make no difference.
386 */
387 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200388 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700389 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700390 .m1 = { .min = 2, .max = 3 },
391 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300392 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300393 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700394};
395
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300396static const intel_limit_t intel_limits_chv = {
397 /*
398 * These are the data rate limits (measured in fast clocks)
399 * since those are the strictest limits we have. The fast
400 * clock and actual rate limits are more relaxed, so checking
401 * them would make no difference.
402 */
403 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200404 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300405 .n = { .min = 1, .max = 1 },
406 .m1 = { .min = 2, .max = 2 },
407 .m2 = { .min = 24 << 22, .max = 175 << 22 },
408 .p1 = { .min = 2, .max = 4 },
409 .p2 = { .p2_slow = 1, .p2_fast = 14 },
410};
411
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200412static const intel_limit_t intel_limits_bxt = {
413 /* FIXME: find real dot limits */
414 .dot = { .min = 0, .max = INT_MAX },
415 .vco = { .min = 4800000, .max = 6480000 },
416 .n = { .min = 1, .max = 1 },
417 .m1 = { .min = 2, .max = 2 },
418 /* FIXME: find real m2 limits */
419 .m2 = { .min = 2 << 22, .max = 255 << 22 },
420 .p1 = { .min = 2, .max = 4 },
421 .p2 = { .p2_slow = 1, .p2_fast = 20 },
422};
423
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300424static void vlv_clock(int refclk, intel_clock_t *clock)
425{
426 clock->m = clock->m1 * clock->m2;
427 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200428 if (WARN_ON(clock->n == 0 || clock->p == 0))
429 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300430 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
431 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300432}
433
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300434/**
435 * Returns whether any output on the specified pipe is of the specified type
436 */
Damien Lespiau40935612014-10-29 11:16:59 +0000437bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300438{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300439 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300440 struct intel_encoder *encoder;
441
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300442 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300443 if (encoder->type == type)
444 return true;
445
446 return false;
447}
448
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200449/**
450 * Returns whether any output on the specified pipe will have the specified
451 * type after a staged modeset is complete, i.e., the same as
452 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
453 * encoder->crtc.
454 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
456 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300459 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200460 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200461 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200462 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200463
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300464 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465 if (connector_state->crtc != crtc_state->base.crtc)
466 continue;
467
468 num_connectors++;
469
470 encoder = to_intel_encoder(connector_state->best_encoder);
471 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200472 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200473 }
474
475 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200476
477 return false;
478}
479
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200480static const intel_limit_t *
481intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800482{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200483 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800484 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800485
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200486 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100487 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000488 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800489 limit = &intel_limits_ironlake_dual_lvds_100m;
490 else
491 limit = &intel_limits_ironlake_dual_lvds;
492 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000493 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800494 limit = &intel_limits_ironlake_single_lvds_100m;
495 else
496 limit = &intel_limits_ironlake_single_lvds;
497 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200498 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800499 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800500
501 return limit;
502}
503
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200504static const intel_limit_t *
505intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800506{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200507 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800508 const intel_limit_t *limit;
509
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100511 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 else
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200515 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
516 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700519 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800520 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700521 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800522
523 return limit;
524}
525
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200526static const intel_limit_t *
527intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800528{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200529 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800530 const intel_limit_t *limit;
531
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200532 if (IS_BROXTON(dev))
533 limit = &intel_limits_bxt;
534 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800536 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500538 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200539 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500540 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800541 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500542 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300543 } else if (IS_CHERRYVIEW(dev)) {
544 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700545 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300546 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100547 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100549 limit = &intel_limits_i9xx_lvds;
550 else
551 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700554 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700556 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200557 else
558 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800559 }
560 return limit;
561}
562
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500563/* m1 is reserved as 0 in Pineview, n is a ring counter */
564static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800565{
Shaohua Li21778322009-02-23 15:19:16 +0800566 clock->m = clock->m2 + 2;
567 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200568 if (WARN_ON(clock->n == 0 || clock->p == 0))
569 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300570 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
571 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800572}
573
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200574static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
575{
576 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
577}
578
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200579static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800580{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200581 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200583 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
584 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300585 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
586 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800587}
588
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300589static void chv_clock(int refclk, intel_clock_t *clock)
590{
591 clock->m = clock->m1 * clock->m2;
592 clock->p = clock->p1 * clock->p2;
593 if (WARN_ON(clock->n == 0 || clock->p == 0))
594 return;
595 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
596 clock->n << 22);
597 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
598}
599
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800600#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800601/**
602 * Returns whether the given set of divisors are valid for a given refclk with
603 * the given connectors.
604 */
605
Chris Wilson1b894b52010-12-14 20:04:54 +0000606static bool intel_PLL_is_valid(struct drm_device *dev,
607 const intel_limit_t *limit,
608 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800609{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300610 if (clock->n < limit->n.min || limit->n.max < clock->n)
611 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400617 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200619 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300620 if (clock->m1 <= clock->m2)
621 INTELPllInvalid("m1 <= m2\n");
622
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200623 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300624 if (clock->p < limit->p.min || limit->p.max < clock->p)
625 INTELPllInvalid("p out of range\n");
626 if (clock->m < limit->m.min || limit->m.max < clock->m)
627 INTELPllInvalid("m out of range\n");
628 }
629
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400631 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800637
638 return true;
639}
640
Ma Lingd4906092009-03-18 20:13:27 +0800641static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200642i9xx_find_best_dpll(const intel_limit_t *limit,
643 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800644 int target, int refclk, intel_clock_t *match_clock,
645 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800646{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200647 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300648 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 int err = target;
651
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200652 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100654 * For LVDS just rely on its current settings for dual-channel.
655 * We haven't figured out how to reliably set up different
656 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100658 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 clock.p2 = limit->p2.p2_fast;
660 else
661 clock.p2 = limit->p2.p2_slow;
662 } else {
663 if (target < limit->p2.dot_limit)
664 clock.p2 = limit->p2.p2_slow;
665 else
666 clock.p2 = limit->p2.p2_fast;
667 }
668
Akshay Joshi0206e352011-08-16 15:34:10 -0400669 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800670
Zhao Yakui42158662009-11-20 11:24:18 +0800671 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
672 clock.m1++) {
673 for (clock.m2 = limit->m2.min;
674 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200675 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800676 break;
677 for (clock.n = limit->n.min;
678 clock.n <= limit->n.max; clock.n++) {
679 for (clock.p1 = limit->p1.min;
680 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 int this_err;
682
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200683 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000684 if (!intel_PLL_is_valid(dev, limit,
685 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800687 if (match_clock &&
688 clock.p != match_clock->p)
689 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
691 this_err = abs(clock.dot - target);
692 if (this_err < err) {
693 *best_clock = clock;
694 err = this_err;
695 }
696 }
697 }
698 }
699 }
700
701 return (err != target);
702}
703
Ma Lingd4906092009-03-18 20:13:27 +0800704static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200705pnv_find_best_dpll(const intel_limit_t *limit,
706 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200707 int target, int refclk, intel_clock_t *match_clock,
708 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200709{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200710 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300711 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200712 intel_clock_t clock;
713 int err = target;
714
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200715 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200716 /*
717 * For LVDS just rely on its current settings for dual-channel.
718 * We haven't figured out how to reliably set up different
719 * single/dual channel state, if we even can.
720 */
721 if (intel_is_dual_link_lvds(dev))
722 clock.p2 = limit->p2.p2_fast;
723 else
724 clock.p2 = limit->p2.p2_slow;
725 } else {
726 if (target < limit->p2.dot_limit)
727 clock.p2 = limit->p2.p2_slow;
728 else
729 clock.p2 = limit->p2.p2_fast;
730 }
731
732 memset(best_clock, 0, sizeof(*best_clock));
733
734 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
735 clock.m1++) {
736 for (clock.m2 = limit->m2.min;
737 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 for (clock.n = limit->n.min;
739 clock.n <= limit->n.max; clock.n++) {
740 for (clock.p1 = limit->p1.min;
741 clock.p1 <= limit->p1.max; clock.p1++) {
742 int this_err;
743
744 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800745 if (!intel_PLL_is_valid(dev, limit,
746 &clock))
747 continue;
748 if (match_clock &&
749 clock.p != match_clock->p)
750 continue;
751
752 this_err = abs(clock.dot - target);
753 if (this_err < err) {
754 *best_clock = clock;
755 err = this_err;
756 }
757 }
758 }
759 }
760 }
761
762 return (err != target);
763}
764
Ma Lingd4906092009-03-18 20:13:27 +0800765static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200766g4x_find_best_dpll(const intel_limit_t *limit,
767 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800770{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200771 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300772 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800773 intel_clock_t clock;
774 int max_n;
775 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400776 /* approximately equals target * 0.00585 */
777 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800778 found = false;
779
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200780 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100781 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800782 clock.p2 = limit->p2.p2_fast;
783 else
784 clock.p2 = limit->p2.p2_slow;
785 } else {
786 if (target < limit->p2.dot_limit)
787 clock.p2 = limit->p2.p2_slow;
788 else
789 clock.p2 = limit->p2.p2_fast;
790 }
791
792 memset(best_clock, 0, sizeof(*best_clock));
793 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200794 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800795 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200796 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800797 for (clock.m1 = limit->m1.max;
798 clock.m1 >= limit->m1.min; clock.m1--) {
799 for (clock.m2 = limit->m2.max;
800 clock.m2 >= limit->m2.min; clock.m2--) {
801 for (clock.p1 = limit->p1.max;
802 clock.p1 >= limit->p1.min; clock.p1--) {
803 int this_err;
804
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200805 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000806 if (!intel_PLL_is_valid(dev, limit,
807 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800808 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000809
810 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800811 if (this_err < err_most) {
812 *best_clock = clock;
813 err_most = this_err;
814 max_n = clock.n;
815 found = true;
816 }
817 }
818 }
819 }
820 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800821 return found;
822}
Ma Lingd4906092009-03-18 20:13:27 +0800823
Imre Deakd5dd62b2015-03-17 11:40:03 +0200824/*
825 * Check if the calculated PLL configuration is more optimal compared to the
826 * best configuration and error found so far. Return the calculated error.
827 */
828static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
829 const intel_clock_t *calculated_clock,
830 const intel_clock_t *best_clock,
831 unsigned int best_error_ppm,
832 unsigned int *error_ppm)
833{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200834 /*
835 * For CHV ignore the error and consider only the P value.
836 * Prefer a bigger P value based on HW requirements.
837 */
838 if (IS_CHERRYVIEW(dev)) {
839 *error_ppm = 0;
840
841 return calculated_clock->p > best_clock->p;
842 }
843
Imre Deak24be4e42015-03-17 11:40:04 +0200844 if (WARN_ON_ONCE(!target_freq))
845 return false;
846
Imre Deakd5dd62b2015-03-17 11:40:03 +0200847 *error_ppm = div_u64(1000000ULL *
848 abs(target_freq - calculated_clock->dot),
849 target_freq);
850 /*
851 * Prefer a better P value over a better (smaller) error if the error
852 * is small. Ensure this preference for future configurations too by
853 * setting the error to 0.
854 */
855 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
856 *error_ppm = 0;
857
858 return true;
859 }
860
861 return *error_ppm + 10 < best_error_ppm;
862}
863
Zhenyu Wang2c072452009-06-05 15:38:42 +0800864static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200865vlv_find_best_dpll(const intel_limit_t *limit,
866 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200867 int target, int refclk, intel_clock_t *match_clock,
868 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700869{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200870 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300871 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300872 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300873 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300874 /* min update 19.2 MHz */
875 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300876 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700877
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300878 target *= 5; /* fast clock */
879
880 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700881
882 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300883 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300884 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300885 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300886 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300887 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700888 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300889 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200890 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300891
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
893 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300894
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300895 vlv_clock(refclk, &clock);
896
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300897 if (!intel_PLL_is_valid(dev, limit,
898 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300899 continue;
900
Imre Deakd5dd62b2015-03-17 11:40:03 +0200901 if (!vlv_PLL_is_optimal(dev, target,
902 &clock,
903 best_clock,
904 bestppm, &ppm))
905 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906
Imre Deakd5dd62b2015-03-17 11:40:03 +0200907 *best_clock = clock;
908 bestppm = ppm;
909 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700910 }
911 }
912 }
913 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700914
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300915 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700916}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700917
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300918static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200919chv_find_best_dpll(const intel_limit_t *limit,
920 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300921 int target, int refclk, intel_clock_t *match_clock,
922 intel_clock_t *best_clock)
923{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200924 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300925 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200926 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300927 intel_clock_t clock;
928 uint64_t m2;
929 int found = false;
930
931 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200932 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300933
934 /*
935 * Based on hardware doc, the n always set to 1, and m1 always
936 * set to 2. If requires to support 200Mhz refclk, we need to
937 * revisit this because n may not 1 anymore.
938 */
939 clock.n = 1, clock.m1 = 2;
940 target *= 5; /* fast clock */
941
942 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
943 for (clock.p2 = limit->p2.p2_fast;
944 clock.p2 >= limit->p2.p2_slow;
945 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200946 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300947
948 clock.p = clock.p1 * clock.p2;
949
950 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
951 clock.n) << 22, refclk * clock.m1);
952
953 if (m2 > INT_MAX/clock.m1)
954 continue;
955
956 clock.m2 = m2;
957
958 chv_clock(refclk, &clock);
959
960 if (!intel_PLL_is_valid(dev, limit, &clock))
961 continue;
962
Imre Deak9ca3ba02015-03-17 11:40:05 +0200963 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
964 best_error_ppm, &error_ppm))
965 continue;
966
967 *best_clock = clock;
968 best_error_ppm = error_ppm;
969 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300970 }
971 }
972
973 return found;
974}
975
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200976bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
977 intel_clock_t *best_clock)
978{
979 int refclk = i9xx_get_refclk(crtc_state, 0);
980
981 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
982 target_clock, refclk, NULL, best_clock);
983}
984
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300985bool intel_crtc_active(struct drm_crtc *crtc)
986{
987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
988
989 /* Be paranoid as we can arrive here with only partial
990 * state retrieved from the hardware during setup.
991 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100992 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300993 * as Haswell has gained clock readout/fastboot support.
994 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000995 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300996 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700997 *
998 * FIXME: The intel_crtc->active here should be switched to
999 * crtc->state->active once we have proper CRTC states wired up
1000 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001001 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001002 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001003 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001004}
1005
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001006enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1007 enum pipe pipe)
1008{
1009 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1011
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001012 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001013}
1014
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001015static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1016{
1017 struct drm_i915_private *dev_priv = dev->dev_private;
1018 u32 reg = PIPEDSL(pipe);
1019 u32 line1, line2;
1020 u32 line_mask;
1021
1022 if (IS_GEN2(dev))
1023 line_mask = DSL_LINEMASK_GEN2;
1024 else
1025 line_mask = DSL_LINEMASK_GEN3;
1026
1027 line1 = I915_READ(reg) & line_mask;
1028 mdelay(5);
1029 line2 = I915_READ(reg) & line_mask;
1030
1031 return line1 == line2;
1032}
1033
Keith Packardab7ad7f2010-10-03 00:33:06 -07001034/*
1035 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001036 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001037 *
1038 * After disabling a pipe, we can't wait for vblank in the usual way,
1039 * spinning on the vblank interrupt status bit, since we won't actually
1040 * see an interrupt when the pipe is disabled.
1041 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001042 * On Gen4 and above:
1043 * wait for the pipe register state bit to turn off
1044 *
1045 * Otherwise:
1046 * wait for the display line value to settle (it usually
1047 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001048 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001050static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001051{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001052 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001053 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001054 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001055 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001056
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001058 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001059
Keith Packardab7ad7f2010-10-03 00:33:06 -07001060 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001061 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1062 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001063 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001064 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001066 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001067 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001068 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001069}
1070
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001071/*
1072 * ibx_digital_port_connected - is the specified port connected?
1073 * @dev_priv: i915 private structure
1074 * @port: the port to test
1075 *
1076 * Returns true if @port is connected, false otherwise.
1077 */
1078bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1079 struct intel_digital_port *port)
1080{
1081 u32 bit;
1082
Damien Lespiauc36346e2012-12-13 16:09:03 +00001083 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001084 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001085 case PORT_B:
1086 bit = SDE_PORTB_HOTPLUG;
1087 break;
1088 case PORT_C:
1089 bit = SDE_PORTC_HOTPLUG;
1090 break;
1091 case PORT_D:
1092 bit = SDE_PORTD_HOTPLUG;
1093 break;
1094 default:
1095 return true;
1096 }
1097 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001098 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001099 case PORT_B:
1100 bit = SDE_PORTB_HOTPLUG_CPT;
1101 break;
1102 case PORT_C:
1103 bit = SDE_PORTC_HOTPLUG_CPT;
1104 break;
1105 case PORT_D:
1106 bit = SDE_PORTD_HOTPLUG_CPT;
1107 break;
1108 default:
1109 return true;
1110 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001111 }
1112
1113 return I915_READ(SDEISR) & bit;
1114}
1115
Jesse Barnesb24e7172011-01-04 15:09:30 -08001116static const char *state_string(bool enabled)
1117{
1118 return enabled ? "on" : "off";
1119}
1120
1121/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001122void assert_pll(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001124{
1125 int reg;
1126 u32 val;
1127 bool cur_state;
1128
1129 reg = DPLL(pipe);
1130 val = I915_READ(reg);
1131 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001132 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133 "PLL state assertion failure (expected %s, current %s)\n",
1134 state_string(state), state_string(cur_state));
1135}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136
Jani Nikula23538ef2013-08-27 15:12:22 +03001137/* XXX: the dsi pll is shared between MIPI DSI ports */
1138static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1139{
1140 u32 val;
1141 bool cur_state;
1142
Ville Syrjäläa5805162015-05-26 20:42:30 +03001143 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001144 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001145 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001146
1147 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001148 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001149 "DSI PLL state assertion failure (expected %s, current %s)\n",
1150 state_string(state), state_string(cur_state));
1151}
1152#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1153#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1154
Daniel Vetter55607e82013-06-16 21:42:39 +02001155struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001156intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001157{
Daniel Vettere2b78262013-06-07 23:10:03 +02001158 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1159
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001160 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001161 return NULL;
1162
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001163 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001164}
1165
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001167void assert_shared_dpll(struct drm_i915_private *dev_priv,
1168 struct intel_shared_dpll *pll,
1169 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001170{
Jesse Barnes040484a2011-01-03 12:14:26 -08001171 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001172 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001173
Chris Wilson92b27b02012-05-20 18:10:50 +01001174 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001175 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001176 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001177
Daniel Vetter53589012013-06-05 13:34:16 +02001178 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001179 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001180 "%s assertion failure (expected %s, current %s)\n",
1181 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001182}
Jesse Barnes040484a2011-01-03 12:14:26 -08001183
1184static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1185 enum pipe pipe, bool state)
1186{
1187 int reg;
1188 u32 val;
1189 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001190 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1191 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001192
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001193 if (HAS_DDI(dev_priv->dev)) {
1194 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001195 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001196 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001197 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001198 } else {
1199 reg = FDI_TX_CTL(pipe);
1200 val = I915_READ(reg);
1201 cur_state = !!(val & FDI_TX_ENABLE);
1202 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001203 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001204 "FDI TX state assertion failure (expected %s, current %s)\n",
1205 state_string(state), state_string(cur_state));
1206}
1207#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1208#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1209
1210static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
1212{
1213 int reg;
1214 u32 val;
1215 bool cur_state;
1216
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001217 reg = FDI_RX_CTL(pipe);
1218 val = I915_READ(reg);
1219 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001220 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001221 "FDI RX state assertion failure (expected %s, current %s)\n",
1222 state_string(state), state_string(cur_state));
1223}
1224#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1225#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1226
1227static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1228 enum pipe pipe)
1229{
1230 int reg;
1231 u32 val;
1232
1233 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001234 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001235 return;
1236
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001237 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001238 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001239 return;
1240
Jesse Barnes040484a2011-01-03 12:14:26 -08001241 reg = FDI_TX_CTL(pipe);
1242 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001244}
1245
Daniel Vetter55607e82013-06-16 21:42:39 +02001246void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1247 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001248{
1249 int reg;
1250 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001251 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001252
1253 reg = FDI_RX_CTL(pipe);
1254 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001255 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001256 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001257 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1258 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001259}
1260
Daniel Vetterb680c372014-09-19 18:27:27 +02001261void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1262 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001264 struct drm_device *dev = dev_priv->dev;
1265 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001266 u32 val;
1267 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001268 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269
Jani Nikulabedd4db2014-08-22 15:04:13 +03001270 if (WARN_ON(HAS_DDI(dev)))
1271 return;
1272
1273 if (HAS_PCH_SPLIT(dev)) {
1274 u32 port_sel;
1275
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001277 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1278
1279 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1280 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1281 panel_pipe = PIPE_B;
1282 /* XXX: else fix for eDP */
1283 } else if (IS_VALLEYVIEW(dev)) {
1284 /* presumably write lock depends on pipe, not port select */
1285 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1286 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001287 } else {
1288 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001289 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1290 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001291 }
1292
1293 val = I915_READ(pp_reg);
1294 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001295 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296 locked = false;
1297
Rob Clarke2c719b2014-12-15 13:56:32 -05001298 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001299 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001300 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301}
1302
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001303static void assert_cursor(struct drm_i915_private *dev_priv,
1304 enum pipe pipe, bool state)
1305{
1306 struct drm_device *dev = dev_priv->dev;
1307 bool cur_state;
1308
Paulo Zanonid9d82082014-02-27 16:30:56 -03001309 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001310 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001311 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001312 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001313
Rob Clarke2c719b2014-12-15 13:56:32 -05001314 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001315 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1316 pipe_name(pipe), state_string(state), state_string(cur_state));
1317}
1318#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1319#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1320
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001321void assert_pipe(struct drm_i915_private *dev_priv,
1322 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001323{
1324 int reg;
1325 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001326 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001327 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1328 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001329
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001330 /* if we need the pipe quirk it must be always on */
1331 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1332 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001333 state = true;
1334
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001335 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001336 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001337 cur_state = false;
1338 } else {
1339 reg = PIPECONF(cpu_transcoder);
1340 val = I915_READ(reg);
1341 cur_state = !!(val & PIPECONF_ENABLE);
1342 }
1343
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001345 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001346 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347}
1348
Chris Wilson931872f2012-01-16 23:01:13 +00001349static void assert_plane(struct drm_i915_private *dev_priv,
1350 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001351{
1352 int reg;
1353 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001354 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001355
1356 reg = DSPCNTR(plane);
1357 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001358 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001359 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001360 "plane %c assertion failure (expected %s, current %s)\n",
1361 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001362}
1363
Chris Wilson931872f2012-01-16 23:01:13 +00001364#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1365#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1366
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe)
1369{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001370 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001371 int reg, i;
1372 u32 val;
1373 int cur_pipe;
1374
Ville Syrjälä653e1022013-06-04 13:49:05 +03001375 /* Primary planes are fixed to pipes on gen4+ */
1376 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001377 reg = DSPCNTR(pipe);
1378 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001380 "plane %c assertion failure, should be disabled but not\n",
1381 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001382 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001383 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001384
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001386 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001387 reg = DSPCNTR(i);
1388 val = I915_READ(reg);
1389 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1390 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001391 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001392 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1393 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001394 }
1395}
1396
Jesse Barnes19332d72013-03-28 09:55:38 -07001397static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe)
1399{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001400 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001401 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001402 u32 val;
1403
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001404 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001405 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001408 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1409 sprite, pipe_name(pipe));
1410 }
1411 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001412 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001413 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001415 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001416 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001417 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001418 }
1419 } else if (INTEL_INFO(dev)->gen >= 7) {
1420 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001421 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001422 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001423 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001424 plane_name(pipe), pipe_name(pipe));
1425 } else if (INTEL_INFO(dev)->gen >= 5) {
1426 reg = DVSCNTR(pipe);
1427 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001428 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001429 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1430 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001431 }
1432}
1433
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001434static void assert_vblank_disabled(struct drm_crtc *crtc)
1435{
Rob Clarke2c719b2014-12-15 13:56:32 -05001436 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001437 drm_crtc_vblank_put(crtc);
1438}
1439
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001440static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001441{
1442 u32 val;
1443 bool enabled;
1444
Rob Clarke2c719b2014-12-15 13:56:32 -05001445 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001446
Jesse Barnes92f25842011-01-04 15:09:34 -08001447 val = I915_READ(PCH_DREF_CONTROL);
1448 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1449 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001450 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001451}
1452
Daniel Vetterab9412b2013-05-03 11:49:46 +02001453static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001455{
1456 int reg;
1457 u32 val;
1458 bool enabled;
1459
Daniel Vetterab9412b2013-05-03 11:49:46 +02001460 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001461 val = I915_READ(reg);
1462 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001463 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001464 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1465 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001466}
1467
Keith Packard4e634382011-08-06 10:39:45 -07001468static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001470{
1471 if ((val & DP_PORT_EN) == 0)
1472 return false;
1473
1474 if (HAS_PCH_CPT(dev_priv->dev)) {
1475 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1476 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1477 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1478 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001479 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1480 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1481 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001482 } else {
1483 if ((val & DP_PIPE_MASK) != (pipe << 30))
1484 return false;
1485 }
1486 return true;
1487}
1488
Keith Packard1519b992011-08-06 10:35:34 -07001489static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1490 enum pipe pipe, u32 val)
1491{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001492 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001493 return false;
1494
1495 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001496 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001497 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001498 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1499 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1500 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001501 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001502 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001503 return false;
1504 }
1505 return true;
1506}
1507
1508static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1509 enum pipe pipe, u32 val)
1510{
1511 if ((val & LVDS_PORT_EN) == 0)
1512 return false;
1513
1514 if (HAS_PCH_CPT(dev_priv->dev)) {
1515 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1516 return false;
1517 } else {
1518 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
1524static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1525 enum pipe pipe, u32 val)
1526{
1527 if ((val & ADPA_DAC_ENABLE) == 0)
1528 return false;
1529 if (HAS_PCH_CPT(dev_priv->dev)) {
1530 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1531 return false;
1532 } else {
1533 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1534 return false;
1535 }
1536 return true;
1537}
1538
Jesse Barnes291906f2011-02-02 12:28:03 -08001539static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001540 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001541{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001542 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001543 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001544 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001545 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001546
Rob Clarke2c719b2014-12-15 13:56:32 -05001547 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001548 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001549 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001550}
1551
1552static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1553 enum pipe pipe, int reg)
1554{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001555 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001556 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001557 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001558 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001559
Rob Clarke2c719b2014-12-15 13:56:32 -05001560 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001561 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001562 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001563}
1564
1565static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1566 enum pipe pipe)
1567{
1568 int reg;
1569 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001570
Keith Packardf0575e92011-07-25 22:12:43 -07001571 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1572 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001574
1575 reg = PCH_ADPA;
1576 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001577 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001578 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001579 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001580
1581 reg = PCH_LVDS;
1582 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001583 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001584 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001585 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001586
Paulo Zanonie2debe92013-02-18 19:00:27 -03001587 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1588 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001590}
1591
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001592static void intel_init_dpio(struct drm_device *dev)
1593{
1594 struct drm_i915_private *dev_priv = dev->dev_private;
1595
1596 if (!IS_VALLEYVIEW(dev))
1597 return;
1598
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001599 /*
1600 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1601 * CHV x1 PHY (DP/HDMI D)
1602 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1603 */
1604 if (IS_CHERRYVIEW(dev)) {
1605 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1607 } else {
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1609 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001610}
1611
Ville Syrjäläd288f652014-10-28 13:20:22 +02001612static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001613 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001614{
Daniel Vetter426115c2013-07-11 22:13:42 +02001615 struct drm_device *dev = crtc->base.dev;
1616 struct drm_i915_private *dev_priv = dev->dev_private;
1617 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001618 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001619
Daniel Vetter426115c2013-07-11 22:13:42 +02001620 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001621
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001622 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001623 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1624
1625 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001626 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001627 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001628
Daniel Vetter426115c2013-07-11 22:13:42 +02001629 I915_WRITE(reg, dpll);
1630 POSTING_READ(reg);
1631 udelay(150);
1632
1633 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1634 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1635
Ville Syrjäläd288f652014-10-28 13:20:22 +02001636 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001637 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001638
1639 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001640 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001641 POSTING_READ(reg);
1642 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001643 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001646 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
1649}
1650
Ville Syrjäläd288f652014-10-28 13:20:22 +02001651static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001652 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001653{
1654 struct drm_device *dev = crtc->base.dev;
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 int pipe = crtc->pipe;
1657 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658 u32 tmp;
1659
1660 assert_pipe_disabled(dev_priv, crtc->pipe);
1661
1662 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1663
Ville Syrjäläa5805162015-05-26 20:42:30 +03001664 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001665
1666 /* Enable back the 10bit clock to display controller */
1667 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1668 tmp |= DPIO_DCLKP_EN;
1669 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1670
Ville Syrjälä54433e92015-05-26 20:42:31 +03001671 mutex_unlock(&dev_priv->sb_lock);
1672
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673 /*
1674 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1675 */
1676 udelay(1);
1677
1678 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001679 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001680
1681 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001682 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001683 DRM_ERROR("PLL %d failed to lock\n", pipe);
1684
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001685 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001686 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001687 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001688}
1689
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001690static int intel_num_dvo_pipes(struct drm_device *dev)
1691{
1692 struct intel_crtc *crtc;
1693 int count = 0;
1694
1695 for_each_intel_crtc(dev, crtc)
1696 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001697 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001698
1699 return count;
1700}
1701
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001702static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001703{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001704 struct drm_device *dev = crtc->base.dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001707 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001708
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001709 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001710
1711 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001712 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001713
1714 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715 if (IS_MOBILE(dev) && !IS_I830(dev))
1716 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001717
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001718 /* Enable DVO 2x clock on both PLLs if necessary */
1719 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1720 /*
1721 * It appears to be important that we don't enable this
1722 * for the current pipe before otherwise configuring the
1723 * PLL. No idea how this should be handled if multiple
1724 * DVO outputs are enabled simultaneosly.
1725 */
1726 dpll |= DPLL_DVO_2X_MODE;
1727 I915_WRITE(DPLL(!crtc->pipe),
1728 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1729 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001730
1731 /* Wait for the clocks to stabilize. */
1732 POSTING_READ(reg);
1733 udelay(150);
1734
1735 if (INTEL_INFO(dev)->gen >= 4) {
1736 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001737 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001738 } else {
1739 /* The pixel multiplier can only be updated once the
1740 * DPLL is enabled and the clocks are stable.
1741 *
1742 * So write it again.
1743 */
1744 I915_WRITE(reg, dpll);
1745 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746
1747 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001748 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001749 POSTING_READ(reg);
1750 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001751 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 POSTING_READ(reg);
1753 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001754 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001755 POSTING_READ(reg);
1756 udelay(150); /* wait for warmup */
1757}
1758
1759/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001760 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001761 * @dev_priv: i915 private structure
1762 * @pipe: pipe PLL to disable
1763 *
1764 * Disable the PLL for @pipe, making sure the pipe is off first.
1765 *
1766 * Note! This is for pre-ILK only.
1767 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001768static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001769{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001770 struct drm_device *dev = crtc->base.dev;
1771 struct drm_i915_private *dev_priv = dev->dev_private;
1772 enum pipe pipe = crtc->pipe;
1773
1774 /* Disable DVO 2x clock on both PLLs if necessary */
1775 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001776 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001777 intel_num_dvo_pipes(dev) == 1) {
1778 I915_WRITE(DPLL(PIPE_B),
1779 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1780 I915_WRITE(DPLL(PIPE_A),
1781 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1782 }
1783
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001784 /* Don't disable pipe or pipe PLLs if needed */
1785 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1786 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001787 return;
1788
1789 /* Make sure the pipe isn't still relying on us */
1790 assert_pipe_disabled(dev_priv, pipe);
1791
Daniel Vetter50b44a42013-06-05 13:34:33 +02001792 I915_WRITE(DPLL(pipe), 0);
1793 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001794}
1795
Jesse Barnesf6071162013-10-01 10:41:38 -07001796static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1797{
1798 u32 val = 0;
1799
1800 /* Make sure the pipe isn't still relying on us */
1801 assert_pipe_disabled(dev_priv, pipe);
1802
Imre Deake5cbfbf2014-01-09 17:08:16 +02001803 /*
1804 * Leave integrated clock source and reference clock enabled for pipe B.
1805 * The latter is needed for VGA hotplug / manual detection.
1806 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001807 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001808 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001809 I915_WRITE(DPLL(pipe), val);
1810 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001811
1812}
1813
1814static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1815{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001816 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001817 u32 val;
1818
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001819 /* Make sure the pipe isn't still relying on us */
1820 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001821
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001822 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001823 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001824 if (pipe != PIPE_A)
1825 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1826 I915_WRITE(DPLL(pipe), val);
1827 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001828
Ville Syrjäläa5805162015-05-26 20:42:30 +03001829 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001830
1831 /* Disable 10bit clock to display controller */
1832 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1833 val &= ~DPIO_DCLKP_EN;
1834 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1835
Ville Syrjälä61407f62014-05-27 16:32:55 +03001836 /* disable left/right clock distribution */
1837 if (pipe != PIPE_B) {
1838 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1839 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1840 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1841 } else {
1842 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1843 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1844 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1845 }
1846
Ville Syrjäläa5805162015-05-26 20:42:30 +03001847 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001848}
1849
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001850void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001851 struct intel_digital_port *dport,
1852 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001853{
1854 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001855 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001857 switch (dport->port) {
1858 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001860 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001861 break;
1862 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001863 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001864 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001865 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001866 break;
1867 case PORT_D:
1868 port_mask = DPLL_PORTD_READY_MASK;
1869 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001870 break;
1871 default:
1872 BUG();
1873 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001874
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001875 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1876 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1877 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001878}
1879
Daniel Vetterb14b1052014-04-24 23:55:13 +02001880static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1881{
1882 struct drm_device *dev = crtc->base.dev;
1883 struct drm_i915_private *dev_priv = dev->dev_private;
1884 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1885
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001886 if (WARN_ON(pll == NULL))
1887 return;
1888
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001889 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001890 if (pll->active == 0) {
1891 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1892 WARN_ON(pll->on);
1893 assert_shared_dpll_disabled(dev_priv, pll);
1894
1895 pll->mode_set(dev_priv, pll);
1896 }
1897}
1898
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001899/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001900 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001901 * @dev_priv: i915 private structure
1902 * @pipe: pipe PLL to enable
1903 *
1904 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1905 * drives the transcoder clock.
1906 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001907static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001908{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001909 struct drm_device *dev = crtc->base.dev;
1910 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001911 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001912
Daniel Vetter87a875b2013-06-05 13:34:19 +02001913 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001914 return;
1915
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001916 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001917 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001918
Damien Lespiau74dd6922014-07-29 18:06:17 +01001919 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001920 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001921 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001922
Daniel Vettercdbd2312013-06-05 13:34:03 +02001923 if (pll->active++) {
1924 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001925 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001926 return;
1927 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001928 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001929
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001930 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1931
Daniel Vetter46edb022013-06-05 13:34:12 +02001932 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001933 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001934 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001935}
1936
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001937static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001938{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001939 struct drm_device *dev = crtc->base.dev;
1940 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001941 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001942
Jesse Barnes92f25842011-01-04 15:09:34 -08001943 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001944 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001945 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001946 return;
1947
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001948 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001949 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Daniel Vetter46edb022013-06-05 13:34:12 +02001951 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1952 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001953 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001954
Chris Wilson48da64a2012-05-13 20:16:12 +01001955 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001956 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001957 return;
1958 }
1959
Daniel Vettere9d69442013-06-05 13:34:15 +02001960 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001961 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001962 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001963 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001964
Daniel Vetter46edb022013-06-05 13:34:12 +02001965 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001966 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001967 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001968
1969 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001970}
1971
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001972static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1973 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001974{
Daniel Vetter23670b322012-11-01 09:15:30 +01001975 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001976 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001978 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001979
1980 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001981 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001984 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001985 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001986
1987 /* FDI must be feeding us bits for PCH ports */
1988 assert_fdi_tx_enabled(dev_priv, pipe);
1989 assert_fdi_rx_enabled(dev_priv, pipe);
1990
Daniel Vetter23670b322012-11-01 09:15:30 +01001991 if (HAS_PCH_CPT(dev)) {
1992 /* Workaround: Set the timing override bit before enabling the
1993 * pch transcoder. */
1994 reg = TRANS_CHICKEN2(pipe);
1995 val = I915_READ(reg);
1996 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1997 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001998 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001999
Daniel Vetterab9412b2013-05-03 11:49:46 +02002000 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002001 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002002 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002003
2004 if (HAS_PCH_IBX(dev_priv->dev)) {
2005 /*
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
2213/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002214 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002215 * @plane: plane to be enabled
2216 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002217 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002218 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002219 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002220static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2221 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002222{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002223 struct drm_device *dev = plane->dev;
2224 struct drm_i915_private *dev_priv = dev->dev_private;
2225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002226
2227 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002228 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002229 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002230
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002231 dev_priv->display.update_primary_plane(crtc, plane->fb,
2232 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002233}
2234
Chris Wilson693db182013-03-05 14:52:39 +00002235static bool need_vtd_wa(struct drm_device *dev)
2236{
2237#ifdef CONFIG_INTEL_IOMMU
2238 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2239 return true;
2240#endif
2241 return false;
2242}
2243
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002244unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002245intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2246 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002247{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002248 unsigned int tile_height;
2249 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002250
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002251 switch (fb_format_modifier) {
2252 case DRM_FORMAT_MOD_NONE:
2253 tile_height = 1;
2254 break;
2255 case I915_FORMAT_MOD_X_TILED:
2256 tile_height = IS_GEN2(dev) ? 16 : 8;
2257 break;
2258 case I915_FORMAT_MOD_Y_TILED:
2259 tile_height = 32;
2260 break;
2261 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002262 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2263 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002264 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002265 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002266 tile_height = 64;
2267 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002268 case 2:
2269 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002270 tile_height = 32;
2271 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002272 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002273 tile_height = 16;
2274 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002275 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002276 WARN_ONCE(1,
2277 "128-bit pixels are not supported for display!");
2278 tile_height = 16;
2279 break;
2280 }
2281 break;
2282 default:
2283 MISSING_CASE(fb_format_modifier);
2284 tile_height = 1;
2285 break;
2286 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002287
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002288 return tile_height;
2289}
2290
2291unsigned int
2292intel_fb_align_height(struct drm_device *dev, unsigned int height,
2293 uint32_t pixel_format, uint64_t fb_format_modifier)
2294{
2295 return ALIGN(height, intel_tile_height(dev, pixel_format,
2296 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002297}
2298
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002299static int
2300intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2301 const struct drm_plane_state *plane_state)
2302{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002303 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002304
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002305 *view = i915_ggtt_view_normal;
2306
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002307 if (!plane_state)
2308 return 0;
2309
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002310 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002311 return 0;
2312
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002313 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002314
2315 info->height = fb->height;
2316 info->pixel_format = fb->pixel_format;
2317 info->pitch = fb->pitches[0];
2318 info->fb_modifier = fb->modifier[0];
2319
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002320 return 0;
2321}
2322
Chris Wilson127bd2a2010-07-23 23:32:05 +01002323int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002324intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2325 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002326 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002327 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002328{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002329 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002330 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002331 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002332 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002333 u32 alignment;
2334 int ret;
2335
Matt Roperebcdd392014-07-09 16:22:11 -07002336 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2337
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002338 switch (fb->modifier[0]) {
2339 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002340 if (INTEL_INFO(dev)->gen >= 9)
2341 alignment = 256 * 1024;
2342 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002343 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002344 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002345 alignment = 4 * 1024;
2346 else
2347 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002348 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002349 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002350 if (INTEL_INFO(dev)->gen >= 9)
2351 alignment = 256 * 1024;
2352 else {
2353 /* pin() will align the object as required by fence */
2354 alignment = 0;
2355 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002357 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002358 case I915_FORMAT_MOD_Yf_TILED:
2359 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2360 "Y tiling bo slipped through, driver bug!\n"))
2361 return -EINVAL;
2362 alignment = 1 * 1024 * 1024;
2363 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002364 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002365 MISSING_CASE(fb->modifier[0]);
2366 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002367 }
2368
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002369 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2370 if (ret)
2371 return ret;
2372
Chris Wilson693db182013-03-05 14:52:39 +00002373 /* Note that the w/a also requires 64 PTE of padding following the
2374 * bo. We currently fill all unused PTE with the shadow page and so
2375 * we should always have valid PTE following the scanout preventing
2376 * the VT-d warning.
2377 */
2378 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2379 alignment = 256 * 1024;
2380
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002381 /*
2382 * Global gtt pte registers are special registers which actually forward
2383 * writes to a chunk of system memory. Which means that there is no risk
2384 * that the register values disappear as soon as we call
2385 * intel_runtime_pm_put(), so it is correct to wrap only the
2386 * pin/unpin/fence and not more.
2387 */
2388 intel_runtime_pm_get(dev_priv);
2389
Chris Wilsonce453d82011-02-21 14:43:56 +00002390 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002391 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002392 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002393 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002394 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002395
2396 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2397 * fence, whereas 965+ only requires a fence if using
2398 * framebuffer compression. For simplicity, we always install
2399 * a fence as the cost is not that onerous.
2400 */
Chris Wilson06d98132012-04-17 15:31:24 +01002401 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002402 if (ret)
2403 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002404
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002405 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002406
Chris Wilsonce453d82011-02-21 14:43:56 +00002407 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002408 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002409 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002410
2411err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002412 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002413err_interruptible:
2414 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002415 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002416 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002417}
2418
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002419static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2420 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002421{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002422 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002423 struct i915_ggtt_view view;
2424 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002425
Matt Roperebcdd392014-07-09 16:22:11 -07002426 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2427
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002428 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2429 WARN_ONCE(ret, "Couldn't get view from plane state!");
2430
Chris Wilson1690e1e2011-12-14 13:57:08 +01002431 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002432 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002433}
2434
Daniel Vetterc2c75132012-07-05 12:17:30 +02002435/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2436 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002437unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2438 unsigned int tiling_mode,
2439 unsigned int cpp,
2440 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002441{
Chris Wilsonbc752862013-02-21 20:04:31 +00002442 if (tiling_mode != I915_TILING_NONE) {
2443 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002444
Chris Wilsonbc752862013-02-21 20:04:31 +00002445 tile_rows = *y / 8;
2446 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002447
Chris Wilsonbc752862013-02-21 20:04:31 +00002448 tiles = *x / (512/cpp);
2449 *x %= 512/cpp;
2450
2451 return tile_rows * pitch * 8 + tiles * 4096;
2452 } else {
2453 unsigned int offset;
2454
2455 offset = *y * pitch + *x * cpp;
2456 *y = 0;
2457 *x = (offset & 4095) / cpp;
2458 return offset & -4096;
2459 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002460}
2461
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002462static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002463{
2464 switch (format) {
2465 case DISPPLANE_8BPP:
2466 return DRM_FORMAT_C8;
2467 case DISPPLANE_BGRX555:
2468 return DRM_FORMAT_XRGB1555;
2469 case DISPPLANE_BGRX565:
2470 return DRM_FORMAT_RGB565;
2471 default:
2472 case DISPPLANE_BGRX888:
2473 return DRM_FORMAT_XRGB8888;
2474 case DISPPLANE_RGBX888:
2475 return DRM_FORMAT_XBGR8888;
2476 case DISPPLANE_BGRX101010:
2477 return DRM_FORMAT_XRGB2101010;
2478 case DISPPLANE_RGBX101010:
2479 return DRM_FORMAT_XBGR2101010;
2480 }
2481}
2482
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002483static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2484{
2485 switch (format) {
2486 case PLANE_CTL_FORMAT_RGB_565:
2487 return DRM_FORMAT_RGB565;
2488 default:
2489 case PLANE_CTL_FORMAT_XRGB_8888:
2490 if (rgb_order) {
2491 if (alpha)
2492 return DRM_FORMAT_ABGR8888;
2493 else
2494 return DRM_FORMAT_XBGR8888;
2495 } else {
2496 if (alpha)
2497 return DRM_FORMAT_ARGB8888;
2498 else
2499 return DRM_FORMAT_XRGB8888;
2500 }
2501 case PLANE_CTL_FORMAT_XRGB_2101010:
2502 if (rgb_order)
2503 return DRM_FORMAT_XBGR2101010;
2504 else
2505 return DRM_FORMAT_XRGB2101010;
2506 }
2507}
2508
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002509static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002510intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2511 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002512{
2513 struct drm_device *dev = crtc->base.dev;
2514 struct drm_i915_gem_object *obj = NULL;
2515 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002516 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002517 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2518 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2519 PAGE_SIZE);
2520
2521 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002522
Chris Wilsonff2652e2014-03-10 08:07:02 +00002523 if (plane_config->size == 0)
2524 return false;
2525
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002526 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2527 base_aligned,
2528 base_aligned,
2529 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002530 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002531 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002532
Damien Lespiau49af4492015-01-20 12:51:44 +00002533 obj->tiling_mode = plane_config->tiling;
2534 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002535 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002537 mode_cmd.pixel_format = fb->pixel_format;
2538 mode_cmd.width = fb->width;
2539 mode_cmd.height = fb->height;
2540 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002541 mode_cmd.modifier[0] = fb->modifier[0];
2542 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002543
2544 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002545 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002546 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002547 DRM_DEBUG_KMS("intel fb init failed\n");
2548 goto out_unref_obj;
2549 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002550 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551
Daniel Vetterf6936e22015-03-26 12:17:05 +01002552 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002553 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554
2555out_unref_obj:
2556 drm_gem_object_unreference(&obj->base);
2557 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 return false;
2559}
2560
Matt Roperafd65eb2015-02-03 13:10:04 -08002561/* Update plane->state->fb to match plane->fb after driver-internal updates */
2562static void
2563update_state_fb(struct drm_plane *plane)
2564{
2565 if (plane->fb == plane->state->fb)
2566 return;
2567
2568 if (plane->state->fb)
2569 drm_framebuffer_unreference(plane->state->fb);
2570 plane->state->fb = plane->fb;
2571 if (plane->state->fb)
2572 drm_framebuffer_reference(plane->state->fb);
2573}
2574
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002575static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002576intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2577 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002578{
2579 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002580 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 struct drm_crtc *c;
2582 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002583 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002584 struct drm_plane *primary = intel_crtc->base.primary;
2585 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002586
Damien Lespiau2d140302015-02-05 17:22:18 +00002587 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002588 return;
2589
Daniel Vetterf6936e22015-03-26 12:17:05 +01002590 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591 fb = &plane_config->fb->base;
2592 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002593 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594
Damien Lespiau2d140302015-02-05 17:22:18 +00002595 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596
2597 /*
2598 * Failed to alloc the obj, check to see if we should share
2599 * an fb with another CRTC instead
2600 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002601 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602 i = to_intel_crtc(c);
2603
2604 if (c == &intel_crtc->base)
2605 continue;
2606
Matt Roper2ff8fde2014-07-08 07:50:07 -07002607 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002608 continue;
2609
Daniel Vetter88595ac2015-03-26 12:42:24 +01002610 fb = c->primary->fb;
2611 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002612 continue;
2613
Daniel Vetter88595ac2015-03-26 12:42:24 +01002614 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002615 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002616 drm_framebuffer_reference(fb);
2617 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002618 }
2619 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002620
2621 return;
2622
2623valid_fb:
2624 obj = intel_fb_obj(fb);
2625 if (obj->tiling_mode != I915_TILING_NONE)
2626 dev_priv->preserve_bios_swizzle = true;
2627
2628 primary->fb = fb;
2629 primary->state->crtc = &intel_crtc->base;
2630 primary->crtc = &intel_crtc->base;
2631 update_state_fb(primary);
2632 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002633}
2634
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002635static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2636 struct drm_framebuffer *fb,
2637 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002638{
2639 struct drm_device *dev = crtc->dev;
2640 struct drm_i915_private *dev_priv = dev->dev_private;
2641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002642 struct drm_plane *primary = crtc->primary;
2643 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002644 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002645 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002646 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002647 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002648 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302649 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002650
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002651 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002652 I915_WRITE(reg, 0);
2653 if (INTEL_INFO(dev)->gen >= 4)
2654 I915_WRITE(DSPSURF(plane), 0);
2655 else
2656 I915_WRITE(DSPADDR(plane), 0);
2657 POSTING_READ(reg);
2658 return;
2659 }
2660
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002661 obj = intel_fb_obj(fb);
2662 if (WARN_ON(obj == NULL))
2663 return;
2664
2665 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2666
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002667 dspcntr = DISPPLANE_GAMMA_ENABLE;
2668
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002669 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002670
2671 if (INTEL_INFO(dev)->gen < 4) {
2672 if (intel_crtc->pipe == PIPE_B)
2673 dspcntr |= DISPPLANE_SEL_PIPE_B;
2674
2675 /* pipesrc and dspsize control the size that is scaled from,
2676 * which should always be the user's requested size.
2677 */
2678 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002679 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2680 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002681 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002682 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2683 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002684 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2685 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002686 I915_WRITE(PRIMPOS(plane), 0);
2687 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002688 }
2689
Ville Syrjälä57779d02012-10-31 17:50:14 +02002690 switch (fb->pixel_format) {
2691 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002692 dspcntr |= DISPPLANE_8BPP;
2693 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002694 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002696 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002697 case DRM_FORMAT_RGB565:
2698 dspcntr |= DISPPLANE_BGRX565;
2699 break;
2700 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701 dspcntr |= DISPPLANE_BGRX888;
2702 break;
2703 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002704 dspcntr |= DISPPLANE_RGBX888;
2705 break;
2706 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 dspcntr |= DISPPLANE_BGRX101010;
2708 break;
2709 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002710 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002711 break;
2712 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002713 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002714 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002715
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002716 if (INTEL_INFO(dev)->gen >= 4 &&
2717 obj->tiling_mode != I915_TILING_NONE)
2718 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002719
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002720 if (IS_G4X(dev))
2721 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2722
Ville Syrjäläb98971272014-08-27 16:51:22 +03002723 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002724
Daniel Vetterc2c75132012-07-05 12:17:30 +02002725 if (INTEL_INFO(dev)->gen >= 4) {
2726 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002727 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002728 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002729 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002730 linear_offset -= intel_crtc->dspaddr_offset;
2731 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002732 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002733 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002734
Matt Roper8e7d6882015-01-21 16:35:41 -08002735 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302736 dspcntr |= DISPPLANE_ROTATE_180;
2737
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002738 x += (intel_crtc->config->pipe_src_w - 1);
2739 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302740
2741 /* Finding the last pixel of the last line of the display
2742 data and adding to linear_offset*/
2743 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002744 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2745 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302746 }
2747
2748 I915_WRITE(reg, dspcntr);
2749
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002750 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002751 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002752 I915_WRITE(DSPSURF(plane),
2753 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002754 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002755 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002756 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002757 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002758 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002759}
2760
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002761static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2762 struct drm_framebuffer *fb,
2763 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002764{
2765 struct drm_device *dev = crtc->dev;
2766 struct drm_i915_private *dev_priv = dev->dev_private;
2767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002768 struct drm_plane *primary = crtc->primary;
2769 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002770 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002771 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002772 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002774 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302775 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002777 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002778 I915_WRITE(reg, 0);
2779 I915_WRITE(DSPSURF(plane), 0);
2780 POSTING_READ(reg);
2781 return;
2782 }
2783
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002784 obj = intel_fb_obj(fb);
2785 if (WARN_ON(obj == NULL))
2786 return;
2787
2788 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2789
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002790 dspcntr = DISPPLANE_GAMMA_ENABLE;
2791
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002792 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002793
2794 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2795 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2796
Ville Syrjälä57779d02012-10-31 17:50:14 +02002797 switch (fb->pixel_format) {
2798 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799 dspcntr |= DISPPLANE_8BPP;
2800 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002801 case DRM_FORMAT_RGB565:
2802 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002803 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002804 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 dspcntr |= DISPPLANE_BGRX888;
2806 break;
2807 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 dspcntr |= DISPPLANE_RGBX888;
2809 break;
2810 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 dspcntr |= DISPPLANE_BGRX101010;
2812 break;
2813 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002815 break;
2816 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002817 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002818 }
2819
2820 if (obj->tiling_mode != I915_TILING_NONE)
2821 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002823 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002824 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002825
Ville Syrjäläb98971272014-08-27 16:51:22 +03002826 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002827 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002828 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002829 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002830 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002831 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002832 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302833 dspcntr |= DISPPLANE_ROTATE_180;
2834
2835 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002836 x += (intel_crtc->config->pipe_src_w - 1);
2837 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302838
2839 /* Finding the last pixel of the last line of the display
2840 data and adding to linear_offset*/
2841 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002842 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2843 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302844 }
2845 }
2846
2847 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002849 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002850 I915_WRITE(DSPSURF(plane),
2851 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002852 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002853 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2854 } else {
2855 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2856 I915_WRITE(DSPLINOFF(plane), linear_offset);
2857 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002858 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002859}
2860
Damien Lespiaub3218032015-02-27 11:15:18 +00002861u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2862 uint32_t pixel_format)
2863{
2864 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2865
2866 /*
2867 * The stride is either expressed as a multiple of 64 bytes
2868 * chunks for linear buffers or in number of tiles for tiled
2869 * buffers.
2870 */
2871 switch (fb_modifier) {
2872 case DRM_FORMAT_MOD_NONE:
2873 return 64;
2874 case I915_FORMAT_MOD_X_TILED:
2875 if (INTEL_INFO(dev)->gen == 2)
2876 return 128;
2877 return 512;
2878 case I915_FORMAT_MOD_Y_TILED:
2879 /* No need to check for old gens and Y tiling since this is
2880 * about the display engine and those will be blocked before
2881 * we get here.
2882 */
2883 return 128;
2884 case I915_FORMAT_MOD_Yf_TILED:
2885 if (bits_per_pixel == 8)
2886 return 64;
2887 else
2888 return 128;
2889 default:
2890 MISSING_CASE(fb_modifier);
2891 return 64;
2892 }
2893}
2894
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002895unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2896 struct drm_i915_gem_object *obj)
2897{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002898 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002899
2900 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002901 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002902
2903 return i915_gem_obj_ggtt_offset_view(obj, view);
2904}
2905
Chandra Kondurua1b22782015-04-07 15:28:45 -07002906/*
2907 * This function detaches (aka. unbinds) unused scalers in hardware
2908 */
2909void skl_detach_scalers(struct intel_crtc *intel_crtc)
2910{
2911 struct drm_device *dev;
2912 struct drm_i915_private *dev_priv;
2913 struct intel_crtc_scaler_state *scaler_state;
2914 int i;
2915
2916 if (!intel_crtc || !intel_crtc->config)
2917 return;
2918
2919 dev = intel_crtc->base.dev;
2920 dev_priv = dev->dev_private;
2921 scaler_state = &intel_crtc->config->scaler_state;
2922
2923 /* loop through and disable scalers that aren't in use */
2924 for (i = 0; i < intel_crtc->num_scalers; i++) {
2925 if (!scaler_state->scalers[i].in_use) {
2926 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2927 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2928 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2929 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2930 intel_crtc->base.base.id, intel_crtc->pipe, i);
2931 }
2932 }
2933}
2934
Chandra Konduru6156a452015-04-27 13:48:39 -07002935u32 skl_plane_ctl_format(uint32_t pixel_format)
2936{
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002938 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002941 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002942 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002943 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 /*
2947 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2948 * to be already pre-multiplied. We need to add a knob (or a different
2949 * DRM_FORMAT) for user-space to configure that.
2950 */
2951 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002970 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002972
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974}
2975
2976u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2977{
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 switch (fb_modifier) {
2979 case DRM_FORMAT_MOD_NONE:
2980 break;
2981 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002984 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002986 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 default:
2988 MISSING_CASE(fb_modifier);
2989 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002990
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002991 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992}
2993
2994u32 skl_plane_ctl_rotation(unsigned int rotation)
2995{
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 switch (rotation) {
2997 case BIT(DRM_ROTATE_0):
2998 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302999 /*
3000 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3001 * while i915 HW rotation is clockwise, thats why this swapping.
3002 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303004 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003005 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003006 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003007 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303008 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 default:
3010 MISSING_CASE(rotation);
3011 }
3012
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003013 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003014}
3015
Damien Lespiau70d21f02013-07-03 21:06:04 +01003016static void skylake_update_primary_plane(struct drm_crtc *crtc,
3017 struct drm_framebuffer *fb,
3018 int x, int y)
3019{
3020 struct drm_device *dev = crtc->dev;
3021 struct drm_i915_private *dev_priv = dev->dev_private;
3022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003023 struct drm_plane *plane = crtc->primary;
3024 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003025 struct drm_i915_gem_object *obj;
3026 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303027 u32 plane_ctl, stride_div, stride;
3028 u32 tile_height, plane_offset, plane_size;
3029 unsigned int rotation;
3030 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003031 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003032 struct intel_crtc_state *crtc_state = intel_crtc->config;
3033 struct intel_plane_state *plane_state;
3034 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3035 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3036 int scaler_id = -1;
3037
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003039
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003040 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003041 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3042 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3043 POSTING_READ(PLANE_CTL(pipe, 0));
3044 return;
3045 }
3046
3047 plane_ctl = PLANE_CTL_ENABLE |
3048 PLANE_CTL_PIPE_GAMMA_ENABLE |
3049 PLANE_CTL_PIPE_CSC_ENABLE;
3050
Chandra Konduru6156a452015-04-27 13:48:39 -07003051 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3052 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003053 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303054
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303055 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003056 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003057
Damien Lespiaub3218032015-02-27 11:15:18 +00003058 obj = intel_fb_obj(fb);
3059 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3060 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303061 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3062
Chandra Konduru6156a452015-04-27 13:48:39 -07003063 /*
3064 * FIXME: intel_plane_state->src, dst aren't set when transitional
3065 * update_plane helpers are called from legacy paths.
3066 * Once full atomic crtc is available, below check can be avoided.
3067 */
3068 if (drm_rect_width(&plane_state->src)) {
3069 scaler_id = plane_state->scaler_id;
3070 src_x = plane_state->src.x1 >> 16;
3071 src_y = plane_state->src.y1 >> 16;
3072 src_w = drm_rect_width(&plane_state->src) >> 16;
3073 src_h = drm_rect_height(&plane_state->src) >> 16;
3074 dst_x = plane_state->dst.x1;
3075 dst_y = plane_state->dst.y1;
3076 dst_w = drm_rect_width(&plane_state->dst);
3077 dst_h = drm_rect_height(&plane_state->dst);
3078
3079 WARN_ON(x != src_x || y != src_y);
3080 } else {
3081 src_w = intel_crtc->config->pipe_src_w;
3082 src_h = intel_crtc->config->pipe_src_h;
3083 }
3084
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303085 if (intel_rotation_90_or_270(rotation)) {
3086 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003087 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303088 fb->modifier[0]);
3089 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003090 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303091 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003092 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303093 } else {
3094 stride = fb->pitches[0] / stride_div;
3095 x_offset = x;
3096 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003097 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303098 }
3099 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003100
Damien Lespiau70d21f02013-07-03 21:06:04 +01003101 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303102 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3103 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3104 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003105
3106 if (scaler_id >= 0) {
3107 uint32_t ps_ctrl = 0;
3108
3109 WARN_ON(!dst_w || !dst_h);
3110 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3111 crtc_state->scaler_state.scalers[scaler_id].mode;
3112 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3113 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3114 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3115 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3116 I915_WRITE(PLANE_POS(pipe, 0), 0);
3117 } else {
3118 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3119 }
3120
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003121 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003122
3123 POSTING_READ(PLANE_SURF(pipe, 0));
3124}
3125
Jesse Barnes17638cd2011-06-24 12:19:23 -07003126/* Assume fb object is pinned & idle & fenced and just update base pointers */
3127static int
3128intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3129 int x, int y, enum mode_set_atomic state)
3130{
3131 struct drm_device *dev = crtc->dev;
3132 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003133
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003134 if (dev_priv->display.disable_fbc)
3135 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003136
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003137 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3138
3139 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003140}
3141
Ville Syrjälä75147472014-11-24 18:28:11 +02003142static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003143{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003144 struct drm_crtc *crtc;
3145
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003146 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3148 enum plane plane = intel_crtc->plane;
3149
3150 intel_prepare_page_flip(dev, plane);
3151 intel_finish_page_flip_plane(dev, plane);
3152 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003153}
3154
3155static void intel_update_primary_planes(struct drm_device *dev)
3156{
3157 struct drm_i915_private *dev_priv = dev->dev_private;
3158 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003159
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
Rob Clark51fd3712013-11-19 12:10:12 -05003163 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003164 /*
3165 * FIXME: Once we have proper support for primary planes (and
3166 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003167 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003168 */
Matt Roperf4510a22014-04-01 15:22:40 -07003169 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003170 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003171 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003172 crtc->x,
3173 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003174 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003175 }
3176}
3177
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003178void intel_crtc_reset(struct intel_crtc *crtc)
3179{
3180 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3181
3182 if (!crtc->active)
3183 return;
3184
3185 intel_crtc_disable_planes(&crtc->base);
3186 dev_priv->display.crtc_disable(&crtc->base);
3187 dev_priv->display.crtc_enable(&crtc->base);
3188 intel_crtc_enable_planes(&crtc->base);
3189}
3190
Ville Syrjälä75147472014-11-24 18:28:11 +02003191void intel_prepare_reset(struct drm_device *dev)
3192{
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003193 struct drm_i915_private *dev_priv = to_i915(dev);
3194 struct intel_crtc *crtc;
3195
Ville Syrjälä75147472014-11-24 18:28:11 +02003196 /* no reset support for gen2 */
3197 if (IS_GEN2(dev))
3198 return;
3199
3200 /* reset doesn't touch the display */
3201 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3202 return;
3203
3204 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003205
3206 /*
3207 * Disabling the crtcs gracefully seems nicer. Also the
3208 * g33 docs say we should at least disable all the planes.
3209 */
3210 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003211 if (!crtc->active)
3212 continue;
3213
3214 intel_crtc_disable_planes(&crtc->base);
3215 dev_priv->display.crtc_disable(&crtc->base);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003216 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003217}
3218
3219void intel_finish_reset(struct drm_device *dev)
3220{
3221 struct drm_i915_private *dev_priv = to_i915(dev);
3222
3223 /*
3224 * Flips in the rings will be nuked by the reset,
3225 * so complete all pending flips so that user space
3226 * will get its events and not get stuck.
3227 */
3228 intel_complete_page_flips(dev);
3229
3230 /* no reset support for gen2 */
3231 if (IS_GEN2(dev))
3232 return;
3233
3234 /* reset doesn't touch the display */
3235 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3236 /*
3237 * Flips in the rings have been nuked by the reset,
3238 * so update the base address of all primary
3239 * planes to the the last fb to make sure we're
3240 * showing the correct fb after a reset.
3241 */
3242 intel_update_primary_planes(dev);
3243 return;
3244 }
3245
3246 /*
3247 * The display has been reset as well,
3248 * so need a full re-initialization.
3249 */
3250 intel_runtime_pm_disable_interrupts(dev_priv);
3251 intel_runtime_pm_enable_interrupts(dev_priv);
3252
3253 intel_modeset_init_hw(dev);
3254
3255 spin_lock_irq(&dev_priv->irq_lock);
3256 if (dev_priv->display.hpd_irq_setup)
3257 dev_priv->display.hpd_irq_setup(dev);
3258 spin_unlock_irq(&dev_priv->irq_lock);
3259
3260 intel_modeset_setup_hw_state(dev, true);
3261
3262 intel_hpd_init(dev_priv);
3263
3264 drm_modeset_unlock_all(dev);
3265}
3266
Chris Wilson2e2f3512015-04-27 13:41:14 +01003267static void
Chris Wilson14667a42012-04-03 17:58:35 +01003268intel_finish_fb(struct drm_framebuffer *old_fb)
3269{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003270 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003271 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003272 bool was_interruptible = dev_priv->mm.interruptible;
3273 int ret;
3274
Chris Wilson14667a42012-04-03 17:58:35 +01003275 /* Big Hammer, we also need to ensure that any pending
3276 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3277 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003278 * framebuffer. Note that we rely on userspace rendering
3279 * into the buffer attached to the pipe they are waiting
3280 * on. If not, userspace generates a GPU hang with IPEHR
3281 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003282 *
3283 * This should only fail upon a hung GPU, in which case we
3284 * can safely continue.
3285 */
3286 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003287 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003288 dev_priv->mm.interruptible = was_interruptible;
3289
Chris Wilson2e2f3512015-04-27 13:41:14 +01003290 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003291}
3292
Chris Wilson7d5e3792014-03-04 13:15:08 +00003293static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3294{
3295 struct drm_device *dev = crtc->dev;
3296 struct drm_i915_private *dev_priv = dev->dev_private;
3297 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003298 bool pending;
3299
3300 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3301 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3302 return false;
3303
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003304 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003305 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003306 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003307
3308 return pending;
3309}
3310
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003311static void intel_update_pipe_size(struct intel_crtc *crtc)
3312{
3313 struct drm_device *dev = crtc->base.dev;
3314 struct drm_i915_private *dev_priv = dev->dev_private;
3315 const struct drm_display_mode *adjusted_mode;
3316
3317 if (!i915.fastboot)
3318 return;
3319
3320 /*
3321 * Update pipe size and adjust fitter if needed: the reason for this is
3322 * that in compute_mode_changes we check the native mode (not the pfit
3323 * mode) to see if we can flip rather than do a full mode set. In the
3324 * fastboot case, we'll flip, but if we don't update the pipesrc and
3325 * pfit state, we'll end up with a big fb scanned out into the wrong
3326 * sized surface.
3327 *
3328 * To fix this properly, we need to hoist the checks up into
3329 * compute_mode_changes (or above), check the actual pfit state and
3330 * whether the platform allows pfit disable with pipe active, and only
3331 * then update the pipesrc and pfit state, even on the flip path.
3332 */
3333
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003334 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003335
3336 I915_WRITE(PIPESRC(crtc->pipe),
3337 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3338 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003339 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003340 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3341 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003342 I915_WRITE(PF_CTL(crtc->pipe), 0);
3343 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3344 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3345 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003346 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3347 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003348}
3349
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003350static void intel_fdi_normal_train(struct drm_crtc *crtc)
3351{
3352 struct drm_device *dev = crtc->dev;
3353 struct drm_i915_private *dev_priv = dev->dev_private;
3354 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3355 int pipe = intel_crtc->pipe;
3356 u32 reg, temp;
3357
3358 /* enable normal train */
3359 reg = FDI_TX_CTL(pipe);
3360 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003361 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003362 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3363 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003364 } else {
3365 temp &= ~FDI_LINK_TRAIN_NONE;
3366 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003367 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003368 I915_WRITE(reg, temp);
3369
3370 reg = FDI_RX_CTL(pipe);
3371 temp = I915_READ(reg);
3372 if (HAS_PCH_CPT(dev)) {
3373 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3374 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3375 } else {
3376 temp &= ~FDI_LINK_TRAIN_NONE;
3377 temp |= FDI_LINK_TRAIN_NONE;
3378 }
3379 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3380
3381 /* wait one idle pattern time */
3382 POSTING_READ(reg);
3383 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003384
3385 /* IVB wants error correction enabled */
3386 if (IS_IVYBRIDGE(dev))
3387 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3388 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003389}
3390
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003391/* The FDI link training functions for ILK/Ibexpeak. */
3392static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3393{
3394 struct drm_device *dev = crtc->dev;
3395 struct drm_i915_private *dev_priv = dev->dev_private;
3396 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3397 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003398 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003399
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003400 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003401 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003402
Adam Jacksone1a44742010-06-25 15:32:14 -04003403 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3404 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 reg = FDI_RX_IMR(pipe);
3406 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003407 temp &= ~FDI_RX_SYMBOL_LOCK;
3408 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 I915_WRITE(reg, temp);
3410 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003411 udelay(150);
3412
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003413 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003414 reg = FDI_TX_CTL(pipe);
3415 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003416 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003417 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003418 temp &= ~FDI_LINK_TRAIN_NONE;
3419 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003420 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003421
Chris Wilson5eddb702010-09-11 13:48:45 +01003422 reg = FDI_RX_CTL(pipe);
3423 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003424 temp &= ~FDI_LINK_TRAIN_NONE;
3425 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3427
3428 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003429 udelay(150);
3430
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003431 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003432 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3433 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3434 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003435
Chris Wilson5eddb702010-09-11 13:48:45 +01003436 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003437 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3440
3441 if ((temp & FDI_RX_BIT_LOCK)) {
3442 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003443 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 break;
3445 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003446 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003447 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003449
3450 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 reg = FDI_TX_CTL(pipe);
3452 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003453 temp &= ~FDI_LINK_TRAIN_NONE;
3454 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 reg = FDI_RX_CTL(pipe);
3458 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 temp &= ~FDI_LINK_TRAIN_NONE;
3460 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003461 I915_WRITE(reg, temp);
3462
3463 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 udelay(150);
3465
Chris Wilson5eddb702010-09-11 13:48:45 +01003466 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003467 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003468 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003469 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3470
3471 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003472 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473 DRM_DEBUG_KMS("FDI train 2 done.\n");
3474 break;
3475 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003477 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003478 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003479
3480 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003481
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482}
3483
Akshay Joshi0206e352011-08-16 15:34:10 -04003484static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003485 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3486 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3487 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3488 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3489};
3490
3491/* The FDI link training functions for SNB/Cougarpoint. */
3492static void gen6_fdi_link_train(struct drm_crtc *crtc)
3493{
3494 struct drm_device *dev = crtc->dev;
3495 struct drm_i915_private *dev_priv = dev->dev_private;
3496 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3497 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003498 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499
Adam Jacksone1a44742010-06-25 15:32:14 -04003500 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3501 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003502 reg = FDI_RX_IMR(pipe);
3503 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003504 temp &= ~FDI_RX_SYMBOL_LOCK;
3505 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003506 I915_WRITE(reg, temp);
3507
3508 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003509 udelay(150);
3510
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 reg = FDI_TX_CTL(pipe);
3513 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003514 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003515 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003516 temp &= ~FDI_LINK_TRAIN_NONE;
3517 temp |= FDI_LINK_TRAIN_PATTERN_1;
3518 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3519 /* SNB-B */
3520 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003521 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003522
Daniel Vetterd74cf322012-10-26 10:58:13 +02003523 I915_WRITE(FDI_RX_MISC(pipe),
3524 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3525
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 reg = FDI_RX_CTL(pipe);
3527 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003528 if (HAS_PCH_CPT(dev)) {
3529 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3530 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3531 } else {
3532 temp &= ~FDI_LINK_TRAIN_NONE;
3533 temp |= FDI_LINK_TRAIN_PATTERN_1;
3534 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003535 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3536
3537 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003538 udelay(150);
3539
Akshay Joshi0206e352011-08-16 15:34:10 -04003540 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003541 reg = FDI_TX_CTL(pipe);
3542 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3544 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003545 I915_WRITE(reg, temp);
3546
3547 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003548 udelay(500);
3549
Sean Paulfa37d392012-03-02 12:53:39 -05003550 for (retry = 0; retry < 5; retry++) {
3551 reg = FDI_RX_IIR(pipe);
3552 temp = I915_READ(reg);
3553 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3554 if (temp & FDI_RX_BIT_LOCK) {
3555 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3556 DRM_DEBUG_KMS("FDI train 1 done.\n");
3557 break;
3558 }
3559 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003560 }
Sean Paulfa37d392012-03-02 12:53:39 -05003561 if (retry < 5)
3562 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003563 }
3564 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003565 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003566
3567 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003568 reg = FDI_TX_CTL(pipe);
3569 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003570 temp &= ~FDI_LINK_TRAIN_NONE;
3571 temp |= FDI_LINK_TRAIN_PATTERN_2;
3572 if (IS_GEN6(dev)) {
3573 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3574 /* SNB-B */
3575 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3576 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003577 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003578
Chris Wilson5eddb702010-09-11 13:48:45 +01003579 reg = FDI_RX_CTL(pipe);
3580 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003581 if (HAS_PCH_CPT(dev)) {
3582 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3583 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3584 } else {
3585 temp &= ~FDI_LINK_TRAIN_NONE;
3586 temp |= FDI_LINK_TRAIN_PATTERN_2;
3587 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003588 I915_WRITE(reg, temp);
3589
3590 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003591 udelay(150);
3592
Akshay Joshi0206e352011-08-16 15:34:10 -04003593 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003594 reg = FDI_TX_CTL(pipe);
3595 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3597 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003598 I915_WRITE(reg, temp);
3599
3600 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003601 udelay(500);
3602
Sean Paulfa37d392012-03-02 12:53:39 -05003603 for (retry = 0; retry < 5; retry++) {
3604 reg = FDI_RX_IIR(pipe);
3605 temp = I915_READ(reg);
3606 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3607 if (temp & FDI_RX_SYMBOL_LOCK) {
3608 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3609 DRM_DEBUG_KMS("FDI train 2 done.\n");
3610 break;
3611 }
3612 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003613 }
Sean Paulfa37d392012-03-02 12:53:39 -05003614 if (retry < 5)
3615 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003616 }
3617 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003618 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003619
3620 DRM_DEBUG_KMS("FDI train done.\n");
3621}
3622
Jesse Barnes357555c2011-04-28 15:09:55 -07003623/* Manual link training for Ivy Bridge A0 parts */
3624static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3625{
3626 struct drm_device *dev = crtc->dev;
3627 struct drm_i915_private *dev_priv = dev->dev_private;
3628 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3629 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003630 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003631
3632 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3633 for train result */
3634 reg = FDI_RX_IMR(pipe);
3635 temp = I915_READ(reg);
3636 temp &= ~FDI_RX_SYMBOL_LOCK;
3637 temp &= ~FDI_RX_BIT_LOCK;
3638 I915_WRITE(reg, temp);
3639
3640 POSTING_READ(reg);
3641 udelay(150);
3642
Daniel Vetter01a415f2012-10-27 15:58:40 +02003643 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3644 I915_READ(FDI_RX_IIR(pipe)));
3645
Jesse Barnes139ccd32013-08-19 11:04:55 -07003646 /* Try each vswing and preemphasis setting twice before moving on */
3647 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3648 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003649 reg = FDI_TX_CTL(pipe);
3650 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003651 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3652 temp &= ~FDI_TX_ENABLE;
3653 I915_WRITE(reg, temp);
3654
3655 reg = FDI_RX_CTL(pipe);
3656 temp = I915_READ(reg);
3657 temp &= ~FDI_LINK_TRAIN_AUTO;
3658 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3659 temp &= ~FDI_RX_ENABLE;
3660 I915_WRITE(reg, temp);
3661
3662 /* enable CPU FDI TX and PCH FDI RX */
3663 reg = FDI_TX_CTL(pipe);
3664 temp = I915_READ(reg);
3665 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003666 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003667 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003668 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003669 temp |= snb_b_fdi_train_param[j/2];
3670 temp |= FDI_COMPOSITE_SYNC;
3671 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3672
3673 I915_WRITE(FDI_RX_MISC(pipe),
3674 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3675
3676 reg = FDI_RX_CTL(pipe);
3677 temp = I915_READ(reg);
3678 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3679 temp |= FDI_COMPOSITE_SYNC;
3680 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3681
3682 POSTING_READ(reg);
3683 udelay(1); /* should be 0.5us */
3684
3685 for (i = 0; i < 4; i++) {
3686 reg = FDI_RX_IIR(pipe);
3687 temp = I915_READ(reg);
3688 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3689
3690 if (temp & FDI_RX_BIT_LOCK ||
3691 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3692 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3693 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3694 i);
3695 break;
3696 }
3697 udelay(1); /* should be 0.5us */
3698 }
3699 if (i == 4) {
3700 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3701 continue;
3702 }
3703
3704 /* Train 2 */
3705 reg = FDI_TX_CTL(pipe);
3706 temp = I915_READ(reg);
3707 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3708 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3709 I915_WRITE(reg, temp);
3710
3711 reg = FDI_RX_CTL(pipe);
3712 temp = I915_READ(reg);
3713 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3714 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003715 I915_WRITE(reg, temp);
3716
3717 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003718 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003719
Jesse Barnes139ccd32013-08-19 11:04:55 -07003720 for (i = 0; i < 4; i++) {
3721 reg = FDI_RX_IIR(pipe);
3722 temp = I915_READ(reg);
3723 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003724
Jesse Barnes139ccd32013-08-19 11:04:55 -07003725 if (temp & FDI_RX_SYMBOL_LOCK ||
3726 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3727 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3728 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3729 i);
3730 goto train_done;
3731 }
3732 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003733 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003734 if (i == 4)
3735 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003736 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003737
Jesse Barnes139ccd32013-08-19 11:04:55 -07003738train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003739 DRM_DEBUG_KMS("FDI train done.\n");
3740}
3741
Daniel Vetter88cefb62012-08-12 19:27:14 +02003742static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003743{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003744 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003745 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003746 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003747 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003748
Jesse Barnesc64e3112010-09-10 11:27:03 -07003749
Jesse Barnes0e23b992010-09-10 11:10:00 -07003750 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003751 reg = FDI_RX_CTL(pipe);
3752 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003753 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003754 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003755 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003756 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3757
3758 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003759 udelay(200);
3760
3761 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003762 temp = I915_READ(reg);
3763 I915_WRITE(reg, temp | FDI_PCDCLK);
3764
3765 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003766 udelay(200);
3767
Paulo Zanoni20749732012-11-23 15:30:38 -02003768 /* Enable CPU FDI TX PLL, always on for Ironlake */
3769 reg = FDI_TX_CTL(pipe);
3770 temp = I915_READ(reg);
3771 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3772 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003773
Paulo Zanoni20749732012-11-23 15:30:38 -02003774 POSTING_READ(reg);
3775 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003776 }
3777}
3778
Daniel Vetter88cefb62012-08-12 19:27:14 +02003779static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3780{
3781 struct drm_device *dev = intel_crtc->base.dev;
3782 struct drm_i915_private *dev_priv = dev->dev_private;
3783 int pipe = intel_crtc->pipe;
3784 u32 reg, temp;
3785
3786 /* Switch from PCDclk to Rawclk */
3787 reg = FDI_RX_CTL(pipe);
3788 temp = I915_READ(reg);
3789 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3790
3791 /* Disable CPU FDI TX PLL */
3792 reg = FDI_TX_CTL(pipe);
3793 temp = I915_READ(reg);
3794 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3795
3796 POSTING_READ(reg);
3797 udelay(100);
3798
3799 reg = FDI_RX_CTL(pipe);
3800 temp = I915_READ(reg);
3801 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3802
3803 /* Wait for the clocks to turn off. */
3804 POSTING_READ(reg);
3805 udelay(100);
3806}
3807
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003808static void ironlake_fdi_disable(struct drm_crtc *crtc)
3809{
3810 struct drm_device *dev = crtc->dev;
3811 struct drm_i915_private *dev_priv = dev->dev_private;
3812 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3813 int pipe = intel_crtc->pipe;
3814 u32 reg, temp;
3815
3816 /* disable CPU FDI tx and PCH FDI rx */
3817 reg = FDI_TX_CTL(pipe);
3818 temp = I915_READ(reg);
3819 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3820 POSTING_READ(reg);
3821
3822 reg = FDI_RX_CTL(pipe);
3823 temp = I915_READ(reg);
3824 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003825 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003826 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3827
3828 POSTING_READ(reg);
3829 udelay(100);
3830
3831 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003832 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003833 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003834
3835 /* still set train pattern 1 */
3836 reg = FDI_TX_CTL(pipe);
3837 temp = I915_READ(reg);
3838 temp &= ~FDI_LINK_TRAIN_NONE;
3839 temp |= FDI_LINK_TRAIN_PATTERN_1;
3840 I915_WRITE(reg, temp);
3841
3842 reg = FDI_RX_CTL(pipe);
3843 temp = I915_READ(reg);
3844 if (HAS_PCH_CPT(dev)) {
3845 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3846 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3847 } else {
3848 temp &= ~FDI_LINK_TRAIN_NONE;
3849 temp |= FDI_LINK_TRAIN_PATTERN_1;
3850 }
3851 /* BPC in FDI rx is consistent with that in PIPECONF */
3852 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003853 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003854 I915_WRITE(reg, temp);
3855
3856 POSTING_READ(reg);
3857 udelay(100);
3858}
3859
Chris Wilson5dce5b932014-01-20 10:17:36 +00003860bool intel_has_pending_fb_unpin(struct drm_device *dev)
3861{
3862 struct intel_crtc *crtc;
3863
3864 /* Note that we don't need to be called with mode_config.lock here
3865 * as our list of CRTC objects is static for the lifetime of the
3866 * device and so cannot disappear as we iterate. Similarly, we can
3867 * happily treat the predicates as racy, atomic checks as userspace
3868 * cannot claim and pin a new fb without at least acquring the
3869 * struct_mutex and so serialising with us.
3870 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003871 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003872 if (atomic_read(&crtc->unpin_work_count) == 0)
3873 continue;
3874
3875 if (crtc->unpin_work)
3876 intel_wait_for_vblank(dev, crtc->pipe);
3877
3878 return true;
3879 }
3880
3881 return false;
3882}
3883
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003884static void page_flip_completed(struct intel_crtc *intel_crtc)
3885{
3886 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3887 struct intel_unpin_work *work = intel_crtc->unpin_work;
3888
3889 /* ensure that the unpin work is consistent wrt ->pending. */
3890 smp_rmb();
3891 intel_crtc->unpin_work = NULL;
3892
3893 if (work->event)
3894 drm_send_vblank_event(intel_crtc->base.dev,
3895 intel_crtc->pipe,
3896 work->event);
3897
3898 drm_crtc_vblank_put(&intel_crtc->base);
3899
3900 wake_up_all(&dev_priv->pending_flip_queue);
3901 queue_work(dev_priv->wq, &work->work);
3902
3903 trace_i915_flip_complete(intel_crtc->plane,
3904 work->pending_flip_obj);
3905}
3906
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003907void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003908{
Chris Wilson0f911282012-04-17 10:05:38 +01003909 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003910 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003911
Daniel Vetter2c10d572012-12-20 21:24:07 +01003912 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003913 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3914 !intel_crtc_has_pending_flip(crtc),
3915 60*HZ) == 0)) {
3916 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003917
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003918 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003919 if (intel_crtc->unpin_work) {
3920 WARN_ONCE(1, "Removing stuck page flip\n");
3921 page_flip_completed(intel_crtc);
3922 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003923 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003924 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003925
Chris Wilson975d5682014-08-20 13:13:34 +01003926 if (crtc->primary->fb) {
3927 mutex_lock(&dev->struct_mutex);
3928 intel_finish_fb(crtc->primary->fb);
3929 mutex_unlock(&dev->struct_mutex);
3930 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003931}
3932
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003933/* Program iCLKIP clock to the desired frequency */
3934static void lpt_program_iclkip(struct drm_crtc *crtc)
3935{
3936 struct drm_device *dev = crtc->dev;
3937 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003938 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003939 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3940 u32 temp;
3941
Ville Syrjäläa5805162015-05-26 20:42:30 +03003942 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003943
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003944 /* It is necessary to ungate the pixclk gate prior to programming
3945 * the divisors, and gate it back when it is done.
3946 */
3947 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3948
3949 /* Disable SSCCTL */
3950 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003951 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3952 SBI_SSCCTL_DISABLE,
3953 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003954
3955 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003956 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003957 auxdiv = 1;
3958 divsel = 0x41;
3959 phaseinc = 0x20;
3960 } else {
3961 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003962 * but the adjusted_mode->crtc_clock in in KHz. To get the
3963 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003964 * convert the virtual clock precision to KHz here for higher
3965 * precision.
3966 */
3967 u32 iclk_virtual_root_freq = 172800 * 1000;
3968 u32 iclk_pi_range = 64;
3969 u32 desired_divisor, msb_divisor_value, pi_value;
3970
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003971 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003972 msb_divisor_value = desired_divisor / iclk_pi_range;
3973 pi_value = desired_divisor % iclk_pi_range;
3974
3975 auxdiv = 0;
3976 divsel = msb_divisor_value - 2;
3977 phaseinc = pi_value;
3978 }
3979
3980 /* This should not happen with any sane values */
3981 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3982 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3983 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3984 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3985
3986 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 +03003987 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003988 auxdiv,
3989 divsel,
3990 phasedir,
3991 phaseinc);
3992
3993 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003994 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003995 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3996 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3997 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3998 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3999 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
4000 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004001 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004002
4003 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004004 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004005 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
4006 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004007 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004008
4009 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004010 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004011 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004012 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004013
4014 /* Wait for initialization time */
4015 udelay(24);
4016
4017 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004018
Ville Syrjäläa5805162015-05-26 20:42:30 +03004019 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004020}
4021
Daniel Vetter275f01b22013-05-03 11:49:47 +02004022static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4023 enum pipe pch_transcoder)
4024{
4025 struct drm_device *dev = crtc->base.dev;
4026 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004027 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004028
4029 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4030 I915_READ(HTOTAL(cpu_transcoder)));
4031 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4032 I915_READ(HBLANK(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4034 I915_READ(HSYNC(cpu_transcoder)));
4035
4036 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4037 I915_READ(VTOTAL(cpu_transcoder)));
4038 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4039 I915_READ(VBLANK(cpu_transcoder)));
4040 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4041 I915_READ(VSYNC(cpu_transcoder)));
4042 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4043 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4044}
4045
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004046static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004047{
4048 struct drm_i915_private *dev_priv = dev->dev_private;
4049 uint32_t temp;
4050
4051 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004052 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004053 return;
4054
4055 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4056 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4057
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004058 temp &= ~FDI_BC_BIFURCATION_SELECT;
4059 if (enable)
4060 temp |= FDI_BC_BIFURCATION_SELECT;
4061
4062 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004063 I915_WRITE(SOUTH_CHICKEN1, temp);
4064 POSTING_READ(SOUTH_CHICKEN1);
4065}
4066
4067static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4068{
4069 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004070
4071 switch (intel_crtc->pipe) {
4072 case PIPE_A:
4073 break;
4074 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004075 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004076 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004077 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004078 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004079
4080 break;
4081 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004082 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004083
4084 break;
4085 default:
4086 BUG();
4087 }
4088}
4089
Jesse Barnesf67a5592011-01-05 10:31:48 -08004090/*
4091 * Enable PCH resources required for PCH ports:
4092 * - PCH PLLs
4093 * - FDI training & RX/TX
4094 * - update transcoder timings
4095 * - DP transcoding bits
4096 * - transcoder
4097 */
4098static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004099{
4100 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004101 struct drm_i915_private *dev_priv = dev->dev_private;
4102 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4103 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004104 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004105
Daniel Vetterab9412b2013-05-03 11:49:46 +02004106 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004107
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004108 if (IS_IVYBRIDGE(dev))
4109 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4110
Daniel Vettercd986ab2012-10-26 10:58:12 +02004111 /* Write the TU size bits before fdi link training, so that error
4112 * detection works. */
4113 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4114 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4115
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004116 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004117 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004118
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004119 /* We need to program the right clock selection before writing the pixel
4120 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004121 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004122 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004123
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004124 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004125 temp |= TRANS_DPLL_ENABLE(pipe);
4126 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004127 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004128 temp |= sel;
4129 else
4130 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004131 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004133
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004134 /* XXX: pch pll's can be enabled any time before we enable the PCH
4135 * transcoder, and we actually should do this to not upset any PCH
4136 * transcoder that already use the clock when we share it.
4137 *
4138 * Note that enable_shared_dpll tries to do the right thing, but
4139 * get_shared_dpll unconditionally resets the pll - we need that to have
4140 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004141 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004142
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004143 /* set transcoder timing, panel must allow it */
4144 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004145 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004146
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004147 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004148
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004149 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004150 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004151 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004152 reg = TRANS_DP_CTL(pipe);
4153 temp = I915_READ(reg);
4154 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004155 TRANS_DP_SYNC_MASK |
4156 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004157 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004158 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159
4160 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004163 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004164
4165 switch (intel_trans_dp_port_sel(crtc)) {
4166 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004167 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168 break;
4169 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004170 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004171 break;
4172 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004173 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004174 break;
4175 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004176 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004177 }
4178
Chris Wilson5eddb702010-09-11 13:48:45 +01004179 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004180 }
4181
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004182 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004183}
4184
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004185static void lpt_pch_enable(struct drm_crtc *crtc)
4186{
4187 struct drm_device *dev = crtc->dev;
4188 struct drm_i915_private *dev_priv = dev->dev_private;
4189 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004190 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004191
Daniel Vetterab9412b2013-05-03 11:49:46 +02004192 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004193
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004194 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004195
Paulo Zanoni0540e482012-10-31 18:12:40 -02004196 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004197 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004198
Paulo Zanoni937bb612012-10-31 18:12:47 -02004199 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004200}
4201
Daniel Vetter716c2e52014-06-25 22:02:02 +03004202void intel_put_shared_dpll(struct intel_crtc *crtc)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004203{
Daniel Vettere2b78262013-06-07 23:10:03 +02004204 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004205
4206 if (pll == NULL)
4207 return;
4208
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004209 if (!(pll->config.crtc_mask & (1 << crtc->pipe))) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004210 WARN(1, "bad %s crtc mask\n", pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004211 return;
4212 }
4213
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02004214 pll->config.crtc_mask &= ~(1 << crtc->pipe);
4215 if (pll->config.crtc_mask == 0) {
Daniel Vetterf4a091c2013-06-10 17:28:22 +02004216 WARN_ON(pll->on);
4217 WARN_ON(pll->active);
4218 }
4219
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004220 crtc->config->shared_dpll = DPLL_ID_PRIVATE;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004221}
4222
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004223struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4224 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004225{
Daniel Vettere2b78262013-06-07 23:10:03 +02004226 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004227 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004228 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004229
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004230 if (HAS_PCH_IBX(dev_priv->dev)) {
4231 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004232 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004233 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004234
Daniel Vetter46edb022013-06-05 13:34:12 +02004235 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4236 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004237
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004238 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004239
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004240 goto found;
4241 }
4242
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304243 if (IS_BROXTON(dev_priv->dev)) {
4244 /* PLL is attached to port in bxt */
4245 struct intel_encoder *encoder;
4246 struct intel_digital_port *intel_dig_port;
4247
4248 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4249 if (WARN_ON(!encoder))
4250 return NULL;
4251
4252 intel_dig_port = enc_to_dig_port(&encoder->base);
4253 /* 1:1 mapping between ports and PLLs */
4254 i = (enum intel_dpll_id)intel_dig_port->port;
4255 pll = &dev_priv->shared_dplls[i];
4256 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4257 crtc->base.base.id, pll->name);
4258 WARN_ON(pll->new_config->crtc_mask);
4259
4260 goto found;
4261 }
4262
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004263 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4264 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004265
4266 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004267 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004268 continue;
4269
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004270 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004271 &pll->new_config->hw_state,
4272 sizeof(pll->new_config->hw_state)) == 0) {
4273 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004274 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004275 pll->new_config->crtc_mask,
4276 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004277 goto found;
4278 }
4279 }
4280
4281 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004282 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4283 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004284 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004285 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4286 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004287 goto found;
4288 }
4289 }
4290
4291 return NULL;
4292
4293found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004294 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004295 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004296
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004297 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004298 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4299 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004300
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004301 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004302
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004303 return pll;
4304}
4305
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004306/**
4307 * intel_shared_dpll_start_config - start a new PLL staged config
4308 * @dev_priv: DRM device
4309 * @clear_pipes: mask of pipes that will have their PLLs freed
4310 *
4311 * Starts a new PLL staged config, copying the current config but
4312 * releasing the references of pipes specified in clear_pipes.
4313 */
4314static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4315 unsigned clear_pipes)
4316{
4317 struct intel_shared_dpll *pll;
4318 enum intel_dpll_id i;
4319
4320 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4321 pll = &dev_priv->shared_dplls[i];
4322
4323 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4324 GFP_KERNEL);
4325 if (!pll->new_config)
4326 goto cleanup;
4327
4328 pll->new_config->crtc_mask &= ~clear_pipes;
4329 }
4330
4331 return 0;
4332
4333cleanup:
4334 while (--i >= 0) {
4335 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004336 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004337 pll->new_config = NULL;
4338 }
4339
4340 return -ENOMEM;
4341}
4342
4343static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4344{
4345 struct intel_shared_dpll *pll;
4346 enum intel_dpll_id i;
4347
4348 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4349 pll = &dev_priv->shared_dplls[i];
4350
4351 WARN_ON(pll->new_config == &pll->config);
4352
4353 pll->config = *pll->new_config;
4354 kfree(pll->new_config);
4355 pll->new_config = NULL;
4356 }
4357}
4358
4359static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4360{
4361 struct intel_shared_dpll *pll;
4362 enum intel_dpll_id i;
4363
4364 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4365 pll = &dev_priv->shared_dplls[i];
4366
4367 WARN_ON(pll->new_config == &pll->config);
4368
4369 kfree(pll->new_config);
4370 pll->new_config = NULL;
4371 }
4372}
4373
Daniel Vettera1520312013-05-03 11:49:50 +02004374static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004375{
4376 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004377 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004378 u32 temp;
4379
4380 temp = I915_READ(dslreg);
4381 udelay(500);
4382 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004383 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004384 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004385 }
4386}
4387
Chandra Kondurua1b22782015-04-07 15:28:45 -07004388/**
4389 * skl_update_scaler_users - Stages update to crtc's scaler state
4390 * @intel_crtc: crtc
4391 * @crtc_state: crtc_state
4392 * @plane: plane (NULL indicates crtc is requesting update)
4393 * @plane_state: plane's state
4394 * @force_detach: request unconditional detachment of scaler
4395 *
4396 * This function updates scaler state for requested plane or crtc.
4397 * To request scaler usage update for a plane, caller shall pass plane pointer.
4398 * To request scaler usage update for crtc, caller shall pass plane pointer
4399 * as NULL.
4400 *
4401 * Return
4402 * 0 - scaler_usage updated successfully
4403 * error - requested scaling cannot be supported or other error condition
4404 */
4405int
4406skl_update_scaler_users(
4407 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4408 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4409 int force_detach)
4410{
4411 int need_scaling;
4412 int idx;
4413 int src_w, src_h, dst_w, dst_h;
4414 int *scaler_id;
4415 struct drm_framebuffer *fb;
4416 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004417 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004418
4419 if (!intel_crtc || !crtc_state)
4420 return 0;
4421
4422 scaler_state = &crtc_state->scaler_state;
4423
4424 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4425 fb = intel_plane ? plane_state->base.fb : NULL;
4426
4427 if (intel_plane) {
4428 src_w = drm_rect_width(&plane_state->src) >> 16;
4429 src_h = drm_rect_height(&plane_state->src) >> 16;
4430 dst_w = drm_rect_width(&plane_state->dst);
4431 dst_h = drm_rect_height(&plane_state->dst);
4432 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004433 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004434 } else {
4435 struct drm_display_mode *adjusted_mode =
4436 &crtc_state->base.adjusted_mode;
4437 src_w = crtc_state->pipe_src_w;
4438 src_h = crtc_state->pipe_src_h;
4439 dst_w = adjusted_mode->hdisplay;
4440 dst_h = adjusted_mode->vdisplay;
4441 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004442 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004443 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004444
4445 need_scaling = intel_rotation_90_or_270(rotation) ?
4446 (src_h != dst_w || src_w != dst_h):
4447 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004448
4449 /*
4450 * if plane is being disabled or scaler is no more required or force detach
4451 * - free scaler binded to this plane/crtc
4452 * - in order to do this, update crtc->scaler_usage
4453 *
4454 * Here scaler state in crtc_state is set free so that
4455 * scaler can be assigned to other user. Actual register
4456 * update to free the scaler is done in plane/panel-fit programming.
4457 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4458 */
4459 if (force_detach || !need_scaling || (intel_plane &&
4460 (!fb || !plane_state->visible))) {
4461 if (*scaler_id >= 0) {
4462 scaler_state->scaler_users &= ~(1 << idx);
4463 scaler_state->scalers[*scaler_id].in_use = 0;
4464
4465 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4466 "crtc_state = %p scaler_users = 0x%x\n",
4467 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4468 intel_plane ? intel_plane->base.base.id :
4469 intel_crtc->base.base.id, crtc_state,
4470 scaler_state->scaler_users);
4471 *scaler_id = -1;
4472 }
4473 return 0;
4474 }
4475
4476 /* range checks */
4477 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4478 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4479
4480 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4481 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4482 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4483 "size is out of scaler range\n",
4484 intel_plane ? "PLANE" : "CRTC",
4485 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4486 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4487 return -EINVAL;
4488 }
4489
4490 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004491 if (WARN_ON(intel_plane &&
4492 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4493 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4494 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004495 return -EINVAL;
4496 }
4497
4498 /* Check src format */
4499 if (intel_plane) {
4500 switch (fb->pixel_format) {
4501 case DRM_FORMAT_RGB565:
4502 case DRM_FORMAT_XBGR8888:
4503 case DRM_FORMAT_XRGB8888:
4504 case DRM_FORMAT_ABGR8888:
4505 case DRM_FORMAT_ARGB8888:
4506 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004507 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004508 case DRM_FORMAT_YUYV:
4509 case DRM_FORMAT_YVYU:
4510 case DRM_FORMAT_UYVY:
4511 case DRM_FORMAT_VYUY:
4512 break;
4513 default:
4514 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4515 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4516 return -EINVAL;
4517 }
4518 }
4519
4520 /* mark this plane as a scaler user in crtc_state */
4521 scaler_state->scaler_users |= (1 << idx);
4522 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4523 "crtc_state = %p scaler_users = 0x%x\n",
4524 intel_plane ? "PLANE" : "CRTC",
4525 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4526 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4527 return 0;
4528}
4529
4530static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004531{
4532 struct drm_device *dev = crtc->base.dev;
4533 struct drm_i915_private *dev_priv = dev->dev_private;
4534 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004535 struct intel_crtc_scaler_state *scaler_state =
4536 &crtc->config->scaler_state;
4537
4538 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4539
4540 /* To update pfit, first update scaler state */
4541 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4542 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4543 skl_detach_scalers(crtc);
4544 if (!enable)
4545 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004546
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004547 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004548 int id;
4549
4550 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4551 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4552 return;
4553 }
4554
4555 id = scaler_state->scaler_id;
4556 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4557 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4558 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4559 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4560
4561 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004562 }
4563}
4564
Jesse Barnesb074cec2013-04-25 12:55:02 -07004565static void ironlake_pfit_enable(struct intel_crtc *crtc)
4566{
4567 struct drm_device *dev = crtc->base.dev;
4568 struct drm_i915_private *dev_priv = dev->dev_private;
4569 int pipe = crtc->pipe;
4570
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004571 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004572 /* Force use of hard-coded filter coefficients
4573 * as some pre-programmed values are broken,
4574 * e.g. x201.
4575 */
4576 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4577 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4578 PF_PIPE_SEL_IVB(pipe));
4579 else
4580 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004581 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4582 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004583 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004584}
4585
Matt Roper4a3b8762014-12-23 10:41:51 -08004586static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004587{
4588 struct drm_device *dev = crtc->dev;
4589 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004590 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004591 struct intel_plane *intel_plane;
4592
Matt Roperaf2b6532014-04-01 15:22:32 -07004593 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4594 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004595 if (intel_plane->pipe == pipe)
4596 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004597 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004598}
4599
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004600void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004601{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004602 struct drm_device *dev = crtc->base.dev;
4603 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004604
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004605 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004606 return;
4607
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004608 /* We can only enable IPS after we enable a plane and wait for a vblank */
4609 intel_wait_for_vblank(dev, crtc->pipe);
4610
Paulo Zanonid77e4532013-09-24 13:52:55 -03004611 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004612 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004613 mutex_lock(&dev_priv->rps.hw_lock);
4614 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4615 mutex_unlock(&dev_priv->rps.hw_lock);
4616 /* Quoting Art Runyan: "its not safe to expect any particular
4617 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004618 * mailbox." Moreover, the mailbox may return a bogus state,
4619 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004620 */
4621 } else {
4622 I915_WRITE(IPS_CTL, IPS_ENABLE);
4623 /* The bit only becomes 1 in the next vblank, so this wait here
4624 * is essentially intel_wait_for_vblank. If we don't have this
4625 * and don't wait for vblanks until the end of crtc_enable, then
4626 * the HW state readout code will complain that the expected
4627 * IPS_CTL value is not the one we read. */
4628 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4629 DRM_ERROR("Timed out waiting for IPS enable\n");
4630 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004631}
4632
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004633void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004634{
4635 struct drm_device *dev = crtc->base.dev;
4636 struct drm_i915_private *dev_priv = dev->dev_private;
4637
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004638 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004639 return;
4640
4641 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004642 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004643 mutex_lock(&dev_priv->rps.hw_lock);
4644 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4645 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004646 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4647 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4648 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004649 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004650 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004651 POSTING_READ(IPS_CTL);
4652 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004653
4654 /* We need to wait for a vblank before we can disable the plane. */
4655 intel_wait_for_vblank(dev, crtc->pipe);
4656}
4657
4658/** Loads the palette/gamma unit for the CRTC with the prepared values */
4659static void intel_crtc_load_lut(struct drm_crtc *crtc)
4660{
4661 struct drm_device *dev = crtc->dev;
4662 struct drm_i915_private *dev_priv = dev->dev_private;
4663 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4664 enum pipe pipe = intel_crtc->pipe;
4665 int palreg = PALETTE(pipe);
4666 int i;
4667 bool reenable_ips = false;
4668
4669 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004670 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004671 return;
4672
Imre Deak50360402015-01-16 00:55:16 -08004673 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004674 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004675 assert_dsi_pll_enabled(dev_priv);
4676 else
4677 assert_pll_enabled(dev_priv, pipe);
4678 }
4679
4680 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304681 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004682 palreg = LGC_PALETTE(pipe);
4683
4684 /* Workaround : Do not read or write the pipe palette/gamma data while
4685 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4686 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004687 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004688 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4689 GAMMA_MODE_MODE_SPLIT)) {
4690 hsw_disable_ips(intel_crtc);
4691 reenable_ips = true;
4692 }
4693
4694 for (i = 0; i < 256; i++) {
4695 I915_WRITE(palreg + 4 * i,
4696 (intel_crtc->lut_r[i] << 16) |
4697 (intel_crtc->lut_g[i] << 8) |
4698 intel_crtc->lut_b[i]);
4699 }
4700
4701 if (reenable_ips)
4702 hsw_enable_ips(intel_crtc);
4703}
4704
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004705static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004706{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004707 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004708 struct drm_device *dev = intel_crtc->base.dev;
4709 struct drm_i915_private *dev_priv = dev->dev_private;
4710
4711 mutex_lock(&dev->struct_mutex);
4712 dev_priv->mm.interruptible = false;
4713 (void) intel_overlay_switch_off(intel_crtc->overlay);
4714 dev_priv->mm.interruptible = true;
4715 mutex_unlock(&dev->struct_mutex);
4716 }
4717
4718 /* Let userspace switch the overlay on again. In most cases userspace
4719 * has to recompute where to put it anyway.
4720 */
4721}
4722
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004723/**
4724 * intel_post_enable_primary - Perform operations after enabling primary plane
4725 * @crtc: the CRTC whose primary plane was just enabled
4726 *
4727 * Performs potentially sleeping operations that must be done after the primary
4728 * plane is enabled, such as updating FBC and IPS. Note that this may be
4729 * called due to an explicit primary plane update, or due to an implicit
4730 * re-enable that is caused when a sprite plane is updated to no longer
4731 * completely hide the primary plane.
4732 */
4733static void
4734intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004735{
4736 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004737 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004738 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4739 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004740
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004741 /*
4742 * BDW signals flip done immediately if the plane
4743 * is disabled, even if the plane enable is already
4744 * armed to occur at the next vblank :(
4745 */
4746 if (IS_BROADWELL(dev))
4747 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004748
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004749 /*
4750 * FIXME IPS should be fine as long as one plane is
4751 * enabled, but in practice it seems to have problems
4752 * when going from primary only to sprite only and vice
4753 * versa.
4754 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004755 hsw_enable_ips(intel_crtc);
4756
4757 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004758 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004759 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004760
4761 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004762 * Gen2 reports pipe underruns whenever all planes are disabled.
4763 * So don't enable underrun reporting before at least some planes
4764 * are enabled.
4765 * FIXME: Need to fix the logic to work when we turn off all planes
4766 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004767 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004768 if (IS_GEN2(dev))
4769 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4770
4771 /* Underruns don't raise interrupts, so check manually. */
4772 if (HAS_GMCH_DISPLAY(dev))
4773 i9xx_check_fifo_underruns(dev_priv);
4774}
4775
4776/**
4777 * intel_pre_disable_primary - Perform operations before disabling primary plane
4778 * @crtc: the CRTC whose primary plane is to be disabled
4779 *
4780 * Performs potentially sleeping operations that must be done before the
4781 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4782 * be called due to an explicit primary plane update, or due to an implicit
4783 * disable that is caused when a sprite plane completely hides the primary
4784 * plane.
4785 */
4786static void
4787intel_pre_disable_primary(struct drm_crtc *crtc)
4788{
4789 struct drm_device *dev = crtc->dev;
4790 struct drm_i915_private *dev_priv = dev->dev_private;
4791 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4792 int pipe = intel_crtc->pipe;
4793
4794 /*
4795 * Gen2 reports pipe underruns whenever all planes are disabled.
4796 * So diasble underrun reporting before all the planes get disabled.
4797 * FIXME: Need to fix the logic to work when we turn off all planes
4798 * but leave the pipe running.
4799 */
4800 if (IS_GEN2(dev))
4801 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4802
4803 /*
4804 * Vblank time updates from the shadow to live plane control register
4805 * are blocked if the memory self-refresh mode is active at that
4806 * moment. So to make sure the plane gets truly disabled, disable
4807 * first the self-refresh mode. The self-refresh enable bit in turn
4808 * will be checked/applied by the HW only at the next frame start
4809 * event which is after the vblank start event, so we need to have a
4810 * wait-for-vblank between disabling the plane and the pipe.
4811 */
4812 if (HAS_GMCH_DISPLAY(dev))
4813 intel_set_memory_cxsr(dev_priv, false);
4814
4815 mutex_lock(&dev->struct_mutex);
4816 if (dev_priv->fbc.crtc == intel_crtc)
4817 intel_fbc_disable(dev);
4818 mutex_unlock(&dev->struct_mutex);
4819
4820 /*
4821 * FIXME IPS should be fine as long as one plane is
4822 * enabled, but in practice it seems to have problems
4823 * when going from primary only to sprite only and vice
4824 * versa.
4825 */
4826 hsw_disable_ips(intel_crtc);
4827}
4828
4829static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4830{
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004831 intel_enable_primary_hw_plane(crtc->primary, crtc);
4832 intel_enable_sprite_planes(crtc);
4833 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004834
4835 intel_post_enable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004836}
4837
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004838static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004839{
4840 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004841 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004842 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004843 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004844
4845 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004846
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004847 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004848
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004849 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004850 for_each_intel_plane(dev, intel_plane) {
4851 if (intel_plane->pipe == pipe) {
4852 struct drm_crtc *from = intel_plane->base.crtc;
4853
4854 intel_plane->disable_plane(&intel_plane->base,
4855 from ?: crtc, true);
4856 }
4857 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004858
Daniel Vetterf99d7062014-06-19 16:01:59 +02004859 /*
4860 * FIXME: Once we grow proper nuclear flip support out of this we need
4861 * to compute the mask of flip planes precisely. For the time being
4862 * consider this a flip to a NULL plane.
4863 */
4864 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004865}
4866
Jesse Barnesf67a5592011-01-05 10:31:48 -08004867static void ironlake_crtc_enable(struct drm_crtc *crtc)
4868{
4869 struct drm_device *dev = crtc->dev;
4870 struct drm_i915_private *dev_priv = dev->dev_private;
4871 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004872 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004873 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004874
Matt Roper83d65732015-02-25 13:12:16 -08004875 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004876
Jesse Barnesf67a5592011-01-05 10:31:48 -08004877 if (intel_crtc->active)
4878 return;
4879
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004880 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004881 intel_prepare_shared_dpll(intel_crtc);
4882
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004883 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304884 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004885
4886 intel_set_pipe_timings(intel_crtc);
4887
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004888 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004889 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004890 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004891 }
4892
4893 ironlake_set_pipeconf(crtc);
4894
Jesse Barnesf67a5592011-01-05 10:31:48 -08004895 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004896
Daniel Vettera72e4c92014-09-30 10:56:47 +02004897 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4898 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004899
Daniel Vetterf6736a12013-06-05 13:34:30 +02004900 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004901 if (encoder->pre_enable)
4902 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004903
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004904 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004905 /* Note: FDI PLL enabling _must_ be done before we enable the
4906 * cpu pipes, hence this is separate from all the other fdi/pch
4907 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004908 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004909 } else {
4910 assert_fdi_tx_disabled(dev_priv, pipe);
4911 assert_fdi_rx_disabled(dev_priv, pipe);
4912 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004913
Jesse Barnesb074cec2013-04-25 12:55:02 -07004914 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004915
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004916 /*
4917 * On ILK+ LUT must be loaded before the pipe is running but with
4918 * clocks enabled
4919 */
4920 intel_crtc_load_lut(crtc);
4921
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004922 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004923 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004924
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004925 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004926 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004927
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004928 assert_vblank_disabled(crtc);
4929 drm_crtc_vblank_on(crtc);
4930
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004931 for_each_encoder_on_crtc(dev, crtc, encoder)
4932 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004933
4934 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004935 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004936}
4937
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004938/* IPS only exists on ULT machines and is tied to pipe A. */
4939static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4940{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004941 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004942}
4943
Paulo Zanonie4916942013-09-20 16:21:19 -03004944/*
4945 * This implements the workaround described in the "notes" section of the mode
4946 * set sequence documentation. When going from no pipes or single pipe to
4947 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4948 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4949 */
4950static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4951{
4952 struct drm_device *dev = crtc->base.dev;
4953 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4954
4955 /* We want to get the other_active_crtc only if there's only 1 other
4956 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004957 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004958 if (!crtc_it->active || crtc_it == crtc)
4959 continue;
4960
4961 if (other_active_crtc)
4962 return;
4963
4964 other_active_crtc = crtc_it;
4965 }
4966 if (!other_active_crtc)
4967 return;
4968
4969 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4970 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4971}
4972
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004973static void haswell_crtc_enable(struct drm_crtc *crtc)
4974{
4975 struct drm_device *dev = crtc->dev;
4976 struct drm_i915_private *dev_priv = dev->dev_private;
4977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4978 struct intel_encoder *encoder;
4979 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004980
Matt Roper83d65732015-02-25 13:12:16 -08004981 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004982
4983 if (intel_crtc->active)
4984 return;
4985
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004986 if (intel_crtc_to_shared_dpll(intel_crtc))
4987 intel_enable_shared_dpll(intel_crtc);
4988
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004989 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304990 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004991
4992 intel_set_pipe_timings(intel_crtc);
4993
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004994 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4995 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4996 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004997 }
4998
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004999 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02005000 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005001 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02005002 }
5003
5004 haswell_set_pipeconf(crtc);
5005
5006 intel_set_pipe_csc(crtc);
5007
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005008 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03005009
Daniel Vettera72e4c92014-09-30 10:56:47 +02005010 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005011 for_each_encoder_on_crtc(dev, crtc, encoder)
5012 if (encoder->pre_enable)
5013 encoder->pre_enable(encoder);
5014
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005015 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02005016 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5017 true);
Imre Deak4fe94672014-06-25 22:01:49 +03005018 dev_priv->display.fdi_link_train(crtc);
5019 }
5020
Paulo Zanoni1f544382012-10-24 11:32:00 -02005021 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005022
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005023 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005024 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005025 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005026 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005027 else
5028 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005029
5030 /*
5031 * On ILK+ LUT must be loaded before the pipe is running but with
5032 * clocks enabled
5033 */
5034 intel_crtc_load_lut(crtc);
5035
Paulo Zanoni1f544382012-10-24 11:32:00 -02005036 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005037 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005038
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005039 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005040 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005041
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005042 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005043 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005044
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005045 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005046 intel_ddi_set_vc_payload_alloc(crtc, true);
5047
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005048 assert_vblank_disabled(crtc);
5049 drm_crtc_vblank_on(crtc);
5050
Jani Nikula8807e552013-08-30 19:40:32 +03005051 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005052 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005053 intel_opregion_notify_encoder(encoder, true);
5054 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005055
Paulo Zanonie4916942013-09-20 16:21:19 -03005056 /* If we change the relative order between pipe/planes enabling, we need
5057 * to change the workaround. */
5058 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005059}
5060
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005061static void ironlake_pfit_disable(struct intel_crtc *crtc)
5062{
5063 struct drm_device *dev = crtc->base.dev;
5064 struct drm_i915_private *dev_priv = dev->dev_private;
5065 int pipe = crtc->pipe;
5066
5067 /* To avoid upsetting the power well on haswell only disable the pfit if
5068 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005069 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005070 I915_WRITE(PF_CTL(pipe), 0);
5071 I915_WRITE(PF_WIN_POS(pipe), 0);
5072 I915_WRITE(PF_WIN_SZ(pipe), 0);
5073 }
5074}
5075
Jesse Barnes6be4a602010-09-10 10:26:01 -07005076static void ironlake_crtc_disable(struct drm_crtc *crtc)
5077{
5078 struct drm_device *dev = crtc->dev;
5079 struct drm_i915_private *dev_priv = dev->dev_private;
5080 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005081 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005082 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005083 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005084
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005085 if (!intel_crtc->active)
5086 return;
5087
Daniel Vetterea9d7582012-07-10 10:42:52 +02005088 for_each_encoder_on_crtc(dev, crtc, encoder)
5089 encoder->disable(encoder);
5090
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005091 drm_crtc_vblank_off(crtc);
5092 assert_vblank_disabled(crtc);
5093
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005094 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005095 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005096
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005097 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005098
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005099 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005100
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005101 if (intel_crtc->config->has_pch_encoder)
5102 ironlake_fdi_disable(crtc);
5103
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005104 for_each_encoder_on_crtc(dev, crtc, encoder)
5105 if (encoder->post_disable)
5106 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005107
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005108 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005109 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005110
Daniel Vetterd925c592013-06-05 13:34:04 +02005111 if (HAS_PCH_CPT(dev)) {
5112 /* disable TRANS_DP_CTL */
5113 reg = TRANS_DP_CTL(pipe);
5114 temp = I915_READ(reg);
5115 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5116 TRANS_DP_PORT_SEL_MASK);
5117 temp |= TRANS_DP_PORT_SEL_NONE;
5118 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005119
Daniel Vetterd925c592013-06-05 13:34:04 +02005120 /* disable DPLL_SEL */
5121 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005122 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005123 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005124 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005125
5126 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005127 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005128
5129 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005130 }
5131
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005132 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005133 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005134
5135 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005136 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005137 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005138}
5139
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005140static void haswell_crtc_disable(struct drm_crtc *crtc)
5141{
5142 struct drm_device *dev = crtc->dev;
5143 struct drm_i915_private *dev_priv = dev->dev_private;
5144 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5145 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005146 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005147
5148 if (!intel_crtc->active)
5149 return;
5150
Jani Nikula8807e552013-08-30 19:40:32 +03005151 for_each_encoder_on_crtc(dev, crtc, encoder) {
5152 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005153 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005154 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005155
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005156 drm_crtc_vblank_off(crtc);
5157 assert_vblank_disabled(crtc);
5158
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005159 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005160 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5161 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005162 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005163
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005164 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005165 intel_ddi_set_vc_payload_alloc(crtc, false);
5166
Paulo Zanoniad80a812012-10-24 16:06:19 -02005167 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005168
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005169 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005170 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005171 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005172 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005173 else
5174 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005175
Paulo Zanoni1f544382012-10-24 11:32:00 -02005176 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005177
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005178 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005179 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005180 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005181 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005182
Imre Deak97b040a2014-06-25 22:01:50 +03005183 for_each_encoder_on_crtc(dev, crtc, encoder)
5184 if (encoder->post_disable)
5185 encoder->post_disable(encoder);
5186
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005187 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005188 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005189
5190 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005191 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005192 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005193
5194 if (intel_crtc_to_shared_dpll(intel_crtc))
5195 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005196}
5197
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005198static void ironlake_crtc_off(struct drm_crtc *crtc)
5199{
5200 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005201 intel_put_shared_dpll(intel_crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01005202}
5203
Paulo Zanoni6441ab52012-10-05 12:05:58 -03005204
Jesse Barnes2dd24552013-04-25 12:55:01 -07005205static void i9xx_pfit_enable(struct intel_crtc *crtc)
5206{
5207 struct drm_device *dev = crtc->base.dev;
5208 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005209 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005210
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005211 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005212 return;
5213
Daniel Vetterc0b03412013-05-28 12:05:54 +02005214 /*
5215 * The panel fitter should only be adjusted whilst the pipe is disabled,
5216 * according to register description and PRM.
5217 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005218 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5219 assert_pipe_disabled(dev_priv, crtc->pipe);
5220
Jesse Barnesb074cec2013-04-25 12:55:02 -07005221 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5222 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005223
5224 /* Border color in case we don't scale up to the full screen. Black by
5225 * default, change to something else for debugging. */
5226 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005227}
5228
Dave Airlied05410f2014-06-05 13:22:59 +10005229static enum intel_display_power_domain port_to_power_domain(enum port port)
5230{
5231 switch (port) {
5232 case PORT_A:
5233 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5234 case PORT_B:
5235 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5236 case PORT_C:
5237 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5238 case PORT_D:
5239 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5240 default:
5241 WARN_ON_ONCE(1);
5242 return POWER_DOMAIN_PORT_OTHER;
5243 }
5244}
5245
Imre Deak77d22dc2014-03-05 16:20:52 +02005246#define for_each_power_domain(domain, mask) \
5247 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5248 if ((1 << (domain)) & (mask))
5249
Imre Deak319be8a2014-03-04 19:22:57 +02005250enum intel_display_power_domain
5251intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005252{
Imre Deak319be8a2014-03-04 19:22:57 +02005253 struct drm_device *dev = intel_encoder->base.dev;
5254 struct intel_digital_port *intel_dig_port;
5255
5256 switch (intel_encoder->type) {
5257 case INTEL_OUTPUT_UNKNOWN:
5258 /* Only DDI platforms should ever use this output type */
5259 WARN_ON_ONCE(!HAS_DDI(dev));
5260 case INTEL_OUTPUT_DISPLAYPORT:
5261 case INTEL_OUTPUT_HDMI:
5262 case INTEL_OUTPUT_EDP:
5263 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005264 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005265 case INTEL_OUTPUT_DP_MST:
5266 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5267 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005268 case INTEL_OUTPUT_ANALOG:
5269 return POWER_DOMAIN_PORT_CRT;
5270 case INTEL_OUTPUT_DSI:
5271 return POWER_DOMAIN_PORT_DSI;
5272 default:
5273 return POWER_DOMAIN_PORT_OTHER;
5274 }
5275}
5276
5277static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5278{
5279 struct drm_device *dev = crtc->dev;
5280 struct intel_encoder *intel_encoder;
5281 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5282 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005283 unsigned long mask;
5284 enum transcoder transcoder;
5285
5286 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5287
5288 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5289 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005290 if (intel_crtc->config->pch_pfit.enabled ||
5291 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005292 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5293
Imre Deak319be8a2014-03-04 19:22:57 +02005294 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5295 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5296
Imre Deak77d22dc2014-03-05 16:20:52 +02005297 return mask;
5298}
5299
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005300static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005301{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005302 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005303 struct drm_i915_private *dev_priv = dev->dev_private;
5304 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5305 struct intel_crtc *crtc;
5306
5307 /*
5308 * First get all needed power domains, then put all unneeded, to avoid
5309 * any unnecessary toggling of the power wells.
5310 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005311 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005312 enum intel_display_power_domain domain;
5313
Matt Roper83d65732015-02-25 13:12:16 -08005314 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005315 continue;
5316
Imre Deak319be8a2014-03-04 19:22:57 +02005317 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005318
5319 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5320 intel_display_power_get(dev_priv, domain);
5321 }
5322
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005323 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005324 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005325
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005326 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005327 enum intel_display_power_domain domain;
5328
5329 for_each_power_domain(domain, crtc->enabled_power_domains)
5330 intel_display_power_put(dev_priv, domain);
5331
5332 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5333 }
5334
5335 intel_display_set_init_power(dev_priv, false);
5336}
5337
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305338void broxton_set_cdclk(struct drm_device *dev, int frequency)
5339{
5340 struct drm_i915_private *dev_priv = dev->dev_private;
5341 uint32_t divider;
5342 uint32_t ratio;
5343 uint32_t current_freq;
5344 int ret;
5345
5346 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5347 switch (frequency) {
5348 case 144000:
5349 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5350 ratio = BXT_DE_PLL_RATIO(60);
5351 break;
5352 case 288000:
5353 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5354 ratio = BXT_DE_PLL_RATIO(60);
5355 break;
5356 case 384000:
5357 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5358 ratio = BXT_DE_PLL_RATIO(60);
5359 break;
5360 case 576000:
5361 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5362 ratio = BXT_DE_PLL_RATIO(60);
5363 break;
5364 case 624000:
5365 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5366 ratio = BXT_DE_PLL_RATIO(65);
5367 break;
5368 case 19200:
5369 /*
5370 * Bypass frequency with DE PLL disabled. Init ratio, divider
5371 * to suppress GCC warning.
5372 */
5373 ratio = 0;
5374 divider = 0;
5375 break;
5376 default:
5377 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5378
5379 return;
5380 }
5381
5382 mutex_lock(&dev_priv->rps.hw_lock);
5383 /* Inform power controller of upcoming frequency change */
5384 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5385 0x80000000);
5386 mutex_unlock(&dev_priv->rps.hw_lock);
5387
5388 if (ret) {
5389 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5390 ret, frequency);
5391 return;
5392 }
5393
5394 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5395 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5396 current_freq = current_freq * 500 + 1000;
5397
5398 /*
5399 * DE PLL has to be disabled when
5400 * - setting to 19.2MHz (bypass, PLL isn't used)
5401 * - before setting to 624MHz (PLL needs toggling)
5402 * - before setting to any frequency from 624MHz (PLL needs toggling)
5403 */
5404 if (frequency == 19200 || frequency == 624000 ||
5405 current_freq == 624000) {
5406 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5407 /* Timeout 200us */
5408 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5409 1))
5410 DRM_ERROR("timout waiting for DE PLL unlock\n");
5411 }
5412
5413 if (frequency != 19200) {
5414 uint32_t val;
5415
5416 val = I915_READ(BXT_DE_PLL_CTL);
5417 val &= ~BXT_DE_PLL_RATIO_MASK;
5418 val |= ratio;
5419 I915_WRITE(BXT_DE_PLL_CTL, val);
5420
5421 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5422 /* Timeout 200us */
5423 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5424 DRM_ERROR("timeout waiting for DE PLL lock\n");
5425
5426 val = I915_READ(CDCLK_CTL);
5427 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5428 val |= divider;
5429 /*
5430 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5431 * enable otherwise.
5432 */
5433 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5434 if (frequency >= 500000)
5435 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5436
5437 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5438 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5439 val |= (frequency - 1000) / 500;
5440 I915_WRITE(CDCLK_CTL, val);
5441 }
5442
5443 mutex_lock(&dev_priv->rps.hw_lock);
5444 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5445 DIV_ROUND_UP(frequency, 25000));
5446 mutex_unlock(&dev_priv->rps.hw_lock);
5447
5448 if (ret) {
5449 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5450 ret, frequency);
5451 return;
5452 }
5453
5454 dev_priv->cdclk_freq = frequency;
5455}
5456
5457void broxton_init_cdclk(struct drm_device *dev)
5458{
5459 struct drm_i915_private *dev_priv = dev->dev_private;
5460 uint32_t val;
5461
5462 /*
5463 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5464 * or else the reset will hang because there is no PCH to respond.
5465 * Move the handshake programming to initialization sequence.
5466 * Previously was left up to BIOS.
5467 */
5468 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5469 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5470 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5471
5472 /* Enable PG1 for cdclk */
5473 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5474
5475 /* check if cd clock is enabled */
5476 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5477 DRM_DEBUG_KMS("Display already initialized\n");
5478 return;
5479 }
5480
5481 /*
5482 * FIXME:
5483 * - The initial CDCLK needs to be read from VBT.
5484 * Need to make this change after VBT has changes for BXT.
5485 * - check if setting the max (or any) cdclk freq is really necessary
5486 * here, it belongs to modeset time
5487 */
5488 broxton_set_cdclk(dev, 624000);
5489
5490 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005491 POSTING_READ(DBUF_CTL);
5492
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305493 udelay(10);
5494
5495 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5496 DRM_ERROR("DBuf power enable timeout!\n");
5497}
5498
5499void broxton_uninit_cdclk(struct drm_device *dev)
5500{
5501 struct drm_i915_private *dev_priv = dev->dev_private;
5502
5503 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005504 POSTING_READ(DBUF_CTL);
5505
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305506 udelay(10);
5507
5508 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5509 DRM_ERROR("DBuf power disable timeout!\n");
5510
5511 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5512 broxton_set_cdclk(dev, 19200);
5513
5514 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5515}
5516
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005517static const struct skl_cdclk_entry {
5518 unsigned int freq;
5519 unsigned int vco;
5520} skl_cdclk_frequencies[] = {
5521 { .freq = 308570, .vco = 8640 },
5522 { .freq = 337500, .vco = 8100 },
5523 { .freq = 432000, .vco = 8640 },
5524 { .freq = 450000, .vco = 8100 },
5525 { .freq = 540000, .vco = 8100 },
5526 { .freq = 617140, .vco = 8640 },
5527 { .freq = 675000, .vco = 8100 },
5528};
5529
5530static unsigned int skl_cdclk_decimal(unsigned int freq)
5531{
5532 return (freq - 1000) / 500;
5533}
5534
5535static unsigned int skl_cdclk_get_vco(unsigned int freq)
5536{
5537 unsigned int i;
5538
5539 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5540 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5541
5542 if (e->freq == freq)
5543 return e->vco;
5544 }
5545
5546 return 8100;
5547}
5548
5549static void
5550skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5551{
5552 unsigned int min_freq;
5553 u32 val;
5554
5555 /* select the minimum CDCLK before enabling DPLL 0 */
5556 val = I915_READ(CDCLK_CTL);
5557 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5558 val |= CDCLK_FREQ_337_308;
5559
5560 if (required_vco == 8640)
5561 min_freq = 308570;
5562 else
5563 min_freq = 337500;
5564
5565 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5566
5567 I915_WRITE(CDCLK_CTL, val);
5568 POSTING_READ(CDCLK_CTL);
5569
5570 /*
5571 * We always enable DPLL0 with the lowest link rate possible, but still
5572 * taking into account the VCO required to operate the eDP panel at the
5573 * desired frequency. The usual DP link rates operate with a VCO of
5574 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5575 * The modeset code is responsible for the selection of the exact link
5576 * rate later on, with the constraint of choosing a frequency that
5577 * works with required_vco.
5578 */
5579 val = I915_READ(DPLL_CTRL1);
5580
5581 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5582 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5583 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5584 if (required_vco == 8640)
5585 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5586 SKL_DPLL0);
5587 else
5588 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5589 SKL_DPLL0);
5590
5591 I915_WRITE(DPLL_CTRL1, val);
5592 POSTING_READ(DPLL_CTRL1);
5593
5594 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5595
5596 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5597 DRM_ERROR("DPLL0 not locked\n");
5598}
5599
5600static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5601{
5602 int ret;
5603 u32 val;
5604
5605 /* inform PCU we want to change CDCLK */
5606 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5607 mutex_lock(&dev_priv->rps.hw_lock);
5608 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5609 mutex_unlock(&dev_priv->rps.hw_lock);
5610
5611 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5612}
5613
5614static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5615{
5616 unsigned int i;
5617
5618 for (i = 0; i < 15; i++) {
5619 if (skl_cdclk_pcu_ready(dev_priv))
5620 return true;
5621 udelay(10);
5622 }
5623
5624 return false;
5625}
5626
5627static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5628{
5629 u32 freq_select, pcu_ack;
5630
5631 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5632
5633 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5634 DRM_ERROR("failed to inform PCU about cdclk change\n");
5635 return;
5636 }
5637
5638 /* set CDCLK_CTL */
5639 switch(freq) {
5640 case 450000:
5641 case 432000:
5642 freq_select = CDCLK_FREQ_450_432;
5643 pcu_ack = 1;
5644 break;
5645 case 540000:
5646 freq_select = CDCLK_FREQ_540;
5647 pcu_ack = 2;
5648 break;
5649 case 308570:
5650 case 337500:
5651 default:
5652 freq_select = CDCLK_FREQ_337_308;
5653 pcu_ack = 0;
5654 break;
5655 case 617140:
5656 case 675000:
5657 freq_select = CDCLK_FREQ_675_617;
5658 pcu_ack = 3;
5659 break;
5660 }
5661
5662 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5663 POSTING_READ(CDCLK_CTL);
5664
5665 /* inform PCU of the change */
5666 mutex_lock(&dev_priv->rps.hw_lock);
5667 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5668 mutex_unlock(&dev_priv->rps.hw_lock);
5669}
5670
5671void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5672{
5673 /* disable DBUF power */
5674 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5675 POSTING_READ(DBUF_CTL);
5676
5677 udelay(10);
5678
5679 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5680 DRM_ERROR("DBuf power disable timeout\n");
5681
5682 /* disable DPLL0 */
5683 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5684 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5685 DRM_ERROR("Couldn't disable DPLL0\n");
5686
5687 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5688}
5689
5690void skl_init_cdclk(struct drm_i915_private *dev_priv)
5691{
5692 u32 val;
5693 unsigned int required_vco;
5694
5695 /* enable PCH reset handshake */
5696 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5697 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5698
5699 /* enable PG1 and Misc I/O */
5700 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5701
5702 /* DPLL0 already enabed !? */
5703 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5704 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5705 return;
5706 }
5707
5708 /* enable DPLL0 */
5709 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5710 skl_dpll0_enable(dev_priv, required_vco);
5711
5712 /* set CDCLK to the frequency the BIOS chose */
5713 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5714
5715 /* enable DBUF power */
5716 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5717 POSTING_READ(DBUF_CTL);
5718
5719 udelay(10);
5720
5721 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5722 DRM_ERROR("DBuf power enable timeout\n");
5723}
5724
Ville Syrjälädfcab172014-06-13 13:37:47 +03005725/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005726static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005727{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005728 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005729
Jesse Barnes586f49d2013-11-04 16:06:59 -08005730 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005731 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005732 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5733 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005734 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005735
Ville Syrjälädfcab172014-06-13 13:37:47 +03005736 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005737}
5738
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005739static void vlv_update_cdclk(struct drm_device *dev)
5740{
5741 struct drm_i915_private *dev_priv = dev->dev_private;
5742
Vandana Kannan164dfd22014-11-24 13:37:41 +05305743 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
Ville Syrjälä43dc52c2014-10-07 17:41:20 +03005744 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
Vandana Kannan164dfd22014-11-24 13:37:41 +05305745 dev_priv->cdclk_freq);
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005746
5747 /*
5748 * Program the gmbus_freq based on the cdclk frequency.
5749 * BSpec erroneously claims we should aim for 4MHz, but
5750 * in fact 1MHz is the correct frequency.
5751 */
Vandana Kannan164dfd22014-11-24 13:37:41 +05305752 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005753}
5754
Jesse Barnes30a970c2013-11-04 13:48:12 -08005755/* Adjust CDclk dividers to allow high res or save power if possible */
5756static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5757{
5758 struct drm_i915_private *dev_priv = dev->dev_private;
5759 u32 val, cmd;
5760
Vandana Kannan164dfd22014-11-24 13:37:41 +05305761 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5762 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005763
Ville Syrjälädfcab172014-06-13 13:37:47 +03005764 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005765 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005766 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005767 cmd = 1;
5768 else
5769 cmd = 0;
5770
5771 mutex_lock(&dev_priv->rps.hw_lock);
5772 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5773 val &= ~DSPFREQGUAR_MASK;
5774 val |= (cmd << DSPFREQGUAR_SHIFT);
5775 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5776 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5777 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5778 50)) {
5779 DRM_ERROR("timed out waiting for CDclk change\n");
5780 }
5781 mutex_unlock(&dev_priv->rps.hw_lock);
5782
Ville Syrjälä54433e92015-05-26 20:42:31 +03005783 mutex_lock(&dev_priv->sb_lock);
5784
Ville Syrjälädfcab172014-06-13 13:37:47 +03005785 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005786 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005787
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005788 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005789
Jesse Barnes30a970c2013-11-04 13:48:12 -08005790 /* adjust cdclk divider */
5791 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005792 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793 val |= divider;
5794 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005795
5796 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5797 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5798 50))
5799 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005800 }
5801
Jesse Barnes30a970c2013-11-04 13:48:12 -08005802 /* adjust self-refresh exit latency value */
5803 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5804 val &= ~0x7f;
5805
5806 /*
5807 * For high bandwidth configs, we set a higher latency in the bunit
5808 * so that the core display fetch happens in time to avoid underruns.
5809 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005810 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005811 val |= 4500 / 250; /* 4.5 usec */
5812 else
5813 val |= 3000 / 250; /* 3.0 usec */
5814 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005815
Ville Syrjäläa5805162015-05-26 20:42:30 +03005816 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005817
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005818 vlv_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005819}
5820
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005821static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5822{
5823 struct drm_i915_private *dev_priv = dev->dev_private;
5824 u32 val, cmd;
5825
Vandana Kannan164dfd22014-11-24 13:37:41 +05305826 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5827 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005828
5829 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005830 case 333333:
5831 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005832 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005833 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005834 break;
5835 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005836 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005837 return;
5838 }
5839
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005840 /*
5841 * Specs are full of misinformation, but testing on actual
5842 * hardware has shown that we just need to write the desired
5843 * CCK divider into the Punit register.
5844 */
5845 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5846
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005847 mutex_lock(&dev_priv->rps.hw_lock);
5848 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5849 val &= ~DSPFREQGUAR_MASK_CHV;
5850 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5851 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5852 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5853 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5854 50)) {
5855 DRM_ERROR("timed out waiting for CDclk change\n");
5856 }
5857 mutex_unlock(&dev_priv->rps.hw_lock);
5858
5859 vlv_update_cdclk(dev);
5860}
5861
Jesse Barnes30a970c2013-11-04 13:48:12 -08005862static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5863 int max_pixclk)
5864{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005865 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005866 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005867
Jesse Barnes30a970c2013-11-04 13:48:12 -08005868 /*
5869 * Really only a few cases to deal with, as only 4 CDclks are supported:
5870 * 200MHz
5871 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005872 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005873 * 400MHz (VLV only)
5874 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5875 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005876 *
5877 * We seem to get an unstable or solid color picture at 200MHz.
5878 * Not sure what's wrong. For now use 200MHz only when all pipes
5879 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005880 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005881 if (!IS_CHERRYVIEW(dev_priv) &&
5882 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005883 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005884 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005885 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005886 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005887 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005888 else
5889 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005890}
5891
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305892static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5893 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005894{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305895 /*
5896 * FIXME:
5897 * - remove the guardband, it's not needed on BXT
5898 * - set 19.2MHz bypass frequency if there are no active pipes
5899 */
5900 if (max_pixclk > 576000*9/10)
5901 return 624000;
5902 else if (max_pixclk > 384000*9/10)
5903 return 576000;
5904 else if (max_pixclk > 288000*9/10)
5905 return 384000;
5906 else if (max_pixclk > 144000*9/10)
5907 return 288000;
5908 else
5909 return 144000;
5910}
5911
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005912/* Compute the max pixel clock for new configuration. Uses atomic state if
5913 * that's non-NULL, look at current state otherwise. */
5914static int intel_mode_max_pixclk(struct drm_device *dev,
5915 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005916{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005917 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005918 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005919 int max_pixclk = 0;
5920
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005921 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005922 if (state)
5923 crtc_state =
5924 intel_atomic_get_crtc_state(state, intel_crtc);
5925 else
5926 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005927 if (IS_ERR(crtc_state))
5928 return PTR_ERR(crtc_state);
5929
5930 if (!crtc_state->base.enable)
5931 continue;
5932
5933 max_pixclk = max(max_pixclk,
5934 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935 }
5936
5937 return max_pixclk;
5938}
5939
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005940static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005941{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005942 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005943 struct drm_crtc *crtc;
5944 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005945 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005946 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005947
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005948 if (max_pixclk < 0)
5949 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005950
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305951 if (IS_VALLEYVIEW(dev_priv))
5952 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5953 else
5954 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5955
5956 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005957 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005958
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005959 /* add all active pipes to the state */
5960 for_each_crtc(state->dev, crtc) {
5961 if (!crtc->state->enable)
5962 continue;
5963
5964 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5965 if (IS_ERR(crtc_state))
5966 return PTR_ERR(crtc_state);
5967 }
5968
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005969 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005970 for_each_crtc_in_state(state, crtc, crtc_state, i)
5971 if (crtc_state->enable)
5972 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005973
5974 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005975}
5976
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005977static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5978{
5979 unsigned int credits, default_credits;
5980
5981 if (IS_CHERRYVIEW(dev_priv))
5982 default_credits = PFI_CREDIT(12);
5983 else
5984 default_credits = PFI_CREDIT(8);
5985
Vandana Kannan164dfd22014-11-24 13:37:41 +05305986 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005987 /* CHV suggested value is 31 or 63 */
5988 if (IS_CHERRYVIEW(dev_priv))
5989 credits = PFI_CREDIT_31;
5990 else
5991 credits = PFI_CREDIT(15);
5992 } else {
5993 credits = default_credits;
5994 }
5995
5996 /*
5997 * WA - write default credits before re-programming
5998 * FIXME: should we also set the resend bit here?
5999 */
6000 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6001 default_credits);
6002
6003 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6004 credits | PFI_CREDIT_RESEND);
6005
6006 /*
6007 * FIXME is this guaranteed to clear
6008 * immediately or should we poll for it?
6009 */
6010 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6011}
6012
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006013static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006014{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006015 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006016 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006017 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006018 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006019
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006020 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
6021 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006022 if (WARN_ON(max_pixclk < 0))
6023 return;
6024
6025 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006026
Vandana Kannan164dfd22014-11-24 13:37:41 +05306027 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02006028 /*
6029 * FIXME: We can end up here with all power domains off, yet
6030 * with a CDCLK frequency other than the minimum. To account
6031 * for this take the PIPE-A power domain, which covers the HW
6032 * blocks needed for the following programming. This can be
6033 * removed once it's guaranteed that we get here either with
6034 * the minimum CDCLK set, or the required power domains
6035 * enabled.
6036 */
6037 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6038
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006039 if (IS_CHERRYVIEW(dev))
6040 cherryview_set_cdclk(dev, req_cdclk);
6041 else
6042 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006043
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006044 vlv_program_pfi_credits(dev_priv);
6045
Imre Deak738c05c2014-11-19 16:25:37 +02006046 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006047 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006048}
6049
Jesse Barnes89b667f2013-04-18 14:51:36 -07006050static void valleyview_crtc_enable(struct drm_crtc *crtc)
6051{
6052 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006053 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006054 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6055 struct intel_encoder *encoder;
6056 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006057 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006058
Matt Roper83d65732015-02-25 13:12:16 -08006059 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006060
6061 if (intel_crtc->active)
6062 return;
6063
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006064 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306065
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006066 if (!is_dsi) {
6067 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006068 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006069 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006070 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006071 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006072
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006073 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306074 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006075
6076 intel_set_pipe_timings(intel_crtc);
6077
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006078 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6079 struct drm_i915_private *dev_priv = dev->dev_private;
6080
6081 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6082 I915_WRITE(CHV_CANVAS(pipe), 0);
6083 }
6084
Daniel Vetter5b18e572014-04-24 23:55:06 +02006085 i9xx_set_pipeconf(intel_crtc);
6086
Jesse Barnes89b667f2013-04-18 14:51:36 -07006087 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006088
Daniel Vettera72e4c92014-09-30 10:56:47 +02006089 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006090
Jesse Barnes89b667f2013-04-18 14:51:36 -07006091 for_each_encoder_on_crtc(dev, crtc, encoder)
6092 if (encoder->pre_pll_enable)
6093 encoder->pre_pll_enable(encoder);
6094
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006095 if (!is_dsi) {
6096 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006097 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006098 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006099 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006100 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006101
6102 for_each_encoder_on_crtc(dev, crtc, encoder)
6103 if (encoder->pre_enable)
6104 encoder->pre_enable(encoder);
6105
Jesse Barnes2dd24552013-04-25 12:55:01 -07006106 i9xx_pfit_enable(intel_crtc);
6107
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006108 intel_crtc_load_lut(crtc);
6109
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006110 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006111 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006112
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006113 assert_vblank_disabled(crtc);
6114 drm_crtc_vblank_on(crtc);
6115
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006116 for_each_encoder_on_crtc(dev, crtc, encoder)
6117 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006118}
6119
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006120static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6121{
6122 struct drm_device *dev = crtc->base.dev;
6123 struct drm_i915_private *dev_priv = dev->dev_private;
6124
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006125 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6126 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006127}
6128
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006129static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006130{
6131 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006132 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006133 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006134 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006135 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006136
Matt Roper83d65732015-02-25 13:12:16 -08006137 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02006138
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006139 if (intel_crtc->active)
6140 return;
6141
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006142 i9xx_set_pll_dividers(intel_crtc);
6143
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006144 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306145 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006146
6147 intel_set_pipe_timings(intel_crtc);
6148
Daniel Vetter5b18e572014-04-24 23:55:06 +02006149 i9xx_set_pipeconf(intel_crtc);
6150
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006151 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006152
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006153 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006154 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006155
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006156 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006157 if (encoder->pre_enable)
6158 encoder->pre_enable(encoder);
6159
Daniel Vetterf6736a12013-06-05 13:34:30 +02006160 i9xx_enable_pll(intel_crtc);
6161
Jesse Barnes2dd24552013-04-25 12:55:01 -07006162 i9xx_pfit_enable(intel_crtc);
6163
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006164 intel_crtc_load_lut(crtc);
6165
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006166 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006167 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006168
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006169 assert_vblank_disabled(crtc);
6170 drm_crtc_vblank_on(crtc);
6171
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006172 for_each_encoder_on_crtc(dev, crtc, encoder)
6173 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006174}
6175
Daniel Vetter87476d62013-04-11 16:29:06 +02006176static void i9xx_pfit_disable(struct intel_crtc *crtc)
6177{
6178 struct drm_device *dev = crtc->base.dev;
6179 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006180
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006181 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006182 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006183
6184 assert_pipe_disabled(dev_priv, crtc->pipe);
6185
Daniel Vetter328d8e82013-05-08 10:36:31 +02006186 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6187 I915_READ(PFIT_CONTROL));
6188 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006189}
6190
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006191static void i9xx_crtc_disable(struct drm_crtc *crtc)
6192{
6193 struct drm_device *dev = crtc->dev;
6194 struct drm_i915_private *dev_priv = dev->dev_private;
6195 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006196 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006197 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006198
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006199 if (!intel_crtc->active)
6200 return;
6201
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006202 /*
6203 * On gen2 planes are double buffered but the pipe isn't, so we must
6204 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006205 * We also need to wait on all gmch platforms because of the
6206 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006207 */
Imre Deak564ed192014-06-13 14:54:21 +03006208 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006209
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006210 for_each_encoder_on_crtc(dev, crtc, encoder)
6211 encoder->disable(encoder);
6212
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006213 drm_crtc_vblank_off(crtc);
6214 assert_vblank_disabled(crtc);
6215
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006216 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006217
Daniel Vetter87476d62013-04-11 16:29:06 +02006218 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006219
Jesse Barnes89b667f2013-04-18 14:51:36 -07006220 for_each_encoder_on_crtc(dev, crtc, encoder)
6221 if (encoder->post_disable)
6222 encoder->post_disable(encoder);
6223
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006224 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006225 if (IS_CHERRYVIEW(dev))
6226 chv_disable_pll(dev_priv, pipe);
6227 else if (IS_VALLEYVIEW(dev))
6228 vlv_disable_pll(dev_priv, pipe);
6229 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006230 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006231 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006232
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006233 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006234 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006235
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006236 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006237 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006238
Daniel Vetterefa96242014-04-24 23:55:02 +02006239 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006240 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006241 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006242}
6243
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006244static void i9xx_crtc_off(struct drm_crtc *crtc)
6245{
6246}
6247
Borun Fub04c5bd2014-07-12 10:02:27 +05306248/* Master function to enable/disable CRTC and corresponding power wells */
6249void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006250{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006251 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006252 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006253 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006254 enum intel_display_power_domain domain;
6255 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006256
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006257 if (enable) {
6258 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006259 domains = get_crtc_power_domains(crtc);
6260 for_each_power_domain(domain, domains)
6261 intel_display_power_get(dev_priv, domain);
6262 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006263
6264 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006265 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006266 }
6267 } else {
6268 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006269 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006270 dev_priv->display.crtc_disable(crtc);
6271
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006272 domains = intel_crtc->enabled_power_domains;
6273 for_each_power_domain(domain, domains)
6274 intel_display_power_put(dev_priv, domain);
6275 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006276 }
6277 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306278}
6279
6280/**
6281 * Sets the power management mode of the pipe and plane.
6282 */
6283void intel_crtc_update_dpms(struct drm_crtc *crtc)
6284{
6285 struct drm_device *dev = crtc->dev;
6286 struct intel_encoder *intel_encoder;
6287 bool enable = false;
6288
6289 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6290 enable |= intel_encoder->connectors_active;
6291
6292 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006293
6294 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006295}
6296
Daniel Vetter976f8a22012-07-08 22:34:21 +02006297static void intel_crtc_disable(struct drm_crtc *crtc)
6298{
6299 struct drm_device *dev = crtc->dev;
6300 struct drm_connector *connector;
6301 struct drm_i915_private *dev_priv = dev->dev_private;
6302
6303 /* crtc should still be enabled when we disable it. */
Matt Roper83d65732015-02-25 13:12:16 -08006304 WARN_ON(!crtc->state->enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006305
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006306 intel_crtc_disable_planes(crtc);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006307 dev_priv->display.crtc_disable(crtc);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006308 dev_priv->display.off(crtc);
6309
Matt Roper70a101f2015-04-08 18:56:53 -07006310 drm_plane_helper_disable(crtc->primary);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006311
6312 /* Update computed state. */
6313 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
6314 if (!connector->encoder || !connector->encoder->crtc)
6315 continue;
6316
6317 if (connector->encoder->crtc != crtc)
6318 continue;
6319
6320 connector->dpms = DRM_MODE_DPMS_OFF;
6321 to_intel_encoder(connector->encoder)->connectors_active = false;
Chris Wilsoncdd59982010-09-08 16:30:16 +01006322 }
6323}
6324
Chris Wilsonea5b2132010-08-04 13:50:23 +01006325void intel_encoder_destroy(struct drm_encoder *encoder)
6326{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006327 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006328
Chris Wilsonea5b2132010-08-04 13:50:23 +01006329 drm_encoder_cleanup(encoder);
6330 kfree(intel_encoder);
6331}
6332
Damien Lespiau92373292013-08-08 22:28:57 +01006333/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006334 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6335 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006336static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006337{
6338 if (mode == DRM_MODE_DPMS_ON) {
6339 encoder->connectors_active = true;
6340
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006341 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006342 } else {
6343 encoder->connectors_active = false;
6344
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006345 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006346 }
6347}
6348
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006349/* Cross check the actual hw state with our own modeset state tracking (and it's
6350 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006351static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006352{
6353 if (connector->get_hw_state(connector)) {
6354 struct intel_encoder *encoder = connector->encoder;
6355 struct drm_crtc *crtc;
6356 bool encoder_enabled;
6357 enum pipe pipe;
6358
6359 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6360 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006361 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006362
Dave Airlie0e32b392014-05-02 14:02:48 +10006363 /* there is no real hw state for MST connectors */
6364 if (connector->mst_port)
6365 return;
6366
Rob Clarke2c719b2014-12-15 13:56:32 -05006367 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006368 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006369 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006370 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006371
Dave Airlie36cd7442014-05-02 13:44:18 +10006372 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006373 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006374 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006375
Dave Airlie36cd7442014-05-02 13:44:18 +10006376 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006377 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6378 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006379 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006380
Dave Airlie36cd7442014-05-02 13:44:18 +10006381 crtc = encoder->base.crtc;
6382
Matt Roper83d65732015-02-25 13:12:16 -08006383 I915_STATE_WARN(!crtc->state->enable,
6384 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006385 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6386 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006387 "encoder active on the wrong pipe\n");
6388 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006389 }
6390}
6391
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006392int intel_connector_init(struct intel_connector *connector)
6393{
6394 struct drm_connector_state *connector_state;
6395
6396 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6397 if (!connector_state)
6398 return -ENOMEM;
6399
6400 connector->base.state = connector_state;
6401 return 0;
6402}
6403
6404struct intel_connector *intel_connector_alloc(void)
6405{
6406 struct intel_connector *connector;
6407
6408 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6409 if (!connector)
6410 return NULL;
6411
6412 if (intel_connector_init(connector) < 0) {
6413 kfree(connector);
6414 return NULL;
6415 }
6416
6417 return connector;
6418}
6419
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006420/* Even simpler default implementation, if there's really no special case to
6421 * consider. */
6422void intel_connector_dpms(struct drm_connector *connector, int mode)
6423{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006424 /* All the simple cases only support two dpms states. */
6425 if (mode != DRM_MODE_DPMS_ON)
6426 mode = DRM_MODE_DPMS_OFF;
6427
6428 if (mode == connector->dpms)
6429 return;
6430
6431 connector->dpms = mode;
6432
6433 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006434 if (connector->encoder)
6435 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006436
Daniel Vetterb9805142012-08-31 17:37:33 +02006437 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006438}
6439
Daniel Vetterf0947c32012-07-02 13:10:34 +02006440/* Simple connector->get_hw_state implementation for encoders that support only
6441 * one connector and no cloning and hence the encoder state determines the state
6442 * of the connector. */
6443bool intel_connector_get_hw_state(struct intel_connector *connector)
6444{
Daniel Vetter24929352012-07-02 20:28:59 +02006445 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006446 struct intel_encoder *encoder = connector->encoder;
6447
6448 return encoder->get_hw_state(encoder, &pipe);
6449}
6450
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006451static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006452{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006453 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6454 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006455
6456 return 0;
6457}
6458
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006459static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006460 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006461{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006462 struct drm_atomic_state *state = pipe_config->base.state;
6463 struct intel_crtc *other_crtc;
6464 struct intel_crtc_state *other_crtc_state;
6465
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006466 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6467 pipe_name(pipe), pipe_config->fdi_lanes);
6468 if (pipe_config->fdi_lanes > 4) {
6469 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6470 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006471 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006472 }
6473
Paulo Zanonibafb6552013-11-02 21:07:44 -07006474 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006475 if (pipe_config->fdi_lanes > 2) {
6476 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6477 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006478 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006479 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006480 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006481 }
6482 }
6483
6484 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006486
6487 /* Ivybridge 3 pipe is really complicated */
6488 switch (pipe) {
6489 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006490 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006491 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006492 if (pipe_config->fdi_lanes <= 2)
6493 return 0;
6494
6495 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6496 other_crtc_state =
6497 intel_atomic_get_crtc_state(state, other_crtc);
6498 if (IS_ERR(other_crtc_state))
6499 return PTR_ERR(other_crtc_state);
6500
6501 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006502 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6503 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006504 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006505 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006506 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006507 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006508 if (pipe_config->fdi_lanes > 2) {
6509 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6510 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006511 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006512 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006513
6514 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6515 other_crtc_state =
6516 intel_atomic_get_crtc_state(state, other_crtc);
6517 if (IS_ERR(other_crtc_state))
6518 return PTR_ERR(other_crtc_state);
6519
6520 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006521 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006522 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006523 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006524 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006525 default:
6526 BUG();
6527 }
6528}
6529
Daniel Vettere29c22c2013-02-21 00:00:16 +01006530#define RETRY 1
6531static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006532 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006533{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006534 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006535 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006536 int lane, link_bw, fdi_dotclock, ret;
6537 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006538
Daniel Vettere29c22c2013-02-21 00:00:16 +01006539retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006540 /* FDI is a binary signal running at ~2.7GHz, encoding
6541 * each output octet as 10 bits. The actual frequency
6542 * is stored as a divider into a 100MHz clock, and the
6543 * mode pixel clock is stored in units of 1KHz.
6544 * Hence the bw of each lane in terms of the mode signal
6545 * is:
6546 */
6547 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6548
Damien Lespiau241bfc32013-09-25 16:45:37 +01006549 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006550
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006551 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006552 pipe_config->pipe_bpp);
6553
6554 pipe_config->fdi_lanes = lane;
6555
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006556 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006557 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006558
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006559 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6560 intel_crtc->pipe, pipe_config);
6561 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006562 pipe_config->pipe_bpp -= 2*3;
6563 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6564 pipe_config->pipe_bpp);
6565 needs_recompute = true;
6566 pipe_config->bw_constrained = true;
6567
6568 goto retry;
6569 }
6570
6571 if (needs_recompute)
6572 return RETRY;
6573
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006574 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006575}
6576
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006577static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006578 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006579{
Jani Nikulad330a952014-01-21 11:24:25 +02006580 pipe_config->ips_enabled = i915.enable_ips &&
Paulo Zanoni3c4ca582013-05-31 16:33:23 -03006581 hsw_crtc_supports_ips(crtc) &&
Jesse Barnesb6dfdc92013-07-25 10:06:50 -07006582 pipe_config->pipe_bpp <= 24;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006583}
6584
Daniel Vettera43f6e02013-06-07 23:10:32 +02006585static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006586 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006587{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006588 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006589 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006590 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006591 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006592
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006593 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006594 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006595 int clock_limit =
6596 dev_priv->display.get_display_clock_speed(dev);
6597
6598 /*
6599 * Enable pixel doubling when the dot clock
6600 * is > 90% of the (display) core speed.
6601 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006602 * GDG double wide on either pipe,
6603 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006604 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006605 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006606 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006607 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006608 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006609 }
6610
Damien Lespiau241bfc32013-09-25 16:45:37 +01006611 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006612 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006613 }
Chris Wilson89749352010-09-12 18:25:19 +01006614
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006615 /*
6616 * Pipe horizontal size must be even in:
6617 * - DVO ganged mode
6618 * - LVDS dual channel mode
6619 * - Double wide pipe
6620 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006621 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006622 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6623 pipe_config->pipe_src_w &= ~1;
6624
Damien Lespiau8693a822013-05-03 18:48:11 +01006625 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6626 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006627 */
6628 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6629 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006630 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006631
Damien Lespiauf5adf942013-06-24 18:29:34 +01006632 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006633 hsw_compute_ips_config(crtc, pipe_config);
6634
Daniel Vetter877d48d2013-04-19 11:24:43 +02006635 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006636 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006637
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006638 /* FIXME: remove below call once atomic mode set is place and all crtc
6639 * related checks called from atomic_crtc_check function */
6640 ret = 0;
6641 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6642 crtc, pipe_config->base.state);
6643 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6644
6645 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006646}
6647
Ville Syrjälä1652d192015-03-31 14:12:01 +03006648static int skylake_get_display_clock_speed(struct drm_device *dev)
6649{
6650 struct drm_i915_private *dev_priv = to_i915(dev);
6651 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6652 uint32_t cdctl = I915_READ(CDCLK_CTL);
6653 uint32_t linkrate;
6654
6655 if (!(lcpll1 & LCPLL_PLL_ENABLE)) {
6656 WARN(1, "LCPLL1 not enabled\n");
6657 return 24000; /* 24MHz is the cd freq with NSSC ref */
6658 }
6659
6660 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6661 return 540000;
6662
6663 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006664 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006665
Damien Lespiau71cd8422015-04-30 16:39:17 +01006666 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6667 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006668 /* vco 8640 */
6669 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6670 case CDCLK_FREQ_450_432:
6671 return 432000;
6672 case CDCLK_FREQ_337_308:
6673 return 308570;
6674 case CDCLK_FREQ_675_617:
6675 return 617140;
6676 default:
6677 WARN(1, "Unknown cd freq selection\n");
6678 }
6679 } else {
6680 /* vco 8100 */
6681 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6682 case CDCLK_FREQ_450_432:
6683 return 450000;
6684 case CDCLK_FREQ_337_308:
6685 return 337500;
6686 case CDCLK_FREQ_675_617:
6687 return 675000;
6688 default:
6689 WARN(1, "Unknown cd freq selection\n");
6690 }
6691 }
6692
6693 /* error case, do as if DPLL0 isn't enabled */
6694 return 24000;
6695}
6696
6697static int broadwell_get_display_clock_speed(struct drm_device *dev)
6698{
6699 struct drm_i915_private *dev_priv = dev->dev_private;
6700 uint32_t lcpll = I915_READ(LCPLL_CTL);
6701 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6702
6703 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6704 return 800000;
6705 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6706 return 450000;
6707 else if (freq == LCPLL_CLK_FREQ_450)
6708 return 450000;
6709 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6710 return 540000;
6711 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6712 return 337500;
6713 else
6714 return 675000;
6715}
6716
6717static int haswell_get_display_clock_speed(struct drm_device *dev)
6718{
6719 struct drm_i915_private *dev_priv = dev->dev_private;
6720 uint32_t lcpll = I915_READ(LCPLL_CTL);
6721 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6722
6723 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6724 return 800000;
6725 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6726 return 450000;
6727 else if (freq == LCPLL_CLK_FREQ_450)
6728 return 450000;
6729 else if (IS_HSW_ULT(dev))
6730 return 337500;
6731 else
6732 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006733}
6734
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006735static int valleyview_get_display_clock_speed(struct drm_device *dev)
6736{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006737 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006738 u32 val;
6739 int divider;
6740
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006741 if (dev_priv->hpll_freq == 0)
6742 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6743
Ville Syrjäläa5805162015-05-26 20:42:30 +03006744 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006745 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006746 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006747
6748 divider = val & DISPLAY_FREQUENCY_VALUES;
6749
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006750 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6751 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6752 "cdclk change in progress\n");
6753
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006754 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006755}
6756
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006757static int ilk_get_display_clock_speed(struct drm_device *dev)
6758{
6759 return 450000;
6760}
6761
Jesse Barnese70236a2009-09-21 10:42:27 -07006762static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006763{
Jesse Barnese70236a2009-09-21 10:42:27 -07006764 return 400000;
6765}
Jesse Barnes79e53942008-11-07 14:24:08 -08006766
Jesse Barnese70236a2009-09-21 10:42:27 -07006767static int i915_get_display_clock_speed(struct drm_device *dev)
6768{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006769 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006770}
Jesse Barnes79e53942008-11-07 14:24:08 -08006771
Jesse Barnese70236a2009-09-21 10:42:27 -07006772static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6773{
6774 return 200000;
6775}
Jesse Barnes79e53942008-11-07 14:24:08 -08006776
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006777static int pnv_get_display_clock_speed(struct drm_device *dev)
6778{
6779 u16 gcfgc = 0;
6780
6781 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6782
6783 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6784 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006785 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006786 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006787 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006788 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006789 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006790 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6791 return 200000;
6792 default:
6793 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6794 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006795 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006796 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006797 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006798 }
6799}
6800
Jesse Barnese70236a2009-09-21 10:42:27 -07006801static int i915gm_get_display_clock_speed(struct drm_device *dev)
6802{
6803 u16 gcfgc = 0;
6804
6805 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6806
6807 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006808 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006809 else {
6810 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6811 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006812 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006813 default:
6814 case GC_DISPLAY_CLOCK_190_200_MHZ:
6815 return 190000;
6816 }
6817 }
6818}
Jesse Barnes79e53942008-11-07 14:24:08 -08006819
Jesse Barnese70236a2009-09-21 10:42:27 -07006820static int i865_get_display_clock_speed(struct drm_device *dev)
6821{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006822 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006823}
6824
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006825static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006826{
6827 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006828
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006829 /*
6830 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6831 * encoding is different :(
6832 * FIXME is this the right way to detect 852GM/852GMV?
6833 */
6834 if (dev->pdev->revision == 0x1)
6835 return 133333;
6836
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006837 pci_bus_read_config_word(dev->pdev->bus,
6838 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6839
Jesse Barnese70236a2009-09-21 10:42:27 -07006840 /* Assume that the hardware is in the high speed state. This
6841 * should be the default.
6842 */
6843 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6844 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006845 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006846 case GC_CLOCK_100_200:
6847 return 200000;
6848 case GC_CLOCK_166_250:
6849 return 250000;
6850 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006851 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006852 case GC_CLOCK_133_266:
6853 case GC_CLOCK_133_266_2:
6854 case GC_CLOCK_166_266:
6855 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006856 }
6857
6858 /* Shouldn't happen */
6859 return 0;
6860}
6861
6862static int i830_get_display_clock_speed(struct drm_device *dev)
6863{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006864 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006865}
6866
Zhenyu Wang2c072452009-06-05 15:38:42 +08006867static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006868intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006869{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006870 while (*num > DATA_LINK_M_N_MASK ||
6871 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08006872 *num >>= 1;
6873 *den >>= 1;
6874 }
6875}
6876
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006877static void compute_m_n(unsigned int m, unsigned int n,
6878 uint32_t *ret_m, uint32_t *ret_n)
6879{
6880 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
6881 *ret_m = div_u64((uint64_t) m * *ret_n, n);
6882 intel_reduce_m_n_ratio(ret_m, ret_n);
6883}
6884
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006885void
6886intel_link_compute_m_n(int bits_per_pixel, int nlanes,
6887 int pixel_clock, int link_clock,
6888 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006889{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01006890 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03006891
6892 compute_m_n(bits_per_pixel * pixel_clock,
6893 link_clock * nlanes * 8,
6894 &m_n->gmch_m, &m_n->gmch_n);
6895
6896 compute_m_n(pixel_clock, link_clock,
6897 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08006898}
6899
Chris Wilsona7615032011-01-12 17:04:08 +00006900static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
6901{
Jani Nikulad330a952014-01-21 11:24:25 +02006902 if (i915.panel_use_ssc >= 0)
6903 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03006904 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07006905 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00006906}
6907
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006908static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
6909 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006910{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006911 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006912 struct drm_i915_private *dev_priv = dev->dev_private;
6913 int refclk;
6914
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006915 WARN_ON(!crtc_state->base.state);
6916
Imre Deak5ab7b0b2015-03-06 03:29:25 +02006917 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02006918 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006919 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006920 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02006921 refclk = dev_priv->vbt.lvds_ssc_freq;
6922 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006923 } else if (!IS_GEN2(dev)) {
6924 refclk = 96000;
6925 } else {
6926 refclk = 48000;
6927 }
6928
6929 return refclk;
6930}
6931
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006932static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006933{
Daniel Vetter7df00d72013-05-21 21:54:55 +02006934 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006935}
Daniel Vetterf47709a2013-03-28 10:42:02 +01006936
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006937static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
6938{
6939 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08006940}
6941
Daniel Vetterf47709a2013-03-28 10:42:02 +01006942static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006943 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08006944 intel_clock_t *reduced_clock)
6945{
Daniel Vetterf47709a2013-03-28 10:42:02 +01006946 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006947 u32 fp, fp2 = 0;
6948
6949 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006950 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006951 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006952 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006953 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006954 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006955 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02006956 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08006957 }
6958
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006959 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006960
Daniel Vetterf47709a2013-03-28 10:42:02 +01006961 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006962 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07006963 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006964 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01006965 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006966 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02006967 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08006968 }
6969}
6970
Chon Ming Lee5e69f972013-09-05 20:41:49 +08006971static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
6972 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07006973{
6974 u32 reg_val;
6975
6976 /*
6977 * PLLB opamp always calibrates to max value of 0x3f, force enable it
6978 * and set it to a reasonable value instead.
6979 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006980 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006981 reg_val &= 0xffffff00;
6982 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006983 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006984
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006985 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006986 reg_val &= 0x8cffffff;
6987 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006988 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006989
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006990 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07006991 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006992 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006993
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006994 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006995 reg_val &= 0x00ffffff;
6996 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08006997 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006998}
6999
Daniel Vetterb5518422013-05-03 11:49:48 +02007000static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7001 struct intel_link_m_n *m_n)
7002{
7003 struct drm_device *dev = crtc->base.dev;
7004 struct drm_i915_private *dev_priv = dev->dev_private;
7005 int pipe = crtc->pipe;
7006
Daniel Vettere3b95f12013-05-03 11:49:49 +02007007 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7008 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7009 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7010 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007011}
7012
7013static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007014 struct intel_link_m_n *m_n,
7015 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007016{
7017 struct drm_device *dev = crtc->base.dev;
7018 struct drm_i915_private *dev_priv = dev->dev_private;
7019 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007020 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007021
7022 if (INTEL_INFO(dev)->gen >= 5) {
7023 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7024 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7025 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7026 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007027 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7028 * for gen < 8) and if DRRS is supported (to make sure the
7029 * registers are not unnecessarily accessed).
7030 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307031 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007032 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007033 I915_WRITE(PIPE_DATA_M2(transcoder),
7034 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7035 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7036 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7037 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7038 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007039 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007040 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7041 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7042 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7043 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007044 }
7045}
7046
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307047void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007048{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307049 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7050
7051 if (m_n == M1_N1) {
7052 dp_m_n = &crtc->config->dp_m_n;
7053 dp_m2_n2 = &crtc->config->dp_m2_n2;
7054 } else if (m_n == M2_N2) {
7055
7056 /*
7057 * M2_N2 registers are not supported. Hence m2_n2 divider value
7058 * needs to be programmed into M1_N1.
7059 */
7060 dp_m_n = &crtc->config->dp_m2_n2;
7061 } else {
7062 DRM_ERROR("Unsupported divider value\n");
7063 return;
7064 }
7065
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007066 if (crtc->config->has_pch_encoder)
7067 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007068 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307069 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007070}
7071
Ville Syrjäläd288f652014-10-28 13:20:22 +02007072static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007073 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007074{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007075 u32 dpll, dpll_md;
7076
7077 /*
7078 * Enable DPIO clock input. We should never disable the reference
7079 * clock for pipe B, since VGA hotplug / manual detection depends
7080 * on it.
7081 */
7082 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7083 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7084 /* We should never disable this, set it here for state tracking */
7085 if (crtc->pipe == PIPE_B)
7086 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7087 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007088 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007089
Ville Syrjäläd288f652014-10-28 13:20:22 +02007090 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007091 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007092 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007093}
7094
Ville Syrjäläd288f652014-10-28 13:20:22 +02007095static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007096 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007097{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007098 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007099 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007100 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007101 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007102 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007103 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007104
Ville Syrjäläa5805162015-05-26 20:42:30 +03007105 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007106
Ville Syrjäläd288f652014-10-28 13:20:22 +02007107 bestn = pipe_config->dpll.n;
7108 bestm1 = pipe_config->dpll.m1;
7109 bestm2 = pipe_config->dpll.m2;
7110 bestp1 = pipe_config->dpll.p1;
7111 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007112
Jesse Barnes89b667f2013-04-18 14:51:36 -07007113 /* See eDP HDMI DPIO driver vbios notes doc */
7114
7115 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007116 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007117 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007118
7119 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007120 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007121
7122 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007123 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007124 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007125 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007126
7127 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007128 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007129
7130 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007131 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7132 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7133 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007134 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007135
7136 /*
7137 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7138 * but we don't support that).
7139 * Note: don't use the DAC post divider as it seems unstable.
7140 */
7141 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007142 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007143
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007144 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007145 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007146
Jesse Barnes89b667f2013-04-18 14:51:36 -07007147 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007148 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007149 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7150 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007151 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007152 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007153 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007154 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007155 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007156
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007157 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007158 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007159 if (pipe == PIPE_A)
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 0x0df40000);
7162 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007163 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007164 0x0df70000);
7165 } else { /* HDMI or VGA */
7166 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007167 if (pipe == PIPE_A)
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 0x0df70000);
7170 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007171 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007172 0x0df40000);
7173 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007174
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007175 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007176 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007177 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7178 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007179 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007180 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007181
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007182 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007183 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007184}
7185
Ville Syrjäläd288f652014-10-28 13:20:22 +02007186static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007187 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007188{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007189 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007190 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7191 DPLL_VCO_ENABLE;
7192 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007193 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007194
Ville Syrjäläd288f652014-10-28 13:20:22 +02007195 pipe_config->dpll_hw_state.dpll_md =
7196 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007197}
7198
Ville Syrjäläd288f652014-10-28 13:20:22 +02007199static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007200 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007201{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007202 struct drm_device *dev = crtc->base.dev;
7203 struct drm_i915_private *dev_priv = dev->dev_private;
7204 int pipe = crtc->pipe;
7205 int dpll_reg = DPLL(crtc->pipe);
7206 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307207 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007208 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307209 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307210 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007211
Ville Syrjäläd288f652014-10-28 13:20:22 +02007212 bestn = pipe_config->dpll.n;
7213 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7214 bestm1 = pipe_config->dpll.m1;
7215 bestm2 = pipe_config->dpll.m2 >> 22;
7216 bestp1 = pipe_config->dpll.p1;
7217 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307218 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307219 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307220 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007221
7222 /*
7223 * Enable Refclk and SSC
7224 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007225 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007226 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007227
Ville Syrjäläa5805162015-05-26 20:42:30 +03007228 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007229
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007230 /* p1 and p2 divider */
7231 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7232 5 << DPIO_CHV_S1_DIV_SHIFT |
7233 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7234 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7235 1 << DPIO_CHV_K_DIV_SHIFT);
7236
7237 /* Feedback post-divider - m2 */
7238 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7239
7240 /* Feedback refclk divider - n and m1 */
7241 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7242 DPIO_CHV_M1_DIV_BY_2 |
7243 1 << DPIO_CHV_N_DIV_SHIFT);
7244
7245 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307246 if (bestm2_frac)
7247 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007248
7249 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307250 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7251 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7252 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7253 if (bestm2_frac)
7254 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7255 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007256
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307257 /* Program digital lock detect threshold */
7258 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7259 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7260 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7261 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7262 if (!bestm2_frac)
7263 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7264 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7265
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007266 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307267 if (vco == 5400000) {
7268 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7269 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7270 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7271 tribuf_calcntr = 0x9;
7272 } else if (vco <= 6200000) {
7273 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7274 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7275 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7276 tribuf_calcntr = 0x9;
7277 } else if (vco <= 6480000) {
7278 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7279 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7280 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7281 tribuf_calcntr = 0x8;
7282 } else {
7283 /* Not supported. Apply the same limits as in the max case */
7284 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7285 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7286 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7287 tribuf_calcntr = 0;
7288 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007289 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7290
Ville Syrjälä968040b2015-03-11 22:52:08 +02007291 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307292 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7293 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7294 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7295
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007296 /* AFC Recal */
7297 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7298 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7299 DPIO_AFC_RECAL);
7300
Ville Syrjäläa5805162015-05-26 20:42:30 +03007301 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007302}
7303
Ville Syrjäläd288f652014-10-28 13:20:22 +02007304/**
7305 * vlv_force_pll_on - forcibly enable just the PLL
7306 * @dev_priv: i915 private structure
7307 * @pipe: pipe PLL to enable
7308 * @dpll: PLL configuration
7309 *
7310 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7311 * in cases where we need the PLL enabled even when @pipe is not going to
7312 * be enabled.
7313 */
7314void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7315 const struct dpll *dpll)
7316{
7317 struct intel_crtc *crtc =
7318 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007319 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007320 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007321 .pixel_multiplier = 1,
7322 .dpll = *dpll,
7323 };
7324
7325 if (IS_CHERRYVIEW(dev)) {
7326 chv_update_pll(crtc, &pipe_config);
7327 chv_prepare_pll(crtc, &pipe_config);
7328 chv_enable_pll(crtc, &pipe_config);
7329 } else {
7330 vlv_update_pll(crtc, &pipe_config);
7331 vlv_prepare_pll(crtc, &pipe_config);
7332 vlv_enable_pll(crtc, &pipe_config);
7333 }
7334}
7335
7336/**
7337 * vlv_force_pll_off - forcibly disable just the PLL
7338 * @dev_priv: i915 private structure
7339 * @pipe: pipe PLL to disable
7340 *
7341 * Disable the PLL for @pipe. To be used in cases where we need
7342 * the PLL enabled even when @pipe is not going to be enabled.
7343 */
7344void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7345{
7346 if (IS_CHERRYVIEW(dev))
7347 chv_disable_pll(to_i915(dev), pipe);
7348 else
7349 vlv_disable_pll(to_i915(dev), pipe);
7350}
7351
Daniel Vetterf47709a2013-03-28 10:42:02 +01007352static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007353 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007354 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007355 int num_connectors)
7356{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007357 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007358 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007359 u32 dpll;
7360 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007361 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007362
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007363 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307364
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007365 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7366 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007367
7368 dpll = DPLL_VGA_MODE_DIS;
7369
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007370 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007371 dpll |= DPLLB_MODE_LVDS;
7372 else
7373 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007374
Daniel Vetteref1b4602013-06-01 17:17:04 +02007375 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007376 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007377 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007378 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007379
7380 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007381 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007382
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007383 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007384 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007385
7386 /* compute bitmask from p1 value */
7387 if (IS_PINEVIEW(dev))
7388 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7389 else {
7390 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7391 if (IS_G4X(dev) && reduced_clock)
7392 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7393 }
7394 switch (clock->p2) {
7395 case 5:
7396 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7397 break;
7398 case 7:
7399 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7400 break;
7401 case 10:
7402 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7403 break;
7404 case 14:
7405 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7406 break;
7407 }
7408 if (INTEL_INFO(dev)->gen >= 4)
7409 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7410
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007411 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007412 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007413 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007414 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7415 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7416 else
7417 dpll |= PLL_REF_INPUT_DREFCLK;
7418
7419 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007420 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007421
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007422 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007423 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007424 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007425 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007426 }
7427}
7428
Daniel Vetterf47709a2013-03-28 10:42:02 +01007429static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007430 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007431 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007432 int num_connectors)
7433{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007434 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007435 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007436 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007437 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007438
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007439 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307440
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007441 dpll = DPLL_VGA_MODE_DIS;
7442
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007443 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007444 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7445 } else {
7446 if (clock->p1 == 2)
7447 dpll |= PLL_P1_DIVIDE_BY_TWO;
7448 else
7449 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7450 if (clock->p2 == 4)
7451 dpll |= PLL_P2_DIVIDE_BY_4;
7452 }
7453
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007454 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007455 dpll |= DPLL_DVO_2X_MODE;
7456
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007457 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007458 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7459 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7460 else
7461 dpll |= PLL_REF_INPUT_DREFCLK;
7462
7463 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007464 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007465}
7466
Daniel Vetter8a654f32013-06-01 17:16:22 +02007467static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007468{
7469 struct drm_device *dev = intel_crtc->base.dev;
7470 struct drm_i915_private *dev_priv = dev->dev_private;
7471 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007472 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007473 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007474 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007475 uint32_t crtc_vtotal, crtc_vblank_end;
7476 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007477
7478 /* We need to be careful not to changed the adjusted mode, for otherwise
7479 * the hw state checker will get angry at the mismatch. */
7480 crtc_vtotal = adjusted_mode->crtc_vtotal;
7481 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007482
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007483 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007484 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007485 crtc_vtotal -= 1;
7486 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007487
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007488 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007489 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7490 else
7491 vsyncshift = adjusted_mode->crtc_hsync_start -
7492 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007493 if (vsyncshift < 0)
7494 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007495 }
7496
7497 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007498 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007499
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007500 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007501 (adjusted_mode->crtc_hdisplay - 1) |
7502 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007503 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007504 (adjusted_mode->crtc_hblank_start - 1) |
7505 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007506 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007507 (adjusted_mode->crtc_hsync_start - 1) |
7508 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7509
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007510 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007511 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007512 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007513 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007514 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007515 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007516 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007517 (adjusted_mode->crtc_vsync_start - 1) |
7518 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7519
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007520 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7521 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7522 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7523 * bits. */
7524 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7525 (pipe == PIPE_B || pipe == PIPE_C))
7526 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7527
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007528 /* pipesrc controls the size that is scaled from, which should
7529 * always be the user's requested size.
7530 */
7531 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007532 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7533 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007534}
7535
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007536static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007537 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007538{
7539 struct drm_device *dev = crtc->base.dev;
7540 struct drm_i915_private *dev_priv = dev->dev_private;
7541 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7542 uint32_t tmp;
7543
7544 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007545 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7546 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007547 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007548 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7549 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007550 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007551 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7552 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007553
7554 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007555 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7556 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007557 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007558 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7559 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007560 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007561 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7562 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007563
7564 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007565 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7566 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7567 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007568 }
7569
7570 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007571 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7572 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7573
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007574 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7575 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007576}
7577
Daniel Vetterf6a83282014-02-11 15:28:57 -08007578void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007579 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007580{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007581 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7582 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7583 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7584 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007585
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007586 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7587 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7588 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7589 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007590
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007591 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007592
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007593 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7594 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007595}
7596
Daniel Vetter84b046f2013-02-19 18:48:54 +01007597static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7598{
7599 struct drm_device *dev = intel_crtc->base.dev;
7600 struct drm_i915_private *dev_priv = dev->dev_private;
7601 uint32_t pipeconf;
7602
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007603 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007604
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007605 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7606 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7607 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007608
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007609 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007610 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007611
Daniel Vetterff9ce462013-04-24 14:57:17 +02007612 /* only g4x and later have fancy bpc/dither controls */
7613 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007614 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007615 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007616 pipeconf |= PIPECONF_DITHER_EN |
7617 PIPECONF_DITHER_TYPE_SP;
7618
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007619 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007620 case 18:
7621 pipeconf |= PIPECONF_6BPC;
7622 break;
7623 case 24:
7624 pipeconf |= PIPECONF_8BPC;
7625 break;
7626 case 30:
7627 pipeconf |= PIPECONF_10BPC;
7628 break;
7629 default:
7630 /* Case prevented by intel_choose_pipe_bpp_dither. */
7631 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007632 }
7633 }
7634
7635 if (HAS_PIPE_CXSR(dev)) {
7636 if (intel_crtc->lowfreq_avail) {
7637 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7638 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7639 } else {
7640 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007641 }
7642 }
7643
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007644 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007645 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007646 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007647 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7648 else
7649 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7650 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007651 pipeconf |= PIPECONF_PROGRESSIVE;
7652
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007653 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007654 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007655
Daniel Vetter84b046f2013-02-19 18:48:54 +01007656 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7657 POSTING_READ(PIPECONF(intel_crtc->pipe));
7658}
7659
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007660static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7661 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007662{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007663 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007664 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007665 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007666 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007667 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007668 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007669 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007670 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007671 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007672 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007673 struct drm_connector_state *connector_state;
7674 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007675
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007676 memset(&crtc_state->dpll_hw_state, 0,
7677 sizeof(crtc_state->dpll_hw_state));
7678
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007679 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007680 if (connector_state->crtc != &crtc->base)
7681 continue;
7682
7683 encoder = to_intel_encoder(connector_state->best_encoder);
7684
Chris Wilson5eddb702010-09-11 13:48:45 +01007685 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007686 case INTEL_OUTPUT_LVDS:
7687 is_lvds = true;
7688 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007689 case INTEL_OUTPUT_DSI:
7690 is_dsi = true;
7691 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007692 default:
7693 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007694 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007695
Eric Anholtc751ce42010-03-25 11:48:48 -07007696 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007697 }
7698
Jani Nikulaf2335332013-09-13 11:03:09 +03007699 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007700 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007701
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007702 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007703 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007704
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007705 /*
7706 * Returns a set of divisors for the desired target clock with
7707 * the given refclk, or FALSE. The returned values represent
7708 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7709 * 2) / p1 / p2.
7710 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007711 limit = intel_limit(crtc_state, refclk);
7712 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007713 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007714 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007715 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007716 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7717 return -EINVAL;
7718 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007719
Jani Nikulaf2335332013-09-13 11:03:09 +03007720 if (is_lvds && dev_priv->lvds_downclock_avail) {
7721 /*
7722 * Ensure we match the reduced clock's P to the target
7723 * clock. If the clocks don't match, we can't switch
7724 * the display clock by using the FP0/FP1. In such case
7725 * we will disable the LVDS downclock feature.
7726 */
7727 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007728 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007729 dev_priv->lvds_downclock,
7730 refclk, &clock,
7731 &reduced_clock);
7732 }
7733 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007734 crtc_state->dpll.n = clock.n;
7735 crtc_state->dpll.m1 = clock.m1;
7736 crtc_state->dpll.m2 = clock.m2;
7737 crtc_state->dpll.p1 = clock.p1;
7738 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007739 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007740
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007741 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007742 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307743 has_reduced_clock ? &reduced_clock : NULL,
7744 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007745 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007746 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007747 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007748 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007749 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007750 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007751 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007752 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007753 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007754
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007755 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007756}
7757
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007758static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007759 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007760{
7761 struct drm_device *dev = crtc->base.dev;
7762 struct drm_i915_private *dev_priv = dev->dev_private;
7763 uint32_t tmp;
7764
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007765 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7766 return;
7767
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007768 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007769 if (!(tmp & PFIT_ENABLE))
7770 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007771
Daniel Vetter06922822013-07-11 13:35:40 +02007772 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007773 if (INTEL_INFO(dev)->gen < 4) {
7774 if (crtc->pipe != PIPE_B)
7775 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007776 } else {
7777 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7778 return;
7779 }
7780
Daniel Vetter06922822013-07-11 13:35:40 +02007781 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007782 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7783 if (INTEL_INFO(dev)->gen < 5)
7784 pipe_config->gmch_pfit.lvds_border_bits =
7785 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7786}
7787
Jesse Barnesacbec812013-09-20 11:29:32 -07007788static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007789 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007790{
7791 struct drm_device *dev = crtc->base.dev;
7792 struct drm_i915_private *dev_priv = dev->dev_private;
7793 int pipe = pipe_config->cpu_transcoder;
7794 intel_clock_t clock;
7795 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007796 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007797
Shobhit Kumarf573de52014-07-30 20:32:37 +05307798 /* In case of MIPI DPLL will not even be used */
7799 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7800 return;
7801
Ville Syrjäläa5805162015-05-26 20:42:30 +03007802 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007803 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007804 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007805
7806 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
7807 clock.m2 = mdiv & DPIO_M2DIV_MASK;
7808 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
7809 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
7810 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
7811
Ville Syrjäläf6466282013-10-14 14:50:31 +03007812 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07007813
Ville Syrjäläf6466282013-10-14 14:50:31 +03007814 /* clock.dot is the fast clock */
7815 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07007816}
7817
Damien Lespiau5724dbd2015-01-20 12:51:52 +00007818static void
7819i9xx_get_initial_plane_config(struct intel_crtc *crtc,
7820 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007821{
7822 struct drm_device *dev = crtc->base.dev;
7823 struct drm_i915_private *dev_priv = dev->dev_private;
7824 u32 val, base, offset;
7825 int pipe = crtc->pipe, plane = crtc->plane;
7826 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00007827 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007828 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00007829 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007830
Damien Lespiau42a7b082015-02-05 19:35:13 +00007831 val = I915_READ(DSPCNTR(plane));
7832 if (!(val & DISPLAY_PLANE_ENABLE))
7833 return;
7834
Damien Lespiaud9806c92015-01-21 14:07:19 +00007835 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00007836 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007837 DRM_DEBUG_KMS("failed to alloc fb\n");
7838 return;
7839 }
7840
Damien Lespiau1b842c82015-01-21 13:50:54 +00007841 fb = &intel_fb->base;
7842
Daniel Vetter18c52472015-02-10 17:16:09 +00007843 if (INTEL_INFO(dev)->gen >= 4) {
7844 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007845 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00007846 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
7847 }
7848 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007849
7850 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00007851 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007852 fb->pixel_format = fourcc;
7853 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007854
7855 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00007856 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007857 offset = I915_READ(DSPTILEOFF(plane));
7858 else
7859 offset = I915_READ(DSPLINOFF(plane));
7860 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
7861 } else {
7862 base = I915_READ(DSPADDR(plane));
7863 }
7864 plane_config->base = base;
7865
7866 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007867 fb->width = ((val >> 16) & 0xfff) + 1;
7868 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007869
7870 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007871 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007872
Damien Lespiaub113d5e2015-01-20 12:51:46 +00007873 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00007874 fb->pixel_format,
7875 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007876
Daniel Vetterf37b5c22015-02-10 23:12:27 +01007877 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007878
Damien Lespiau2844a922015-01-20 12:51:48 +00007879 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
7880 pipe_name(pipe), plane, fb->width, fb->height,
7881 fb->bits_per_pixel, base, fb->pitches[0],
7882 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007883
Damien Lespiau2d140302015-02-05 17:22:18 +00007884 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08007885}
7886
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007887static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007888 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007889{
7890 struct drm_device *dev = crtc->base.dev;
7891 struct drm_i915_private *dev_priv = dev->dev_private;
7892 int pipe = pipe_config->cpu_transcoder;
7893 enum dpio_channel port = vlv_pipe_to_channel(pipe);
7894 intel_clock_t clock;
7895 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
7896 int refclk = 100000;
7897
Ville Syrjäläa5805162015-05-26 20:42:30 +03007898 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007899 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
7900 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
7901 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
7902 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03007903 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007904
7905 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
7906 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
7907 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
7908 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
7909 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
7910
7911 chv_clock(refclk, &clock);
7912
7913 /* clock.dot is the fast clock */
7914 pipe_config->port_clock = clock.dot / 5;
7915}
7916
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007917static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007918 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007919{
7920 struct drm_device *dev = crtc->base.dev;
7921 struct drm_i915_private *dev_priv = dev->dev_private;
7922 uint32_t tmp;
7923
Daniel Vetterf458ebb2014-09-30 10:56:39 +02007924 if (!intel_display_power_is_enabled(dev_priv,
7925 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02007926 return false;
7927
Daniel Vettere143a212013-07-04 12:01:15 +02007928 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02007929 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02007930
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01007931 tmp = I915_READ(PIPECONF(crtc->pipe));
7932 if (!(tmp & PIPECONF_ENABLE))
7933 return false;
7934
Ville Syrjälä42571ae2013-09-06 23:29:00 +03007935 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
7936 switch (tmp & PIPECONF_BPC_MASK) {
7937 case PIPECONF_6BPC:
7938 pipe_config->pipe_bpp = 18;
7939 break;
7940 case PIPECONF_8BPC:
7941 pipe_config->pipe_bpp = 24;
7942 break;
7943 case PIPECONF_10BPC:
7944 pipe_config->pipe_bpp = 30;
7945 break;
7946 default:
7947 break;
7948 }
7949 }
7950
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02007951 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
7952 pipe_config->limited_color_range = true;
7953
Ville Syrjälä282740f2013-09-04 18:30:03 +03007954 if (INTEL_INFO(dev)->gen < 4)
7955 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
7956
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007957 intel_get_pipe_timings(crtc, pipe_config);
7958
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007959 i9xx_get_pfit_config(crtc, pipe_config);
7960
Daniel Vetter6c49f242013-06-06 12:45:25 +02007961 if (INTEL_INFO(dev)->gen >= 4) {
7962 tmp = I915_READ(DPLL_MD(crtc->pipe));
7963 pipe_config->pixel_multiplier =
7964 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
7965 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007966 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02007967 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
7968 tmp = I915_READ(DPLL(crtc->pipe));
7969 pipe_config->pixel_multiplier =
7970 ((tmp & SDVO_MULTIPLIER_MASK)
7971 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
7972 } else {
7973 /* Note that on i915G/GM the pixel multiplier is in the sdvo
7974 * port and will be fixed up in the encoder->get_config
7975 * function. */
7976 pipe_config->pixel_multiplier = 1;
7977 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007978 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
7979 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03007980 /*
7981 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
7982 * on 830. Filter it out here so that we don't
7983 * report errors due to that.
7984 */
7985 if (IS_I830(dev))
7986 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
7987
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007988 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
7989 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03007990 } else {
7991 /* Mask out read-only status bits. */
7992 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
7993 DPLL_PORTC_READY_MASK |
7994 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007995 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02007996
Ville Syrjälä70b23a92014-04-09 13:28:22 +03007997 if (IS_CHERRYVIEW(dev))
7998 chv_crtc_clock_get(crtc, pipe_config);
7999 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008000 vlv_crtc_clock_get(crtc, pipe_config);
8001 else
8002 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008003
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008004 return true;
8005}
8006
Paulo Zanonidde86e22012-12-01 12:04:25 -02008007static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008008{
8009 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008010 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008011 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008012 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008013 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008014 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008015 bool has_ck505 = false;
8016 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008017
8018 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008019 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008020 switch (encoder->type) {
8021 case INTEL_OUTPUT_LVDS:
8022 has_panel = true;
8023 has_lvds = true;
8024 break;
8025 case INTEL_OUTPUT_EDP:
8026 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008027 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008028 has_cpu_edp = true;
8029 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008030 default:
8031 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008032 }
8033 }
8034
Keith Packard99eb6a02011-09-26 14:29:12 -07008035 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008036 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008037 can_ssc = has_ck505;
8038 } else {
8039 has_ck505 = false;
8040 can_ssc = true;
8041 }
8042
Imre Deak2de69052013-05-08 13:14:04 +03008043 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8044 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008045
8046 /* Ironlake: try to setup display ref clock before DPLL
8047 * enabling. This is only under driver's control after
8048 * PCH B stepping, previous chipset stepping should be
8049 * ignoring this setting.
8050 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008051 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008052
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008053 /* As we must carefully and slowly disable/enable each source in turn,
8054 * compute the final state we want first and check if we need to
8055 * make any changes at all.
8056 */
8057 final = val;
8058 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008059 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008060 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008061 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008062 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8063
8064 final &= ~DREF_SSC_SOURCE_MASK;
8065 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8066 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008067
Keith Packard199e5d72011-09-22 12:01:57 -07008068 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008069 final |= DREF_SSC_SOURCE_ENABLE;
8070
8071 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8072 final |= DREF_SSC1_ENABLE;
8073
8074 if (has_cpu_edp) {
8075 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8076 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8077 else
8078 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8079 } else
8080 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8081 } else {
8082 final |= DREF_SSC_SOURCE_DISABLE;
8083 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8084 }
8085
8086 if (final == val)
8087 return;
8088
8089 /* Always enable nonspread source */
8090 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8091
8092 if (has_ck505)
8093 val |= DREF_NONSPREAD_CK505_ENABLE;
8094 else
8095 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8096
8097 if (has_panel) {
8098 val &= ~DREF_SSC_SOURCE_MASK;
8099 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008100
Keith Packard199e5d72011-09-22 12:01:57 -07008101 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008102 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008103 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008104 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008105 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008106 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008107
8108 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008109 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008110 POSTING_READ(PCH_DREF_CONTROL);
8111 udelay(200);
8112
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008113 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008114
8115 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008116 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008117 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008118 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008119 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008120 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008121 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008122 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008123 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008124
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008125 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008126 POSTING_READ(PCH_DREF_CONTROL);
8127 udelay(200);
8128 } else {
8129 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8130
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008131 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008132
8133 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008134 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008135
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008136 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008137 POSTING_READ(PCH_DREF_CONTROL);
8138 udelay(200);
8139
8140 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008141 val &= ~DREF_SSC_SOURCE_MASK;
8142 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008143
8144 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008145 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008146
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008147 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008148 POSTING_READ(PCH_DREF_CONTROL);
8149 udelay(200);
8150 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008151
8152 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008153}
8154
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008155static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008156{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008157 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008158
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008159 tmp = I915_READ(SOUTH_CHICKEN2);
8160 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8161 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008162
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008163 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8164 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8165 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008166
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008167 tmp = I915_READ(SOUTH_CHICKEN2);
8168 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8169 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008170
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008171 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8172 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8173 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008174}
8175
8176/* WaMPhyProgramming:hsw */
8177static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8178{
8179 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008180
8181 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8182 tmp &= ~(0xFF << 24);
8183 tmp |= (0x12 << 24);
8184 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8185
Paulo Zanonidde86e22012-12-01 12:04:25 -02008186 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8187 tmp |= (1 << 11);
8188 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8189
8190 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8191 tmp |= (1 << 11);
8192 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8193
Paulo Zanonidde86e22012-12-01 12:04:25 -02008194 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8195 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8196 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8197
8198 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8199 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8200 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8201
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008202 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8203 tmp &= ~(7 << 13);
8204 tmp |= (5 << 13);
8205 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008206
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008207 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8208 tmp &= ~(7 << 13);
8209 tmp |= (5 << 13);
8210 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008211
8212 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8213 tmp &= ~0xFF;
8214 tmp |= 0x1C;
8215 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8216
8217 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8218 tmp &= ~0xFF;
8219 tmp |= 0x1C;
8220 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8221
8222 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8223 tmp &= ~(0xFF << 16);
8224 tmp |= (0x1C << 16);
8225 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8226
8227 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8228 tmp &= ~(0xFF << 16);
8229 tmp |= (0x1C << 16);
8230 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8231
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008232 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8233 tmp |= (1 << 27);
8234 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008235
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008236 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8237 tmp |= (1 << 27);
8238 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008239
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008240 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8241 tmp &= ~(0xF << 28);
8242 tmp |= (4 << 28);
8243 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008244
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008245 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8246 tmp &= ~(0xF << 28);
8247 tmp |= (4 << 28);
8248 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008249}
8250
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008251/* Implements 3 different sequences from BSpec chapter "Display iCLK
8252 * Programming" based on the parameters passed:
8253 * - Sequence to enable CLKOUT_DP
8254 * - Sequence to enable CLKOUT_DP without spread
8255 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8256 */
8257static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8258 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008259{
8260 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008261 uint32_t reg, tmp;
8262
8263 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8264 with_spread = true;
8265 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8266 with_fdi, "LP PCH doesn't have FDI\n"))
8267 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008268
Ville Syrjäläa5805162015-05-26 20:42:30 +03008269 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008270
8271 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8272 tmp &= ~SBI_SSCCTL_DISABLE;
8273 tmp |= SBI_SSCCTL_PATHALT;
8274 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8275
8276 udelay(24);
8277
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008278 if (with_spread) {
8279 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8280 tmp &= ~SBI_SSCCTL_PATHALT;
8281 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008282
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008283 if (with_fdi) {
8284 lpt_reset_fdi_mphy(dev_priv);
8285 lpt_program_fdi_mphy(dev_priv);
8286 }
8287 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008288
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008289 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8290 SBI_GEN0 : SBI_DBUFF0;
8291 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8292 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8293 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008294
Ville Syrjäläa5805162015-05-26 20:42:30 +03008295 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008296}
8297
Paulo Zanoni47701c32013-07-23 11:19:25 -03008298/* Sequence to disable CLKOUT_DP */
8299static void lpt_disable_clkout_dp(struct drm_device *dev)
8300{
8301 struct drm_i915_private *dev_priv = dev->dev_private;
8302 uint32_t reg, tmp;
8303
Ville Syrjäläa5805162015-05-26 20:42:30 +03008304 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008305
8306 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8307 SBI_GEN0 : SBI_DBUFF0;
8308 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8309 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8310 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8311
8312 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8313 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8314 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8315 tmp |= SBI_SSCCTL_PATHALT;
8316 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8317 udelay(32);
8318 }
8319 tmp |= SBI_SSCCTL_DISABLE;
8320 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8321 }
8322
Ville Syrjäläa5805162015-05-26 20:42:30 +03008323 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008324}
8325
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008326static void lpt_init_pch_refclk(struct drm_device *dev)
8327{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008328 struct intel_encoder *encoder;
8329 bool has_vga = false;
8330
Damien Lespiaub2784e12014-08-05 11:29:37 +01008331 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008332 switch (encoder->type) {
8333 case INTEL_OUTPUT_ANALOG:
8334 has_vga = true;
8335 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008336 default:
8337 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008338 }
8339 }
8340
Paulo Zanoni47701c32013-07-23 11:19:25 -03008341 if (has_vga)
8342 lpt_enable_clkout_dp(dev, true, true);
8343 else
8344 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008345}
8346
Paulo Zanonidde86e22012-12-01 12:04:25 -02008347/*
8348 * Initialize reference clocks when the driver loads
8349 */
8350void intel_init_pch_refclk(struct drm_device *dev)
8351{
8352 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8353 ironlake_init_pch_refclk(dev);
8354 else if (HAS_PCH_LPT(dev))
8355 lpt_init_pch_refclk(dev);
8356}
8357
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008358static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008359{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008360 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008361 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008362 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008363 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008364 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008365 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008366 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008367 bool is_lvds = false;
8368
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008369 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008370 if (connector_state->crtc != crtc_state->base.crtc)
8371 continue;
8372
8373 encoder = to_intel_encoder(connector_state->best_encoder);
8374
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008375 switch (encoder->type) {
8376 case INTEL_OUTPUT_LVDS:
8377 is_lvds = true;
8378 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008379 default:
8380 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008381 }
8382 num_connectors++;
8383 }
8384
8385 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008386 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008387 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008388 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008389 }
8390
8391 return 120000;
8392}
8393
Daniel Vetter6ff93602013-04-19 11:24:36 +02008394static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008395{
8396 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8397 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8398 int pipe = intel_crtc->pipe;
8399 uint32_t val;
8400
Daniel Vetter78114072013-06-13 00:54:57 +02008401 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008402
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008403 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008404 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008405 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008406 break;
8407 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008408 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008409 break;
8410 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008411 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008412 break;
8413 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008414 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008415 break;
8416 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008417 /* Case prevented by intel_choose_pipe_bpp_dither. */
8418 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008419 }
8420
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008421 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008422 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8423
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008424 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008425 val |= PIPECONF_INTERLACED_ILK;
8426 else
8427 val |= PIPECONF_PROGRESSIVE;
8428
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008429 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008430 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008431
Paulo Zanonic8203562012-09-12 10:06:29 -03008432 I915_WRITE(PIPECONF(pipe), val);
8433 POSTING_READ(PIPECONF(pipe));
8434}
8435
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008436/*
8437 * Set up the pipe CSC unit.
8438 *
8439 * Currently only full range RGB to limited range RGB conversion
8440 * is supported, but eventually this should handle various
8441 * RGB<->YCbCr scenarios as well.
8442 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008443static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008444{
8445 struct drm_device *dev = crtc->dev;
8446 struct drm_i915_private *dev_priv = dev->dev_private;
8447 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8448 int pipe = intel_crtc->pipe;
8449 uint16_t coeff = 0x7800; /* 1.0 */
8450
8451 /*
8452 * TODO: Check what kind of values actually come out of the pipe
8453 * with these coeff/postoff values and adjust to get the best
8454 * accuracy. Perhaps we even need to take the bpc value into
8455 * consideration.
8456 */
8457
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008458 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008459 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8460
8461 /*
8462 * GY/GU and RY/RU should be the other way around according
8463 * to BSpec, but reality doesn't agree. Just set them up in
8464 * a way that results in the correct picture.
8465 */
8466 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8467 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8468
8469 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8470 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8471
8472 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8473 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8474
8475 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8476 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8477 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8478
8479 if (INTEL_INFO(dev)->gen > 6) {
8480 uint16_t postoff = 0;
8481
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008482 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008483 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008484
8485 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8486 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8487 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8488
8489 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8490 } else {
8491 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8492
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008493 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008494 mode |= CSC_BLACK_SCREEN_OFFSET;
8495
8496 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8497 }
8498}
8499
Daniel Vetter6ff93602013-04-19 11:24:36 +02008500static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008501{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008502 struct drm_device *dev = crtc->dev;
8503 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008504 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008505 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008506 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008507 uint32_t val;
8508
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008509 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008510
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008511 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008512 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8513
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008514 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008515 val |= PIPECONF_INTERLACED_ILK;
8516 else
8517 val |= PIPECONF_PROGRESSIVE;
8518
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008519 I915_WRITE(PIPECONF(cpu_transcoder), val);
8520 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008521
8522 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8523 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008524
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308525 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008526 val = 0;
8527
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008528 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008529 case 18:
8530 val |= PIPEMISC_DITHER_6_BPC;
8531 break;
8532 case 24:
8533 val |= PIPEMISC_DITHER_8_BPC;
8534 break;
8535 case 30:
8536 val |= PIPEMISC_DITHER_10_BPC;
8537 break;
8538 case 36:
8539 val |= PIPEMISC_DITHER_12_BPC;
8540 break;
8541 default:
8542 /* Case prevented by pipe_config_set_bpp. */
8543 BUG();
8544 }
8545
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008546 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008547 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8548
8549 I915_WRITE(PIPEMISC(pipe), val);
8550 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008551}
8552
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008553static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008554 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008555 intel_clock_t *clock,
8556 bool *has_reduced_clock,
8557 intel_clock_t *reduced_clock)
8558{
8559 struct drm_device *dev = crtc->dev;
8560 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008561 int refclk;
8562 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008563 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008564
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008565 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008566
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008567 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008568
8569 /*
8570 * Returns a set of divisors for the desired target clock with the given
8571 * refclk, or FALSE. The returned values represent the clock equation:
8572 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8573 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008574 limit = intel_limit(crtc_state, refclk);
8575 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008576 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008577 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008578 if (!ret)
8579 return false;
8580
8581 if (is_lvds && dev_priv->lvds_downclock_avail) {
8582 /*
8583 * Ensure we match the reduced clock's P to the target clock.
8584 * If the clocks don't match, we can't switch the display clock
8585 * by using the FP0/FP1. In such case we will disable the LVDS
8586 * downclock feature.
8587 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008588 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008589 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008590 dev_priv->lvds_downclock,
8591 refclk, clock,
8592 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008593 }
8594
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008595 return true;
8596}
8597
Paulo Zanonid4b19312012-11-29 11:29:32 -02008598int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8599{
8600 /*
8601 * Account for spread spectrum to avoid
8602 * oversubscribing the link. Max center spread
8603 * is 2.5%; use 5% for safety's sake.
8604 */
8605 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008606 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008607}
8608
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008609static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008610{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008611 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008612}
8613
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008614static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008615 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008616 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008617 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008618{
8619 struct drm_crtc *crtc = &intel_crtc->base;
8620 struct drm_device *dev = crtc->dev;
8621 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008622 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008623 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008624 struct drm_connector_state *connector_state;
8625 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008626 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008627 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008628 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008629
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008630 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008631 if (connector_state->crtc != crtc_state->base.crtc)
8632 continue;
8633
8634 encoder = to_intel_encoder(connector_state->best_encoder);
8635
8636 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008637 case INTEL_OUTPUT_LVDS:
8638 is_lvds = true;
8639 break;
8640 case INTEL_OUTPUT_SDVO:
8641 case INTEL_OUTPUT_HDMI:
8642 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008643 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008644 default:
8645 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008646 }
8647
8648 num_connectors++;
8649 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008650
Chris Wilsonc1858122010-12-03 21:35:48 +00008651 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008652 factor = 21;
8653 if (is_lvds) {
8654 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008655 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008656 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008657 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008658 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008659 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008660
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008661 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008662 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008663
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008664 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8665 *fp2 |= FP_CB_TUNE;
8666
Chris Wilson5eddb702010-09-11 13:48:45 +01008667 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008668
Eric Anholta07d6782011-03-30 13:01:08 -07008669 if (is_lvds)
8670 dpll |= DPLLB_MODE_LVDS;
8671 else
8672 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008673
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008674 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008675 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008676
8677 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008678 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008679 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008680 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008681
Eric Anholta07d6782011-03-30 13:01:08 -07008682 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008683 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008684 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008685 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008686
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008687 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008688 case 5:
8689 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8690 break;
8691 case 7:
8692 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8693 break;
8694 case 10:
8695 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8696 break;
8697 case 14:
8698 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8699 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008700 }
8701
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008702 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008703 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008704 else
8705 dpll |= PLL_REF_INPUT_DREFCLK;
8706
Daniel Vetter959e16d2013-06-05 13:34:21 +02008707 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008708}
8709
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008710static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8711 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008712{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008713 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008714 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008715 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008716 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008717 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008718 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008719
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008720 memset(&crtc_state->dpll_hw_state, 0,
8721 sizeof(crtc_state->dpll_hw_state));
8722
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008723 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008724
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008725 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8726 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8727
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008728 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008729 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008730 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008731 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8732 return -EINVAL;
8733 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008734 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008735 if (!crtc_state->clock_set) {
8736 crtc_state->dpll.n = clock.n;
8737 crtc_state->dpll.m1 = clock.m1;
8738 crtc_state->dpll.m2 = clock.m2;
8739 crtc_state->dpll.p1 = clock.p1;
8740 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008741 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008742
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008743 /* 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 +02008744 if (crtc_state->has_pch_encoder) {
8745 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008746 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008747 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008748
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008749 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008750 &fp, &reduced_clock,
8751 has_reduced_clock ? &fp2 : NULL);
8752
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008753 crtc_state->dpll_hw_state.dpll = dpll;
8754 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008755 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008756 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008757 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008758 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008759
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008760 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008761 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008762 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008763 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008764 return -EINVAL;
8765 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008766 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008767
Rodrigo Viviab585de2015-03-24 12:40:09 -07008768 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008769 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008770 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008771 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008772
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008773 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008774}
8775
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008776static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8777 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008778{
8779 struct drm_device *dev = crtc->base.dev;
8780 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008781 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008782
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008783 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8784 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8785 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8786 & ~TU_SIZE_MASK;
8787 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8788 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8789 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8790}
8791
8792static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8793 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008794 struct intel_link_m_n *m_n,
8795 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008796{
8797 struct drm_device *dev = crtc->base.dev;
8798 struct drm_i915_private *dev_priv = dev->dev_private;
8799 enum pipe pipe = crtc->pipe;
8800
8801 if (INTEL_INFO(dev)->gen >= 5) {
8802 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
8803 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
8804 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
8805 & ~TU_SIZE_MASK;
8806 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
8807 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
8808 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008809 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
8810 * gen < 8) and if DRRS is supported (to make sure the
8811 * registers are not unnecessarily read).
8812 */
8813 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008814 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008815 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
8816 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
8817 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
8818 & ~TU_SIZE_MASK;
8819 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
8820 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
8821 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8822 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008823 } else {
8824 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
8825 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
8826 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
8827 & ~TU_SIZE_MASK;
8828 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
8829 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
8830 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8831 }
8832}
8833
8834void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008835 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008836{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02008837 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008838 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
8839 else
8840 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008841 &pipe_config->dp_m_n,
8842 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008843}
8844
Daniel Vetter72419202013-04-04 13:28:53 +02008845static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008846 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02008847{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008848 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008849 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02008850}
8851
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008852static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008853 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008854{
8855 struct drm_device *dev = crtc->base.dev;
8856 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07008857 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
8858 uint32_t ps_ctrl = 0;
8859 int id = -1;
8860 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008861
Chandra Kondurua1b22782015-04-07 15:28:45 -07008862 /* find scaler attached to this pipe */
8863 for (i = 0; i < crtc->num_scalers; i++) {
8864 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
8865 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
8866 id = i;
8867 pipe_config->pch_pfit.enabled = true;
8868 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
8869 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
8870 break;
8871 }
8872 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008873
Chandra Kondurua1b22782015-04-07 15:28:45 -07008874 scaler_state->scaler_id = id;
8875 if (id >= 0) {
8876 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
8877 } else {
8878 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00008879 }
8880}
8881
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008882static void
8883skylake_get_initial_plane_config(struct intel_crtc *crtc,
8884 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008885{
8886 struct drm_device *dev = crtc->base.dev;
8887 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00008888 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008889 int pipe = crtc->pipe;
8890 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008891 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008892 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008893 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008894
Damien Lespiaud9806c92015-01-21 14:07:19 +00008895 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008896 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008897 DRM_DEBUG_KMS("failed to alloc fb\n");
8898 return;
8899 }
8900
Damien Lespiau1b842c82015-01-21 13:50:54 +00008901 fb = &intel_fb->base;
8902
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008903 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00008904 if (!(val & PLANE_CTL_ENABLE))
8905 goto error;
8906
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008907 pixel_format = val & PLANE_CTL_FORMAT_MASK;
8908 fourcc = skl_format_to_fourcc(pixel_format,
8909 val & PLANE_CTL_ORDER_RGBX,
8910 val & PLANE_CTL_ALPHA_MASK);
8911 fb->pixel_format = fourcc;
8912 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
8913
Damien Lespiau40f46282015-02-27 11:15:21 +00008914 tiling = val & PLANE_CTL_TILED_MASK;
8915 switch (tiling) {
8916 case PLANE_CTL_TILED_LINEAR:
8917 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
8918 break;
8919 case PLANE_CTL_TILED_X:
8920 plane_config->tiling = I915_TILING_X;
8921 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8922 break;
8923 case PLANE_CTL_TILED_Y:
8924 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
8925 break;
8926 case PLANE_CTL_TILED_YF:
8927 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
8928 break;
8929 default:
8930 MISSING_CASE(tiling);
8931 goto error;
8932 }
8933
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008934 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
8935 plane_config->base = base;
8936
8937 offset = I915_READ(PLANE_OFFSET(pipe, 0));
8938
8939 val = I915_READ(PLANE_SIZE(pipe, 0));
8940 fb->height = ((val >> 16) & 0xfff) + 1;
8941 fb->width = ((val >> 0) & 0x1fff) + 1;
8942
8943 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00008944 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
8945 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008946 fb->pitches[0] = (val & 0x3ff) * stride_mult;
8947
8948 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008949 fb->pixel_format,
8950 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008951
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008952 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008953
8954 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8955 pipe_name(pipe), fb->width, fb->height,
8956 fb->bits_per_pixel, base, fb->pitches[0],
8957 plane_config->size);
8958
Damien Lespiau2d140302015-02-05 17:22:18 +00008959 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00008960 return;
8961
8962error:
8963 kfree(fb);
8964}
8965
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008966static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008967 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008968{
8969 struct drm_device *dev = crtc->base.dev;
8970 struct drm_i915_private *dev_priv = dev->dev_private;
8971 uint32_t tmp;
8972
8973 tmp = I915_READ(PF_CTL(crtc->pipe));
8974
8975 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01008976 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008977 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
8978 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02008979
8980 /* We currently do not free assignements of panel fitters on
8981 * ivb/hsw (since we don't use the higher upscaling modes which
8982 * differentiates them) so just WARN about this case for now. */
8983 if (IS_GEN7(dev)) {
8984 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
8985 PF_PIPE_SEL_IVB(crtc->pipe));
8986 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008987 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008988}
8989
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008990static void
8991ironlake_get_initial_plane_config(struct intel_crtc *crtc,
8992 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008993{
8994 struct drm_device *dev = crtc->base.dev;
8995 struct drm_i915_private *dev_priv = dev->dev_private;
8996 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00008997 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08008998 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008999 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009000 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009001 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009002
Damien Lespiau42a7b082015-02-05 19:35:13 +00009003 val = I915_READ(DSPCNTR(pipe));
9004 if (!(val & DISPLAY_PLANE_ENABLE))
9005 return;
9006
Damien Lespiaud9806c92015-01-21 14:07:19 +00009007 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009008 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009009 DRM_DEBUG_KMS("failed to alloc fb\n");
9010 return;
9011 }
9012
Damien Lespiau1b842c82015-01-21 13:50:54 +00009013 fb = &intel_fb->base;
9014
Daniel Vetter18c52472015-02-10 17:16:09 +00009015 if (INTEL_INFO(dev)->gen >= 4) {
9016 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009017 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009018 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9019 }
9020 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009021
9022 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009023 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009024 fb->pixel_format = fourcc;
9025 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009026
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009027 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009028 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009029 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009030 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009031 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009032 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009033 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009034 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009035 }
9036 plane_config->base = base;
9037
9038 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009039 fb->width = ((val >> 16) & 0xfff) + 1;
9040 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009041
9042 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009043 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009044
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009045 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009046 fb->pixel_format,
9047 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009048
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009049 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009050
Damien Lespiau2844a922015-01-20 12:51:48 +00009051 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9052 pipe_name(pipe), fb->width, fb->height,
9053 fb->bits_per_pixel, base, fb->pitches[0],
9054 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009055
Damien Lespiau2d140302015-02-05 17:22:18 +00009056 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009057}
9058
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009059static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009060 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009061{
9062 struct drm_device *dev = crtc->base.dev;
9063 struct drm_i915_private *dev_priv = dev->dev_private;
9064 uint32_t tmp;
9065
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009066 if (!intel_display_power_is_enabled(dev_priv,
9067 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009068 return false;
9069
Daniel Vettere143a212013-07-04 12:01:15 +02009070 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009071 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009072
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009073 tmp = I915_READ(PIPECONF(crtc->pipe));
9074 if (!(tmp & PIPECONF_ENABLE))
9075 return false;
9076
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009077 switch (tmp & PIPECONF_BPC_MASK) {
9078 case PIPECONF_6BPC:
9079 pipe_config->pipe_bpp = 18;
9080 break;
9081 case PIPECONF_8BPC:
9082 pipe_config->pipe_bpp = 24;
9083 break;
9084 case PIPECONF_10BPC:
9085 pipe_config->pipe_bpp = 30;
9086 break;
9087 case PIPECONF_12BPC:
9088 pipe_config->pipe_bpp = 36;
9089 break;
9090 default:
9091 break;
9092 }
9093
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009094 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9095 pipe_config->limited_color_range = true;
9096
Daniel Vetterab9412b2013-05-03 11:49:46 +02009097 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009098 struct intel_shared_dpll *pll;
9099
Daniel Vetter88adfff2013-03-28 10:42:01 +01009100 pipe_config->has_pch_encoder = true;
9101
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009102 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9103 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9104 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009105
9106 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009107
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009108 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009109 pipe_config->shared_dpll =
9110 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009111 } else {
9112 tmp = I915_READ(PCH_DPLL_SEL);
9113 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9114 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9115 else
9116 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9117 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009118
9119 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9120
9121 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9122 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009123
9124 tmp = pipe_config->dpll_hw_state.dpll;
9125 pipe_config->pixel_multiplier =
9126 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9127 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009128
9129 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009130 } else {
9131 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009132 }
9133
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009134 intel_get_pipe_timings(crtc, pipe_config);
9135
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009136 ironlake_get_pfit_config(crtc, pipe_config);
9137
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009138 return true;
9139}
9140
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009141static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9142{
9143 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009144 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009145
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009146 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009147 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009148 pipe_name(crtc->pipe));
9149
Rob Clarke2c719b2014-12-15 13:56:32 -05009150 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9151 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9152 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9153 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9154 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9155 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009156 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009157 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009158 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009159 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009160 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009161 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009162 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009163 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009164 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009165
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009166 /*
9167 * In theory we can still leave IRQs enabled, as long as only the HPD
9168 * interrupts remain enabled. We used to check for that, but since it's
9169 * gen-specific and since we only disable LCPLL after we fully disable
9170 * the interrupts, the check below should be enough.
9171 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009172 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009173}
9174
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009175static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9176{
9177 struct drm_device *dev = dev_priv->dev;
9178
9179 if (IS_HASWELL(dev))
9180 return I915_READ(D_COMP_HSW);
9181 else
9182 return I915_READ(D_COMP_BDW);
9183}
9184
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009185static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9186{
9187 struct drm_device *dev = dev_priv->dev;
9188
9189 if (IS_HASWELL(dev)) {
9190 mutex_lock(&dev_priv->rps.hw_lock);
9191 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9192 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009193 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009194 mutex_unlock(&dev_priv->rps.hw_lock);
9195 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009196 I915_WRITE(D_COMP_BDW, val);
9197 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009198 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009199}
9200
9201/*
9202 * This function implements pieces of two sequences from BSpec:
9203 * - Sequence for display software to disable LCPLL
9204 * - Sequence for display software to allow package C8+
9205 * The steps implemented here are just the steps that actually touch the LCPLL
9206 * register. Callers should take care of disabling all the display engine
9207 * functions, doing the mode unset, fixing interrupts, etc.
9208 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009209static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9210 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009211{
9212 uint32_t val;
9213
9214 assert_can_disable_lcpll(dev_priv);
9215
9216 val = I915_READ(LCPLL_CTL);
9217
9218 if (switch_to_fclk) {
9219 val |= LCPLL_CD_SOURCE_FCLK;
9220 I915_WRITE(LCPLL_CTL, val);
9221
9222 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9223 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9224 DRM_ERROR("Switching to FCLK failed\n");
9225
9226 val = I915_READ(LCPLL_CTL);
9227 }
9228
9229 val |= LCPLL_PLL_DISABLE;
9230 I915_WRITE(LCPLL_CTL, val);
9231 POSTING_READ(LCPLL_CTL);
9232
9233 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9234 DRM_ERROR("LCPLL still locked\n");
9235
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009236 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009237 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009238 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009239 ndelay(100);
9240
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009241 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9242 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009243 DRM_ERROR("D_COMP RCOMP still in progress\n");
9244
9245 if (allow_power_down) {
9246 val = I915_READ(LCPLL_CTL);
9247 val |= LCPLL_POWER_DOWN_ALLOW;
9248 I915_WRITE(LCPLL_CTL, val);
9249 POSTING_READ(LCPLL_CTL);
9250 }
9251}
9252
9253/*
9254 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9255 * source.
9256 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009257static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009258{
9259 uint32_t val;
9260
9261 val = I915_READ(LCPLL_CTL);
9262
9263 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9264 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9265 return;
9266
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009267 /*
9268 * Make sure we're not on PC8 state before disabling PC8, otherwise
9269 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009270 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009271 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009272
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009273 if (val & LCPLL_POWER_DOWN_ALLOW) {
9274 val &= ~LCPLL_POWER_DOWN_ALLOW;
9275 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009276 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009277 }
9278
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009279 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009280 val |= D_COMP_COMP_FORCE;
9281 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009282 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009283
9284 val = I915_READ(LCPLL_CTL);
9285 val &= ~LCPLL_PLL_DISABLE;
9286 I915_WRITE(LCPLL_CTL, val);
9287
9288 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9289 DRM_ERROR("LCPLL not locked yet\n");
9290
9291 if (val & LCPLL_CD_SOURCE_FCLK) {
9292 val = I915_READ(LCPLL_CTL);
9293 val &= ~LCPLL_CD_SOURCE_FCLK;
9294 I915_WRITE(LCPLL_CTL, val);
9295
9296 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9297 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9298 DRM_ERROR("Switching back to LCPLL failed\n");
9299 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009300
Mika Kuoppala59bad942015-01-16 11:34:40 +02009301 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009302}
9303
Paulo Zanoni765dab672014-03-07 20:08:18 -03009304/*
9305 * Package states C8 and deeper are really deep PC states that can only be
9306 * reached when all the devices on the system allow it, so even if the graphics
9307 * device allows PC8+, it doesn't mean the system will actually get to these
9308 * states. Our driver only allows PC8+ when going into runtime PM.
9309 *
9310 * The requirements for PC8+ are that all the outputs are disabled, the power
9311 * well is disabled and most interrupts are disabled, and these are also
9312 * requirements for runtime PM. When these conditions are met, we manually do
9313 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9314 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9315 * hang the machine.
9316 *
9317 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9318 * the state of some registers, so when we come back from PC8+ we need to
9319 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9320 * need to take care of the registers kept by RC6. Notice that this happens even
9321 * if we don't put the device in PCI D3 state (which is what currently happens
9322 * because of the runtime PM support).
9323 *
9324 * For more, read "Display Sequences for Package C8" on the hardware
9325 * documentation.
9326 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009327void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009328{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009329 struct drm_device *dev = dev_priv->dev;
9330 uint32_t val;
9331
Paulo Zanonic67a4702013-08-19 13:18:09 -03009332 DRM_DEBUG_KMS("Enabling package C8+\n");
9333
Paulo Zanonic67a4702013-08-19 13:18:09 -03009334 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9335 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9336 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9337 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9338 }
9339
9340 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009341 hsw_disable_lcpll(dev_priv, true, true);
9342}
9343
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009344void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009345{
9346 struct drm_device *dev = dev_priv->dev;
9347 uint32_t val;
9348
Paulo Zanonic67a4702013-08-19 13:18:09 -03009349 DRM_DEBUG_KMS("Disabling package C8+\n");
9350
9351 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009352 lpt_init_pch_refclk(dev);
9353
9354 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9355 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9356 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9357 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9358 }
9359
9360 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009361}
9362
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009363static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309364{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009365 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309366 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009367 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309368 int req_cdclk;
9369
9370 /* see the comment in valleyview_modeset_global_resources */
9371 if (WARN_ON(max_pixclk < 0))
9372 return;
9373
9374 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9375
9376 if (req_cdclk != dev_priv->cdclk_freq)
9377 broxton_set_cdclk(dev, req_cdclk);
9378}
9379
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009380static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9381 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009382{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009383 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009384 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009385
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009386 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009387
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009388 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009389}
9390
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309391static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9392 enum port port,
9393 struct intel_crtc_state *pipe_config)
9394{
9395 switch (port) {
9396 case PORT_A:
9397 pipe_config->ddi_pll_sel = SKL_DPLL0;
9398 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9399 break;
9400 case PORT_B:
9401 pipe_config->ddi_pll_sel = SKL_DPLL1;
9402 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9403 break;
9404 case PORT_C:
9405 pipe_config->ddi_pll_sel = SKL_DPLL2;
9406 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9407 break;
9408 default:
9409 DRM_ERROR("Incorrect port type\n");
9410 }
9411}
9412
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009413static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9414 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009415 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009416{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009417 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009418
9419 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9420 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9421
9422 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009423 case SKL_DPLL0:
9424 /*
9425 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9426 * of the shared DPLL framework and thus needs to be read out
9427 * separately
9428 */
9429 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9430 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9431 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009432 case SKL_DPLL1:
9433 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9434 break;
9435 case SKL_DPLL2:
9436 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9437 break;
9438 case SKL_DPLL3:
9439 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9440 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009441 }
9442}
9443
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009444static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9445 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009446 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009447{
9448 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9449
9450 switch (pipe_config->ddi_pll_sel) {
9451 case PORT_CLK_SEL_WRPLL1:
9452 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9453 break;
9454 case PORT_CLK_SEL_WRPLL2:
9455 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9456 break;
9457 }
9458}
9459
Daniel Vetter26804af2014-06-25 22:01:55 +03009460static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009461 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009462{
9463 struct drm_device *dev = crtc->base.dev;
9464 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009465 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009466 enum port port;
9467 uint32_t tmp;
9468
9469 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9470
9471 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9472
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009473 if (IS_SKYLAKE(dev))
9474 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309475 else if (IS_BROXTON(dev))
9476 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009477 else
9478 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009479
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009480 if (pipe_config->shared_dpll >= 0) {
9481 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9482
9483 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9484 &pipe_config->dpll_hw_state));
9485 }
9486
Daniel Vetter26804af2014-06-25 22:01:55 +03009487 /*
9488 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9489 * DDI E. So just check whether this pipe is wired to DDI E and whether
9490 * the PCH transcoder is on.
9491 */
Damien Lespiauca370452013-12-03 13:56:24 +00009492 if (INTEL_INFO(dev)->gen < 9 &&
9493 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009494 pipe_config->has_pch_encoder = true;
9495
9496 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9497 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9498 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9499
9500 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9501 }
9502}
9503
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009504static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009505 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009506{
9507 struct drm_device *dev = crtc->base.dev;
9508 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009509 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009510 uint32_t tmp;
9511
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009512 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009513 POWER_DOMAIN_PIPE(crtc->pipe)))
9514 return false;
9515
Daniel Vettere143a212013-07-04 12:01:15 +02009516 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009517 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9518
Daniel Vettereccb1402013-05-22 00:50:22 +02009519 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9520 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9521 enum pipe trans_edp_pipe;
9522 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9523 default:
9524 WARN(1, "unknown pipe linked to edp transcoder\n");
9525 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9526 case TRANS_DDI_EDP_INPUT_A_ON:
9527 trans_edp_pipe = PIPE_A;
9528 break;
9529 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9530 trans_edp_pipe = PIPE_B;
9531 break;
9532 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9533 trans_edp_pipe = PIPE_C;
9534 break;
9535 }
9536
9537 if (trans_edp_pipe == crtc->pipe)
9538 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9539 }
9540
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009541 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009542 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009543 return false;
9544
Daniel Vettereccb1402013-05-22 00:50:22 +02009545 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009546 if (!(tmp & PIPECONF_ENABLE))
9547 return false;
9548
Daniel Vetter26804af2014-06-25 22:01:55 +03009549 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009550
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009551 intel_get_pipe_timings(crtc, pipe_config);
9552
Chandra Kondurua1b22782015-04-07 15:28:45 -07009553 if (INTEL_INFO(dev)->gen >= 9) {
9554 skl_init_scalers(dev, crtc, pipe_config);
9555 }
9556
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009557 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009558
9559 if (INTEL_INFO(dev)->gen >= 9) {
9560 pipe_config->scaler_state.scaler_id = -1;
9561 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9562 }
9563
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009564 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009565 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009566 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009567 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009568 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009569 else
9570 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009571 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009572
Jesse Barnese59150d2014-01-07 13:30:45 -08009573 if (IS_HASWELL(dev))
9574 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9575 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009576
Clint Taylorebb69c92014-09-30 10:30:22 -07009577 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9578 pipe_config->pixel_multiplier =
9579 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9580 } else {
9581 pipe_config->pixel_multiplier = 1;
9582 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009583
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009584 return true;
9585}
9586
Chris Wilson560b85b2010-08-07 11:01:38 +01009587static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9588{
9589 struct drm_device *dev = crtc->dev;
9590 struct drm_i915_private *dev_priv = dev->dev_private;
9591 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009592 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009593
Ville Syrjälädc41c152014-08-13 11:57:05 +03009594 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009595 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9596 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009597 unsigned int stride = roundup_pow_of_two(width) * 4;
9598
9599 switch (stride) {
9600 default:
9601 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9602 width, stride);
9603 stride = 256;
9604 /* fallthrough */
9605 case 256:
9606 case 512:
9607 case 1024:
9608 case 2048:
9609 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009610 }
9611
Ville Syrjälädc41c152014-08-13 11:57:05 +03009612 cntl |= CURSOR_ENABLE |
9613 CURSOR_GAMMA_ENABLE |
9614 CURSOR_FORMAT_ARGB |
9615 CURSOR_STRIDE(stride);
9616
9617 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009618 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009619
Ville Syrjälädc41c152014-08-13 11:57:05 +03009620 if (intel_crtc->cursor_cntl != 0 &&
9621 (intel_crtc->cursor_base != base ||
9622 intel_crtc->cursor_size != size ||
9623 intel_crtc->cursor_cntl != cntl)) {
9624 /* On these chipsets we can only modify the base/size/stride
9625 * whilst the cursor is disabled.
9626 */
9627 I915_WRITE(_CURACNTR, 0);
9628 POSTING_READ(_CURACNTR);
9629 intel_crtc->cursor_cntl = 0;
9630 }
9631
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009632 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +03009633 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009634 intel_crtc->cursor_base = base;
9635 }
Ville Syrjälädc41c152014-08-13 11:57:05 +03009636
9637 if (intel_crtc->cursor_size != size) {
9638 I915_WRITE(CURSIZE, size);
9639 intel_crtc->cursor_size = size;
9640 }
9641
Chris Wilson4b0e3332014-05-30 16:35:26 +03009642 if (intel_crtc->cursor_cntl != cntl) {
9643 I915_WRITE(_CURACNTR, cntl);
9644 POSTING_READ(_CURACNTR);
9645 intel_crtc->cursor_cntl = cntl;
9646 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009647}
9648
9649static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
9650{
9651 struct drm_device *dev = crtc->dev;
9652 struct drm_i915_private *dev_priv = dev->dev_private;
9653 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9654 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009655 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +01009656
Chris Wilson4b0e3332014-05-30 16:35:26 +03009657 cntl = 0;
9658 if (base) {
9659 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -08009660 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309661 case 64:
9662 cntl |= CURSOR_MODE_64_ARGB_AX;
9663 break;
9664 case 128:
9665 cntl |= CURSOR_MODE_128_ARGB_AX;
9666 break;
9667 case 256:
9668 cntl |= CURSOR_MODE_256_ARGB_AX;
9669 break;
9670 default:
Matt Roper3dd512f2015-02-27 10:12:00 -08009671 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +05309672 return;
Chris Wilson560b85b2010-08-07 11:01:38 +01009673 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009674 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +03009675
9676 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
9677 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +01009678 }
Chris Wilson4b0e3332014-05-30 16:35:26 +03009679
Matt Roper8e7d6882015-01-21 16:35:41 -08009680 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009681 cntl |= CURSOR_ROTATE_180;
9682
Chris Wilson4b0e3332014-05-30 16:35:26 +03009683 if (intel_crtc->cursor_cntl != cntl) {
9684 I915_WRITE(CURCNTR(pipe), cntl);
9685 POSTING_READ(CURCNTR(pipe));
9686 intel_crtc->cursor_cntl = cntl;
9687 }
9688
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009689 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009690 I915_WRITE(CURBASE(pipe), base);
9691 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +03009692
9693 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -07009694}
9695
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009696/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +01009697static void intel_crtc_update_cursor(struct drm_crtc *crtc,
9698 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009699{
9700 struct drm_device *dev = crtc->dev;
9701 struct drm_i915_private *dev_priv = dev->dev_private;
9702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
9703 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -07009704 int x = crtc->cursor_x;
9705 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009706 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009707
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009708 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009709 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009710
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009711 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +03009712 base = 0;
9713
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009714 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009715 base = 0;
9716
9717 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009718 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009719 base = 0;
9720
9721 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
9722 x = -x;
9723 }
9724 pos |= x << CURSOR_X_SHIFT;
9725
9726 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009727 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009728 base = 0;
9729
9730 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
9731 y = -y;
9732 }
9733 pos |= y << CURSOR_Y_SHIFT;
9734
Chris Wilson4b0e3332014-05-30 16:35:26 +03009735 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009736 return;
9737
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009738 I915_WRITE(CURPOS(pipe), pos);
9739
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009740 /* ILK+ do this automagically */
9741 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -08009742 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009743 base += (intel_crtc->base.cursor->state->crtc_h *
9744 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -07009745 }
9746
Ville Syrjälä8ac54662014-08-12 19:39:54 +03009747 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03009748 i845_update_cursor(crtc, base);
9749 else
9750 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +01009751}
9752
Ville Syrjälädc41c152014-08-13 11:57:05 +03009753static bool cursor_size_ok(struct drm_device *dev,
9754 uint32_t width, uint32_t height)
9755{
9756 if (width == 0 || height == 0)
9757 return false;
9758
9759 /*
9760 * 845g/865g are special in that they are only limited by
9761 * the width of their cursors, the height is arbitrary up to
9762 * the precision of the register. Everything else requires
9763 * square cursors, limited to a few power-of-two sizes.
9764 */
9765 if (IS_845G(dev) || IS_I865G(dev)) {
9766 if ((width & 63) != 0)
9767 return false;
9768
9769 if (width > (IS_845G(dev) ? 64 : 512))
9770 return false;
9771
9772 if (height > 1023)
9773 return false;
9774 } else {
9775 switch (width | height) {
9776 case 256:
9777 case 128:
9778 if (IS_GEN2(dev))
9779 return false;
9780 case 64:
9781 break;
9782 default:
9783 return false;
9784 }
9785 }
9786
9787 return true;
9788}
9789
Jesse Barnes79e53942008-11-07 14:24:08 -08009790static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +01009791 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -08009792{
James Simmons72034252010-08-03 01:33:19 +01009793 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -08009794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -08009795
James Simmons72034252010-08-03 01:33:19 +01009796 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -08009797 intel_crtc->lut_r[i] = red[i] >> 8;
9798 intel_crtc->lut_g[i] = green[i] >> 8;
9799 intel_crtc->lut_b[i] = blue[i] >> 8;
9800 }
9801
9802 intel_crtc_load_lut(crtc);
9803}
9804
Jesse Barnes79e53942008-11-07 14:24:08 -08009805/* VESA 640x480x72Hz mode to set on the pipe */
9806static struct drm_display_mode load_detect_mode = {
9807 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
9808 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
9809};
9810
Daniel Vettera8bb6812014-02-10 18:00:39 +01009811struct drm_framebuffer *
9812__intel_framebuffer_create(struct drm_device *dev,
9813 struct drm_mode_fb_cmd2 *mode_cmd,
9814 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +01009815{
9816 struct intel_framebuffer *intel_fb;
9817 int ret;
9818
9819 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
9820 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009821 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +01009822 return ERR_PTR(-ENOMEM);
9823 }
9824
9825 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009826 if (ret)
9827 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +01009828
9829 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009830err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +03009831 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +02009832 kfree(intel_fb);
9833
9834 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +01009835}
9836
Daniel Vetterb5ea6422014-03-02 21:18:00 +01009837static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +01009838intel_framebuffer_create(struct drm_device *dev,
9839 struct drm_mode_fb_cmd2 *mode_cmd,
9840 struct drm_i915_gem_object *obj)
9841{
9842 struct drm_framebuffer *fb;
9843 int ret;
9844
9845 ret = i915_mutex_lock_interruptible(dev);
9846 if (ret)
9847 return ERR_PTR(ret);
9848 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
9849 mutex_unlock(&dev->struct_mutex);
9850
9851 return fb;
9852}
9853
Chris Wilsond2dff872011-04-19 08:36:26 +01009854static u32
9855intel_framebuffer_pitch_for_width(int width, int bpp)
9856{
9857 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
9858 return ALIGN(pitch, 64);
9859}
9860
9861static u32
9862intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
9863{
9864 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +02009865 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +01009866}
9867
9868static struct drm_framebuffer *
9869intel_framebuffer_create_for_mode(struct drm_device *dev,
9870 struct drm_display_mode *mode,
9871 int depth, int bpp)
9872{
9873 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +00009874 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +01009875
9876 obj = i915_gem_alloc_object(dev,
9877 intel_framebuffer_size_for_mode(mode, bpp));
9878 if (obj == NULL)
9879 return ERR_PTR(-ENOMEM);
9880
9881 mode_cmd.width = mode->hdisplay;
9882 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -08009883 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
9884 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +00009885 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +01009886
9887 return intel_framebuffer_create(dev, &mode_cmd, obj);
9888}
9889
9890static struct drm_framebuffer *
9891mode_fits_in_fbdev(struct drm_device *dev,
9892 struct drm_display_mode *mode)
9893{
Daniel Vetter4520f532013-10-09 09:18:51 +02009894#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +01009895 struct drm_i915_private *dev_priv = dev->dev_private;
9896 struct drm_i915_gem_object *obj;
9897 struct drm_framebuffer *fb;
9898
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009899 if (!dev_priv->fbdev)
9900 return NULL;
9901
9902 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +01009903 return NULL;
9904
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009905 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +01009906 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +01009907
Jesse Barnes8bcd4552014-02-07 12:10:38 -08009908 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009909 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
9910 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +01009911 return NULL;
9912
Ville Syrjälä01f2c772011-12-20 00:06:49 +02009913 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +01009914 return NULL;
9915
9916 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +02009917#else
9918 return NULL;
9919#endif
Chris Wilsond2dff872011-04-19 08:36:26 +01009920}
9921
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +03009922static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
9923 struct drm_crtc *crtc,
9924 struct drm_display_mode *mode,
9925 struct drm_framebuffer *fb,
9926 int x, int y)
9927{
9928 struct drm_plane_state *plane_state;
9929 int hdisplay, vdisplay;
9930 int ret;
9931
9932 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
9933 if (IS_ERR(plane_state))
9934 return PTR_ERR(plane_state);
9935
9936 if (mode)
9937 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
9938 else
9939 hdisplay = vdisplay = 0;
9940
9941 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
9942 if (ret)
9943 return ret;
9944 drm_atomic_set_fb_for_plane(plane_state, fb);
9945 plane_state->crtc_x = 0;
9946 plane_state->crtc_y = 0;
9947 plane_state->crtc_w = hdisplay;
9948 plane_state->crtc_h = vdisplay;
9949 plane_state->src_x = x << 16;
9950 plane_state->src_y = y << 16;
9951 plane_state->src_w = hdisplay << 16;
9952 plane_state->src_h = vdisplay << 16;
9953
9954 return 0;
9955}
9956
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009957bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +01009958 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -05009959 struct intel_load_detect_pipe *old,
9960 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -08009961{
9962 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +02009963 struct intel_encoder *intel_encoder =
9964 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -08009965 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +01009966 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -08009967 struct drm_crtc *crtc = NULL;
9968 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +02009969 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -05009970 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +02009971 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +02009972 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +03009973 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -05009974 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -08009975
Chris Wilsond2dff872011-04-19 08:36:26 +01009976 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +03009977 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +03009978 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +01009979
Rob Clark51fd3712013-11-19 12:10:12 -05009980retry:
9981 ret = drm_modeset_lock(&config->connection_mutex, ctx);
9982 if (ret)
9983 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +02009984
Jesse Barnes79e53942008-11-07 14:24:08 -08009985 /*
9986 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +01009987 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009988 * - if the connector already has an assigned crtc, use it (but make
9989 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +01009990 *
Jesse Barnes79e53942008-11-07 14:24:08 -08009991 * - try to find the first unused crtc that can drive this connector,
9992 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -08009993 */
9994
9995 /* See if we already have a CRTC for this connector */
9996 if (encoder->crtc) {
9997 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +01009998
Rob Clark51fd3712013-11-19 12:10:12 -05009999 ret = drm_modeset_lock(&crtc->mutex, ctx);
10000 if (ret)
10001 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010002 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10003 if (ret)
10004 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010005
Daniel Vetter24218aa2012-08-12 19:27:11 +020010006 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010007 old->load_detect_temp = false;
10008
10009 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010010 if (connector->dpms != DRM_MODE_DPMS_ON)
10011 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010012
Chris Wilson71731882011-04-19 23:10:58 +010010013 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010014 }
10015
10016 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010017 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010018 i++;
10019 if (!(encoder->possible_crtcs & (1 << i)))
10020 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010021 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010022 continue;
10023 /* This can occur when applying the pipe A quirk on resume. */
10024 if (to_intel_crtc(possible_crtc)->new_enabled)
10025 continue;
10026
10027 crtc = possible_crtc;
10028 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010029 }
10030
10031 /*
10032 * If we didn't find an unused CRTC, don't use any.
10033 */
10034 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010035 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010036 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010037 }
10038
Rob Clark51fd3712013-11-19 12:10:12 -050010039 ret = drm_modeset_lock(&crtc->mutex, ctx);
10040 if (ret)
10041 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010042 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10043 if (ret)
10044 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010045 intel_encoder->new_crtc = to_intel_crtc(crtc);
10046 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010047
10048 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010049 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010050 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010051 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010052 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010053
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010054 state = drm_atomic_state_alloc(dev);
10055 if (!state)
10056 return false;
10057
10058 state->acquire_ctx = ctx;
10059
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010060 connector_state = drm_atomic_get_connector_state(state, connector);
10061 if (IS_ERR(connector_state)) {
10062 ret = PTR_ERR(connector_state);
10063 goto fail;
10064 }
10065
10066 connector_state->crtc = crtc;
10067 connector_state->best_encoder = &intel_encoder->base;
10068
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010069 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10070 if (IS_ERR(crtc_state)) {
10071 ret = PTR_ERR(crtc_state);
10072 goto fail;
10073 }
10074
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010075 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010076
Chris Wilson64927112011-04-20 07:25:26 +010010077 if (!mode)
10078 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010079
Chris Wilsond2dff872011-04-19 08:36:26 +010010080 /* We need a framebuffer large enough to accommodate all accesses
10081 * that the plane may generate whilst we perform load detection.
10082 * We can not rely on the fbcon either being present (we get called
10083 * during its initialisation to detect all boot displays, or it may
10084 * not even exist) or that it is large enough to satisfy the
10085 * requested mode.
10086 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010087 fb = mode_fits_in_fbdev(dev, mode);
10088 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010089 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010090 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10091 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010092 } else
10093 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010094 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010095 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010096 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010097 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010098
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010099 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10100 if (ret)
10101 goto fail;
10102
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010103 drm_mode_copy(&crtc_state->base.mode, mode);
10104
10105 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010106 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010107 if (old->release_fb)
10108 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010109 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010110 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010111 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010112
Jesse Barnes79e53942008-11-07 14:24:08 -080010113 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010114 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010115 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010116
10117 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010118 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010119fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010120 drm_atomic_state_free(state);
10121 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010122
Rob Clark51fd3712013-11-19 12:10:12 -050010123 if (ret == -EDEADLK) {
10124 drm_modeset_backoff(ctx);
10125 goto retry;
10126 }
10127
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010128 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010129}
10130
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010131void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010132 struct intel_load_detect_pipe *old,
10133 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010134{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010135 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010136 struct intel_encoder *intel_encoder =
10137 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010138 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010139 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010141 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010142 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010143 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010144 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010145
Chris Wilsond2dff872011-04-19 08:36:26 +010010146 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010147 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010148 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010149
Chris Wilson8261b192011-04-19 23:18:09 +010010150 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010151 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010152 if (!state)
10153 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010154
10155 state->acquire_ctx = ctx;
10156
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010157 connector_state = drm_atomic_get_connector_state(state, connector);
10158 if (IS_ERR(connector_state))
10159 goto fail;
10160
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010161 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10162 if (IS_ERR(crtc_state))
10163 goto fail;
10164
Daniel Vetterfc303102012-07-09 10:40:58 +020010165 to_intel_connector(connector)->new_encoder = NULL;
10166 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010167 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010168
10169 connector_state->best_encoder = NULL;
10170 connector_state->crtc = NULL;
10171
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010172 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010173
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010174 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10175 0, 0);
10176 if (ret)
10177 goto fail;
10178
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010179 ret = intel_set_mode(crtc, state);
10180 if (ret)
10181 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010182
Daniel Vetter36206362012-12-10 20:42:17 +010010183 if (old->release_fb) {
10184 drm_framebuffer_unregister_private(old->release_fb);
10185 drm_framebuffer_unreference(old->release_fb);
10186 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010187
Chris Wilson0622a532011-04-21 09:32:11 +010010188 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010189 }
10190
Eric Anholtc751ce42010-03-25 11:48:48 -070010191 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010192 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10193 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010194
10195 return;
10196fail:
10197 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10198 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010199}
10200
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010201static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010202 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010203{
10204 struct drm_i915_private *dev_priv = dev->dev_private;
10205 u32 dpll = pipe_config->dpll_hw_state.dpll;
10206
10207 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010208 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010209 else if (HAS_PCH_SPLIT(dev))
10210 return 120000;
10211 else if (!IS_GEN2(dev))
10212 return 96000;
10213 else
10214 return 48000;
10215}
10216
Jesse Barnes79e53942008-11-07 14:24:08 -080010217/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010218static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010219 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010220{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010221 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010222 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010223 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010224 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010225 u32 fp;
10226 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010227 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010228
10229 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010230 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010231 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010232 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010233
10234 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010235 if (IS_PINEVIEW(dev)) {
10236 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10237 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010238 } else {
10239 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10240 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10241 }
10242
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010243 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010244 if (IS_PINEVIEW(dev))
10245 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10246 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010247 else
10248 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010249 DPLL_FPA01_P1_POST_DIV_SHIFT);
10250
10251 switch (dpll & DPLL_MODE_MASK) {
10252 case DPLLB_MODE_DAC_SERIAL:
10253 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10254 5 : 10;
10255 break;
10256 case DPLLB_MODE_LVDS:
10257 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10258 7 : 14;
10259 break;
10260 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010261 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010262 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010263 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010264 }
10265
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010266 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010267 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010268 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010269 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010270 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010271 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010272 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010273
10274 if (is_lvds) {
10275 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10276 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010277
10278 if (lvds & LVDS_CLKB_POWER_UP)
10279 clock.p2 = 7;
10280 else
10281 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010282 } else {
10283 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10284 clock.p1 = 2;
10285 else {
10286 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10287 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10288 }
10289 if (dpll & PLL_P2_DIVIDE_BY_4)
10290 clock.p2 = 4;
10291 else
10292 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010293 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010294
10295 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010296 }
10297
Ville Syrjälä18442d02013-09-13 16:00:08 +030010298 /*
10299 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010300 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010301 * encoder's get_config() function.
10302 */
10303 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010304}
10305
Ville Syrjälä6878da02013-09-13 15:59:11 +030010306int intel_dotclock_calculate(int link_freq,
10307 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010308{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010309 /*
10310 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010311 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010312 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010313 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010314 *
10315 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010316 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010317 */
10318
Ville Syrjälä6878da02013-09-13 15:59:11 +030010319 if (!m_n->link_n)
10320 return 0;
10321
10322 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10323}
10324
Ville Syrjälä18442d02013-09-13 16:00:08 +030010325static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010326 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010327{
10328 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010329
10330 /* read out port_clock from the DPLL */
10331 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010332
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010333 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010334 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010335 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010336 * agree once we know their relationship in the encoder's
10337 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010338 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010339 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010340 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10341 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010342}
10343
10344/** Returns the currently programmed mode of the given pipe. */
10345struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10346 struct drm_crtc *crtc)
10347{
Jesse Barnes548f2452011-02-17 10:40:53 -080010348 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010349 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010350 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010351 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010352 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010353 int htot = I915_READ(HTOTAL(cpu_transcoder));
10354 int hsync = I915_READ(HSYNC(cpu_transcoder));
10355 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10356 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010357 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010358
10359 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10360 if (!mode)
10361 return NULL;
10362
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010363 /*
10364 * Construct a pipe_config sufficient for getting the clock info
10365 * back out of crtc_clock_get.
10366 *
10367 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10368 * to use a real value here instead.
10369 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010370 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010371 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010372 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10373 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10374 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010375 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10376
Ville Syrjälä773ae032013-09-23 17:48:20 +030010377 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010378 mode->hdisplay = (htot & 0xffff) + 1;
10379 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10380 mode->hsync_start = (hsync & 0xffff) + 1;
10381 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10382 mode->vdisplay = (vtot & 0xffff) + 1;
10383 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10384 mode->vsync_start = (vsync & 0xffff) + 1;
10385 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10386
10387 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010388
10389 return mode;
10390}
10391
Jesse Barnes652c3932009-08-17 13:31:43 -070010392static void intel_decrease_pllclock(struct drm_crtc *crtc)
10393{
10394 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010395 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010396 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010397
Sonika Jindalbaff2962014-07-22 11:16:35 +053010398 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010399 return;
10400
10401 if (!dev_priv->lvds_downclock_avail)
10402 return;
10403
10404 /*
10405 * Since this is called by a timer, we should never get here in
10406 * the manual case.
10407 */
10408 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010409 int pipe = intel_crtc->pipe;
10410 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010411 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010412
Zhao Yakui44d98a62009-10-09 11:39:40 +080010413 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010414
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010415 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010416
Chris Wilson074b5e12012-05-02 12:07:06 +010010417 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010418 dpll |= DISPLAY_RATE_SELECT_FPA1;
10419 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010420 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010421 dpll = I915_READ(dpll_reg);
10422 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010423 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010424 }
10425
10426}
10427
Chris Wilsonf047e392012-07-21 12:31:41 +010010428void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010429{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010430 struct drm_i915_private *dev_priv = dev->dev_private;
10431
Chris Wilsonf62a0072014-02-21 17:55:39 +000010432 if (dev_priv->mm.busy)
10433 return;
10434
Paulo Zanoni43694d62014-03-07 20:08:08 -030010435 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010436 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010437 if (INTEL_INFO(dev)->gen >= 6)
10438 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010439 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010440}
10441
10442void intel_mark_idle(struct drm_device *dev)
10443{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010444 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010445 struct drm_crtc *crtc;
10446
Chris Wilsonf62a0072014-02-21 17:55:39 +000010447 if (!dev_priv->mm.busy)
10448 return;
10449
10450 dev_priv->mm.busy = false;
10451
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010452 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010453 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010454 continue;
10455
10456 intel_decrease_pllclock(crtc);
10457 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010458
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010459 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010460 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010461
Paulo Zanoni43694d62014-03-07 20:08:08 -030010462 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010463}
10464
Jesse Barnes79e53942008-11-07 14:24:08 -080010465static void intel_crtc_destroy(struct drm_crtc *crtc)
10466{
10467 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010468 struct drm_device *dev = crtc->dev;
10469 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010470
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010471 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010472 work = intel_crtc->unpin_work;
10473 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010474 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010475
10476 if (work) {
10477 cancel_work_sync(&work->work);
10478 kfree(work);
10479 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010480
10481 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010482
Jesse Barnes79e53942008-11-07 14:24:08 -080010483 kfree(intel_crtc);
10484}
10485
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010486static void intel_unpin_work_fn(struct work_struct *__work)
10487{
10488 struct intel_unpin_work *work =
10489 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010490 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010491 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010492
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010493 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010494 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010495 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010496
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010497 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010498
10499 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010500 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010501 mutex_unlock(&dev->struct_mutex);
10502
Daniel Vetterf99d7062014-06-19 16:01:59 +020010503 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010504 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010505
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010506 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10507 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10508
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010509 kfree(work);
10510}
10511
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010512static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010513 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010514{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10516 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010517 unsigned long flags;
10518
10519 /* Ignore early vblank irqs */
10520 if (intel_crtc == NULL)
10521 return;
10522
Daniel Vetterf3260382014-09-15 14:55:23 +020010523 /*
10524 * This is called both by irq handlers and the reset code (to complete
10525 * lost pageflips) so needs the full irqsave spinlocks.
10526 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010527 spin_lock_irqsave(&dev->event_lock, flags);
10528 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010529
10530 /* Ensure we don't miss a work->pending update ... */
10531 smp_rmb();
10532
10533 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010534 spin_unlock_irqrestore(&dev->event_lock, flags);
10535 return;
10536 }
10537
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010538 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010539
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010540 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010541}
10542
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010543void intel_finish_page_flip(struct drm_device *dev, int pipe)
10544{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010545 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010546 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10547
Mario Kleiner49b14a52010-12-09 07:00:07 +010010548 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010549}
10550
10551void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10552{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010553 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010554 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10555
Mario Kleiner49b14a52010-12-09 07:00:07 +010010556 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010557}
10558
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010559/* Is 'a' after or equal to 'b'? */
10560static bool g4x_flip_count_after_eq(u32 a, u32 b)
10561{
10562 return !((a - b) & 0x80000000);
10563}
10564
10565static bool page_flip_finished(struct intel_crtc *crtc)
10566{
10567 struct drm_device *dev = crtc->base.dev;
10568 struct drm_i915_private *dev_priv = dev->dev_private;
10569
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010570 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10571 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10572 return true;
10573
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010574 /*
10575 * The relevant registers doen't exist on pre-ctg.
10576 * As the flip done interrupt doesn't trigger for mmio
10577 * flips on gmch platforms, a flip count check isn't
10578 * really needed there. But since ctg has the registers,
10579 * include it in the check anyway.
10580 */
10581 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10582 return true;
10583
10584 /*
10585 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10586 * used the same base address. In that case the mmio flip might
10587 * have completed, but the CS hasn't even executed the flip yet.
10588 *
10589 * A flip count check isn't enough as the CS might have updated
10590 * the base address just after start of vblank, but before we
10591 * managed to process the interrupt. This means we'd complete the
10592 * CS flip too soon.
10593 *
10594 * Combining both checks should get us a good enough result. It may
10595 * still happen that the CS flip has been executed, but has not
10596 * yet actually completed. But in case the base address is the same
10597 * anyway, we don't really care.
10598 */
10599 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10600 crtc->unpin_work->gtt_offset &&
10601 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10602 crtc->unpin_work->flip_count);
10603}
10604
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010605void intel_prepare_page_flip(struct drm_device *dev, int plane)
10606{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010607 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010608 struct intel_crtc *intel_crtc =
10609 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10610 unsigned long flags;
10611
Daniel Vetterf3260382014-09-15 14:55:23 +020010612
10613 /*
10614 * This is called both by irq handlers and the reset code (to complete
10615 * lost pageflips) so needs the full irqsave spinlocks.
10616 *
10617 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010618 * generate a page-flip completion irq, i.e. every modeset
10619 * is also accompanied by a spurious intel_prepare_page_flip().
10620 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010621 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010622 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010623 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010624 spin_unlock_irqrestore(&dev->event_lock, flags);
10625}
10626
Robin Schroereba905b2014-05-18 02:24:50 +020010627static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000010628{
10629 /* Ensure that the work item is consistent when activating it ... */
10630 smp_wmb();
10631 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
10632 /* and that it is marked active as soon as the irq could fire. */
10633 smp_wmb();
10634}
10635
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010636static int intel_gen2_queue_flip(struct drm_device *dev,
10637 struct drm_crtc *crtc,
10638 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010639 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010640 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010641 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010642{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010643 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010644 u32 flip_mask;
10645 int ret;
10646
Daniel Vetter6d90c952012-04-26 23:28:05 +020010647 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010648 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010649 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010650
10651 /* Can't queue multiple flips, so wait for the previous
10652 * one to finish before executing the next.
10653 */
10654 if (intel_crtc->plane)
10655 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10656 else
10657 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010658 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10659 intel_ring_emit(ring, MI_NOOP);
10660 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10661 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10662 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010663 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010664 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000010665
10666 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010667 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010668 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010669}
10670
10671static int intel_gen3_queue_flip(struct drm_device *dev,
10672 struct drm_crtc *crtc,
10673 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010674 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010675 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010676 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010677{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010678 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010679 u32 flip_mask;
10680 int ret;
10681
Daniel Vetter6d90c952012-04-26 23:28:05 +020010682 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010683 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010684 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010685
10686 if (intel_crtc->plane)
10687 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
10688 else
10689 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010690 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
10691 intel_ring_emit(ring, MI_NOOP);
10692 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
10693 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10694 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010695 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020010696 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010697
Chris Wilsone7d841c2012-12-03 11:36:30 +000010698 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010699 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010700 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010701}
10702
10703static int intel_gen4_queue_flip(struct drm_device *dev,
10704 struct drm_crtc *crtc,
10705 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010706 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010707 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010708 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010709{
10710 struct drm_i915_private *dev_priv = dev->dev_private;
10711 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10712 uint32_t pf, pipesrc;
10713 int ret;
10714
Daniel Vetter6d90c952012-04-26 23:28:05 +020010715 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010716 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010717 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010718
10719 /* i965+ uses the linear or tiled offsets from the
10720 * Display Registers (which do not change across a page-flip)
10721 * so we need only reprogram the base address.
10722 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020010723 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10724 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10725 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010726 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020010727 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010728
10729 /* XXX Enabling the panel-fitter across page-flip is so far
10730 * untested on non-native modes, so ignore it for now.
10731 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
10732 */
10733 pf = 0;
10734 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010735 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010736
10737 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010738 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010739 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010740}
10741
10742static int intel_gen6_queue_flip(struct drm_device *dev,
10743 struct drm_crtc *crtc,
10744 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010745 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010746 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010747 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010748{
10749 struct drm_i915_private *dev_priv = dev->dev_private;
10750 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10751 uint32_t pf, pipesrc;
10752 int ret;
10753
Daniel Vetter6d90c952012-04-26 23:28:05 +020010754 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010755 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010756 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010757
Daniel Vetter6d90c952012-04-26 23:28:05 +020010758 intel_ring_emit(ring, MI_DISPLAY_FLIP |
10759 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
10760 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010761 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010762
Chris Wilson99d9acd2012-04-17 20:37:00 +010010763 /* Contrary to the suggestions in the documentation,
10764 * "Enable Panel Fitter" does not seem to be required when page
10765 * flipping with a non-native mode, and worse causes a normal
10766 * modeset to fail.
10767 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
10768 */
10769 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010770 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020010771 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000010772
10773 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010774 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010775 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070010776}
10777
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010778static int intel_gen7_queue_flip(struct drm_device *dev,
10779 struct drm_crtc *crtc,
10780 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070010781 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010010782 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070010783 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010784{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010785 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010786 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010010787 int len, ret;
10788
Robin Schroereba905b2014-05-18 02:24:50 +020010789 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010790 case PLANE_A:
10791 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
10792 break;
10793 case PLANE_B:
10794 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
10795 break;
10796 case PLANE_C:
10797 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
10798 break;
10799 default:
10800 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010801 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010802 }
10803
Chris Wilsonffe74d72013-08-26 20:58:12 +010010804 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010010805 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010010806 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010010807 /*
10808 * On Gen 8, SRM is now taking an extra dword to accommodate
10809 * 48bits addresses, and we need a NOOP for the batch size to
10810 * stay even.
10811 */
10812 if (IS_GEN8(dev))
10813 len += 2;
10814 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010815
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010816 /*
10817 * BSpec MI_DISPLAY_FLIP for IVB:
10818 * "The full packet must be contained within the same cache line."
10819 *
10820 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
10821 * cacheline, if we ever start emitting more commands before
10822 * the MI_DISPLAY_FLIP we may need to first emit everything else,
10823 * then do the cacheline alignment, and finally emit the
10824 * MI_DISPLAY_FLIP.
10825 */
10826 ret = intel_ring_cacheline_align(ring);
10827 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010828 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020010829
Chris Wilsonffe74d72013-08-26 20:58:12 +010010830 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010831 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030010832 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010833
Chris Wilsonffe74d72013-08-26 20:58:12 +010010834 /* Unmask the flip-done completion message. Note that the bspec says that
10835 * we should do this for both the BCS and RCS, and that we must not unmask
10836 * more than one flip event at any time (or ensure that one flip message
10837 * can be sent by waiting for flip-done prior to queueing new flips).
10838 * Experimentation says that BCS works despite DERRMR masking all
10839 * flip-done completion events and that unmasking all planes at once
10840 * for the RCS also doesn't appear to drop events. Setting the DERRMR
10841 * to zero does lead to lockups within MI_DISPLAY_FLIP.
10842 */
10843 if (ring->id == RCS) {
10844 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
10845 intel_ring_emit(ring, DERRMR);
10846 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
10847 DERRMR_PIPEB_PRI_FLIP_DONE |
10848 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010010849 if (IS_GEN8(dev))
10850 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
10851 MI_SRM_LRM_GLOBAL_GTT);
10852 else
10853 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
10854 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010010855 intel_ring_emit(ring, DERRMR);
10856 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010010857 if (IS_GEN8(dev)) {
10858 intel_ring_emit(ring, 0);
10859 intel_ring_emit(ring, MI_NOOP);
10860 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010010861 }
10862
Daniel Vettercb05d8d2012-05-23 14:02:00 +020010863 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010864 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010865 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010866 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000010867
10868 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010010869 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010010870 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070010871}
10872
Sourab Gupta84c33a62014-06-02 16:47:17 +053010873static bool use_mmio_flip(struct intel_engine_cs *ring,
10874 struct drm_i915_gem_object *obj)
10875{
10876 /*
10877 * This is not being used for older platforms, because
10878 * non-availability of flip done interrupt forces us to use
10879 * CS flips. Older platforms derive flip done using some clever
10880 * tricks involving the flip_pending status bits and vblank irqs.
10881 * So using MMIO flips there would disrupt this mechanism.
10882 */
10883
Chris Wilson8e09bf82014-07-08 10:40:30 +010010884 if (ring == NULL)
10885 return true;
10886
Sourab Gupta84c33a62014-06-02 16:47:17 +053010887 if (INTEL_INFO(ring->dev)->gen < 5)
10888 return false;
10889
10890 if (i915.use_mmio_flip < 0)
10891 return false;
10892 else if (i915.use_mmio_flip > 0)
10893 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010010894 else if (i915.enable_execlists)
10895 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053010896 else
Chris Wilsonb4716182015-04-27 13:41:17 +010010897 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010898}
10899
Damien Lespiauff944562014-11-20 14:58:16 +000010900static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
10901{
10902 struct drm_device *dev = intel_crtc->base.dev;
10903 struct drm_i915_private *dev_priv = dev->dev_private;
10904 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000010905 const enum pipe pipe = intel_crtc->pipe;
10906 u32 ctl, stride;
10907
10908 ctl = I915_READ(PLANE_CTL(pipe, 0));
10909 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010910 switch (fb->modifier[0]) {
10911 case DRM_FORMAT_MOD_NONE:
10912 break;
10913 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000010914 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010915 break;
10916 case I915_FORMAT_MOD_Y_TILED:
10917 ctl |= PLANE_CTL_TILED_Y;
10918 break;
10919 case I915_FORMAT_MOD_Yf_TILED:
10920 ctl |= PLANE_CTL_TILED_YF;
10921 break;
10922 default:
10923 MISSING_CASE(fb->modifier[0]);
10924 }
Damien Lespiauff944562014-11-20 14:58:16 +000010925
10926 /*
10927 * The stride is either expressed as a multiple of 64 bytes chunks for
10928 * linear buffers or in number of tiles for tiled buffers.
10929 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010010930 stride = fb->pitches[0] /
10931 intel_fb_stride_alignment(dev, fb->modifier[0],
10932 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000010933
10934 /*
10935 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
10936 * PLANE_SURF updates, the update is then guaranteed to be atomic.
10937 */
10938 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
10939 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
10940
10941 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
10942 POSTING_READ(PLANE_SURF(pipe, 0));
10943}
10944
10945static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010946{
10947 struct drm_device *dev = intel_crtc->base.dev;
10948 struct drm_i915_private *dev_priv = dev->dev_private;
10949 struct intel_framebuffer *intel_fb =
10950 to_intel_framebuffer(intel_crtc->base.primary->fb);
10951 struct drm_i915_gem_object *obj = intel_fb->obj;
10952 u32 dspcntr;
10953 u32 reg;
10954
Sourab Gupta84c33a62014-06-02 16:47:17 +053010955 reg = DSPCNTR(intel_crtc->plane);
10956 dspcntr = I915_READ(reg);
10957
Damien Lespiauc5d97472014-10-25 00:11:11 +010010958 if (obj->tiling_mode != I915_TILING_NONE)
10959 dspcntr |= DISPPLANE_TILED;
10960 else
10961 dspcntr &= ~DISPPLANE_TILED;
10962
Sourab Gupta84c33a62014-06-02 16:47:17 +053010963 I915_WRITE(reg, dspcntr);
10964
10965 I915_WRITE(DSPSURF(intel_crtc->plane),
10966 intel_crtc->unpin_work->gtt_offset);
10967 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010968
Damien Lespiauff944562014-11-20 14:58:16 +000010969}
10970
10971/*
10972 * XXX: This is the temporary way to update the plane registers until we get
10973 * around to using the usual plane update functions for MMIO flips
10974 */
10975static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
10976{
10977 struct drm_device *dev = intel_crtc->base.dev;
10978 bool atomic_update;
10979 u32 start_vbl_count;
10980
10981 intel_mark_page_flip_active(intel_crtc);
10982
10983 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
10984
10985 if (INTEL_INFO(dev)->gen >= 9)
10986 skl_do_mmio_flip(intel_crtc);
10987 else
10988 /* use_mmio_flip() retricts MMIO flips to ilk+ */
10989 ilk_do_mmio_flip(intel_crtc);
10990
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010991 if (atomic_update)
10992 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010993}
10994
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020010995static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053010996{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010010997 struct intel_mmio_flip *mmio_flip =
10998 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053010999
Daniel Vettereed29a52015-05-21 14:21:25 +020011000 if (mmio_flip->req)
11001 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011002 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011003 false, NULL,
11004 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011005
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011006 intel_do_mmio_flip(mmio_flip->crtc);
11007
Daniel Vettereed29a52015-05-21 14:21:25 +020011008 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011009 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011010}
11011
11012static int intel_queue_mmio_flip(struct drm_device *dev,
11013 struct drm_crtc *crtc,
11014 struct drm_framebuffer *fb,
11015 struct drm_i915_gem_object *obj,
11016 struct intel_engine_cs *ring,
11017 uint32_t flags)
11018{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011019 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011020
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011021 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11022 if (mmio_flip == NULL)
11023 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011024
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011025 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011026 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011027 mmio_flip->crtc = to_intel_crtc(crtc);
11028
11029 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11030 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011031
Sourab Gupta84c33a62014-06-02 16:47:17 +053011032 return 0;
11033}
11034
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011035static int intel_default_queue_flip(struct drm_device *dev,
11036 struct drm_crtc *crtc,
11037 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011038 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011039 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011040 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011041{
11042 return -ENODEV;
11043}
11044
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011045static bool __intel_pageflip_stall_check(struct drm_device *dev,
11046 struct drm_crtc *crtc)
11047{
11048 struct drm_i915_private *dev_priv = dev->dev_private;
11049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11050 struct intel_unpin_work *work = intel_crtc->unpin_work;
11051 u32 addr;
11052
11053 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11054 return true;
11055
11056 if (!work->enable_stall_check)
11057 return false;
11058
11059 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011060 if (work->flip_queued_req &&
11061 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011062 return false;
11063
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011064 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011065 }
11066
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011067 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011068 return false;
11069
11070 /* Potential stall - if we see that the flip has happened,
11071 * assume a missed interrupt. */
11072 if (INTEL_INFO(dev)->gen >= 4)
11073 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11074 else
11075 addr = I915_READ(DSPADDR(intel_crtc->plane));
11076
11077 /* There is a potential issue here with a false positive after a flip
11078 * to the same address. We could address this by checking for a
11079 * non-incrementing frame counter.
11080 */
11081 return addr == work->gtt_offset;
11082}
11083
11084void intel_check_page_flip(struct drm_device *dev, int pipe)
11085{
11086 struct drm_i915_private *dev_priv = dev->dev_private;
11087 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11088 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011089 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011090
Dave Gordon6c51d462015-03-06 15:34:26 +000011091 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011092
11093 if (crtc == NULL)
11094 return;
11095
Daniel Vetterf3260382014-09-15 14:55:23 +020011096 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011097 work = intel_crtc->unpin_work;
11098 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011099 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011100 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011101 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011102 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011103 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011104 if (work != NULL &&
11105 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11106 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011107 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011108}
11109
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011110static int intel_crtc_page_flip(struct drm_crtc *crtc,
11111 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011112 struct drm_pending_vblank_event *event,
11113 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011114{
11115 struct drm_device *dev = crtc->dev;
11116 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011117 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011118 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011119 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011120 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011121 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011122 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011123 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011124 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011125 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011126
Matt Roper2ff8fde2014-07-08 07:50:07 -070011127 /*
11128 * drm_mode_page_flip_ioctl() should already catch this, but double
11129 * check to be safe. In the future we may enable pageflipping from
11130 * a disabled primary plane.
11131 */
11132 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11133 return -EBUSY;
11134
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011135 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011136 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011137 return -EINVAL;
11138
11139 /*
11140 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11141 * Note that pitch changes could also affect these register.
11142 */
11143 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011144 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11145 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011146 return -EINVAL;
11147
Chris Wilsonf900db42014-02-20 09:26:13 +000011148 if (i915_terminally_wedged(&dev_priv->gpu_error))
11149 goto out_hang;
11150
Daniel Vetterb14c5672013-09-19 12:18:32 +020011151 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011152 if (work == NULL)
11153 return -ENOMEM;
11154
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011155 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011156 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011157 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011158 INIT_WORK(&work->work, intel_unpin_work_fn);
11159
Daniel Vetter87b6b102014-05-15 15:33:46 +020011160 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011161 if (ret)
11162 goto free_work;
11163
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011164 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011165 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011166 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011167 /* Before declaring the flip queue wedged, check if
11168 * the hardware completed the operation behind our backs.
11169 */
11170 if (__intel_pageflip_stall_check(dev, crtc)) {
11171 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11172 page_flip_completed(intel_crtc);
11173 } else {
11174 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011175 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011176
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011177 drm_crtc_vblank_put(crtc);
11178 kfree(work);
11179 return -EBUSY;
11180 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011181 }
11182 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011183 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011184
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011185 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11186 flush_workqueue(dev_priv->wq);
11187
Jesse Barnes75dfca82010-02-10 15:09:44 -080011188 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011189 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011190 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011191
Matt Roperf4510a22014-04-01 15:22:40 -070011192 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011193 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011194
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011195 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011196
Chris Wilson89ed88b2015-02-16 14:31:49 +000011197 ret = i915_mutex_lock_interruptible(dev);
11198 if (ret)
11199 goto cleanup;
11200
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011201 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011202 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011203
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011204 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011205 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011206
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011207 if (IS_VALLEYVIEW(dev)) {
11208 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011209 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011210 /* vlv: DISPLAY_FLIP fails to change tiling */
11211 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011212 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011213 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011214 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011215 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011216 if (ring == NULL || ring->id != RCS)
11217 ring = &dev_priv->ring[BCS];
11218 } else {
11219 ring = &dev_priv->ring[RCS];
11220 }
11221
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011222 mmio_flip = use_mmio_flip(ring, obj);
11223
11224 /* When using CS flips, we want to emit semaphores between rings.
11225 * However, when using mmio flips we will create a task to do the
11226 * synchronisation, so all we want here is to pin the framebuffer
11227 * into the display plane and skip any waits.
11228 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011229 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011230 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011231 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011232 if (ret)
11233 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011234
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011235 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11236 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011237
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011238 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011239 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11240 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011241 if (ret)
11242 goto cleanup_unpin;
11243
John Harrisonf06cc1b2014-11-24 18:49:37 +000011244 i915_gem_request_assign(&work->flip_queued_req,
11245 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011246 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011247 if (obj->last_write_req) {
11248 ret = i915_gem_check_olr(obj->last_write_req);
11249 if (ret)
11250 goto cleanup_unpin;
11251 }
11252
Sourab Gupta84c33a62014-06-02 16:47:17 +053011253 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011254 page_flip_flags);
11255 if (ret)
11256 goto cleanup_unpin;
11257
John Harrisonf06cc1b2014-11-24 18:49:37 +000011258 i915_gem_request_assign(&work->flip_queued_req,
11259 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011260 }
11261
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011262 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011263 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011264
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011265 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011266 INTEL_FRONTBUFFER_PRIMARY(pipe));
11267
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011268 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011269 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011270 mutex_unlock(&dev->struct_mutex);
11271
Jesse Barnese5510fa2010-07-01 16:48:37 -070011272 trace_i915_flip_request(intel_crtc->plane, obj);
11273
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011274 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011275
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011276cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011277 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011278cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011279 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011280 mutex_unlock(&dev->struct_mutex);
11281cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011282 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011283 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011284
Chris Wilson89ed88b2015-02-16 14:31:49 +000011285 drm_gem_object_unreference_unlocked(&obj->base);
11286 drm_framebuffer_unreference(work->old_fb);
11287
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011288 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011289 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011290 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011291
Daniel Vetter87b6b102014-05-15 15:33:46 +020011292 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011293free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011294 kfree(work);
11295
Chris Wilsonf900db42014-02-20 09:26:13 +000011296 if (ret == -EIO) {
11297out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011298 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011299 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011300 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011301 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011302 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011303 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011304 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011305 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011306}
11307
Jani Nikula65b38e02015-04-13 11:26:56 +030011308static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011309 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11310 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011311 .atomic_begin = intel_begin_crtc_commit,
11312 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011313};
11314
Daniel Vetter9a935852012-07-05 22:34:27 +020011315/**
11316 * intel_modeset_update_staged_output_state
11317 *
11318 * Updates the staged output configuration state, e.g. after we've read out the
11319 * current hw state.
11320 */
11321static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11322{
Ville Syrjälä76688512014-01-10 11:28:06 +020011323 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011324 struct intel_encoder *encoder;
11325 struct intel_connector *connector;
11326
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011327 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011328 connector->new_encoder =
11329 to_intel_encoder(connector->base.encoder);
11330 }
11331
Damien Lespiaub2784e12014-08-05 11:29:37 +010011332 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011333 encoder->new_crtc =
11334 to_intel_crtc(encoder->base.crtc);
11335 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011336
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011337 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011338 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011339 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011340}
11341
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011342/* Transitional helper to copy current connector/encoder state to
11343 * connector->state. This is needed so that code that is partially
11344 * converted to atomic does the right thing.
11345 */
11346static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11347{
11348 struct intel_connector *connector;
11349
11350 for_each_intel_connector(dev, connector) {
11351 if (connector->base.encoder) {
11352 connector->base.state->best_encoder =
11353 connector->base.encoder;
11354 connector->base.state->crtc =
11355 connector->base.encoder->crtc;
11356 } else {
11357 connector->base.state->best_encoder = NULL;
11358 connector->base.state->crtc = NULL;
11359 }
11360 }
11361}
11362
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011363/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011364 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011365static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011366{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011367 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011368 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011369 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011370
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011371 for_each_intel_connector(state->dev, connector) {
11372 connector->base.encoder = connector->base.state->best_encoder;
11373 if (connector->base.encoder)
11374 connector->base.encoder->crtc =
11375 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011376 }
11377
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011378 /* Update crtc of disabled encoders */
11379 for_each_intel_encoder(state->dev, encoder) {
11380 int num_connectors = 0;
11381
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011382 for_each_intel_connector(state->dev, connector)
11383 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011384 num_connectors++;
11385
11386 if (num_connectors == 0)
11387 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011388 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011389
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011390 for_each_intel_crtc(state->dev, crtc) {
11391 crtc->base.enabled = crtc->base.state->enable;
11392 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011393 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011394
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011395 /* Copy the new configuration to the staged state, to keep the few
11396 * pieces of code that haven't been converted yet happy */
11397 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011398}
11399
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011400static void
Robin Schroereba905b2014-05-18 02:24:50 +020011401connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011402 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011403{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011404 int bpp = pipe_config->pipe_bpp;
11405
11406 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11407 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011408 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011409
11410 /* Don't use an invalid EDID bpc value */
11411 if (connector->base.display_info.bpc &&
11412 connector->base.display_info.bpc * 3 < bpp) {
11413 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11414 bpp, connector->base.display_info.bpc*3);
11415 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11416 }
11417
11418 /* Clamp bpp to 8 on screens without EDID 1.4 */
11419 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11420 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11421 bpp);
11422 pipe_config->pipe_bpp = 24;
11423 }
11424}
11425
11426static int
11427compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011428 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011429{
11430 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011431 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011432 struct drm_connector *connector;
11433 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011434 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011435
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011436 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011437 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011438 else if (INTEL_INFO(dev)->gen >= 5)
11439 bpp = 12*3;
11440 else
11441 bpp = 8*3;
11442
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011443
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011444 pipe_config->pipe_bpp = bpp;
11445
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011446 state = pipe_config->base.state;
11447
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011448 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011449 for_each_connector_in_state(state, connector, connector_state, i) {
11450 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011451 continue;
11452
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011453 connected_sink_compute_bpp(to_intel_connector(connector),
11454 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011455 }
11456
11457 return bpp;
11458}
11459
Daniel Vetter644db712013-09-19 14:53:58 +020011460static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11461{
11462 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11463 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011464 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011465 mode->crtc_hdisplay, mode->crtc_hsync_start,
11466 mode->crtc_hsync_end, mode->crtc_htotal,
11467 mode->crtc_vdisplay, mode->crtc_vsync_start,
11468 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11469}
11470
Daniel Vetterc0b03412013-05-28 12:05:54 +020011471static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011472 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011473 const char *context)
11474{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011475 struct drm_device *dev = crtc->base.dev;
11476 struct drm_plane *plane;
11477 struct intel_plane *intel_plane;
11478 struct intel_plane_state *state;
11479 struct drm_framebuffer *fb;
11480
11481 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11482 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011483
11484 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11485 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11486 pipe_config->pipe_bpp, pipe_config->dither);
11487 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11488 pipe_config->has_pch_encoder,
11489 pipe_config->fdi_lanes,
11490 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11491 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11492 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011493 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11494 pipe_config->has_dp_encoder,
11495 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11496 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11497 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011498
11499 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11500 pipe_config->has_dp_encoder,
11501 pipe_config->dp_m2_n2.gmch_m,
11502 pipe_config->dp_m2_n2.gmch_n,
11503 pipe_config->dp_m2_n2.link_m,
11504 pipe_config->dp_m2_n2.link_n,
11505 pipe_config->dp_m2_n2.tu);
11506
Daniel Vetter55072d12014-11-20 16:10:28 +010011507 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11508 pipe_config->has_audio,
11509 pipe_config->has_infoframe);
11510
Daniel Vetterc0b03412013-05-28 12:05:54 +020011511 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011512 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011513 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011514 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11515 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011516 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011517 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11518 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011519 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11520 crtc->num_scalers,
11521 pipe_config->scaler_state.scaler_users,
11522 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011523 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11524 pipe_config->gmch_pfit.control,
11525 pipe_config->gmch_pfit.pgm_ratios,
11526 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011527 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011528 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011529 pipe_config->pch_pfit.size,
11530 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011531 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011532 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011533
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011534 if (IS_BROXTON(dev)) {
11535 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11536 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11537 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11538 pipe_config->ddi_pll_sel,
11539 pipe_config->dpll_hw_state.ebb0,
11540 pipe_config->dpll_hw_state.pll0,
11541 pipe_config->dpll_hw_state.pll1,
11542 pipe_config->dpll_hw_state.pll2,
11543 pipe_config->dpll_hw_state.pll3,
11544 pipe_config->dpll_hw_state.pll6,
11545 pipe_config->dpll_hw_state.pll8,
11546 pipe_config->dpll_hw_state.pcsdw12);
11547 } else if (IS_SKYLAKE(dev)) {
11548 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11549 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11550 pipe_config->ddi_pll_sel,
11551 pipe_config->dpll_hw_state.ctrl1,
11552 pipe_config->dpll_hw_state.cfgcr1,
11553 pipe_config->dpll_hw_state.cfgcr2);
11554 } else if (HAS_DDI(dev)) {
11555 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11556 pipe_config->ddi_pll_sel,
11557 pipe_config->dpll_hw_state.wrpll);
11558 } else {
11559 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11560 "fp0: 0x%x, fp1: 0x%x\n",
11561 pipe_config->dpll_hw_state.dpll,
11562 pipe_config->dpll_hw_state.dpll_md,
11563 pipe_config->dpll_hw_state.fp0,
11564 pipe_config->dpll_hw_state.fp1);
11565 }
11566
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011567 DRM_DEBUG_KMS("planes on this crtc\n");
11568 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11569 intel_plane = to_intel_plane(plane);
11570 if (intel_plane->pipe != crtc->pipe)
11571 continue;
11572
11573 state = to_intel_plane_state(plane->state);
11574 fb = state->base.fb;
11575 if (!fb) {
11576 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11577 "disabled, scaler_id = %d\n",
11578 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11579 plane->base.id, intel_plane->pipe,
11580 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11581 drm_plane_index(plane), state->scaler_id);
11582 continue;
11583 }
11584
11585 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11586 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11587 plane->base.id, intel_plane->pipe,
11588 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11589 drm_plane_index(plane));
11590 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11591 fb->base.id, fb->width, fb->height, fb->pixel_format);
11592 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11593 state->scaler_id,
11594 state->src.x1 >> 16, state->src.y1 >> 16,
11595 drm_rect_width(&state->src) >> 16,
11596 drm_rect_height(&state->src) >> 16,
11597 state->dst.x1, state->dst.y1,
11598 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11599 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011600}
11601
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011602static bool encoders_cloneable(const struct intel_encoder *a,
11603 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011604{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011605 /* masks could be asymmetric, so check both ways */
11606 return a == b || (a->cloneable & (1 << b->type) &&
11607 b->cloneable & (1 << a->type));
11608}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011609
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011610static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11611 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011612 struct intel_encoder *encoder)
11613{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011614 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011615 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011616 struct drm_connector_state *connector_state;
11617 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011618
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011619 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011620 if (connector_state->crtc != &crtc->base)
11621 continue;
11622
11623 source_encoder =
11624 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011625 if (!encoders_cloneable(encoder, source_encoder))
11626 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011627 }
11628
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011629 return true;
11630}
11631
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011632static bool check_encoder_cloning(struct drm_atomic_state *state,
11633 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011634{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011635 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011636 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011637 struct drm_connector_state *connector_state;
11638 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011639
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011640 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011641 if (connector_state->crtc != &crtc->base)
11642 continue;
11643
11644 encoder = to_intel_encoder(connector_state->best_encoder);
11645 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011646 return false;
11647 }
11648
11649 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011650}
11651
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011652static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011653{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011654 struct drm_device *dev = state->dev;
11655 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011656 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011657 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011658 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011659 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011660
11661 /*
11662 * Walk the connector list instead of the encoder
11663 * list to detect the problem on ddi platforms
11664 * where there's just one encoder per digital port.
11665 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011666 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011667 if (!connector_state->best_encoder)
11668 continue;
11669
11670 encoder = to_intel_encoder(connector_state->best_encoder);
11671
11672 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011673
11674 switch (encoder->type) {
11675 unsigned int port_mask;
11676 case INTEL_OUTPUT_UNKNOWN:
11677 if (WARN_ON(!HAS_DDI(dev)))
11678 break;
11679 case INTEL_OUTPUT_DISPLAYPORT:
11680 case INTEL_OUTPUT_HDMI:
11681 case INTEL_OUTPUT_EDP:
11682 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
11683
11684 /* the same port mustn't appear more than once */
11685 if (used_ports & port_mask)
11686 return false;
11687
11688 used_ports |= port_mask;
11689 default:
11690 break;
11691 }
11692 }
11693
11694 return true;
11695}
11696
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011697static void
11698clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
11699{
11700 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011701 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011702 struct intel_dpll_hw_state dpll_hw_state;
11703 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011704 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011705
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030011706 /* FIXME: before the switch to atomic started, a new pipe_config was
11707 * kzalloc'd. Code that depends on any field being zero should be
11708 * fixed, so that the crtc_state can be safely duplicated. For now,
11709 * only fields that are know to not cause problems are preserved. */
11710
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011711 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070011712 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011713 shared_dpll = crtc_state->shared_dpll;
11714 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011715 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011716
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011717 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011718
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011719 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070011720 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030011721 crtc_state->shared_dpll = shared_dpll;
11722 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030011723 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011724}
11725
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011726static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011727intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011728 struct drm_atomic_state *state,
11729 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020011730{
Daniel Vetter7758a112012-07-08 19:40:39 +020011731 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011732 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011733 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011734 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011735 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010011736 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020011737
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011738 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011739 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011740 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011741 }
11742
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011743 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011744 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011745 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011746 }
11747
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020011748 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020011749
Daniel Vettere143a212013-07-04 12:01:15 +020011750 pipe_config->cpu_transcoder =
11751 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010011752
Imre Deak2960bc92013-07-30 13:36:32 +030011753 /*
11754 * Sanitize sync polarity flags based on requested ones. If neither
11755 * positive or negative polarity is requested, treat this as meaning
11756 * negative polarity.
11757 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011758 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011759 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011760 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011761
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011762 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030011763 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011764 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030011765
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011766 /* Compute a starting value for pipe_config->pipe_bpp taking the source
11767 * plane pixel format and any sink constraints into account. Returns the
11768 * source plane bpp so that dithering can be selected on mismatches
11769 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011770 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
11771 pipe_config);
11772 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011773 goto fail;
11774
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011775 /*
11776 * Determine the real pipe dimensions. Note that stereo modes can
11777 * increase the actual pipe size due to the frame doubling and
11778 * insertion of additional space for blanks between the frame. This
11779 * is stored in the crtc timings. We use the requested mode to do this
11780 * computation to clearly distinguish it from the adjusted mode, which
11781 * can be changed by the connectors in the below retry loop.
11782 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011783 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080011784 &pipe_config->pipe_src_w,
11785 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030011786
Daniel Vettere29c22c2013-02-21 00:00:16 +010011787encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020011788 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020011789 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020011790 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011791
Daniel Vetter135c81b2013-07-21 21:37:09 +020011792 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011793 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
11794 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020011795
Daniel Vetter7758a112012-07-08 19:40:39 +020011796 /* Pass our mode to the connectors and the CRTC to give them a chance to
11797 * adjust it according to limitations or connector properties, and also
11798 * a chance to reject the mode entirely.
11799 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011800 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020011801 if (connector_state->crtc != crtc)
11802 continue;
11803
11804 encoder = to_intel_encoder(connector_state->best_encoder);
11805
Daniel Vetterefea6e82013-07-21 21:36:59 +020011806 if (!(encoder->compute_config(encoder, pipe_config))) {
11807 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020011808 goto fail;
11809 }
11810 }
11811
Daniel Vetterff9a6752013-06-01 17:16:21 +020011812 /* Set default port clock if not overwritten by the encoder. Needs to be
11813 * done afterwards in case the encoder adjusts the mode. */
11814 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011815 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010011816 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020011817
Daniel Vettera43f6e02013-06-07 23:10:32 +020011818 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010011819 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020011820 DRM_DEBUG_KMS("CRTC fixup failed\n");
11821 goto fail;
11822 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010011823
11824 if (ret == RETRY) {
11825 if (WARN(!retry, "loop in pipe configuration computation\n")) {
11826 ret = -EINVAL;
11827 goto fail;
11828 }
11829
11830 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
11831 retry = false;
11832 goto encoder_retry;
11833 }
11834
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011835 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011836 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011837 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011838
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011839 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020011840fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030011841 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020011842}
11843
Daniel Vetterea9d7582012-07-10 10:42:52 +020011844static bool intel_crtc_in_use(struct drm_crtc *crtc)
11845{
11846 struct drm_encoder *encoder;
11847 struct drm_device *dev = crtc->dev;
11848
11849 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
11850 if (encoder->crtc == crtc)
11851 return true;
11852
11853 return false;
11854}
11855
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011856static bool
11857needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020011858{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011859 return state->mode_changed || state->active_changed;
11860}
11861
11862static void
11863intel_modeset_update_state(struct drm_atomic_state *state)
11864{
11865 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011866 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011867 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011868 struct drm_crtc *crtc;
11869 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011870 struct drm_connector *connector;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011871 int i;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011872
Daniel Vetterba41c0de2014-11-03 15:04:55 +010011873 intel_shared_dpll_commit(dev_priv);
11874
Damien Lespiaub2784e12014-08-05 11:29:37 +010011875 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020011876 if (!intel_encoder->base.crtc)
11877 continue;
11878
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011879 for_each_crtc_in_state(state, crtc, crtc_state, i)
11880 if (crtc == intel_encoder->base.crtc)
11881 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011882
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011883 if (crtc != intel_encoder->base.crtc)
11884 continue;
11885
11886 if (crtc_state->enable && needs_modeset(crtc_state))
Daniel Vetterea9d7582012-07-10 10:42:52 +020011887 intel_encoder->connectors_active = false;
11888 }
11889
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011890 drm_atomic_helper_swap_state(state->dev, state);
11891 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011892
Ville Syrjälä76688512014-01-10 11:28:06 +020011893 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011894 for_each_crtc(dev, crtc) {
11895 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020011896 }
11897
11898 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
11899 if (!connector->encoder || !connector->encoder->crtc)
11900 continue;
11901
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011902 for_each_crtc_in_state(state, crtc, crtc_state, i)
11903 if (crtc == connector->encoder->crtc)
11904 break;
Daniel Vetterea9d7582012-07-10 10:42:52 +020011905
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030011906 if (crtc != connector->encoder->crtc)
11907 continue;
11908
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011909 if (crtc->state->enable && needs_modeset(crtc->state)) {
Daniel Vetter68d34722012-09-06 22:08:35 +020011910 struct drm_property *dpms_property =
11911 dev->mode_config.dpms_property;
11912
Daniel Vetterea9d7582012-07-10 10:42:52 +020011913 connector->dpms = DRM_MODE_DPMS_ON;
Rob Clark662595d2012-10-11 20:36:04 -050011914 drm_object_property_set_value(&connector->base,
Daniel Vetter68d34722012-09-06 22:08:35 +020011915 dpms_property,
11916 DRM_MODE_DPMS_ON);
Daniel Vetterea9d7582012-07-10 10:42:52 +020011917
11918 intel_encoder = to_intel_encoder(connector->encoder);
11919 intel_encoder->connectors_active = true;
11920 }
11921 }
11922
11923}
11924
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011925static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011926{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030011927 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030011928
11929 if (clock1 == clock2)
11930 return true;
11931
11932 if (!clock1 || !clock2)
11933 return false;
11934
11935 diff = abs(clock1 - clock2);
11936
11937 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
11938 return true;
11939
11940 return false;
11941}
11942
Daniel Vetter25c5b262012-07-08 22:08:04 +020011943#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
11944 list_for_each_entry((intel_crtc), \
11945 &(dev)->mode_config.crtc_list, \
11946 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020011947 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020011948
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011949static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020011950intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011951 struct intel_crtc_state *current_config,
11952 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010011953{
Daniel Vetter66e985c2013-06-05 13:34:20 +020011954#define PIPE_CONF_CHECK_X(name) \
11955 if (current_config->name != pipe_config->name) { \
11956 DRM_ERROR("mismatch in " #name " " \
11957 "(expected 0x%08x, found 0x%08x)\n", \
11958 current_config->name, \
11959 pipe_config->name); \
11960 return false; \
11961 }
11962
Daniel Vetter08a24032013-04-19 11:25:34 +020011963#define PIPE_CONF_CHECK_I(name) \
11964 if (current_config->name != pipe_config->name) { \
11965 DRM_ERROR("mismatch in " #name " " \
11966 "(expected %i, found %i)\n", \
11967 current_config->name, \
11968 pipe_config->name); \
11969 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010011970 }
11971
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011972/* This is required for BDW+ where there is only one set of registers for
11973 * switching between high and low RR.
11974 * This macro can be used whenever a comparison has to be made between one
11975 * hw state and multiple sw state variables.
11976 */
11977#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
11978 if ((current_config->name != pipe_config->name) && \
11979 (current_config->alt_name != pipe_config->name)) { \
11980 DRM_ERROR("mismatch in " #name " " \
11981 "(expected %i or %i, found %i)\n", \
11982 current_config->name, \
11983 current_config->alt_name, \
11984 pipe_config->name); \
11985 return false; \
11986 }
11987
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011988#define PIPE_CONF_CHECK_FLAGS(name, mask) \
11989 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070011990 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020011991 "(expected %i, found %i)\n", \
11992 current_config->name & (mask), \
11993 pipe_config->name & (mask)); \
11994 return false; \
11995 }
11996
Ville Syrjälä5e550652013-09-06 23:29:07 +030011997#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
11998 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
11999 DRM_ERROR("mismatch in " #name " " \
12000 "(expected %i, found %i)\n", \
12001 current_config->name, \
12002 pipe_config->name); \
12003 return false; \
12004 }
12005
Daniel Vetterbb760062013-06-06 14:55:52 +020012006#define PIPE_CONF_QUIRK(quirk) \
12007 ((current_config->quirks | pipe_config->quirks) & (quirk))
12008
Daniel Vettereccb1402013-05-22 00:50:22 +020012009 PIPE_CONF_CHECK_I(cpu_transcoder);
12010
Daniel Vetter08a24032013-04-19 11:25:34 +020012011 PIPE_CONF_CHECK_I(has_pch_encoder);
12012 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012013 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12014 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12015 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12016 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12017 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012018
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012019 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012020
12021 if (INTEL_INFO(dev)->gen < 8) {
12022 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12023 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12024 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12025 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12026 PIPE_CONF_CHECK_I(dp_m_n.tu);
12027
12028 if (current_config->has_drrs) {
12029 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12030 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12031 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12032 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12033 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12034 }
12035 } else {
12036 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12037 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12038 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12039 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12040 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12041 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012042
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012043 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12044 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12045 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12046 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12047 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12048 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012049
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012050 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12051 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12052 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12053 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12054 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12055 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012056
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012057 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012058 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012059 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12060 IS_VALLEYVIEW(dev))
12061 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012062 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012063
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012064 PIPE_CONF_CHECK_I(has_audio);
12065
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012066 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012067 DRM_MODE_FLAG_INTERLACE);
12068
Daniel Vetterbb760062013-06-06 14:55:52 +020012069 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012070 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012071 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012072 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012073 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012074 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012075 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012076 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012077 DRM_MODE_FLAG_NVSYNC);
12078 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012079
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012080 PIPE_CONF_CHECK_I(pipe_src_w);
12081 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012082
Daniel Vetter99535992014-04-13 12:00:33 +020012083 /*
12084 * FIXME: BIOS likes to set up a cloned config with lvds+external
12085 * screen. Since we don't yet re-compute the pipe config when moving
12086 * just the lvds port away to another pipe the sw tracking won't match.
12087 *
12088 * Proper atomic modesets with recomputed global state will fix this.
12089 * Until then just don't check gmch state for inherited modes.
12090 */
12091 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12092 PIPE_CONF_CHECK_I(gmch_pfit.control);
12093 /* pfit ratios are autocomputed by the hw on gen4+ */
12094 if (INTEL_INFO(dev)->gen < 4)
12095 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12096 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12097 }
12098
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012099 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12100 if (current_config->pch_pfit.enabled) {
12101 PIPE_CONF_CHECK_I(pch_pfit.pos);
12102 PIPE_CONF_CHECK_I(pch_pfit.size);
12103 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012104
Chandra Kondurua1b22782015-04-07 15:28:45 -070012105 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12106
Jesse Barnese59150d2014-01-07 13:30:45 -080012107 /* BDW+ don't expose a synchronous way to read the state */
12108 if (IS_HASWELL(dev))
12109 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012110
Ville Syrjälä282740f2013-09-04 18:30:03 +030012111 PIPE_CONF_CHECK_I(double_wide);
12112
Daniel Vetter26804af2014-06-25 22:01:55 +030012113 PIPE_CONF_CHECK_X(ddi_pll_sel);
12114
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012115 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012116 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012117 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012118 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12119 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012120 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012121 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12122 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12123 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012124
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012125 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12126 PIPE_CONF_CHECK_I(pipe_bpp);
12127
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012128 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012129 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012130
Daniel Vetter66e985c2013-06-05 13:34:20 +020012131#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012132#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012133#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012134#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012135#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012136#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012137
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012138 return true;
12139}
12140
Damien Lespiau08db6652014-11-04 17:06:52 +000012141static void check_wm_state(struct drm_device *dev)
12142{
12143 struct drm_i915_private *dev_priv = dev->dev_private;
12144 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12145 struct intel_crtc *intel_crtc;
12146 int plane;
12147
12148 if (INTEL_INFO(dev)->gen < 9)
12149 return;
12150
12151 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12152 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12153
12154 for_each_intel_crtc(dev, intel_crtc) {
12155 struct skl_ddb_entry *hw_entry, *sw_entry;
12156 const enum pipe pipe = intel_crtc->pipe;
12157
12158 if (!intel_crtc->active)
12159 continue;
12160
12161 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012162 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012163 hw_entry = &hw_ddb.plane[pipe][plane];
12164 sw_entry = &sw_ddb->plane[pipe][plane];
12165
12166 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12167 continue;
12168
12169 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12170 "(expected (%u,%u), found (%u,%u))\n",
12171 pipe_name(pipe), plane + 1,
12172 sw_entry->start, sw_entry->end,
12173 hw_entry->start, hw_entry->end);
12174 }
12175
12176 /* cursor */
12177 hw_entry = &hw_ddb.cursor[pipe];
12178 sw_entry = &sw_ddb->cursor[pipe];
12179
12180 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12181 continue;
12182
12183 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12184 "(expected (%u,%u), found (%u,%u))\n",
12185 pipe_name(pipe),
12186 sw_entry->start, sw_entry->end,
12187 hw_entry->start, hw_entry->end);
12188 }
12189}
12190
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012191static void
12192check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012193{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012194 struct intel_connector *connector;
12195
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012196 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012197 /* This also checks the encoder/connector hw state with the
12198 * ->get_hw_state callbacks. */
12199 intel_connector_check_state(connector);
12200
Rob Clarke2c719b2014-12-15 13:56:32 -050012201 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012202 "connector's staged encoder doesn't match current encoder\n");
12203 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012204}
12205
12206static void
12207check_encoder_state(struct drm_device *dev)
12208{
12209 struct intel_encoder *encoder;
12210 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012211
Damien Lespiaub2784e12014-08-05 11:29:37 +010012212 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012213 bool enabled = false;
12214 bool active = false;
12215 enum pipe pipe, tracked_pipe;
12216
12217 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12218 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012219 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012220
Rob Clarke2c719b2014-12-15 13:56:32 -050012221 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012222 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012223 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012224 "encoder's active_connectors set, but no crtc\n");
12225
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012226 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012227 if (connector->base.encoder != &encoder->base)
12228 continue;
12229 enabled = true;
12230 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12231 active = true;
12232 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012233 /*
12234 * for MST connectors if we unplug the connector is gone
12235 * away but the encoder is still connected to a crtc
12236 * until a modeset happens in response to the hotplug.
12237 */
12238 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12239 continue;
12240
Rob Clarke2c719b2014-12-15 13:56:32 -050012241 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012242 "encoder's enabled state mismatch "
12243 "(expected %i, found %i)\n",
12244 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012245 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012246 "active encoder with no crtc\n");
12247
Rob Clarke2c719b2014-12-15 13:56:32 -050012248 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012249 "encoder's computed active state doesn't match tracked active state "
12250 "(expected %i, found %i)\n", active, encoder->connectors_active);
12251
12252 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012253 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012254 "encoder's hw state doesn't match sw tracking "
12255 "(expected %i, found %i)\n",
12256 encoder->connectors_active, active);
12257
12258 if (!encoder->base.crtc)
12259 continue;
12260
12261 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012262 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012263 "active encoder's pipe doesn't match"
12264 "(expected %i, found %i)\n",
12265 tracked_pipe, pipe);
12266
12267 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012268}
12269
12270static void
12271check_crtc_state(struct drm_device *dev)
12272{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012273 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012274 struct intel_crtc *crtc;
12275 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012276 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012277
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012278 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012279 bool enabled = false;
12280 bool active = false;
12281
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012282 memset(&pipe_config, 0, sizeof(pipe_config));
12283
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012284 DRM_DEBUG_KMS("[CRTC:%d]\n",
12285 crtc->base.base.id);
12286
Matt Roper83d65732015-02-25 13:12:16 -080012287 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012288 "active crtc, but not enabled in sw tracking\n");
12289
Damien Lespiaub2784e12014-08-05 11:29:37 +010012290 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012291 if (encoder->base.crtc != &crtc->base)
12292 continue;
12293 enabled = true;
12294 if (encoder->connectors_active)
12295 active = true;
12296 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012297
Rob Clarke2c719b2014-12-15 13:56:32 -050012298 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012299 "crtc's computed active state doesn't match tracked active state "
12300 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012301 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012302 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012303 "(expected %i, found %i)\n", enabled,
12304 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012305
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012306 active = dev_priv->display.get_pipe_config(crtc,
12307 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012308
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012309 /* hw state is inconsistent with the pipe quirk */
12310 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12311 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012312 active = crtc->active;
12313
Damien Lespiaub2784e12014-08-05 11:29:37 +010012314 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012315 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012316 if (encoder->base.crtc != &crtc->base)
12317 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012318 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012319 encoder->get_config(encoder, &pipe_config);
12320 }
12321
Rob Clarke2c719b2014-12-15 13:56:32 -050012322 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012323 "crtc active state doesn't match with hw state "
12324 "(expected %i, found %i)\n", crtc->active, active);
12325
Daniel Vetterc0b03412013-05-28 12:05:54 +020012326 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012327 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012328 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012329 intel_dump_pipe_config(crtc, &pipe_config,
12330 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012331 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012332 "[sw state]");
12333 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012334 }
12335}
12336
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012337static void
12338check_shared_dpll_state(struct drm_device *dev)
12339{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012340 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012341 struct intel_crtc *crtc;
12342 struct intel_dpll_hw_state dpll_hw_state;
12343 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012344
12345 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12346 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12347 int enabled_crtcs = 0, active_crtcs = 0;
12348 bool active;
12349
12350 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12351
12352 DRM_DEBUG_KMS("%s\n", pll->name);
12353
12354 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12355
Rob Clarke2c719b2014-12-15 13:56:32 -050012356 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012357 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012358 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012359 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012360 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012361 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012362 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012363 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012364 "pll on state mismatch (expected %i, found %i)\n",
12365 pll->on, active);
12366
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012367 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012368 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012369 enabled_crtcs++;
12370 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12371 active_crtcs++;
12372 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012373 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012374 "pll active crtcs mismatch (expected %i, found %i)\n",
12375 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012376 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012377 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012378 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012379
Rob Clarke2c719b2014-12-15 13:56:32 -050012380 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012381 sizeof(dpll_hw_state)),
12382 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012383 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012384}
12385
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012386void
12387intel_modeset_check_state(struct drm_device *dev)
12388{
Damien Lespiau08db6652014-11-04 17:06:52 +000012389 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012390 check_connector_state(dev);
12391 check_encoder_state(dev);
12392 check_crtc_state(dev);
12393 check_shared_dpll_state(dev);
12394}
12395
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012396void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012397 int dotclock)
12398{
12399 /*
12400 * FDI already provided one idea for the dotclock.
12401 * Yell if the encoder disagrees.
12402 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012403 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012404 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012405 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012406}
12407
Ville Syrjälä80715b22014-05-15 20:23:23 +030012408static void update_scanline_offset(struct intel_crtc *crtc)
12409{
12410 struct drm_device *dev = crtc->base.dev;
12411
12412 /*
12413 * The scanline counter increments at the leading edge of hsync.
12414 *
12415 * On most platforms it starts counting from vtotal-1 on the
12416 * first active line. That means the scanline counter value is
12417 * always one less than what we would expect. Ie. just after
12418 * start of vblank, which also occurs at start of hsync (on the
12419 * last active line), the scanline counter will read vblank_start-1.
12420 *
12421 * On gen2 the scanline counter starts counting from 1 instead
12422 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12423 * to keep the value positive), instead of adding one.
12424 *
12425 * On HSW+ the behaviour of the scanline counter depends on the output
12426 * type. For DP ports it behaves like most other platforms, but on HDMI
12427 * there's an extra 1 line difference. So we need to add two instead of
12428 * one to the value.
12429 */
12430 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012431 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012432 int vtotal;
12433
12434 vtotal = mode->crtc_vtotal;
12435 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12436 vtotal /= 2;
12437
12438 crtc->scanline_offset = vtotal - 1;
12439 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012440 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012441 crtc->scanline_offset = 2;
12442 } else
12443 crtc->scanline_offset = 1;
12444}
12445
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012446static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012447intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012448 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012449{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012450 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012451 int ret = 0;
12452
12453 ret = drm_atomic_add_affected_connectors(state, crtc);
12454 if (ret)
12455 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012456
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012457 ret = drm_atomic_helper_check_modeset(state->dev, state);
12458 if (ret)
12459 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012460
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012461 /*
12462 * Note this needs changes when we start tracking multiple modes
12463 * and crtcs. At that point we'll need to compute the whole config
12464 * (i.e. one pipe_config for each crtc) rather than just the one
12465 * for this crtc.
12466 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012467 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12468 if (IS_ERR(pipe_config))
12469 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012470
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012471 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012472 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012473
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012474 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012475 if (ret)
12476 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012477
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012478 /* Check things that can only be changed through modeset */
12479 if (pipe_config->has_audio !=
12480 to_intel_crtc(crtc)->config->has_audio)
12481 pipe_config->base.mode_changed = true;
12482
12483 /*
12484 * Note we have an issue here with infoframes: current code
12485 * only updates them on the full mode set path per hw
12486 * requirements. So here we should be checking for any
12487 * required changes and forcing a mode set.
12488 */
12489
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012490 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12491
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012492 ret = drm_atomic_helper_check_planes(state->dev, state);
12493 if (ret)
12494 return ERR_PTR(ret);
12495
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012496 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012497}
12498
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012499static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012500{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012501 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012502 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012503 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012504 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012505 struct intel_crtc_state *intel_crtc_state;
12506 struct drm_crtc *crtc;
12507 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012508 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012509 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012510
12511 if (!dev_priv->display.crtc_compute_clock)
12512 return 0;
12513
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012514 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12515 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012516 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012517
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012518 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012519 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012520 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012521 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012522 }
12523
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012524 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12525 if (ret)
12526 goto done;
12527
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012528 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12529 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012530 continue;
12531
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012532 intel_crtc = to_intel_crtc(crtc);
12533 intel_crtc_state = to_intel_crtc_state(crtc_state);
12534
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012535 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012536 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012537 if (ret) {
12538 intel_shared_dpll_abort_config(dev_priv);
12539 goto done;
12540 }
12541 }
12542
12543done:
12544 return ret;
12545}
12546
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012547/* Code that should eventually be part of atomic_check() */
12548static int __intel_set_mode_checks(struct drm_atomic_state *state)
12549{
12550 struct drm_device *dev = state->dev;
12551 int ret;
12552
12553 /*
12554 * See if the config requires any additional preparation, e.g.
12555 * to adjust global state with pipes off. We need to do this
12556 * here so we can get the modeset_pipe updated config for the new
12557 * mode set on this crtc. For other crtcs we need to use the
12558 * adjusted_mode bits in the crtc directly.
12559 */
12560 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
12561 ret = valleyview_modeset_global_pipes(state);
12562 if (ret)
12563 return ret;
12564 }
12565
12566 ret = __intel_set_mode_setup_plls(state);
12567 if (ret)
12568 return ret;
12569
12570 return 0;
12571}
12572
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012573static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012574 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012575{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012576 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012577 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012578 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012579 struct drm_crtc *crtc;
12580 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012581 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012582 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012583
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012584 ret = __intel_set_mode_checks(state);
12585 if (ret < 0)
12586 return ret;
12587
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012588 ret = drm_atomic_helper_prepare_planes(dev, state);
12589 if (ret)
12590 return ret;
12591
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012592 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12593 if (!needs_modeset(crtc_state))
12594 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012595
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012596 if (!crtc_state->enable) {
12597 intel_crtc_disable(crtc);
12598 } else if (crtc->state->enable) {
12599 intel_crtc_disable_planes(crtc);
12600 dev_priv->display.crtc_disable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030012601 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012602 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012603
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012604 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12605 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012606 *
12607 * Note we'll need to fix this up when we start tracking multiple
12608 * pipes; here we assume a single modeset_pipe and only track the
12609 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012610 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012611 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012612 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012613
12614 /*
12615 * Calculate and store various constants which
12616 * are later needed by vblank and swap-completion
12617 * timestamping. They are derived from true hwmode.
12618 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012619 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012620 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012621 }
Daniel Vetter7758a112012-07-08 19:40:39 +020012622
Daniel Vetterea9d7582012-07-10 10:42:52 +020012623 /* Only after disabling all output pipelines that will be changed can we
12624 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012625 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012626
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012627 /* The state has been swaped above, so state actually contains the
12628 * old state now. */
12629
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012630 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012631
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012632 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012633
12634 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012635 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012636 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012637 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012638
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012639 update_scanline_offset(to_intel_crtc(crtc));
12640
12641 dev_priv->display.crtc_enable(crtc);
12642 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012643 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012644
Daniel Vettera6778b32012-07-02 09:56:42 +020012645 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012646
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012647 drm_atomic_helper_cleanup_planes(dev, state);
12648
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012649 drm_atomic_state_free(state);
12650
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012651 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012652}
12653
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012654static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012655 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012656{
12657 int ret;
12658
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012659 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012660
12661 if (ret == 0)
12662 intel_modeset_check_state(crtc->dev);
12663
12664 return ret;
12665}
12666
Damien Lespiaue7457a92013-08-08 22:28:59 +010012667static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012668 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012669{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012670 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012671 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012672
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012673 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012674 if (IS_ERR(pipe_config)) {
12675 ret = PTR_ERR(pipe_config);
12676 goto out;
12677 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012678
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012679 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012680 if (ret)
12681 goto out;
12682
12683out:
12684 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020012685}
12686
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012687void intel_crtc_restore_mode(struct drm_crtc *crtc)
12688{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012689 struct drm_device *dev = crtc->dev;
12690 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012691 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012692 struct intel_encoder *encoder;
12693 struct intel_connector *connector;
12694 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012695 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012696 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012697
12698 state = drm_atomic_state_alloc(dev);
12699 if (!state) {
12700 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
12701 crtc->base.id);
12702 return;
12703 }
12704
12705 state->acquire_ctx = dev->mode_config.acquire_ctx;
12706
12707 /* The force restore path in the HW readout code relies on the staged
12708 * config still keeping the user requested config while the actual
12709 * state has been overwritten by the configuration read from HW. We
12710 * need to copy the staged config to the atomic state, otherwise the
12711 * mode set will just reapply the state the HW is already in. */
12712 for_each_intel_encoder(dev, encoder) {
12713 if (&encoder->new_crtc->base != crtc)
12714 continue;
12715
12716 for_each_intel_connector(dev, connector) {
12717 if (connector->new_encoder != encoder)
12718 continue;
12719
12720 connector_state = drm_atomic_get_connector_state(state, &connector->base);
12721 if (IS_ERR(connector_state)) {
12722 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
12723 connector->base.base.id,
12724 connector->base.name,
12725 PTR_ERR(connector_state));
12726 continue;
12727 }
12728
12729 connector_state->crtc = crtc;
12730 connector_state->best_encoder = &encoder->base;
12731 }
12732 }
12733
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012734 for_each_intel_crtc(dev, intel_crtc) {
12735 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
12736 continue;
12737
12738 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
12739 if (IS_ERR(crtc_state)) {
12740 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
12741 intel_crtc->base.base.id,
12742 PTR_ERR(crtc_state));
12743 continue;
12744 }
12745
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012746 crtc_state->base.active = crtc_state->base.enable =
12747 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012748
12749 if (&intel_crtc->base == crtc)
12750 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030012751 }
12752
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030012753 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
12754 crtc->primary->fb, crtc->x, crtc->y);
12755
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012756 ret = intel_set_mode(crtc, state);
12757 if (ret)
12758 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012759}
12760
Daniel Vetter25c5b262012-07-08 22:08:04 +020012761#undef for_each_intel_crtc_masked
12762
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012763static bool intel_connector_in_mode_set(struct intel_connector *connector,
12764 struct drm_mode_set *set)
12765{
12766 int ro;
12767
12768 for (ro = 0; ro < set->num_connectors; ro++)
12769 if (set->connectors[ro] == &connector->base)
12770 return true;
12771
12772 return false;
12773}
12774
Daniel Vetter2e431052012-07-04 22:42:15 +020012775static int
Daniel Vetter9a935852012-07-05 22:34:27 +020012776intel_modeset_stage_output_state(struct drm_device *dev,
12777 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012778 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020012779{
Daniel Vetter9a935852012-07-05 22:34:27 +020012780 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012781 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012782 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012783 struct drm_crtc *crtc;
12784 struct drm_crtc_state *crtc_state;
12785 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020012786
Damien Lespiau9abdda72013-02-13 13:29:23 +000012787 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020012788 * of connectors. For paranoia, double-check this. */
12789 WARN_ON(!set->fb && (set->num_connectors != 0));
12790 WARN_ON(set->fb && (set->num_connectors == 0));
12791
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012792 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012793 bool in_mode_set = intel_connector_in_mode_set(connector, set);
12794
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012795 if (!in_mode_set && connector->base.state->crtc != set->crtc)
12796 continue;
12797
12798 connector_state =
12799 drm_atomic_get_connector_state(state, &connector->base);
12800 if (IS_ERR(connector_state))
12801 return PTR_ERR(connector_state);
12802
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012803 if (in_mode_set) {
12804 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012805 connector_state->best_encoder =
12806 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020012807 }
12808
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012809 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012810 continue;
12811
Daniel Vetter9a935852012-07-05 22:34:27 +020012812 /* If we disable the crtc, disable all its connectors. Also, if
12813 * the connector is on the changing crtc but not on the new
12814 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030012815 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012816 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020012817
12818 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
12819 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012820 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020012821 }
Daniel Vetter9a935852012-07-05 22:34:27 +020012822 }
12823 /* connector->new_encoder is now updated for all connectors. */
12824
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012825 for_each_connector_in_state(state, drm_connector, connector_state, i) {
12826 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020012827
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012828 if (!connector_state->best_encoder) {
12829 ret = drm_atomic_set_crtc_for_connector(connector_state,
12830 NULL);
12831 if (ret)
12832 return ret;
12833
Daniel Vetter50f56112012-07-02 09:35:43 +020012834 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012835 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012836
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012837 if (intel_connector_in_mode_set(connector, set)) {
12838 struct drm_crtc *crtc = connector->base.state->crtc;
12839
12840 /* If this connector was in a previous crtc, add it
12841 * to the state. We might need to disable it. */
12842 if (crtc) {
12843 crtc_state =
12844 drm_atomic_get_crtc_state(state, crtc);
12845 if (IS_ERR(crtc_state))
12846 return PTR_ERR(crtc_state);
12847 }
12848
12849 ret = drm_atomic_set_crtc_for_connector(connector_state,
12850 set->crtc);
12851 if (ret)
12852 return ret;
12853 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012854
12855 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012856 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
12857 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020012858 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020012859 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020012860
Daniel Vetter9a935852012-07-05 22:34:27 +020012861 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
12862 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030012863 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012864 connector_state->crtc->base.id);
12865
12866 if (connector_state->best_encoder != &connector->encoder->base)
12867 connector->encoder =
12868 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020012869 }
12870
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012871 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012872 bool has_connectors;
12873
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030012874 ret = drm_atomic_add_affected_connectors(state, crtc);
12875 if (ret)
12876 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020012877
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020012878 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
12879 if (has_connectors != crtc_state->enable)
12880 crtc_state->enable =
12881 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020012882 }
12883
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012884 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
12885 set->fb, set->x, set->y);
12886 if (ret)
12887 return ret;
12888
12889 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
12890 if (IS_ERR(crtc_state))
12891 return PTR_ERR(crtc_state);
12892
12893 if (set->mode)
12894 drm_mode_copy(&crtc_state->mode, set->mode);
12895
12896 if (set->num_connectors)
12897 crtc_state->active = true;
12898
Daniel Vetter2e431052012-07-04 22:42:15 +020012899 return 0;
12900}
12901
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012902static bool primary_plane_visible(struct drm_crtc *crtc)
12903{
12904 struct intel_plane_state *plane_state =
12905 to_intel_plane_state(crtc->primary->state);
12906
12907 return plane_state->visible;
12908}
12909
Daniel Vetter2e431052012-07-04 22:42:15 +020012910static int intel_crtc_set_config(struct drm_mode_set *set)
12911{
12912 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012913 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012914 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012915 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020012916 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020012917
Daniel Vetter8d3e3752012-07-05 16:09:09 +020012918 BUG_ON(!set);
12919 BUG_ON(!set->crtc);
12920 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020012921
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010012922 /* Enforce sane interface api - has been abused by the fb helper. */
12923 BUG_ON(!set->mode && set->fb);
12924 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020012925
Daniel Vetter2e431052012-07-04 22:42:15 +020012926 if (set->fb) {
12927 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
12928 set->crtc->base.id, set->fb->base.id,
12929 (int)set->num_connectors, set->x, set->y);
12930 } else {
12931 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020012932 }
12933
12934 dev = set->crtc->dev;
12935
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012936 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012937 if (!state)
12938 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012939
12940 state->acquire_ctx = dev->mode_config.acquire_ctx;
12941
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030012942 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020012943 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012944 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020012945
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012946 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080012947 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080012948 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012949 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080012950 }
Jesse Barnes50f52752014-11-07 13:11:00 -080012951
Jesse Barnes1f9954d2014-11-05 14:26:10 -080012952 intel_update_pipe_size(to_intel_crtc(set->crtc));
12953
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012954 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012955
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012956 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012957
12958 if (ret == 0 &&
12959 pipe_config->base.enable &&
12960 pipe_config->base.planes_changed &&
12961 !needs_modeset(&pipe_config->base)) {
12962 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012963
12964 /*
12965 * We need to make sure the primary plane is re-enabled if it
12966 * has previously been turned off.
12967 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030012968 if (ret == 0 && !primary_plane_was_visible &&
12969 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070012970 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030012971 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070012972 }
12973
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012974 /*
12975 * In the fastboot case this may be our only check of the
12976 * state after boot. It would be better to only do it on
12977 * the first update, but we don't have a nice way of doing that
12978 * (and really, set_config isn't used much for high freq page
12979 * flipping, so increasing its cost here shouldn't be a big
12980 * deal).
12981 */
Jani Nikulad330a952014-01-21 11:24:25 +020012982 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080012983 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020012984 }
12985
Chris Wilson2d05eae2013-05-03 17:36:25 +010012986 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020012987 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
12988 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010012989 }
Daniel Vetter50f56112012-07-02 09:35:43 +020012990
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030012991out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012992 if (ret)
12993 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020012994 return ret;
12995}
12996
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012997static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010012998 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020012999 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013000 .destroy = intel_crtc_destroy,
13001 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013002 .atomic_duplicate_state = intel_crtc_duplicate_state,
13003 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013004};
13005
Daniel Vetter53589012013-06-05 13:34:16 +020013006static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13007 struct intel_shared_dpll *pll,
13008 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013009{
Daniel Vetter53589012013-06-05 13:34:16 +020013010 uint32_t val;
13011
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013012 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013013 return false;
13014
Daniel Vetter53589012013-06-05 13:34:16 +020013015 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013016 hw_state->dpll = val;
13017 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13018 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013019
13020 return val & DPLL_VCO_ENABLE;
13021}
13022
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013023static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13024 struct intel_shared_dpll *pll)
13025{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013026 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13027 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013028}
13029
Daniel Vettere7b903d2013-06-05 13:34:14 +020013030static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13031 struct intel_shared_dpll *pll)
13032{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013033 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013034 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013035
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013036 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013037
13038 /* Wait for the clocks to stabilize. */
13039 POSTING_READ(PCH_DPLL(pll->id));
13040 udelay(150);
13041
13042 /* The pixel multiplier can only be updated once the
13043 * DPLL is enabled and the clocks are stable.
13044 *
13045 * So write it again.
13046 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013047 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013048 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013049 udelay(200);
13050}
13051
13052static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13053 struct intel_shared_dpll *pll)
13054{
13055 struct drm_device *dev = dev_priv->dev;
13056 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013057
13058 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013059 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013060 if (intel_crtc_to_shared_dpll(crtc) == pll)
13061 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13062 }
13063
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013064 I915_WRITE(PCH_DPLL(pll->id), 0);
13065 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013066 udelay(200);
13067}
13068
Daniel Vetter46edb022013-06-05 13:34:12 +020013069static char *ibx_pch_dpll_names[] = {
13070 "PCH DPLL A",
13071 "PCH DPLL B",
13072};
13073
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013074static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013075{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013076 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013077 int i;
13078
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013079 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013080
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013081 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013082 dev_priv->shared_dplls[i].id = i;
13083 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013084 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013085 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13086 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013087 dev_priv->shared_dplls[i].get_hw_state =
13088 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013089 }
13090}
13091
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013092static void intel_shared_dpll_init(struct drm_device *dev)
13093{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013094 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013095
Daniel Vetter9cd86932014-06-25 22:01:57 +030013096 if (HAS_DDI(dev))
13097 intel_ddi_pll_init(dev);
13098 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013099 ibx_pch_dpll_init(dev);
13100 else
13101 dev_priv->num_shared_dpll = 0;
13102
13103 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013104}
13105
Matt Roper6beb8c232014-12-01 15:40:14 -080013106/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013107 * intel_wm_need_update - Check whether watermarks need updating
13108 * @plane: drm plane
13109 * @state: new plane state
13110 *
13111 * Check current plane state versus the new one to determine whether
13112 * watermarks need to be recalculated.
13113 *
13114 * Returns true or false.
13115 */
13116bool intel_wm_need_update(struct drm_plane *plane,
13117 struct drm_plane_state *state)
13118{
13119 /* Update watermarks on tiling changes. */
13120 if (!plane->state->fb || !state->fb ||
13121 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13122 plane->state->rotation != state->rotation)
13123 return true;
13124
13125 return false;
13126}
13127
13128/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013129 * intel_prepare_plane_fb - Prepare fb for usage on plane
13130 * @plane: drm plane to prepare for
13131 * @fb: framebuffer to prepare for presentation
13132 *
13133 * Prepares a framebuffer for usage on a display plane. Generally this
13134 * involves pinning the underlying object and updating the frontbuffer tracking
13135 * bits. Some older platforms need special physical address handling for
13136 * cursor planes.
13137 *
13138 * Returns 0 on success, negative error code on failure.
13139 */
13140int
13141intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013142 struct drm_framebuffer *fb,
13143 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013144{
13145 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013146 struct intel_plane *intel_plane = to_intel_plane(plane);
13147 enum pipe pipe = intel_plane->pipe;
13148 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13149 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13150 unsigned frontbuffer_bits = 0;
13151 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013152
Matt Roperea2c67b2014-12-23 10:41:52 -080013153 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013154 return 0;
13155
Matt Roper6beb8c232014-12-01 15:40:14 -080013156 switch (plane->type) {
13157 case DRM_PLANE_TYPE_PRIMARY:
13158 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13159 break;
13160 case DRM_PLANE_TYPE_CURSOR:
13161 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13162 break;
13163 case DRM_PLANE_TYPE_OVERLAY:
13164 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13165 break;
13166 }
Matt Roper465c1202014-05-29 08:06:54 -070013167
Matt Roper4c345742014-07-09 16:22:10 -070013168 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013169
Matt Roper6beb8c232014-12-01 15:40:14 -080013170 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13171 INTEL_INFO(dev)->cursor_needs_physical) {
13172 int align = IS_I830(dev) ? 16 * 1024 : 256;
13173 ret = i915_gem_object_attach_phys(obj, align);
13174 if (ret)
13175 DRM_DEBUG_KMS("failed to attach phys object\n");
13176 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013177 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013178 }
13179
13180 if (ret == 0)
13181 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13182
13183 mutex_unlock(&dev->struct_mutex);
13184
13185 return ret;
13186}
13187
Matt Roper38f3ce32014-12-02 07:45:25 -080013188/**
13189 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13190 * @plane: drm plane to clean up for
13191 * @fb: old framebuffer that was on plane
13192 *
13193 * Cleans up a framebuffer that has just been removed from a plane.
13194 */
13195void
13196intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013197 struct drm_framebuffer *fb,
13198 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013199{
13200 struct drm_device *dev = plane->dev;
13201 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13202
13203 if (WARN_ON(!obj))
13204 return;
13205
13206 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13207 !INTEL_INFO(dev)->cursor_needs_physical) {
13208 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013209 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013210 mutex_unlock(&dev->struct_mutex);
13211 }
Matt Roper465c1202014-05-29 08:06:54 -070013212}
13213
Chandra Konduru6156a452015-04-27 13:48:39 -070013214int
13215skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13216{
13217 int max_scale;
13218 struct drm_device *dev;
13219 struct drm_i915_private *dev_priv;
13220 int crtc_clock, cdclk;
13221
13222 if (!intel_crtc || !crtc_state)
13223 return DRM_PLANE_HELPER_NO_SCALING;
13224
13225 dev = intel_crtc->base.dev;
13226 dev_priv = dev->dev_private;
13227 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13228 cdclk = dev_priv->display.get_display_clock_speed(dev);
13229
13230 if (!crtc_clock || !cdclk)
13231 return DRM_PLANE_HELPER_NO_SCALING;
13232
13233 /*
13234 * skl max scale is lower of:
13235 * close to 3 but not 3, -1 is for that purpose
13236 * or
13237 * cdclk/crtc_clock
13238 */
13239 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13240
13241 return max_scale;
13242}
13243
Matt Roper465c1202014-05-29 08:06:54 -070013244static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013245intel_check_primary_plane(struct drm_plane *plane,
13246 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013247{
Matt Roper32b7eee2014-12-24 07:59:06 -080013248 struct drm_device *dev = plane->dev;
13249 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013250 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013251 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013252 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013253 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013254 struct drm_rect *dest = &state->dst;
13255 struct drm_rect *src = &state->src;
13256 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013257 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013258 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13259 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013260 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013261
Matt Roperea2c67b2014-12-23 10:41:52 -080013262 crtc = crtc ? crtc : plane->crtc;
13263 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013264 crtc_state = state->base.state ?
13265 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013266
Chandra Konduru6156a452015-04-27 13:48:39 -070013267 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013268 /* use scaler when colorkey is not required */
13269 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13270 min_scale = 1;
13271 max_scale = skl_max_scale(intel_crtc, crtc_state);
13272 }
Sonika Jindald8106362015-04-10 14:37:28 +053013273 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013274 }
Sonika Jindald8106362015-04-10 14:37:28 +053013275
Matt Roperc59cb172014-12-01 15:40:16 -080013276 ret = drm_plane_helper_check_update(plane, crtc, fb,
13277 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013278 min_scale,
13279 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013280 can_position, true,
13281 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013282 if (ret)
13283 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013284
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013285 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013286 struct intel_plane_state *old_state =
13287 to_intel_plane_state(plane->state);
13288
Matt Roper32b7eee2014-12-24 07:59:06 -080013289 intel_crtc->atomic.wait_for_flips = true;
13290
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013291 /*
13292 * FBC does not work on some platforms for rotated
13293 * planes, so disable it when rotation is not 0 and
13294 * update it when rotation is set back to 0.
13295 *
13296 * FIXME: This is redundant with the fbc update done in
13297 * the primary plane enable function except that that
13298 * one is done too late. We eventually need to unify
13299 * this.
13300 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013301 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013302 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013303 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013304 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013305 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013306 }
13307
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013308 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013309 /*
13310 * BDW signals flip done immediately if the plane
13311 * is disabled, even if the plane enable is already
13312 * armed to occur at the next vblank :(
13313 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013314 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013315 intel_crtc->atomic.wait_vblank = true;
13316 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013317
Matt Roper32b7eee2014-12-24 07:59:06 -080013318 intel_crtc->atomic.fb_bits |=
13319 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13320
13321 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013322
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013323 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013324 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013325 }
13326
Chandra Konduru6156a452015-04-27 13:48:39 -070013327 if (INTEL_INFO(dev)->gen >= 9) {
13328 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13329 to_intel_plane(plane), state, 0);
13330 if (ret)
13331 return ret;
13332 }
13333
Matt Roperc59cb172014-12-01 15:40:16 -080013334 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013335}
13336
Sonika Jindal48404c12014-08-22 14:06:04 +053013337static void
13338intel_commit_primary_plane(struct drm_plane *plane,
13339 struct intel_plane_state *state)
13340{
Matt Roper2b875c22014-12-01 15:40:13 -080013341 struct drm_crtc *crtc = state->base.crtc;
13342 struct drm_framebuffer *fb = state->base.fb;
13343 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013344 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013345 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013346 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013347
Matt Roperea2c67b2014-12-23 10:41:52 -080013348 crtc = crtc ? crtc : plane->crtc;
13349 intel_crtc = to_intel_crtc(crtc);
13350
Matt Ropercf4c7c12014-12-04 10:27:42 -080013351 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013352 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013353 crtc->y = src->y1 >> 16;
13354
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013355 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013356 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013357 /* FIXME: kill this fastboot hack */
13358 intel_update_pipe_size(intel_crtc);
13359
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013360 dev_priv->display.update_primary_plane(crtc, plane->fb,
13361 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013362 }
13363}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013364
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013365static void
13366intel_disable_primary_plane(struct drm_plane *plane,
13367 struct drm_crtc *crtc,
13368 bool force)
13369{
13370 struct drm_device *dev = plane->dev;
13371 struct drm_i915_private *dev_priv = dev->dev_private;
13372
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013373 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13374}
13375
Matt Roper32b7eee2014-12-24 07:59:06 -080013376static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13377{
13378 struct drm_device *dev = crtc->dev;
13379 struct drm_i915_private *dev_priv = dev->dev_private;
13380 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013381 struct intel_plane *intel_plane;
13382 struct drm_plane *p;
13383 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013384
Matt Roperea2c67b2014-12-23 10:41:52 -080013385 /* Track fb's for any planes being disabled */
13386 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13387 intel_plane = to_intel_plane(p);
13388
13389 if (intel_crtc->atomic.disabled_planes &
13390 (1 << drm_plane_index(p))) {
13391 switch (p->type) {
13392 case DRM_PLANE_TYPE_PRIMARY:
13393 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13394 break;
13395 case DRM_PLANE_TYPE_CURSOR:
13396 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13397 break;
13398 case DRM_PLANE_TYPE_OVERLAY:
13399 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13400 break;
13401 }
13402
13403 mutex_lock(&dev->struct_mutex);
13404 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13405 mutex_unlock(&dev->struct_mutex);
13406 }
13407 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013408
Matt Roper32b7eee2014-12-24 07:59:06 -080013409 if (intel_crtc->atomic.wait_for_flips)
13410 intel_crtc_wait_for_pending_flips(crtc);
13411
13412 if (intel_crtc->atomic.disable_fbc)
13413 intel_fbc_disable(dev);
13414
13415 if (intel_crtc->atomic.pre_disable_primary)
13416 intel_pre_disable_primary(crtc);
13417
13418 if (intel_crtc->atomic.update_wm)
13419 intel_update_watermarks(crtc);
13420
13421 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013422
13423 /* Perform vblank evasion around commit operation */
13424 if (intel_crtc->active)
13425 intel_crtc->atomic.evade =
13426 intel_pipe_update_start(intel_crtc,
13427 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013428}
13429
13430static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13431{
13432 struct drm_device *dev = crtc->dev;
13433 struct drm_i915_private *dev_priv = dev->dev_private;
13434 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13435 struct drm_plane *p;
13436
Matt Roperc34c9ee2014-12-23 10:41:50 -080013437 if (intel_crtc->atomic.evade)
13438 intel_pipe_update_end(intel_crtc,
13439 intel_crtc->atomic.start_vbl_count);
13440
Matt Roper32b7eee2014-12-24 07:59:06 -080013441 intel_runtime_pm_put(dev_priv);
13442
13443 if (intel_crtc->atomic.wait_vblank)
13444 intel_wait_for_vblank(dev, intel_crtc->pipe);
13445
13446 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13447
13448 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013449 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013450 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013451 mutex_unlock(&dev->struct_mutex);
13452 }
Matt Roper465c1202014-05-29 08:06:54 -070013453
Matt Roper32b7eee2014-12-24 07:59:06 -080013454 if (intel_crtc->atomic.post_enable_primary)
13455 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013456
Matt Roper32b7eee2014-12-24 07:59:06 -080013457 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13458 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13459 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13460 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013461
Matt Roper32b7eee2014-12-24 07:59:06 -080013462 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013463}
13464
Matt Ropercf4c7c12014-12-04 10:27:42 -080013465/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013466 * intel_plane_destroy - destroy a plane
13467 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013468 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013469 * Common destruction function for all types of planes (primary, cursor,
13470 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013471 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013472void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013473{
13474 struct intel_plane *intel_plane = to_intel_plane(plane);
13475 drm_plane_cleanup(plane);
13476 kfree(intel_plane);
13477}
13478
Matt Roper65a3fea2015-01-21 16:35:42 -080013479const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013480 .update_plane = drm_atomic_helper_update_plane,
13481 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013482 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013483 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013484 .atomic_get_property = intel_plane_atomic_get_property,
13485 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013486 .atomic_duplicate_state = intel_plane_duplicate_state,
13487 .atomic_destroy_state = intel_plane_destroy_state,
13488
Matt Roper465c1202014-05-29 08:06:54 -070013489};
13490
13491static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13492 int pipe)
13493{
13494 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013495 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013496 const uint32_t *intel_primary_formats;
13497 int num_formats;
13498
13499 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13500 if (primary == NULL)
13501 return NULL;
13502
Matt Roper8e7d6882015-01-21 16:35:41 -080013503 state = intel_create_plane_state(&primary->base);
13504 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013505 kfree(primary);
13506 return NULL;
13507 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013508 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013509
Matt Roper465c1202014-05-29 08:06:54 -070013510 primary->can_scale = false;
13511 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013512 if (INTEL_INFO(dev)->gen >= 9) {
13513 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013514 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013515 }
Matt Roper465c1202014-05-29 08:06:54 -070013516 primary->pipe = pipe;
13517 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013518 primary->check_plane = intel_check_primary_plane;
13519 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013520 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013521 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013522 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13523 primary->plane = !pipe;
13524
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013525 if (INTEL_INFO(dev)->gen >= 9) {
13526 intel_primary_formats = skl_primary_formats;
13527 num_formats = ARRAY_SIZE(skl_primary_formats);
13528 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013529 intel_primary_formats = i965_primary_formats;
13530 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013531 } else {
13532 intel_primary_formats = i8xx_primary_formats;
13533 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013534 }
13535
13536 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013537 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013538 intel_primary_formats, num_formats,
13539 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013540
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013541 if (INTEL_INFO(dev)->gen >= 4)
13542 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013543
Matt Roperea2c67b2014-12-23 10:41:52 -080013544 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13545
Matt Roper465c1202014-05-29 08:06:54 -070013546 return &primary->base;
13547}
13548
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013549void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13550{
13551 if (!dev->mode_config.rotation_property) {
13552 unsigned long flags = BIT(DRM_ROTATE_0) |
13553 BIT(DRM_ROTATE_180);
13554
13555 if (INTEL_INFO(dev)->gen >= 9)
13556 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13557
13558 dev->mode_config.rotation_property =
13559 drm_mode_create_rotation_property(dev, flags);
13560 }
13561 if (dev->mode_config.rotation_property)
13562 drm_object_attach_property(&plane->base.base,
13563 dev->mode_config.rotation_property,
13564 plane->base.state->rotation);
13565}
13566
Matt Roper3d7d6512014-06-10 08:28:13 -070013567static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013568intel_check_cursor_plane(struct drm_plane *plane,
13569 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013570{
Matt Roper2b875c22014-12-01 15:40:13 -080013571 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013572 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013573 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013574 struct drm_rect *dest = &state->dst;
13575 struct drm_rect *src = &state->src;
13576 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013577 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013578 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013579 unsigned stride;
13580 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013581
Matt Roperea2c67b2014-12-23 10:41:52 -080013582 crtc = crtc ? crtc : plane->crtc;
13583 intel_crtc = to_intel_crtc(crtc);
13584
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013585 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013586 src, dest, clip,
13587 DRM_PLANE_HELPER_NO_SCALING,
13588 DRM_PLANE_HELPER_NO_SCALING,
13589 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013590 if (ret)
13591 return ret;
13592
13593
13594 /* if we want to turn off the cursor ignore width and height */
13595 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013596 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013597
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013598 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013599 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13600 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13601 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013602 return -EINVAL;
13603 }
13604
Matt Roperea2c67b2014-12-23 10:41:52 -080013605 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13606 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013607 DRM_DEBUG_KMS("buffer is too small\n");
13608 return -ENOMEM;
13609 }
13610
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013611 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013612 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13613 ret = -EINVAL;
13614 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013615
Matt Roper32b7eee2014-12-24 07:59:06 -080013616finish:
13617 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013618 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013619 intel_crtc->atomic.update_wm = true;
13620
13621 intel_crtc->atomic.fb_bits |=
13622 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13623 }
13624
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013625 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013626}
13627
Matt Roperf4a2cf22014-12-01 15:40:12 -080013628static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013629intel_disable_cursor_plane(struct drm_plane *plane,
13630 struct drm_crtc *crtc,
13631 bool force)
13632{
13633 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13634
13635 if (!force) {
13636 plane->fb = NULL;
13637 intel_crtc->cursor_bo = NULL;
13638 intel_crtc->cursor_addr = 0;
13639 }
13640
13641 intel_crtc_update_cursor(crtc, false);
13642}
13643
13644static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013645intel_commit_cursor_plane(struct drm_plane *plane,
13646 struct intel_plane_state *state)
13647{
Matt Roper2b875c22014-12-01 15:40:13 -080013648 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013649 struct drm_device *dev = plane->dev;
13650 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013651 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013652 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013653
Matt Roperea2c67b2014-12-23 10:41:52 -080013654 crtc = crtc ? crtc : plane->crtc;
13655 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013656
Matt Roperea2c67b2014-12-23 10:41:52 -080013657 plane->fb = state->base.fb;
13658 crtc->cursor_x = state->base.crtc_x;
13659 crtc->cursor_y = state->base.crtc_y;
13660
Gustavo Padovana912f122014-12-01 15:40:10 -080013661 if (intel_crtc->cursor_bo == obj)
13662 goto update;
13663
Matt Roperf4a2cf22014-12-01 15:40:12 -080013664 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013665 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013666 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013667 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013668 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013669 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013670
Gustavo Padovana912f122014-12-01 15:40:10 -080013671 intel_crtc->cursor_addr = addr;
13672 intel_crtc->cursor_bo = obj;
13673update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013674
Matt Roper32b7eee2014-12-24 07:59:06 -080013675 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013676 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070013677}
Gustavo Padovan852e7872014-09-05 17:22:31 -030013678
Matt Roper3d7d6512014-06-10 08:28:13 -070013679static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
13680 int pipe)
13681{
13682 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080013683 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070013684
13685 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
13686 if (cursor == NULL)
13687 return NULL;
13688
Matt Roper8e7d6882015-01-21 16:35:41 -080013689 state = intel_create_plane_state(&cursor->base);
13690 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013691 kfree(cursor);
13692 return NULL;
13693 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013694 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013695
Matt Roper3d7d6512014-06-10 08:28:13 -070013696 cursor->can_scale = false;
13697 cursor->max_downscale = 1;
13698 cursor->pipe = pipe;
13699 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013700 cursor->check_plane = intel_check_cursor_plane;
13701 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013702 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070013703
13704 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013705 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070013706 intel_cursor_formats,
13707 ARRAY_SIZE(intel_cursor_formats),
13708 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013709
13710 if (INTEL_INFO(dev)->gen >= 4) {
13711 if (!dev->mode_config.rotation_property)
13712 dev->mode_config.rotation_property =
13713 drm_mode_create_rotation_property(dev,
13714 BIT(DRM_ROTATE_0) |
13715 BIT(DRM_ROTATE_180));
13716 if (dev->mode_config.rotation_property)
13717 drm_object_attach_property(&cursor->base.base,
13718 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080013719 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070013720 }
13721
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013722 if (INTEL_INFO(dev)->gen >=9)
13723 state->scaler_id = -1;
13724
Matt Roperea2c67b2014-12-23 10:41:52 -080013725 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
13726
Matt Roper3d7d6512014-06-10 08:28:13 -070013727 return &cursor->base;
13728}
13729
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013730static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
13731 struct intel_crtc_state *crtc_state)
13732{
13733 int i;
13734 struct intel_scaler *intel_scaler;
13735 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
13736
13737 for (i = 0; i < intel_crtc->num_scalers; i++) {
13738 intel_scaler = &scaler_state->scalers[i];
13739 intel_scaler->in_use = 0;
13740 intel_scaler->id = i;
13741
13742 intel_scaler->mode = PS_SCALER_MODE_DYN;
13743 }
13744
13745 scaler_state->scaler_id = -1;
13746}
13747
Hannes Ederb358d0a2008-12-18 21:18:47 +010013748static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080013749{
Jani Nikulafbee40d2014-03-31 14:27:18 +030013750 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080013751 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013752 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070013753 struct drm_plane *primary = NULL;
13754 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070013755 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080013756
Daniel Vetter955382f2013-09-19 14:05:45 +020013757 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080013758 if (intel_crtc == NULL)
13759 return;
13760
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013761 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
13762 if (!crtc_state)
13763 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030013764 intel_crtc->config = crtc_state;
13765 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080013766 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013767
Chandra Konduru549e2bf2015-04-07 15:28:38 -070013768 /* initialize shared scalers */
13769 if (INTEL_INFO(dev)->gen >= 9) {
13770 if (pipe == PIPE_C)
13771 intel_crtc->num_scalers = 1;
13772 else
13773 intel_crtc->num_scalers = SKL_NUM_SCALERS;
13774
13775 skl_init_scalers(dev, intel_crtc, crtc_state);
13776 }
13777
Matt Roper465c1202014-05-29 08:06:54 -070013778 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013779 if (!primary)
13780 goto fail;
13781
13782 cursor = intel_cursor_plane_create(dev, pipe);
13783 if (!cursor)
13784 goto fail;
13785
Matt Roper465c1202014-05-29 08:06:54 -070013786 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070013787 cursor, &intel_crtc_funcs);
13788 if (ret)
13789 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080013790
13791 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080013792 for (i = 0; i < 256; i++) {
13793 intel_crtc->lut_r[i] = i;
13794 intel_crtc->lut_g[i] = i;
13795 intel_crtc->lut_b[i] = i;
13796 }
13797
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013798 /*
13799 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020013800 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020013801 */
Jesse Barnes80824002009-09-10 15:28:06 -070013802 intel_crtc->pipe = pipe;
13803 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010013804 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080013805 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010013806 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070013807 }
13808
Chris Wilson4b0e3332014-05-30 16:35:26 +030013809 intel_crtc->cursor_base = ~0;
13810 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030013811 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030013812
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080013813 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
13814 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
13815 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
13816 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
13817
Jesse Barnes79e53942008-11-07 14:24:08 -080013818 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020013819
13820 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070013821 return;
13822
13823fail:
13824 if (primary)
13825 drm_plane_cleanup(primary);
13826 if (cursor)
13827 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020013828 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070013829 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080013830}
13831
Jesse Barnes752aa882013-10-31 18:55:49 +020013832enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
13833{
13834 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020013835 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020013836
Rob Clark51fd3712013-11-19 12:10:12 -050013837 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020013838
Ville Syrjäläd3babd32014-11-07 11:16:01 +020013839 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020013840 return INVALID_PIPE;
13841
13842 return to_intel_crtc(encoder->crtc)->pipe;
13843}
13844
Carl Worth08d7b3d2009-04-29 14:43:54 -070013845int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000013846 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070013847{
Carl Worth08d7b3d2009-04-29 14:43:54 -070013848 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040013849 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020013850 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013851
Rob Clark7707e652014-07-17 23:30:04 -040013852 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070013853
Rob Clark7707e652014-07-17 23:30:04 -040013854 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070013855 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030013856 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013857 }
13858
Rob Clark7707e652014-07-17 23:30:04 -040013859 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020013860 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013861
Daniel Vetterc05422d2009-08-11 16:05:30 +020013862 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070013863}
13864
Daniel Vetter66a92782012-07-12 20:08:18 +020013865static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080013866{
Daniel Vetter66a92782012-07-12 20:08:18 +020013867 struct drm_device *dev = encoder->base.dev;
13868 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080013869 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080013870 int entry = 0;
13871
Damien Lespiaub2784e12014-08-05 11:29:37 +010013872 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020013873 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020013874 index_mask |= (1 << entry);
13875
Jesse Barnes79e53942008-11-07 14:24:08 -080013876 entry++;
13877 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010013878
Jesse Barnes79e53942008-11-07 14:24:08 -080013879 return index_mask;
13880}
13881
Chris Wilson4d302442010-12-14 19:21:29 +000013882static bool has_edp_a(struct drm_device *dev)
13883{
13884 struct drm_i915_private *dev_priv = dev->dev_private;
13885
13886 if (!IS_MOBILE(dev))
13887 return false;
13888
13889 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
13890 return false;
13891
Damien Lespiaue3589902014-02-07 19:12:50 +000013892 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000013893 return false;
13894
13895 return true;
13896}
13897
Jesse Barnes84b4e042014-06-25 08:24:29 -070013898static bool intel_crt_present(struct drm_device *dev)
13899{
13900 struct drm_i915_private *dev_priv = dev->dev_private;
13901
Damien Lespiau884497e2013-12-03 13:56:23 +000013902 if (INTEL_INFO(dev)->gen >= 9)
13903 return false;
13904
Damien Lespiaucf404ce2014-10-01 20:04:15 +010013905 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070013906 return false;
13907
13908 if (IS_CHERRYVIEW(dev))
13909 return false;
13910
13911 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
13912 return false;
13913
13914 return true;
13915}
13916
Jesse Barnes79e53942008-11-07 14:24:08 -080013917static void intel_setup_outputs(struct drm_device *dev)
13918{
Eric Anholt725e30a2009-01-22 13:01:02 -080013919 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010013920 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013921 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080013922
Daniel Vetterc9093352013-06-06 22:22:47 +020013923 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080013924
Jesse Barnes84b4e042014-06-25 08:24:29 -070013925 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020013926 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013927
Vandana Kannanc776eb22014-08-19 12:05:01 +053013928 if (IS_BROXTON(dev)) {
13929 /*
13930 * FIXME: Broxton doesn't support port detection via the
13931 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
13932 * detect the ports.
13933 */
13934 intel_ddi_init(dev, PORT_A);
13935 intel_ddi_init(dev, PORT_B);
13936 intel_ddi_init(dev, PORT_C);
13937 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013938 int found;
13939
Jesse Barnesde31fac2015-03-06 15:53:32 -080013940 /*
13941 * Haswell uses DDI functions to detect digital outputs.
13942 * On SKL pre-D0 the strap isn't connected, so we assume
13943 * it's there.
13944 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013945 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080013946 /* WaIgnoreDDIAStrap: skl */
13947 if (found ||
13948 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030013949 intel_ddi_init(dev, PORT_A);
13950
13951 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
13952 * register */
13953 found = I915_READ(SFUSE_STRAP);
13954
13955 if (found & SFUSE_STRAP_DDIB_DETECTED)
13956 intel_ddi_init(dev, PORT_B);
13957 if (found & SFUSE_STRAP_DDIC_DETECTED)
13958 intel_ddi_init(dev, PORT_C);
13959 if (found & SFUSE_STRAP_DDID_DETECTED)
13960 intel_ddi_init(dev, PORT_D);
13961 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013962 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020013963 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020013964
13965 if (has_edp_a(dev))
13966 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040013967
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013968 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080013969 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010013970 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013971 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013972 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013973 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013974 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013975 }
13976
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013977 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013978 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013979
Paulo Zanonidc0fa712013-02-19 16:21:46 -030013980 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030013981 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080013982
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013983 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013984 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080013985
Daniel Vetter270b3042012-10-27 15:52:05 +020013986 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030013987 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070013988 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030013989 /*
13990 * The DP_DETECTED bit is the latched state of the DDC
13991 * SDA pin at boot. However since eDP doesn't require DDC
13992 * (no way to plug in a DP->HDMI dongle) the DDC pins for
13993 * eDP ports may have been muxed to an alternate function.
13994 * Thus we can't rely on the DP_DETECTED bit alone to detect
13995 * eDP ports. Consult the VBT as well as DP_DETECTED to
13996 * detect eDP ports.
13997 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020013998 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
13999 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014000 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14001 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014002 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14003 intel_dp_is_edp(dev, PORT_B))
14004 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014005
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014006 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14007 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014008 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14009 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014010 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14011 intel_dp_is_edp(dev, PORT_C))
14012 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014013
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014014 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014015 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014016 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14017 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014018 /* eDP not supported on port D, so don't check VBT */
14019 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14020 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014021 }
14022
Jani Nikula3cfca972013-08-27 15:12:26 +030014023 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014024 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014025 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014026
Paulo Zanonie2debe92013-02-18 19:00:27 -030014027 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014028 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014029 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014030 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14031 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014032 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014033 }
Ma Ling27185ae2009-08-24 13:50:23 +080014034
Imre Deake7281ea2013-05-08 13:14:08 +030014035 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014036 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014037 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014038
14039 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014040
Paulo Zanonie2debe92013-02-18 19:00:27 -030014041 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014042 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014043 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014044 }
Ma Ling27185ae2009-08-24 13:50:23 +080014045
Paulo Zanonie2debe92013-02-18 19:00:27 -030014046 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014047
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014048 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14049 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014050 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014051 }
Imre Deake7281ea2013-05-08 13:14:08 +030014052 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014053 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014054 }
Ma Ling27185ae2009-08-24 13:50:23 +080014055
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014056 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014057 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014058 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014059 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014060 intel_dvo_init(dev);
14061
Zhenyu Wang103a1962009-11-27 11:44:36 +080014062 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014063 intel_tv_init(dev);
14064
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014065 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014066
Damien Lespiaub2784e12014-08-05 11:29:37 +010014067 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014068 encoder->base.possible_crtcs = encoder->crtc_mask;
14069 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014070 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014071 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014072
Paulo Zanonidde86e22012-12-01 12:04:25 -020014073 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014074
14075 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014076}
14077
14078static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14079{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014080 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014081 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014082
Daniel Vetteref2d6332014-02-10 18:00:38 +010014083 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014084 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014085 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014086 drm_gem_object_unreference(&intel_fb->obj->base);
14087 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014088 kfree(intel_fb);
14089}
14090
14091static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014092 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014093 unsigned int *handle)
14094{
14095 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014096 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014097
Chris Wilson05394f32010-11-08 19:18:58 +000014098 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014099}
14100
14101static const struct drm_framebuffer_funcs intel_fb_funcs = {
14102 .destroy = intel_user_framebuffer_destroy,
14103 .create_handle = intel_user_framebuffer_create_handle,
14104};
14105
Damien Lespiaub3218032015-02-27 11:15:18 +000014106static
14107u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14108 uint32_t pixel_format)
14109{
14110 u32 gen = INTEL_INFO(dev)->gen;
14111
14112 if (gen >= 9) {
14113 /* "The stride in bytes must not exceed the of the size of 8K
14114 * pixels and 32K bytes."
14115 */
14116 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14117 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14118 return 32*1024;
14119 } else if (gen >= 4) {
14120 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14121 return 16*1024;
14122 else
14123 return 32*1024;
14124 } else if (gen >= 3) {
14125 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14126 return 8*1024;
14127 else
14128 return 16*1024;
14129 } else {
14130 /* XXX DSPC is limited to 4k tiled */
14131 return 8*1024;
14132 }
14133}
14134
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014135static int intel_framebuffer_init(struct drm_device *dev,
14136 struct intel_framebuffer *intel_fb,
14137 struct drm_mode_fb_cmd2 *mode_cmd,
14138 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014139{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014140 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014141 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014142 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014143
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014144 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14145
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014146 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14147 /* Enforce that fb modifier and tiling mode match, but only for
14148 * X-tiled. This is needed for FBC. */
14149 if (!!(obj->tiling_mode == I915_TILING_X) !=
14150 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14151 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14152 return -EINVAL;
14153 }
14154 } else {
14155 if (obj->tiling_mode == I915_TILING_X)
14156 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14157 else if (obj->tiling_mode == I915_TILING_Y) {
14158 DRM_DEBUG("No Y tiling for legacy addfb\n");
14159 return -EINVAL;
14160 }
14161 }
14162
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014163 /* Passed in modifier sanity checking. */
14164 switch (mode_cmd->modifier[0]) {
14165 case I915_FORMAT_MOD_Y_TILED:
14166 case I915_FORMAT_MOD_Yf_TILED:
14167 if (INTEL_INFO(dev)->gen < 9) {
14168 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14169 mode_cmd->modifier[0]);
14170 return -EINVAL;
14171 }
14172 case DRM_FORMAT_MOD_NONE:
14173 case I915_FORMAT_MOD_X_TILED:
14174 break;
14175 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014176 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14177 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014178 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014179 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014180
Damien Lespiaub3218032015-02-27 11:15:18 +000014181 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14182 mode_cmd->pixel_format);
14183 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14184 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14185 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014186 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014187 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014188
Damien Lespiaub3218032015-02-27 11:15:18 +000014189 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14190 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014191 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014192 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14193 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014194 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014195 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014196 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014197 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014198
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014199 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014200 mode_cmd->pitches[0] != obj->stride) {
14201 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14202 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014203 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014204 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014205
Ville Syrjälä57779d02012-10-31 17:50:14 +020014206 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014207 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014208 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014209 case DRM_FORMAT_RGB565:
14210 case DRM_FORMAT_XRGB8888:
14211 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014212 break;
14213 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014214 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014215 DRM_DEBUG("unsupported pixel format: %s\n",
14216 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014217 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014218 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014219 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014220 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014221 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14222 DRM_DEBUG("unsupported pixel format: %s\n",
14223 drm_get_format_name(mode_cmd->pixel_format));
14224 return -EINVAL;
14225 }
14226 break;
14227 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014228 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014229 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014230 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014231 DRM_DEBUG("unsupported pixel format: %s\n",
14232 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014233 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014234 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014235 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014236 case DRM_FORMAT_ABGR2101010:
14237 if (!IS_VALLEYVIEW(dev)) {
14238 DRM_DEBUG("unsupported pixel format: %s\n",
14239 drm_get_format_name(mode_cmd->pixel_format));
14240 return -EINVAL;
14241 }
14242 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014243 case DRM_FORMAT_YUYV:
14244 case DRM_FORMAT_UYVY:
14245 case DRM_FORMAT_YVYU:
14246 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014247 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014248 DRM_DEBUG("unsupported pixel format: %s\n",
14249 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014250 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014251 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014252 break;
14253 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014254 DRM_DEBUG("unsupported pixel format: %s\n",
14255 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014256 return -EINVAL;
14257 }
14258
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014259 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14260 if (mode_cmd->offsets[0] != 0)
14261 return -EINVAL;
14262
Damien Lespiauec2c9812015-01-20 12:51:45 +000014263 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014264 mode_cmd->pixel_format,
14265 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014266 /* FIXME drm helper for size checks (especially planar formats)? */
14267 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14268 return -EINVAL;
14269
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014270 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14271 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014272 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014273
Jesse Barnes79e53942008-11-07 14:24:08 -080014274 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14275 if (ret) {
14276 DRM_ERROR("framebuffer init failed %d\n", ret);
14277 return ret;
14278 }
14279
Jesse Barnes79e53942008-11-07 14:24:08 -080014280 return 0;
14281}
14282
Jesse Barnes79e53942008-11-07 14:24:08 -080014283static struct drm_framebuffer *
14284intel_user_framebuffer_create(struct drm_device *dev,
14285 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014286 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014287{
Chris Wilson05394f32010-11-08 19:18:58 +000014288 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014289
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014290 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14291 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014292 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014293 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014294
Chris Wilsond2dff872011-04-19 08:36:26 +010014295 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014296}
14297
Daniel Vetter4520f532013-10-09 09:18:51 +020014298#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014299static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014300{
14301}
14302#endif
14303
Jesse Barnes79e53942008-11-07 14:24:08 -080014304static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014305 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014306 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014307 .atomic_check = intel_atomic_check,
14308 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014309};
14310
Jesse Barnese70236a2009-09-21 10:42:27 -070014311/* Set up chip specific display functions */
14312static void intel_init_display(struct drm_device *dev)
14313{
14314 struct drm_i915_private *dev_priv = dev->dev_private;
14315
Daniel Vetteree9300b2013-06-03 22:40:22 +020014316 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14317 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014318 else if (IS_CHERRYVIEW(dev))
14319 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014320 else if (IS_VALLEYVIEW(dev))
14321 dev_priv->display.find_dpll = vlv_find_best_dpll;
14322 else if (IS_PINEVIEW(dev))
14323 dev_priv->display.find_dpll = pnv_find_best_dpll;
14324 else
14325 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14326
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014327 if (INTEL_INFO(dev)->gen >= 9) {
14328 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014329 dev_priv->display.get_initial_plane_config =
14330 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014331 dev_priv->display.crtc_compute_clock =
14332 haswell_crtc_compute_clock;
14333 dev_priv->display.crtc_enable = haswell_crtc_enable;
14334 dev_priv->display.crtc_disable = haswell_crtc_disable;
14335 dev_priv->display.off = ironlake_crtc_off;
14336 dev_priv->display.update_primary_plane =
14337 skylake_update_primary_plane;
14338 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014339 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014340 dev_priv->display.get_initial_plane_config =
14341 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014342 dev_priv->display.crtc_compute_clock =
14343 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014344 dev_priv->display.crtc_enable = haswell_crtc_enable;
14345 dev_priv->display.crtc_disable = haswell_crtc_disable;
Daniel Vetterdf8ad702014-06-25 22:02:03 +030014346 dev_priv->display.off = ironlake_crtc_off;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014347 dev_priv->display.update_primary_plane =
14348 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014349 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014350 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014351 dev_priv->display.get_initial_plane_config =
14352 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014353 dev_priv->display.crtc_compute_clock =
14354 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014355 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14356 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014357 dev_priv->display.off = ironlake_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014358 dev_priv->display.update_primary_plane =
14359 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014360 } else if (IS_VALLEYVIEW(dev)) {
14361 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014362 dev_priv->display.get_initial_plane_config =
14363 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014364 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014365 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14366 dev_priv->display.crtc_disable = i9xx_crtc_disable;
14367 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014368 dev_priv->display.update_primary_plane =
14369 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014370 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014371 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014372 dev_priv->display.get_initial_plane_config =
14373 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014374 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014375 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14376 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014377 dev_priv->display.off = i9xx_crtc_off;
Matt Roper262ca2b2014-03-18 17:22:55 -070014378 dev_priv->display.update_primary_plane =
14379 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014380 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014381
Jesse Barnese70236a2009-09-21 10:42:27 -070014382 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014383 if (IS_SKYLAKE(dev))
14384 dev_priv->display.get_display_clock_speed =
14385 skylake_get_display_clock_speed;
14386 else if (IS_BROADWELL(dev))
14387 dev_priv->display.get_display_clock_speed =
14388 broadwell_get_display_clock_speed;
14389 else if (IS_HASWELL(dev))
14390 dev_priv->display.get_display_clock_speed =
14391 haswell_get_display_clock_speed;
14392 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014393 dev_priv->display.get_display_clock_speed =
14394 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014395 else if (IS_GEN5(dev))
14396 dev_priv->display.get_display_clock_speed =
14397 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014398 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
14399 IS_GEN6(dev) || IS_IVYBRIDGE(dev) || (IS_G33(dev) && !IS_PINEVIEW_M(dev)))
Jesse Barnese70236a2009-09-21 10:42:27 -070014400 dev_priv->display.get_display_clock_speed =
14401 i945_get_display_clock_speed;
14402 else if (IS_I915G(dev))
14403 dev_priv->display.get_display_clock_speed =
14404 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014405 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014406 dev_priv->display.get_display_clock_speed =
14407 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014408 else if (IS_PINEVIEW(dev))
14409 dev_priv->display.get_display_clock_speed =
14410 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014411 else if (IS_I915GM(dev))
14412 dev_priv->display.get_display_clock_speed =
14413 i915gm_get_display_clock_speed;
14414 else if (IS_I865G(dev))
14415 dev_priv->display.get_display_clock_speed =
14416 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014417 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014418 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014419 i85x_get_display_clock_speed;
14420 else /* 830 */
Jesse Barnese70236a2009-09-21 10:42:27 -070014421 dev_priv->display.get_display_clock_speed =
14422 i830_get_display_clock_speed;
14423
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014424 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014425 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014426 } else if (IS_GEN6(dev)) {
14427 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014428 } else if (IS_IVYBRIDGE(dev)) {
14429 /* FIXME: detect B0+ stepping and use auto training */
14430 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014431 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014432 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014433 } else if (IS_VALLEYVIEW(dev)) {
14434 dev_priv->display.modeset_global_resources =
14435 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014436 } else if (IS_BROXTON(dev)) {
14437 dev_priv->display.modeset_global_resources =
14438 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014439 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014440
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014441 switch (INTEL_INFO(dev)->gen) {
14442 case 2:
14443 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14444 break;
14445
14446 case 3:
14447 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14448 break;
14449
14450 case 4:
14451 case 5:
14452 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14453 break;
14454
14455 case 6:
14456 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14457 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014458 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014459 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014460 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14461 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014462 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014463 /* Drop through - unsupported since execlist only. */
14464 default:
14465 /* Default just returns -ENODEV to indicate unsupported */
14466 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014467 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014468
14469 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014470
14471 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014472}
14473
Jesse Barnesb690e962010-07-19 13:53:12 -070014474/*
14475 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14476 * resume, or other times. This quirk makes sure that's the case for
14477 * affected systems.
14478 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014479static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014480{
14481 struct drm_i915_private *dev_priv = dev->dev_private;
14482
14483 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014484 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014485}
14486
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014487static void quirk_pipeb_force(struct drm_device *dev)
14488{
14489 struct drm_i915_private *dev_priv = dev->dev_private;
14490
14491 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14492 DRM_INFO("applying pipe b force quirk\n");
14493}
14494
Keith Packard435793d2011-07-12 14:56:22 -070014495/*
14496 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14497 */
14498static void quirk_ssc_force_disable(struct drm_device *dev)
14499{
14500 struct drm_i915_private *dev_priv = dev->dev_private;
14501 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014502 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014503}
14504
Carsten Emde4dca20e2012-03-15 15:56:26 +010014505/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014506 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14507 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014508 */
14509static void quirk_invert_brightness(struct drm_device *dev)
14510{
14511 struct drm_i915_private *dev_priv = dev->dev_private;
14512 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014513 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014514}
14515
Scot Doyle9c72cc62014-07-03 23:27:50 +000014516/* Some VBT's incorrectly indicate no backlight is present */
14517static void quirk_backlight_present(struct drm_device *dev)
14518{
14519 struct drm_i915_private *dev_priv = dev->dev_private;
14520 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14521 DRM_INFO("applying backlight present quirk\n");
14522}
14523
Jesse Barnesb690e962010-07-19 13:53:12 -070014524struct intel_quirk {
14525 int device;
14526 int subsystem_vendor;
14527 int subsystem_device;
14528 void (*hook)(struct drm_device *dev);
14529};
14530
Egbert Eich5f85f172012-10-14 15:46:38 +020014531/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14532struct intel_dmi_quirk {
14533 void (*hook)(struct drm_device *dev);
14534 const struct dmi_system_id (*dmi_id_list)[];
14535};
14536
14537static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14538{
14539 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14540 return 1;
14541}
14542
14543static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14544 {
14545 .dmi_id_list = &(const struct dmi_system_id[]) {
14546 {
14547 .callback = intel_dmi_reverse_brightness,
14548 .ident = "NCR Corporation",
14549 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14550 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14551 },
14552 },
14553 { } /* terminating entry */
14554 },
14555 .hook = quirk_invert_brightness,
14556 },
14557};
14558
Ben Widawskyc43b5632012-04-16 14:07:40 -070014559static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014560 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14561 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14562
Jesse Barnesb690e962010-07-19 13:53:12 -070014563 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14564 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14565
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014566 /* 830 needs to leave pipe A & dpll A up */
14567 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14568
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014569 /* 830 needs to leave pipe B & dpll B up */
14570 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14571
Keith Packard435793d2011-07-12 14:56:22 -070014572 /* Lenovo U160 cannot use SSC on LVDS */
14573 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014574
14575 /* Sony Vaio Y cannot use SSC on LVDS */
14576 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014577
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014578 /* Acer Aspire 5734Z must invert backlight brightness */
14579 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14580
14581 /* Acer/eMachines G725 */
14582 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14583
14584 /* Acer/eMachines e725 */
14585 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14586
14587 /* Acer/Packard Bell NCL20 */
14588 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14589
14590 /* Acer Aspire 4736Z */
14591 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014592
14593 /* Acer Aspire 5336 */
14594 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014595
14596 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14597 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014598
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014599 /* Acer C720 Chromebook (Core i3 4005U) */
14600 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14601
jens steinb2a96012014-10-28 20:25:53 +010014602 /* Apple Macbook 2,1 (Core 2 T7400) */
14603 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14604
Scot Doyled4967d82014-07-03 23:27:52 +000014605 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14606 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014607
14608 /* HP Chromebook 14 (Celeron 2955U) */
14609 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014610
14611 /* Dell Chromebook 11 */
14612 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014613};
14614
14615static void intel_init_quirks(struct drm_device *dev)
14616{
14617 struct pci_dev *d = dev->pdev;
14618 int i;
14619
14620 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14621 struct intel_quirk *q = &intel_quirks[i];
14622
14623 if (d->device == q->device &&
14624 (d->subsystem_vendor == q->subsystem_vendor ||
14625 q->subsystem_vendor == PCI_ANY_ID) &&
14626 (d->subsystem_device == q->subsystem_device ||
14627 q->subsystem_device == PCI_ANY_ID))
14628 q->hook(dev);
14629 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014630 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14631 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14632 intel_dmi_quirks[i].hook(dev);
14633 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014634}
14635
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014636/* Disable the VGA plane that we never use */
14637static void i915_disable_vga(struct drm_device *dev)
14638{
14639 struct drm_i915_private *dev_priv = dev->dev_private;
14640 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014641 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014642
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014643 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014644 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014645 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014646 sr1 = inb(VGA_SR_DATA);
14647 outb(sr1 | 1<<5, VGA_SR_DATA);
14648 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14649 udelay(300);
14650
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014651 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014652 POSTING_READ(vga_reg);
14653}
14654
Daniel Vetterf8175862012-04-10 15:50:11 +020014655void intel_modeset_init_hw(struct drm_device *dev)
14656{
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014657 intel_prepare_ddi(dev);
14658
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +030014659 if (IS_VALLEYVIEW(dev))
14660 vlv_update_cdclk(dev);
14661
Daniel Vetterf8175862012-04-10 15:50:11 +020014662 intel_init_clock_gating(dev);
14663
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014664 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014665}
14666
Jesse Barnes79e53942008-11-07 14:24:08 -080014667void intel_modeset_init(struct drm_device *dev)
14668{
Jesse Barnes652c3932009-08-17 13:31:43 -070014669 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000014670 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014671 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080014672 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080014673
14674 drm_mode_config_init(dev);
14675
14676 dev->mode_config.min_width = 0;
14677 dev->mode_config.min_height = 0;
14678
Dave Airlie019d96c2011-09-29 16:20:42 +010014679 dev->mode_config.preferred_depth = 24;
14680 dev->mode_config.prefer_shadow = 1;
14681
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000014682 dev->mode_config.allow_fb_modifiers = true;
14683
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020014684 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080014685
Jesse Barnesb690e962010-07-19 13:53:12 -070014686 intel_init_quirks(dev);
14687
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030014688 intel_init_pm(dev);
14689
Ben Widawskye3c74752013-04-05 13:12:39 -070014690 if (INTEL_INFO(dev)->num_pipes == 0)
14691 return;
14692
Jesse Barnese70236a2009-09-21 10:42:27 -070014693 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014694 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070014695
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014696 if (IS_GEN2(dev)) {
14697 dev->mode_config.max_width = 2048;
14698 dev->mode_config.max_height = 2048;
14699 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070014700 dev->mode_config.max_width = 4096;
14701 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080014702 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010014703 dev->mode_config.max_width = 8192;
14704 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080014705 }
Damien Lespiau068be562014-03-28 14:17:49 +000014706
Ville Syrjälädc41c152014-08-13 11:57:05 +030014707 if (IS_845G(dev) || IS_I865G(dev)) {
14708 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
14709 dev->mode_config.cursor_height = 1023;
14710 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000014711 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
14712 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
14713 } else {
14714 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
14715 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
14716 }
14717
Ben Widawsky5d4545a2013-01-17 12:45:15 -080014718 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080014719
Zhao Yakui28c97732009-10-09 11:39:41 +080014720 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014721 INTEL_INFO(dev)->num_pipes,
14722 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080014723
Damien Lespiau055e3932014-08-18 13:49:10 +010014724 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000014725 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000014726 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000014727 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014728 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030014729 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000014730 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070014731 }
Jesse Barnes79e53942008-11-07 14:24:08 -080014732 }
14733
Jesse Barnesf42bb702013-12-16 16:34:23 -080014734 intel_init_dpio(dev);
14735
Daniel Vettere72f9fb2013-06-05 13:34:06 +020014736 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010014737
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014738 /* Just disable it once at startup */
14739 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014740 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000014741
14742 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020014743 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014744
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014745 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080014746 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014747 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014748
Damien Lespiaud3fcc802014-05-13 23:32:22 +010014749 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080014750 if (!crtc->active)
14751 continue;
14752
Jesse Barnes46f297f2014-03-07 08:57:48 -080014753 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080014754 * Note that reserving the BIOS fb up front prevents us
14755 * from stuffing other stolen allocations like the ring
14756 * on top. This prevents some ugliness at boot time, and
14757 * can even allow for smooth boot transitions if the BIOS
14758 * fb is large enough for the active pipe configuration.
14759 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014760 if (dev_priv->display.get_initial_plane_config) {
14761 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080014762 &crtc->plane_config);
14763 /*
14764 * If the fb is shared between multiple heads, we'll
14765 * just get the first one.
14766 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010014767 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080014768 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080014769 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010014770}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080014771
Daniel Vetter7fad7982012-07-04 17:51:47 +020014772static void intel_enable_pipe_a(struct drm_device *dev)
14773{
14774 struct intel_connector *connector;
14775 struct drm_connector *crt = NULL;
14776 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014777 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020014778
14779 /* We can't just switch on the pipe A, we need to set things up with a
14780 * proper mode and output configuration. As a gross hack, enable pipe A
14781 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014782 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020014783 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
14784 crt = &connector->base;
14785 break;
14786 }
14787 }
14788
14789 if (!crt)
14790 return;
14791
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030014792 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020014793 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020014794}
14795
Daniel Vetterfa555832012-10-10 23:14:00 +020014796static bool
14797intel_check_plane_mapping(struct intel_crtc *crtc)
14798{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014799 struct drm_device *dev = crtc->base.dev;
14800 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014801 u32 reg, val;
14802
Ben Widawsky7eb552a2013-03-13 14:05:41 -070014803 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020014804 return true;
14805
14806 reg = DSPCNTR(!crtc->plane);
14807 val = I915_READ(reg);
14808
14809 if ((val & DISPLAY_PLANE_ENABLE) &&
14810 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
14811 return false;
14812
14813 return true;
14814}
14815
Daniel Vetter24929352012-07-02 20:28:59 +020014816static void intel_sanitize_crtc(struct intel_crtc *crtc)
14817{
14818 struct drm_device *dev = crtc->base.dev;
14819 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020014820 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020014821
Daniel Vetter24929352012-07-02 20:28:59 +020014822 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020014823 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014824 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
14825
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014826 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010014827 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030014828 if (crtc->active) {
14829 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010014830 drm_crtc_vblank_on(&crtc->base);
14831 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030014832
Daniel Vetter24929352012-07-02 20:28:59 +020014833 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020014834 * disable the crtc (and hence change the state) if it is wrong. Note
14835 * that gen4+ has a fixed plane -> pipe mapping. */
14836 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020014837 struct intel_connector *connector;
14838 bool plane;
14839
Daniel Vetter24929352012-07-02 20:28:59 +020014840 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
14841 crtc->base.base.id);
14842
14843 /* Pipe has the wrong plane attached and the plane is active.
14844 * Temporarily change the plane mapping and disable everything
14845 * ... */
14846 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030014847 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020014848 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030014849 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020014850 dev_priv->display.crtc_disable(&crtc->base);
14851 crtc->plane = plane;
14852
14853 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014854 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014855 if (connector->encoder->base.crtc != &crtc->base)
14856 continue;
14857
Egbert Eich7f1950f2014-04-25 10:56:22 +020014858 connector->base.dpms = DRM_MODE_DPMS_OFF;
14859 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014860 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014861 /* multiple connectors may have the same encoder:
14862 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014863 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020014864 if (connector->encoder->base.crtc == &crtc->base) {
14865 connector->encoder->base.crtc = NULL;
14866 connector->encoder->connectors_active = false;
14867 }
Daniel Vetter24929352012-07-02 20:28:59 +020014868
14869 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080014870 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014871 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014872 crtc->base.enabled = false;
14873 }
Daniel Vetter24929352012-07-02 20:28:59 +020014874
Daniel Vetter7fad7982012-07-04 17:51:47 +020014875 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
14876 crtc->pipe == PIPE_A && !crtc->active) {
14877 /* BIOS forgot to enable pipe A, this mostly happens after
14878 * resume. Force-enable the pipe to fix this, the update_dpms
14879 * call below we restore the pipe to the right state, but leave
14880 * the required bits on. */
14881 intel_enable_pipe_a(dev);
14882 }
14883
Daniel Vetter24929352012-07-02 20:28:59 +020014884 /* Adjust the state of the output pipe according to whether we
14885 * have active connectors/encoders. */
14886 intel_crtc_update_dpms(&crtc->base);
14887
Matt Roper83d65732015-02-25 13:12:16 -080014888 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020014889 struct intel_encoder *encoder;
14890
14891 /* This can happen either due to bugs in the get_hw_state
14892 * functions or because the pipe is force-enabled due to the
14893 * pipe A quirk. */
14894 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
14895 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080014896 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020014897 crtc->active ? "enabled" : "disabled");
14898
Matt Roper83d65732015-02-25 13:12:16 -080014899 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020014900 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020014901 crtc->base.enabled = crtc->active;
14902
14903 /* Because we only establish the connector -> encoder ->
14904 * crtc links if something is active, this means the
14905 * crtc is now deactivated. Break the links. connector
14906 * -> encoder links are only establish when things are
14907 * actually up, hence no need to break them. */
14908 WARN_ON(crtc->active);
14909
14910 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
14911 WARN_ON(encoder->connectors_active);
14912 encoder->base.crtc = NULL;
14913 }
14914 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014915
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030014916 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010014917 /*
14918 * We start out with underrun reporting disabled to avoid races.
14919 * For correct bookkeeping mark this on active crtcs.
14920 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020014921 * Also on gmch platforms we dont have any hardware bits to
14922 * disable the underrun reporting. Which means we need to start
14923 * out with underrun reporting disabled also on inactive pipes,
14924 * since otherwise we'll complain about the garbage we read when
14925 * e.g. coming up after runtime pm.
14926 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010014927 * No protection against concurrent access is required - at
14928 * worst a fifo underrun happens which also sets this to false.
14929 */
14930 crtc->cpu_fifo_underrun_disabled = true;
14931 crtc->pch_fifo_underrun_disabled = true;
14932 }
Daniel Vetter24929352012-07-02 20:28:59 +020014933}
14934
14935static void intel_sanitize_encoder(struct intel_encoder *encoder)
14936{
14937 struct intel_connector *connector;
14938 struct drm_device *dev = encoder->base.dev;
14939
14940 /* We need to check both for a crtc link (meaning that the
14941 * encoder is active and trying to read from a pipe) and the
14942 * pipe itself being active. */
14943 bool has_active_crtc = encoder->base.crtc &&
14944 to_intel_crtc(encoder->base.crtc)->active;
14945
14946 if (encoder->connectors_active && !has_active_crtc) {
14947 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
14948 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014949 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014950
14951 /* Connector is active, but has no active pipe. This is
14952 * fallout from our resume register restoring. Disable
14953 * the encoder manually again. */
14954 if (encoder->base.crtc) {
14955 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
14956 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030014957 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020014958 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030014959 if (encoder->post_disable)
14960 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020014961 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020014962 encoder->base.crtc = NULL;
14963 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020014964
14965 /* Inconsistent output/port/pipe state happens presumably due to
14966 * a bug in one of the get_hw_state functions. Or someplace else
14967 * in our code, like the register restore mess on resume. Clamp
14968 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020014969 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020014970 if (connector->encoder != encoder)
14971 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020014972 connector->base.dpms = DRM_MODE_DPMS_OFF;
14973 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020014974 }
14975 }
14976 /* Enabled encoders without active connectors will be fixed in
14977 * the crtc fixup. */
14978}
14979
Imre Deak04098752014-02-18 00:02:16 +020014980void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014981{
14982 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014983 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010014984
Imre Deak04098752014-02-18 00:02:16 +020014985 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
14986 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
14987 i915_disable_vga(dev);
14988 }
14989}
14990
14991void i915_redisable_vga(struct drm_device *dev)
14992{
14993 struct drm_i915_private *dev_priv = dev->dev_private;
14994
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030014995 /* This function can be called both from intel_modeset_setup_hw_state or
14996 * at a very early point in our resume sequence, where the power well
14997 * structures are not yet restored. Since this function is at a very
14998 * paranoid "someone might have enabled VGA while we were not looking"
14999 * level, just check if the power well is enabled instead of trying to
15000 * follow the "don't touch the power well if we don't need it" policy
15001 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015002 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015003 return;
15004
Imre Deak04098752014-02-18 00:02:16 +020015005 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015006}
15007
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015008static bool primary_get_hw_state(struct intel_crtc *crtc)
15009{
15010 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15011
15012 if (!crtc->active)
15013 return false;
15014
15015 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15016}
15017
Daniel Vetter30e984d2013-06-05 13:34:17 +020015018static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015019{
15020 struct drm_i915_private *dev_priv = dev->dev_private;
15021 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015022 struct intel_crtc *crtc;
15023 struct intel_encoder *encoder;
15024 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015025 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015026
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015027 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015028 struct drm_plane *primary = crtc->base.primary;
15029 struct intel_plane_state *plane_state;
15030
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015031 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015032
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015033 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015034
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015035 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015036 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015037
Matt Roper83d65732015-02-25 13:12:16 -080015038 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015039 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015040 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015041
15042 plane_state = to_intel_plane_state(primary->state);
15043 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015044
15045 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15046 crtc->base.base.id,
15047 crtc->active ? "enabled" : "disabled");
15048 }
15049
Daniel Vetter53589012013-06-05 13:34:16 +020015050 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15051 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15052
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015053 pll->on = pll->get_hw_state(dev_priv, pll,
15054 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015055 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015056 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015057 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015058 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015059 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015060 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015061 }
Daniel Vetter53589012013-06-05 13:34:16 +020015062 }
Daniel Vetter53589012013-06-05 13:34:16 +020015063
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015064 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015065 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015066
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015067 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015068 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015069 }
15070
Damien Lespiaub2784e12014-08-05 11:29:37 +010015071 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015072 pipe = 0;
15073
15074 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015075 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15076 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015077 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015078 } else {
15079 encoder->base.crtc = NULL;
15080 }
15081
15082 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015083 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015084 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015085 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015086 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015087 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015088 }
15089
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015090 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015091 if (connector->get_hw_state(connector)) {
15092 connector->base.dpms = DRM_MODE_DPMS_ON;
15093 connector->encoder->connectors_active = true;
15094 connector->base.encoder = &connector->encoder->base;
15095 } else {
15096 connector->base.dpms = DRM_MODE_DPMS_OFF;
15097 connector->base.encoder = NULL;
15098 }
15099 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15100 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015101 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015102 connector->base.encoder ? "enabled" : "disabled");
15103 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015104}
15105
15106/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15107 * and i915 state tracking structures. */
15108void intel_modeset_setup_hw_state(struct drm_device *dev,
15109 bool force_restore)
15110{
15111 struct drm_i915_private *dev_priv = dev->dev_private;
15112 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015113 struct intel_crtc *crtc;
15114 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015115 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015116
15117 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015118
Jesse Barnesbabea612013-06-26 18:57:38 +030015119 /*
15120 * Now that we have the config, copy it to each CRTC struct
15121 * Note that this could go away if we move to using crtc_config
15122 * checking everywhere.
15123 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015124 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015125 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015126 intel_mode_from_pipe_config(&crtc->base.mode,
15127 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015128 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15129 crtc->base.base.id);
15130 drm_mode_debug_printmodeline(&crtc->base.mode);
15131 }
15132 }
15133
Daniel Vetter24929352012-07-02 20:28:59 +020015134 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015135 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015136 intel_sanitize_encoder(encoder);
15137 }
15138
Damien Lespiau055e3932014-08-18 13:49:10 +010015139 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015140 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15141 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015142 intel_dump_pipe_config(crtc, crtc->config,
15143 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015144 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015145
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015146 intel_modeset_update_connector_atomic_state(dev);
15147
Daniel Vetter35c95372013-07-17 06:55:04 +020015148 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15149 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15150
15151 if (!pll->on || pll->active)
15152 continue;
15153
15154 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15155
15156 pll->disable(dev_priv, pll);
15157 pll->on = false;
15158 }
15159
Pradeep Bhat30789992014-11-04 17:06:45 +000015160 if (IS_GEN9(dev))
15161 skl_wm_get_hw_state(dev);
15162 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015163 ilk_wm_get_hw_state(dev);
15164
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015165 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015166 i915_redisable_vga(dev);
15167
Daniel Vetterf30da182013-04-11 20:22:50 +020015168 /*
15169 * We need to use raw interfaces for restoring state to avoid
15170 * checking (bogus) intermediate states.
15171 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015172 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015173 struct drm_crtc *crtc =
15174 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015175
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015176 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015177 }
15178 } else {
15179 intel_modeset_update_staged_output_state(dev);
15180 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015181
15182 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015183}
15184
15185void intel_modeset_gem_init(struct drm_device *dev)
15186{
Jesse Barnes92122782014-10-09 12:57:42 -070015187 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015188 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015189 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015190 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015191
Imre Deakae484342014-03-31 15:10:44 +030015192 mutex_lock(&dev->struct_mutex);
15193 intel_init_gt_powersave(dev);
15194 mutex_unlock(&dev->struct_mutex);
15195
Jesse Barnes92122782014-10-09 12:57:42 -070015196 /*
15197 * There may be no VBT; and if the BIOS enabled SSC we can
15198 * just keep using it to avoid unnecessary flicker. Whereas if the
15199 * BIOS isn't using it, don't assume it will work even if the VBT
15200 * indicates as much.
15201 */
15202 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15203 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15204 DREF_SSC1_ENABLE);
15205
Chris Wilson1833b132012-05-09 11:56:28 +010015206 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015207
15208 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015209
15210 /*
15211 * Make sure any fbs we allocated at startup are properly
15212 * pinned & fenced. When we do the allocation it's too early
15213 * for this.
15214 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015215 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015216 obj = intel_fb_obj(c->primary->fb);
15217 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015218 continue;
15219
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015220 mutex_lock(&dev->struct_mutex);
15221 ret = intel_pin_and_fence_fb_obj(c->primary,
15222 c->primary->fb,
15223 c->primary->state,
15224 NULL);
15225 mutex_unlock(&dev->struct_mutex);
15226 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015227 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15228 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015229 drm_framebuffer_unreference(c->primary->fb);
15230 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015231 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015232 }
15233 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015234
15235 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015236}
15237
Imre Deak4932e2c2014-02-11 17:12:48 +020015238void intel_connector_unregister(struct intel_connector *intel_connector)
15239{
15240 struct drm_connector *connector = &intel_connector->base;
15241
15242 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015243 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015244}
15245
Jesse Barnes79e53942008-11-07 14:24:08 -080015246void intel_modeset_cleanup(struct drm_device *dev)
15247{
Jesse Barnes652c3932009-08-17 13:31:43 -070015248 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015249 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015250
Imre Deak2eb52522014-11-19 15:30:05 +020015251 intel_disable_gt_powersave(dev);
15252
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015253 intel_backlight_unregister(dev);
15254
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015255 /*
15256 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015257 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015258 * experience fancy races otherwise.
15259 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015260 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015261
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015262 /*
15263 * Due to the hpd irq storm handling the hotplug work can re-arm the
15264 * poll handlers. Hence disable polling after hpd handling is shut down.
15265 */
Keith Packardf87ea762010-10-03 19:36:26 -070015266 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015267
Jesse Barnes652c3932009-08-17 13:31:43 -070015268 mutex_lock(&dev->struct_mutex);
15269
Jesse Barnes723bfd72010-10-07 16:01:13 -070015270 intel_unregister_dsm_handler();
15271
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015272 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015273
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015274 mutex_unlock(&dev->struct_mutex);
15275
Chris Wilson1630fe72011-07-08 12:22:42 +010015276 /* flush any delayed tasks or pending work */
15277 flush_scheduled_work();
15278
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015279 /* destroy the backlight and sysfs files before encoders/connectors */
15280 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015281 struct intel_connector *intel_connector;
15282
15283 intel_connector = to_intel_connector(connector);
15284 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015285 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015286
Jesse Barnes79e53942008-11-07 14:24:08 -080015287 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015288
15289 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015290
15291 mutex_lock(&dev->struct_mutex);
15292 intel_cleanup_gt_powersave(dev);
15293 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015294}
15295
Dave Airlie28d52042009-09-21 14:33:58 +100015296/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015297 * Return which encoder is currently attached for connector.
15298 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015299struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015300{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015301 return &intel_attached_encoder(connector)->base;
15302}
Jesse Barnes79e53942008-11-07 14:24:08 -080015303
Chris Wilsondf0e9242010-09-09 16:20:55 +010015304void intel_connector_attach_encoder(struct intel_connector *connector,
15305 struct intel_encoder *encoder)
15306{
15307 connector->encoder = encoder;
15308 drm_mode_connector_attach_encoder(&connector->base,
15309 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015310}
Dave Airlie28d52042009-09-21 14:33:58 +100015311
15312/*
15313 * set vga decode state - true == enable VGA decode
15314 */
15315int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15316{
15317 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015318 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015319 u16 gmch_ctrl;
15320
Chris Wilson75fa0412014-02-07 18:37:02 -020015321 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15322 DRM_ERROR("failed to read control word\n");
15323 return -EIO;
15324 }
15325
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015326 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15327 return 0;
15328
Dave Airlie28d52042009-09-21 14:33:58 +100015329 if (state)
15330 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15331 else
15332 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015333
15334 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15335 DRM_ERROR("failed to write control word\n");
15336 return -EIO;
15337 }
15338
Dave Airlie28d52042009-09-21 14:33:58 +100015339 return 0;
15340}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015341
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015342struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015343
15344 u32 power_well_driver;
15345
Chris Wilson63b66e52013-08-08 15:12:06 +020015346 int num_transcoders;
15347
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015348 struct intel_cursor_error_state {
15349 u32 control;
15350 u32 position;
15351 u32 base;
15352 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015353 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015354
15355 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015356 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015357 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015358 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015359 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015360
15361 struct intel_plane_error_state {
15362 u32 control;
15363 u32 stride;
15364 u32 size;
15365 u32 pos;
15366 u32 addr;
15367 u32 surface;
15368 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015369 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015370
15371 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015372 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015373 enum transcoder cpu_transcoder;
15374
15375 u32 conf;
15376
15377 u32 htotal;
15378 u32 hblank;
15379 u32 hsync;
15380 u32 vtotal;
15381 u32 vblank;
15382 u32 vsync;
15383 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015384};
15385
15386struct intel_display_error_state *
15387intel_display_capture_error_state(struct drm_device *dev)
15388{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015389 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015390 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015391 int transcoders[] = {
15392 TRANSCODER_A,
15393 TRANSCODER_B,
15394 TRANSCODER_C,
15395 TRANSCODER_EDP,
15396 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015397 int i;
15398
Chris Wilson63b66e52013-08-08 15:12:06 +020015399 if (INTEL_INFO(dev)->num_pipes == 0)
15400 return NULL;
15401
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015402 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015403 if (error == NULL)
15404 return NULL;
15405
Imre Deak190be112013-11-25 17:15:31 +020015406 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015407 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15408
Damien Lespiau055e3932014-08-18 13:49:10 +010015409 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015410 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015411 __intel_display_power_is_enabled(dev_priv,
15412 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015413 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015414 continue;
15415
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015416 error->cursor[i].control = I915_READ(CURCNTR(i));
15417 error->cursor[i].position = I915_READ(CURPOS(i));
15418 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015419
15420 error->plane[i].control = I915_READ(DSPCNTR(i));
15421 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015422 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015423 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015424 error->plane[i].pos = I915_READ(DSPPOS(i));
15425 }
Paulo Zanonica291362013-03-06 20:03:14 -030015426 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15427 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015428 if (INTEL_INFO(dev)->gen >= 4) {
15429 error->plane[i].surface = I915_READ(DSPSURF(i));
15430 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15431 }
15432
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015433 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015434
Sonika Jindal3abfce72014-07-21 15:23:43 +053015435 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015436 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015437 }
15438
15439 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15440 if (HAS_DDI(dev_priv->dev))
15441 error->num_transcoders++; /* Account for eDP. */
15442
15443 for (i = 0; i < error->num_transcoders; i++) {
15444 enum transcoder cpu_transcoder = transcoders[i];
15445
Imre Deakddf9c532013-11-27 22:02:02 +020015446 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015447 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015448 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015449 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015450 continue;
15451
Chris Wilson63b66e52013-08-08 15:12:06 +020015452 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15453
15454 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15455 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15456 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15457 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15458 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15459 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15460 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015461 }
15462
15463 return error;
15464}
15465
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015466#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15467
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015468void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015469intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015470 struct drm_device *dev,
15471 struct intel_display_error_state *error)
15472{
Damien Lespiau055e3932014-08-18 13:49:10 +010015473 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015474 int i;
15475
Chris Wilson63b66e52013-08-08 15:12:06 +020015476 if (!error)
15477 return;
15478
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015479 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015480 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015481 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015482 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015483 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015484 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015485 err_printf(m, " Power: %s\n",
15486 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015487 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015488 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015489
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015490 err_printf(m, "Plane [%d]:\n", i);
15491 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15492 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015493 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015494 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15495 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015496 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015497 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015498 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015499 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015500 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15501 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015502 }
15503
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015504 err_printf(m, "Cursor [%d]:\n", i);
15505 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15506 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15507 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015508 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015509
15510 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015511 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015512 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015513 err_printf(m, " Power: %s\n",
15514 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015515 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15516 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15517 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15518 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15519 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15520 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15521 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15522 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015523}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015524
15525void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15526{
15527 struct intel_crtc *crtc;
15528
15529 for_each_intel_crtc(dev, crtc) {
15530 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015531
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015532 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015533
15534 work = crtc->unpin_work;
15535
15536 if (work && work->event &&
15537 work->event->base.file_priv == file) {
15538 kfree(work->event);
15539 work->event = NULL;
15540 }
15541
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015542 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015543 }
15544}