blob: 4648fa5ab55afcb045920f4eaf0730724cde514c [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
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +0200434static bool
435needs_modeset(struct drm_crtc_state *state)
436{
437 return state->mode_changed || state->active_changed;
438}
439
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300440/**
441 * Returns whether any output on the specified pipe is of the specified type
442 */
Damien Lespiau40935612014-10-29 11:16:59 +0000443bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300444{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300445 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300446 struct intel_encoder *encoder;
447
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300448 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300449 if (encoder->type == type)
450 return true;
451
452 return false;
453}
454
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200455/**
456 * Returns whether any output on the specified pipe will have the specified
457 * type after a staged modeset is complete, i.e., the same as
458 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
459 * encoder->crtc.
460 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200461static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
462 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200463{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200464 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300465 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200466 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200467 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200468 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200469
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300470 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200471 if (connector_state->crtc != crtc_state->base.crtc)
472 continue;
473
474 num_connectors++;
475
476 encoder = to_intel_encoder(connector_state->best_encoder);
477 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200478 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200479 }
480
481 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200482
483 return false;
484}
485
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200486static const intel_limit_t *
487intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800488{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200489 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800490 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800491
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200492 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100493 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000494 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800495 limit = &intel_limits_ironlake_dual_lvds_100m;
496 else
497 limit = &intel_limits_ironlake_dual_lvds;
498 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000499 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800500 limit = &intel_limits_ironlake_single_lvds_100m;
501 else
502 limit = &intel_limits_ironlake_single_lvds;
503 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200504 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800505 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800506
507 return limit;
508}
509
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510static const intel_limit_t *
511intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800512{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200513 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800514 const intel_limit_t *limit;
515
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200516 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100517 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700518 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800519 else
Keith Packarde4b36692009-06-05 19:22:17 -0700520 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200521 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
522 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700523 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200524 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700525 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800526 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700527 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800528
529 return limit;
530}
531
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200532static const intel_limit_t *
533intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800534{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800536 const intel_limit_t *limit;
537
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200538 if (IS_BROXTON(dev))
539 limit = &intel_limits_bxt;
540 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200541 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800542 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200543 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500544 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200545 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500546 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800547 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500548 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300549 } else if (IS_CHERRYVIEW(dev)) {
550 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700551 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300552 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100553 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200554 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100555 limit = &intel_limits_i9xx_lvds;
556 else
557 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800558 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200559 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700560 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200561 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700562 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200563 else
564 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800565 }
566 return limit;
567}
568
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500569/* m1 is reserved as 0 in Pineview, n is a ring counter */
570static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800571{
Shaohua Li21778322009-02-23 15:19:16 +0800572 clock->m = clock->m2 + 2;
573 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200574 if (WARN_ON(clock->n == 0 || clock->p == 0))
575 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300576 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
577 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800578}
579
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200580static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
581{
582 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
583}
584
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200585static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800586{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200587 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800588 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200589 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
590 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300591 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
592 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800593}
594
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300595static void chv_clock(int refclk, intel_clock_t *clock)
596{
597 clock->m = clock->m1 * clock->m2;
598 clock->p = clock->p1 * clock->p2;
599 if (WARN_ON(clock->n == 0 || clock->p == 0))
600 return;
601 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
602 clock->n << 22);
603 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
604}
605
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800606#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800607/**
608 * Returns whether the given set of divisors are valid for a given refclk with
609 * the given connectors.
610 */
611
Chris Wilson1b894b52010-12-14 20:04:54 +0000612static bool intel_PLL_is_valid(struct drm_device *dev,
613 const intel_limit_t *limit,
614 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800615{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300616 if (clock->n < limit->n.min || limit->n.max < clock->n)
617 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800618 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400619 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800620 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400621 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800622 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400623 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300624
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200625 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300626 if (clock->m1 <= clock->m2)
627 INTELPllInvalid("m1 <= m2\n");
628
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200629 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300630 if (clock->p < limit->p.min || limit->p.max < clock->p)
631 INTELPllInvalid("p out of range\n");
632 if (clock->m < limit->m.min || limit->m.max < clock->m)
633 INTELPllInvalid("m out of range\n");
634 }
635
Jesse Barnes79e53942008-11-07 14:24:08 -0800636 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400637 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800638 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
639 * connector, etc., rather than just a single range.
640 */
641 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400642 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800643
644 return true;
645}
646
Ma Lingd4906092009-03-18 20:13:27 +0800647static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200648i9xx_find_best_dpll(const intel_limit_t *limit,
649 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800650 int target, int refclk, intel_clock_t *match_clock,
651 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800652{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200653 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300654 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800655 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800656 int err = target;
657
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200658 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100660 * For LVDS just rely on its current settings for dual-channel.
661 * We haven't figured out how to reliably set up different
662 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800663 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100664 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800665 clock.p2 = limit->p2.p2_fast;
666 else
667 clock.p2 = limit->p2.p2_slow;
668 } else {
669 if (target < limit->p2.dot_limit)
670 clock.p2 = limit->p2.p2_slow;
671 else
672 clock.p2 = limit->p2.p2_fast;
673 }
674
Akshay Joshi0206e352011-08-16 15:34:10 -0400675 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800676
Zhao Yakui42158662009-11-20 11:24:18 +0800677 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
678 clock.m1++) {
679 for (clock.m2 = limit->m2.min;
680 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200681 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800682 break;
683 for (clock.n = limit->n.min;
684 clock.n <= limit->n.max; clock.n++) {
685 for (clock.p1 = limit->p1.min;
686 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800687 int this_err;
688
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200689 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000690 if (!intel_PLL_is_valid(dev, limit,
691 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800692 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800693 if (match_clock &&
694 clock.p != match_clock->p)
695 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800696
697 this_err = abs(clock.dot - target);
698 if (this_err < err) {
699 *best_clock = clock;
700 err = this_err;
701 }
702 }
703 }
704 }
705 }
706
707 return (err != target);
708}
709
Ma Lingd4906092009-03-18 20:13:27 +0800710static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200711pnv_find_best_dpll(const intel_limit_t *limit,
712 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200713 int target, int refclk, intel_clock_t *match_clock,
714 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200715{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200716 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300717 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200718 intel_clock_t clock;
719 int err = target;
720
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200721 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200722 /*
723 * For LVDS just rely on its current settings for dual-channel.
724 * We haven't figured out how to reliably set up different
725 * single/dual channel state, if we even can.
726 */
727 if (intel_is_dual_link_lvds(dev))
728 clock.p2 = limit->p2.p2_fast;
729 else
730 clock.p2 = limit->p2.p2_slow;
731 } else {
732 if (target < limit->p2.dot_limit)
733 clock.p2 = limit->p2.p2_slow;
734 else
735 clock.p2 = limit->p2.p2_fast;
736 }
737
738 memset(best_clock, 0, sizeof(*best_clock));
739
740 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
741 clock.m1++) {
742 for (clock.m2 = limit->m2.min;
743 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200744 for (clock.n = limit->n.min;
745 clock.n <= limit->n.max; clock.n++) {
746 for (clock.p1 = limit->p1.min;
747 clock.p1 <= limit->p1.max; clock.p1++) {
748 int this_err;
749
750 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800751 if (!intel_PLL_is_valid(dev, limit,
752 &clock))
753 continue;
754 if (match_clock &&
755 clock.p != match_clock->p)
756 continue;
757
758 this_err = abs(clock.dot - target);
759 if (this_err < err) {
760 *best_clock = clock;
761 err = this_err;
762 }
763 }
764 }
765 }
766 }
767
768 return (err != target);
769}
770
Ma Lingd4906092009-03-18 20:13:27 +0800771static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200772g4x_find_best_dpll(const intel_limit_t *limit,
773 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200774 int target, int refclk, intel_clock_t *match_clock,
775 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800776{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200777 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300778 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800779 intel_clock_t clock;
780 int max_n;
781 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400782 /* approximately equals target * 0.00585 */
783 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800784 found = false;
785
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200786 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100787 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800788 clock.p2 = limit->p2.p2_fast;
789 else
790 clock.p2 = limit->p2.p2_slow;
791 } else {
792 if (target < limit->p2.dot_limit)
793 clock.p2 = limit->p2.p2_slow;
794 else
795 clock.p2 = limit->p2.p2_fast;
796 }
797
798 memset(best_clock, 0, sizeof(*best_clock));
799 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200800 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800801 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200802 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800803 for (clock.m1 = limit->m1.max;
804 clock.m1 >= limit->m1.min; clock.m1--) {
805 for (clock.m2 = limit->m2.max;
806 clock.m2 >= limit->m2.min; clock.m2--) {
807 for (clock.p1 = limit->p1.max;
808 clock.p1 >= limit->p1.min; clock.p1--) {
809 int this_err;
810
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200811 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000812 if (!intel_PLL_is_valid(dev, limit,
813 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800814 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000815
816 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800817 if (this_err < err_most) {
818 *best_clock = clock;
819 err_most = this_err;
820 max_n = clock.n;
821 found = true;
822 }
823 }
824 }
825 }
826 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800827 return found;
828}
Ma Lingd4906092009-03-18 20:13:27 +0800829
Imre Deakd5dd62b2015-03-17 11:40:03 +0200830/*
831 * Check if the calculated PLL configuration is more optimal compared to the
832 * best configuration and error found so far. Return the calculated error.
833 */
834static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
835 const intel_clock_t *calculated_clock,
836 const intel_clock_t *best_clock,
837 unsigned int best_error_ppm,
838 unsigned int *error_ppm)
839{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200840 /*
841 * For CHV ignore the error and consider only the P value.
842 * Prefer a bigger P value based on HW requirements.
843 */
844 if (IS_CHERRYVIEW(dev)) {
845 *error_ppm = 0;
846
847 return calculated_clock->p > best_clock->p;
848 }
849
Imre Deak24be4e42015-03-17 11:40:04 +0200850 if (WARN_ON_ONCE(!target_freq))
851 return false;
852
Imre Deakd5dd62b2015-03-17 11:40:03 +0200853 *error_ppm = div_u64(1000000ULL *
854 abs(target_freq - calculated_clock->dot),
855 target_freq);
856 /*
857 * Prefer a better P value over a better (smaller) error if the error
858 * is small. Ensure this preference for future configurations too by
859 * setting the error to 0.
860 */
861 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
862 *error_ppm = 0;
863
864 return true;
865 }
866
867 return *error_ppm + 10 < best_error_ppm;
868}
869
Zhenyu Wang2c072452009-06-05 15:38:42 +0800870static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200871vlv_find_best_dpll(const intel_limit_t *limit,
872 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200873 int target, int refclk, intel_clock_t *match_clock,
874 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700875{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200876 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300877 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300878 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300879 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300880 /* min update 19.2 MHz */
881 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300882 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700883
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300884 target *= 5; /* fast clock */
885
886 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700887
888 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300889 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300890 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300891 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300892 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300893 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700894 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300895 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200896 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300897
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300898 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
899 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300900
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300901 vlv_clock(refclk, &clock);
902
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300903 if (!intel_PLL_is_valid(dev, limit,
904 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300905 continue;
906
Imre Deakd5dd62b2015-03-17 11:40:03 +0200907 if (!vlv_PLL_is_optimal(dev, target,
908 &clock,
909 best_clock,
910 bestppm, &ppm))
911 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300912
Imre Deakd5dd62b2015-03-17 11:40:03 +0200913 *best_clock = clock;
914 bestppm = ppm;
915 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700916 }
917 }
918 }
919 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700920
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300921 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700922}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700923
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300924static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200925chv_find_best_dpll(const intel_limit_t *limit,
926 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300927 int target, int refclk, intel_clock_t *match_clock,
928 intel_clock_t *best_clock)
929{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200930 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300931 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200932 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300933 intel_clock_t clock;
934 uint64_t m2;
935 int found = false;
936
937 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200938 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300939
940 /*
941 * Based on hardware doc, the n always set to 1, and m1 always
942 * set to 2. If requires to support 200Mhz refclk, we need to
943 * revisit this because n may not 1 anymore.
944 */
945 clock.n = 1, clock.m1 = 2;
946 target *= 5; /* fast clock */
947
948 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
949 for (clock.p2 = limit->p2.p2_fast;
950 clock.p2 >= limit->p2.p2_slow;
951 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200952 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300953
954 clock.p = clock.p1 * clock.p2;
955
956 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
957 clock.n) << 22, refclk * clock.m1);
958
959 if (m2 > INT_MAX/clock.m1)
960 continue;
961
962 clock.m2 = m2;
963
964 chv_clock(refclk, &clock);
965
966 if (!intel_PLL_is_valid(dev, limit, &clock))
967 continue;
968
Imre Deak9ca3ba02015-03-17 11:40:05 +0200969 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
970 best_error_ppm, &error_ppm))
971 continue;
972
973 *best_clock = clock;
974 best_error_ppm = error_ppm;
975 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300976 }
977 }
978
979 return found;
980}
981
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200982bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
983 intel_clock_t *best_clock)
984{
985 int refclk = i9xx_get_refclk(crtc_state, 0);
986
987 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
988 target_clock, refclk, NULL, best_clock);
989}
990
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300991bool intel_crtc_active(struct drm_crtc *crtc)
992{
993 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
994
995 /* Be paranoid as we can arrive here with only partial
996 * state retrieved from the hardware during setup.
997 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100998 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300999 * as Haswell has gained clock readout/fastboot support.
1000 *
Dave Airlie66e514c2014-04-03 07:51:54 +10001001 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001002 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -07001003 *
1004 * FIXME: The intel_crtc->active here should be switched to
1005 * crtc->state->active once we have proper CRTC states wired up
1006 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001007 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001008 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001009 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001010}
1011
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001012enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1013 enum pipe pipe)
1014{
1015 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1016 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1017
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001018 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001019}
1020
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001021static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1022{
1023 struct drm_i915_private *dev_priv = dev->dev_private;
1024 u32 reg = PIPEDSL(pipe);
1025 u32 line1, line2;
1026 u32 line_mask;
1027
1028 if (IS_GEN2(dev))
1029 line_mask = DSL_LINEMASK_GEN2;
1030 else
1031 line_mask = DSL_LINEMASK_GEN3;
1032
1033 line1 = I915_READ(reg) & line_mask;
1034 mdelay(5);
1035 line2 = I915_READ(reg) & line_mask;
1036
1037 return line1 == line2;
1038}
1039
Keith Packardab7ad7f2010-10-03 00:33:06 -07001040/*
1041 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001042 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001043 *
1044 * After disabling a pipe, we can't wait for vblank in the usual way,
1045 * spinning on the vblank interrupt status bit, since we won't actually
1046 * see an interrupt when the pipe is disabled.
1047 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001048 * On Gen4 and above:
1049 * wait for the pipe register state bit to turn off
1050 *
1051 * Otherwise:
1052 * wait for the display line value to settle (it usually
1053 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001054 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001055 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001056static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001057{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001058 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001059 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001060 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001061 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001062
Keith Packardab7ad7f2010-10-03 00:33:06 -07001063 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001064 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001065
Keith Packardab7ad7f2010-10-03 00:33:06 -07001066 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001067 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1068 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001069 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001070 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001071 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001072 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001073 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001074 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001075}
1076
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001077/*
1078 * ibx_digital_port_connected - is the specified port connected?
1079 * @dev_priv: i915 private structure
1080 * @port: the port to test
1081 *
1082 * Returns true if @port is connected, false otherwise.
1083 */
1084bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1085 struct intel_digital_port *port)
1086{
1087 u32 bit;
1088
Damien Lespiauc36346e2012-12-13 16:09:03 +00001089 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001090 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001091 case PORT_B:
1092 bit = SDE_PORTB_HOTPLUG;
1093 break;
1094 case PORT_C:
1095 bit = SDE_PORTC_HOTPLUG;
1096 break;
1097 case PORT_D:
1098 bit = SDE_PORTD_HOTPLUG;
1099 break;
1100 default:
1101 return true;
1102 }
1103 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001104 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001105 case PORT_B:
1106 bit = SDE_PORTB_HOTPLUG_CPT;
1107 break;
1108 case PORT_C:
1109 bit = SDE_PORTC_HOTPLUG_CPT;
1110 break;
1111 case PORT_D:
1112 bit = SDE_PORTD_HOTPLUG_CPT;
1113 break;
1114 default:
1115 return true;
1116 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001117 }
1118
1119 return I915_READ(SDEISR) & bit;
1120}
1121
Jesse Barnesb24e7172011-01-04 15:09:30 -08001122static const char *state_string(bool enabled)
1123{
1124 return enabled ? "on" : "off";
1125}
1126
1127/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001128void assert_pll(struct drm_i915_private *dev_priv,
1129 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001130{
1131 int reg;
1132 u32 val;
1133 bool cur_state;
1134
1135 reg = DPLL(pipe);
1136 val = I915_READ(reg);
1137 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001138 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001139 "PLL state assertion failure (expected %s, current %s)\n",
1140 state_string(state), state_string(cur_state));
1141}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001142
Jani Nikula23538ef2013-08-27 15:12:22 +03001143/* XXX: the dsi pll is shared between MIPI DSI ports */
1144static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1145{
1146 u32 val;
1147 bool cur_state;
1148
Ville Syrjäläa5805162015-05-26 20:42:30 +03001149 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001150 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001151 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001152
1153 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001154 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001155 "DSI PLL state assertion failure (expected %s, current %s)\n",
1156 state_string(state), state_string(cur_state));
1157}
1158#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1159#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1160
Daniel Vetter55607e82013-06-16 21:42:39 +02001161struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001162intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001163{
Daniel Vettere2b78262013-06-07 23:10:03 +02001164 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1165
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001166 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001167 return NULL;
1168
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001169 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001170}
1171
Jesse Barnesb24e7172011-01-04 15:09:30 -08001172/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001173void assert_shared_dpll(struct drm_i915_private *dev_priv,
1174 struct intel_shared_dpll *pll,
1175 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001176{
Jesse Barnes040484a2011-01-03 12:14:26 -08001177 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001178 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001179
Chris Wilson92b27b02012-05-20 18:10:50 +01001180 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001181 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001182 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001183
Daniel Vetter53589012013-06-05 13:34:16 +02001184 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001185 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001186 "%s assertion failure (expected %s, current %s)\n",
1187 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001188}
Jesse Barnes040484a2011-01-03 12:14:26 -08001189
1190static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1191 enum pipe pipe, bool state)
1192{
1193 int reg;
1194 u32 val;
1195 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001196 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1197 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001198
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001199 if (HAS_DDI(dev_priv->dev)) {
1200 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001201 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001202 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001203 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001204 } else {
1205 reg = FDI_TX_CTL(pipe);
1206 val = I915_READ(reg);
1207 cur_state = !!(val & FDI_TX_ENABLE);
1208 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001209 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001210 "FDI TX state assertion failure (expected %s, current %s)\n",
1211 state_string(state), state_string(cur_state));
1212}
1213#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1214#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1215
1216static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1217 enum pipe pipe, bool state)
1218{
1219 int reg;
1220 u32 val;
1221 bool cur_state;
1222
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001223 reg = FDI_RX_CTL(pipe);
1224 val = I915_READ(reg);
1225 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001226 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001227 "FDI RX state assertion failure (expected %s, current %s)\n",
1228 state_string(state), state_string(cur_state));
1229}
1230#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1231#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1232
1233static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1234 enum pipe pipe)
1235{
1236 int reg;
1237 u32 val;
1238
1239 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001240 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001241 return;
1242
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001243 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001244 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001245 return;
1246
Jesse Barnes040484a2011-01-03 12:14:26 -08001247 reg = FDI_TX_CTL(pipe);
1248 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001249 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 -08001250}
1251
Daniel Vetter55607e82013-06-16 21:42:39 +02001252void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1253 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001254{
1255 int reg;
1256 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001257 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001258
1259 reg = FDI_RX_CTL(pipe);
1260 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001261 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001262 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001263 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1264 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001265}
1266
Daniel Vetterb680c372014-09-19 18:27:27 +02001267void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1268 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001270 struct drm_device *dev = dev_priv->dev;
1271 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001272 u32 val;
1273 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001274 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001275
Jani Nikulabedd4db2014-08-22 15:04:13 +03001276 if (WARN_ON(HAS_DDI(dev)))
1277 return;
1278
1279 if (HAS_PCH_SPLIT(dev)) {
1280 u32 port_sel;
1281
Jesse Barnesea0760c2011-01-04 15:09:32 -08001282 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001283 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1284
1285 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1286 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1287 panel_pipe = PIPE_B;
1288 /* XXX: else fix for eDP */
1289 } else if (IS_VALLEYVIEW(dev)) {
1290 /* presumably write lock depends on pipe, not port select */
1291 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1292 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001293 } else {
1294 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001295 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1296 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001297 }
1298
1299 val = I915_READ(pp_reg);
1300 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001301 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001302 locked = false;
1303
Rob Clarke2c719b2014-12-15 13:56:32 -05001304 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001305 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001306 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001307}
1308
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001309static void assert_cursor(struct drm_i915_private *dev_priv,
1310 enum pipe pipe, bool state)
1311{
1312 struct drm_device *dev = dev_priv->dev;
1313 bool cur_state;
1314
Paulo Zanonid9d82082014-02-27 16:30:56 -03001315 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001316 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001317 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001318 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001319
Rob Clarke2c719b2014-12-15 13:56:32 -05001320 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001321 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1322 pipe_name(pipe), state_string(state), state_string(cur_state));
1323}
1324#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1325#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1326
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001327void assert_pipe(struct drm_i915_private *dev_priv,
1328 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001329{
1330 int reg;
1331 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001332 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001333 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1334 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001335
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001336 /* if we need the pipe quirk it must be always on */
1337 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1338 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001339 state = true;
1340
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001341 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001342 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001343 cur_state = false;
1344 } else {
1345 reg = PIPECONF(cpu_transcoder);
1346 val = I915_READ(reg);
1347 cur_state = !!(val & PIPECONF_ENABLE);
1348 }
1349
Rob Clarke2c719b2014-12-15 13:56:32 -05001350 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001351 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001352 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001353}
1354
Chris Wilson931872f2012-01-16 23:01:13 +00001355static void assert_plane(struct drm_i915_private *dev_priv,
1356 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001357{
1358 int reg;
1359 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001360 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001361
1362 reg = DSPCNTR(plane);
1363 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001364 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001365 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001366 "plane %c assertion failure (expected %s, current %s)\n",
1367 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001368}
1369
Chris Wilson931872f2012-01-16 23:01:13 +00001370#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1371#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1372
Jesse Barnesb24e7172011-01-04 15:09:30 -08001373static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1374 enum pipe pipe)
1375{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001376 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001377 int reg, i;
1378 u32 val;
1379 int cur_pipe;
1380
Ville Syrjälä653e1022013-06-04 13:49:05 +03001381 /* Primary planes are fixed to pipes on gen4+ */
1382 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001383 reg = DSPCNTR(pipe);
1384 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001385 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001386 "plane %c assertion failure, should be disabled but not\n",
1387 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001388 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001389 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001390
Jesse Barnesb24e7172011-01-04 15:09:30 -08001391 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001392 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001393 reg = DSPCNTR(i);
1394 val = I915_READ(reg);
1395 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1396 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001397 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001398 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1399 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001400 }
1401}
1402
Jesse Barnes19332d72013-03-28 09:55:38 -07001403static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1404 enum pipe pipe)
1405{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001406 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001407 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001408 u32 val;
1409
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001410 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001411 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001412 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001413 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001414 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1415 sprite, pipe_name(pipe));
1416 }
1417 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001418 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001419 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001420 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001421 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001422 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001423 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001424 }
1425 } else if (INTEL_INFO(dev)->gen >= 7) {
1426 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001427 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001428 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001429 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001430 plane_name(pipe), pipe_name(pipe));
1431 } else if (INTEL_INFO(dev)->gen >= 5) {
1432 reg = DVSCNTR(pipe);
1433 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001434 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001435 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1436 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001437 }
1438}
1439
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001440static void assert_vblank_disabled(struct drm_crtc *crtc)
1441{
Rob Clarke2c719b2014-12-15 13:56:32 -05001442 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001443 drm_crtc_vblank_put(crtc);
1444}
1445
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001446static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001447{
1448 u32 val;
1449 bool enabled;
1450
Rob Clarke2c719b2014-12-15 13:56:32 -05001451 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001452
Jesse Barnes92f25842011-01-04 15:09:34 -08001453 val = I915_READ(PCH_DREF_CONTROL);
1454 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1455 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001456 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001457}
1458
Daniel Vetterab9412b2013-05-03 11:49:46 +02001459static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1460 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001461{
1462 int reg;
1463 u32 val;
1464 bool enabled;
1465
Daniel Vetterab9412b2013-05-03 11:49:46 +02001466 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001467 val = I915_READ(reg);
1468 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001469 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001470 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1471 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001472}
1473
Keith Packard4e634382011-08-06 10:39:45 -07001474static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1475 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001476{
1477 if ((val & DP_PORT_EN) == 0)
1478 return false;
1479
1480 if (HAS_PCH_CPT(dev_priv->dev)) {
1481 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1482 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1483 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1484 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001485 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1486 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1487 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001488 } else {
1489 if ((val & DP_PIPE_MASK) != (pipe << 30))
1490 return false;
1491 }
1492 return true;
1493}
1494
Keith Packard1519b992011-08-06 10:35:34 -07001495static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1496 enum pipe pipe, u32 val)
1497{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001498 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001499 return false;
1500
1501 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001502 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001503 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001504 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1505 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1506 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001507 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001508 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001509 return false;
1510 }
1511 return true;
1512}
1513
1514static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1515 enum pipe pipe, u32 val)
1516{
1517 if ((val & LVDS_PORT_EN) == 0)
1518 return false;
1519
1520 if (HAS_PCH_CPT(dev_priv->dev)) {
1521 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1522 return false;
1523 } else {
1524 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1525 return false;
1526 }
1527 return true;
1528}
1529
1530static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1531 enum pipe pipe, u32 val)
1532{
1533 if ((val & ADPA_DAC_ENABLE) == 0)
1534 return false;
1535 if (HAS_PCH_CPT(dev_priv->dev)) {
1536 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1537 return false;
1538 } else {
1539 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1540 return false;
1541 }
1542 return true;
1543}
1544
Jesse Barnes291906f2011-02-02 12:28:03 -08001545static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001546 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001547{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001548 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001549 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001550 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001551 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001552
Rob Clarke2c719b2014-12-15 13:56:32 -05001553 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001554 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001555 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001556}
1557
1558static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1559 enum pipe pipe, int reg)
1560{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001561 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001562 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001563 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001564 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001565
Rob Clarke2c719b2014-12-15 13:56:32 -05001566 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001567 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001568 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001569}
1570
1571static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1572 enum pipe pipe)
1573{
1574 int reg;
1575 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001576
Keith Packardf0575e92011-07-25 22:12:43 -07001577 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1578 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1579 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001580
1581 reg = PCH_ADPA;
1582 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001583 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001584 "PCH VGA 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
1587 reg = PCH_LVDS;
1588 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001589 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001590 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001591 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001592
Paulo Zanonie2debe92013-02-18 19:00:27 -03001593 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1594 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1595 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001596}
1597
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001598static void intel_init_dpio(struct drm_device *dev)
1599{
1600 struct drm_i915_private *dev_priv = dev->dev_private;
1601
1602 if (!IS_VALLEYVIEW(dev))
1603 return;
1604
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001605 /*
1606 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1607 * CHV x1 PHY (DP/HDMI D)
1608 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1609 */
1610 if (IS_CHERRYVIEW(dev)) {
1611 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1612 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1613 } else {
1614 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1615 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001616}
1617
Ville Syrjäläd288f652014-10-28 13:20:22 +02001618static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001619 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001620{
Daniel Vetter426115c2013-07-11 22:13:42 +02001621 struct drm_device *dev = crtc->base.dev;
1622 struct drm_i915_private *dev_priv = dev->dev_private;
1623 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001624 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001625
Daniel Vetter426115c2013-07-11 22:13:42 +02001626 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001627
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001628 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001629 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1630
1631 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001632 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001633 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001634
Daniel Vetter426115c2013-07-11 22:13:42 +02001635 I915_WRITE(reg, dpll);
1636 POSTING_READ(reg);
1637 udelay(150);
1638
1639 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1640 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1641
Ville Syrjäläd288f652014-10-28 13:20:22 +02001642 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001643 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001644
1645 /* We do this three times for luck */
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 */
Daniel Vetter426115c2013-07-11 22:13:42 +02001649 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001650 POSTING_READ(reg);
1651 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001652 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001653 POSTING_READ(reg);
1654 udelay(150); /* wait for warmup */
1655}
1656
Ville Syrjäläd288f652014-10-28 13:20:22 +02001657static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001658 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001659{
1660 struct drm_device *dev = crtc->base.dev;
1661 struct drm_i915_private *dev_priv = dev->dev_private;
1662 int pipe = crtc->pipe;
1663 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001664 u32 tmp;
1665
1666 assert_pipe_disabled(dev_priv, crtc->pipe);
1667
1668 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1669
Ville Syrjäläa5805162015-05-26 20:42:30 +03001670 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001671
1672 /* Enable back the 10bit clock to display controller */
1673 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1674 tmp |= DPIO_DCLKP_EN;
1675 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1676
Ville Syrjälä54433e92015-05-26 20:42:31 +03001677 mutex_unlock(&dev_priv->sb_lock);
1678
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001679 /*
1680 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1681 */
1682 udelay(1);
1683
1684 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001685 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001686
1687 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001688 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001689 DRM_ERROR("PLL %d failed to lock\n", pipe);
1690
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001691 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001692 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001693 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001694}
1695
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001696static int intel_num_dvo_pipes(struct drm_device *dev)
1697{
1698 struct intel_crtc *crtc;
1699 int count = 0;
1700
1701 for_each_intel_crtc(dev, crtc)
1702 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001703 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001704
1705 return count;
1706}
1707
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001708static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001709{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001710 struct drm_device *dev = crtc->base.dev;
1711 struct drm_i915_private *dev_priv = dev->dev_private;
1712 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001713 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001714
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001716
1717 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001718 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001719
1720 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001721 if (IS_MOBILE(dev) && !IS_I830(dev))
1722 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001723
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001724 /* Enable DVO 2x clock on both PLLs if necessary */
1725 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1726 /*
1727 * It appears to be important that we don't enable this
1728 * for the current pipe before otherwise configuring the
1729 * PLL. No idea how this should be handled if multiple
1730 * DVO outputs are enabled simultaneosly.
1731 */
1732 dpll |= DPLL_DVO_2X_MODE;
1733 I915_WRITE(DPLL(!crtc->pipe),
1734 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1735 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001736
1737 /* Wait for the clocks to stabilize. */
1738 POSTING_READ(reg);
1739 udelay(150);
1740
1741 if (INTEL_INFO(dev)->gen >= 4) {
1742 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001743 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001744 } else {
1745 /* The pixel multiplier can only be updated once the
1746 * DPLL is enabled and the clocks are stable.
1747 *
1748 * So write it again.
1749 */
1750 I915_WRITE(reg, dpll);
1751 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752
1753 /* We do this three times for luck */
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 */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001757 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001758 POSTING_READ(reg);
1759 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001760 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001761 POSTING_READ(reg);
1762 udelay(150); /* wait for warmup */
1763}
1764
1765/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001766 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001767 * @dev_priv: i915 private structure
1768 * @pipe: pipe PLL to disable
1769 *
1770 * Disable the PLL for @pipe, making sure the pipe is off first.
1771 *
1772 * Note! This is for pre-ILK only.
1773 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001774static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001775{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001776 struct drm_device *dev = crtc->base.dev;
1777 struct drm_i915_private *dev_priv = dev->dev_private;
1778 enum pipe pipe = crtc->pipe;
1779
1780 /* Disable DVO 2x clock on both PLLs if necessary */
1781 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001782 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001783 intel_num_dvo_pipes(dev) == 1) {
1784 I915_WRITE(DPLL(PIPE_B),
1785 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1786 I915_WRITE(DPLL(PIPE_A),
1787 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1788 }
1789
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001790 /* Don't disable pipe or pipe PLLs if needed */
1791 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1792 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001793 return;
1794
1795 /* Make sure the pipe isn't still relying on us */
1796 assert_pipe_disabled(dev_priv, pipe);
1797
Daniel Vetter50b44a42013-06-05 13:34:33 +02001798 I915_WRITE(DPLL(pipe), 0);
1799 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001800}
1801
Jesse Barnesf6071162013-10-01 10:41:38 -07001802static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1803{
1804 u32 val = 0;
1805
1806 /* Make sure the pipe isn't still relying on us */
1807 assert_pipe_disabled(dev_priv, pipe);
1808
Imre Deake5cbfbf2014-01-09 17:08:16 +02001809 /*
1810 * Leave integrated clock source and reference clock enabled for pipe B.
1811 * The latter is needed for VGA hotplug / manual detection.
1812 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001813 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001814 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001815 I915_WRITE(DPLL(pipe), val);
1816 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001817
1818}
1819
1820static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1821{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001822 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001823 u32 val;
1824
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001825 /* Make sure the pipe isn't still relying on us */
1826 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001827
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001828 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001829 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001830 if (pipe != PIPE_A)
1831 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1832 I915_WRITE(DPLL(pipe), val);
1833 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001834
Ville Syrjäläa5805162015-05-26 20:42:30 +03001835 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001836
1837 /* Disable 10bit clock to display controller */
1838 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1839 val &= ~DPIO_DCLKP_EN;
1840 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1841
Ville Syrjälä61407f62014-05-27 16:32:55 +03001842 /* disable left/right clock distribution */
1843 if (pipe != PIPE_B) {
1844 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1845 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1846 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1847 } else {
1848 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1849 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1850 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1851 }
1852
Ville Syrjäläa5805162015-05-26 20:42:30 +03001853 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001854}
1855
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001856void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001857 struct intel_digital_port *dport,
1858 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859{
1860 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001861 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001862
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001863 switch (dport->port) {
1864 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001865 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001866 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001867 break;
1868 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001869 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001870 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001871 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001872 break;
1873 case PORT_D:
1874 port_mask = DPLL_PORTD_READY_MASK;
1875 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001876 break;
1877 default:
1878 BUG();
1879 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001880
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001881 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1882 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1883 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001884}
1885
Daniel Vetterb14b1052014-04-24 23:55:13 +02001886static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1887{
1888 struct drm_device *dev = crtc->base.dev;
1889 struct drm_i915_private *dev_priv = dev->dev_private;
1890 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1891
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001892 if (WARN_ON(pll == NULL))
1893 return;
1894
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001895 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001896 if (pll->active == 0) {
1897 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1898 WARN_ON(pll->on);
1899 assert_shared_dpll_disabled(dev_priv, pll);
1900
1901 pll->mode_set(dev_priv, pll);
1902 }
1903}
1904
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001905/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001906 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001907 * @dev_priv: i915 private structure
1908 * @pipe: pipe PLL to enable
1909 *
1910 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1911 * drives the transcoder clock.
1912 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001913static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001914{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001915 struct drm_device *dev = crtc->base.dev;
1916 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001917 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001918
Daniel Vetter87a875b2013-06-05 13:34:19 +02001919 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001920 return;
1921
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001922 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001923 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001924
Damien Lespiau74dd6922014-07-29 18:06:17 +01001925 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001926 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001927 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001928
Daniel Vettercdbd2312013-06-05 13:34:03 +02001929 if (pll->active++) {
1930 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001931 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001932 return;
1933 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001934 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001935
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001936 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1937
Daniel Vetter46edb022013-06-05 13:34:12 +02001938 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001939 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001940 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001941}
1942
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001943static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001944{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001945 struct drm_device *dev = crtc->base.dev;
1946 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001947 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001948
Jesse Barnes92f25842011-01-04 15:09:34 -08001949 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001950 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001951 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001952 return;
1953
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001954 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001955 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001956
Daniel Vetter46edb022013-06-05 13:34:12 +02001957 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1958 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001959 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001960
Chris Wilson48da64a2012-05-13 20:16:12 +01001961 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001962 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001963 return;
1964 }
1965
Daniel Vettere9d69442013-06-05 13:34:15 +02001966 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001967 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001968 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001969 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001970
Daniel Vetter46edb022013-06-05 13:34:12 +02001971 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001972 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001973 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001974
1975 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001976}
1977
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001978static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1979 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001980{
Daniel Vetter23670b322012-11-01 09:15:30 +01001981 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001982 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001983 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001984 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001985
1986 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001987 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001988
1989 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001990 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001991 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001992
1993 /* FDI must be feeding us bits for PCH ports */
1994 assert_fdi_tx_enabled(dev_priv, pipe);
1995 assert_fdi_rx_enabled(dev_priv, pipe);
1996
Daniel Vetter23670b322012-11-01 09:15:30 +01001997 if (HAS_PCH_CPT(dev)) {
1998 /* Workaround: Set the timing override bit before enabling the
1999 * pch transcoder. */
2000 reg = TRANS_CHICKEN2(pipe);
2001 val = I915_READ(reg);
2002 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
2003 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03002004 }
Daniel Vetter23670b322012-11-01 09:15:30 +01002005
Daniel Vetterab9412b2013-05-03 11:49:46 +02002006 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002007 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002008 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002009
2010 if (HAS_PCH_IBX(dev_priv->dev)) {
2011 /*
2012 * make the BPC in transcoder be consistent with
2013 * that in pipeconf reg.
2014 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002015 val &= ~PIPECONF_BPC_MASK;
2016 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002017 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002018
2019 val &= ~TRANS_INTERLACE_MASK;
2020 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002021 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002022 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002023 val |= TRANS_LEGACY_INTERLACED_ILK;
2024 else
2025 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002026 else
2027 val |= TRANS_PROGRESSIVE;
2028
Jesse Barnes040484a2011-01-03 12:14:26 -08002029 I915_WRITE(reg, val | TRANS_ENABLE);
2030 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002031 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002032}
2033
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002034static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002035 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002036{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002037 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002038
2039 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002040 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002041
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002042 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002043 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002044 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002045
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002046 /* Workaround: set timing override bit. */
2047 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002048 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002049 I915_WRITE(_TRANSA_CHICKEN2, val);
2050
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002051 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002052 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002053
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002054 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2055 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002056 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002057 else
2058 val |= TRANS_PROGRESSIVE;
2059
Daniel Vetterab9412b2013-05-03 11:49:46 +02002060 I915_WRITE(LPT_TRANSCONF, val);
2061 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002062 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002063}
2064
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002065static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2066 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002067{
Daniel Vetter23670b322012-11-01 09:15:30 +01002068 struct drm_device *dev = dev_priv->dev;
2069 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002070
2071 /* FDI relies on the transcoder */
2072 assert_fdi_tx_disabled(dev_priv, pipe);
2073 assert_fdi_rx_disabled(dev_priv, pipe);
2074
Jesse Barnes291906f2011-02-02 12:28:03 -08002075 /* Ports must be off as well */
2076 assert_pch_ports_disabled(dev_priv, pipe);
2077
Daniel Vetterab9412b2013-05-03 11:49:46 +02002078 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002079 val = I915_READ(reg);
2080 val &= ~TRANS_ENABLE;
2081 I915_WRITE(reg, val);
2082 /* wait for PCH transcoder off, transcoder state */
2083 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002084 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002085
2086 if (!HAS_PCH_IBX(dev)) {
2087 /* Workaround: Clear the timing override chicken bit again. */
2088 reg = TRANS_CHICKEN2(pipe);
2089 val = I915_READ(reg);
2090 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2091 I915_WRITE(reg, val);
2092 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002093}
2094
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002095static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002096{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002097 u32 val;
2098
Daniel Vetterab9412b2013-05-03 11:49:46 +02002099 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002100 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002101 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002102 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002103 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002104 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002105
2106 /* Workaround: clear timing override bit. */
2107 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002108 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002109 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002110}
2111
2112/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002113 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002114 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002115 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002116 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002117 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002118 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002119static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002120{
Paulo Zanoni03722642014-01-17 13:51:09 -02002121 struct drm_device *dev = crtc->base.dev;
2122 struct drm_i915_private *dev_priv = dev->dev_private;
2123 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002124 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2125 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002126 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002127 int reg;
2128 u32 val;
2129
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002130 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002131 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002132 assert_sprites_disabled(dev_priv, pipe);
2133
Paulo Zanoni681e5812012-12-06 11:12:38 -02002134 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002135 pch_transcoder = TRANSCODER_A;
2136 else
2137 pch_transcoder = pipe;
2138
Jesse Barnesb24e7172011-01-04 15:09:30 -08002139 /*
2140 * A pipe without a PLL won't actually be able to drive bits from
2141 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2142 * need the check.
2143 */
Imre Deak50360402015-01-16 00:55:16 -08002144 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002145 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002146 assert_dsi_pll_enabled(dev_priv);
2147 else
2148 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002149 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002150 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002151 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002152 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002153 assert_fdi_tx_pll_enabled(dev_priv,
2154 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002155 }
2156 /* FIXME: assert CPU port conditions for SNB+ */
2157 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002158
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002159 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002160 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002161 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002162 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2163 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002164 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002165 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002166
2167 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002168 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002169}
2170
2171/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002172 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002173 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002174 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002175 * Disable the pipe of @crtc, making sure that various hardware
2176 * specific requirements are met, if applicable, e.g. plane
2177 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002178 *
2179 * Will wait until the pipe has shut down before returning.
2180 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002181static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002182{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002183 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002184 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002185 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002186 int reg;
2187 u32 val;
2188
2189 /*
2190 * Make sure planes won't keep trying to pump pixels to us,
2191 * or we might hang the display.
2192 */
2193 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002194 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002195 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002196
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002197 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002198 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002199 if ((val & PIPECONF_ENABLE) == 0)
2200 return;
2201
Ville Syrjälä67adc642014-08-15 01:21:57 +03002202 /*
2203 * Double wide has implications for planes
2204 * so best keep it disabled when not needed.
2205 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002206 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002207 val &= ~PIPECONF_DOUBLE_WIDE;
2208
2209 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002210 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2211 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002212 val &= ~PIPECONF_ENABLE;
2213
2214 I915_WRITE(reg, val);
2215 if ((val & PIPECONF_ENABLE) == 0)
2216 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002217}
2218
2219/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002220 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002221 * @plane: plane to be enabled
2222 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002223 *
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002224 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002225 */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002226static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2227 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002228{
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002229 struct drm_device *dev = plane->dev;
2230 struct drm_i915_private *dev_priv = dev->dev_private;
2231 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002232
2233 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002234 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002235 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002236
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002237 dev_priv->display.update_primary_plane(crtc, plane->fb,
2238 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002239}
2240
Chris Wilson693db182013-03-05 14:52:39 +00002241static bool need_vtd_wa(struct drm_device *dev)
2242{
2243#ifdef CONFIG_INTEL_IOMMU
2244 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2245 return true;
2246#endif
2247 return false;
2248}
2249
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002250unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002251intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2252 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002253{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002254 unsigned int tile_height;
2255 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002256
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002257 switch (fb_format_modifier) {
2258 case DRM_FORMAT_MOD_NONE:
2259 tile_height = 1;
2260 break;
2261 case I915_FORMAT_MOD_X_TILED:
2262 tile_height = IS_GEN2(dev) ? 16 : 8;
2263 break;
2264 case I915_FORMAT_MOD_Y_TILED:
2265 tile_height = 32;
2266 break;
2267 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002268 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2269 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002270 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002271 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002272 tile_height = 64;
2273 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002274 case 2:
2275 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002276 tile_height = 32;
2277 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002278 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002279 tile_height = 16;
2280 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002281 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002282 WARN_ONCE(1,
2283 "128-bit pixels are not supported for display!");
2284 tile_height = 16;
2285 break;
2286 }
2287 break;
2288 default:
2289 MISSING_CASE(fb_format_modifier);
2290 tile_height = 1;
2291 break;
2292 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002293
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002294 return tile_height;
2295}
2296
2297unsigned int
2298intel_fb_align_height(struct drm_device *dev, unsigned int height,
2299 uint32_t pixel_format, uint64_t fb_format_modifier)
2300{
2301 return ALIGN(height, intel_tile_height(dev, pixel_format,
2302 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002303}
2304
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002305static int
2306intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2307 const struct drm_plane_state *plane_state)
2308{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002309 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002310
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002311 *view = i915_ggtt_view_normal;
2312
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002313 if (!plane_state)
2314 return 0;
2315
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002316 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002317 return 0;
2318
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002319 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002320
2321 info->height = fb->height;
2322 info->pixel_format = fb->pixel_format;
2323 info->pitch = fb->pitches[0];
2324 info->fb_modifier = fb->modifier[0];
2325
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002326 return 0;
2327}
2328
Chris Wilson127bd2a2010-07-23 23:32:05 +01002329int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002330intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2331 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002332 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002333 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002334{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002335 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002336 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002337 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002338 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002339 u32 alignment;
2340 int ret;
2341
Matt Roperebcdd392014-07-09 16:22:11 -07002342 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2343
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002344 switch (fb->modifier[0]) {
2345 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002346 if (INTEL_INFO(dev)->gen >= 9)
2347 alignment = 256 * 1024;
2348 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002349 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002350 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002351 alignment = 4 * 1024;
2352 else
2353 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002354 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002355 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002356 if (INTEL_INFO(dev)->gen >= 9)
2357 alignment = 256 * 1024;
2358 else {
2359 /* pin() will align the object as required by fence */
2360 alignment = 0;
2361 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002362 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002363 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002364 case I915_FORMAT_MOD_Yf_TILED:
2365 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2366 "Y tiling bo slipped through, driver bug!\n"))
2367 return -EINVAL;
2368 alignment = 1 * 1024 * 1024;
2369 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002370 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002371 MISSING_CASE(fb->modifier[0]);
2372 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002373 }
2374
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002375 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2376 if (ret)
2377 return ret;
2378
Chris Wilson693db182013-03-05 14:52:39 +00002379 /* Note that the w/a also requires 64 PTE of padding following the
2380 * bo. We currently fill all unused PTE with the shadow page and so
2381 * we should always have valid PTE following the scanout preventing
2382 * the VT-d warning.
2383 */
2384 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2385 alignment = 256 * 1024;
2386
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002387 /*
2388 * Global gtt pte registers are special registers which actually forward
2389 * writes to a chunk of system memory. Which means that there is no risk
2390 * that the register values disappear as soon as we call
2391 * intel_runtime_pm_put(), so it is correct to wrap only the
2392 * pin/unpin/fence and not more.
2393 */
2394 intel_runtime_pm_get(dev_priv);
2395
Chris Wilsonce453d82011-02-21 14:43:56 +00002396 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002397 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002398 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002399 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002400 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002401
2402 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2403 * fence, whereas 965+ only requires a fence if using
2404 * framebuffer compression. For simplicity, we always install
2405 * a fence as the cost is not that onerous.
2406 */
Chris Wilson06d98132012-04-17 15:31:24 +01002407 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002408 if (ret)
2409 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002410
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002411 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002412
Chris Wilsonce453d82011-02-21 14:43:56 +00002413 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002414 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002415 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002416
2417err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002418 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002419err_interruptible:
2420 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002421 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002422 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002423}
2424
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002425static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2426 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002427{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002428 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002429 struct i915_ggtt_view view;
2430 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002431
Matt Roperebcdd392014-07-09 16:22:11 -07002432 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2433
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002434 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2435 WARN_ONCE(ret, "Couldn't get view from plane state!");
2436
Chris Wilson1690e1e2011-12-14 13:57:08 +01002437 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002438 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002439}
2440
Daniel Vetterc2c75132012-07-05 12:17:30 +02002441/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2442 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002443unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2444 unsigned int tiling_mode,
2445 unsigned int cpp,
2446 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002447{
Chris Wilsonbc752862013-02-21 20:04:31 +00002448 if (tiling_mode != I915_TILING_NONE) {
2449 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002450
Chris Wilsonbc752862013-02-21 20:04:31 +00002451 tile_rows = *y / 8;
2452 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002453
Chris Wilsonbc752862013-02-21 20:04:31 +00002454 tiles = *x / (512/cpp);
2455 *x %= 512/cpp;
2456
2457 return tile_rows * pitch * 8 + tiles * 4096;
2458 } else {
2459 unsigned int offset;
2460
2461 offset = *y * pitch + *x * cpp;
2462 *y = 0;
2463 *x = (offset & 4095) / cpp;
2464 return offset & -4096;
2465 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002466}
2467
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002468static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002469{
2470 switch (format) {
2471 case DISPPLANE_8BPP:
2472 return DRM_FORMAT_C8;
2473 case DISPPLANE_BGRX555:
2474 return DRM_FORMAT_XRGB1555;
2475 case DISPPLANE_BGRX565:
2476 return DRM_FORMAT_RGB565;
2477 default:
2478 case DISPPLANE_BGRX888:
2479 return DRM_FORMAT_XRGB8888;
2480 case DISPPLANE_RGBX888:
2481 return DRM_FORMAT_XBGR8888;
2482 case DISPPLANE_BGRX101010:
2483 return DRM_FORMAT_XRGB2101010;
2484 case DISPPLANE_RGBX101010:
2485 return DRM_FORMAT_XBGR2101010;
2486 }
2487}
2488
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002489static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2490{
2491 switch (format) {
2492 case PLANE_CTL_FORMAT_RGB_565:
2493 return DRM_FORMAT_RGB565;
2494 default:
2495 case PLANE_CTL_FORMAT_XRGB_8888:
2496 if (rgb_order) {
2497 if (alpha)
2498 return DRM_FORMAT_ABGR8888;
2499 else
2500 return DRM_FORMAT_XBGR8888;
2501 } else {
2502 if (alpha)
2503 return DRM_FORMAT_ARGB8888;
2504 else
2505 return DRM_FORMAT_XRGB8888;
2506 }
2507 case PLANE_CTL_FORMAT_XRGB_2101010:
2508 if (rgb_order)
2509 return DRM_FORMAT_XBGR2101010;
2510 else
2511 return DRM_FORMAT_XRGB2101010;
2512 }
2513}
2514
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002515static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002516intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2517 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002518{
2519 struct drm_device *dev = crtc->base.dev;
2520 struct drm_i915_gem_object *obj = NULL;
2521 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002522 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002523 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2524 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2525 PAGE_SIZE);
2526
2527 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002528
Chris Wilsonff2652e2014-03-10 08:07:02 +00002529 if (plane_config->size == 0)
2530 return false;
2531
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002532 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2533 base_aligned,
2534 base_aligned,
2535 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002537 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002538
Damien Lespiau49af4492015-01-20 12:51:44 +00002539 obj->tiling_mode = plane_config->tiling;
2540 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002541 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002542
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002543 mode_cmd.pixel_format = fb->pixel_format;
2544 mode_cmd.width = fb->width;
2545 mode_cmd.height = fb->height;
2546 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002547 mode_cmd.modifier[0] = fb->modifier[0];
2548 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002549
2550 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002551 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002552 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002553 DRM_DEBUG_KMS("intel fb init failed\n");
2554 goto out_unref_obj;
2555 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002556 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002557
Daniel Vetterf6936e22015-03-26 12:17:05 +01002558 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002559 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002560
2561out_unref_obj:
2562 drm_gem_object_unreference(&obj->base);
2563 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002564 return false;
2565}
2566
Matt Roperafd65eb2015-02-03 13:10:04 -08002567/* Update plane->state->fb to match plane->fb after driver-internal updates */
2568static void
2569update_state_fb(struct drm_plane *plane)
2570{
2571 if (plane->fb == plane->state->fb)
2572 return;
2573
2574 if (plane->state->fb)
2575 drm_framebuffer_unreference(plane->state->fb);
2576 plane->state->fb = plane->fb;
2577 if (plane->state->fb)
2578 drm_framebuffer_reference(plane->state->fb);
2579}
2580
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002581static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002582intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2583 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002584{
2585 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002586 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002587 struct drm_crtc *c;
2588 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002589 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002590 struct drm_plane *primary = intel_crtc->base.primary;
2591 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002592
Damien Lespiau2d140302015-02-05 17:22:18 +00002593 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594 return;
2595
Daniel Vetterf6936e22015-03-26 12:17:05 +01002596 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002597 fb = &plane_config->fb->base;
2598 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002599 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002600
Damien Lespiau2d140302015-02-05 17:22:18 +00002601 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602
2603 /*
2604 * Failed to alloc the obj, check to see if we should share
2605 * an fb with another CRTC instead
2606 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002607 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002608 i = to_intel_crtc(c);
2609
2610 if (c == &intel_crtc->base)
2611 continue;
2612
Matt Roper2ff8fde2014-07-08 07:50:07 -07002613 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002614 continue;
2615
Daniel Vetter88595ac2015-03-26 12:42:24 +01002616 fb = c->primary->fb;
2617 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002618 continue;
2619
Daniel Vetter88595ac2015-03-26 12:42:24 +01002620 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002621 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002622 drm_framebuffer_reference(fb);
2623 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002624 }
2625 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002626
2627 return;
2628
2629valid_fb:
2630 obj = intel_fb_obj(fb);
2631 if (obj->tiling_mode != I915_TILING_NONE)
2632 dev_priv->preserve_bios_swizzle = true;
2633
2634 primary->fb = fb;
2635 primary->state->crtc = &intel_crtc->base;
2636 primary->crtc = &intel_crtc->base;
2637 update_state_fb(primary);
2638 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002639}
2640
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002641static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2642 struct drm_framebuffer *fb,
2643 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002644{
2645 struct drm_device *dev = crtc->dev;
2646 struct drm_i915_private *dev_priv = dev->dev_private;
2647 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002648 struct drm_plane *primary = crtc->primary;
2649 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002650 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002651 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002652 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002653 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002654 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302655 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002656
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002657 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002658 I915_WRITE(reg, 0);
2659 if (INTEL_INFO(dev)->gen >= 4)
2660 I915_WRITE(DSPSURF(plane), 0);
2661 else
2662 I915_WRITE(DSPADDR(plane), 0);
2663 POSTING_READ(reg);
2664 return;
2665 }
2666
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002667 obj = intel_fb_obj(fb);
2668 if (WARN_ON(obj == NULL))
2669 return;
2670
2671 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2672
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002673 dspcntr = DISPPLANE_GAMMA_ENABLE;
2674
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002675 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002676
2677 if (INTEL_INFO(dev)->gen < 4) {
2678 if (intel_crtc->pipe == PIPE_B)
2679 dspcntr |= DISPPLANE_SEL_PIPE_B;
2680
2681 /* pipesrc and dspsize control the size that is scaled from,
2682 * which should always be the user's requested size.
2683 */
2684 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002685 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2686 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002687 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002688 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2689 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002690 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2691 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002692 I915_WRITE(PRIMPOS(plane), 0);
2693 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002694 }
2695
Ville Syrjälä57779d02012-10-31 17:50:14 +02002696 switch (fb->pixel_format) {
2697 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002698 dspcntr |= DISPPLANE_8BPP;
2699 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002700 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002702 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002703 case DRM_FORMAT_RGB565:
2704 dspcntr |= DISPPLANE_BGRX565;
2705 break;
2706 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 dspcntr |= DISPPLANE_BGRX888;
2708 break;
2709 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002710 dspcntr |= DISPPLANE_RGBX888;
2711 break;
2712 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002713 dspcntr |= DISPPLANE_BGRX101010;
2714 break;
2715 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002716 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002717 break;
2718 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002719 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002720 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002721
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002722 if (INTEL_INFO(dev)->gen >= 4 &&
2723 obj->tiling_mode != I915_TILING_NONE)
2724 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002725
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002726 if (IS_G4X(dev))
2727 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2728
Ville Syrjäläb98971272014-08-27 16:51:22 +03002729 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002730
Daniel Vetterc2c75132012-07-05 12:17:30 +02002731 if (INTEL_INFO(dev)->gen >= 4) {
2732 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002733 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002734 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002735 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002736 linear_offset -= intel_crtc->dspaddr_offset;
2737 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002738 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002739 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002740
Matt Roper8e7d6882015-01-21 16:35:41 -08002741 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302742 dspcntr |= DISPPLANE_ROTATE_180;
2743
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002744 x += (intel_crtc->config->pipe_src_w - 1);
2745 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302746
2747 /* Finding the last pixel of the last line of the display
2748 data and adding to linear_offset*/
2749 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002750 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2751 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302752 }
2753
2754 I915_WRITE(reg, dspcntr);
2755
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002756 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002757 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002758 I915_WRITE(DSPSURF(plane),
2759 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002760 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002761 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002762 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002763 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002764 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002765}
2766
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002767static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2768 struct drm_framebuffer *fb,
2769 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002770{
2771 struct drm_device *dev = crtc->dev;
2772 struct drm_i915_private *dev_priv = dev->dev_private;
2773 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002774 struct drm_plane *primary = crtc->primary;
2775 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002776 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002777 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002778 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002779 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002780 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302781 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002782
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002783 if (!visible || !fb) {
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002784 I915_WRITE(reg, 0);
2785 I915_WRITE(DSPSURF(plane), 0);
2786 POSTING_READ(reg);
2787 return;
2788 }
2789
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002790 obj = intel_fb_obj(fb);
2791 if (WARN_ON(obj == NULL))
2792 return;
2793
2794 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2795
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002796 dspcntr = DISPPLANE_GAMMA_ENABLE;
2797
Ville Syrjäläfdd508a2014-08-08 21:51:11 +03002798 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002799
2800 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2801 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2802
Ville Syrjälä57779d02012-10-31 17:50:14 +02002803 switch (fb->pixel_format) {
2804 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002805 dspcntr |= DISPPLANE_8BPP;
2806 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002807 case DRM_FORMAT_RGB565:
2808 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002809 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002810 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 dspcntr |= DISPPLANE_BGRX888;
2812 break;
2813 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 dspcntr |= DISPPLANE_RGBX888;
2815 break;
2816 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002817 dspcntr |= DISPPLANE_BGRX101010;
2818 break;
2819 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002820 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002821 break;
2822 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002823 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002824 }
2825
2826 if (obj->tiling_mode != I915_TILING_NONE)
2827 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002828
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002829 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002830 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002831
Ville Syrjäläb98971272014-08-27 16:51:22 +03002832 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002833 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002834 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002835 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002836 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002837 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002838 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302839 dspcntr |= DISPPLANE_ROTATE_180;
2840
2841 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002842 x += (intel_crtc->config->pipe_src_w - 1);
2843 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302844
2845 /* Finding the last pixel of the last line of the display
2846 data and adding to linear_offset*/
2847 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002848 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2849 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302850 }
2851 }
2852
2853 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002854
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002855 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002856 I915_WRITE(DSPSURF(plane),
2857 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002858 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002859 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2860 } else {
2861 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2862 I915_WRITE(DSPLINOFF(plane), linear_offset);
2863 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002864 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002865}
2866
Damien Lespiaub3218032015-02-27 11:15:18 +00002867u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2868 uint32_t pixel_format)
2869{
2870 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2871
2872 /*
2873 * The stride is either expressed as a multiple of 64 bytes
2874 * chunks for linear buffers or in number of tiles for tiled
2875 * buffers.
2876 */
2877 switch (fb_modifier) {
2878 case DRM_FORMAT_MOD_NONE:
2879 return 64;
2880 case I915_FORMAT_MOD_X_TILED:
2881 if (INTEL_INFO(dev)->gen == 2)
2882 return 128;
2883 return 512;
2884 case I915_FORMAT_MOD_Y_TILED:
2885 /* No need to check for old gens and Y tiling since this is
2886 * about the display engine and those will be blocked before
2887 * we get here.
2888 */
2889 return 128;
2890 case I915_FORMAT_MOD_Yf_TILED:
2891 if (bits_per_pixel == 8)
2892 return 64;
2893 else
2894 return 128;
2895 default:
2896 MISSING_CASE(fb_modifier);
2897 return 64;
2898 }
2899}
2900
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002901unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2902 struct drm_i915_gem_object *obj)
2903{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002904 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002905
2906 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002907 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002908
2909 return i915_gem_obj_ggtt_offset_view(obj, view);
2910}
2911
Chandra Kondurua1b22782015-04-07 15:28:45 -07002912/*
2913 * This function detaches (aka. unbinds) unused scalers in hardware
2914 */
2915void skl_detach_scalers(struct intel_crtc *intel_crtc)
2916{
2917 struct drm_device *dev;
2918 struct drm_i915_private *dev_priv;
2919 struct intel_crtc_scaler_state *scaler_state;
2920 int i;
2921
2922 if (!intel_crtc || !intel_crtc->config)
2923 return;
2924
2925 dev = intel_crtc->base.dev;
2926 dev_priv = dev->dev_private;
2927 scaler_state = &intel_crtc->config->scaler_state;
2928
2929 /* loop through and disable scalers that aren't in use */
2930 for (i = 0; i < intel_crtc->num_scalers; i++) {
2931 if (!scaler_state->scalers[i].in_use) {
2932 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2933 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2934 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2935 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2936 intel_crtc->base.base.id, intel_crtc->pipe, i);
2937 }
2938 }
2939}
2940
Chandra Konduru6156a452015-04-27 13:48:39 -07002941u32 skl_plane_ctl_format(uint32_t pixel_format)
2942{
Chandra Konduru6156a452015-04-27 13:48:39 -07002943 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002944 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002947 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002948 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002949 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002950 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002951 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002952 /*
2953 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2954 * to be already pre-multiplied. We need to add a knob (or a different
2955 * DRM_FORMAT) for user-space to configure that.
2956 */
2957 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002960 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002961 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002962 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002970 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002972 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002973 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002974 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002975 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002976 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002977 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002978
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002979 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002980}
2981
2982u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2983{
Chandra Konduru6156a452015-04-27 13:48:39 -07002984 switch (fb_modifier) {
2985 case DRM_FORMAT_MOD_NONE:
2986 break;
2987 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002988 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002989 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002990 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002991 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002992 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002993 default:
2994 MISSING_CASE(fb_modifier);
2995 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002996
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002997 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002998}
2999
3000u32 skl_plane_ctl_rotation(unsigned int rotation)
3001{
Chandra Konduru6156a452015-04-27 13:48:39 -07003002 switch (rotation) {
3003 case BIT(DRM_ROTATE_0):
3004 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05303005 /*
3006 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3007 * while i915 HW rotation is clockwise, thats why this swapping.
3008 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303010 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003011 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003012 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003013 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303014 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003015 default:
3016 MISSING_CASE(rotation);
3017 }
3018
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003019 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003020}
3021
Damien Lespiau70d21f02013-07-03 21:06:04 +01003022static void skylake_update_primary_plane(struct drm_crtc *crtc,
3023 struct drm_framebuffer *fb,
3024 int x, int y)
3025{
3026 struct drm_device *dev = crtc->dev;
3027 struct drm_i915_private *dev_priv = dev->dev_private;
3028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003029 struct drm_plane *plane = crtc->primary;
3030 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003031 struct drm_i915_gem_object *obj;
3032 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303033 u32 plane_ctl, stride_div, stride;
3034 u32 tile_height, plane_offset, plane_size;
3035 unsigned int rotation;
3036 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003037 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 struct intel_crtc_state *crtc_state = intel_crtc->config;
3039 struct intel_plane_state *plane_state;
3040 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3041 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3042 int scaler_id = -1;
3043
Chandra Konduru6156a452015-04-27 13:48:39 -07003044 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003045
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003046 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003047 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3048 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3049 POSTING_READ(PLANE_CTL(pipe, 0));
3050 return;
3051 }
3052
3053 plane_ctl = PLANE_CTL_ENABLE |
3054 PLANE_CTL_PIPE_GAMMA_ENABLE |
3055 PLANE_CTL_PIPE_CSC_ENABLE;
3056
Chandra Konduru6156a452015-04-27 13:48:39 -07003057 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3058 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003059 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303060
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303061 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003062 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003063
Damien Lespiaub3218032015-02-27 11:15:18 +00003064 obj = intel_fb_obj(fb);
3065 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3066 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303067 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3068
Chandra Konduru6156a452015-04-27 13:48:39 -07003069 /*
3070 * FIXME: intel_plane_state->src, dst aren't set when transitional
3071 * update_plane helpers are called from legacy paths.
3072 * Once full atomic crtc is available, below check can be avoided.
3073 */
3074 if (drm_rect_width(&plane_state->src)) {
3075 scaler_id = plane_state->scaler_id;
3076 src_x = plane_state->src.x1 >> 16;
3077 src_y = plane_state->src.y1 >> 16;
3078 src_w = drm_rect_width(&plane_state->src) >> 16;
3079 src_h = drm_rect_height(&plane_state->src) >> 16;
3080 dst_x = plane_state->dst.x1;
3081 dst_y = plane_state->dst.y1;
3082 dst_w = drm_rect_width(&plane_state->dst);
3083 dst_h = drm_rect_height(&plane_state->dst);
3084
3085 WARN_ON(x != src_x || y != src_y);
3086 } else {
3087 src_w = intel_crtc->config->pipe_src_w;
3088 src_h = intel_crtc->config->pipe_src_h;
3089 }
3090
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303091 if (intel_rotation_90_or_270(rotation)) {
3092 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003093 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303094 fb->modifier[0]);
3095 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003096 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303097 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003098 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303099 } else {
3100 stride = fb->pitches[0] / stride_div;
3101 x_offset = x;
3102 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003103 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303104 }
3105 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003106
Damien Lespiau70d21f02013-07-03 21:06:04 +01003107 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303108 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3109 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3110 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003111
3112 if (scaler_id >= 0) {
3113 uint32_t ps_ctrl = 0;
3114
3115 WARN_ON(!dst_w || !dst_h);
3116 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3117 crtc_state->scaler_state.scalers[scaler_id].mode;
3118 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3119 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3120 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3121 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3122 I915_WRITE(PLANE_POS(pipe, 0), 0);
3123 } else {
3124 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3125 }
3126
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003127 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003128
3129 POSTING_READ(PLANE_SURF(pipe, 0));
3130}
3131
Jesse Barnes17638cd2011-06-24 12:19:23 -07003132/* Assume fb object is pinned & idle & fenced and just update base pointers */
3133static int
3134intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3135 int x, int y, enum mode_set_atomic state)
3136{
3137 struct drm_device *dev = crtc->dev;
3138 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003139
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003140 if (dev_priv->display.disable_fbc)
3141 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003142
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003143 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3144
3145 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003146}
3147
Ville Syrjälä75147472014-11-24 18:28:11 +02003148static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003149{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003150 struct drm_crtc *crtc;
3151
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003152 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003153 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3154 enum plane plane = intel_crtc->plane;
3155
3156 intel_prepare_page_flip(dev, plane);
3157 intel_finish_page_flip_plane(dev, plane);
3158 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003159}
3160
3161static void intel_update_primary_planes(struct drm_device *dev)
3162{
3163 struct drm_i915_private *dev_priv = dev->dev_private;
3164 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003165
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003166 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003167 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3168
Rob Clark51fd3712013-11-19 12:10:12 -05003169 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003170 /*
3171 * FIXME: Once we have proper support for primary planes (and
3172 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003173 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003174 */
Matt Roperf4510a22014-04-01 15:22:40 -07003175 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003176 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003177 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003178 crtc->x,
3179 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003180 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003181 }
3182}
3183
Ville Syrjälä75147472014-11-24 18:28:11 +02003184void intel_prepare_reset(struct drm_device *dev)
3185{
3186 /* no reset support for gen2 */
3187 if (IS_GEN2(dev))
3188 return;
3189
3190 /* reset doesn't touch the display */
3191 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3192 return;
3193
3194 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003195 /*
3196 * Disabling the crtcs gracefully seems nicer. Also the
3197 * g33 docs say we should at least disable all the planes.
3198 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003199 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003200}
3201
3202void intel_finish_reset(struct drm_device *dev)
3203{
3204 struct drm_i915_private *dev_priv = to_i915(dev);
3205
3206 /*
3207 * Flips in the rings will be nuked by the reset,
3208 * so complete all pending flips so that user space
3209 * will get its events and not get stuck.
3210 */
3211 intel_complete_page_flips(dev);
3212
3213 /* no reset support for gen2 */
3214 if (IS_GEN2(dev))
3215 return;
3216
3217 /* reset doesn't touch the display */
3218 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3219 /*
3220 * Flips in the rings have been nuked by the reset,
3221 * so update the base address of all primary
3222 * planes to the the last fb to make sure we're
3223 * showing the correct fb after a reset.
3224 */
3225 intel_update_primary_planes(dev);
3226 return;
3227 }
3228
3229 /*
3230 * The display has been reset as well,
3231 * so need a full re-initialization.
3232 */
3233 intel_runtime_pm_disable_interrupts(dev_priv);
3234 intel_runtime_pm_enable_interrupts(dev_priv);
3235
3236 intel_modeset_init_hw(dev);
3237
3238 spin_lock_irq(&dev_priv->irq_lock);
3239 if (dev_priv->display.hpd_irq_setup)
3240 dev_priv->display.hpd_irq_setup(dev);
3241 spin_unlock_irq(&dev_priv->irq_lock);
3242
3243 intel_modeset_setup_hw_state(dev, true);
3244
3245 intel_hpd_init(dev_priv);
3246
3247 drm_modeset_unlock_all(dev);
3248}
3249
Chris Wilson2e2f3512015-04-27 13:41:14 +01003250static void
Chris Wilson14667a42012-04-03 17:58:35 +01003251intel_finish_fb(struct drm_framebuffer *old_fb)
3252{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003253 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003254 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003255 bool was_interruptible = dev_priv->mm.interruptible;
3256 int ret;
3257
Chris Wilson14667a42012-04-03 17:58:35 +01003258 /* Big Hammer, we also need to ensure that any pending
3259 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3260 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003261 * framebuffer. Note that we rely on userspace rendering
3262 * into the buffer attached to the pipe they are waiting
3263 * on. If not, userspace generates a GPU hang with IPEHR
3264 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003265 *
3266 * This should only fail upon a hung GPU, in which case we
3267 * can safely continue.
3268 */
3269 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003270 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003271 dev_priv->mm.interruptible = was_interruptible;
3272
Chris Wilson2e2f3512015-04-27 13:41:14 +01003273 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003274}
3275
Chris Wilson7d5e3792014-03-04 13:15:08 +00003276static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3277{
3278 struct drm_device *dev = crtc->dev;
3279 struct drm_i915_private *dev_priv = dev->dev_private;
3280 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003281 bool pending;
3282
3283 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3284 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3285 return false;
3286
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003287 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003288 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003289 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003290
3291 return pending;
3292}
3293
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003294static void intel_update_pipe_size(struct intel_crtc *crtc)
3295{
3296 struct drm_device *dev = crtc->base.dev;
3297 struct drm_i915_private *dev_priv = dev->dev_private;
3298 const struct drm_display_mode *adjusted_mode;
3299
3300 if (!i915.fastboot)
3301 return;
3302
3303 /*
3304 * Update pipe size and adjust fitter if needed: the reason for this is
3305 * that in compute_mode_changes we check the native mode (not the pfit
3306 * mode) to see if we can flip rather than do a full mode set. In the
3307 * fastboot case, we'll flip, but if we don't update the pipesrc and
3308 * pfit state, we'll end up with a big fb scanned out into the wrong
3309 * sized surface.
3310 *
3311 * To fix this properly, we need to hoist the checks up into
3312 * compute_mode_changes (or above), check the actual pfit state and
3313 * whether the platform allows pfit disable with pipe active, and only
3314 * then update the pipesrc and pfit state, even on the flip path.
3315 */
3316
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003317 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003318
3319 I915_WRITE(PIPESRC(crtc->pipe),
3320 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3321 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003322 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003323 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3324 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003325 I915_WRITE(PF_CTL(crtc->pipe), 0);
3326 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3327 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3328 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003329 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3330 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003331}
3332
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003333static void intel_fdi_normal_train(struct drm_crtc *crtc)
3334{
3335 struct drm_device *dev = crtc->dev;
3336 struct drm_i915_private *dev_priv = dev->dev_private;
3337 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3338 int pipe = intel_crtc->pipe;
3339 u32 reg, temp;
3340
3341 /* enable normal train */
3342 reg = FDI_TX_CTL(pipe);
3343 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003344 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003345 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3346 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003347 } else {
3348 temp &= ~FDI_LINK_TRAIN_NONE;
3349 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003350 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003351 I915_WRITE(reg, temp);
3352
3353 reg = FDI_RX_CTL(pipe);
3354 temp = I915_READ(reg);
3355 if (HAS_PCH_CPT(dev)) {
3356 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3357 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3358 } else {
3359 temp &= ~FDI_LINK_TRAIN_NONE;
3360 temp |= FDI_LINK_TRAIN_NONE;
3361 }
3362 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3363
3364 /* wait one idle pattern time */
3365 POSTING_READ(reg);
3366 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003367
3368 /* IVB wants error correction enabled */
3369 if (IS_IVYBRIDGE(dev))
3370 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3371 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003372}
3373
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003374/* The FDI link training functions for ILK/Ibexpeak. */
3375static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3376{
3377 struct drm_device *dev = crtc->dev;
3378 struct drm_i915_private *dev_priv = dev->dev_private;
3379 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3380 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003381 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003383 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003384 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003385
Adam Jacksone1a44742010-06-25 15:32:14 -04003386 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3387 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003388 reg = FDI_RX_IMR(pipe);
3389 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003390 temp &= ~FDI_RX_SYMBOL_LOCK;
3391 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003392 I915_WRITE(reg, temp);
3393 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003394 udelay(150);
3395
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003396 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003397 reg = FDI_TX_CTL(pipe);
3398 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003399 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003400 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003401 temp &= ~FDI_LINK_TRAIN_NONE;
3402 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003403 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003404
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 reg = FDI_RX_CTL(pipe);
3406 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003407 temp &= ~FDI_LINK_TRAIN_NONE;
3408 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003409 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3410
3411 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412 udelay(150);
3413
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003414 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003415 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3416 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3417 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003418
Chris Wilson5eddb702010-09-11 13:48:45 +01003419 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003420 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003421 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003422 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3423
3424 if ((temp & FDI_RX_BIT_LOCK)) {
3425 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003426 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003427 break;
3428 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003429 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003430 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003431 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003432
3433 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 reg = FDI_TX_CTL(pipe);
3435 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003436 temp &= ~FDI_LINK_TRAIN_NONE;
3437 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003438 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003439
Chris Wilson5eddb702010-09-11 13:48:45 +01003440 reg = FDI_RX_CTL(pipe);
3441 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003442 temp &= ~FDI_LINK_TRAIN_NONE;
3443 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003444 I915_WRITE(reg, temp);
3445
3446 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447 udelay(150);
3448
Chris Wilson5eddb702010-09-11 13:48:45 +01003449 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003450 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003451 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003452 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3453
3454 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003455 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003456 DRM_DEBUG_KMS("FDI train 2 done.\n");
3457 break;
3458 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003459 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003460 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003461 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003462
3463 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003464
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003465}
3466
Akshay Joshi0206e352011-08-16 15:34:10 -04003467static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003468 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3469 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3470 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3471 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3472};
3473
3474/* The FDI link training functions for SNB/Cougarpoint. */
3475static void gen6_fdi_link_train(struct drm_crtc *crtc)
3476{
3477 struct drm_device *dev = crtc->dev;
3478 struct drm_i915_private *dev_priv = dev->dev_private;
3479 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3480 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003481 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003482
Adam Jacksone1a44742010-06-25 15:32:14 -04003483 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3484 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003485 reg = FDI_RX_IMR(pipe);
3486 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003487 temp &= ~FDI_RX_SYMBOL_LOCK;
3488 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003489 I915_WRITE(reg, temp);
3490
3491 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003492 udelay(150);
3493
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003494 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003495 reg = FDI_TX_CTL(pipe);
3496 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003497 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003498 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003499 temp &= ~FDI_LINK_TRAIN_NONE;
3500 temp |= FDI_LINK_TRAIN_PATTERN_1;
3501 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3502 /* SNB-B */
3503 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003504 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003505
Daniel Vetterd74cf322012-10-26 10:58:13 +02003506 I915_WRITE(FDI_RX_MISC(pipe),
3507 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3508
Chris Wilson5eddb702010-09-11 13:48:45 +01003509 reg = FDI_RX_CTL(pipe);
3510 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003511 if (HAS_PCH_CPT(dev)) {
3512 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3513 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3514 } else {
3515 temp &= ~FDI_LINK_TRAIN_NONE;
3516 temp |= FDI_LINK_TRAIN_PATTERN_1;
3517 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003518 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3519
3520 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003521 udelay(150);
3522
Akshay Joshi0206e352011-08-16 15:34:10 -04003523 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003524 reg = FDI_TX_CTL(pipe);
3525 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003526 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3527 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003528 I915_WRITE(reg, temp);
3529
3530 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003531 udelay(500);
3532
Sean Paulfa37d392012-03-02 12:53:39 -05003533 for (retry = 0; retry < 5; retry++) {
3534 reg = FDI_RX_IIR(pipe);
3535 temp = I915_READ(reg);
3536 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3537 if (temp & FDI_RX_BIT_LOCK) {
3538 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3539 DRM_DEBUG_KMS("FDI train 1 done.\n");
3540 break;
3541 }
3542 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003543 }
Sean Paulfa37d392012-03-02 12:53:39 -05003544 if (retry < 5)
3545 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003546 }
3547 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003548 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003549
3550 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003551 reg = FDI_TX_CTL(pipe);
3552 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003553 temp &= ~FDI_LINK_TRAIN_NONE;
3554 temp |= FDI_LINK_TRAIN_PATTERN_2;
3555 if (IS_GEN6(dev)) {
3556 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3557 /* SNB-B */
3558 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3559 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003560 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003561
Chris Wilson5eddb702010-09-11 13:48:45 +01003562 reg = FDI_RX_CTL(pipe);
3563 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003564 if (HAS_PCH_CPT(dev)) {
3565 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3566 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3567 } else {
3568 temp &= ~FDI_LINK_TRAIN_NONE;
3569 temp |= FDI_LINK_TRAIN_PATTERN_2;
3570 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003571 I915_WRITE(reg, temp);
3572
3573 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003574 udelay(150);
3575
Akshay Joshi0206e352011-08-16 15:34:10 -04003576 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003577 reg = FDI_TX_CTL(pipe);
3578 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003579 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3580 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003581 I915_WRITE(reg, temp);
3582
3583 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003584 udelay(500);
3585
Sean Paulfa37d392012-03-02 12:53:39 -05003586 for (retry = 0; retry < 5; retry++) {
3587 reg = FDI_RX_IIR(pipe);
3588 temp = I915_READ(reg);
3589 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3590 if (temp & FDI_RX_SYMBOL_LOCK) {
3591 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3592 DRM_DEBUG_KMS("FDI train 2 done.\n");
3593 break;
3594 }
3595 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003596 }
Sean Paulfa37d392012-03-02 12:53:39 -05003597 if (retry < 5)
3598 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003599 }
3600 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003601 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003602
3603 DRM_DEBUG_KMS("FDI train done.\n");
3604}
3605
Jesse Barnes357555c2011-04-28 15:09:55 -07003606/* Manual link training for Ivy Bridge A0 parts */
3607static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3608{
3609 struct drm_device *dev = crtc->dev;
3610 struct drm_i915_private *dev_priv = dev->dev_private;
3611 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3612 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003613 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003614
3615 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3616 for train result */
3617 reg = FDI_RX_IMR(pipe);
3618 temp = I915_READ(reg);
3619 temp &= ~FDI_RX_SYMBOL_LOCK;
3620 temp &= ~FDI_RX_BIT_LOCK;
3621 I915_WRITE(reg, temp);
3622
3623 POSTING_READ(reg);
3624 udelay(150);
3625
Daniel Vetter01a415f2012-10-27 15:58:40 +02003626 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3627 I915_READ(FDI_RX_IIR(pipe)));
3628
Jesse Barnes139ccd32013-08-19 11:04:55 -07003629 /* Try each vswing and preemphasis setting twice before moving on */
3630 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3631 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003632 reg = FDI_TX_CTL(pipe);
3633 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003634 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3635 temp &= ~FDI_TX_ENABLE;
3636 I915_WRITE(reg, temp);
3637
3638 reg = FDI_RX_CTL(pipe);
3639 temp = I915_READ(reg);
3640 temp &= ~FDI_LINK_TRAIN_AUTO;
3641 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3642 temp &= ~FDI_RX_ENABLE;
3643 I915_WRITE(reg, temp);
3644
3645 /* enable CPU FDI TX and PCH FDI RX */
3646 reg = FDI_TX_CTL(pipe);
3647 temp = I915_READ(reg);
3648 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003649 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003650 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003651 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003652 temp |= snb_b_fdi_train_param[j/2];
3653 temp |= FDI_COMPOSITE_SYNC;
3654 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3655
3656 I915_WRITE(FDI_RX_MISC(pipe),
3657 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3658
3659 reg = FDI_RX_CTL(pipe);
3660 temp = I915_READ(reg);
3661 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3662 temp |= FDI_COMPOSITE_SYNC;
3663 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3664
3665 POSTING_READ(reg);
3666 udelay(1); /* should be 0.5us */
3667
3668 for (i = 0; i < 4; i++) {
3669 reg = FDI_RX_IIR(pipe);
3670 temp = I915_READ(reg);
3671 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3672
3673 if (temp & FDI_RX_BIT_LOCK ||
3674 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3675 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3676 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3677 i);
3678 break;
3679 }
3680 udelay(1); /* should be 0.5us */
3681 }
3682 if (i == 4) {
3683 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3684 continue;
3685 }
3686
3687 /* Train 2 */
3688 reg = FDI_TX_CTL(pipe);
3689 temp = I915_READ(reg);
3690 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3691 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3692 I915_WRITE(reg, temp);
3693
3694 reg = FDI_RX_CTL(pipe);
3695 temp = I915_READ(reg);
3696 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3697 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003698 I915_WRITE(reg, temp);
3699
3700 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003701 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003702
Jesse Barnes139ccd32013-08-19 11:04:55 -07003703 for (i = 0; i < 4; i++) {
3704 reg = FDI_RX_IIR(pipe);
3705 temp = I915_READ(reg);
3706 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003707
Jesse Barnes139ccd32013-08-19 11:04:55 -07003708 if (temp & FDI_RX_SYMBOL_LOCK ||
3709 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3710 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3711 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3712 i);
3713 goto train_done;
3714 }
3715 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003716 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003717 if (i == 4)
3718 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003719 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003720
Jesse Barnes139ccd32013-08-19 11:04:55 -07003721train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003722 DRM_DEBUG_KMS("FDI train done.\n");
3723}
3724
Daniel Vetter88cefb62012-08-12 19:27:14 +02003725static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003726{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003727 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003728 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003729 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003730 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003731
Jesse Barnesc64e3112010-09-10 11:27:03 -07003732
Jesse Barnes0e23b992010-09-10 11:10:00 -07003733 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003734 reg = FDI_RX_CTL(pipe);
3735 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003736 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003737 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003738 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003739 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3740
3741 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003742 udelay(200);
3743
3744 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003745 temp = I915_READ(reg);
3746 I915_WRITE(reg, temp | FDI_PCDCLK);
3747
3748 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003749 udelay(200);
3750
Paulo Zanoni20749732012-11-23 15:30:38 -02003751 /* Enable CPU FDI TX PLL, always on for Ironlake */
3752 reg = FDI_TX_CTL(pipe);
3753 temp = I915_READ(reg);
3754 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3755 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003756
Paulo Zanoni20749732012-11-23 15:30:38 -02003757 POSTING_READ(reg);
3758 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003759 }
3760}
3761
Daniel Vetter88cefb62012-08-12 19:27:14 +02003762static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3763{
3764 struct drm_device *dev = intel_crtc->base.dev;
3765 struct drm_i915_private *dev_priv = dev->dev_private;
3766 int pipe = intel_crtc->pipe;
3767 u32 reg, temp;
3768
3769 /* Switch from PCDclk to Rawclk */
3770 reg = FDI_RX_CTL(pipe);
3771 temp = I915_READ(reg);
3772 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3773
3774 /* Disable CPU FDI TX PLL */
3775 reg = FDI_TX_CTL(pipe);
3776 temp = I915_READ(reg);
3777 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3778
3779 POSTING_READ(reg);
3780 udelay(100);
3781
3782 reg = FDI_RX_CTL(pipe);
3783 temp = I915_READ(reg);
3784 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3785
3786 /* Wait for the clocks to turn off. */
3787 POSTING_READ(reg);
3788 udelay(100);
3789}
3790
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003791static void ironlake_fdi_disable(struct drm_crtc *crtc)
3792{
3793 struct drm_device *dev = crtc->dev;
3794 struct drm_i915_private *dev_priv = dev->dev_private;
3795 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3796 int pipe = intel_crtc->pipe;
3797 u32 reg, temp;
3798
3799 /* disable CPU FDI tx and PCH FDI rx */
3800 reg = FDI_TX_CTL(pipe);
3801 temp = I915_READ(reg);
3802 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3803 POSTING_READ(reg);
3804
3805 reg = FDI_RX_CTL(pipe);
3806 temp = I915_READ(reg);
3807 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003808 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003809 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3810
3811 POSTING_READ(reg);
3812 udelay(100);
3813
3814 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003815 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003816 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003817
3818 /* still set train pattern 1 */
3819 reg = FDI_TX_CTL(pipe);
3820 temp = I915_READ(reg);
3821 temp &= ~FDI_LINK_TRAIN_NONE;
3822 temp |= FDI_LINK_TRAIN_PATTERN_1;
3823 I915_WRITE(reg, temp);
3824
3825 reg = FDI_RX_CTL(pipe);
3826 temp = I915_READ(reg);
3827 if (HAS_PCH_CPT(dev)) {
3828 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3829 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3830 } else {
3831 temp &= ~FDI_LINK_TRAIN_NONE;
3832 temp |= FDI_LINK_TRAIN_PATTERN_1;
3833 }
3834 /* BPC in FDI rx is consistent with that in PIPECONF */
3835 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003836 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003837 I915_WRITE(reg, temp);
3838
3839 POSTING_READ(reg);
3840 udelay(100);
3841}
3842
Chris Wilson5dce5b932014-01-20 10:17:36 +00003843bool intel_has_pending_fb_unpin(struct drm_device *dev)
3844{
3845 struct intel_crtc *crtc;
3846
3847 /* Note that we don't need to be called with mode_config.lock here
3848 * as our list of CRTC objects is static for the lifetime of the
3849 * device and so cannot disappear as we iterate. Similarly, we can
3850 * happily treat the predicates as racy, atomic checks as userspace
3851 * cannot claim and pin a new fb without at least acquring the
3852 * struct_mutex and so serialising with us.
3853 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003854 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003855 if (atomic_read(&crtc->unpin_work_count) == 0)
3856 continue;
3857
3858 if (crtc->unpin_work)
3859 intel_wait_for_vblank(dev, crtc->pipe);
3860
3861 return true;
3862 }
3863
3864 return false;
3865}
3866
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003867static void page_flip_completed(struct intel_crtc *intel_crtc)
3868{
3869 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3870 struct intel_unpin_work *work = intel_crtc->unpin_work;
3871
3872 /* ensure that the unpin work is consistent wrt ->pending. */
3873 smp_rmb();
3874 intel_crtc->unpin_work = NULL;
3875
3876 if (work->event)
3877 drm_send_vblank_event(intel_crtc->base.dev,
3878 intel_crtc->pipe,
3879 work->event);
3880
3881 drm_crtc_vblank_put(&intel_crtc->base);
3882
3883 wake_up_all(&dev_priv->pending_flip_queue);
3884 queue_work(dev_priv->wq, &work->work);
3885
3886 trace_i915_flip_complete(intel_crtc->plane,
3887 work->pending_flip_obj);
3888}
3889
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003890void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003891{
Chris Wilson0f911282012-04-17 10:05:38 +01003892 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003893 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003894
Daniel Vetter2c10d572012-12-20 21:24:07 +01003895 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003896 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3897 !intel_crtc_has_pending_flip(crtc),
3898 60*HZ) == 0)) {
3899 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003900
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003901 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003902 if (intel_crtc->unpin_work) {
3903 WARN_ONCE(1, "Removing stuck page flip\n");
3904 page_flip_completed(intel_crtc);
3905 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003906 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003907 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003908
Chris Wilson975d5682014-08-20 13:13:34 +01003909 if (crtc->primary->fb) {
3910 mutex_lock(&dev->struct_mutex);
3911 intel_finish_fb(crtc->primary->fb);
3912 mutex_unlock(&dev->struct_mutex);
3913 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003914}
3915
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003916/* Program iCLKIP clock to the desired frequency */
3917static void lpt_program_iclkip(struct drm_crtc *crtc)
3918{
3919 struct drm_device *dev = crtc->dev;
3920 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003921 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003922 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3923 u32 temp;
3924
Ville Syrjäläa5805162015-05-26 20:42:30 +03003925 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003926
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003927 /* It is necessary to ungate the pixclk gate prior to programming
3928 * the divisors, and gate it back when it is done.
3929 */
3930 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3931
3932 /* Disable SSCCTL */
3933 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003934 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3935 SBI_SSCCTL_DISABLE,
3936 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003937
3938 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003939 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003940 auxdiv = 1;
3941 divsel = 0x41;
3942 phaseinc = 0x20;
3943 } else {
3944 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003945 * but the adjusted_mode->crtc_clock in in KHz. To get the
3946 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003947 * convert the virtual clock precision to KHz here for higher
3948 * precision.
3949 */
3950 u32 iclk_virtual_root_freq = 172800 * 1000;
3951 u32 iclk_pi_range = 64;
3952 u32 desired_divisor, msb_divisor_value, pi_value;
3953
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003954 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955 msb_divisor_value = desired_divisor / iclk_pi_range;
3956 pi_value = desired_divisor % iclk_pi_range;
3957
3958 auxdiv = 0;
3959 divsel = msb_divisor_value - 2;
3960 phaseinc = pi_value;
3961 }
3962
3963 /* This should not happen with any sane values */
3964 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3965 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3966 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3967 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3968
3969 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 +03003970 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003971 auxdiv,
3972 divsel,
3973 phasedir,
3974 phaseinc);
3975
3976 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003977 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003978 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3979 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3980 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3981 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3982 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3983 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003984 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003985
3986 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003987 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003988 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3989 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003990 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003991
3992 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003993 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003994 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003995 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003996
3997 /* Wait for initialization time */
3998 udelay(24);
3999
4000 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004001
Ville Syrjäläa5805162015-05-26 20:42:30 +03004002 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004003}
4004
Daniel Vetter275f01b22013-05-03 11:49:47 +02004005static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4006 enum pipe pch_transcoder)
4007{
4008 struct drm_device *dev = crtc->base.dev;
4009 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004010 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004011
4012 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4013 I915_READ(HTOTAL(cpu_transcoder)));
4014 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4015 I915_READ(HBLANK(cpu_transcoder)));
4016 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4017 I915_READ(HSYNC(cpu_transcoder)));
4018
4019 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4020 I915_READ(VTOTAL(cpu_transcoder)));
4021 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4022 I915_READ(VBLANK(cpu_transcoder)));
4023 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4024 I915_READ(VSYNC(cpu_transcoder)));
4025 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4026 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4027}
4028
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004029static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004030{
4031 struct drm_i915_private *dev_priv = dev->dev_private;
4032 uint32_t temp;
4033
4034 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004035 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004036 return;
4037
4038 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4039 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4040
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004041 temp &= ~FDI_BC_BIFURCATION_SELECT;
4042 if (enable)
4043 temp |= FDI_BC_BIFURCATION_SELECT;
4044
4045 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004046 I915_WRITE(SOUTH_CHICKEN1, temp);
4047 POSTING_READ(SOUTH_CHICKEN1);
4048}
4049
4050static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4051{
4052 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004053
4054 switch (intel_crtc->pipe) {
4055 case PIPE_A:
4056 break;
4057 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004058 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004059 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004060 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004061 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004062
4063 break;
4064 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004065 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004066
4067 break;
4068 default:
4069 BUG();
4070 }
4071}
4072
Jesse Barnesf67a5592011-01-05 10:31:48 -08004073/*
4074 * Enable PCH resources required for PCH ports:
4075 * - PCH PLLs
4076 * - FDI training & RX/TX
4077 * - update transcoder timings
4078 * - DP transcoding bits
4079 * - transcoder
4080 */
4081static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004082{
4083 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004084 struct drm_i915_private *dev_priv = dev->dev_private;
4085 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4086 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004087 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004088
Daniel Vetterab9412b2013-05-03 11:49:46 +02004089 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004090
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004091 if (IS_IVYBRIDGE(dev))
4092 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4093
Daniel Vettercd986ab2012-10-26 10:58:12 +02004094 /* Write the TU size bits before fdi link training, so that error
4095 * detection works. */
4096 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4097 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4098
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004099 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004100 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004101
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004102 /* We need to program the right clock selection before writing the pixel
4103 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004104 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004105 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004106
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004108 temp |= TRANS_DPLL_ENABLE(pipe);
4109 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004110 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004111 temp |= sel;
4112 else
4113 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004114 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004115 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004116
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004117 /* XXX: pch pll's can be enabled any time before we enable the PCH
4118 * transcoder, and we actually should do this to not upset any PCH
4119 * transcoder that already use the clock when we share it.
4120 *
4121 * Note that enable_shared_dpll tries to do the right thing, but
4122 * get_shared_dpll unconditionally resets the pll - we need that to have
4123 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004124 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004125
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004126 /* set transcoder timing, panel must allow it */
4127 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004128 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004129
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004130 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004131
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004132 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004133 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004134 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004135 reg = TRANS_DP_CTL(pipe);
4136 temp = I915_READ(reg);
4137 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004138 TRANS_DP_SYNC_MASK |
4139 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004140 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004141 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004142
4143 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004144 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004145 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004146 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004147
4148 switch (intel_trans_dp_port_sel(crtc)) {
4149 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004150 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004151 break;
4152 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004153 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004154 break;
4155 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004156 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004157 break;
4158 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004159 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004160 }
4161
Chris Wilson5eddb702010-09-11 13:48:45 +01004162 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004163 }
4164
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004165 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004166}
4167
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004168static void lpt_pch_enable(struct drm_crtc *crtc)
4169{
4170 struct drm_device *dev = crtc->dev;
4171 struct drm_i915_private *dev_priv = dev->dev_private;
4172 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004173 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004174
Daniel Vetterab9412b2013-05-03 11:49:46 +02004175 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004176
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004177 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004178
Paulo Zanoni0540e482012-10-31 18:12:40 -02004179 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004180 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004181
Paulo Zanoni937bb612012-10-31 18:12:47 -02004182 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004183}
4184
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004185struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4186 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004187{
Daniel Vettere2b78262013-06-07 23:10:03 +02004188 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004189 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004190 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004191
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004192 if (HAS_PCH_IBX(dev_priv->dev)) {
4193 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004194 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004195 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004196
Daniel Vetter46edb022013-06-05 13:34:12 +02004197 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4198 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004199
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004200 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004201
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004202 goto found;
4203 }
4204
Satheeshakrishna Mbcddf612014-08-22 09:49:10 +05304205 if (IS_BROXTON(dev_priv->dev)) {
4206 /* PLL is attached to port in bxt */
4207 struct intel_encoder *encoder;
4208 struct intel_digital_port *intel_dig_port;
4209
4210 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4211 if (WARN_ON(!encoder))
4212 return NULL;
4213
4214 intel_dig_port = enc_to_dig_port(&encoder->base);
4215 /* 1:1 mapping between ports and PLLs */
4216 i = (enum intel_dpll_id)intel_dig_port->port;
4217 pll = &dev_priv->shared_dplls[i];
4218 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4219 crtc->base.base.id, pll->name);
4220 WARN_ON(pll->new_config->crtc_mask);
4221
4222 goto found;
4223 }
4224
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004225 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4226 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004227
4228 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004229 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004230 continue;
4231
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004232 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004233 &pll->new_config->hw_state,
4234 sizeof(pll->new_config->hw_state)) == 0) {
4235 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004236 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004237 pll->new_config->crtc_mask,
4238 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004239 goto found;
4240 }
4241 }
4242
4243 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004244 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4245 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004246 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004247 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4248 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004249 goto found;
4250 }
4251 }
4252
4253 return NULL;
4254
4255found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004256 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004257 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004258
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004259 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004260 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4261 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004262
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004263 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004264
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004265 return pll;
4266}
4267
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004268/**
4269 * intel_shared_dpll_start_config - start a new PLL staged config
4270 * @dev_priv: DRM device
4271 * @clear_pipes: mask of pipes that will have their PLLs freed
4272 *
4273 * Starts a new PLL staged config, copying the current config but
4274 * releasing the references of pipes specified in clear_pipes.
4275 */
4276static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4277 unsigned clear_pipes)
4278{
4279 struct intel_shared_dpll *pll;
4280 enum intel_dpll_id i;
4281
4282 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4283 pll = &dev_priv->shared_dplls[i];
4284
4285 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4286 GFP_KERNEL);
4287 if (!pll->new_config)
4288 goto cleanup;
4289
4290 pll->new_config->crtc_mask &= ~clear_pipes;
4291 }
4292
4293 return 0;
4294
4295cleanup:
4296 while (--i >= 0) {
4297 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004298 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004299 pll->new_config = NULL;
4300 }
4301
4302 return -ENOMEM;
4303}
4304
4305static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4306{
4307 struct intel_shared_dpll *pll;
4308 enum intel_dpll_id i;
4309
4310 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4311 pll = &dev_priv->shared_dplls[i];
4312
4313 WARN_ON(pll->new_config == &pll->config);
4314
4315 pll->config = *pll->new_config;
4316 kfree(pll->new_config);
4317 pll->new_config = NULL;
4318 }
4319}
4320
4321static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4322{
4323 struct intel_shared_dpll *pll;
4324 enum intel_dpll_id i;
4325
4326 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4327 pll = &dev_priv->shared_dplls[i];
4328
4329 WARN_ON(pll->new_config == &pll->config);
4330
4331 kfree(pll->new_config);
4332 pll->new_config = NULL;
4333 }
4334}
4335
Daniel Vettera1520312013-05-03 11:49:50 +02004336static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004337{
4338 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004339 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004340 u32 temp;
4341
4342 temp = I915_READ(dslreg);
4343 udelay(500);
4344 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004345 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004346 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004347 }
4348}
4349
Chandra Kondurua1b22782015-04-07 15:28:45 -07004350/**
4351 * skl_update_scaler_users - Stages update to crtc's scaler state
4352 * @intel_crtc: crtc
4353 * @crtc_state: crtc_state
4354 * @plane: plane (NULL indicates crtc is requesting update)
4355 * @plane_state: plane's state
4356 * @force_detach: request unconditional detachment of scaler
4357 *
4358 * This function updates scaler state for requested plane or crtc.
4359 * To request scaler usage update for a plane, caller shall pass plane pointer.
4360 * To request scaler usage update for crtc, caller shall pass plane pointer
4361 * as NULL.
4362 *
4363 * Return
4364 * 0 - scaler_usage updated successfully
4365 * error - requested scaling cannot be supported or other error condition
4366 */
4367int
4368skl_update_scaler_users(
4369 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4370 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4371 int force_detach)
4372{
4373 int need_scaling;
4374 int idx;
4375 int src_w, src_h, dst_w, dst_h;
4376 int *scaler_id;
4377 struct drm_framebuffer *fb;
4378 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004379 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004380
4381 if (!intel_crtc || !crtc_state)
4382 return 0;
4383
4384 scaler_state = &crtc_state->scaler_state;
4385
4386 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4387 fb = intel_plane ? plane_state->base.fb : NULL;
4388
4389 if (intel_plane) {
4390 src_w = drm_rect_width(&plane_state->src) >> 16;
4391 src_h = drm_rect_height(&plane_state->src) >> 16;
4392 dst_w = drm_rect_width(&plane_state->dst);
4393 dst_h = drm_rect_height(&plane_state->dst);
4394 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004395 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004396 } else {
4397 struct drm_display_mode *adjusted_mode =
4398 &crtc_state->base.adjusted_mode;
4399 src_w = crtc_state->pipe_src_w;
4400 src_h = crtc_state->pipe_src_h;
4401 dst_w = adjusted_mode->hdisplay;
4402 dst_h = adjusted_mode->vdisplay;
4403 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004404 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004405 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004406
4407 need_scaling = intel_rotation_90_or_270(rotation) ?
4408 (src_h != dst_w || src_w != dst_h):
4409 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004410
4411 /*
4412 * if plane is being disabled or scaler is no more required or force detach
4413 * - free scaler binded to this plane/crtc
4414 * - in order to do this, update crtc->scaler_usage
4415 *
4416 * Here scaler state in crtc_state is set free so that
4417 * scaler can be assigned to other user. Actual register
4418 * update to free the scaler is done in plane/panel-fit programming.
4419 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4420 */
4421 if (force_detach || !need_scaling || (intel_plane &&
4422 (!fb || !plane_state->visible))) {
4423 if (*scaler_id >= 0) {
4424 scaler_state->scaler_users &= ~(1 << idx);
4425 scaler_state->scalers[*scaler_id].in_use = 0;
4426
4427 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4428 "crtc_state = %p scaler_users = 0x%x\n",
4429 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4430 intel_plane ? intel_plane->base.base.id :
4431 intel_crtc->base.base.id, crtc_state,
4432 scaler_state->scaler_users);
4433 *scaler_id = -1;
4434 }
4435 return 0;
4436 }
4437
4438 /* range checks */
4439 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4440 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4441
4442 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4443 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4444 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4445 "size is out of scaler range\n",
4446 intel_plane ? "PLANE" : "CRTC",
4447 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4448 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4449 return -EINVAL;
4450 }
4451
4452 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004453 if (WARN_ON(intel_plane &&
4454 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4455 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4456 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004457 return -EINVAL;
4458 }
4459
4460 /* Check src format */
4461 if (intel_plane) {
4462 switch (fb->pixel_format) {
4463 case DRM_FORMAT_RGB565:
4464 case DRM_FORMAT_XBGR8888:
4465 case DRM_FORMAT_XRGB8888:
4466 case DRM_FORMAT_ABGR8888:
4467 case DRM_FORMAT_ARGB8888:
4468 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004469 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004470 case DRM_FORMAT_YUYV:
4471 case DRM_FORMAT_YVYU:
4472 case DRM_FORMAT_UYVY:
4473 case DRM_FORMAT_VYUY:
4474 break;
4475 default:
4476 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4477 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4478 return -EINVAL;
4479 }
4480 }
4481
4482 /* mark this plane as a scaler user in crtc_state */
4483 scaler_state->scaler_users |= (1 << idx);
4484 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4485 "crtc_state = %p scaler_users = 0x%x\n",
4486 intel_plane ? "PLANE" : "CRTC",
4487 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4488 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4489 return 0;
4490}
4491
4492static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004493{
4494 struct drm_device *dev = crtc->base.dev;
4495 struct drm_i915_private *dev_priv = dev->dev_private;
4496 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004497 struct intel_crtc_scaler_state *scaler_state =
4498 &crtc->config->scaler_state;
4499
4500 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4501
4502 /* To update pfit, first update scaler state */
4503 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4504 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4505 skl_detach_scalers(crtc);
4506 if (!enable)
4507 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004508
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004509 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004510 int id;
4511
4512 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4513 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4514 return;
4515 }
4516
4517 id = scaler_state->scaler_id;
4518 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4519 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4520 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4521 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4522
4523 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004524 }
4525}
4526
Jesse Barnesb074cec2013-04-25 12:55:02 -07004527static void ironlake_pfit_enable(struct intel_crtc *crtc)
4528{
4529 struct drm_device *dev = crtc->base.dev;
4530 struct drm_i915_private *dev_priv = dev->dev_private;
4531 int pipe = crtc->pipe;
4532
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004533 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004534 /* Force use of hard-coded filter coefficients
4535 * as some pre-programmed values are broken,
4536 * e.g. x201.
4537 */
4538 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4539 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4540 PF_PIPE_SEL_IVB(pipe));
4541 else
4542 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004543 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4544 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004545 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004546}
4547
Matt Roper4a3b8762014-12-23 10:41:51 -08004548static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004549{
4550 struct drm_device *dev = crtc->dev;
4551 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004552 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004553 struct intel_plane *intel_plane;
4554
Matt Roperaf2b6532014-04-01 15:22:32 -07004555 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4556 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004557 if (intel_plane->pipe == pipe)
4558 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004559 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004560}
4561
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004562void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004563{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004564 struct drm_device *dev = crtc->base.dev;
4565 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004566
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004567 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004568 return;
4569
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004570 /* We can only enable IPS after we enable a plane and wait for a vblank */
4571 intel_wait_for_vblank(dev, crtc->pipe);
4572
Paulo Zanonid77e4532013-09-24 13:52:55 -03004573 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004574 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004575 mutex_lock(&dev_priv->rps.hw_lock);
4576 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4577 mutex_unlock(&dev_priv->rps.hw_lock);
4578 /* Quoting Art Runyan: "its not safe to expect any particular
4579 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004580 * mailbox." Moreover, the mailbox may return a bogus state,
4581 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004582 */
4583 } else {
4584 I915_WRITE(IPS_CTL, IPS_ENABLE);
4585 /* The bit only becomes 1 in the next vblank, so this wait here
4586 * is essentially intel_wait_for_vblank. If we don't have this
4587 * and don't wait for vblanks until the end of crtc_enable, then
4588 * the HW state readout code will complain that the expected
4589 * IPS_CTL value is not the one we read. */
4590 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4591 DRM_ERROR("Timed out waiting for IPS enable\n");
4592 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004593}
4594
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004595void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004596{
4597 struct drm_device *dev = crtc->base.dev;
4598 struct drm_i915_private *dev_priv = dev->dev_private;
4599
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004600 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004601 return;
4602
4603 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004604 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004605 mutex_lock(&dev_priv->rps.hw_lock);
4606 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4607 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004608 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4609 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4610 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004611 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004612 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004613 POSTING_READ(IPS_CTL);
4614 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004615
4616 /* We need to wait for a vblank before we can disable the plane. */
4617 intel_wait_for_vblank(dev, crtc->pipe);
4618}
4619
4620/** Loads the palette/gamma unit for the CRTC with the prepared values */
4621static void intel_crtc_load_lut(struct drm_crtc *crtc)
4622{
4623 struct drm_device *dev = crtc->dev;
4624 struct drm_i915_private *dev_priv = dev->dev_private;
4625 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4626 enum pipe pipe = intel_crtc->pipe;
4627 int palreg = PALETTE(pipe);
4628 int i;
4629 bool reenable_ips = false;
4630
4631 /* The clocks have to be on to load the palette. */
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004632 if (!crtc->state->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004633 return;
4634
Imre Deak50360402015-01-16 00:55:16 -08004635 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004636 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004637 assert_dsi_pll_enabled(dev_priv);
4638 else
4639 assert_pll_enabled(dev_priv, pipe);
4640 }
4641
4642 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304643 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004644 palreg = LGC_PALETTE(pipe);
4645
4646 /* Workaround : Do not read or write the pipe palette/gamma data while
4647 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4648 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004649 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004650 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4651 GAMMA_MODE_MODE_SPLIT)) {
4652 hsw_disable_ips(intel_crtc);
4653 reenable_ips = true;
4654 }
4655
4656 for (i = 0; i < 256; i++) {
4657 I915_WRITE(palreg + 4 * i,
4658 (intel_crtc->lut_r[i] << 16) |
4659 (intel_crtc->lut_g[i] << 8) |
4660 intel_crtc->lut_b[i]);
4661 }
4662
4663 if (reenable_ips)
4664 hsw_enable_ips(intel_crtc);
4665}
4666
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004667static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004668{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004669 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004670 struct drm_device *dev = intel_crtc->base.dev;
4671 struct drm_i915_private *dev_priv = dev->dev_private;
4672
4673 mutex_lock(&dev->struct_mutex);
4674 dev_priv->mm.interruptible = false;
4675 (void) intel_overlay_switch_off(intel_crtc->overlay);
4676 dev_priv->mm.interruptible = true;
4677 mutex_unlock(&dev->struct_mutex);
4678 }
4679
4680 /* Let userspace switch the overlay on again. In most cases userspace
4681 * has to recompute where to put it anyway.
4682 */
4683}
4684
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004685/**
4686 * intel_post_enable_primary - Perform operations after enabling primary plane
4687 * @crtc: the CRTC whose primary plane was just enabled
4688 *
4689 * Performs potentially sleeping operations that must be done after the primary
4690 * plane is enabled, such as updating FBC and IPS. Note that this may be
4691 * called due to an explicit primary plane update, or due to an implicit
4692 * re-enable that is caused when a sprite plane is updated to no longer
4693 * completely hide the primary plane.
4694 */
4695static void
4696intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004697{
4698 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004699 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004700 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4701 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004702
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004703 /*
4704 * BDW signals flip done immediately if the plane
4705 * is disabled, even if the plane enable is already
4706 * armed to occur at the next vblank :(
4707 */
4708 if (IS_BROADWELL(dev))
4709 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004711 /*
4712 * FIXME IPS should be fine as long as one plane is
4713 * enabled, but in practice it seems to have problems
4714 * when going from primary only to sprite only and vice
4715 * versa.
4716 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004717 hsw_enable_ips(intel_crtc);
4718
4719 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004720 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004721 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004722
4723 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004724 * Gen2 reports pipe underruns whenever all planes are disabled.
4725 * So don't enable underrun reporting before at least some planes
4726 * are enabled.
4727 * FIXME: Need to fix the logic to work when we turn off all planes
4728 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004729 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004730 if (IS_GEN2(dev))
4731 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4732
4733 /* Underruns don't raise interrupts, so check manually. */
4734 if (HAS_GMCH_DISPLAY(dev))
4735 i9xx_check_fifo_underruns(dev_priv);
4736}
4737
4738/**
4739 * intel_pre_disable_primary - Perform operations before disabling primary plane
4740 * @crtc: the CRTC whose primary plane is to be disabled
4741 *
4742 * Performs potentially sleeping operations that must be done before the
4743 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4744 * be called due to an explicit primary plane update, or due to an implicit
4745 * disable that is caused when a sprite plane completely hides the primary
4746 * plane.
4747 */
4748static void
4749intel_pre_disable_primary(struct drm_crtc *crtc)
4750{
4751 struct drm_device *dev = crtc->dev;
4752 struct drm_i915_private *dev_priv = dev->dev_private;
4753 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4754 int pipe = intel_crtc->pipe;
4755
4756 /*
4757 * Gen2 reports pipe underruns whenever all planes are disabled.
4758 * So diasble underrun reporting before all the planes get disabled.
4759 * FIXME: Need to fix the logic to work when we turn off all planes
4760 * but leave the pipe running.
4761 */
4762 if (IS_GEN2(dev))
4763 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4764
4765 /*
4766 * Vblank time updates from the shadow to live plane control register
4767 * are blocked if the memory self-refresh mode is active at that
4768 * moment. So to make sure the plane gets truly disabled, disable
4769 * first the self-refresh mode. The self-refresh enable bit in turn
4770 * will be checked/applied by the HW only at the next frame start
4771 * event which is after the vblank start event, so we need to have a
4772 * wait-for-vblank between disabling the plane and the pipe.
4773 */
4774 if (HAS_GMCH_DISPLAY(dev))
4775 intel_set_memory_cxsr(dev_priv, false);
4776
4777 mutex_lock(&dev->struct_mutex);
4778 if (dev_priv->fbc.crtc == intel_crtc)
4779 intel_fbc_disable(dev);
4780 mutex_unlock(&dev->struct_mutex);
4781
4782 /*
4783 * FIXME IPS should be fine as long as one plane is
4784 * enabled, but in practice it seems to have problems
4785 * when going from primary only to sprite only and vice
4786 * versa.
4787 */
4788 hsw_disable_ips(intel_crtc);
4789}
4790
4791static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4792{
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004793 struct drm_device *dev = crtc->dev;
4794 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4795 int pipe = intel_crtc->pipe;
4796
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004797 intel_enable_primary_hw_plane(crtc->primary, crtc);
4798 intel_enable_sprite_planes(crtc);
4799 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004800
4801 intel_post_enable_primary(crtc);
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004802
4803 /*
4804 * FIXME: Once we grow proper nuclear flip support out of this we need
4805 * to compute the mask of flip planes precisely. For the time being
4806 * consider this a flip to a NULL plane.
4807 */
4808 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004809}
4810
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004811static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004812{
4813 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004814 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004815 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004816 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004817
4818 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004819
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004820 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004821
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004822 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004823 for_each_intel_plane(dev, intel_plane) {
4824 if (intel_plane->pipe == pipe) {
4825 struct drm_crtc *from = intel_plane->base.crtc;
4826
4827 intel_plane->disable_plane(&intel_plane->base,
4828 from ?: crtc, true);
4829 }
4830 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004831
Daniel Vetterf99d7062014-06-19 16:01:59 +02004832 /*
4833 * FIXME: Once we grow proper nuclear flip support out of this we need
4834 * to compute the mask of flip planes precisely. For the time being
4835 * consider this a flip to a NULL plane.
4836 */
4837 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004838}
4839
Jesse Barnesf67a5592011-01-05 10:31:48 -08004840static void ironlake_crtc_enable(struct drm_crtc *crtc)
4841{
4842 struct drm_device *dev = crtc->dev;
4843 struct drm_i915_private *dev_priv = dev->dev_private;
4844 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004845 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004846 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004847
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004848 if (WARN_ON(intel_crtc->active))
Jesse Barnesf67a5592011-01-05 10:31:48 -08004849 return;
4850
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004851 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004852 intel_prepare_shared_dpll(intel_crtc);
4853
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004854 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304855 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004856
4857 intel_set_pipe_timings(intel_crtc);
4858
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004859 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004860 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004861 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004862 }
4863
4864 ironlake_set_pipeconf(crtc);
4865
Jesse Barnesf67a5592011-01-05 10:31:48 -08004866 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004867
Daniel Vettera72e4c92014-09-30 10:56:47 +02004868 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4869 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004870
Daniel Vetterf6736a12013-06-05 13:34:30 +02004871 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004872 if (encoder->pre_enable)
4873 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004874
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004875 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004876 /* Note: FDI PLL enabling _must_ be done before we enable the
4877 * cpu pipes, hence this is separate from all the other fdi/pch
4878 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004879 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004880 } else {
4881 assert_fdi_tx_disabled(dev_priv, pipe);
4882 assert_fdi_rx_disabled(dev_priv, pipe);
4883 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004884
Jesse Barnesb074cec2013-04-25 12:55:02 -07004885 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004886
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004887 /*
4888 * On ILK+ LUT must be loaded before the pipe is running but with
4889 * clocks enabled
4890 */
4891 intel_crtc_load_lut(crtc);
4892
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004893 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004894 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004895
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004896 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004897 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004898
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004899 assert_vblank_disabled(crtc);
4900 drm_crtc_vblank_on(crtc);
4901
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004902 for_each_encoder_on_crtc(dev, crtc, encoder)
4903 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004904
4905 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004906 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004907}
4908
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004909/* IPS only exists on ULT machines and is tied to pipe A. */
4910static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4911{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004912 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004913}
4914
Paulo Zanonie4916942013-09-20 16:21:19 -03004915/*
4916 * This implements the workaround described in the "notes" section of the mode
4917 * set sequence documentation. When going from no pipes or single pipe to
4918 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4919 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4920 */
4921static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4922{
4923 struct drm_device *dev = crtc->base.dev;
4924 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4925
4926 /* We want to get the other_active_crtc only if there's only 1 other
4927 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004928 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004929 if (!crtc_it->active || crtc_it == crtc)
4930 continue;
4931
4932 if (other_active_crtc)
4933 return;
4934
4935 other_active_crtc = crtc_it;
4936 }
4937 if (!other_active_crtc)
4938 return;
4939
4940 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4941 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4942}
4943
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004944static void haswell_crtc_enable(struct drm_crtc *crtc)
4945{
4946 struct drm_device *dev = crtc->dev;
4947 struct drm_i915_private *dev_priv = dev->dev_private;
4948 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4949 struct intel_encoder *encoder;
4950 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004951
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02004952 if (WARN_ON(intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004953 return;
4954
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004955 if (intel_crtc_to_shared_dpll(intel_crtc))
4956 intel_enable_shared_dpll(intel_crtc);
4957
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004958 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05304959 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004960
4961 intel_set_pipe_timings(intel_crtc);
4962
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004963 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4964 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4965 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004966 }
4967
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004968 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004969 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004970 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004971 }
4972
4973 haswell_set_pipeconf(crtc);
4974
4975 intel_set_pipe_csc(crtc);
4976
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004977 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004978
Daniel Vettera72e4c92014-09-30 10:56:47 +02004979 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004980 for_each_encoder_on_crtc(dev, crtc, encoder)
4981 if (encoder->pre_enable)
4982 encoder->pre_enable(encoder);
4983
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004984 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004985 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4986 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004987 dev_priv->display.fdi_link_train(crtc);
4988 }
4989
Paulo Zanoni1f544382012-10-24 11:32:00 -02004990 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004991
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004992 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07004993 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004994 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004995 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08004996 else
4997 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004998
4999 /*
5000 * On ILK+ LUT must be loaded before the pipe is running but with
5001 * clocks enabled
5002 */
5003 intel_crtc_load_lut(crtc);
5004
Paulo Zanoni1f544382012-10-24 11:32:00 -02005005 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005006 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005007
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005008 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005009 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005010
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005011 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005012 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005013
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005014 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005015 intel_ddi_set_vc_payload_alloc(crtc, true);
5016
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005017 assert_vblank_disabled(crtc);
5018 drm_crtc_vblank_on(crtc);
5019
Jani Nikula8807e552013-08-30 19:40:32 +03005020 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005021 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005022 intel_opregion_notify_encoder(encoder, true);
5023 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005024
Paulo Zanonie4916942013-09-20 16:21:19 -03005025 /* If we change the relative order between pipe/planes enabling, we need
5026 * to change the workaround. */
5027 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005028}
5029
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005030static void ironlake_pfit_disable(struct intel_crtc *crtc)
5031{
5032 struct drm_device *dev = crtc->base.dev;
5033 struct drm_i915_private *dev_priv = dev->dev_private;
5034 int pipe = crtc->pipe;
5035
5036 /* To avoid upsetting the power well on haswell only disable the pfit if
5037 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005038 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005039 I915_WRITE(PF_CTL(pipe), 0);
5040 I915_WRITE(PF_WIN_POS(pipe), 0);
5041 I915_WRITE(PF_WIN_SZ(pipe), 0);
5042 }
5043}
5044
Jesse Barnes6be4a602010-09-10 10:26:01 -07005045static void ironlake_crtc_disable(struct drm_crtc *crtc)
5046{
5047 struct drm_device *dev = crtc->dev;
5048 struct drm_i915_private *dev_priv = dev->dev_private;
5049 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005050 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005051 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005052 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005053
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005054 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005055 return;
5056
Daniel Vetterea9d7582012-07-10 10:42:52 +02005057 for_each_encoder_on_crtc(dev, crtc, encoder)
5058 encoder->disable(encoder);
5059
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005060 drm_crtc_vblank_off(crtc);
5061 assert_vblank_disabled(crtc);
5062
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005063 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005064 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005065
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005066 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005067
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005068 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005069
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005070 if (intel_crtc->config->has_pch_encoder)
5071 ironlake_fdi_disable(crtc);
5072
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005073 for_each_encoder_on_crtc(dev, crtc, encoder)
5074 if (encoder->post_disable)
5075 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005076
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005077 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005078 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005079
Daniel Vetterd925c592013-06-05 13:34:04 +02005080 if (HAS_PCH_CPT(dev)) {
5081 /* disable TRANS_DP_CTL */
5082 reg = TRANS_DP_CTL(pipe);
5083 temp = I915_READ(reg);
5084 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5085 TRANS_DP_PORT_SEL_MASK);
5086 temp |= TRANS_DP_PORT_SEL_NONE;
5087 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005088
Daniel Vetterd925c592013-06-05 13:34:04 +02005089 /* disable DPLL_SEL */
5090 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005091 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005092 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005093 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005094
5095 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005096 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005097
5098 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005099 }
5100
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005101 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005102 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005103
5104 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005105 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005106 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005107}
5108
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005109static void haswell_crtc_disable(struct drm_crtc *crtc)
5110{
5111 struct drm_device *dev = crtc->dev;
5112 struct drm_i915_private *dev_priv = dev->dev_private;
5113 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5114 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005115 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005116
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005117 if (WARN_ON(!intel_crtc->active))
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005118 return;
5119
Jani Nikula8807e552013-08-30 19:40:32 +03005120 for_each_encoder_on_crtc(dev, crtc, encoder) {
5121 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005122 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005123 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005124
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005125 drm_crtc_vblank_off(crtc);
5126 assert_vblank_disabled(crtc);
5127
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005128 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005129 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5130 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005131 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005132
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005133 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005134 intel_ddi_set_vc_payload_alloc(crtc, false);
5135
Paulo Zanoniad80a812012-10-24 16:06:19 -02005136 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005137
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005138 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005139 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005140 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005141 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005142 else
5143 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005144
Paulo Zanoni1f544382012-10-24 11:32:00 -02005145 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005146
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005147 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005148 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005149 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005150 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005151
Imre Deak97b040a2014-06-25 22:01:50 +03005152 for_each_encoder_on_crtc(dev, crtc, encoder)
5153 if (encoder->post_disable)
5154 encoder->post_disable(encoder);
5155
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005156 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005157 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005158
5159 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005160 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005161 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005162
5163 if (intel_crtc_to_shared_dpll(intel_crtc))
5164 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005165}
5166
Jesse Barnes2dd24552013-04-25 12:55:01 -07005167static void i9xx_pfit_enable(struct intel_crtc *crtc)
5168{
5169 struct drm_device *dev = crtc->base.dev;
5170 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005171 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005172
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005173 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005174 return;
5175
Daniel Vetterc0b03412013-05-28 12:05:54 +02005176 /*
5177 * The panel fitter should only be adjusted whilst the pipe is disabled,
5178 * according to register description and PRM.
5179 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005180 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5181 assert_pipe_disabled(dev_priv, crtc->pipe);
5182
Jesse Barnesb074cec2013-04-25 12:55:02 -07005183 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5184 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005185
5186 /* Border color in case we don't scale up to the full screen. Black by
5187 * default, change to something else for debugging. */
5188 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005189}
5190
Dave Airlied05410f2014-06-05 13:22:59 +10005191static enum intel_display_power_domain port_to_power_domain(enum port port)
5192{
5193 switch (port) {
5194 case PORT_A:
5195 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5196 case PORT_B:
5197 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5198 case PORT_C:
5199 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5200 case PORT_D:
5201 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5202 default:
5203 WARN_ON_ONCE(1);
5204 return POWER_DOMAIN_PORT_OTHER;
5205 }
5206}
5207
Imre Deak77d22dc2014-03-05 16:20:52 +02005208#define for_each_power_domain(domain, mask) \
5209 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5210 if ((1 << (domain)) & (mask))
5211
Imre Deak319be8a2014-03-04 19:22:57 +02005212enum intel_display_power_domain
5213intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005214{
Imre Deak319be8a2014-03-04 19:22:57 +02005215 struct drm_device *dev = intel_encoder->base.dev;
5216 struct intel_digital_port *intel_dig_port;
5217
5218 switch (intel_encoder->type) {
5219 case INTEL_OUTPUT_UNKNOWN:
5220 /* Only DDI platforms should ever use this output type */
5221 WARN_ON_ONCE(!HAS_DDI(dev));
5222 case INTEL_OUTPUT_DISPLAYPORT:
5223 case INTEL_OUTPUT_HDMI:
5224 case INTEL_OUTPUT_EDP:
5225 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005226 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005227 case INTEL_OUTPUT_DP_MST:
5228 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5229 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005230 case INTEL_OUTPUT_ANALOG:
5231 return POWER_DOMAIN_PORT_CRT;
5232 case INTEL_OUTPUT_DSI:
5233 return POWER_DOMAIN_PORT_DSI;
5234 default:
5235 return POWER_DOMAIN_PORT_OTHER;
5236 }
5237}
5238
5239static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5240{
5241 struct drm_device *dev = crtc->dev;
5242 struct intel_encoder *intel_encoder;
5243 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5244 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005245 unsigned long mask;
5246 enum transcoder transcoder;
5247
5248 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5249
5250 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5251 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005252 if (intel_crtc->config->pch_pfit.enabled ||
5253 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005254 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5255
Imre Deak319be8a2014-03-04 19:22:57 +02005256 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5257 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5258
Imre Deak77d22dc2014-03-05 16:20:52 +02005259 return mask;
5260}
5261
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005262static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005263{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005264 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005265 struct drm_i915_private *dev_priv = dev->dev_private;
5266 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5267 struct intel_crtc *crtc;
5268
5269 /*
5270 * First get all needed power domains, then put all unneeded, to avoid
5271 * any unnecessary toggling of the power wells.
5272 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005273 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005274 enum intel_display_power_domain domain;
5275
Matt Roper83d65732015-02-25 13:12:16 -08005276 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005277 continue;
5278
Imre Deak319be8a2014-03-04 19:22:57 +02005279 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005280
5281 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5282 intel_display_power_get(dev_priv, domain);
5283 }
5284
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005285 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005286 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005287
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005288 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005289 enum intel_display_power_domain domain;
5290
5291 for_each_power_domain(domain, crtc->enabled_power_domains)
5292 intel_display_power_put(dev_priv, domain);
5293
5294 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5295 }
5296
5297 intel_display_set_init_power(dev_priv, false);
5298}
5299
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005300static void intel_update_max_cdclk(struct drm_device *dev)
5301{
5302 struct drm_i915_private *dev_priv = dev->dev_private;
5303
5304 if (IS_SKYLAKE(dev)) {
5305 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5306
5307 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5308 dev_priv->max_cdclk_freq = 675000;
5309 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5310 dev_priv->max_cdclk_freq = 540000;
5311 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5312 dev_priv->max_cdclk_freq = 450000;
5313 else
5314 dev_priv->max_cdclk_freq = 337500;
5315 } else if (IS_BROADWELL(dev)) {
5316 /*
5317 * FIXME with extra cooling we can allow
5318 * 540 MHz for ULX and 675 Mhz for ULT.
5319 * How can we know if extra cooling is
5320 * available? PCI ID, VTB, something else?
5321 */
5322 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5323 dev_priv->max_cdclk_freq = 450000;
5324 else if (IS_BDW_ULX(dev))
5325 dev_priv->max_cdclk_freq = 450000;
5326 else if (IS_BDW_ULT(dev))
5327 dev_priv->max_cdclk_freq = 540000;
5328 else
5329 dev_priv->max_cdclk_freq = 675000;
5330 } else if (IS_VALLEYVIEW(dev)) {
5331 dev_priv->max_cdclk_freq = 400000;
5332 } else {
5333 /* otherwise assume cdclk is fixed */
5334 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5335 }
5336
5337 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5338 dev_priv->max_cdclk_freq);
5339}
5340
5341static void intel_update_cdclk(struct drm_device *dev)
5342{
5343 struct drm_i915_private *dev_priv = dev->dev_private;
5344
5345 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5346 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5347 dev_priv->cdclk_freq);
5348
5349 /*
5350 * Program the gmbus_freq based on the cdclk frequency.
5351 * BSpec erroneously claims we should aim for 4MHz, but
5352 * in fact 1MHz is the correct frequency.
5353 */
5354 if (IS_VALLEYVIEW(dev)) {
5355 /*
5356 * Program the gmbus_freq based on the cdclk frequency.
5357 * BSpec erroneously claims we should aim for 4MHz, but
5358 * in fact 1MHz is the correct frequency.
5359 */
5360 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5361 }
5362
5363 if (dev_priv->max_cdclk_freq == 0)
5364 intel_update_max_cdclk(dev);
5365}
5366
Damien Lespiau70d0c572015-06-04 18:21:29 +01005367static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305368{
5369 struct drm_i915_private *dev_priv = dev->dev_private;
5370 uint32_t divider;
5371 uint32_t ratio;
5372 uint32_t current_freq;
5373 int ret;
5374
5375 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5376 switch (frequency) {
5377 case 144000:
5378 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5379 ratio = BXT_DE_PLL_RATIO(60);
5380 break;
5381 case 288000:
5382 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5383 ratio = BXT_DE_PLL_RATIO(60);
5384 break;
5385 case 384000:
5386 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5387 ratio = BXT_DE_PLL_RATIO(60);
5388 break;
5389 case 576000:
5390 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5391 ratio = BXT_DE_PLL_RATIO(60);
5392 break;
5393 case 624000:
5394 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5395 ratio = BXT_DE_PLL_RATIO(65);
5396 break;
5397 case 19200:
5398 /*
5399 * Bypass frequency with DE PLL disabled. Init ratio, divider
5400 * to suppress GCC warning.
5401 */
5402 ratio = 0;
5403 divider = 0;
5404 break;
5405 default:
5406 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5407
5408 return;
5409 }
5410
5411 mutex_lock(&dev_priv->rps.hw_lock);
5412 /* Inform power controller of upcoming frequency change */
5413 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5414 0x80000000);
5415 mutex_unlock(&dev_priv->rps.hw_lock);
5416
5417 if (ret) {
5418 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5419 ret, frequency);
5420 return;
5421 }
5422
5423 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5424 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5425 current_freq = current_freq * 500 + 1000;
5426
5427 /*
5428 * DE PLL has to be disabled when
5429 * - setting to 19.2MHz (bypass, PLL isn't used)
5430 * - before setting to 624MHz (PLL needs toggling)
5431 * - before setting to any frequency from 624MHz (PLL needs toggling)
5432 */
5433 if (frequency == 19200 || frequency == 624000 ||
5434 current_freq == 624000) {
5435 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5436 /* Timeout 200us */
5437 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5438 1))
5439 DRM_ERROR("timout waiting for DE PLL unlock\n");
5440 }
5441
5442 if (frequency != 19200) {
5443 uint32_t val;
5444
5445 val = I915_READ(BXT_DE_PLL_CTL);
5446 val &= ~BXT_DE_PLL_RATIO_MASK;
5447 val |= ratio;
5448 I915_WRITE(BXT_DE_PLL_CTL, val);
5449
5450 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5451 /* Timeout 200us */
5452 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5453 DRM_ERROR("timeout waiting for DE PLL lock\n");
5454
5455 val = I915_READ(CDCLK_CTL);
5456 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5457 val |= divider;
5458 /*
5459 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5460 * enable otherwise.
5461 */
5462 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5463 if (frequency >= 500000)
5464 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5465
5466 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5467 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5468 val |= (frequency - 1000) / 500;
5469 I915_WRITE(CDCLK_CTL, val);
5470 }
5471
5472 mutex_lock(&dev_priv->rps.hw_lock);
5473 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5474 DIV_ROUND_UP(frequency, 25000));
5475 mutex_unlock(&dev_priv->rps.hw_lock);
5476
5477 if (ret) {
5478 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5479 ret, frequency);
5480 return;
5481 }
5482
Damien Lespiaua47871b2015-06-04 18:21:34 +01005483 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305484}
5485
5486void broxton_init_cdclk(struct drm_device *dev)
5487{
5488 struct drm_i915_private *dev_priv = dev->dev_private;
5489 uint32_t val;
5490
5491 /*
5492 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5493 * or else the reset will hang because there is no PCH to respond.
5494 * Move the handshake programming to initialization sequence.
5495 * Previously was left up to BIOS.
5496 */
5497 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5498 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5499 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5500
5501 /* Enable PG1 for cdclk */
5502 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5503
5504 /* check if cd clock is enabled */
5505 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5506 DRM_DEBUG_KMS("Display already initialized\n");
5507 return;
5508 }
5509
5510 /*
5511 * FIXME:
5512 * - The initial CDCLK needs to be read from VBT.
5513 * Need to make this change after VBT has changes for BXT.
5514 * - check if setting the max (or any) cdclk freq is really necessary
5515 * here, it belongs to modeset time
5516 */
5517 broxton_set_cdclk(dev, 624000);
5518
5519 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005520 POSTING_READ(DBUF_CTL);
5521
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305522 udelay(10);
5523
5524 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5525 DRM_ERROR("DBuf power enable timeout!\n");
5526}
5527
5528void broxton_uninit_cdclk(struct drm_device *dev)
5529{
5530 struct drm_i915_private *dev_priv = dev->dev_private;
5531
5532 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005533 POSTING_READ(DBUF_CTL);
5534
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305535 udelay(10);
5536
5537 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5538 DRM_ERROR("DBuf power disable timeout!\n");
5539
5540 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5541 broxton_set_cdclk(dev, 19200);
5542
5543 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5544}
5545
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005546static const struct skl_cdclk_entry {
5547 unsigned int freq;
5548 unsigned int vco;
5549} skl_cdclk_frequencies[] = {
5550 { .freq = 308570, .vco = 8640 },
5551 { .freq = 337500, .vco = 8100 },
5552 { .freq = 432000, .vco = 8640 },
5553 { .freq = 450000, .vco = 8100 },
5554 { .freq = 540000, .vco = 8100 },
5555 { .freq = 617140, .vco = 8640 },
5556 { .freq = 675000, .vco = 8100 },
5557};
5558
5559static unsigned int skl_cdclk_decimal(unsigned int freq)
5560{
5561 return (freq - 1000) / 500;
5562}
5563
5564static unsigned int skl_cdclk_get_vco(unsigned int freq)
5565{
5566 unsigned int i;
5567
5568 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5569 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5570
5571 if (e->freq == freq)
5572 return e->vco;
5573 }
5574
5575 return 8100;
5576}
5577
5578static void
5579skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5580{
5581 unsigned int min_freq;
5582 u32 val;
5583
5584 /* select the minimum CDCLK before enabling DPLL 0 */
5585 val = I915_READ(CDCLK_CTL);
5586 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5587 val |= CDCLK_FREQ_337_308;
5588
5589 if (required_vco == 8640)
5590 min_freq = 308570;
5591 else
5592 min_freq = 337500;
5593
5594 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5595
5596 I915_WRITE(CDCLK_CTL, val);
5597 POSTING_READ(CDCLK_CTL);
5598
5599 /*
5600 * We always enable DPLL0 with the lowest link rate possible, but still
5601 * taking into account the VCO required to operate the eDP panel at the
5602 * desired frequency. The usual DP link rates operate with a VCO of
5603 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5604 * The modeset code is responsible for the selection of the exact link
5605 * rate later on, with the constraint of choosing a frequency that
5606 * works with required_vco.
5607 */
5608 val = I915_READ(DPLL_CTRL1);
5609
5610 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5611 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5612 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5613 if (required_vco == 8640)
5614 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5615 SKL_DPLL0);
5616 else
5617 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5618 SKL_DPLL0);
5619
5620 I915_WRITE(DPLL_CTRL1, val);
5621 POSTING_READ(DPLL_CTRL1);
5622
5623 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5624
5625 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5626 DRM_ERROR("DPLL0 not locked\n");
5627}
5628
5629static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5630{
5631 int ret;
5632 u32 val;
5633
5634 /* inform PCU we want to change CDCLK */
5635 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5636 mutex_lock(&dev_priv->rps.hw_lock);
5637 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5638 mutex_unlock(&dev_priv->rps.hw_lock);
5639
5640 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5641}
5642
5643static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5644{
5645 unsigned int i;
5646
5647 for (i = 0; i < 15; i++) {
5648 if (skl_cdclk_pcu_ready(dev_priv))
5649 return true;
5650 udelay(10);
5651 }
5652
5653 return false;
5654}
5655
5656static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5657{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005658 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005659 u32 freq_select, pcu_ack;
5660
5661 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5662
5663 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5664 DRM_ERROR("failed to inform PCU about cdclk change\n");
5665 return;
5666 }
5667
5668 /* set CDCLK_CTL */
5669 switch(freq) {
5670 case 450000:
5671 case 432000:
5672 freq_select = CDCLK_FREQ_450_432;
5673 pcu_ack = 1;
5674 break;
5675 case 540000:
5676 freq_select = CDCLK_FREQ_540;
5677 pcu_ack = 2;
5678 break;
5679 case 308570:
5680 case 337500:
5681 default:
5682 freq_select = CDCLK_FREQ_337_308;
5683 pcu_ack = 0;
5684 break;
5685 case 617140:
5686 case 675000:
5687 freq_select = CDCLK_FREQ_675_617;
5688 pcu_ack = 3;
5689 break;
5690 }
5691
5692 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5693 POSTING_READ(CDCLK_CTL);
5694
5695 /* inform PCU of the change */
5696 mutex_lock(&dev_priv->rps.hw_lock);
5697 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5698 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005699
5700 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005701}
5702
5703void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5704{
5705 /* disable DBUF power */
5706 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5707 POSTING_READ(DBUF_CTL);
5708
5709 udelay(10);
5710
5711 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5712 DRM_ERROR("DBuf power disable timeout\n");
5713
5714 /* disable DPLL0 */
5715 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5716 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5717 DRM_ERROR("Couldn't disable DPLL0\n");
5718
5719 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5720}
5721
5722void skl_init_cdclk(struct drm_i915_private *dev_priv)
5723{
5724 u32 val;
5725 unsigned int required_vco;
5726
5727 /* enable PCH reset handshake */
5728 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5729 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5730
5731 /* enable PG1 and Misc I/O */
5732 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5733
5734 /* DPLL0 already enabed !? */
5735 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5736 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5737 return;
5738 }
5739
5740 /* enable DPLL0 */
5741 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5742 skl_dpll0_enable(dev_priv, required_vco);
5743
5744 /* set CDCLK to the frequency the BIOS chose */
5745 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5746
5747 /* enable DBUF power */
5748 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5749 POSTING_READ(DBUF_CTL);
5750
5751 udelay(10);
5752
5753 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5754 DRM_ERROR("DBuf power enable timeout\n");
5755}
5756
Ville Syrjälädfcab172014-06-13 13:37:47 +03005757/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005758static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005759{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005760 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005761
Jesse Barnes586f49d2013-11-04 16:06:59 -08005762 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005763 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005764 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5765 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005766 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005767
Ville Syrjälädfcab172014-06-13 13:37:47 +03005768 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005769}
5770
5771/* Adjust CDclk dividers to allow high res or save power if possible */
5772static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5773{
5774 struct drm_i915_private *dev_priv = dev->dev_private;
5775 u32 val, cmd;
5776
Vandana Kannan164dfd22014-11-24 13:37:41 +05305777 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5778 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005779
Ville Syrjälädfcab172014-06-13 13:37:47 +03005780 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005782 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005783 cmd = 1;
5784 else
5785 cmd = 0;
5786
5787 mutex_lock(&dev_priv->rps.hw_lock);
5788 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5789 val &= ~DSPFREQGUAR_MASK;
5790 val |= (cmd << DSPFREQGUAR_SHIFT);
5791 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5792 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5793 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5794 50)) {
5795 DRM_ERROR("timed out waiting for CDclk change\n");
5796 }
5797 mutex_unlock(&dev_priv->rps.hw_lock);
5798
Ville Syrjälä54433e92015-05-26 20:42:31 +03005799 mutex_lock(&dev_priv->sb_lock);
5800
Ville Syrjälädfcab172014-06-13 13:37:47 +03005801 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005802 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005803
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005804 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005805
Jesse Barnes30a970c2013-11-04 13:48:12 -08005806 /* adjust cdclk divider */
5807 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005808 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005809 val |= divider;
5810 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005811
5812 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5813 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5814 50))
5815 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005816 }
5817
Jesse Barnes30a970c2013-11-04 13:48:12 -08005818 /* adjust self-refresh exit latency value */
5819 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5820 val &= ~0x7f;
5821
5822 /*
5823 * For high bandwidth configs, we set a higher latency in the bunit
5824 * so that the core display fetch happens in time to avoid underruns.
5825 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005826 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005827 val |= 4500 / 250; /* 4.5 usec */
5828 else
5829 val |= 3000 / 250; /* 3.0 usec */
5830 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005831
Ville Syrjäläa5805162015-05-26 20:42:30 +03005832 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005833
Ville Syrjäläb6283052015-06-03 15:45:07 +03005834 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005835}
5836
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005837static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5838{
5839 struct drm_i915_private *dev_priv = dev->dev_private;
5840 u32 val, cmd;
5841
Vandana Kannan164dfd22014-11-24 13:37:41 +05305842 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5843 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005844
5845 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005846 case 333333:
5847 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005848 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005849 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005850 break;
5851 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005852 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005853 return;
5854 }
5855
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005856 /*
5857 * Specs are full of misinformation, but testing on actual
5858 * hardware has shown that we just need to write the desired
5859 * CCK divider into the Punit register.
5860 */
5861 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5862
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005863 mutex_lock(&dev_priv->rps.hw_lock);
5864 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5865 val &= ~DSPFREQGUAR_MASK_CHV;
5866 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5867 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5868 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5869 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5870 50)) {
5871 DRM_ERROR("timed out waiting for CDclk change\n");
5872 }
5873 mutex_unlock(&dev_priv->rps.hw_lock);
5874
Ville Syrjäläb6283052015-06-03 15:45:07 +03005875 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005876}
5877
Jesse Barnes30a970c2013-11-04 13:48:12 -08005878static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5879 int max_pixclk)
5880{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005881 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005882 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005883
Jesse Barnes30a970c2013-11-04 13:48:12 -08005884 /*
5885 * Really only a few cases to deal with, as only 4 CDclks are supported:
5886 * 200MHz
5887 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005888 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005889 * 400MHz (VLV only)
5890 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5891 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005892 *
5893 * We seem to get an unstable or solid color picture at 200MHz.
5894 * Not sure what's wrong. For now use 200MHz only when all pipes
5895 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005896 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005897 if (!IS_CHERRYVIEW(dev_priv) &&
5898 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005899 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005900 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005901 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005902 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005903 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005904 else
5905 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005906}
5907
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305908static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5909 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005910{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305911 /*
5912 * FIXME:
5913 * - remove the guardband, it's not needed on BXT
5914 * - set 19.2MHz bypass frequency if there are no active pipes
5915 */
5916 if (max_pixclk > 576000*9/10)
5917 return 624000;
5918 else if (max_pixclk > 384000*9/10)
5919 return 576000;
5920 else if (max_pixclk > 288000*9/10)
5921 return 384000;
5922 else if (max_pixclk > 144000*9/10)
5923 return 288000;
5924 else
5925 return 144000;
5926}
5927
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005928/* Compute the max pixel clock for new configuration. Uses atomic state if
5929 * that's non-NULL, look at current state otherwise. */
5930static int intel_mode_max_pixclk(struct drm_device *dev,
5931 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005932{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005933 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005934 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005935 int max_pixclk = 0;
5936
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005937 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005938 if (state)
5939 crtc_state =
5940 intel_atomic_get_crtc_state(state, intel_crtc);
5941 else
5942 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005943 if (IS_ERR(crtc_state))
5944 return PTR_ERR(crtc_state);
5945
5946 if (!crtc_state->base.enable)
5947 continue;
5948
5949 max_pixclk = max(max_pixclk,
5950 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005951 }
5952
5953 return max_pixclk;
5954}
5955
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005956static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005957{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005958 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005959 struct drm_crtc *crtc;
5960 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005961 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005962 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005963
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005964 if (max_pixclk < 0)
5965 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005966
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305967 if (IS_VALLEYVIEW(dev_priv))
5968 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5969 else
5970 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5971
5972 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005973 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005974
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005975 /* add all active pipes to the state */
5976 for_each_crtc(state->dev, crtc) {
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005977 if (!crtc->state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005978 continue;
5979
5980 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5981 if (IS_ERR(crtc_state))
5982 return PTR_ERR(crtc_state);
5983 }
5984
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005985 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005986 for_each_crtc_in_state(state, crtc, crtc_state, i)
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02005987 if (crtc_state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005988 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005989
5990 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005991}
5992
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02005993static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
5994{
5995 unsigned int credits, default_credits;
5996
5997 if (IS_CHERRYVIEW(dev_priv))
5998 default_credits = PFI_CREDIT(12);
5999 else
6000 default_credits = PFI_CREDIT(8);
6001
Vandana Kannan164dfd22014-11-24 13:37:41 +05306002 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006003 /* CHV suggested value is 31 or 63 */
6004 if (IS_CHERRYVIEW(dev_priv))
6005 credits = PFI_CREDIT_31;
6006 else
6007 credits = PFI_CREDIT(15);
6008 } else {
6009 credits = default_credits;
6010 }
6011
6012 /*
6013 * WA - write default credits before re-programming
6014 * FIXME: should we also set the resend bit here?
6015 */
6016 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6017 default_credits);
6018
6019 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6020 credits | PFI_CREDIT_RESEND);
6021
6022 /*
6023 * FIXME is this guaranteed to clear
6024 * immediately or should we poll for it?
6025 */
6026 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6027}
6028
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006029static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006030{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006031 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006032 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006033 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006034 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006035
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006036 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
6037 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006038 if (WARN_ON(max_pixclk < 0))
6039 return;
6040
6041 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006042
Vandana Kannan164dfd22014-11-24 13:37:41 +05306043 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02006044 /*
6045 * FIXME: We can end up here with all power domains off, yet
6046 * with a CDCLK frequency other than the minimum. To account
6047 * for this take the PIPE-A power domain, which covers the HW
6048 * blocks needed for the following programming. This can be
6049 * removed once it's guaranteed that we get here either with
6050 * the minimum CDCLK set, or the required power domains
6051 * enabled.
6052 */
6053 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6054
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006055 if (IS_CHERRYVIEW(dev))
6056 cherryview_set_cdclk(dev, req_cdclk);
6057 else
6058 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006059
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006060 vlv_program_pfi_credits(dev_priv);
6061
Imre Deak738c05c2014-11-19 16:25:37 +02006062 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006063 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006064}
6065
Jesse Barnes89b667f2013-04-18 14:51:36 -07006066static void valleyview_crtc_enable(struct drm_crtc *crtc)
6067{
6068 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006069 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006070 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6071 struct intel_encoder *encoder;
6072 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006073 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006074
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006075 if (WARN_ON(intel_crtc->active))
Jesse Barnes89b667f2013-04-18 14:51:36 -07006076 return;
6077
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006078 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306079
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006080 if (!is_dsi) {
6081 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006082 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006083 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006084 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006085 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006086
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006087 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306088 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006089
6090 intel_set_pipe_timings(intel_crtc);
6091
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006092 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6093 struct drm_i915_private *dev_priv = dev->dev_private;
6094
6095 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6096 I915_WRITE(CHV_CANVAS(pipe), 0);
6097 }
6098
Daniel Vetter5b18e572014-04-24 23:55:06 +02006099 i9xx_set_pipeconf(intel_crtc);
6100
Jesse Barnes89b667f2013-04-18 14:51:36 -07006101 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006102
Daniel Vettera72e4c92014-09-30 10:56:47 +02006103 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006104
Jesse Barnes89b667f2013-04-18 14:51:36 -07006105 for_each_encoder_on_crtc(dev, crtc, encoder)
6106 if (encoder->pre_pll_enable)
6107 encoder->pre_pll_enable(encoder);
6108
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006109 if (!is_dsi) {
6110 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006111 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006112 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006113 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006114 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006115
6116 for_each_encoder_on_crtc(dev, crtc, encoder)
6117 if (encoder->pre_enable)
6118 encoder->pre_enable(encoder);
6119
Jesse Barnes2dd24552013-04-25 12:55:01 -07006120 i9xx_pfit_enable(intel_crtc);
6121
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006122 intel_crtc_load_lut(crtc);
6123
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006124 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006125 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006126
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006127 assert_vblank_disabled(crtc);
6128 drm_crtc_vblank_on(crtc);
6129
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006130 for_each_encoder_on_crtc(dev, crtc, encoder)
6131 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006132}
6133
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006134static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6135{
6136 struct drm_device *dev = crtc->base.dev;
6137 struct drm_i915_private *dev_priv = dev->dev_private;
6138
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006139 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6140 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006141}
6142
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006143static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006144{
6145 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006146 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006148 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006149 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006150
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006151 if (WARN_ON(intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006152 return;
6153
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006154 i9xx_set_pll_dividers(intel_crtc);
6155
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006156 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05306157 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006158
6159 intel_set_pipe_timings(intel_crtc);
6160
Daniel Vetter5b18e572014-04-24 23:55:06 +02006161 i9xx_set_pipeconf(intel_crtc);
6162
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006163 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006164
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006165 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006166 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006167
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006168 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006169 if (encoder->pre_enable)
6170 encoder->pre_enable(encoder);
6171
Daniel Vetterf6736a12013-06-05 13:34:30 +02006172 i9xx_enable_pll(intel_crtc);
6173
Jesse Barnes2dd24552013-04-25 12:55:01 -07006174 i9xx_pfit_enable(intel_crtc);
6175
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006176 intel_crtc_load_lut(crtc);
6177
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006178 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006179 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006180
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006181 assert_vblank_disabled(crtc);
6182 drm_crtc_vblank_on(crtc);
6183
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006184 for_each_encoder_on_crtc(dev, crtc, encoder)
6185 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006186}
6187
Daniel Vetter87476d62013-04-11 16:29:06 +02006188static void i9xx_pfit_disable(struct intel_crtc *crtc)
6189{
6190 struct drm_device *dev = crtc->base.dev;
6191 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006192
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006193 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006194 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006195
6196 assert_pipe_disabled(dev_priv, crtc->pipe);
6197
Daniel Vetter328d8e82013-05-08 10:36:31 +02006198 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6199 I915_READ(PFIT_CONTROL));
6200 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006201}
6202
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006203static void i9xx_crtc_disable(struct drm_crtc *crtc)
6204{
6205 struct drm_device *dev = crtc->dev;
6206 struct drm_i915_private *dev_priv = dev->dev_private;
6207 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006208 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006209 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006210
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +02006211 if (WARN_ON(!intel_crtc->active))
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006212 return;
6213
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006214 /*
6215 * On gen2 planes are double buffered but the pipe isn't, so we must
6216 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006217 * We also need to wait on all gmch platforms because of the
6218 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006219 */
Imre Deak564ed192014-06-13 14:54:21 +03006220 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006221
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006222 for_each_encoder_on_crtc(dev, crtc, encoder)
6223 encoder->disable(encoder);
6224
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006225 drm_crtc_vblank_off(crtc);
6226 assert_vblank_disabled(crtc);
6227
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006228 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006229
Daniel Vetter87476d62013-04-11 16:29:06 +02006230 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006231
Jesse Barnes89b667f2013-04-18 14:51:36 -07006232 for_each_encoder_on_crtc(dev, crtc, encoder)
6233 if (encoder->post_disable)
6234 encoder->post_disable(encoder);
6235
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006236 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006237 if (IS_CHERRYVIEW(dev))
6238 chv_disable_pll(dev_priv, pipe);
6239 else if (IS_VALLEYVIEW(dev))
6240 vlv_disable_pll(dev_priv, pipe);
6241 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006242 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006243 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006244
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006245 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006246 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006247
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006248 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006249 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006250
Daniel Vetterefa96242014-04-24 23:55:02 +02006251 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006252 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006253 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006254}
6255
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006256/*
6257 * turn all crtc's off, but do not adjust state
6258 * This has to be paired with a call to intel_modeset_setup_hw_state.
6259 */
6260void intel_display_suspend(struct drm_device *dev)
6261{
6262 struct drm_i915_private *dev_priv = to_i915(dev);
6263 struct drm_crtc *crtc;
6264
6265 for_each_crtc(dev, crtc) {
6266 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6267 enum intel_display_power_domain domain;
6268 unsigned long domains;
6269
6270 if (!intel_crtc->active)
6271 continue;
6272
6273 intel_crtc_disable_planes(crtc);
6274 dev_priv->display.crtc_disable(crtc);
6275
6276 domains = intel_crtc->enabled_power_domains;
6277 for_each_power_domain(domain, domains)
6278 intel_display_power_put(dev_priv, domain);
6279 intel_crtc->enabled_power_domains = 0;
6280 }
6281}
6282
Borun Fub04c5bd2014-07-12 10:02:27 +05306283/* Master function to enable/disable CRTC and corresponding power wells */
6284void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006285{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006286 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006287 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006289 enum intel_display_power_domain domain;
6290 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006291
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006292 if (enable == intel_crtc->active)
6293 return;
6294
6295 if (enable && !crtc->state->enable)
6296 return;
6297
6298 crtc->state->active = enable;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006299 if (enable) {
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006300 domains = get_crtc_power_domains(crtc);
6301 for_each_power_domain(domain, domains)
6302 intel_display_power_get(dev_priv, domain);
6303 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006304
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006305 dev_priv->display.crtc_enable(crtc);
6306 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006307 } else {
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006308 intel_crtc_disable_planes(crtc);
6309 dev_priv->display.crtc_disable(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006310
Maarten Lankhorst1b509252015-06-01 12:49:48 +02006311 domains = intel_crtc->enabled_power_domains;
6312 for_each_power_domain(domain, domains)
6313 intel_display_power_put(dev_priv, domain);
6314 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006315 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306316}
6317
6318/**
6319 * Sets the power management mode of the pipe and plane.
6320 */
6321void intel_crtc_update_dpms(struct drm_crtc *crtc)
6322{
6323 struct drm_device *dev = crtc->dev;
6324 struct intel_encoder *intel_encoder;
6325 bool enable = false;
6326
6327 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6328 enable |= intel_encoder->connectors_active;
6329
6330 intel_crtc_control(crtc, enable);
Daniel Vetter976f8a22012-07-08 22:34:21 +02006331}
6332
Chris Wilsonea5b2132010-08-04 13:50:23 +01006333void intel_encoder_destroy(struct drm_encoder *encoder)
6334{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006335 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006336
Chris Wilsonea5b2132010-08-04 13:50:23 +01006337 drm_encoder_cleanup(encoder);
6338 kfree(intel_encoder);
6339}
6340
Damien Lespiau92373292013-08-08 22:28:57 +01006341/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006342 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6343 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006344static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006345{
6346 if (mode == DRM_MODE_DPMS_ON) {
6347 encoder->connectors_active = true;
6348
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006349 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006350 } else {
6351 encoder->connectors_active = false;
6352
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006353 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006354 }
6355}
6356
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006357/* Cross check the actual hw state with our own modeset state tracking (and it's
6358 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006359static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006360{
6361 if (connector->get_hw_state(connector)) {
6362 struct intel_encoder *encoder = connector->encoder;
6363 struct drm_crtc *crtc;
6364 bool encoder_enabled;
6365 enum pipe pipe;
6366
6367 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6368 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006369 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006370
Dave Airlie0e32b392014-05-02 14:02:48 +10006371 /* there is no real hw state for MST connectors */
6372 if (connector->mst_port)
6373 return;
6374
Rob Clarke2c719b2014-12-15 13:56:32 -05006375 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006376 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006377 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006378 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006379
Dave Airlie36cd7442014-05-02 13:44:18 +10006380 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006381 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006382 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006383
Dave Airlie36cd7442014-05-02 13:44:18 +10006384 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006385 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6386 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006387 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006388
Dave Airlie36cd7442014-05-02 13:44:18 +10006389 crtc = encoder->base.crtc;
6390
Matt Roper83d65732015-02-25 13:12:16 -08006391 I915_STATE_WARN(!crtc->state->enable,
6392 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006393 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6394 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006395 "encoder active on the wrong pipe\n");
6396 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006397 }
6398}
6399
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006400int intel_connector_init(struct intel_connector *connector)
6401{
6402 struct drm_connector_state *connector_state;
6403
6404 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6405 if (!connector_state)
6406 return -ENOMEM;
6407
6408 connector->base.state = connector_state;
6409 return 0;
6410}
6411
6412struct intel_connector *intel_connector_alloc(void)
6413{
6414 struct intel_connector *connector;
6415
6416 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6417 if (!connector)
6418 return NULL;
6419
6420 if (intel_connector_init(connector) < 0) {
6421 kfree(connector);
6422 return NULL;
6423 }
6424
6425 return connector;
6426}
6427
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006428/* Even simpler default implementation, if there's really no special case to
6429 * consider. */
6430void intel_connector_dpms(struct drm_connector *connector, int mode)
6431{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006432 /* All the simple cases only support two dpms states. */
6433 if (mode != DRM_MODE_DPMS_ON)
6434 mode = DRM_MODE_DPMS_OFF;
6435
6436 if (mode == connector->dpms)
6437 return;
6438
6439 connector->dpms = mode;
6440
6441 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dcf2013-09-29 19:15:07 +01006442 if (connector->encoder)
6443 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006444
Daniel Vetterb9805142012-08-31 17:37:33 +02006445 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006446}
6447
Daniel Vetterf0947c32012-07-02 13:10:34 +02006448/* Simple connector->get_hw_state implementation for encoders that support only
6449 * one connector and no cloning and hence the encoder state determines the state
6450 * of the connector. */
6451bool intel_connector_get_hw_state(struct intel_connector *connector)
6452{
Daniel Vetter24929352012-07-02 20:28:59 +02006453 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006454 struct intel_encoder *encoder = connector->encoder;
6455
6456 return encoder->get_hw_state(encoder, &pipe);
6457}
6458
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006459static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006460{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006461 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6462 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006463
6464 return 0;
6465}
6466
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006467static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006468 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006469{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006470 struct drm_atomic_state *state = pipe_config->base.state;
6471 struct intel_crtc *other_crtc;
6472 struct intel_crtc_state *other_crtc_state;
6473
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006474 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6475 pipe_name(pipe), pipe_config->fdi_lanes);
6476 if (pipe_config->fdi_lanes > 4) {
6477 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6478 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006479 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006480 }
6481
Paulo Zanonibafb6552013-11-02 21:07:44 -07006482 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006483 if (pipe_config->fdi_lanes > 2) {
6484 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6485 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006486 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006487 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006488 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006489 }
6490 }
6491
6492 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006493 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006494
6495 /* Ivybridge 3 pipe is really complicated */
6496 switch (pipe) {
6497 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006498 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006499 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006500 if (pipe_config->fdi_lanes <= 2)
6501 return 0;
6502
6503 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6504 other_crtc_state =
6505 intel_atomic_get_crtc_state(state, other_crtc);
6506 if (IS_ERR(other_crtc_state))
6507 return PTR_ERR(other_crtc_state);
6508
6509 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006510 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6511 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006512 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006513 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006514 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006515 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006516 if (pipe_config->fdi_lanes > 2) {
6517 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6518 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006519 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006520 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006521
6522 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6523 other_crtc_state =
6524 intel_atomic_get_crtc_state(state, other_crtc);
6525 if (IS_ERR(other_crtc_state))
6526 return PTR_ERR(other_crtc_state);
6527
6528 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006529 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006530 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006531 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006532 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006533 default:
6534 BUG();
6535 }
6536}
6537
Daniel Vettere29c22c2013-02-21 00:00:16 +01006538#define RETRY 1
6539static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006540 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006541{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006542 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006543 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006544 int lane, link_bw, fdi_dotclock, ret;
6545 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006546
Daniel Vettere29c22c2013-02-21 00:00:16 +01006547retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006548 /* FDI is a binary signal running at ~2.7GHz, encoding
6549 * each output octet as 10 bits. The actual frequency
6550 * is stored as a divider into a 100MHz clock, and the
6551 * mode pixel clock is stored in units of 1KHz.
6552 * Hence the bw of each lane in terms of the mode signal
6553 * is:
6554 */
6555 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6556
Damien Lespiau241bfc32013-09-25 16:45:37 +01006557 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006558
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006559 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006560 pipe_config->pipe_bpp);
6561
6562 pipe_config->fdi_lanes = lane;
6563
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006564 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006565 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006566
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006567 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6568 intel_crtc->pipe, pipe_config);
6569 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006570 pipe_config->pipe_bpp -= 2*3;
6571 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6572 pipe_config->pipe_bpp);
6573 needs_recompute = true;
6574 pipe_config->bw_constrained = true;
6575
6576 goto retry;
6577 }
6578
6579 if (needs_recompute)
6580 return RETRY;
6581
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006582 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006583}
6584
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006585static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6586 struct intel_crtc_state *pipe_config)
6587{
6588 if (pipe_config->pipe_bpp > 24)
6589 return false;
6590
6591 /* HSW can handle pixel rate up to cdclk? */
6592 if (IS_HASWELL(dev_priv->dev))
6593 return true;
6594
6595 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006596 * We compare against max which means we must take
6597 * the increased cdclk requirement into account when
6598 * calculating the new cdclk.
6599 *
6600 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006601 */
6602 return ilk_pipe_pixel_rate(pipe_config) <=
6603 dev_priv->max_cdclk_freq * 95 / 100;
6604}
6605
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006606static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006607 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006608{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006609 struct drm_device *dev = crtc->base.dev;
6610 struct drm_i915_private *dev_priv = dev->dev_private;
6611
Jani Nikulad330a952014-01-21 11:24:25 +02006612 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006613 hsw_crtc_supports_ips(crtc) &&
6614 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006615}
6616
Daniel Vettera43f6e02013-06-07 23:10:32 +02006617static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006618 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006619{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006620 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006621 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006622 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006623 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006624
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006625 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006626 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006627 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006628
6629 /*
6630 * Enable pixel doubling when the dot clock
6631 * is > 90% of the (display) core speed.
6632 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006633 * GDG double wide on either pipe,
6634 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006635 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006636 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006637 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006638 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006639 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006640 }
6641
Damien Lespiau241bfc32013-09-25 16:45:37 +01006642 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006643 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006644 }
Chris Wilson89749352010-09-12 18:25:19 +01006645
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006646 /*
6647 * Pipe horizontal size must be even in:
6648 * - DVO ganged mode
6649 * - LVDS dual channel mode
6650 * - Double wide pipe
6651 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006652 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006653 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6654 pipe_config->pipe_src_w &= ~1;
6655
Damien Lespiau8693a822013-05-03 18:48:11 +01006656 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6657 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006658 */
6659 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6660 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006661 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006662
Damien Lespiauf5adf942013-06-24 18:29:34 +01006663 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006664 hsw_compute_ips_config(crtc, pipe_config);
6665
Daniel Vetter877d48d2013-04-19 11:24:43 +02006666 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006667 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006668
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006669 /* FIXME: remove below call once atomic mode set is place and all crtc
6670 * related checks called from atomic_crtc_check function */
6671 ret = 0;
6672 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6673 crtc, pipe_config->base.state);
6674 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6675
6676 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006677}
6678
Ville Syrjälä1652d192015-03-31 14:12:01 +03006679static int skylake_get_display_clock_speed(struct drm_device *dev)
6680{
6681 struct drm_i915_private *dev_priv = to_i915(dev);
6682 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6683 uint32_t cdctl = I915_READ(CDCLK_CTL);
6684 uint32_t linkrate;
6685
Damien Lespiau414355a2015-06-04 18:21:31 +01006686 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006687 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006688
6689 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6690 return 540000;
6691
6692 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006693 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006694
Damien Lespiau71cd8422015-04-30 16:39:17 +01006695 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6696 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006697 /* vco 8640 */
6698 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6699 case CDCLK_FREQ_450_432:
6700 return 432000;
6701 case CDCLK_FREQ_337_308:
6702 return 308570;
6703 case CDCLK_FREQ_675_617:
6704 return 617140;
6705 default:
6706 WARN(1, "Unknown cd freq selection\n");
6707 }
6708 } else {
6709 /* vco 8100 */
6710 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6711 case CDCLK_FREQ_450_432:
6712 return 450000;
6713 case CDCLK_FREQ_337_308:
6714 return 337500;
6715 case CDCLK_FREQ_675_617:
6716 return 675000;
6717 default:
6718 WARN(1, "Unknown cd freq selection\n");
6719 }
6720 }
6721
6722 /* error case, do as if DPLL0 isn't enabled */
6723 return 24000;
6724}
6725
6726static int broadwell_get_display_clock_speed(struct drm_device *dev)
6727{
6728 struct drm_i915_private *dev_priv = dev->dev_private;
6729 uint32_t lcpll = I915_READ(LCPLL_CTL);
6730 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6731
6732 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6733 return 800000;
6734 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6735 return 450000;
6736 else if (freq == LCPLL_CLK_FREQ_450)
6737 return 450000;
6738 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6739 return 540000;
6740 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6741 return 337500;
6742 else
6743 return 675000;
6744}
6745
6746static int haswell_get_display_clock_speed(struct drm_device *dev)
6747{
6748 struct drm_i915_private *dev_priv = dev->dev_private;
6749 uint32_t lcpll = I915_READ(LCPLL_CTL);
6750 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6751
6752 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6753 return 800000;
6754 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6755 return 450000;
6756 else if (freq == LCPLL_CLK_FREQ_450)
6757 return 450000;
6758 else if (IS_HSW_ULT(dev))
6759 return 337500;
6760 else
6761 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006762}
6763
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006764static int valleyview_get_display_clock_speed(struct drm_device *dev)
6765{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006766 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006767 u32 val;
6768 int divider;
6769
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006770 if (dev_priv->hpll_freq == 0)
6771 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6772
Ville Syrjäläa5805162015-05-26 20:42:30 +03006773 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006774 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006775 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006776
6777 divider = val & DISPLAY_FREQUENCY_VALUES;
6778
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006779 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6780 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6781 "cdclk change in progress\n");
6782
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006783 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006784}
6785
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006786static int ilk_get_display_clock_speed(struct drm_device *dev)
6787{
6788 return 450000;
6789}
6790
Jesse Barnese70236a2009-09-21 10:42:27 -07006791static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006792{
Jesse Barnese70236a2009-09-21 10:42:27 -07006793 return 400000;
6794}
Jesse Barnes79e53942008-11-07 14:24:08 -08006795
Jesse Barnese70236a2009-09-21 10:42:27 -07006796static int i915_get_display_clock_speed(struct drm_device *dev)
6797{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006798 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006799}
Jesse Barnes79e53942008-11-07 14:24:08 -08006800
Jesse Barnese70236a2009-09-21 10:42:27 -07006801static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6802{
6803 return 200000;
6804}
Jesse Barnes79e53942008-11-07 14:24:08 -08006805
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006806static int pnv_get_display_clock_speed(struct drm_device *dev)
6807{
6808 u16 gcfgc = 0;
6809
6810 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6811
6812 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6813 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006814 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006815 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006816 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006817 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006818 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006819 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6820 return 200000;
6821 default:
6822 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6823 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006824 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006825 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006826 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006827 }
6828}
6829
Jesse Barnese70236a2009-09-21 10:42:27 -07006830static int i915gm_get_display_clock_speed(struct drm_device *dev)
6831{
6832 u16 gcfgc = 0;
6833
6834 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6835
6836 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006837 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006838 else {
6839 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6840 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006841 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006842 default:
6843 case GC_DISPLAY_CLOCK_190_200_MHZ:
6844 return 190000;
6845 }
6846 }
6847}
Jesse Barnes79e53942008-11-07 14:24:08 -08006848
Jesse Barnese70236a2009-09-21 10:42:27 -07006849static int i865_get_display_clock_speed(struct drm_device *dev)
6850{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006851 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006852}
6853
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006854static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006855{
6856 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006857
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006858 /*
6859 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6860 * encoding is different :(
6861 * FIXME is this the right way to detect 852GM/852GMV?
6862 */
6863 if (dev->pdev->revision == 0x1)
6864 return 133333;
6865
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006866 pci_bus_read_config_word(dev->pdev->bus,
6867 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6868
Jesse Barnese70236a2009-09-21 10:42:27 -07006869 /* Assume that the hardware is in the high speed state. This
6870 * should be the default.
6871 */
6872 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6873 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006874 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006875 case GC_CLOCK_100_200:
6876 return 200000;
6877 case GC_CLOCK_166_250:
6878 return 250000;
6879 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006880 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006881 case GC_CLOCK_133_266:
6882 case GC_CLOCK_133_266_2:
6883 case GC_CLOCK_166_266:
6884 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006885 }
6886
6887 /* Shouldn't happen */
6888 return 0;
6889}
6890
6891static int i830_get_display_clock_speed(struct drm_device *dev)
6892{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006893 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006894}
6895
Ville Syrjälä34edce22015-05-22 11:22:33 +03006896static unsigned int intel_hpll_vco(struct drm_device *dev)
6897{
6898 struct drm_i915_private *dev_priv = dev->dev_private;
6899 static const unsigned int blb_vco[8] = {
6900 [0] = 3200000,
6901 [1] = 4000000,
6902 [2] = 5333333,
6903 [3] = 4800000,
6904 [4] = 6400000,
6905 };
6906 static const unsigned int pnv_vco[8] = {
6907 [0] = 3200000,
6908 [1] = 4000000,
6909 [2] = 5333333,
6910 [3] = 4800000,
6911 [4] = 2666667,
6912 };
6913 static const unsigned int cl_vco[8] = {
6914 [0] = 3200000,
6915 [1] = 4000000,
6916 [2] = 5333333,
6917 [3] = 6400000,
6918 [4] = 3333333,
6919 [5] = 3566667,
6920 [6] = 4266667,
6921 };
6922 static const unsigned int elk_vco[8] = {
6923 [0] = 3200000,
6924 [1] = 4000000,
6925 [2] = 5333333,
6926 [3] = 4800000,
6927 };
6928 static const unsigned int ctg_vco[8] = {
6929 [0] = 3200000,
6930 [1] = 4000000,
6931 [2] = 5333333,
6932 [3] = 6400000,
6933 [4] = 2666667,
6934 [5] = 4266667,
6935 };
6936 const unsigned int *vco_table;
6937 unsigned int vco;
6938 uint8_t tmp = 0;
6939
6940 /* FIXME other chipsets? */
6941 if (IS_GM45(dev))
6942 vco_table = ctg_vco;
6943 else if (IS_G4X(dev))
6944 vco_table = elk_vco;
6945 else if (IS_CRESTLINE(dev))
6946 vco_table = cl_vco;
6947 else if (IS_PINEVIEW(dev))
6948 vco_table = pnv_vco;
6949 else if (IS_G33(dev))
6950 vco_table = blb_vco;
6951 else
6952 return 0;
6953
6954 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6955
6956 vco = vco_table[tmp & 0x7];
6957 if (vco == 0)
6958 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6959 else
6960 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6961
6962 return vco;
6963}
6964
6965static int gm45_get_display_clock_speed(struct drm_device *dev)
6966{
6967 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6968 uint16_t tmp = 0;
6969
6970 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6971
6972 cdclk_sel = (tmp >> 12) & 0x1;
6973
6974 switch (vco) {
6975 case 2666667:
6976 case 4000000:
6977 case 5333333:
6978 return cdclk_sel ? 333333 : 222222;
6979 case 3200000:
6980 return cdclk_sel ? 320000 : 228571;
6981 default:
6982 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6983 return 222222;
6984 }
6985}
6986
6987static int i965gm_get_display_clock_speed(struct drm_device *dev)
6988{
6989 static const uint8_t div_3200[] = { 16, 10, 8 };
6990 static const uint8_t div_4000[] = { 20, 12, 10 };
6991 static const uint8_t div_5333[] = { 24, 16, 14 };
6992 const uint8_t *div_table;
6993 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6994 uint16_t tmp = 0;
6995
6996 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6997
6998 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
6999
7000 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7001 goto fail;
7002
7003 switch (vco) {
7004 case 3200000:
7005 div_table = div_3200;
7006 break;
7007 case 4000000:
7008 div_table = div_4000;
7009 break;
7010 case 5333333:
7011 div_table = div_5333;
7012 break;
7013 default:
7014 goto fail;
7015 }
7016
7017 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7018
7019 fail:
7020 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7021 return 200000;
7022}
7023
7024static int g33_get_display_clock_speed(struct drm_device *dev)
7025{
7026 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7027 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7028 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7029 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7030 const uint8_t *div_table;
7031 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7032 uint16_t tmp = 0;
7033
7034 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7035
7036 cdclk_sel = (tmp >> 4) & 0x7;
7037
7038 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7039 goto fail;
7040
7041 switch (vco) {
7042 case 3200000:
7043 div_table = div_3200;
7044 break;
7045 case 4000000:
7046 div_table = div_4000;
7047 break;
7048 case 4800000:
7049 div_table = div_4800;
7050 break;
7051 case 5333333:
7052 div_table = div_5333;
7053 break;
7054 default:
7055 goto fail;
7056 }
7057
7058 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7059
7060 fail:
7061 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7062 return 190476;
7063}
7064
Zhenyu Wang2c072452009-06-05 15:38:42 +08007065static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007066intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007067{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007068 while (*num > DATA_LINK_M_N_MASK ||
7069 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007070 *num >>= 1;
7071 *den >>= 1;
7072 }
7073}
7074
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007075static void compute_m_n(unsigned int m, unsigned int n,
7076 uint32_t *ret_m, uint32_t *ret_n)
7077{
7078 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7079 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7080 intel_reduce_m_n_ratio(ret_m, ret_n);
7081}
7082
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007083void
7084intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7085 int pixel_clock, int link_clock,
7086 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007087{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007088 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007089
7090 compute_m_n(bits_per_pixel * pixel_clock,
7091 link_clock * nlanes * 8,
7092 &m_n->gmch_m, &m_n->gmch_n);
7093
7094 compute_m_n(pixel_clock, link_clock,
7095 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007096}
7097
Chris Wilsona7615032011-01-12 17:04:08 +00007098static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7099{
Jani Nikulad330a952014-01-21 11:24:25 +02007100 if (i915.panel_use_ssc >= 0)
7101 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007102 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007103 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007104}
7105
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007106static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7107 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007108{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007109 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007110 struct drm_i915_private *dev_priv = dev->dev_private;
7111 int refclk;
7112
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007113 WARN_ON(!crtc_state->base.state);
7114
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007115 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007116 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007117 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007118 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007119 refclk = dev_priv->vbt.lvds_ssc_freq;
7120 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007121 } else if (!IS_GEN2(dev)) {
7122 refclk = 96000;
7123 } else {
7124 refclk = 48000;
7125 }
7126
7127 return refclk;
7128}
7129
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007130static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007131{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007132 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007133}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007134
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007135static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7136{
7137 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007138}
7139
Daniel Vetterf47709a2013-03-28 10:42:02 +01007140static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007141 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007142 intel_clock_t *reduced_clock)
7143{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007144 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007145 u32 fp, fp2 = 0;
7146
7147 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007148 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007149 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007150 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007151 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007152 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007153 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007154 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007155 }
7156
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007157 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007158
Daniel Vetterf47709a2013-03-28 10:42:02 +01007159 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007160 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007161 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007162 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007163 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007164 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007165 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007166 }
7167}
7168
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007169static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7170 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007171{
7172 u32 reg_val;
7173
7174 /*
7175 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7176 * and set it to a reasonable value instead.
7177 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007178 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007179 reg_val &= 0xffffff00;
7180 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007181 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007182
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007183 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007184 reg_val &= 0x8cffffff;
7185 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007186 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007187
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007188 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007189 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007190 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007191
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007192 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007193 reg_val &= 0x00ffffff;
7194 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007195 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007196}
7197
Daniel Vetterb5518422013-05-03 11:49:48 +02007198static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7199 struct intel_link_m_n *m_n)
7200{
7201 struct drm_device *dev = crtc->base.dev;
7202 struct drm_i915_private *dev_priv = dev->dev_private;
7203 int pipe = crtc->pipe;
7204
Daniel Vettere3b95f12013-05-03 11:49:49 +02007205 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7206 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7207 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7208 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007209}
7210
7211static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007212 struct intel_link_m_n *m_n,
7213 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007214{
7215 struct drm_device *dev = crtc->base.dev;
7216 struct drm_i915_private *dev_priv = dev->dev_private;
7217 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007218 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007219
7220 if (INTEL_INFO(dev)->gen >= 5) {
7221 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7222 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7223 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7224 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007225 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7226 * for gen < 8) and if DRRS is supported (to make sure the
7227 * registers are not unnecessarily accessed).
7228 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307229 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007230 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007231 I915_WRITE(PIPE_DATA_M2(transcoder),
7232 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7233 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7234 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7235 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7236 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007237 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007238 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7239 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7240 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7241 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007242 }
7243}
7244
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307245void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007246{
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307247 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7248
7249 if (m_n == M1_N1) {
7250 dp_m_n = &crtc->config->dp_m_n;
7251 dp_m2_n2 = &crtc->config->dp_m2_n2;
7252 } else if (m_n == M2_N2) {
7253
7254 /*
7255 * M2_N2 registers are not supported. Hence m2_n2 divider value
7256 * needs to be programmed into M1_N1.
7257 */
7258 dp_m_n = &crtc->config->dp_m2_n2;
7259 } else {
7260 DRM_ERROR("Unsupported divider value\n");
7261 return;
7262 }
7263
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007264 if (crtc->config->has_pch_encoder)
7265 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007266 else
Ramalingam Cfe3cd48d2015-02-13 15:32:59 +05307267 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007268}
7269
Ville Syrjäläd288f652014-10-28 13:20:22 +02007270static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007271 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007272{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007273 u32 dpll, dpll_md;
7274
7275 /*
7276 * Enable DPIO clock input. We should never disable the reference
7277 * clock for pipe B, since VGA hotplug / manual detection depends
7278 * on it.
7279 */
7280 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7281 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7282 /* We should never disable this, set it here for state tracking */
7283 if (crtc->pipe == PIPE_B)
7284 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7285 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007286 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007287
Ville Syrjäläd288f652014-10-28 13:20:22 +02007288 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007289 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007290 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007291}
7292
Ville Syrjäläd288f652014-10-28 13:20:22 +02007293static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007294 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007295{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007296 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007297 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007298 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007299 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007300 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007301 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007302
Ville Syrjäläa5805162015-05-26 20:42:30 +03007303 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007304
Ville Syrjäläd288f652014-10-28 13:20:22 +02007305 bestn = pipe_config->dpll.n;
7306 bestm1 = pipe_config->dpll.m1;
7307 bestm2 = pipe_config->dpll.m2;
7308 bestp1 = pipe_config->dpll.p1;
7309 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007310
Jesse Barnes89b667f2013-04-18 14:51:36 -07007311 /* See eDP HDMI DPIO driver vbios notes doc */
7312
7313 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007314 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007315 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007316
7317 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007318 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007319
7320 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007321 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007322 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007323 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007324
7325 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007326 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007327
7328 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007329 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7330 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7331 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007332 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007333
7334 /*
7335 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7336 * but we don't support that).
7337 * Note: don't use the DAC post divider as it seems unstable.
7338 */
7339 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007340 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007341
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007342 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007343 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007344
Jesse Barnes89b667f2013-04-18 14:51:36 -07007345 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007346 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007347 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7348 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007349 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b0122013-07-05 19:21:38 +03007350 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007351 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007352 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007353 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007354
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007355 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007356 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007357 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007358 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007359 0x0df40000);
7360 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007361 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007362 0x0df70000);
7363 } else { /* HDMI or VGA */
7364 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007365 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007366 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007367 0x0df70000);
7368 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007369 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007370 0x0df40000);
7371 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007372
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007373 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007374 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007375 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7376 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007377 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007378 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007379
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007380 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007381 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007382}
7383
Ville Syrjäläd288f652014-10-28 13:20:22 +02007384static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007385 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007386{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007387 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007388 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7389 DPLL_VCO_ENABLE;
7390 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007391 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007392
Ville Syrjäläd288f652014-10-28 13:20:22 +02007393 pipe_config->dpll_hw_state.dpll_md =
7394 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007395}
7396
Ville Syrjäläd288f652014-10-28 13:20:22 +02007397static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007398 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007399{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007400 struct drm_device *dev = crtc->base.dev;
7401 struct drm_i915_private *dev_priv = dev->dev_private;
7402 int pipe = crtc->pipe;
7403 int dpll_reg = DPLL(crtc->pipe);
7404 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307405 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007406 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307407 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307408 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007409
Ville Syrjäläd288f652014-10-28 13:20:22 +02007410 bestn = pipe_config->dpll.n;
7411 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7412 bestm1 = pipe_config->dpll.m1;
7413 bestm2 = pipe_config->dpll.m2 >> 22;
7414 bestp1 = pipe_config->dpll.p1;
7415 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307416 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307417 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307418 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007419
7420 /*
7421 * Enable Refclk and SSC
7422 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007423 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007424 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007425
Ville Syrjäläa5805162015-05-26 20:42:30 +03007426 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007427
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007428 /* p1 and p2 divider */
7429 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7430 5 << DPIO_CHV_S1_DIV_SHIFT |
7431 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7432 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7433 1 << DPIO_CHV_K_DIV_SHIFT);
7434
7435 /* Feedback post-divider - m2 */
7436 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7437
7438 /* Feedback refclk divider - n and m1 */
7439 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7440 DPIO_CHV_M1_DIV_BY_2 |
7441 1 << DPIO_CHV_N_DIV_SHIFT);
7442
7443 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307444 if (bestm2_frac)
7445 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007446
7447 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307448 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7449 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7450 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7451 if (bestm2_frac)
7452 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7453 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007454
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307455 /* Program digital lock detect threshold */
7456 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7457 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7458 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7459 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7460 if (!bestm2_frac)
7461 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7462 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7463
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007464 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307465 if (vco == 5400000) {
7466 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7467 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7468 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7469 tribuf_calcntr = 0x9;
7470 } else if (vco <= 6200000) {
7471 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7472 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7473 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7474 tribuf_calcntr = 0x9;
7475 } else if (vco <= 6480000) {
7476 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7477 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7478 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7479 tribuf_calcntr = 0x8;
7480 } else {
7481 /* Not supported. Apply the same limits as in the max case */
7482 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7483 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7484 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7485 tribuf_calcntr = 0;
7486 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007487 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7488
Ville Syrjälä968040b2015-03-11 22:52:08 +02007489 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307490 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7491 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7492 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7493
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007494 /* AFC Recal */
7495 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7496 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7497 DPIO_AFC_RECAL);
7498
Ville Syrjäläa5805162015-05-26 20:42:30 +03007499 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007500}
7501
Ville Syrjäläd288f652014-10-28 13:20:22 +02007502/**
7503 * vlv_force_pll_on - forcibly enable just the PLL
7504 * @dev_priv: i915 private structure
7505 * @pipe: pipe PLL to enable
7506 * @dpll: PLL configuration
7507 *
7508 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7509 * in cases where we need the PLL enabled even when @pipe is not going to
7510 * be enabled.
7511 */
7512void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7513 const struct dpll *dpll)
7514{
7515 struct intel_crtc *crtc =
7516 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007517 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007518 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007519 .pixel_multiplier = 1,
7520 .dpll = *dpll,
7521 };
7522
7523 if (IS_CHERRYVIEW(dev)) {
7524 chv_update_pll(crtc, &pipe_config);
7525 chv_prepare_pll(crtc, &pipe_config);
7526 chv_enable_pll(crtc, &pipe_config);
7527 } else {
7528 vlv_update_pll(crtc, &pipe_config);
7529 vlv_prepare_pll(crtc, &pipe_config);
7530 vlv_enable_pll(crtc, &pipe_config);
7531 }
7532}
7533
7534/**
7535 * vlv_force_pll_off - forcibly disable just the PLL
7536 * @dev_priv: i915 private structure
7537 * @pipe: pipe PLL to disable
7538 *
7539 * Disable the PLL for @pipe. To be used in cases where we need
7540 * the PLL enabled even when @pipe is not going to be enabled.
7541 */
7542void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7543{
7544 if (IS_CHERRYVIEW(dev))
7545 chv_disable_pll(to_i915(dev), pipe);
7546 else
7547 vlv_disable_pll(to_i915(dev), pipe);
7548}
7549
Daniel Vetterf47709a2013-03-28 10:42:02 +01007550static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007551 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007552 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007553 int num_connectors)
7554{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007555 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007556 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007557 u32 dpll;
7558 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007559 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007560
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007561 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307562
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007563 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7564 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007565
7566 dpll = DPLL_VGA_MODE_DIS;
7567
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007568 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007569 dpll |= DPLLB_MODE_LVDS;
7570 else
7571 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007572
Daniel Vetteref1b4602013-06-01 17:17:04 +02007573 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007574 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007575 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007576 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007577
7578 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007579 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007580
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007581 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007582 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007583
7584 /* compute bitmask from p1 value */
7585 if (IS_PINEVIEW(dev))
7586 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7587 else {
7588 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7589 if (IS_G4X(dev) && reduced_clock)
7590 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7591 }
7592 switch (clock->p2) {
7593 case 5:
7594 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7595 break;
7596 case 7:
7597 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7598 break;
7599 case 10:
7600 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7601 break;
7602 case 14:
7603 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7604 break;
7605 }
7606 if (INTEL_INFO(dev)->gen >= 4)
7607 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7608
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007609 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007610 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007611 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007612 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7613 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7614 else
7615 dpll |= PLL_REF_INPUT_DREFCLK;
7616
7617 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007618 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007619
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007620 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007621 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007622 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007623 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007624 }
7625}
7626
Daniel Vetterf47709a2013-03-28 10:42:02 +01007627static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007628 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007629 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007630 int num_connectors)
7631{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007632 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007633 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007634 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007635 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007636
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007637 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307638
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007639 dpll = DPLL_VGA_MODE_DIS;
7640
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007641 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007642 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7643 } else {
7644 if (clock->p1 == 2)
7645 dpll |= PLL_P1_DIVIDE_BY_TWO;
7646 else
7647 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7648 if (clock->p2 == 4)
7649 dpll |= PLL_P2_DIVIDE_BY_4;
7650 }
7651
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007652 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007653 dpll |= DPLL_DVO_2X_MODE;
7654
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007655 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007656 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7657 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7658 else
7659 dpll |= PLL_REF_INPUT_DREFCLK;
7660
7661 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007662 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007663}
7664
Daniel Vetter8a654f32013-06-01 17:16:22 +02007665static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007666{
7667 struct drm_device *dev = intel_crtc->base.dev;
7668 struct drm_i915_private *dev_priv = dev->dev_private;
7669 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007670 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007671 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007672 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007673 uint32_t crtc_vtotal, crtc_vblank_end;
7674 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007675
7676 /* We need to be careful not to changed the adjusted mode, for otherwise
7677 * the hw state checker will get angry at the mismatch. */
7678 crtc_vtotal = adjusted_mode->crtc_vtotal;
7679 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007680
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007681 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007682 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007683 crtc_vtotal -= 1;
7684 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007685
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007686 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007687 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7688 else
7689 vsyncshift = adjusted_mode->crtc_hsync_start -
7690 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007691 if (vsyncshift < 0)
7692 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007693 }
7694
7695 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007696 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007697
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007698 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007699 (adjusted_mode->crtc_hdisplay - 1) |
7700 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007701 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007702 (adjusted_mode->crtc_hblank_start - 1) |
7703 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007704 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007705 (adjusted_mode->crtc_hsync_start - 1) |
7706 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7707
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007708 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007709 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007710 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007711 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007712 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007713 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007714 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007715 (adjusted_mode->crtc_vsync_start - 1) |
7716 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7717
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007718 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7719 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7720 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7721 * bits. */
7722 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7723 (pipe == PIPE_B || pipe == PIPE_C))
7724 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7725
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007726 /* pipesrc controls the size that is scaled from, which should
7727 * always be the user's requested size.
7728 */
7729 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007730 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7731 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007732}
7733
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007734static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007735 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007736{
7737 struct drm_device *dev = crtc->base.dev;
7738 struct drm_i915_private *dev_priv = dev->dev_private;
7739 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7740 uint32_t tmp;
7741
7742 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007743 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7744 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007745 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007746 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7747 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007748 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007749 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7750 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007751
7752 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007753 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7754 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007755 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007756 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7757 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007758 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007759 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7760 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007761
7762 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007763 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7764 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7765 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007766 }
7767
7768 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007769 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7770 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7771
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007772 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7773 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007774}
7775
Daniel Vetterf6a83282014-02-11 15:28:57 -08007776void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007777 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007778{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007779 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7780 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7781 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7782 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007783
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007784 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7785 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7786 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7787 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007788
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007789 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007790
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007791 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7792 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007793}
7794
Daniel Vetter84b046f2013-02-19 18:48:54 +01007795static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7796{
7797 struct drm_device *dev = intel_crtc->base.dev;
7798 struct drm_i915_private *dev_priv = dev->dev_private;
7799 uint32_t pipeconf;
7800
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007801 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007802
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007803 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7804 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7805 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007806
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007807 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007808 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007809
Daniel Vetterff9ce462013-04-24 14:57:17 +02007810 /* only g4x and later have fancy bpc/dither controls */
7811 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007812 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007813 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007814 pipeconf |= PIPECONF_DITHER_EN |
7815 PIPECONF_DITHER_TYPE_SP;
7816
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007817 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007818 case 18:
7819 pipeconf |= PIPECONF_6BPC;
7820 break;
7821 case 24:
7822 pipeconf |= PIPECONF_8BPC;
7823 break;
7824 case 30:
7825 pipeconf |= PIPECONF_10BPC;
7826 break;
7827 default:
7828 /* Case prevented by intel_choose_pipe_bpp_dither. */
7829 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007830 }
7831 }
7832
7833 if (HAS_PIPE_CXSR(dev)) {
7834 if (intel_crtc->lowfreq_avail) {
7835 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7836 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7837 } else {
7838 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007839 }
7840 }
7841
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007842 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007843 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007844 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007845 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7846 else
7847 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7848 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007849 pipeconf |= PIPECONF_PROGRESSIVE;
7850
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007851 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007852 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007853
Daniel Vetter84b046f2013-02-19 18:48:54 +01007854 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7855 POSTING_READ(PIPECONF(intel_crtc->pipe));
7856}
7857
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007858static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7859 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007860{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007861 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007862 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007863 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007864 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007865 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007866 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007867 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007868 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007869 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007870 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007871 struct drm_connector_state *connector_state;
7872 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007873
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007874 memset(&crtc_state->dpll_hw_state, 0,
7875 sizeof(crtc_state->dpll_hw_state));
7876
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007877 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007878 if (connector_state->crtc != &crtc->base)
7879 continue;
7880
7881 encoder = to_intel_encoder(connector_state->best_encoder);
7882
Chris Wilson5eddb702010-09-11 13:48:45 +01007883 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007884 case INTEL_OUTPUT_LVDS:
7885 is_lvds = true;
7886 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007887 case INTEL_OUTPUT_DSI:
7888 is_dsi = true;
7889 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007890 default:
7891 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007892 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007893
Eric Anholtc751ce42010-03-25 11:48:48 -07007894 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007895 }
7896
Jani Nikulaf2335332013-09-13 11:03:09 +03007897 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007898 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007899
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007900 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007901 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007902
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007903 /*
7904 * Returns a set of divisors for the desired target clock with
7905 * the given refclk, or FALSE. The returned values represent
7906 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7907 * 2) / p1 / p2.
7908 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007909 limit = intel_limit(crtc_state, refclk);
7910 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007911 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007912 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007913 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007914 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7915 return -EINVAL;
7916 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007917
Jani Nikulaf2335332013-09-13 11:03:09 +03007918 if (is_lvds && dev_priv->lvds_downclock_avail) {
7919 /*
7920 * Ensure we match the reduced clock's P to the target
7921 * clock. If the clocks don't match, we can't switch
7922 * the display clock by using the FP0/FP1. In such case
7923 * we will disable the LVDS downclock feature.
7924 */
7925 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007926 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007927 dev_priv->lvds_downclock,
7928 refclk, &clock,
7929 &reduced_clock);
7930 }
7931 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007932 crtc_state->dpll.n = clock.n;
7933 crtc_state->dpll.m1 = clock.m1;
7934 crtc_state->dpll.m2 = clock.m2;
7935 crtc_state->dpll.p1 = clock.p1;
7936 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007937 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007938
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007939 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007940 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307941 has_reduced_clock ? &reduced_clock : NULL,
7942 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007943 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007944 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007945 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007946 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007947 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007948 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007949 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007950 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007951 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007952
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007953 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007954}
7955
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007956static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007957 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007958{
7959 struct drm_device *dev = crtc->base.dev;
7960 struct drm_i915_private *dev_priv = dev->dev_private;
7961 uint32_t tmp;
7962
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007963 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7964 return;
7965
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007966 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007967 if (!(tmp & PFIT_ENABLE))
7968 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007969
Daniel Vetter06922822013-07-11 13:35:40 +02007970 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007971 if (INTEL_INFO(dev)->gen < 4) {
7972 if (crtc->pipe != PIPE_B)
7973 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007974 } else {
7975 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7976 return;
7977 }
7978
Daniel Vetter06922822013-07-11 13:35:40 +02007979 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007980 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7981 if (INTEL_INFO(dev)->gen < 5)
7982 pipe_config->gmch_pfit.lvds_border_bits =
7983 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7984}
7985
Jesse Barnesacbec812013-09-20 11:29:32 -07007986static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007987 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07007988{
7989 struct drm_device *dev = crtc->base.dev;
7990 struct drm_i915_private *dev_priv = dev->dev_private;
7991 int pipe = pipe_config->cpu_transcoder;
7992 intel_clock_t clock;
7993 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07007994 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07007995
Shobhit Kumarf573de52014-07-30 20:32:37 +05307996 /* In case of MIPI DPLL will not even be used */
7997 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
7998 return;
7999
Ville Syrjäläa5805162015-05-26 20:42:30 +03008000 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008001 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008002 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008003
8004 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8005 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8006 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8007 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8008 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8009
Ville Syrjäläf6466282013-10-14 14:50:31 +03008010 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008011
Ville Syrjäläf6466282013-10-14 14:50:31 +03008012 /* clock.dot is the fast clock */
8013 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07008014}
8015
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008016static void
8017i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8018 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008019{
8020 struct drm_device *dev = crtc->base.dev;
8021 struct drm_i915_private *dev_priv = dev->dev_private;
8022 u32 val, base, offset;
8023 int pipe = crtc->pipe, plane = crtc->plane;
8024 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008025 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008026 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008027 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008028
Damien Lespiau42a7b082015-02-05 19:35:13 +00008029 val = I915_READ(DSPCNTR(plane));
8030 if (!(val & DISPLAY_PLANE_ENABLE))
8031 return;
8032
Damien Lespiaud9806c92015-01-21 14:07:19 +00008033 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008034 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008035 DRM_DEBUG_KMS("failed to alloc fb\n");
8036 return;
8037 }
8038
Damien Lespiau1b842c82015-01-21 13:50:54 +00008039 fb = &intel_fb->base;
8040
Daniel Vetter18c52472015-02-10 17:16:09 +00008041 if (INTEL_INFO(dev)->gen >= 4) {
8042 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008043 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008044 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8045 }
8046 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008047
8048 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008049 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008050 fb->pixel_format = fourcc;
8051 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008052
8053 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008054 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008055 offset = I915_READ(DSPTILEOFF(plane));
8056 else
8057 offset = I915_READ(DSPLINOFF(plane));
8058 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8059 } else {
8060 base = I915_READ(DSPADDR(plane));
8061 }
8062 plane_config->base = base;
8063
8064 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008065 fb->width = ((val >> 16) & 0xfff) + 1;
8066 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008067
8068 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008069 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008070
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008071 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008072 fb->pixel_format,
8073 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008074
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008075 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008076
Damien Lespiau2844a922015-01-20 12:51:48 +00008077 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8078 pipe_name(pipe), plane, fb->width, fb->height,
8079 fb->bits_per_pixel, base, fb->pitches[0],
8080 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008081
Damien Lespiau2d140302015-02-05 17:22:18 +00008082 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008083}
8084
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008085static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008086 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008087{
8088 struct drm_device *dev = crtc->base.dev;
8089 struct drm_i915_private *dev_priv = dev->dev_private;
8090 int pipe = pipe_config->cpu_transcoder;
8091 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8092 intel_clock_t clock;
8093 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8094 int refclk = 100000;
8095
Ville Syrjäläa5805162015-05-26 20:42:30 +03008096 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008097 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8098 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8099 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8100 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008101 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008102
8103 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8104 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8105 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8106 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8107 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8108
8109 chv_clock(refclk, &clock);
8110
8111 /* clock.dot is the fast clock */
8112 pipe_config->port_clock = clock.dot / 5;
8113}
8114
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008115static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008116 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008117{
8118 struct drm_device *dev = crtc->base.dev;
8119 struct drm_i915_private *dev_priv = dev->dev_private;
8120 uint32_t tmp;
8121
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008122 if (!intel_display_power_is_enabled(dev_priv,
8123 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008124 return false;
8125
Daniel Vettere143a212013-07-04 12:01:15 +02008126 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008127 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008128
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008129 tmp = I915_READ(PIPECONF(crtc->pipe));
8130 if (!(tmp & PIPECONF_ENABLE))
8131 return false;
8132
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008133 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8134 switch (tmp & PIPECONF_BPC_MASK) {
8135 case PIPECONF_6BPC:
8136 pipe_config->pipe_bpp = 18;
8137 break;
8138 case PIPECONF_8BPC:
8139 pipe_config->pipe_bpp = 24;
8140 break;
8141 case PIPECONF_10BPC:
8142 pipe_config->pipe_bpp = 30;
8143 break;
8144 default:
8145 break;
8146 }
8147 }
8148
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008149 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8150 pipe_config->limited_color_range = true;
8151
Ville Syrjälä282740f2013-09-04 18:30:03 +03008152 if (INTEL_INFO(dev)->gen < 4)
8153 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8154
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008155 intel_get_pipe_timings(crtc, pipe_config);
8156
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008157 i9xx_get_pfit_config(crtc, pipe_config);
8158
Daniel Vetter6c49f242013-06-06 12:45:25 +02008159 if (INTEL_INFO(dev)->gen >= 4) {
8160 tmp = I915_READ(DPLL_MD(crtc->pipe));
8161 pipe_config->pixel_multiplier =
8162 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8163 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008164 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008165 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8166 tmp = I915_READ(DPLL(crtc->pipe));
8167 pipe_config->pixel_multiplier =
8168 ((tmp & SDVO_MULTIPLIER_MASK)
8169 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8170 } else {
8171 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8172 * port and will be fixed up in the encoder->get_config
8173 * function. */
8174 pipe_config->pixel_multiplier = 1;
8175 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008176 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8177 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008178 /*
8179 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8180 * on 830. Filter it out here so that we don't
8181 * report errors due to that.
8182 */
8183 if (IS_I830(dev))
8184 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8185
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008186 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8187 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008188 } else {
8189 /* Mask out read-only status bits. */
8190 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8191 DPLL_PORTC_READY_MASK |
8192 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008193 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008194
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008195 if (IS_CHERRYVIEW(dev))
8196 chv_crtc_clock_get(crtc, pipe_config);
8197 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008198 vlv_crtc_clock_get(crtc, pipe_config);
8199 else
8200 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008201
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008202 return true;
8203}
8204
Paulo Zanonidde86e22012-12-01 12:04:25 -02008205static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008206{
8207 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008208 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008209 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008210 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008211 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008212 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008213 bool has_ck505 = false;
8214 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008215
8216 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008217 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008218 switch (encoder->type) {
8219 case INTEL_OUTPUT_LVDS:
8220 has_panel = true;
8221 has_lvds = true;
8222 break;
8223 case INTEL_OUTPUT_EDP:
8224 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008225 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008226 has_cpu_edp = true;
8227 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008228 default:
8229 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008230 }
8231 }
8232
Keith Packard99eb6a02011-09-26 14:29:12 -07008233 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008234 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008235 can_ssc = has_ck505;
8236 } else {
8237 has_ck505 = false;
8238 can_ssc = true;
8239 }
8240
Imre Deak2de69052013-05-08 13:14:04 +03008241 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8242 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008243
8244 /* Ironlake: try to setup display ref clock before DPLL
8245 * enabling. This is only under driver's control after
8246 * PCH B stepping, previous chipset stepping should be
8247 * ignoring this setting.
8248 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008249 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008250
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008251 /* As we must carefully and slowly disable/enable each source in turn,
8252 * compute the final state we want first and check if we need to
8253 * make any changes at all.
8254 */
8255 final = val;
8256 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008257 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008258 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008259 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008260 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8261
8262 final &= ~DREF_SSC_SOURCE_MASK;
8263 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8264 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008265
Keith Packard199e5d72011-09-22 12:01:57 -07008266 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008267 final |= DREF_SSC_SOURCE_ENABLE;
8268
8269 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8270 final |= DREF_SSC1_ENABLE;
8271
8272 if (has_cpu_edp) {
8273 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8274 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8275 else
8276 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8277 } else
8278 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8279 } else {
8280 final |= DREF_SSC_SOURCE_DISABLE;
8281 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8282 }
8283
8284 if (final == val)
8285 return;
8286
8287 /* Always enable nonspread source */
8288 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8289
8290 if (has_ck505)
8291 val |= DREF_NONSPREAD_CK505_ENABLE;
8292 else
8293 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8294
8295 if (has_panel) {
8296 val &= ~DREF_SSC_SOURCE_MASK;
8297 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008298
Keith Packard199e5d72011-09-22 12:01:57 -07008299 /* SSC must be turned on before enabling the CPU output */
Keith Packard99eb6a02011-09-26 14:29:12 -07008300 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008301 DRM_DEBUG_KMS("Using SSC on panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008302 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008303 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008304 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008305
8306 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008307 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008308 POSTING_READ(PCH_DREF_CONTROL);
8309 udelay(200);
8310
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008311 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008312
8313 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008314 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008315 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008316 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008317 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008318 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008319 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008320 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008321 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008322
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008323 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008324 POSTING_READ(PCH_DREF_CONTROL);
8325 udelay(200);
8326 } else {
8327 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8328
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008329 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008330
8331 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008332 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008333
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008334 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008335 POSTING_READ(PCH_DREF_CONTROL);
8336 udelay(200);
8337
8338 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008339 val &= ~DREF_SSC_SOURCE_MASK;
8340 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008341
8342 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008343 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008344
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008345 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008346 POSTING_READ(PCH_DREF_CONTROL);
8347 udelay(200);
8348 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008349
8350 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008351}
8352
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008353static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008354{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008355 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008356
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008357 tmp = I915_READ(SOUTH_CHICKEN2);
8358 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8359 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008360
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008361 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8362 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8363 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008364
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008365 tmp = I915_READ(SOUTH_CHICKEN2);
8366 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8367 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008368
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008369 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8370 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8371 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008372}
8373
8374/* WaMPhyProgramming:hsw */
8375static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8376{
8377 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008378
8379 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8380 tmp &= ~(0xFF << 24);
8381 tmp |= (0x12 << 24);
8382 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8383
Paulo Zanonidde86e22012-12-01 12:04:25 -02008384 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8385 tmp |= (1 << 11);
8386 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8387
8388 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8389 tmp |= (1 << 11);
8390 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8391
Paulo Zanonidde86e22012-12-01 12:04:25 -02008392 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8393 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8394 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8395
8396 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8397 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8398 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8399
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008400 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8401 tmp &= ~(7 << 13);
8402 tmp |= (5 << 13);
8403 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008404
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008405 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8406 tmp &= ~(7 << 13);
8407 tmp |= (5 << 13);
8408 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008409
8410 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8411 tmp &= ~0xFF;
8412 tmp |= 0x1C;
8413 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8414
8415 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8416 tmp &= ~0xFF;
8417 tmp |= 0x1C;
8418 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8419
8420 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8421 tmp &= ~(0xFF << 16);
8422 tmp |= (0x1C << 16);
8423 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8424
8425 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8426 tmp &= ~(0xFF << 16);
8427 tmp |= (0x1C << 16);
8428 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8429
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008430 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8431 tmp |= (1 << 27);
8432 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008433
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008434 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8435 tmp |= (1 << 27);
8436 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008437
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008438 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8439 tmp &= ~(0xF << 28);
8440 tmp |= (4 << 28);
8441 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008442
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008443 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8444 tmp &= ~(0xF << 28);
8445 tmp |= (4 << 28);
8446 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008447}
8448
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008449/* Implements 3 different sequences from BSpec chapter "Display iCLK
8450 * Programming" based on the parameters passed:
8451 * - Sequence to enable CLKOUT_DP
8452 * - Sequence to enable CLKOUT_DP without spread
8453 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8454 */
8455static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8456 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008457{
8458 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008459 uint32_t reg, tmp;
8460
8461 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8462 with_spread = true;
8463 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8464 with_fdi, "LP PCH doesn't have FDI\n"))
8465 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008466
Ville Syrjäläa5805162015-05-26 20:42:30 +03008467 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008468
8469 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8470 tmp &= ~SBI_SSCCTL_DISABLE;
8471 tmp |= SBI_SSCCTL_PATHALT;
8472 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8473
8474 udelay(24);
8475
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008476 if (with_spread) {
8477 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8478 tmp &= ~SBI_SSCCTL_PATHALT;
8479 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008480
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008481 if (with_fdi) {
8482 lpt_reset_fdi_mphy(dev_priv);
8483 lpt_program_fdi_mphy(dev_priv);
8484 }
8485 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008486
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008487 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8488 SBI_GEN0 : SBI_DBUFF0;
8489 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8490 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8491 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008492
Ville Syrjäläa5805162015-05-26 20:42:30 +03008493 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008494}
8495
Paulo Zanoni47701c32013-07-23 11:19:25 -03008496/* Sequence to disable CLKOUT_DP */
8497static void lpt_disable_clkout_dp(struct drm_device *dev)
8498{
8499 struct drm_i915_private *dev_priv = dev->dev_private;
8500 uint32_t reg, tmp;
8501
Ville Syrjäläa5805162015-05-26 20:42:30 +03008502 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008503
8504 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8505 SBI_GEN0 : SBI_DBUFF0;
8506 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8507 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8508 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8509
8510 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8511 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8512 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8513 tmp |= SBI_SSCCTL_PATHALT;
8514 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8515 udelay(32);
8516 }
8517 tmp |= SBI_SSCCTL_DISABLE;
8518 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8519 }
8520
Ville Syrjäläa5805162015-05-26 20:42:30 +03008521 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008522}
8523
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008524static void lpt_init_pch_refclk(struct drm_device *dev)
8525{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008526 struct intel_encoder *encoder;
8527 bool has_vga = false;
8528
Damien Lespiaub2784e12014-08-05 11:29:37 +01008529 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008530 switch (encoder->type) {
8531 case INTEL_OUTPUT_ANALOG:
8532 has_vga = true;
8533 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008534 default:
8535 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008536 }
8537 }
8538
Paulo Zanoni47701c32013-07-23 11:19:25 -03008539 if (has_vga)
8540 lpt_enable_clkout_dp(dev, true, true);
8541 else
8542 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008543}
8544
Paulo Zanonidde86e22012-12-01 12:04:25 -02008545/*
8546 * Initialize reference clocks when the driver loads
8547 */
8548void intel_init_pch_refclk(struct drm_device *dev)
8549{
8550 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8551 ironlake_init_pch_refclk(dev);
8552 else if (HAS_PCH_LPT(dev))
8553 lpt_init_pch_refclk(dev);
8554}
8555
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008556static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008557{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008558 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008559 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008560 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008561 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008562 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008563 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008564 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008565 bool is_lvds = false;
8566
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008567 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008568 if (connector_state->crtc != crtc_state->base.crtc)
8569 continue;
8570
8571 encoder = to_intel_encoder(connector_state->best_encoder);
8572
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008573 switch (encoder->type) {
8574 case INTEL_OUTPUT_LVDS:
8575 is_lvds = true;
8576 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008577 default:
8578 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008579 }
8580 num_connectors++;
8581 }
8582
8583 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008584 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008585 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008586 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008587 }
8588
8589 return 120000;
8590}
8591
Daniel Vetter6ff93602013-04-19 11:24:36 +02008592static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008593{
8594 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8595 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8596 int pipe = intel_crtc->pipe;
8597 uint32_t val;
8598
Daniel Vetter78114072013-06-13 00:54:57 +02008599 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008600
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008601 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008602 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008603 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008604 break;
8605 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008606 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008607 break;
8608 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008609 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008610 break;
8611 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008612 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008613 break;
8614 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008615 /* Case prevented by intel_choose_pipe_bpp_dither. */
8616 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008617 }
8618
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008619 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008620 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8621
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008622 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008623 val |= PIPECONF_INTERLACED_ILK;
8624 else
8625 val |= PIPECONF_PROGRESSIVE;
8626
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008627 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008628 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008629
Paulo Zanonic8203562012-09-12 10:06:29 -03008630 I915_WRITE(PIPECONF(pipe), val);
8631 POSTING_READ(PIPECONF(pipe));
8632}
8633
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008634/*
8635 * Set up the pipe CSC unit.
8636 *
8637 * Currently only full range RGB to limited range RGB conversion
8638 * is supported, but eventually this should handle various
8639 * RGB<->YCbCr scenarios as well.
8640 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008641static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008642{
8643 struct drm_device *dev = crtc->dev;
8644 struct drm_i915_private *dev_priv = dev->dev_private;
8645 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8646 int pipe = intel_crtc->pipe;
8647 uint16_t coeff = 0x7800; /* 1.0 */
8648
8649 /*
8650 * TODO: Check what kind of values actually come out of the pipe
8651 * with these coeff/postoff values and adjust to get the best
8652 * accuracy. Perhaps we even need to take the bpc value into
8653 * consideration.
8654 */
8655
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008656 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008657 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8658
8659 /*
8660 * GY/GU and RY/RU should be the other way around according
8661 * to BSpec, but reality doesn't agree. Just set them up in
8662 * a way that results in the correct picture.
8663 */
8664 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8665 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8666
8667 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8668 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8669
8670 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8671 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8672
8673 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8674 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8675 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8676
8677 if (INTEL_INFO(dev)->gen > 6) {
8678 uint16_t postoff = 0;
8679
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008680 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008681 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008682
8683 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8684 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8685 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8686
8687 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8688 } else {
8689 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8690
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008691 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008692 mode |= CSC_BLACK_SCREEN_OFFSET;
8693
8694 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8695 }
8696}
8697
Daniel Vetter6ff93602013-04-19 11:24:36 +02008698static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008699{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008700 struct drm_device *dev = crtc->dev;
8701 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008702 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008703 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008704 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008705 uint32_t val;
8706
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008707 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008708
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008709 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008710 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8711
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008712 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008713 val |= PIPECONF_INTERLACED_ILK;
8714 else
8715 val |= PIPECONF_PROGRESSIVE;
8716
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008717 I915_WRITE(PIPECONF(cpu_transcoder), val);
8718 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008719
8720 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8721 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008722
Satheeshakrishna M3cdf1222014-04-08 15:46:53 +05308723 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008724 val = 0;
8725
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008726 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008727 case 18:
8728 val |= PIPEMISC_DITHER_6_BPC;
8729 break;
8730 case 24:
8731 val |= PIPEMISC_DITHER_8_BPC;
8732 break;
8733 case 30:
8734 val |= PIPEMISC_DITHER_10_BPC;
8735 break;
8736 case 36:
8737 val |= PIPEMISC_DITHER_12_BPC;
8738 break;
8739 default:
8740 /* Case prevented by pipe_config_set_bpp. */
8741 BUG();
8742 }
8743
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008744 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008745 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8746
8747 I915_WRITE(PIPEMISC(pipe), val);
8748 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008749}
8750
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008751static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008752 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008753 intel_clock_t *clock,
8754 bool *has_reduced_clock,
8755 intel_clock_t *reduced_clock)
8756{
8757 struct drm_device *dev = crtc->dev;
8758 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008759 int refclk;
8760 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008761 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008762
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008763 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008764
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008765 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008766
8767 /*
8768 * Returns a set of divisors for the desired target clock with the given
8769 * refclk, or FALSE. The returned values represent the clock equation:
8770 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8771 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008772 limit = intel_limit(crtc_state, refclk);
8773 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008774 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008775 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008776 if (!ret)
8777 return false;
8778
8779 if (is_lvds && dev_priv->lvds_downclock_avail) {
8780 /*
8781 * Ensure we match the reduced clock's P to the target clock.
8782 * If the clocks don't match, we can't switch the display clock
8783 * by using the FP0/FP1. In such case we will disable the LVDS
8784 * downclock feature.
8785 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008786 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008787 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008788 dev_priv->lvds_downclock,
8789 refclk, clock,
8790 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008791 }
8792
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008793 return true;
8794}
8795
Paulo Zanonid4b19312012-11-29 11:29:32 -02008796int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8797{
8798 /*
8799 * Account for spread spectrum to avoid
8800 * oversubscribing the link. Max center spread
8801 * is 2.5%; use 5% for safety's sake.
8802 */
8803 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008804 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008805}
8806
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008807static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008808{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008809 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008810}
8811
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008812static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008813 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008814 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008815 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008816{
8817 struct drm_crtc *crtc = &intel_crtc->base;
8818 struct drm_device *dev = crtc->dev;
8819 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008820 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008821 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008822 struct drm_connector_state *connector_state;
8823 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008824 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008825 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008826 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008827
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008828 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008829 if (connector_state->crtc != crtc_state->base.crtc)
8830 continue;
8831
8832 encoder = to_intel_encoder(connector_state->best_encoder);
8833
8834 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008835 case INTEL_OUTPUT_LVDS:
8836 is_lvds = true;
8837 break;
8838 case INTEL_OUTPUT_SDVO:
8839 case INTEL_OUTPUT_HDMI:
8840 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008841 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008842 default:
8843 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008844 }
8845
8846 num_connectors++;
8847 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008848
Chris Wilsonc1858122010-12-03 21:35:48 +00008849 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008850 factor = 21;
8851 if (is_lvds) {
8852 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008853 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008854 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008855 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008856 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008857 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008858
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008859 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008860 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008861
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008862 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8863 *fp2 |= FP_CB_TUNE;
8864
Chris Wilson5eddb702010-09-11 13:48:45 +01008865 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008866
Eric Anholta07d6782011-03-30 13:01:08 -07008867 if (is_lvds)
8868 dpll |= DPLLB_MODE_LVDS;
8869 else
8870 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008871
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008872 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008873 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008874
8875 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008876 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008877 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008878 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008879
Eric Anholta07d6782011-03-30 13:01:08 -07008880 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008881 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008882 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008883 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008884
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008885 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008886 case 5:
8887 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8888 break;
8889 case 7:
8890 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8891 break;
8892 case 10:
8893 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8894 break;
8895 case 14:
8896 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8897 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008898 }
8899
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008900 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008901 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008902 else
8903 dpll |= PLL_REF_INPUT_DREFCLK;
8904
Daniel Vetter959e16d2013-06-05 13:34:21 +02008905 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008906}
8907
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008908static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8909 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008910{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008911 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008912 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008913 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008914 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008915 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008916 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008917
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008918 memset(&crtc_state->dpll_hw_state, 0,
8919 sizeof(crtc_state->dpll_hw_state));
8920
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008921 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008922
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008923 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8924 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8925
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008926 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008927 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008928 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008929 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8930 return -EINVAL;
8931 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008932 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008933 if (!crtc_state->clock_set) {
8934 crtc_state->dpll.n = clock.n;
8935 crtc_state->dpll.m1 = clock.m1;
8936 crtc_state->dpll.m2 = clock.m2;
8937 crtc_state->dpll.p1 = clock.p1;
8938 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008939 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008940
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008941 /* 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 +02008942 if (crtc_state->has_pch_encoder) {
8943 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008944 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008945 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008946
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008947 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008948 &fp, &reduced_clock,
8949 has_reduced_clock ? &fp2 : NULL);
8950
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008951 crtc_state->dpll_hw_state.dpll = dpll;
8952 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008953 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008954 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008955 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008956 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008957
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008958 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008959 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008960 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008961 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008962 return -EINVAL;
8963 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008964 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008965
Rodrigo Viviab585de2015-03-24 12:40:09 -07008966 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008967 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008968 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008969 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008970
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008971 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008972}
8973
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008974static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8975 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008976{
8977 struct drm_device *dev = crtc->base.dev;
8978 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008979 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008980
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008981 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8982 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8983 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8984 & ~TU_SIZE_MASK;
8985 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
8986 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
8987 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
8988}
8989
8990static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
8991 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07008992 struct intel_link_m_n *m_n,
8993 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008994{
8995 struct drm_device *dev = crtc->base.dev;
8996 struct drm_i915_private *dev_priv = dev->dev_private;
8997 enum pipe pipe = crtc->pipe;
8998
8999 if (INTEL_INFO(dev)->gen >= 5) {
9000 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9001 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9002 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9003 & ~TU_SIZE_MASK;
9004 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9005 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9006 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009007 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9008 * gen < 8) and if DRRS is supported (to make sure the
9009 * registers are not unnecessarily read).
9010 */
9011 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009012 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009013 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9014 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9015 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9016 & ~TU_SIZE_MASK;
9017 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9018 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9019 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9020 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009021 } else {
9022 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9023 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9024 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9025 & ~TU_SIZE_MASK;
9026 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9027 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9028 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9029 }
9030}
9031
9032void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009033 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009034{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009035 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009036 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9037 else
9038 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009039 &pipe_config->dp_m_n,
9040 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009041}
9042
Daniel Vetter72419202013-04-04 13:28:53 +02009043static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009044 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009045{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009046 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009047 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009048}
9049
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009050static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009051 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009052{
9053 struct drm_device *dev = crtc->base.dev;
9054 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009055 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9056 uint32_t ps_ctrl = 0;
9057 int id = -1;
9058 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009059
Chandra Kondurua1b22782015-04-07 15:28:45 -07009060 /* find scaler attached to this pipe */
9061 for (i = 0; i < crtc->num_scalers; i++) {
9062 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9063 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9064 id = i;
9065 pipe_config->pch_pfit.enabled = true;
9066 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9067 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9068 break;
9069 }
9070 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009071
Chandra Kondurua1b22782015-04-07 15:28:45 -07009072 scaler_state->scaler_id = id;
9073 if (id >= 0) {
9074 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9075 } else {
9076 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009077 }
9078}
9079
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009080static void
9081skylake_get_initial_plane_config(struct intel_crtc *crtc,
9082 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009083{
9084 struct drm_device *dev = crtc->base.dev;
9085 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009086 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009087 int pipe = crtc->pipe;
9088 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009089 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009090 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009091 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009092
Damien Lespiaud9806c92015-01-21 14:07:19 +00009093 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009094 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009095 DRM_DEBUG_KMS("failed to alloc fb\n");
9096 return;
9097 }
9098
Damien Lespiau1b842c82015-01-21 13:50:54 +00009099 fb = &intel_fb->base;
9100
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009101 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009102 if (!(val & PLANE_CTL_ENABLE))
9103 goto error;
9104
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009105 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9106 fourcc = skl_format_to_fourcc(pixel_format,
9107 val & PLANE_CTL_ORDER_RGBX,
9108 val & PLANE_CTL_ALPHA_MASK);
9109 fb->pixel_format = fourcc;
9110 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9111
Damien Lespiau40f46282015-02-27 11:15:21 +00009112 tiling = val & PLANE_CTL_TILED_MASK;
9113 switch (tiling) {
9114 case PLANE_CTL_TILED_LINEAR:
9115 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9116 break;
9117 case PLANE_CTL_TILED_X:
9118 plane_config->tiling = I915_TILING_X;
9119 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9120 break;
9121 case PLANE_CTL_TILED_Y:
9122 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9123 break;
9124 case PLANE_CTL_TILED_YF:
9125 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9126 break;
9127 default:
9128 MISSING_CASE(tiling);
9129 goto error;
9130 }
9131
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009132 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9133 plane_config->base = base;
9134
9135 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9136
9137 val = I915_READ(PLANE_SIZE(pipe, 0));
9138 fb->height = ((val >> 16) & 0xfff) + 1;
9139 fb->width = ((val >> 0) & 0x1fff) + 1;
9140
9141 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009142 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9143 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009144 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9145
9146 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009147 fb->pixel_format,
9148 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009149
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009150 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009151
9152 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9153 pipe_name(pipe), fb->width, fb->height,
9154 fb->bits_per_pixel, base, fb->pitches[0],
9155 plane_config->size);
9156
Damien Lespiau2d140302015-02-05 17:22:18 +00009157 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009158 return;
9159
9160error:
9161 kfree(fb);
9162}
9163
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009164static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009165 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009166{
9167 struct drm_device *dev = crtc->base.dev;
9168 struct drm_i915_private *dev_priv = dev->dev_private;
9169 uint32_t tmp;
9170
9171 tmp = I915_READ(PF_CTL(crtc->pipe));
9172
9173 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009174 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009175 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9176 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009177
9178 /* We currently do not free assignements of panel fitters on
9179 * ivb/hsw (since we don't use the higher upscaling modes which
9180 * differentiates them) so just WARN about this case for now. */
9181 if (IS_GEN7(dev)) {
9182 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9183 PF_PIPE_SEL_IVB(crtc->pipe));
9184 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009185 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009186}
9187
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009188static void
9189ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9190 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009191{
9192 struct drm_device *dev = crtc->base.dev;
9193 struct drm_i915_private *dev_priv = dev->dev_private;
9194 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009195 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009196 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009197 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009198 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009199 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009200
Damien Lespiau42a7b082015-02-05 19:35:13 +00009201 val = I915_READ(DSPCNTR(pipe));
9202 if (!(val & DISPLAY_PLANE_ENABLE))
9203 return;
9204
Damien Lespiaud9806c92015-01-21 14:07:19 +00009205 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009206 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009207 DRM_DEBUG_KMS("failed to alloc fb\n");
9208 return;
9209 }
9210
Damien Lespiau1b842c82015-01-21 13:50:54 +00009211 fb = &intel_fb->base;
9212
Daniel Vetter18c52472015-02-10 17:16:09 +00009213 if (INTEL_INFO(dev)->gen >= 4) {
9214 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009215 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009216 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9217 }
9218 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009219
9220 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009221 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009222 fb->pixel_format = fourcc;
9223 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009224
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009225 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009226 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009227 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009228 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009229 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009230 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009231 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009232 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009233 }
9234 plane_config->base = base;
9235
9236 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009237 fb->width = ((val >> 16) & 0xfff) + 1;
9238 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009239
9240 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009241 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009242
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009243 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009244 fb->pixel_format,
9245 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009246
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009247 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009248
Damien Lespiau2844a922015-01-20 12:51:48 +00009249 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9250 pipe_name(pipe), fb->width, fb->height,
9251 fb->bits_per_pixel, base, fb->pitches[0],
9252 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009253
Damien Lespiau2d140302015-02-05 17:22:18 +00009254 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009255}
9256
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009257static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009258 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009259{
9260 struct drm_device *dev = crtc->base.dev;
9261 struct drm_i915_private *dev_priv = dev->dev_private;
9262 uint32_t tmp;
9263
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009264 if (!intel_display_power_is_enabled(dev_priv,
9265 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009266 return false;
9267
Daniel Vettere143a212013-07-04 12:01:15 +02009268 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009269 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009270
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009271 tmp = I915_READ(PIPECONF(crtc->pipe));
9272 if (!(tmp & PIPECONF_ENABLE))
9273 return false;
9274
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009275 switch (tmp & PIPECONF_BPC_MASK) {
9276 case PIPECONF_6BPC:
9277 pipe_config->pipe_bpp = 18;
9278 break;
9279 case PIPECONF_8BPC:
9280 pipe_config->pipe_bpp = 24;
9281 break;
9282 case PIPECONF_10BPC:
9283 pipe_config->pipe_bpp = 30;
9284 break;
9285 case PIPECONF_12BPC:
9286 pipe_config->pipe_bpp = 36;
9287 break;
9288 default:
9289 break;
9290 }
9291
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009292 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9293 pipe_config->limited_color_range = true;
9294
Daniel Vetterab9412b2013-05-03 11:49:46 +02009295 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009296 struct intel_shared_dpll *pll;
9297
Daniel Vetter88adfff2013-03-28 10:42:01 +01009298 pipe_config->has_pch_encoder = true;
9299
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009300 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9301 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9302 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009303
9304 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009305
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009306 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009307 pipe_config->shared_dpll =
9308 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009309 } else {
9310 tmp = I915_READ(PCH_DPLL_SEL);
9311 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9312 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9313 else
9314 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9315 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009316
9317 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9318
9319 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9320 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009321
9322 tmp = pipe_config->dpll_hw_state.dpll;
9323 pipe_config->pixel_multiplier =
9324 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9325 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009326
9327 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009328 } else {
9329 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009330 }
9331
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009332 intel_get_pipe_timings(crtc, pipe_config);
9333
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009334 ironlake_get_pfit_config(crtc, pipe_config);
9335
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009336 return true;
9337}
9338
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009339static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9340{
9341 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009342 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009343
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009344 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009345 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009346 pipe_name(crtc->pipe));
9347
Rob Clarke2c719b2014-12-15 13:56:32 -05009348 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9349 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9350 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9351 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9352 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9353 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009354 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009355 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009356 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009357 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009358 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009359 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009360 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009361 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009362 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009363
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009364 /*
9365 * In theory we can still leave IRQs enabled, as long as only the HPD
9366 * interrupts remain enabled. We used to check for that, but since it's
9367 * gen-specific and since we only disable LCPLL after we fully disable
9368 * the interrupts, the check below should be enough.
9369 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009370 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009371}
9372
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009373static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9374{
9375 struct drm_device *dev = dev_priv->dev;
9376
9377 if (IS_HASWELL(dev))
9378 return I915_READ(D_COMP_HSW);
9379 else
9380 return I915_READ(D_COMP_BDW);
9381}
9382
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009383static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9384{
9385 struct drm_device *dev = dev_priv->dev;
9386
9387 if (IS_HASWELL(dev)) {
9388 mutex_lock(&dev_priv->rps.hw_lock);
9389 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9390 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009391 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009392 mutex_unlock(&dev_priv->rps.hw_lock);
9393 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009394 I915_WRITE(D_COMP_BDW, val);
9395 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009396 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009397}
9398
9399/*
9400 * This function implements pieces of two sequences from BSpec:
9401 * - Sequence for display software to disable LCPLL
9402 * - Sequence for display software to allow package C8+
9403 * The steps implemented here are just the steps that actually touch the LCPLL
9404 * register. Callers should take care of disabling all the display engine
9405 * functions, doing the mode unset, fixing interrupts, etc.
9406 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009407static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9408 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009409{
9410 uint32_t val;
9411
9412 assert_can_disable_lcpll(dev_priv);
9413
9414 val = I915_READ(LCPLL_CTL);
9415
9416 if (switch_to_fclk) {
9417 val |= LCPLL_CD_SOURCE_FCLK;
9418 I915_WRITE(LCPLL_CTL, val);
9419
9420 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9421 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9422 DRM_ERROR("Switching to FCLK failed\n");
9423
9424 val = I915_READ(LCPLL_CTL);
9425 }
9426
9427 val |= LCPLL_PLL_DISABLE;
9428 I915_WRITE(LCPLL_CTL, val);
9429 POSTING_READ(LCPLL_CTL);
9430
9431 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9432 DRM_ERROR("LCPLL still locked\n");
9433
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009434 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009435 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009436 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009437 ndelay(100);
9438
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009439 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9440 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009441 DRM_ERROR("D_COMP RCOMP still in progress\n");
9442
9443 if (allow_power_down) {
9444 val = I915_READ(LCPLL_CTL);
9445 val |= LCPLL_POWER_DOWN_ALLOW;
9446 I915_WRITE(LCPLL_CTL, val);
9447 POSTING_READ(LCPLL_CTL);
9448 }
9449}
9450
9451/*
9452 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9453 * source.
9454 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009455static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009456{
9457 uint32_t val;
9458
9459 val = I915_READ(LCPLL_CTL);
9460
9461 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9462 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9463 return;
9464
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009465 /*
9466 * Make sure we're not on PC8 state before disabling PC8, otherwise
9467 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009468 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009469 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009470
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471 if (val & LCPLL_POWER_DOWN_ALLOW) {
9472 val &= ~LCPLL_POWER_DOWN_ALLOW;
9473 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009474 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009475 }
9476
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009477 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009478 val |= D_COMP_COMP_FORCE;
9479 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009480 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009481
9482 val = I915_READ(LCPLL_CTL);
9483 val &= ~LCPLL_PLL_DISABLE;
9484 I915_WRITE(LCPLL_CTL, val);
9485
9486 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9487 DRM_ERROR("LCPLL not locked yet\n");
9488
9489 if (val & LCPLL_CD_SOURCE_FCLK) {
9490 val = I915_READ(LCPLL_CTL);
9491 val &= ~LCPLL_CD_SOURCE_FCLK;
9492 I915_WRITE(LCPLL_CTL, val);
9493
9494 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9495 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9496 DRM_ERROR("Switching back to LCPLL failed\n");
9497 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009498
Mika Kuoppala59bad942015-01-16 11:34:40 +02009499 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009500 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009501}
9502
Paulo Zanoni765dab672014-03-07 20:08:18 -03009503/*
9504 * Package states C8 and deeper are really deep PC states that can only be
9505 * reached when all the devices on the system allow it, so even if the graphics
9506 * device allows PC8+, it doesn't mean the system will actually get to these
9507 * states. Our driver only allows PC8+ when going into runtime PM.
9508 *
9509 * The requirements for PC8+ are that all the outputs are disabled, the power
9510 * well is disabled and most interrupts are disabled, and these are also
9511 * requirements for runtime PM. When these conditions are met, we manually do
9512 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9513 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9514 * hang the machine.
9515 *
9516 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9517 * the state of some registers, so when we come back from PC8+ we need to
9518 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9519 * need to take care of the registers kept by RC6. Notice that this happens even
9520 * if we don't put the device in PCI D3 state (which is what currently happens
9521 * because of the runtime PM support).
9522 *
9523 * For more, read "Display Sequences for Package C8" on the hardware
9524 * documentation.
9525 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009526void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009527{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009528 struct drm_device *dev = dev_priv->dev;
9529 uint32_t val;
9530
Paulo Zanonic67a4702013-08-19 13:18:09 -03009531 DRM_DEBUG_KMS("Enabling package C8+\n");
9532
Paulo Zanonic67a4702013-08-19 13:18:09 -03009533 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9534 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9535 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9536 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9537 }
9538
9539 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009540 hsw_disable_lcpll(dev_priv, true, true);
9541}
9542
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009543void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009544{
9545 struct drm_device *dev = dev_priv->dev;
9546 uint32_t val;
9547
Paulo Zanonic67a4702013-08-19 13:18:09 -03009548 DRM_DEBUG_KMS("Disabling package C8+\n");
9549
9550 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009551 lpt_init_pch_refclk(dev);
9552
9553 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9554 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9555 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9556 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9557 }
9558
9559 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009560}
9561
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009562static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309563{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009564 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309565 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009566 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309567 int req_cdclk;
9568
9569 /* see the comment in valleyview_modeset_global_resources */
9570 if (WARN_ON(max_pixclk < 0))
9571 return;
9572
9573 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9574
9575 if (req_cdclk != dev_priv->cdclk_freq)
9576 broxton_set_cdclk(dev, req_cdclk);
9577}
9578
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009579/* compute the max rate for new configuration */
9580static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9581{
9582 struct drm_device *dev = dev_priv->dev;
9583 struct intel_crtc *intel_crtc;
9584 struct drm_crtc *crtc;
9585 int max_pixel_rate = 0;
9586 int pixel_rate;
9587
9588 for_each_crtc(dev, crtc) {
9589 if (!crtc->state->enable)
9590 continue;
9591
9592 intel_crtc = to_intel_crtc(crtc);
9593 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9594
9595 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9596 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9597 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9598
9599 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9600 }
9601
9602 return max_pixel_rate;
9603}
9604
9605static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9606{
9607 struct drm_i915_private *dev_priv = dev->dev_private;
9608 uint32_t val, data;
9609 int ret;
9610
9611 if (WARN((I915_READ(LCPLL_CTL) &
9612 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9613 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9614 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9615 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9616 "trying to change cdclk frequency with cdclk not enabled\n"))
9617 return;
9618
9619 mutex_lock(&dev_priv->rps.hw_lock);
9620 ret = sandybridge_pcode_write(dev_priv,
9621 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9622 mutex_unlock(&dev_priv->rps.hw_lock);
9623 if (ret) {
9624 DRM_ERROR("failed to inform pcode about cdclk change\n");
9625 return;
9626 }
9627
9628 val = I915_READ(LCPLL_CTL);
9629 val |= LCPLL_CD_SOURCE_FCLK;
9630 I915_WRITE(LCPLL_CTL, val);
9631
9632 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9633 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9634 DRM_ERROR("Switching to FCLK failed\n");
9635
9636 val = I915_READ(LCPLL_CTL);
9637 val &= ~LCPLL_CLK_FREQ_MASK;
9638
9639 switch (cdclk) {
9640 case 450000:
9641 val |= LCPLL_CLK_FREQ_450;
9642 data = 0;
9643 break;
9644 case 540000:
9645 val |= LCPLL_CLK_FREQ_54O_BDW;
9646 data = 1;
9647 break;
9648 case 337500:
9649 val |= LCPLL_CLK_FREQ_337_5_BDW;
9650 data = 2;
9651 break;
9652 case 675000:
9653 val |= LCPLL_CLK_FREQ_675_BDW;
9654 data = 3;
9655 break;
9656 default:
9657 WARN(1, "invalid cdclk frequency\n");
9658 return;
9659 }
9660
9661 I915_WRITE(LCPLL_CTL, val);
9662
9663 val = I915_READ(LCPLL_CTL);
9664 val &= ~LCPLL_CD_SOURCE_FCLK;
9665 I915_WRITE(LCPLL_CTL, val);
9666
9667 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9668 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9669 DRM_ERROR("Switching back to LCPLL failed\n");
9670
9671 mutex_lock(&dev_priv->rps.hw_lock);
9672 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9673 mutex_unlock(&dev_priv->rps.hw_lock);
9674
9675 intel_update_cdclk(dev);
9676
9677 WARN(cdclk != dev_priv->cdclk_freq,
9678 "cdclk requested %d kHz but got %d kHz\n",
9679 cdclk, dev_priv->cdclk_freq);
9680}
9681
9682static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9683 int max_pixel_rate)
9684{
9685 int cdclk;
9686
9687 /*
9688 * FIXME should also account for plane ratio
9689 * once 64bpp pixel formats are supported.
9690 */
9691 if (max_pixel_rate > 540000)
9692 cdclk = 675000;
9693 else if (max_pixel_rate > 450000)
9694 cdclk = 540000;
9695 else if (max_pixel_rate > 337500)
9696 cdclk = 450000;
9697 else
9698 cdclk = 337500;
9699
9700 /*
9701 * FIXME move the cdclk caclulation to
9702 * compute_config() so we can fail gracegully.
9703 */
9704 if (cdclk > dev_priv->max_cdclk_freq) {
9705 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9706 cdclk, dev_priv->max_cdclk_freq);
9707 cdclk = dev_priv->max_cdclk_freq;
9708 }
9709
9710 return cdclk;
9711}
9712
9713static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9714{
9715 struct drm_i915_private *dev_priv = to_i915(state->dev);
9716 struct drm_crtc *crtc;
9717 struct drm_crtc_state *crtc_state;
9718 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9719 int cdclk, i;
9720
9721 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9722
9723 if (cdclk == dev_priv->cdclk_freq)
9724 return 0;
9725
9726 /* add all active pipes to the state */
9727 for_each_crtc(state->dev, crtc) {
9728 if (!crtc->state->enable)
9729 continue;
9730
9731 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9732 if (IS_ERR(crtc_state))
9733 return PTR_ERR(crtc_state);
9734 }
9735
9736 /* disable/enable all currently active pipes while we change cdclk */
9737 for_each_crtc_in_state(state, crtc, crtc_state, i)
9738 if (crtc_state->enable)
9739 crtc_state->mode_changed = true;
9740
9741 return 0;
9742}
9743
9744static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9745{
9746 struct drm_device *dev = state->dev;
9747 struct drm_i915_private *dev_priv = dev->dev_private;
9748 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9749 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9750
9751 if (req_cdclk != dev_priv->cdclk_freq)
9752 broadwell_set_cdclk(dev, req_cdclk);
9753}
9754
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009755static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9756 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009757{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009758 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009759 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009760
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009761 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009762
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009763 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009764}
9765
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309766static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9767 enum port port,
9768 struct intel_crtc_state *pipe_config)
9769{
9770 switch (port) {
9771 case PORT_A:
9772 pipe_config->ddi_pll_sel = SKL_DPLL0;
9773 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9774 break;
9775 case PORT_B:
9776 pipe_config->ddi_pll_sel = SKL_DPLL1;
9777 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9778 break;
9779 case PORT_C:
9780 pipe_config->ddi_pll_sel = SKL_DPLL2;
9781 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9782 break;
9783 default:
9784 DRM_ERROR("Incorrect port type\n");
9785 }
9786}
9787
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009788static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9789 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009790 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009791{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009792 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009793
9794 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9795 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9796
9797 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009798 case SKL_DPLL0:
9799 /*
9800 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9801 * of the shared DPLL framework and thus needs to be read out
9802 * separately
9803 */
9804 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9805 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9806 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009807 case SKL_DPLL1:
9808 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9809 break;
9810 case SKL_DPLL2:
9811 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9812 break;
9813 case SKL_DPLL3:
9814 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9815 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009816 }
9817}
9818
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009819static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9820 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009821 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009822{
9823 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9824
9825 switch (pipe_config->ddi_pll_sel) {
9826 case PORT_CLK_SEL_WRPLL1:
9827 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9828 break;
9829 case PORT_CLK_SEL_WRPLL2:
9830 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9831 break;
9832 }
9833}
9834
Daniel Vetter26804af2014-06-25 22:01:55 +03009835static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009836 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009837{
9838 struct drm_device *dev = crtc->base.dev;
9839 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009840 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009841 enum port port;
9842 uint32_t tmp;
9843
9844 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9845
9846 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9847
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009848 if (IS_SKYLAKE(dev))
9849 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309850 else if (IS_BROXTON(dev))
9851 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009852 else
9853 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009854
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009855 if (pipe_config->shared_dpll >= 0) {
9856 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9857
9858 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9859 &pipe_config->dpll_hw_state));
9860 }
9861
Daniel Vetter26804af2014-06-25 22:01:55 +03009862 /*
9863 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9864 * DDI E. So just check whether this pipe is wired to DDI E and whether
9865 * the PCH transcoder is on.
9866 */
Damien Lespiauca370452013-12-03 13:56:24 +00009867 if (INTEL_INFO(dev)->gen < 9 &&
9868 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009869 pipe_config->has_pch_encoder = true;
9870
9871 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9872 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9873 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9874
9875 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9876 }
9877}
9878
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009879static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009880 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009881{
9882 struct drm_device *dev = crtc->base.dev;
9883 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009884 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009885 uint32_t tmp;
9886
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009887 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009888 POWER_DOMAIN_PIPE(crtc->pipe)))
9889 return false;
9890
Daniel Vettere143a212013-07-04 12:01:15 +02009891 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009892 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9893
Daniel Vettereccb1402013-05-22 00:50:22 +02009894 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9895 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9896 enum pipe trans_edp_pipe;
9897 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9898 default:
9899 WARN(1, "unknown pipe linked to edp transcoder\n");
9900 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9901 case TRANS_DDI_EDP_INPUT_A_ON:
9902 trans_edp_pipe = PIPE_A;
9903 break;
9904 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9905 trans_edp_pipe = PIPE_B;
9906 break;
9907 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9908 trans_edp_pipe = PIPE_C;
9909 break;
9910 }
9911
9912 if (trans_edp_pipe == crtc->pipe)
9913 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9914 }
9915
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009916 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009917 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009918 return false;
9919
Daniel Vettereccb1402013-05-22 00:50:22 +02009920 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009921 if (!(tmp & PIPECONF_ENABLE))
9922 return false;
9923
Daniel Vetter26804af2014-06-25 22:01:55 +03009924 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009925
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009926 intel_get_pipe_timings(crtc, pipe_config);
9927
Chandra Kondurua1b22782015-04-07 15:28:45 -07009928 if (INTEL_INFO(dev)->gen >= 9) {
9929 skl_init_scalers(dev, crtc, pipe_config);
9930 }
9931
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009932 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009933
9934 if (INTEL_INFO(dev)->gen >= 9) {
9935 pipe_config->scaler_state.scaler_id = -1;
9936 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9937 }
9938
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009939 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009940 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009941 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009942 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009943 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009944 else
9945 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009946 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009947
Jesse Barnese59150d2014-01-07 13:30:45 -08009948 if (IS_HASWELL(dev))
9949 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9950 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009951
Clint Taylorebb69c92014-09-30 10:30:22 -07009952 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9953 pipe_config->pixel_multiplier =
9954 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9955 } else {
9956 pipe_config->pixel_multiplier = 1;
9957 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009958
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009959 return true;
9960}
9961
Chris Wilson560b85b2010-08-07 11:01:38 +01009962static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9963{
9964 struct drm_device *dev = crtc->dev;
9965 struct drm_i915_private *dev_priv = dev->dev_private;
9966 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009967 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009968
Ville Syrjälädc41c152014-08-13 11:57:05 +03009969 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009970 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9971 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009972 unsigned int stride = roundup_pow_of_two(width) * 4;
9973
9974 switch (stride) {
9975 default:
9976 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9977 width, stride);
9978 stride = 256;
9979 /* fallthrough */
9980 case 256:
9981 case 512:
9982 case 1024:
9983 case 2048:
9984 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009985 }
9986
Ville Syrjälädc41c152014-08-13 11:57:05 +03009987 cntl |= CURSOR_ENABLE |
9988 CURSOR_GAMMA_ENABLE |
9989 CURSOR_FORMAT_ARGB |
9990 CURSOR_STRIDE(stride);
9991
9992 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +03009993 }
Chris Wilson560b85b2010-08-07 11:01:38 +01009994
Ville Syrjälädc41c152014-08-13 11:57:05 +03009995 if (intel_crtc->cursor_cntl != 0 &&
9996 (intel_crtc->cursor_base != base ||
9997 intel_crtc->cursor_size != size ||
9998 intel_crtc->cursor_cntl != cntl)) {
9999 /* On these chipsets we can only modify the base/size/stride
10000 * whilst the cursor is disabled.
10001 */
10002 I915_WRITE(_CURACNTR, 0);
10003 POSTING_READ(_CURACNTR);
10004 intel_crtc->cursor_cntl = 0;
10005 }
10006
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010007 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +030010008 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010009 intel_crtc->cursor_base = base;
10010 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010011
10012 if (intel_crtc->cursor_size != size) {
10013 I915_WRITE(CURSIZE, size);
10014 intel_crtc->cursor_size = size;
10015 }
10016
Chris Wilson4b0e3332014-05-30 16:35:26 +030010017 if (intel_crtc->cursor_cntl != cntl) {
10018 I915_WRITE(_CURACNTR, cntl);
10019 POSTING_READ(_CURACNTR);
10020 intel_crtc->cursor_cntl = cntl;
10021 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010022}
10023
10024static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
10025{
10026 struct drm_device *dev = crtc->dev;
10027 struct drm_i915_private *dev_priv = dev->dev_private;
10028 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10029 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010030 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +010010031
Chris Wilson4b0e3332014-05-30 16:35:26 +030010032 cntl = 0;
10033 if (base) {
10034 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -080010035 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010036 case 64:
10037 cntl |= CURSOR_MODE_64_ARGB_AX;
10038 break;
10039 case 128:
10040 cntl |= CURSOR_MODE_128_ARGB_AX;
10041 break;
10042 case 256:
10043 cntl |= CURSOR_MODE_256_ARGB_AX;
10044 break;
10045 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010046 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010047 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010048 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010049 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010050
10051 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10052 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010053 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010054
Matt Roper8e7d6882015-01-21 16:35:41 -080010055 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010056 cntl |= CURSOR_ROTATE_180;
10057
Chris Wilson4b0e3332014-05-30 16:35:26 +030010058 if (intel_crtc->cursor_cntl != cntl) {
10059 I915_WRITE(CURCNTR(pipe), cntl);
10060 POSTING_READ(CURCNTR(pipe));
10061 intel_crtc->cursor_cntl = cntl;
10062 }
10063
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010064 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010065 I915_WRITE(CURBASE(pipe), base);
10066 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010067
10068 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010069}
10070
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010071/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010072static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10073 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010074{
10075 struct drm_device *dev = crtc->dev;
10076 struct drm_i915_private *dev_priv = dev->dev_private;
10077 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10078 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010079 int x = crtc->cursor_x;
10080 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010081 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010082
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010083 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010084 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010085
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010086 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010087 base = 0;
10088
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010089 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010090 base = 0;
10091
10092 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010093 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010094 base = 0;
10095
10096 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10097 x = -x;
10098 }
10099 pos |= x << CURSOR_X_SHIFT;
10100
10101 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010102 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010103 base = 0;
10104
10105 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10106 y = -y;
10107 }
10108 pos |= y << CURSOR_Y_SHIFT;
10109
Chris Wilson4b0e3332014-05-30 16:35:26 +030010110 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010111 return;
10112
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010113 I915_WRITE(CURPOS(pipe), pos);
10114
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010115 /* ILK+ do this automagically */
10116 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010117 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010118 base += (intel_crtc->base.cursor->state->crtc_h *
10119 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010120 }
10121
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010122 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010123 i845_update_cursor(crtc, base);
10124 else
10125 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010126}
10127
Ville Syrjälädc41c152014-08-13 11:57:05 +030010128static bool cursor_size_ok(struct drm_device *dev,
10129 uint32_t width, uint32_t height)
10130{
10131 if (width == 0 || height == 0)
10132 return false;
10133
10134 /*
10135 * 845g/865g are special in that they are only limited by
10136 * the width of their cursors, the height is arbitrary up to
10137 * the precision of the register. Everything else requires
10138 * square cursors, limited to a few power-of-two sizes.
10139 */
10140 if (IS_845G(dev) || IS_I865G(dev)) {
10141 if ((width & 63) != 0)
10142 return false;
10143
10144 if (width > (IS_845G(dev) ? 64 : 512))
10145 return false;
10146
10147 if (height > 1023)
10148 return false;
10149 } else {
10150 switch (width | height) {
10151 case 256:
10152 case 128:
10153 if (IS_GEN2(dev))
10154 return false;
10155 case 64:
10156 break;
10157 default:
10158 return false;
10159 }
10160 }
10161
10162 return true;
10163}
10164
Jesse Barnes79e53942008-11-07 14:24:08 -080010165static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010166 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010167{
James Simmons72034252010-08-03 01:33:19 +010010168 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010169 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010170
James Simmons72034252010-08-03 01:33:19 +010010171 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010172 intel_crtc->lut_r[i] = red[i] >> 8;
10173 intel_crtc->lut_g[i] = green[i] >> 8;
10174 intel_crtc->lut_b[i] = blue[i] >> 8;
10175 }
10176
10177 intel_crtc_load_lut(crtc);
10178}
10179
Jesse Barnes79e53942008-11-07 14:24:08 -080010180/* VESA 640x480x72Hz mode to set on the pipe */
10181static struct drm_display_mode load_detect_mode = {
10182 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10183 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10184};
10185
Daniel Vettera8bb6812014-02-10 18:00:39 +010010186struct drm_framebuffer *
10187__intel_framebuffer_create(struct drm_device *dev,
10188 struct drm_mode_fb_cmd2 *mode_cmd,
10189 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010190{
10191 struct intel_framebuffer *intel_fb;
10192 int ret;
10193
10194 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10195 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010196 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010197 return ERR_PTR(-ENOMEM);
10198 }
10199
10200 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010201 if (ret)
10202 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010203
10204 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010205err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010206 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010207 kfree(intel_fb);
10208
10209 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010210}
10211
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010212static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010213intel_framebuffer_create(struct drm_device *dev,
10214 struct drm_mode_fb_cmd2 *mode_cmd,
10215 struct drm_i915_gem_object *obj)
10216{
10217 struct drm_framebuffer *fb;
10218 int ret;
10219
10220 ret = i915_mutex_lock_interruptible(dev);
10221 if (ret)
10222 return ERR_PTR(ret);
10223 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10224 mutex_unlock(&dev->struct_mutex);
10225
10226 return fb;
10227}
10228
Chris Wilsond2dff872011-04-19 08:36:26 +010010229static u32
10230intel_framebuffer_pitch_for_width(int width, int bpp)
10231{
10232 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10233 return ALIGN(pitch, 64);
10234}
10235
10236static u32
10237intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10238{
10239 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010240 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010241}
10242
10243static struct drm_framebuffer *
10244intel_framebuffer_create_for_mode(struct drm_device *dev,
10245 struct drm_display_mode *mode,
10246 int depth, int bpp)
10247{
10248 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010249 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010250
10251 obj = i915_gem_alloc_object(dev,
10252 intel_framebuffer_size_for_mode(mode, bpp));
10253 if (obj == NULL)
10254 return ERR_PTR(-ENOMEM);
10255
10256 mode_cmd.width = mode->hdisplay;
10257 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010258 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10259 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010260 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010261
10262 return intel_framebuffer_create(dev, &mode_cmd, obj);
10263}
10264
10265static struct drm_framebuffer *
10266mode_fits_in_fbdev(struct drm_device *dev,
10267 struct drm_display_mode *mode)
10268{
Daniel Vetter4520f532013-10-09 09:18:51 +020010269#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010270 struct drm_i915_private *dev_priv = dev->dev_private;
10271 struct drm_i915_gem_object *obj;
10272 struct drm_framebuffer *fb;
10273
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010274 if (!dev_priv->fbdev)
10275 return NULL;
10276
10277 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010278 return NULL;
10279
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010280 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010281 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010282
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010283 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010284 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10285 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010286 return NULL;
10287
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010288 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010289 return NULL;
10290
10291 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010292#else
10293 return NULL;
10294#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010295}
10296
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010297static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10298 struct drm_crtc *crtc,
10299 struct drm_display_mode *mode,
10300 struct drm_framebuffer *fb,
10301 int x, int y)
10302{
10303 struct drm_plane_state *plane_state;
10304 int hdisplay, vdisplay;
10305 int ret;
10306
10307 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10308 if (IS_ERR(plane_state))
10309 return PTR_ERR(plane_state);
10310
10311 if (mode)
10312 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10313 else
10314 hdisplay = vdisplay = 0;
10315
10316 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10317 if (ret)
10318 return ret;
10319 drm_atomic_set_fb_for_plane(plane_state, fb);
10320 plane_state->crtc_x = 0;
10321 plane_state->crtc_y = 0;
10322 plane_state->crtc_w = hdisplay;
10323 plane_state->crtc_h = vdisplay;
10324 plane_state->src_x = x << 16;
10325 plane_state->src_y = y << 16;
10326 plane_state->src_w = hdisplay << 16;
10327 plane_state->src_h = vdisplay << 16;
10328
10329 return 0;
10330}
10331
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010332bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010333 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010334 struct intel_load_detect_pipe *old,
10335 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010336{
10337 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010338 struct intel_encoder *intel_encoder =
10339 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010340 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010341 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010342 struct drm_crtc *crtc = NULL;
10343 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010344 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010345 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010346 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010347 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010348 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010349 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010350
Chris Wilsond2dff872011-04-19 08:36:26 +010010351 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010352 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010353 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010354
Rob Clark51fd3712013-11-19 12:10:12 -050010355retry:
10356 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10357 if (ret)
10358 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010359
Jesse Barnes79e53942008-11-07 14:24:08 -080010360 /*
10361 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010362 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010363 * - if the connector already has an assigned crtc, use it (but make
10364 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010365 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010366 * - try to find the first unused crtc that can drive this connector,
10367 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010368 */
10369
10370 /* See if we already have a CRTC for this connector */
10371 if (encoder->crtc) {
10372 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010373
Rob Clark51fd3712013-11-19 12:10:12 -050010374 ret = drm_modeset_lock(&crtc->mutex, ctx);
10375 if (ret)
10376 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010377 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10378 if (ret)
10379 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010380
Daniel Vetter24218aa2012-08-12 19:27:11 +020010381 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010382 old->load_detect_temp = false;
10383
10384 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010385 if (connector->dpms != DRM_MODE_DPMS_ON)
10386 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010387
Chris Wilson71731882011-04-19 23:10:58 +010010388 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010389 }
10390
10391 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010392 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010393 i++;
10394 if (!(encoder->possible_crtcs & (1 << i)))
10395 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010396 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010397 continue;
10398 /* This can occur when applying the pipe A quirk on resume. */
10399 if (to_intel_crtc(possible_crtc)->new_enabled)
10400 continue;
10401
10402 crtc = possible_crtc;
10403 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 }
10405
10406 /*
10407 * If we didn't find an unused CRTC, don't use any.
10408 */
10409 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010410 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010411 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010412 }
10413
Rob Clark51fd3712013-11-19 12:10:12 -050010414 ret = drm_modeset_lock(&crtc->mutex, ctx);
10415 if (ret)
10416 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010417 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10418 if (ret)
10419 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010420 intel_encoder->new_crtc = to_intel_crtc(crtc);
10421 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010422
10423 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010424 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010425 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010426 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010427 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010428
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010429 state = drm_atomic_state_alloc(dev);
10430 if (!state)
10431 return false;
10432
10433 state->acquire_ctx = ctx;
10434
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010435 connector_state = drm_atomic_get_connector_state(state, connector);
10436 if (IS_ERR(connector_state)) {
10437 ret = PTR_ERR(connector_state);
10438 goto fail;
10439 }
10440
10441 connector_state->crtc = crtc;
10442 connector_state->best_encoder = &intel_encoder->base;
10443
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010444 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10445 if (IS_ERR(crtc_state)) {
10446 ret = PTR_ERR(crtc_state);
10447 goto fail;
10448 }
10449
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010450 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010451
Chris Wilson64927112011-04-20 07:25:26 +010010452 if (!mode)
10453 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010454
Chris Wilsond2dff872011-04-19 08:36:26 +010010455 /* We need a framebuffer large enough to accommodate all accesses
10456 * that the plane may generate whilst we perform load detection.
10457 * We can not rely on the fbcon either being present (we get called
10458 * during its initialisation to detect all boot displays, or it may
10459 * not even exist) or that it is large enough to satisfy the
10460 * requested mode.
10461 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010462 fb = mode_fits_in_fbdev(dev, mode);
10463 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010464 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010465 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10466 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010467 } else
10468 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010469 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010470 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010471 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010472 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010473
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010474 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10475 if (ret)
10476 goto fail;
10477
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010478 drm_mode_copy(&crtc_state->base.mode, mode);
10479
10480 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010481 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010482 if (old->release_fb)
10483 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010484 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010485 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010486 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010487
Jesse Barnes79e53942008-11-07 14:24:08 -080010488 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010489 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010490 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010491
10492 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010493 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010494fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010495 drm_atomic_state_free(state);
10496 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010497
Rob Clark51fd3712013-11-19 12:10:12 -050010498 if (ret == -EDEADLK) {
10499 drm_modeset_backoff(ctx);
10500 goto retry;
10501 }
10502
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010503 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010504}
10505
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010506void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010507 struct intel_load_detect_pipe *old,
10508 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010509{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010510 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010511 struct intel_encoder *intel_encoder =
10512 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010513 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010514 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010515 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010516 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010517 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010518 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010519 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010520
Chris Wilsond2dff872011-04-19 08:36:26 +010010521 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010522 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010523 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010524
Chris Wilson8261b192011-04-19 23:18:09 +010010525 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010526 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010527 if (!state)
10528 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010529
10530 state->acquire_ctx = ctx;
10531
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010532 connector_state = drm_atomic_get_connector_state(state, connector);
10533 if (IS_ERR(connector_state))
10534 goto fail;
10535
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010536 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10537 if (IS_ERR(crtc_state))
10538 goto fail;
10539
Daniel Vetterfc303102012-07-09 10:40:58 +020010540 to_intel_connector(connector)->new_encoder = NULL;
10541 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010542 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010543
10544 connector_state->best_encoder = NULL;
10545 connector_state->crtc = NULL;
10546
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010547 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010548
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010549 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10550 0, 0);
10551 if (ret)
10552 goto fail;
10553
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010554 ret = intel_set_mode(crtc, state);
10555 if (ret)
10556 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010557
Daniel Vetter36206362012-12-10 20:42:17 +010010558 if (old->release_fb) {
10559 drm_framebuffer_unregister_private(old->release_fb);
10560 drm_framebuffer_unreference(old->release_fb);
10561 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010562
Chris Wilson0622a532011-04-21 09:32:11 +010010563 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010564 }
10565
Eric Anholtc751ce42010-03-25 11:48:48 -070010566 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010567 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10568 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010569
10570 return;
10571fail:
10572 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10573 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010574}
10575
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010576static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010577 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010578{
10579 struct drm_i915_private *dev_priv = dev->dev_private;
10580 u32 dpll = pipe_config->dpll_hw_state.dpll;
10581
10582 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010583 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010584 else if (HAS_PCH_SPLIT(dev))
10585 return 120000;
10586 else if (!IS_GEN2(dev))
10587 return 96000;
10588 else
10589 return 48000;
10590}
10591
Jesse Barnes79e53942008-11-07 14:24:08 -080010592/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010593static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010594 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010595{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010596 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010597 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010598 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010599 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010600 u32 fp;
10601 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010602 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010603
10604 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010605 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010606 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010607 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010608
10609 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010610 if (IS_PINEVIEW(dev)) {
10611 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10612 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010613 } else {
10614 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10615 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10616 }
10617
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010618 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010619 if (IS_PINEVIEW(dev))
10620 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10621 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010622 else
10623 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010624 DPLL_FPA01_P1_POST_DIV_SHIFT);
10625
10626 switch (dpll & DPLL_MODE_MASK) {
10627 case DPLLB_MODE_DAC_SERIAL:
10628 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10629 5 : 10;
10630 break;
10631 case DPLLB_MODE_LVDS:
10632 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10633 7 : 14;
10634 break;
10635 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010636 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010637 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010638 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 }
10640
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010641 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010642 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010643 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010644 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010645 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010646 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010647 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010648
10649 if (is_lvds) {
10650 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10651 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010652
10653 if (lvds & LVDS_CLKB_POWER_UP)
10654 clock.p2 = 7;
10655 else
10656 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010657 } else {
10658 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10659 clock.p1 = 2;
10660 else {
10661 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10662 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10663 }
10664 if (dpll & PLL_P2_DIVIDE_BY_4)
10665 clock.p2 = 4;
10666 else
10667 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010668 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010669
10670 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010671 }
10672
Ville Syrjälä18442d02013-09-13 16:00:08 +030010673 /*
10674 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010675 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010676 * encoder's get_config() function.
10677 */
10678 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010679}
10680
Ville Syrjälä6878da02013-09-13 15:59:11 +030010681int intel_dotclock_calculate(int link_freq,
10682 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010683{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010684 /*
10685 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010686 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010687 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010688 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010689 *
10690 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010691 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010692 */
10693
Ville Syrjälä6878da02013-09-13 15:59:11 +030010694 if (!m_n->link_n)
10695 return 0;
10696
10697 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10698}
10699
Ville Syrjälä18442d02013-09-13 16:00:08 +030010700static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010701 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010702{
10703 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010704
10705 /* read out port_clock from the DPLL */
10706 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010707
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010708 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010709 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010710 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010711 * agree once we know their relationship in the encoder's
10712 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010713 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010714 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010715 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10716 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010717}
10718
10719/** Returns the currently programmed mode of the given pipe. */
10720struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10721 struct drm_crtc *crtc)
10722{
Jesse Barnes548f2452011-02-17 10:40:53 -080010723 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010724 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010725 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010726 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010727 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010728 int htot = I915_READ(HTOTAL(cpu_transcoder));
10729 int hsync = I915_READ(HSYNC(cpu_transcoder));
10730 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10731 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010732 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010733
10734 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10735 if (!mode)
10736 return NULL;
10737
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010738 /*
10739 * Construct a pipe_config sufficient for getting the clock info
10740 * back out of crtc_clock_get.
10741 *
10742 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10743 * to use a real value here instead.
10744 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010745 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010746 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010747 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10748 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10749 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010750 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10751
Ville Syrjälä773ae032013-09-23 17:48:20 +030010752 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010753 mode->hdisplay = (htot & 0xffff) + 1;
10754 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10755 mode->hsync_start = (hsync & 0xffff) + 1;
10756 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10757 mode->vdisplay = (vtot & 0xffff) + 1;
10758 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10759 mode->vsync_start = (vsync & 0xffff) + 1;
10760 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10761
10762 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010763
10764 return mode;
10765}
10766
Jesse Barnes652c3932009-08-17 13:31:43 -070010767static void intel_decrease_pllclock(struct drm_crtc *crtc)
10768{
10769 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010770 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010771 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010772
Sonika Jindalbaff2962014-07-22 11:16:35 +053010773 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010774 return;
10775
10776 if (!dev_priv->lvds_downclock_avail)
10777 return;
10778
10779 /*
10780 * Since this is called by a timer, we should never get here in
10781 * the manual case.
10782 */
10783 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010784 int pipe = intel_crtc->pipe;
10785 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010786 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010787
Zhao Yakui44d98a62009-10-09 11:39:40 +080010788 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010789
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010790 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010791
Chris Wilson074b5e12012-05-02 12:07:06 +010010792 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010793 dpll |= DISPLAY_RATE_SELECT_FPA1;
10794 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010795 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010796 dpll = I915_READ(dpll_reg);
10797 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010798 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010799 }
10800
10801}
10802
Chris Wilsonf047e392012-07-21 12:31:41 +010010803void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010804{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010805 struct drm_i915_private *dev_priv = dev->dev_private;
10806
Chris Wilsonf62a0072014-02-21 17:55:39 +000010807 if (dev_priv->mm.busy)
10808 return;
10809
Paulo Zanoni43694d62014-03-07 20:08:08 -030010810 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010811 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010812 if (INTEL_INFO(dev)->gen >= 6)
10813 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010814 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010815}
10816
10817void intel_mark_idle(struct drm_device *dev)
10818{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010819 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010820 struct drm_crtc *crtc;
10821
Chris Wilsonf62a0072014-02-21 17:55:39 +000010822 if (!dev_priv->mm.busy)
10823 return;
10824
10825 dev_priv->mm.busy = false;
10826
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010827 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010828 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010829 continue;
10830
10831 intel_decrease_pllclock(crtc);
10832 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010833
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010834 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010835 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010836
Paulo Zanoni43694d62014-03-07 20:08:08 -030010837 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010838}
10839
Jesse Barnes79e53942008-11-07 14:24:08 -080010840static void intel_crtc_destroy(struct drm_crtc *crtc)
10841{
10842 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010843 struct drm_device *dev = crtc->dev;
10844 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010845
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010846 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010847 work = intel_crtc->unpin_work;
10848 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010849 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010850
10851 if (work) {
10852 cancel_work_sync(&work->work);
10853 kfree(work);
10854 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010855
10856 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010857
Jesse Barnes79e53942008-11-07 14:24:08 -080010858 kfree(intel_crtc);
10859}
10860
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010861static void intel_unpin_work_fn(struct work_struct *__work)
10862{
10863 struct intel_unpin_work *work =
10864 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010865 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010866 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010867
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010868 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010869 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010870 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010871
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010872 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010873
10874 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010875 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010876 mutex_unlock(&dev->struct_mutex);
10877
Daniel Vetterf99d7062014-06-19 16:01:59 +020010878 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010879 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010880
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010881 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10882 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10883
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010884 kfree(work);
10885}
10886
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010887static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010888 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010889{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010890 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10891 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010892 unsigned long flags;
10893
10894 /* Ignore early vblank irqs */
10895 if (intel_crtc == NULL)
10896 return;
10897
Daniel Vetterf3260382014-09-15 14:55:23 +020010898 /*
10899 * This is called both by irq handlers and the reset code (to complete
10900 * lost pageflips) so needs the full irqsave spinlocks.
10901 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010902 spin_lock_irqsave(&dev->event_lock, flags);
10903 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010904
10905 /* Ensure we don't miss a work->pending update ... */
10906 smp_rmb();
10907
10908 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010909 spin_unlock_irqrestore(&dev->event_lock, flags);
10910 return;
10911 }
10912
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010913 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010914
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010915 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010916}
10917
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010918void intel_finish_page_flip(struct drm_device *dev, int pipe)
10919{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010920 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010921 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10922
Mario Kleiner49b14a52010-12-09 07:00:07 +010010923 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010924}
10925
10926void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10927{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010928 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010929 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10930
Mario Kleiner49b14a52010-12-09 07:00:07 +010010931 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010932}
10933
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010934/* Is 'a' after or equal to 'b'? */
10935static bool g4x_flip_count_after_eq(u32 a, u32 b)
10936{
10937 return !((a - b) & 0x80000000);
10938}
10939
10940static bool page_flip_finished(struct intel_crtc *crtc)
10941{
10942 struct drm_device *dev = crtc->base.dev;
10943 struct drm_i915_private *dev_priv = dev->dev_private;
10944
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010945 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10946 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10947 return true;
10948
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010949 /*
10950 * The relevant registers doen't exist on pre-ctg.
10951 * As the flip done interrupt doesn't trigger for mmio
10952 * flips on gmch platforms, a flip count check isn't
10953 * really needed there. But since ctg has the registers,
10954 * include it in the check anyway.
10955 */
10956 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10957 return true;
10958
10959 /*
10960 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10961 * used the same base address. In that case the mmio flip might
10962 * have completed, but the CS hasn't even executed the flip yet.
10963 *
10964 * A flip count check isn't enough as the CS might have updated
10965 * the base address just after start of vblank, but before we
10966 * managed to process the interrupt. This means we'd complete the
10967 * CS flip too soon.
10968 *
10969 * Combining both checks should get us a good enough result. It may
10970 * still happen that the CS flip has been executed, but has not
10971 * yet actually completed. But in case the base address is the same
10972 * anyway, we don't really care.
10973 */
10974 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10975 crtc->unpin_work->gtt_offset &&
10976 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10977 crtc->unpin_work->flip_count);
10978}
10979
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010980void intel_prepare_page_flip(struct drm_device *dev, int plane)
10981{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010982 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010983 struct intel_crtc *intel_crtc =
10984 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
10985 unsigned long flags;
10986
Daniel Vetterf3260382014-09-15 14:55:23 +020010987
10988 /*
10989 * This is called both by irq handlers and the reset code (to complete
10990 * lost pageflips) so needs the full irqsave spinlocks.
10991 *
10992 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000010993 * generate a page-flip completion irq, i.e. every modeset
10994 * is also accompanied by a spurious intel_prepare_page_flip().
10995 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010996 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010997 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000010998 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010999 spin_unlock_irqrestore(&dev->event_lock, flags);
11000}
11001
Robin Schroereba905b2014-05-18 02:24:50 +020011002static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011003{
11004 /* Ensure that the work item is consistent when activating it ... */
11005 smp_wmb();
11006 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
11007 /* and that it is marked active as soon as the irq could fire. */
11008 smp_wmb();
11009}
11010
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011011static int intel_gen2_queue_flip(struct drm_device *dev,
11012 struct drm_crtc *crtc,
11013 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011014 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011015 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011016 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011017{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011018 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011019 u32 flip_mask;
11020 int ret;
11021
Daniel Vetter6d90c952012-04-26 23:28:05 +020011022 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011023 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011024 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011025
11026 /* Can't queue multiple flips, so wait for the previous
11027 * one to finish before executing the next.
11028 */
11029 if (intel_crtc->plane)
11030 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11031 else
11032 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011033 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11034 intel_ring_emit(ring, MI_NOOP);
11035 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11036 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11037 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011038 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011039 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011040
11041 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011042 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011043 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011044}
11045
11046static int intel_gen3_queue_flip(struct drm_device *dev,
11047 struct drm_crtc *crtc,
11048 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011049 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011050 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011051 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011052{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011053 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011054 u32 flip_mask;
11055 int ret;
11056
Daniel Vetter6d90c952012-04-26 23:28:05 +020011057 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011058 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011059 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011060
11061 if (intel_crtc->plane)
11062 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11063 else
11064 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011065 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11066 intel_ring_emit(ring, MI_NOOP);
11067 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11068 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11069 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011070 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011071 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011072
Chris Wilsone7d841c2012-12-03 11:36:30 +000011073 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011074 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011075 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011076}
11077
11078static int intel_gen4_queue_flip(struct drm_device *dev,
11079 struct drm_crtc *crtc,
11080 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011081 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011082 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011083 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011084{
11085 struct drm_i915_private *dev_priv = dev->dev_private;
11086 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11087 uint32_t pf, pipesrc;
11088 int ret;
11089
Daniel Vetter6d90c952012-04-26 23:28:05 +020011090 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011091 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011092 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011093
11094 /* i965+ uses the linear or tiled offsets from the
11095 * Display Registers (which do not change across a page-flip)
11096 * so we need only reprogram the base address.
11097 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011098 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11099 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11100 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011101 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011102 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011103
11104 /* XXX Enabling the panel-fitter across page-flip is so far
11105 * untested on non-native modes, so ignore it for now.
11106 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11107 */
11108 pf = 0;
11109 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011110 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011111
11112 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011113 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011114 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011115}
11116
11117static int intel_gen6_queue_flip(struct drm_device *dev,
11118 struct drm_crtc *crtc,
11119 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011120 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011121 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011122 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011123{
11124 struct drm_i915_private *dev_priv = dev->dev_private;
11125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11126 uint32_t pf, pipesrc;
11127 int ret;
11128
Daniel Vetter6d90c952012-04-26 23:28:05 +020011129 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011130 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011131 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011132
Daniel Vetter6d90c952012-04-26 23:28:05 +020011133 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11134 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11135 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011136 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011137
Chris Wilson99d9acd2012-04-17 20:37:00 +010011138 /* Contrary to the suggestions in the documentation,
11139 * "Enable Panel Fitter" does not seem to be required when page
11140 * flipping with a non-native mode, and worse causes a normal
11141 * modeset to fail.
11142 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11143 */
11144 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011145 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011146 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011147
11148 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011149 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011150 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011151}
11152
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011153static int intel_gen7_queue_flip(struct drm_device *dev,
11154 struct drm_crtc *crtc,
11155 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011156 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011157 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011158 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011159{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011160 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011161 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011162 int len, ret;
11163
Robin Schroereba905b2014-05-18 02:24:50 +020011164 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011165 case PLANE_A:
11166 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11167 break;
11168 case PLANE_B:
11169 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11170 break;
11171 case PLANE_C:
11172 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11173 break;
11174 default:
11175 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011176 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011177 }
11178
Chris Wilsonffe74d72013-08-26 20:58:12 +010011179 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011180 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011181 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011182 /*
11183 * On Gen 8, SRM is now taking an extra dword to accommodate
11184 * 48bits addresses, and we need a NOOP for the batch size to
11185 * stay even.
11186 */
11187 if (IS_GEN8(dev))
11188 len += 2;
11189 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011190
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011191 /*
11192 * BSpec MI_DISPLAY_FLIP for IVB:
11193 * "The full packet must be contained within the same cache line."
11194 *
11195 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11196 * cacheline, if we ever start emitting more commands before
11197 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11198 * then do the cacheline alignment, and finally emit the
11199 * MI_DISPLAY_FLIP.
11200 */
11201 ret = intel_ring_cacheline_align(ring);
11202 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011203 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011204
Chris Wilsonffe74d72013-08-26 20:58:12 +010011205 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011206 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011207 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011208
Chris Wilsonffe74d72013-08-26 20:58:12 +010011209 /* Unmask the flip-done completion message. Note that the bspec says that
11210 * we should do this for both the BCS and RCS, and that we must not unmask
11211 * more than one flip event at any time (or ensure that one flip message
11212 * can be sent by waiting for flip-done prior to queueing new flips).
11213 * Experimentation says that BCS works despite DERRMR masking all
11214 * flip-done completion events and that unmasking all planes at once
11215 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11216 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11217 */
11218 if (ring->id == RCS) {
11219 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11220 intel_ring_emit(ring, DERRMR);
11221 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11222 DERRMR_PIPEB_PRI_FLIP_DONE |
11223 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011224 if (IS_GEN8(dev))
11225 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11226 MI_SRM_LRM_GLOBAL_GTT);
11227 else
11228 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11229 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011230 intel_ring_emit(ring, DERRMR);
11231 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011232 if (IS_GEN8(dev)) {
11233 intel_ring_emit(ring, 0);
11234 intel_ring_emit(ring, MI_NOOP);
11235 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011236 }
11237
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011238 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011239 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011240 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011241 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011242
11243 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011244 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011245 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011246}
11247
Sourab Gupta84c33a62014-06-02 16:47:17 +053011248static bool use_mmio_flip(struct intel_engine_cs *ring,
11249 struct drm_i915_gem_object *obj)
11250{
11251 /*
11252 * This is not being used for older platforms, because
11253 * non-availability of flip done interrupt forces us to use
11254 * CS flips. Older platforms derive flip done using some clever
11255 * tricks involving the flip_pending status bits and vblank irqs.
11256 * So using MMIO flips there would disrupt this mechanism.
11257 */
11258
Chris Wilson8e09bf82014-07-08 10:40:30 +010011259 if (ring == NULL)
11260 return true;
11261
Sourab Gupta84c33a62014-06-02 16:47:17 +053011262 if (INTEL_INFO(ring->dev)->gen < 5)
11263 return false;
11264
11265 if (i915.use_mmio_flip < 0)
11266 return false;
11267 else if (i915.use_mmio_flip > 0)
11268 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011269 else if (i915.enable_execlists)
11270 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011271 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011272 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011273}
11274
Damien Lespiauff944562014-11-20 14:58:16 +000011275static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11276{
11277 struct drm_device *dev = intel_crtc->base.dev;
11278 struct drm_i915_private *dev_priv = dev->dev_private;
11279 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011280 const enum pipe pipe = intel_crtc->pipe;
11281 u32 ctl, stride;
11282
11283 ctl = I915_READ(PLANE_CTL(pipe, 0));
11284 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011285 switch (fb->modifier[0]) {
11286 case DRM_FORMAT_MOD_NONE:
11287 break;
11288 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011289 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011290 break;
11291 case I915_FORMAT_MOD_Y_TILED:
11292 ctl |= PLANE_CTL_TILED_Y;
11293 break;
11294 case I915_FORMAT_MOD_Yf_TILED:
11295 ctl |= PLANE_CTL_TILED_YF;
11296 break;
11297 default:
11298 MISSING_CASE(fb->modifier[0]);
11299 }
Damien Lespiauff944562014-11-20 14:58:16 +000011300
11301 /*
11302 * The stride is either expressed as a multiple of 64 bytes chunks for
11303 * linear buffers or in number of tiles for tiled buffers.
11304 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011305 stride = fb->pitches[0] /
11306 intel_fb_stride_alignment(dev, fb->modifier[0],
11307 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011308
11309 /*
11310 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11311 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11312 */
11313 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11314 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11315
11316 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11317 POSTING_READ(PLANE_SURF(pipe, 0));
11318}
11319
11320static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011321{
11322 struct drm_device *dev = intel_crtc->base.dev;
11323 struct drm_i915_private *dev_priv = dev->dev_private;
11324 struct intel_framebuffer *intel_fb =
11325 to_intel_framebuffer(intel_crtc->base.primary->fb);
11326 struct drm_i915_gem_object *obj = intel_fb->obj;
11327 u32 dspcntr;
11328 u32 reg;
11329
Sourab Gupta84c33a62014-06-02 16:47:17 +053011330 reg = DSPCNTR(intel_crtc->plane);
11331 dspcntr = I915_READ(reg);
11332
Damien Lespiauc5d97472014-10-25 00:11:11 +010011333 if (obj->tiling_mode != I915_TILING_NONE)
11334 dspcntr |= DISPPLANE_TILED;
11335 else
11336 dspcntr &= ~DISPPLANE_TILED;
11337
Sourab Gupta84c33a62014-06-02 16:47:17 +053011338 I915_WRITE(reg, dspcntr);
11339
11340 I915_WRITE(DSPSURF(intel_crtc->plane),
11341 intel_crtc->unpin_work->gtt_offset);
11342 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011343
Damien Lespiauff944562014-11-20 14:58:16 +000011344}
11345
11346/*
11347 * XXX: This is the temporary way to update the plane registers until we get
11348 * around to using the usual plane update functions for MMIO flips
11349 */
11350static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11351{
11352 struct drm_device *dev = intel_crtc->base.dev;
11353 bool atomic_update;
11354 u32 start_vbl_count;
11355
11356 intel_mark_page_flip_active(intel_crtc);
11357
11358 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11359
11360 if (INTEL_INFO(dev)->gen >= 9)
11361 skl_do_mmio_flip(intel_crtc);
11362 else
11363 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11364 ilk_do_mmio_flip(intel_crtc);
11365
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011366 if (atomic_update)
11367 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011368}
11369
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011370static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011371{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011372 struct intel_mmio_flip *mmio_flip =
11373 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011374
Daniel Vettereed29a52015-05-21 14:21:25 +020011375 if (mmio_flip->req)
11376 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011377 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011378 false, NULL,
11379 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011380
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011381 intel_do_mmio_flip(mmio_flip->crtc);
11382
Daniel Vettereed29a52015-05-21 14:21:25 +020011383 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011384 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011385}
11386
11387static int intel_queue_mmio_flip(struct drm_device *dev,
11388 struct drm_crtc *crtc,
11389 struct drm_framebuffer *fb,
11390 struct drm_i915_gem_object *obj,
11391 struct intel_engine_cs *ring,
11392 uint32_t flags)
11393{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011394 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011395
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011396 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11397 if (mmio_flip == NULL)
11398 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011399
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011400 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011401 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011402 mmio_flip->crtc = to_intel_crtc(crtc);
11403
11404 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11405 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011406
Sourab Gupta84c33a62014-06-02 16:47:17 +053011407 return 0;
11408}
11409
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011410static int intel_default_queue_flip(struct drm_device *dev,
11411 struct drm_crtc *crtc,
11412 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011413 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011414 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011415 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011416{
11417 return -ENODEV;
11418}
11419
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011420static bool __intel_pageflip_stall_check(struct drm_device *dev,
11421 struct drm_crtc *crtc)
11422{
11423 struct drm_i915_private *dev_priv = dev->dev_private;
11424 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11425 struct intel_unpin_work *work = intel_crtc->unpin_work;
11426 u32 addr;
11427
11428 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11429 return true;
11430
11431 if (!work->enable_stall_check)
11432 return false;
11433
11434 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011435 if (work->flip_queued_req &&
11436 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011437 return false;
11438
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011439 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011440 }
11441
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011442 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011443 return false;
11444
11445 /* Potential stall - if we see that the flip has happened,
11446 * assume a missed interrupt. */
11447 if (INTEL_INFO(dev)->gen >= 4)
11448 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11449 else
11450 addr = I915_READ(DSPADDR(intel_crtc->plane));
11451
11452 /* There is a potential issue here with a false positive after a flip
11453 * to the same address. We could address this by checking for a
11454 * non-incrementing frame counter.
11455 */
11456 return addr == work->gtt_offset;
11457}
11458
11459void intel_check_page_flip(struct drm_device *dev, int pipe)
11460{
11461 struct drm_i915_private *dev_priv = dev->dev_private;
11462 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11463 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011464 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011465
Dave Gordon6c51d462015-03-06 15:34:26 +000011466 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011467
11468 if (crtc == NULL)
11469 return;
11470
Daniel Vetterf3260382014-09-15 14:55:23 +020011471 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011472 work = intel_crtc->unpin_work;
11473 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011474 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011475 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011476 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011477 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011478 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011479 if (work != NULL &&
11480 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11481 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011482 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011483}
11484
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011485static int intel_crtc_page_flip(struct drm_crtc *crtc,
11486 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011487 struct drm_pending_vblank_event *event,
11488 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011489{
11490 struct drm_device *dev = crtc->dev;
11491 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011492 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011493 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011494 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011495 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011496 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011497 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011498 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011499 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011500 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011501
Matt Roper2ff8fde2014-07-08 07:50:07 -070011502 /*
11503 * drm_mode_page_flip_ioctl() should already catch this, but double
11504 * check to be safe. In the future we may enable pageflipping from
11505 * a disabled primary plane.
11506 */
11507 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11508 return -EBUSY;
11509
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011510 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011511 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011512 return -EINVAL;
11513
11514 /*
11515 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11516 * Note that pitch changes could also affect these register.
11517 */
11518 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011519 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11520 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011521 return -EINVAL;
11522
Chris Wilsonf900db42014-02-20 09:26:13 +000011523 if (i915_terminally_wedged(&dev_priv->gpu_error))
11524 goto out_hang;
11525
Daniel Vetterb14c5672013-09-19 12:18:32 +020011526 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011527 if (work == NULL)
11528 return -ENOMEM;
11529
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011530 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011531 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011532 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011533 INIT_WORK(&work->work, intel_unpin_work_fn);
11534
Daniel Vetter87b6b102014-05-15 15:33:46 +020011535 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011536 if (ret)
11537 goto free_work;
11538
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011539 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011540 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011541 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011542 /* Before declaring the flip queue wedged, check if
11543 * the hardware completed the operation behind our backs.
11544 */
11545 if (__intel_pageflip_stall_check(dev, crtc)) {
11546 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11547 page_flip_completed(intel_crtc);
11548 } else {
11549 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011550 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011551
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011552 drm_crtc_vblank_put(crtc);
11553 kfree(work);
11554 return -EBUSY;
11555 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011556 }
11557 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011558 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011559
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011560 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11561 flush_workqueue(dev_priv->wq);
11562
Jesse Barnes75dfca82010-02-10 15:09:44 -080011563 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011564 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011565 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011566
Matt Roperf4510a22014-04-01 15:22:40 -070011567 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011568 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011569
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011570 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011571
Chris Wilson89ed88b2015-02-16 14:31:49 +000011572 ret = i915_mutex_lock_interruptible(dev);
11573 if (ret)
11574 goto cleanup;
11575
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011576 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011577 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011578
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011579 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011580 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011581
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011582 if (IS_VALLEYVIEW(dev)) {
11583 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011584 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011585 /* vlv: DISPLAY_FLIP fails to change tiling */
11586 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011587 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011588 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011589 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011590 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011591 if (ring == NULL || ring->id != RCS)
11592 ring = &dev_priv->ring[BCS];
11593 } else {
11594 ring = &dev_priv->ring[RCS];
11595 }
11596
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011597 mmio_flip = use_mmio_flip(ring, obj);
11598
11599 /* When using CS flips, we want to emit semaphores between rings.
11600 * However, when using mmio flips we will create a task to do the
11601 * synchronisation, so all we want here is to pin the framebuffer
11602 * into the display plane and skip any waits.
11603 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011604 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011605 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011606 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011607 if (ret)
11608 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011609
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011610 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11611 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011612
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011613 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011614 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11615 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011616 if (ret)
11617 goto cleanup_unpin;
11618
John Harrisonf06cc1b2014-11-24 18:49:37 +000011619 i915_gem_request_assign(&work->flip_queued_req,
11620 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011621 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011622 if (obj->last_write_req) {
11623 ret = i915_gem_check_olr(obj->last_write_req);
11624 if (ret)
11625 goto cleanup_unpin;
11626 }
11627
Sourab Gupta84c33a62014-06-02 16:47:17 +053011628 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011629 page_flip_flags);
11630 if (ret)
11631 goto cleanup_unpin;
11632
John Harrisonf06cc1b2014-11-24 18:49:37 +000011633 i915_gem_request_assign(&work->flip_queued_req,
11634 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011635 }
11636
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011637 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011638 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011639
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011640 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011641 INTEL_FRONTBUFFER_PRIMARY(pipe));
11642
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011643 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011644 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011645 mutex_unlock(&dev->struct_mutex);
11646
Jesse Barnese5510fa2010-07-01 16:48:37 -070011647 trace_i915_flip_request(intel_crtc->plane, obj);
11648
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011649 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011650
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011651cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011652 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011653cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011654 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011655 mutex_unlock(&dev->struct_mutex);
11656cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011657 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011658 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011659
Chris Wilson89ed88b2015-02-16 14:31:49 +000011660 drm_gem_object_unreference_unlocked(&obj->base);
11661 drm_framebuffer_unreference(work->old_fb);
11662
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011663 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011664 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011665 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011666
Daniel Vetter87b6b102014-05-15 15:33:46 +020011667 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011668free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011669 kfree(work);
11670
Chris Wilsonf900db42014-02-20 09:26:13 +000011671 if (ret == -EIO) {
11672out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011673 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011674 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011675 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011676 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011677 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011678 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011679 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011680 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011681}
11682
Jani Nikula65b38e02015-04-13 11:26:56 +030011683static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011684 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11685 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011686 .atomic_begin = intel_begin_crtc_commit,
11687 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011688};
11689
Daniel Vetter9a935852012-07-05 22:34:27 +020011690/**
11691 * intel_modeset_update_staged_output_state
11692 *
11693 * Updates the staged output configuration state, e.g. after we've read out the
11694 * current hw state.
11695 */
11696static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11697{
Ville Syrjälä76688512014-01-10 11:28:06 +020011698 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011699 struct intel_encoder *encoder;
11700 struct intel_connector *connector;
11701
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011702 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011703 connector->new_encoder =
11704 to_intel_encoder(connector->base.encoder);
11705 }
11706
Damien Lespiaub2784e12014-08-05 11:29:37 +010011707 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011708 encoder->new_crtc =
11709 to_intel_crtc(encoder->base.crtc);
11710 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011711
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011712 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011713 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011714 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011715}
11716
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011717/* Transitional helper to copy current connector/encoder state to
11718 * connector->state. This is needed so that code that is partially
11719 * converted to atomic does the right thing.
11720 */
11721static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11722{
11723 struct intel_connector *connector;
11724
11725 for_each_intel_connector(dev, connector) {
11726 if (connector->base.encoder) {
11727 connector->base.state->best_encoder =
11728 connector->base.encoder;
11729 connector->base.state->crtc =
11730 connector->base.encoder->crtc;
11731 } else {
11732 connector->base.state->best_encoder = NULL;
11733 connector->base.state->crtc = NULL;
11734 }
11735 }
11736}
11737
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011738static void
Robin Schroereba905b2014-05-18 02:24:50 +020011739connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011740 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011741{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011742 int bpp = pipe_config->pipe_bpp;
11743
11744 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11745 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011746 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011747
11748 /* Don't use an invalid EDID bpc value */
11749 if (connector->base.display_info.bpc &&
11750 connector->base.display_info.bpc * 3 < bpp) {
11751 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11752 bpp, connector->base.display_info.bpc*3);
11753 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11754 }
11755
11756 /* Clamp bpp to 8 on screens without EDID 1.4 */
11757 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11758 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11759 bpp);
11760 pipe_config->pipe_bpp = 24;
11761 }
11762}
11763
11764static int
11765compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011766 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011767{
11768 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011769 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011770 struct drm_connector *connector;
11771 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011772 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011773
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011774 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011775 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011776 else if (INTEL_INFO(dev)->gen >= 5)
11777 bpp = 12*3;
11778 else
11779 bpp = 8*3;
11780
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011781
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011782 pipe_config->pipe_bpp = bpp;
11783
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011784 state = pipe_config->base.state;
11785
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011786 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011787 for_each_connector_in_state(state, connector, connector_state, i) {
11788 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011789 continue;
11790
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011791 connected_sink_compute_bpp(to_intel_connector(connector),
11792 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011793 }
11794
11795 return bpp;
11796}
11797
Daniel Vetter644db712013-09-19 14:53:58 +020011798static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11799{
11800 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11801 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011802 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011803 mode->crtc_hdisplay, mode->crtc_hsync_start,
11804 mode->crtc_hsync_end, mode->crtc_htotal,
11805 mode->crtc_vdisplay, mode->crtc_vsync_start,
11806 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11807}
11808
Daniel Vetterc0b03412013-05-28 12:05:54 +020011809static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011810 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011811 const char *context)
11812{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011813 struct drm_device *dev = crtc->base.dev;
11814 struct drm_plane *plane;
11815 struct intel_plane *intel_plane;
11816 struct intel_plane_state *state;
11817 struct drm_framebuffer *fb;
11818
11819 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11820 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011821
11822 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11823 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11824 pipe_config->pipe_bpp, pipe_config->dither);
11825 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11826 pipe_config->has_pch_encoder,
11827 pipe_config->fdi_lanes,
11828 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11829 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11830 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011831 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11832 pipe_config->has_dp_encoder,
11833 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11834 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11835 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011836
11837 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11838 pipe_config->has_dp_encoder,
11839 pipe_config->dp_m2_n2.gmch_m,
11840 pipe_config->dp_m2_n2.gmch_n,
11841 pipe_config->dp_m2_n2.link_m,
11842 pipe_config->dp_m2_n2.link_n,
11843 pipe_config->dp_m2_n2.tu);
11844
Daniel Vetter55072d12014-11-20 16:10:28 +010011845 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11846 pipe_config->has_audio,
11847 pipe_config->has_infoframe);
11848
Daniel Vetterc0b03412013-05-28 12:05:54 +020011849 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011850 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011851 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011852 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11853 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011854 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011855 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11856 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011857 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11858 crtc->num_scalers,
11859 pipe_config->scaler_state.scaler_users,
11860 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011861 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11862 pipe_config->gmch_pfit.control,
11863 pipe_config->gmch_pfit.pgm_ratios,
11864 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011865 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011866 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011867 pipe_config->pch_pfit.size,
11868 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011869 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011870 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011871
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011872 if (IS_BROXTON(dev)) {
11873 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11874 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11875 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11876 pipe_config->ddi_pll_sel,
11877 pipe_config->dpll_hw_state.ebb0,
11878 pipe_config->dpll_hw_state.pll0,
11879 pipe_config->dpll_hw_state.pll1,
11880 pipe_config->dpll_hw_state.pll2,
11881 pipe_config->dpll_hw_state.pll3,
11882 pipe_config->dpll_hw_state.pll6,
11883 pipe_config->dpll_hw_state.pll8,
11884 pipe_config->dpll_hw_state.pcsdw12);
11885 } else if (IS_SKYLAKE(dev)) {
11886 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11887 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11888 pipe_config->ddi_pll_sel,
11889 pipe_config->dpll_hw_state.ctrl1,
11890 pipe_config->dpll_hw_state.cfgcr1,
11891 pipe_config->dpll_hw_state.cfgcr2);
11892 } else if (HAS_DDI(dev)) {
11893 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11894 pipe_config->ddi_pll_sel,
11895 pipe_config->dpll_hw_state.wrpll);
11896 } else {
11897 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11898 "fp0: 0x%x, fp1: 0x%x\n",
11899 pipe_config->dpll_hw_state.dpll,
11900 pipe_config->dpll_hw_state.dpll_md,
11901 pipe_config->dpll_hw_state.fp0,
11902 pipe_config->dpll_hw_state.fp1);
11903 }
11904
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011905 DRM_DEBUG_KMS("planes on this crtc\n");
11906 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11907 intel_plane = to_intel_plane(plane);
11908 if (intel_plane->pipe != crtc->pipe)
11909 continue;
11910
11911 state = to_intel_plane_state(plane->state);
11912 fb = state->base.fb;
11913 if (!fb) {
11914 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11915 "disabled, scaler_id = %d\n",
11916 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11917 plane->base.id, intel_plane->pipe,
11918 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11919 drm_plane_index(plane), state->scaler_id);
11920 continue;
11921 }
11922
11923 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11924 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11925 plane->base.id, intel_plane->pipe,
11926 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11927 drm_plane_index(plane));
11928 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11929 fb->base.id, fb->width, fb->height, fb->pixel_format);
11930 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11931 state->scaler_id,
11932 state->src.x1 >> 16, state->src.y1 >> 16,
11933 drm_rect_width(&state->src) >> 16,
11934 drm_rect_height(&state->src) >> 16,
11935 state->dst.x1, state->dst.y1,
11936 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11937 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011938}
11939
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011940static bool encoders_cloneable(const struct intel_encoder *a,
11941 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011942{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011943 /* masks could be asymmetric, so check both ways */
11944 return a == b || (a->cloneable & (1 << b->type) &&
11945 b->cloneable & (1 << a->type));
11946}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011947
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011948static bool check_single_encoder_cloning(struct drm_atomic_state *state,
11949 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011950 struct intel_encoder *encoder)
11951{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011952 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011953 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011954 struct drm_connector_state *connector_state;
11955 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011956
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011957 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011958 if (connector_state->crtc != &crtc->base)
11959 continue;
11960
11961 source_encoder =
11962 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011963 if (!encoders_cloneable(encoder, source_encoder))
11964 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011965 }
11966
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011967 return true;
11968}
11969
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011970static bool check_encoder_cloning(struct drm_atomic_state *state,
11971 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011972{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011973 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011974 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011975 struct drm_connector_state *connector_state;
11976 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011977
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011978 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030011979 if (connector_state->crtc != &crtc->base)
11980 continue;
11981
11982 encoder = to_intel_encoder(connector_state->best_encoder);
11983 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011984 return false;
11985 }
11986
11987 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011988}
11989
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011990static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011991{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011992 struct drm_device *dev = state->dev;
11993 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011994 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011995 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011996 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030011997 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020011998
11999 /*
12000 * Walk the connector list instead of the encoder
12001 * list to detect the problem on ddi platforms
12002 * where there's just one encoder per digital port.
12003 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012004 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012005 if (!connector_state->best_encoder)
12006 continue;
12007
12008 encoder = to_intel_encoder(connector_state->best_encoder);
12009
12010 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012011
12012 switch (encoder->type) {
12013 unsigned int port_mask;
12014 case INTEL_OUTPUT_UNKNOWN:
12015 if (WARN_ON(!HAS_DDI(dev)))
12016 break;
12017 case INTEL_OUTPUT_DISPLAYPORT:
12018 case INTEL_OUTPUT_HDMI:
12019 case INTEL_OUTPUT_EDP:
12020 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12021
12022 /* the same port mustn't appear more than once */
12023 if (used_ports & port_mask)
12024 return false;
12025
12026 used_ports |= port_mask;
12027 default:
12028 break;
12029 }
12030 }
12031
12032 return true;
12033}
12034
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012035static void
12036clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12037{
12038 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012039 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012040 struct intel_dpll_hw_state dpll_hw_state;
12041 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012042 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012043
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012044 /* FIXME: before the switch to atomic started, a new pipe_config was
12045 * kzalloc'd. Code that depends on any field being zero should be
12046 * fixed, so that the crtc_state can be safely duplicated. For now,
12047 * only fields that are know to not cause problems are preserved. */
12048
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012049 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012050 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012051 shared_dpll = crtc_state->shared_dpll;
12052 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012053 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012054
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012055 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012056
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012057 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012058 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012059 crtc_state->shared_dpll = shared_dpll;
12060 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012061 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012062}
12063
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012064static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012065intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012066 struct drm_atomic_state *state,
12067 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012068{
Daniel Vetter7758a112012-07-08 19:40:39 +020012069 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012070 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012071 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012072 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012073 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012074 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012075
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012076 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012077 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012078 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012079 }
12080
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012081 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012082 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012083 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012084 }
12085
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012086 /*
12087 * XXX: Add all connectors to make the crtc state match the encoders.
12088 */
12089 if (!needs_modeset(&pipe_config->base)) {
12090 ret = drm_atomic_add_affected_connectors(state, crtc);
12091 if (ret)
12092 return ret;
12093 }
12094
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012095 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012096
Daniel Vettere143a212013-07-04 12:01:15 +020012097 pipe_config->cpu_transcoder =
12098 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012099
Imre Deak2960bc92013-07-30 13:36:32 +030012100 /*
12101 * Sanitize sync polarity flags based on requested ones. If neither
12102 * positive or negative polarity is requested, treat this as meaning
12103 * negative polarity.
12104 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012105 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012106 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012107 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012108
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012109 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012110 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012111 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012112
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012113 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12114 * plane pixel format and any sink constraints into account. Returns the
12115 * source plane bpp so that dithering can be selected on mismatches
12116 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012117 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12118 pipe_config);
12119 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012120 goto fail;
12121
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012122 /*
12123 * Determine the real pipe dimensions. Note that stereo modes can
12124 * increase the actual pipe size due to the frame doubling and
12125 * insertion of additional space for blanks between the frame. This
12126 * is stored in the crtc timings. We use the requested mode to do this
12127 * computation to clearly distinguish it from the adjusted mode, which
12128 * can be changed by the connectors in the below retry loop.
12129 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012130 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012131 &pipe_config->pipe_src_w,
12132 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012133
Daniel Vettere29c22c2013-02-21 00:00:16 +010012134encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012135 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012136 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012137 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012138
Daniel Vetter135c81b2013-07-21 21:37:09 +020012139 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012140 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12141 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012142
Daniel Vetter7758a112012-07-08 19:40:39 +020012143 /* Pass our mode to the connectors and the CRTC to give them a chance to
12144 * adjust it according to limitations or connector properties, and also
12145 * a chance to reject the mode entirely.
12146 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012147 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012148 if (connector_state->crtc != crtc)
12149 continue;
12150
12151 encoder = to_intel_encoder(connector_state->best_encoder);
12152
Daniel Vetterefea6e82013-07-21 21:36:59 +020012153 if (!(encoder->compute_config(encoder, pipe_config))) {
12154 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012155 goto fail;
12156 }
12157 }
12158
Daniel Vetterff9a6752013-06-01 17:16:21 +020012159 /* Set default port clock if not overwritten by the encoder. Needs to be
12160 * done afterwards in case the encoder adjusts the mode. */
12161 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012162 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012163 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012164
Daniel Vettera43f6e02013-06-07 23:10:32 +020012165 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012166 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012167 DRM_DEBUG_KMS("CRTC fixup failed\n");
12168 goto fail;
12169 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012170
12171 if (ret == RETRY) {
12172 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12173 ret = -EINVAL;
12174 goto fail;
12175 }
12176
12177 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12178 retry = false;
12179 goto encoder_retry;
12180 }
12181
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012182 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012183 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012184 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012185
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012186 /* Check if we need to force a modeset */
12187 if (pipe_config->has_audio !=
12188 to_intel_crtc_state(crtc->state)->has_audio)
12189 pipe_config->base.mode_changed = true;
12190
12191 /*
12192 * Note we have an issue here with infoframes: current code
12193 * only updates them on the full mode set path per hw
12194 * requirements. So here we should be checking for any
12195 * required changes and forcing a mode set.
12196 */
12197
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012198 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020012199fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012200 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012201}
12202
Daniel Vetterea9d7582012-07-10 10:42:52 +020012203static bool intel_crtc_in_use(struct drm_crtc *crtc)
12204{
12205 struct drm_encoder *encoder;
12206 struct drm_device *dev = crtc->dev;
12207
12208 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12209 if (encoder->crtc == crtc)
12210 return true;
12211
12212 return false;
12213}
12214
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012215static void
12216intel_modeset_update_state(struct drm_atomic_state *state)
12217{
12218 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012219 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012220 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012221 struct drm_crtc *crtc;
12222 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012223 struct drm_connector *connector;
12224
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012225 intel_shared_dpll_commit(dev_priv);
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012226 drm_atomic_helper_swap_state(state->dev, state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012227
Damien Lespiaub2784e12014-08-05 11:29:37 +010012228 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012229 if (!intel_encoder->base.crtc)
12230 continue;
12231
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012232 crtc = intel_encoder->base.crtc;
12233 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12234 if (!crtc_state || !needs_modeset(crtc->state))
12235 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012236
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012237 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012238 }
12239
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012240 drm_atomic_helper_update_legacy_modeset_state(state->dev, state);
12241 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012242
Ville Syrjälä76688512014-01-10 11:28:06 +020012243 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012244 for_each_crtc(dev, crtc) {
12245 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Maarten Lankhorst3cb480b2015-06-01 12:49:49 +020012246
12247 to_intel_crtc(crtc)->config = to_intel_crtc_state(crtc->state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012248 }
12249
12250 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12251 if (!connector->encoder || !connector->encoder->crtc)
12252 continue;
12253
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012254 crtc = connector->encoder->crtc;
12255 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12256 if (!crtc_state || !needs_modeset(crtc->state))
12257 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012258
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012259 if (crtc->state->active) {
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012260 struct drm_property *dpms_property =
12261 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012262
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012263 connector->dpms = DRM_MODE_DPMS_ON;
12264 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012265
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012266 intel_encoder = to_intel_encoder(connector->encoder);
12267 intel_encoder->connectors_active = true;
12268 } else
12269 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012270 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012271}
12272
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012273static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012274{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012275 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012276
12277 if (clock1 == clock2)
12278 return true;
12279
12280 if (!clock1 || !clock2)
12281 return false;
12282
12283 diff = abs(clock1 - clock2);
12284
12285 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12286 return true;
12287
12288 return false;
12289}
12290
Daniel Vetter25c5b262012-07-08 22:08:04 +020012291#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12292 list_for_each_entry((intel_crtc), \
12293 &(dev)->mode_config.crtc_list, \
12294 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012295 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012296
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012297static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012298intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012299 struct intel_crtc_state *current_config,
12300 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012301{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012302#define PIPE_CONF_CHECK_X(name) \
12303 if (current_config->name != pipe_config->name) { \
12304 DRM_ERROR("mismatch in " #name " " \
12305 "(expected 0x%08x, found 0x%08x)\n", \
12306 current_config->name, \
12307 pipe_config->name); \
12308 return false; \
12309 }
12310
Daniel Vetter08a24032013-04-19 11:25:34 +020012311#define PIPE_CONF_CHECK_I(name) \
12312 if (current_config->name != pipe_config->name) { \
12313 DRM_ERROR("mismatch in " #name " " \
12314 "(expected %i, found %i)\n", \
12315 current_config->name, \
12316 pipe_config->name); \
12317 return false; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012318 }
12319
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012320/* This is required for BDW+ where there is only one set of registers for
12321 * switching between high and low RR.
12322 * This macro can be used whenever a comparison has to be made between one
12323 * hw state and multiple sw state variables.
12324 */
12325#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12326 if ((current_config->name != pipe_config->name) && \
12327 (current_config->alt_name != pipe_config->name)) { \
12328 DRM_ERROR("mismatch in " #name " " \
12329 "(expected %i or %i, found %i)\n", \
12330 current_config->name, \
12331 current_config->alt_name, \
12332 pipe_config->name); \
12333 return false; \
12334 }
12335
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012336#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12337 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012338 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012339 "(expected %i, found %i)\n", \
12340 current_config->name & (mask), \
12341 pipe_config->name & (mask)); \
12342 return false; \
12343 }
12344
Ville Syrjälä5e550652013-09-06 23:29:07 +030012345#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12346 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12347 DRM_ERROR("mismatch in " #name " " \
12348 "(expected %i, found %i)\n", \
12349 current_config->name, \
12350 pipe_config->name); \
12351 return false; \
12352 }
12353
Daniel Vetterbb760062013-06-06 14:55:52 +020012354#define PIPE_CONF_QUIRK(quirk) \
12355 ((current_config->quirks | pipe_config->quirks) & (quirk))
12356
Daniel Vettereccb1402013-05-22 00:50:22 +020012357 PIPE_CONF_CHECK_I(cpu_transcoder);
12358
Daniel Vetter08a24032013-04-19 11:25:34 +020012359 PIPE_CONF_CHECK_I(has_pch_encoder);
12360 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012361 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12362 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12363 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12364 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12365 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012366
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012367 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012368
12369 if (INTEL_INFO(dev)->gen < 8) {
12370 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12371 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12372 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12373 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12374 PIPE_CONF_CHECK_I(dp_m_n.tu);
12375
12376 if (current_config->has_drrs) {
12377 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12378 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12379 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12380 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12381 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12382 }
12383 } else {
12384 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12385 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12386 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12387 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12388 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12389 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012390
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012391 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12392 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12393 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12394 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12395 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12396 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012397
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012398 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12399 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12400 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12401 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12402 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12403 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012404
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012405 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b2014-04-24 23:54:47 +020012406 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012407 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12408 IS_VALLEYVIEW(dev))
12409 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012410 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012411
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012412 PIPE_CONF_CHECK_I(has_audio);
12413
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012414 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012415 DRM_MODE_FLAG_INTERLACE);
12416
Daniel Vetterbb760062013-06-06 14:55:52 +020012417 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012418 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012419 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012420 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012421 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012422 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012423 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012424 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012425 DRM_MODE_FLAG_NVSYNC);
12426 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012427
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012428 PIPE_CONF_CHECK_I(pipe_src_w);
12429 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012430
Daniel Vetter99535992014-04-13 12:00:33 +020012431 /*
12432 * FIXME: BIOS likes to set up a cloned config with lvds+external
12433 * screen. Since we don't yet re-compute the pipe config when moving
12434 * just the lvds port away to another pipe the sw tracking won't match.
12435 *
12436 * Proper atomic modesets with recomputed global state will fix this.
12437 * Until then just don't check gmch state for inherited modes.
12438 */
12439 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12440 PIPE_CONF_CHECK_I(gmch_pfit.control);
12441 /* pfit ratios are autocomputed by the hw on gen4+ */
12442 if (INTEL_INFO(dev)->gen < 4)
12443 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12444 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12445 }
12446
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012447 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12448 if (current_config->pch_pfit.enabled) {
12449 PIPE_CONF_CHECK_I(pch_pfit.pos);
12450 PIPE_CONF_CHECK_I(pch_pfit.size);
12451 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012452
Chandra Kondurua1b22782015-04-07 15:28:45 -070012453 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12454
Jesse Barnese59150d2014-01-07 13:30:45 -080012455 /* BDW+ don't expose a synchronous way to read the state */
12456 if (IS_HASWELL(dev))
12457 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012458
Ville Syrjälä282740f2013-09-04 18:30:03 +030012459 PIPE_CONF_CHECK_I(double_wide);
12460
Daniel Vetter26804af2014-06-25 22:01:55 +030012461 PIPE_CONF_CHECK_X(ddi_pll_sel);
12462
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012463 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012464 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012465 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012466 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12467 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012468 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012469 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12470 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12471 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012472
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012473 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12474 PIPE_CONF_CHECK_I(pipe_bpp);
12475
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012476 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012477 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012478
Daniel Vetter66e985c2013-06-05 13:34:20 +020012479#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012480#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012481#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012482#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012483#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012484#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012485
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012486 return true;
12487}
12488
Damien Lespiau08db6652014-11-04 17:06:52 +000012489static void check_wm_state(struct drm_device *dev)
12490{
12491 struct drm_i915_private *dev_priv = dev->dev_private;
12492 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12493 struct intel_crtc *intel_crtc;
12494 int plane;
12495
12496 if (INTEL_INFO(dev)->gen < 9)
12497 return;
12498
12499 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12500 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12501
12502 for_each_intel_crtc(dev, intel_crtc) {
12503 struct skl_ddb_entry *hw_entry, *sw_entry;
12504 const enum pipe pipe = intel_crtc->pipe;
12505
12506 if (!intel_crtc->active)
12507 continue;
12508
12509 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012510 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012511 hw_entry = &hw_ddb.plane[pipe][plane];
12512 sw_entry = &sw_ddb->plane[pipe][plane];
12513
12514 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12515 continue;
12516
12517 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12518 "(expected (%u,%u), found (%u,%u))\n",
12519 pipe_name(pipe), plane + 1,
12520 sw_entry->start, sw_entry->end,
12521 hw_entry->start, hw_entry->end);
12522 }
12523
12524 /* cursor */
12525 hw_entry = &hw_ddb.cursor[pipe];
12526 sw_entry = &sw_ddb->cursor[pipe];
12527
12528 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12529 continue;
12530
12531 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12532 "(expected (%u,%u), found (%u,%u))\n",
12533 pipe_name(pipe),
12534 sw_entry->start, sw_entry->end,
12535 hw_entry->start, hw_entry->end);
12536 }
12537}
12538
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012539static void
12540check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012541{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012542 struct intel_connector *connector;
12543
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012544 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012545 /* This also checks the encoder/connector hw state with the
12546 * ->get_hw_state callbacks. */
12547 intel_connector_check_state(connector);
12548
Rob Clarke2c719b2014-12-15 13:56:32 -050012549 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012550 "connector's staged encoder doesn't match current encoder\n");
12551 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012552}
12553
12554static void
12555check_encoder_state(struct drm_device *dev)
12556{
12557 struct intel_encoder *encoder;
12558 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012559
Damien Lespiaub2784e12014-08-05 11:29:37 +010012560 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012561 bool enabled = false;
12562 bool active = false;
12563 enum pipe pipe, tracked_pipe;
12564
12565 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12566 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012567 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012568
Rob Clarke2c719b2014-12-15 13:56:32 -050012569 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012570 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012571 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012572 "encoder's active_connectors set, but no crtc\n");
12573
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012574 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012575 if (connector->base.encoder != &encoder->base)
12576 continue;
12577 enabled = true;
12578 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12579 active = true;
12580 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012581 /*
12582 * for MST connectors if we unplug the connector is gone
12583 * away but the encoder is still connected to a crtc
12584 * until a modeset happens in response to the hotplug.
12585 */
12586 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12587 continue;
12588
Rob Clarke2c719b2014-12-15 13:56:32 -050012589 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012590 "encoder's enabled state mismatch "
12591 "(expected %i, found %i)\n",
12592 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012593 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012594 "active encoder with no crtc\n");
12595
Rob Clarke2c719b2014-12-15 13:56:32 -050012596 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012597 "encoder's computed active state doesn't match tracked active state "
12598 "(expected %i, found %i)\n", active, encoder->connectors_active);
12599
12600 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012601 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012602 "encoder's hw state doesn't match sw tracking "
12603 "(expected %i, found %i)\n",
12604 encoder->connectors_active, active);
12605
12606 if (!encoder->base.crtc)
12607 continue;
12608
12609 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012610 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012611 "active encoder's pipe doesn't match"
12612 "(expected %i, found %i)\n",
12613 tracked_pipe, pipe);
12614
12615 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012616}
12617
12618static void
12619check_crtc_state(struct drm_device *dev)
12620{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012621 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012622 struct intel_crtc *crtc;
12623 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012624 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012625
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012626 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012627 bool enabled = false;
12628 bool active = false;
12629
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012630 memset(&pipe_config, 0, sizeof(pipe_config));
12631
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012632 DRM_DEBUG_KMS("[CRTC:%d]\n",
12633 crtc->base.base.id);
12634
Matt Roper83d65732015-02-25 13:12:16 -080012635 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012636 "active crtc, but not enabled in sw tracking\n");
12637
Damien Lespiaub2784e12014-08-05 11:29:37 +010012638 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012639 if (encoder->base.crtc != &crtc->base)
12640 continue;
12641 enabled = true;
12642 if (encoder->connectors_active)
12643 active = true;
12644 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012645
Rob Clarke2c719b2014-12-15 13:56:32 -050012646 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012647 "crtc's computed active state doesn't match tracked active state "
12648 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012649 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012650 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012651 "(expected %i, found %i)\n", enabled,
12652 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012653
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012654 active = dev_priv->display.get_pipe_config(crtc,
12655 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012656
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012657 /* hw state is inconsistent with the pipe quirk */
12658 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12659 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012660 active = crtc->active;
12661
Damien Lespiaub2784e12014-08-05 11:29:37 +010012662 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012663 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012664 if (encoder->base.crtc != &crtc->base)
12665 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012666 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012667 encoder->get_config(encoder, &pipe_config);
12668 }
12669
Rob Clarke2c719b2014-12-15 13:56:32 -050012670 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012671 "crtc active state doesn't match with hw state "
12672 "(expected %i, found %i)\n", crtc->active, active);
12673
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012674 I915_STATE_WARN(crtc->active != crtc->base.state->active,
12675 "transitional active state does not match atomic hw state "
12676 "(expected %i, found %i)\n", crtc->base.state->active, crtc->active);
12677
Daniel Vetterc0b03412013-05-28 12:05:54 +020012678 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012679 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012680 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012681 intel_dump_pipe_config(crtc, &pipe_config,
12682 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012683 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012684 "[sw state]");
12685 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012686 }
12687}
12688
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012689static void
12690check_shared_dpll_state(struct drm_device *dev)
12691{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012692 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012693 struct intel_crtc *crtc;
12694 struct intel_dpll_hw_state dpll_hw_state;
12695 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012696
12697 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12698 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12699 int enabled_crtcs = 0, active_crtcs = 0;
12700 bool active;
12701
12702 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12703
12704 DRM_DEBUG_KMS("%s\n", pll->name);
12705
12706 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12707
Rob Clarke2c719b2014-12-15 13:56:32 -050012708 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012709 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012710 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012711 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012712 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012713 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012714 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012715 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012716 "pll on state mismatch (expected %i, found %i)\n",
12717 pll->on, active);
12718
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012719 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012720 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012721 enabled_crtcs++;
12722 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12723 active_crtcs++;
12724 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012725 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012726 "pll active crtcs mismatch (expected %i, found %i)\n",
12727 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012728 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012729 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012730 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012731
Rob Clarke2c719b2014-12-15 13:56:32 -050012732 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012733 sizeof(dpll_hw_state)),
12734 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012735 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012736}
12737
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012738void
12739intel_modeset_check_state(struct drm_device *dev)
12740{
Damien Lespiau08db6652014-11-04 17:06:52 +000012741 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012742 check_connector_state(dev);
12743 check_encoder_state(dev);
12744 check_crtc_state(dev);
12745 check_shared_dpll_state(dev);
12746}
12747
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012748void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012749 int dotclock)
12750{
12751 /*
12752 * FDI already provided one idea for the dotclock.
12753 * Yell if the encoder disagrees.
12754 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012755 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012756 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012757 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012758}
12759
Ville Syrjälä80715b22014-05-15 20:23:23 +030012760static void update_scanline_offset(struct intel_crtc *crtc)
12761{
12762 struct drm_device *dev = crtc->base.dev;
12763
12764 /*
12765 * The scanline counter increments at the leading edge of hsync.
12766 *
12767 * On most platforms it starts counting from vtotal-1 on the
12768 * first active line. That means the scanline counter value is
12769 * always one less than what we would expect. Ie. just after
12770 * start of vblank, which also occurs at start of hsync (on the
12771 * last active line), the scanline counter will read vblank_start-1.
12772 *
12773 * On gen2 the scanline counter starts counting from 1 instead
12774 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12775 * to keep the value positive), instead of adding one.
12776 *
12777 * On HSW+ the behaviour of the scanline counter depends on the output
12778 * type. For DP ports it behaves like most other platforms, but on HDMI
12779 * there's an extra 1 line difference. So we need to add two instead of
12780 * one to the value.
12781 */
12782 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012783 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012784 int vtotal;
12785
12786 vtotal = mode->crtc_vtotal;
12787 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12788 vtotal /= 2;
12789
12790 crtc->scanline_offset = vtotal - 1;
12791 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012792 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012793 crtc->scanline_offset = 2;
12794 } else
12795 crtc->scanline_offset = 1;
12796}
12797
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012798static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012799intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012800 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012801{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012802 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012803 int ret = 0;
12804
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012805 ret = drm_atomic_helper_check_modeset(state->dev, state);
12806 if (ret)
12807 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012808
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012809 /*
12810 * Note this needs changes when we start tracking multiple modes
12811 * and crtcs. At that point we'll need to compute the whole config
12812 * (i.e. one pipe_config for each crtc) rather than just the one
12813 * for this crtc.
12814 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012815 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12816 if (IS_ERR(pipe_config))
12817 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012818
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012819 if (!pipe_config->base.enable &&
12820 WARN_ON(pipe_config->base.active))
12821 pipe_config->base.active = false;
12822
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012823 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012824 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012825
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012826 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012827 if (ret)
12828 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012829
Ander Conselvan de Oliveiracdba9542015-06-01 12:49:51 +020012830 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config, "[modeset]");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012831
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012832 ret = drm_atomic_helper_check_planes(state->dev, state);
12833 if (ret)
12834 return ERR_PTR(ret);
12835
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012836 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012837}
12838
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012839static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012840{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012841 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012842 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012843 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012844 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012845 struct intel_crtc_state *intel_crtc_state;
12846 struct drm_crtc *crtc;
12847 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012848 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012849 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012850
12851 if (!dev_priv->display.crtc_compute_clock)
12852 return 0;
12853
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012854 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12855 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012856 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012857
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012858 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012859 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012860 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012861 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012862 }
12863
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012864 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12865 if (ret)
12866 goto done;
12867
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012868 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12869 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012870 continue;
12871
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012872 intel_crtc = to_intel_crtc(crtc);
12873 intel_crtc_state = to_intel_crtc_state(crtc_state);
12874
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012875 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012876 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012877 if (ret) {
12878 intel_shared_dpll_abort_config(dev_priv);
12879 goto done;
12880 }
12881 }
12882
12883done:
12884 return ret;
12885}
12886
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012887/* Code that should eventually be part of atomic_check() */
12888static int __intel_set_mode_checks(struct drm_atomic_state *state)
12889{
12890 struct drm_device *dev = state->dev;
12891 int ret;
12892
12893 /*
12894 * See if the config requires any additional preparation, e.g.
12895 * to adjust global state with pipes off. We need to do this
12896 * here so we can get the modeset_pipe updated config for the new
12897 * mode set on this crtc. For other crtcs we need to use the
12898 * adjusted_mode bits in the crtc directly.
12899 */
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012900 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12901 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12902 ret = valleyview_modeset_global_pipes(state);
12903 else
12904 ret = broadwell_modeset_global_pipes(state);
12905
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012906 if (ret)
12907 return ret;
12908 }
12909
12910 ret = __intel_set_mode_setup_plls(state);
12911 if (ret)
12912 return ret;
12913
12914 return 0;
12915}
12916
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020012917static int __intel_set_mode(struct drm_atomic_state *state)
Daniel Vettera6778b32012-07-02 09:56:42 +020012918{
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020012919 struct drm_device *dev = state->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012920 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012921 struct drm_crtc *crtc;
12922 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012923 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012924 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012925
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012926 ret = __intel_set_mode_checks(state);
12927 if (ret < 0)
12928 return ret;
12929
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012930 ret = drm_atomic_helper_prepare_planes(dev, state);
12931 if (ret)
12932 return ret;
12933
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012934 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012935 if (!needs_modeset(crtc_state) || !crtc->state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012936 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012937
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012938 intel_crtc_disable_planes(crtc);
12939 dev_priv->display.crtc_disable(crtc);
12940 if (!crtc_state->enable)
12941 drm_plane_helper_disable(crtc->primary);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012942 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012943
Daniel Vetterea9d7582012-07-10 10:42:52 +020012944 /* Only after disabling all output pipelines that will be changed can we
12945 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012946 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012947
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012948 /* The state has been swaped above, so state actually contains the
12949 * old state now. */
12950
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012951 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020012952
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012953 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020012954
12955 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012956 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020012957 if (!needs_modeset(crtc->state) || !crtc->state->active)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012958 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012959
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012960 update_scanline_offset(to_intel_crtc(crtc));
12961
12962 dev_priv->display.crtc_enable(crtc);
12963 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030012964 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012965
Daniel Vettera6778b32012-07-02 09:56:42 +020012966 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012967
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012968 drm_atomic_helper_cleanup_planes(dev, state);
12969
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030012970 drm_atomic_state_free(state);
12971
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030012972 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020012973}
12974
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012975static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012976 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012977{
12978 int ret;
12979
Ander Conselvan de Oliveirac72d9692015-06-01 12:49:50 +020012980 ret = __intel_set_mode(pipe_config->base.state);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012981
12982 if (ret == 0)
12983 intel_modeset_check_state(crtc->dev);
12984
12985 return ret;
12986}
12987
Damien Lespiaue7457a92013-08-08 22:28:59 +010012988static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012989 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020012990{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012991 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012992 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020012993
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012994 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012995 if (IS_ERR(pipe_config)) {
12996 ret = PTR_ERR(pipe_config);
12997 goto out;
12998 }
Daniel Vetterf30da182013-04-11 20:22:50 +020012999
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013000 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013001 if (ret)
13002 goto out;
13003
13004out:
13005 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013006}
13007
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013008void intel_crtc_restore_mode(struct drm_crtc *crtc)
13009{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013010 struct drm_device *dev = crtc->dev;
13011 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013012 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013013 struct intel_encoder *encoder;
13014 struct intel_connector *connector;
13015 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013016 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013017 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013018
13019 state = drm_atomic_state_alloc(dev);
13020 if (!state) {
13021 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13022 crtc->base.id);
13023 return;
13024 }
13025
13026 state->acquire_ctx = dev->mode_config.acquire_ctx;
13027
13028 /* The force restore path in the HW readout code relies on the staged
13029 * config still keeping the user requested config while the actual
13030 * state has been overwritten by the configuration read from HW. We
13031 * need to copy the staged config to the atomic state, otherwise the
13032 * mode set will just reapply the state the HW is already in. */
13033 for_each_intel_encoder(dev, encoder) {
13034 if (&encoder->new_crtc->base != crtc)
13035 continue;
13036
13037 for_each_intel_connector(dev, connector) {
13038 if (connector->new_encoder != encoder)
13039 continue;
13040
13041 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13042 if (IS_ERR(connector_state)) {
13043 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13044 connector->base.base.id,
13045 connector->base.name,
13046 PTR_ERR(connector_state));
13047 continue;
13048 }
13049
13050 connector_state->crtc = crtc;
13051 connector_state->best_encoder = &encoder->base;
13052 }
13053 }
13054
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013055 for_each_intel_crtc(dev, intel_crtc) {
13056 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13057 continue;
13058
13059 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13060 if (IS_ERR(crtc_state)) {
13061 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13062 intel_crtc->base.base.id,
13063 PTR_ERR(crtc_state));
13064 continue;
13065 }
13066
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013067 crtc_state->base.active = crtc_state->base.enable =
13068 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013069
13070 if (&intel_crtc->base == crtc)
13071 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013072 }
13073
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013074 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13075 crtc->primary->fb, crtc->x, crtc->y);
13076
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013077 ret = intel_set_mode(crtc, state);
13078 if (ret)
13079 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013080}
13081
Daniel Vetter25c5b262012-07-08 22:08:04 +020013082#undef for_each_intel_crtc_masked
13083
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013084static bool intel_connector_in_mode_set(struct intel_connector *connector,
13085 struct drm_mode_set *set)
13086{
13087 int ro;
13088
13089 for (ro = 0; ro < set->num_connectors; ro++)
13090 if (set->connectors[ro] == &connector->base)
13091 return true;
13092
13093 return false;
13094}
13095
Daniel Vetter2e431052012-07-04 22:42:15 +020013096static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013097intel_modeset_stage_output_state(struct drm_device *dev,
13098 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013099 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013100{
Daniel Vetter9a935852012-07-05 22:34:27 +020013101 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013102 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013103 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013104 struct drm_crtc *crtc;
13105 struct drm_crtc_state *crtc_state;
13106 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013107
Damien Lespiau9abdda72013-02-13 13:29:23 +000013108 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013109 * of connectors. For paranoia, double-check this. */
13110 WARN_ON(!set->fb && (set->num_connectors != 0));
13111 WARN_ON(set->fb && (set->num_connectors == 0));
13112
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013113 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013114 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13115
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013116 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13117 continue;
13118
13119 connector_state =
13120 drm_atomic_get_connector_state(state, &connector->base);
13121 if (IS_ERR(connector_state))
13122 return PTR_ERR(connector_state);
13123
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013124 if (in_mode_set) {
13125 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013126 connector_state->best_encoder =
13127 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013128 }
13129
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013130 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013131 continue;
13132
Daniel Vetter9a935852012-07-05 22:34:27 +020013133 /* If we disable the crtc, disable all its connectors. Also, if
13134 * the connector is on the changing crtc but not on the new
13135 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013136 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013137 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013138
13139 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13140 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013141 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013142 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013143 }
13144 /* connector->new_encoder is now updated for all connectors. */
13145
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013146 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13147 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013148
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013149 if (!connector_state->best_encoder) {
13150 ret = drm_atomic_set_crtc_for_connector(connector_state,
13151 NULL);
13152 if (ret)
13153 return ret;
13154
Daniel Vetter50f56112012-07-02 09:35:43 +020013155 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013156 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013157
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013158 if (intel_connector_in_mode_set(connector, set)) {
13159 struct drm_crtc *crtc = connector->base.state->crtc;
13160
13161 /* If this connector was in a previous crtc, add it
13162 * to the state. We might need to disable it. */
13163 if (crtc) {
13164 crtc_state =
13165 drm_atomic_get_crtc_state(state, crtc);
13166 if (IS_ERR(crtc_state))
13167 return PTR_ERR(crtc_state);
13168 }
13169
13170 ret = drm_atomic_set_crtc_for_connector(connector_state,
13171 set->crtc);
13172 if (ret)
13173 return ret;
13174 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013175
13176 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013177 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13178 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013179 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013180 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013181
Daniel Vetter9a935852012-07-05 22:34:27 +020013182 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13183 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013184 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013185 connector_state->crtc->base.id);
13186
13187 if (connector_state->best_encoder != &connector->encoder->base)
13188 connector->encoder =
13189 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013190 }
13191
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013192 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013193 bool has_connectors;
13194
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013195 ret = drm_atomic_add_affected_connectors(state, crtc);
13196 if (ret)
13197 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013198
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013199 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13200 if (has_connectors != crtc_state->enable)
13201 crtc_state->enable =
13202 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013203 }
13204
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013205 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13206 set->fb, set->x, set->y);
13207 if (ret)
13208 return ret;
13209
13210 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13211 if (IS_ERR(crtc_state))
13212 return PTR_ERR(crtc_state);
13213
13214 if (set->mode)
13215 drm_mode_copy(&crtc_state->mode, set->mode);
13216
13217 if (set->num_connectors)
13218 crtc_state->active = true;
13219
Daniel Vetter2e431052012-07-04 22:42:15 +020013220 return 0;
13221}
13222
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013223static bool primary_plane_visible(struct drm_crtc *crtc)
13224{
13225 struct intel_plane_state *plane_state =
13226 to_intel_plane_state(crtc->primary->state);
13227
13228 return plane_state->visible;
13229}
13230
Daniel Vetter2e431052012-07-04 22:42:15 +020013231static int intel_crtc_set_config(struct drm_mode_set *set)
13232{
13233 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013234 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013235 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013236 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020013237 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013238
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013239 BUG_ON(!set);
13240 BUG_ON(!set->crtc);
13241 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013242
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013243 /* Enforce sane interface api - has been abused by the fb helper. */
13244 BUG_ON(!set->mode && set->fb);
13245 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013246
Daniel Vetter2e431052012-07-04 22:42:15 +020013247 if (set->fb) {
13248 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13249 set->crtc->base.id, set->fb->base.id,
13250 (int)set->num_connectors, set->x, set->y);
13251 } else {
13252 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013253 }
13254
13255 dev = set->crtc->dev;
13256
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013257 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013258 if (!state)
13259 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013260
13261 state->acquire_ctx = dev->mode_config.acquire_ctx;
13262
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013263 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013264 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013265 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013266
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013267 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080013268 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080013269 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013270 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080013271 }
Jesse Barnes50f52752014-11-07 13:11:00 -080013272
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013273 intel_update_pipe_size(to_intel_crtc(set->crtc));
13274
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013275 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013276
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013277 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013278
13279 if (ret == 0 &&
13280 pipe_config->base.enable &&
13281 pipe_config->base.planes_changed &&
13282 !needs_modeset(&pipe_config->base)) {
13283 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013284
13285 /*
13286 * We need to make sure the primary plane is re-enabled if it
13287 * has previously been turned off.
13288 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013289 if (ret == 0 && !primary_plane_was_visible &&
13290 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070013291 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030013292 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013293 }
13294
Jesse Barnes7ca51a32014-01-07 13:50:49 -080013295 /*
13296 * In the fastboot case this may be our only check of the
13297 * state after boot. It would be better to only do it on
13298 * the first update, but we don't have a nice way of doing that
13299 * (and really, set_config isn't used much for high freq page
13300 * flipping, so increasing its cost here shouldn't be a big
13301 * deal).
13302 */
Jani Nikulad330a952014-01-21 11:24:25 +020013303 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080013304 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020013305 }
13306
Chris Wilson2d05eae2013-05-03 17:36:25 +010013307 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013308 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13309 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013310 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013311
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013312out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013313 if (ret)
13314 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013315 return ret;
13316}
13317
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013318static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013319 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013320 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013321 .destroy = intel_crtc_destroy,
13322 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013323 .atomic_duplicate_state = intel_crtc_duplicate_state,
13324 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013325};
13326
Daniel Vetter53589012013-06-05 13:34:16 +020013327static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13328 struct intel_shared_dpll *pll,
13329 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013330{
Daniel Vetter53589012013-06-05 13:34:16 +020013331 uint32_t val;
13332
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013333 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013334 return false;
13335
Daniel Vetter53589012013-06-05 13:34:16 +020013336 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013337 hw_state->dpll = val;
13338 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13339 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013340
13341 return val & DPLL_VCO_ENABLE;
13342}
13343
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013344static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13345 struct intel_shared_dpll *pll)
13346{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013347 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13348 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013349}
13350
Daniel Vettere7b903d2013-06-05 13:34:14 +020013351static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13352 struct intel_shared_dpll *pll)
13353{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013354 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013355 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013356
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013357 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013358
13359 /* Wait for the clocks to stabilize. */
13360 POSTING_READ(PCH_DPLL(pll->id));
13361 udelay(150);
13362
13363 /* The pixel multiplier can only be updated once the
13364 * DPLL is enabled and the clocks are stable.
13365 *
13366 * So write it again.
13367 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013368 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013369 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013370 udelay(200);
13371}
13372
13373static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13374 struct intel_shared_dpll *pll)
13375{
13376 struct drm_device *dev = dev_priv->dev;
13377 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013378
13379 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013380 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013381 if (intel_crtc_to_shared_dpll(crtc) == pll)
13382 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13383 }
13384
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013385 I915_WRITE(PCH_DPLL(pll->id), 0);
13386 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013387 udelay(200);
13388}
13389
Daniel Vetter46edb022013-06-05 13:34:12 +020013390static char *ibx_pch_dpll_names[] = {
13391 "PCH DPLL A",
13392 "PCH DPLL B",
13393};
13394
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013395static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013396{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013397 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013398 int i;
13399
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013400 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013401
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013402 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013403 dev_priv->shared_dplls[i].id = i;
13404 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013405 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013406 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13407 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013408 dev_priv->shared_dplls[i].get_hw_state =
13409 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013410 }
13411}
13412
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013413static void intel_shared_dpll_init(struct drm_device *dev)
13414{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013415 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013416
Ville Syrjäläb6283052015-06-03 15:45:07 +030013417 intel_update_cdclk(dev);
13418
Daniel Vetter9cd86932014-06-25 22:01:57 +030013419 if (HAS_DDI(dev))
13420 intel_ddi_pll_init(dev);
13421 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013422 ibx_pch_dpll_init(dev);
13423 else
13424 dev_priv->num_shared_dpll = 0;
13425
13426 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013427}
13428
Matt Roper6beb8c232014-12-01 15:40:14 -080013429/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013430 * intel_wm_need_update - Check whether watermarks need updating
13431 * @plane: drm plane
13432 * @state: new plane state
13433 *
13434 * Check current plane state versus the new one to determine whether
13435 * watermarks need to be recalculated.
13436 *
13437 * Returns true or false.
13438 */
13439bool intel_wm_need_update(struct drm_plane *plane,
13440 struct drm_plane_state *state)
13441{
13442 /* Update watermarks on tiling changes. */
13443 if (!plane->state->fb || !state->fb ||
13444 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13445 plane->state->rotation != state->rotation)
13446 return true;
13447
13448 return false;
13449}
13450
13451/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013452 * intel_prepare_plane_fb - Prepare fb for usage on plane
13453 * @plane: drm plane to prepare for
13454 * @fb: framebuffer to prepare for presentation
13455 *
13456 * Prepares a framebuffer for usage on a display plane. Generally this
13457 * involves pinning the underlying object and updating the frontbuffer tracking
13458 * bits. Some older platforms need special physical address handling for
13459 * cursor planes.
13460 *
13461 * Returns 0 on success, negative error code on failure.
13462 */
13463int
13464intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013465 struct drm_framebuffer *fb,
13466 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013467{
13468 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013469 struct intel_plane *intel_plane = to_intel_plane(plane);
13470 enum pipe pipe = intel_plane->pipe;
13471 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13472 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13473 unsigned frontbuffer_bits = 0;
13474 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013475
Matt Roperea2c67b2014-12-23 10:41:52 -080013476 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013477 return 0;
13478
Matt Roper6beb8c232014-12-01 15:40:14 -080013479 switch (plane->type) {
13480 case DRM_PLANE_TYPE_PRIMARY:
13481 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13482 break;
13483 case DRM_PLANE_TYPE_CURSOR:
13484 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13485 break;
13486 case DRM_PLANE_TYPE_OVERLAY:
13487 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13488 break;
13489 }
Matt Roper465c1202014-05-29 08:06:54 -070013490
Matt Roper4c345742014-07-09 16:22:10 -070013491 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013492
Matt Roper6beb8c232014-12-01 15:40:14 -080013493 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13494 INTEL_INFO(dev)->cursor_needs_physical) {
13495 int align = IS_I830(dev) ? 16 * 1024 : 256;
13496 ret = i915_gem_object_attach_phys(obj, align);
13497 if (ret)
13498 DRM_DEBUG_KMS("failed to attach phys object\n");
13499 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013500 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013501 }
13502
13503 if (ret == 0)
13504 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13505
13506 mutex_unlock(&dev->struct_mutex);
13507
13508 return ret;
13509}
13510
Matt Roper38f3ce32014-12-02 07:45:25 -080013511/**
13512 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13513 * @plane: drm plane to clean up for
13514 * @fb: old framebuffer that was on plane
13515 *
13516 * Cleans up a framebuffer that has just been removed from a plane.
13517 */
13518void
13519intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013520 struct drm_framebuffer *fb,
13521 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013522{
13523 struct drm_device *dev = plane->dev;
13524 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13525
13526 if (WARN_ON(!obj))
13527 return;
13528
13529 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13530 !INTEL_INFO(dev)->cursor_needs_physical) {
13531 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013532 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013533 mutex_unlock(&dev->struct_mutex);
13534 }
Matt Roper465c1202014-05-29 08:06:54 -070013535}
13536
Chandra Konduru6156a452015-04-27 13:48:39 -070013537int
13538skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13539{
13540 int max_scale;
13541 struct drm_device *dev;
13542 struct drm_i915_private *dev_priv;
13543 int crtc_clock, cdclk;
13544
13545 if (!intel_crtc || !crtc_state)
13546 return DRM_PLANE_HELPER_NO_SCALING;
13547
13548 dev = intel_crtc->base.dev;
13549 dev_priv = dev->dev_private;
13550 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13551 cdclk = dev_priv->display.get_display_clock_speed(dev);
13552
13553 if (!crtc_clock || !cdclk)
13554 return DRM_PLANE_HELPER_NO_SCALING;
13555
13556 /*
13557 * skl max scale is lower of:
13558 * close to 3 but not 3, -1 is for that purpose
13559 * or
13560 * cdclk/crtc_clock
13561 */
13562 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13563
13564 return max_scale;
13565}
13566
Matt Roper465c1202014-05-29 08:06:54 -070013567static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013568intel_check_primary_plane(struct drm_plane *plane,
13569 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013570{
Matt Roper32b7eee2014-12-24 07:59:06 -080013571 struct drm_device *dev = plane->dev;
13572 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013573 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013574 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013575 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013576 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013577 struct drm_rect *dest = &state->dst;
13578 struct drm_rect *src = &state->src;
13579 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013580 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013581 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13582 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013583 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013584
Matt Roperea2c67b2014-12-23 10:41:52 -080013585 crtc = crtc ? crtc : plane->crtc;
13586 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013587 crtc_state = state->base.state ?
13588 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013589
Chandra Konduru6156a452015-04-27 13:48:39 -070013590 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013591 /* use scaler when colorkey is not required */
13592 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13593 min_scale = 1;
13594 max_scale = skl_max_scale(intel_crtc, crtc_state);
13595 }
Sonika Jindald8106362015-04-10 14:37:28 +053013596 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013597 }
Sonika Jindald8106362015-04-10 14:37:28 +053013598
Matt Roperc59cb172014-12-01 15:40:16 -080013599 ret = drm_plane_helper_check_update(plane, crtc, fb,
13600 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013601 min_scale,
13602 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013603 can_position, true,
13604 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013605 if (ret)
13606 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013607
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013608 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013609 struct intel_plane_state *old_state =
13610 to_intel_plane_state(plane->state);
13611
Matt Roper32b7eee2014-12-24 07:59:06 -080013612 intel_crtc->atomic.wait_for_flips = true;
13613
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013614 /*
13615 * FBC does not work on some platforms for rotated
13616 * planes, so disable it when rotation is not 0 and
13617 * update it when rotation is set back to 0.
13618 *
13619 * FIXME: This is redundant with the fbc update done in
13620 * the primary plane enable function except that that
13621 * one is done too late. We eventually need to unify
13622 * this.
13623 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013624 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013625 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013626 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013627 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013628 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013629 }
13630
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013631 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013632 /*
13633 * BDW signals flip done immediately if the plane
13634 * is disabled, even if the plane enable is already
13635 * armed to occur at the next vblank :(
13636 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013637 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013638 intel_crtc->atomic.wait_vblank = true;
13639 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013640
Matt Roper32b7eee2014-12-24 07:59:06 -080013641 intel_crtc->atomic.fb_bits |=
13642 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13643
13644 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013645
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013646 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013647 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013648 }
13649
Chandra Konduru6156a452015-04-27 13:48:39 -070013650 if (INTEL_INFO(dev)->gen >= 9) {
13651 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13652 to_intel_plane(plane), state, 0);
13653 if (ret)
13654 return ret;
13655 }
13656
Matt Roperc59cb172014-12-01 15:40:16 -080013657 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013658}
13659
Sonika Jindal48404c12014-08-22 14:06:04 +053013660static void
13661intel_commit_primary_plane(struct drm_plane *plane,
13662 struct intel_plane_state *state)
13663{
Matt Roper2b875c22014-12-01 15:40:13 -080013664 struct drm_crtc *crtc = state->base.crtc;
13665 struct drm_framebuffer *fb = state->base.fb;
13666 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013667 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013668 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013669 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013670
Matt Roperea2c67b2014-12-23 10:41:52 -080013671 crtc = crtc ? crtc : plane->crtc;
13672 intel_crtc = to_intel_crtc(crtc);
13673
Matt Ropercf4c7c12014-12-04 10:27:42 -080013674 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013675 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013676 crtc->y = src->y1 >> 16;
13677
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013678 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013679 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013680 /* FIXME: kill this fastboot hack */
13681 intel_update_pipe_size(intel_crtc);
13682
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013683 dev_priv->display.update_primary_plane(crtc, plane->fb,
13684 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013685 }
13686}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013687
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013688static void
13689intel_disable_primary_plane(struct drm_plane *plane,
13690 struct drm_crtc *crtc,
13691 bool force)
13692{
13693 struct drm_device *dev = plane->dev;
13694 struct drm_i915_private *dev_priv = dev->dev_private;
13695
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013696 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13697}
13698
Matt Roper32b7eee2014-12-24 07:59:06 -080013699static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13700{
13701 struct drm_device *dev = crtc->dev;
13702 struct drm_i915_private *dev_priv = dev->dev_private;
13703 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013704 struct intel_plane *intel_plane;
13705 struct drm_plane *p;
13706 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013707
Matt Roperea2c67b2014-12-23 10:41:52 -080013708 /* Track fb's for any planes being disabled */
13709 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13710 intel_plane = to_intel_plane(p);
13711
13712 if (intel_crtc->atomic.disabled_planes &
13713 (1 << drm_plane_index(p))) {
13714 switch (p->type) {
13715 case DRM_PLANE_TYPE_PRIMARY:
13716 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13717 break;
13718 case DRM_PLANE_TYPE_CURSOR:
13719 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13720 break;
13721 case DRM_PLANE_TYPE_OVERLAY:
13722 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13723 break;
13724 }
13725
13726 mutex_lock(&dev->struct_mutex);
13727 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13728 mutex_unlock(&dev->struct_mutex);
13729 }
13730 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013731
Matt Roper32b7eee2014-12-24 07:59:06 -080013732 if (intel_crtc->atomic.wait_for_flips)
13733 intel_crtc_wait_for_pending_flips(crtc);
13734
13735 if (intel_crtc->atomic.disable_fbc)
13736 intel_fbc_disable(dev);
13737
13738 if (intel_crtc->atomic.pre_disable_primary)
13739 intel_pre_disable_primary(crtc);
13740
13741 if (intel_crtc->atomic.update_wm)
13742 intel_update_watermarks(crtc);
13743
13744 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013745
13746 /* Perform vblank evasion around commit operation */
13747 if (intel_crtc->active)
13748 intel_crtc->atomic.evade =
13749 intel_pipe_update_start(intel_crtc,
13750 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013751}
13752
13753static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13754{
13755 struct drm_device *dev = crtc->dev;
13756 struct drm_i915_private *dev_priv = dev->dev_private;
13757 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13758 struct drm_plane *p;
13759
Matt Roperc34c9ee2014-12-23 10:41:50 -080013760 if (intel_crtc->atomic.evade)
13761 intel_pipe_update_end(intel_crtc,
13762 intel_crtc->atomic.start_vbl_count);
13763
Matt Roper32b7eee2014-12-24 07:59:06 -080013764 intel_runtime_pm_put(dev_priv);
13765
13766 if (intel_crtc->atomic.wait_vblank)
13767 intel_wait_for_vblank(dev, intel_crtc->pipe);
13768
13769 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13770
13771 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013772 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013773 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013774 mutex_unlock(&dev->struct_mutex);
13775 }
Matt Roper465c1202014-05-29 08:06:54 -070013776
Matt Roper32b7eee2014-12-24 07:59:06 -080013777 if (intel_crtc->atomic.post_enable_primary)
13778 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013779
Matt Roper32b7eee2014-12-24 07:59:06 -080013780 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13781 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13782 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13783 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013784
Matt Roper32b7eee2014-12-24 07:59:06 -080013785 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013786}
13787
Matt Ropercf4c7c12014-12-04 10:27:42 -080013788/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013789 * intel_plane_destroy - destroy a plane
13790 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013791 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013792 * Common destruction function for all types of planes (primary, cursor,
13793 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013794 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013795void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013796{
13797 struct intel_plane *intel_plane = to_intel_plane(plane);
13798 drm_plane_cleanup(plane);
13799 kfree(intel_plane);
13800}
13801
Matt Roper65a3fea2015-01-21 16:35:42 -080013802const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013803 .update_plane = drm_atomic_helper_update_plane,
13804 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013805 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013806 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013807 .atomic_get_property = intel_plane_atomic_get_property,
13808 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013809 .atomic_duplicate_state = intel_plane_duplicate_state,
13810 .atomic_destroy_state = intel_plane_destroy_state,
13811
Matt Roper465c1202014-05-29 08:06:54 -070013812};
13813
13814static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13815 int pipe)
13816{
13817 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013818 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013819 const uint32_t *intel_primary_formats;
13820 int num_formats;
13821
13822 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13823 if (primary == NULL)
13824 return NULL;
13825
Matt Roper8e7d6882015-01-21 16:35:41 -080013826 state = intel_create_plane_state(&primary->base);
13827 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013828 kfree(primary);
13829 return NULL;
13830 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013831 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013832
Matt Roper465c1202014-05-29 08:06:54 -070013833 primary->can_scale = false;
13834 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013835 if (INTEL_INFO(dev)->gen >= 9) {
13836 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013837 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013838 }
Matt Roper465c1202014-05-29 08:06:54 -070013839 primary->pipe = pipe;
13840 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013841 primary->check_plane = intel_check_primary_plane;
13842 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013843 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013844 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013845 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13846 primary->plane = !pipe;
13847
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013848 if (INTEL_INFO(dev)->gen >= 9) {
13849 intel_primary_formats = skl_primary_formats;
13850 num_formats = ARRAY_SIZE(skl_primary_formats);
13851 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013852 intel_primary_formats = i965_primary_formats;
13853 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013854 } else {
13855 intel_primary_formats = i8xx_primary_formats;
13856 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013857 }
13858
13859 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013860 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013861 intel_primary_formats, num_formats,
13862 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013863
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013864 if (INTEL_INFO(dev)->gen >= 4)
13865 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013866
Matt Roperea2c67b2014-12-23 10:41:52 -080013867 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13868
Matt Roper465c1202014-05-29 08:06:54 -070013869 return &primary->base;
13870}
13871
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013872void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13873{
13874 if (!dev->mode_config.rotation_property) {
13875 unsigned long flags = BIT(DRM_ROTATE_0) |
13876 BIT(DRM_ROTATE_180);
13877
13878 if (INTEL_INFO(dev)->gen >= 9)
13879 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13880
13881 dev->mode_config.rotation_property =
13882 drm_mode_create_rotation_property(dev, flags);
13883 }
13884 if (dev->mode_config.rotation_property)
13885 drm_object_attach_property(&plane->base.base,
13886 dev->mode_config.rotation_property,
13887 plane->base.state->rotation);
13888}
13889
Matt Roper3d7d6512014-06-10 08:28:13 -070013890static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013891intel_check_cursor_plane(struct drm_plane *plane,
13892 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013893{
Matt Roper2b875c22014-12-01 15:40:13 -080013894 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013895 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013896 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013897 struct drm_rect *dest = &state->dst;
13898 struct drm_rect *src = &state->src;
13899 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013900 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013901 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013902 unsigned stride;
13903 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013904
Matt Roperea2c67b2014-12-23 10:41:52 -080013905 crtc = crtc ? crtc : plane->crtc;
13906 intel_crtc = to_intel_crtc(crtc);
13907
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013908 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013909 src, dest, clip,
13910 DRM_PLANE_HELPER_NO_SCALING,
13911 DRM_PLANE_HELPER_NO_SCALING,
13912 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013913 if (ret)
13914 return ret;
13915
13916
13917 /* if we want to turn off the cursor ignore width and height */
13918 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013919 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013920
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013921 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013922 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13923 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13924 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013925 return -EINVAL;
13926 }
13927
Matt Roperea2c67b2014-12-23 10:41:52 -080013928 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13929 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013930 DRM_DEBUG_KMS("buffer is too small\n");
13931 return -ENOMEM;
13932 }
13933
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013934 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013935 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13936 ret = -EINVAL;
13937 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013938
Matt Roper32b7eee2014-12-24 07:59:06 -080013939finish:
13940 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020013941 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080013942 intel_crtc->atomic.update_wm = true;
13943
13944 intel_crtc->atomic.fb_bits |=
13945 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
13946 }
13947
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013948 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013949}
13950
Matt Roperf4a2cf22014-12-01 15:40:12 -080013951static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013952intel_disable_cursor_plane(struct drm_plane *plane,
13953 struct drm_crtc *crtc,
13954 bool force)
13955{
13956 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13957
13958 if (!force) {
13959 plane->fb = NULL;
13960 intel_crtc->cursor_bo = NULL;
13961 intel_crtc->cursor_addr = 0;
13962 }
13963
13964 intel_crtc_update_cursor(crtc, false);
13965}
13966
13967static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030013968intel_commit_cursor_plane(struct drm_plane *plane,
13969 struct intel_plane_state *state)
13970{
Matt Roper2b875c22014-12-01 15:40:13 -080013971 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013972 struct drm_device *dev = plane->dev;
13973 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080013974 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080013975 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070013976
Matt Roperea2c67b2014-12-23 10:41:52 -080013977 crtc = crtc ? crtc : plane->crtc;
13978 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070013979
Matt Roperea2c67b2014-12-23 10:41:52 -080013980 plane->fb = state->base.fb;
13981 crtc->cursor_x = state->base.crtc_x;
13982 crtc->cursor_y = state->base.crtc_y;
13983
Gustavo Padovana912f122014-12-01 15:40:10 -080013984 if (intel_crtc->cursor_bo == obj)
13985 goto update;
13986
Matt Roperf4a2cf22014-12-01 15:40:12 -080013987 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080013988 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080013989 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080013990 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080013991 else
Gustavo Padovana912f122014-12-01 15:40:10 -080013992 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080013993
Gustavo Padovana912f122014-12-01 15:40:10 -080013994 intel_crtc->cursor_addr = addr;
13995 intel_crtc->cursor_bo = obj;
13996update:
Gustavo Padovana912f122014-12-01 15:40:10 -080013997
Matt Roper32b7eee2014-12-24 07:59:06 -080013998 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030013999 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014000}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014001
Matt Roper3d7d6512014-06-10 08:28:13 -070014002static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14003 int pipe)
14004{
14005 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014006 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014007
14008 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14009 if (cursor == NULL)
14010 return NULL;
14011
Matt Roper8e7d6882015-01-21 16:35:41 -080014012 state = intel_create_plane_state(&cursor->base);
14013 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014014 kfree(cursor);
14015 return NULL;
14016 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014017 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014018
Matt Roper3d7d6512014-06-10 08:28:13 -070014019 cursor->can_scale = false;
14020 cursor->max_downscale = 1;
14021 cursor->pipe = pipe;
14022 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080014023 cursor->check_plane = intel_check_cursor_plane;
14024 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014025 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014026
14027 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014028 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014029 intel_cursor_formats,
14030 ARRAY_SIZE(intel_cursor_formats),
14031 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014032
14033 if (INTEL_INFO(dev)->gen >= 4) {
14034 if (!dev->mode_config.rotation_property)
14035 dev->mode_config.rotation_property =
14036 drm_mode_create_rotation_property(dev,
14037 BIT(DRM_ROTATE_0) |
14038 BIT(DRM_ROTATE_180));
14039 if (dev->mode_config.rotation_property)
14040 drm_object_attach_property(&cursor->base.base,
14041 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014042 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014043 }
14044
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014045 if (INTEL_INFO(dev)->gen >=9)
14046 state->scaler_id = -1;
14047
Matt Roperea2c67b2014-12-23 10:41:52 -080014048 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14049
Matt Roper3d7d6512014-06-10 08:28:13 -070014050 return &cursor->base;
14051}
14052
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014053static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14054 struct intel_crtc_state *crtc_state)
14055{
14056 int i;
14057 struct intel_scaler *intel_scaler;
14058 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14059
14060 for (i = 0; i < intel_crtc->num_scalers; i++) {
14061 intel_scaler = &scaler_state->scalers[i];
14062 intel_scaler->in_use = 0;
14063 intel_scaler->id = i;
14064
14065 intel_scaler->mode = PS_SCALER_MODE_DYN;
14066 }
14067
14068 scaler_state->scaler_id = -1;
14069}
14070
Hannes Ederb358d0a2008-12-18 21:18:47 +010014071static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014072{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014073 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014074 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014075 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014076 struct drm_plane *primary = NULL;
14077 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014078 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014079
Daniel Vetter955382f2013-09-19 14:05:45 +020014080 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014081 if (intel_crtc == NULL)
14082 return;
14083
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014084 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14085 if (!crtc_state)
14086 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014087 intel_crtc->config = crtc_state;
14088 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014089 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014090
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014091 /* initialize shared scalers */
14092 if (INTEL_INFO(dev)->gen >= 9) {
14093 if (pipe == PIPE_C)
14094 intel_crtc->num_scalers = 1;
14095 else
14096 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14097
14098 skl_init_scalers(dev, intel_crtc, crtc_state);
14099 }
14100
Matt Roper465c1202014-05-29 08:06:54 -070014101 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014102 if (!primary)
14103 goto fail;
14104
14105 cursor = intel_cursor_plane_create(dev, pipe);
14106 if (!cursor)
14107 goto fail;
14108
Matt Roper465c1202014-05-29 08:06:54 -070014109 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014110 cursor, &intel_crtc_funcs);
14111 if (ret)
14112 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014113
14114 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014115 for (i = 0; i < 256; i++) {
14116 intel_crtc->lut_r[i] = i;
14117 intel_crtc->lut_g[i] = i;
14118 intel_crtc->lut_b[i] = i;
14119 }
14120
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014121 /*
14122 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014123 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014124 */
Jesse Barnes80824002009-09-10 15:28:06 -070014125 intel_crtc->pipe = pipe;
14126 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014127 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014128 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014129 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014130 }
14131
Chris Wilson4b0e3332014-05-30 16:35:26 +030014132 intel_crtc->cursor_base = ~0;
14133 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014134 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014135
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014136 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14137 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14138 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14139 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14140
Jesse Barnes79e53942008-11-07 14:24:08 -080014141 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014142
14143 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014144 return;
14145
14146fail:
14147 if (primary)
14148 drm_plane_cleanup(primary);
14149 if (cursor)
14150 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014151 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014152 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014153}
14154
Jesse Barnes752aa882013-10-31 18:55:49 +020014155enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14156{
14157 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014158 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014159
Rob Clark51fd3712013-11-19 12:10:12 -050014160 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014161
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014162 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014163 return INVALID_PIPE;
14164
14165 return to_intel_crtc(encoder->crtc)->pipe;
14166}
14167
Carl Worth08d7b3d2009-04-29 14:43:54 -070014168int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014169 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014170{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014171 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014172 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014173 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014174
Rob Clark7707e652014-07-17 23:30:04 -040014175 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014176
Rob Clark7707e652014-07-17 23:30:04 -040014177 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014178 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014179 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014180 }
14181
Rob Clark7707e652014-07-17 23:30:04 -040014182 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014183 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014184
Daniel Vetterc05422d2009-08-11 16:05:30 +020014185 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014186}
14187
Daniel Vetter66a92782012-07-12 20:08:18 +020014188static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014189{
Daniel Vetter66a92782012-07-12 20:08:18 +020014190 struct drm_device *dev = encoder->base.dev;
14191 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014192 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014193 int entry = 0;
14194
Damien Lespiaub2784e12014-08-05 11:29:37 +010014195 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014196 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014197 index_mask |= (1 << entry);
14198
Jesse Barnes79e53942008-11-07 14:24:08 -080014199 entry++;
14200 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014201
Jesse Barnes79e53942008-11-07 14:24:08 -080014202 return index_mask;
14203}
14204
Chris Wilson4d302442010-12-14 19:21:29 +000014205static bool has_edp_a(struct drm_device *dev)
14206{
14207 struct drm_i915_private *dev_priv = dev->dev_private;
14208
14209 if (!IS_MOBILE(dev))
14210 return false;
14211
14212 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14213 return false;
14214
Damien Lespiaue3589902014-02-07 19:12:50 +000014215 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014216 return false;
14217
14218 return true;
14219}
14220
Jesse Barnes84b4e042014-06-25 08:24:29 -070014221static bool intel_crt_present(struct drm_device *dev)
14222{
14223 struct drm_i915_private *dev_priv = dev->dev_private;
14224
Damien Lespiau884497e2013-12-03 13:56:23 +000014225 if (INTEL_INFO(dev)->gen >= 9)
14226 return false;
14227
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014228 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014229 return false;
14230
14231 if (IS_CHERRYVIEW(dev))
14232 return false;
14233
14234 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14235 return false;
14236
14237 return true;
14238}
14239
Jesse Barnes79e53942008-11-07 14:24:08 -080014240static void intel_setup_outputs(struct drm_device *dev)
14241{
Eric Anholt725e30a2009-01-22 13:01:02 -080014242 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014243 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014244 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014245
Daniel Vetterc9093352013-06-06 22:22:47 +020014246 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014247
Jesse Barnes84b4e042014-06-25 08:24:29 -070014248 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014249 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014250
Vandana Kannanc776eb22014-08-19 12:05:01 +053014251 if (IS_BROXTON(dev)) {
14252 /*
14253 * FIXME: Broxton doesn't support port detection via the
14254 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14255 * detect the ports.
14256 */
14257 intel_ddi_init(dev, PORT_A);
14258 intel_ddi_init(dev, PORT_B);
14259 intel_ddi_init(dev, PORT_C);
14260 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014261 int found;
14262
Jesse Barnesde31fac2015-03-06 15:53:32 -080014263 /*
14264 * Haswell uses DDI functions to detect digital outputs.
14265 * On SKL pre-D0 the strap isn't connected, so we assume
14266 * it's there.
14267 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014268 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014269 /* WaIgnoreDDIAStrap: skl */
14270 if (found ||
14271 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014272 intel_ddi_init(dev, PORT_A);
14273
14274 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14275 * register */
14276 found = I915_READ(SFUSE_STRAP);
14277
14278 if (found & SFUSE_STRAP_DDIB_DETECTED)
14279 intel_ddi_init(dev, PORT_B);
14280 if (found & SFUSE_STRAP_DDIC_DETECTED)
14281 intel_ddi_init(dev, PORT_C);
14282 if (found & SFUSE_STRAP_DDID_DETECTED)
14283 intel_ddi_init(dev, PORT_D);
14284 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014285 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014286 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014287
14288 if (has_edp_a(dev))
14289 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014290
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014291 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014292 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014293 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014294 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014295 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014296 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014297 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014298 }
14299
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014300 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014301 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014302
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014303 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014304 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014305
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014306 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014307 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014308
Daniel Vetter270b3042012-10-27 15:52:05 +020014309 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014310 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014311 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014312 /*
14313 * The DP_DETECTED bit is the latched state of the DDC
14314 * SDA pin at boot. However since eDP doesn't require DDC
14315 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14316 * eDP ports may have been muxed to an alternate function.
14317 * Thus we can't rely on the DP_DETECTED bit alone to detect
14318 * eDP ports. Consult the VBT as well as DP_DETECTED to
14319 * detect eDP ports.
14320 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014321 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14322 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014323 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14324 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014325 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14326 intel_dp_is_edp(dev, PORT_B))
14327 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014328
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014329 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14330 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014331 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14332 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014333 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14334 intel_dp_is_edp(dev, PORT_C))
14335 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014336
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014337 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014338 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014339 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14340 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014341 /* eDP not supported on port D, so don't check VBT */
14342 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14343 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014344 }
14345
Jani Nikula3cfca972013-08-27 15:12:26 +030014346 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014347 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014348 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014349
Paulo Zanonie2debe92013-02-18 19:00:27 -030014350 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014351 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014352 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014353 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14354 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014355 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014356 }
Ma Ling27185ae2009-08-24 13:50:23 +080014357
Imre Deake7281ea2013-05-08 13:14:08 +030014358 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014359 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014360 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014361
14362 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014363
Paulo Zanonie2debe92013-02-18 19:00:27 -030014364 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014365 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014366 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014367 }
Ma Ling27185ae2009-08-24 13:50:23 +080014368
Paulo Zanonie2debe92013-02-18 19:00:27 -030014369 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014370
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014371 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14372 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014373 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014374 }
Imre Deake7281ea2013-05-08 13:14:08 +030014375 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014376 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014377 }
Ma Ling27185ae2009-08-24 13:50:23 +080014378
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014379 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014380 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014381 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014382 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014383 intel_dvo_init(dev);
14384
Zhenyu Wang103a1962009-11-27 11:44:36 +080014385 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014386 intel_tv_init(dev);
14387
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014388 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014389
Damien Lespiaub2784e12014-08-05 11:29:37 +010014390 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014391 encoder->base.possible_crtcs = encoder->crtc_mask;
14392 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014393 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014394 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014395
Paulo Zanonidde86e22012-12-01 12:04:25 -020014396 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014397
14398 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014399}
14400
14401static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14402{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014403 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014404 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014405
Daniel Vetteref2d6332014-02-10 18:00:38 +010014406 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014407 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014408 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014409 drm_gem_object_unreference(&intel_fb->obj->base);
14410 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014411 kfree(intel_fb);
14412}
14413
14414static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014415 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014416 unsigned int *handle)
14417{
14418 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014419 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014420
Chris Wilson05394f32010-11-08 19:18:58 +000014421 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014422}
14423
14424static const struct drm_framebuffer_funcs intel_fb_funcs = {
14425 .destroy = intel_user_framebuffer_destroy,
14426 .create_handle = intel_user_framebuffer_create_handle,
14427};
14428
Damien Lespiaub3218032015-02-27 11:15:18 +000014429static
14430u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14431 uint32_t pixel_format)
14432{
14433 u32 gen = INTEL_INFO(dev)->gen;
14434
14435 if (gen >= 9) {
14436 /* "The stride in bytes must not exceed the of the size of 8K
14437 * pixels and 32K bytes."
14438 */
14439 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14440 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14441 return 32*1024;
14442 } else if (gen >= 4) {
14443 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14444 return 16*1024;
14445 else
14446 return 32*1024;
14447 } else if (gen >= 3) {
14448 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14449 return 8*1024;
14450 else
14451 return 16*1024;
14452 } else {
14453 /* XXX DSPC is limited to 4k tiled */
14454 return 8*1024;
14455 }
14456}
14457
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014458static int intel_framebuffer_init(struct drm_device *dev,
14459 struct intel_framebuffer *intel_fb,
14460 struct drm_mode_fb_cmd2 *mode_cmd,
14461 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014462{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014463 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014464 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014465 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014466
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014467 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14468
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014469 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14470 /* Enforce that fb modifier and tiling mode match, but only for
14471 * X-tiled. This is needed for FBC. */
14472 if (!!(obj->tiling_mode == I915_TILING_X) !=
14473 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14474 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14475 return -EINVAL;
14476 }
14477 } else {
14478 if (obj->tiling_mode == I915_TILING_X)
14479 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14480 else if (obj->tiling_mode == I915_TILING_Y) {
14481 DRM_DEBUG("No Y tiling for legacy addfb\n");
14482 return -EINVAL;
14483 }
14484 }
14485
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014486 /* Passed in modifier sanity checking. */
14487 switch (mode_cmd->modifier[0]) {
14488 case I915_FORMAT_MOD_Y_TILED:
14489 case I915_FORMAT_MOD_Yf_TILED:
14490 if (INTEL_INFO(dev)->gen < 9) {
14491 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14492 mode_cmd->modifier[0]);
14493 return -EINVAL;
14494 }
14495 case DRM_FORMAT_MOD_NONE:
14496 case I915_FORMAT_MOD_X_TILED:
14497 break;
14498 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014499 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14500 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014501 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014502 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014503
Damien Lespiaub3218032015-02-27 11:15:18 +000014504 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14505 mode_cmd->pixel_format);
14506 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14507 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14508 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014509 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014510 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014511
Damien Lespiaub3218032015-02-27 11:15:18 +000014512 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14513 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014514 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014515 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14516 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014517 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014518 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014519 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014520 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014521
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014522 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014523 mode_cmd->pitches[0] != obj->stride) {
14524 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14525 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014526 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014527 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014528
Ville Syrjälä57779d02012-10-31 17:50:14 +020014529 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014530 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014531 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014532 case DRM_FORMAT_RGB565:
14533 case DRM_FORMAT_XRGB8888:
14534 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014535 break;
14536 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014537 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014538 DRM_DEBUG("unsupported pixel format: %s\n",
14539 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014540 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014541 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014542 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014543 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014544 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14545 DRM_DEBUG("unsupported pixel format: %s\n",
14546 drm_get_format_name(mode_cmd->pixel_format));
14547 return -EINVAL;
14548 }
14549 break;
14550 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014551 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014552 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014553 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014554 DRM_DEBUG("unsupported pixel format: %s\n",
14555 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014556 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014557 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014558 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014559 case DRM_FORMAT_ABGR2101010:
14560 if (!IS_VALLEYVIEW(dev)) {
14561 DRM_DEBUG("unsupported pixel format: %s\n",
14562 drm_get_format_name(mode_cmd->pixel_format));
14563 return -EINVAL;
14564 }
14565 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014566 case DRM_FORMAT_YUYV:
14567 case DRM_FORMAT_UYVY:
14568 case DRM_FORMAT_YVYU:
14569 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014570 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014571 DRM_DEBUG("unsupported pixel format: %s\n",
14572 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014573 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014574 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014575 break;
14576 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014577 DRM_DEBUG("unsupported pixel format: %s\n",
14578 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014579 return -EINVAL;
14580 }
14581
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014582 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14583 if (mode_cmd->offsets[0] != 0)
14584 return -EINVAL;
14585
Damien Lespiauec2c9812015-01-20 12:51:45 +000014586 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014587 mode_cmd->pixel_format,
14588 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014589 /* FIXME drm helper for size checks (especially planar formats)? */
14590 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14591 return -EINVAL;
14592
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014593 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14594 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014595 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014596
Jesse Barnes79e53942008-11-07 14:24:08 -080014597 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14598 if (ret) {
14599 DRM_ERROR("framebuffer init failed %d\n", ret);
14600 return ret;
14601 }
14602
Jesse Barnes79e53942008-11-07 14:24:08 -080014603 return 0;
14604}
14605
Jesse Barnes79e53942008-11-07 14:24:08 -080014606static struct drm_framebuffer *
14607intel_user_framebuffer_create(struct drm_device *dev,
14608 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014609 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014610{
Chris Wilson05394f32010-11-08 19:18:58 +000014611 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014612
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014613 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14614 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014615 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014616 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014617
Chris Wilsond2dff872011-04-19 08:36:26 +010014618 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014619}
14620
Daniel Vetter4520f532013-10-09 09:18:51 +020014621#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014622static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014623{
14624}
14625#endif
14626
Jesse Barnes79e53942008-11-07 14:24:08 -080014627static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014628 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014629 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014630 .atomic_check = intel_atomic_check,
14631 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014632};
14633
Jesse Barnese70236a2009-09-21 10:42:27 -070014634/* Set up chip specific display functions */
14635static void intel_init_display(struct drm_device *dev)
14636{
14637 struct drm_i915_private *dev_priv = dev->dev_private;
14638
Daniel Vetteree9300b2013-06-03 22:40:22 +020014639 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14640 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014641 else if (IS_CHERRYVIEW(dev))
14642 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014643 else if (IS_VALLEYVIEW(dev))
14644 dev_priv->display.find_dpll = vlv_find_best_dpll;
14645 else if (IS_PINEVIEW(dev))
14646 dev_priv->display.find_dpll = pnv_find_best_dpll;
14647 else
14648 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14649
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014650 if (INTEL_INFO(dev)->gen >= 9) {
14651 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014652 dev_priv->display.get_initial_plane_config =
14653 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014654 dev_priv->display.crtc_compute_clock =
14655 haswell_crtc_compute_clock;
14656 dev_priv->display.crtc_enable = haswell_crtc_enable;
14657 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014658 dev_priv->display.update_primary_plane =
14659 skylake_update_primary_plane;
14660 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014661 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014662 dev_priv->display.get_initial_plane_config =
14663 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014664 dev_priv->display.crtc_compute_clock =
14665 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014666 dev_priv->display.crtc_enable = haswell_crtc_enable;
14667 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014668 dev_priv->display.update_primary_plane =
14669 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014670 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014671 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014672 dev_priv->display.get_initial_plane_config =
14673 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014674 dev_priv->display.crtc_compute_clock =
14675 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014676 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14677 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014678 dev_priv->display.update_primary_plane =
14679 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014680 } else if (IS_VALLEYVIEW(dev)) {
14681 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014682 dev_priv->display.get_initial_plane_config =
14683 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014684 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014685 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14686 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014687 dev_priv->display.update_primary_plane =
14688 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014689 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014690 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014691 dev_priv->display.get_initial_plane_config =
14692 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014693 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014694 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14695 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014696 dev_priv->display.update_primary_plane =
14697 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014698 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014699
Jesse Barnese70236a2009-09-21 10:42:27 -070014700 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014701 if (IS_SKYLAKE(dev))
14702 dev_priv->display.get_display_clock_speed =
14703 skylake_get_display_clock_speed;
14704 else if (IS_BROADWELL(dev))
14705 dev_priv->display.get_display_clock_speed =
14706 broadwell_get_display_clock_speed;
14707 else if (IS_HASWELL(dev))
14708 dev_priv->display.get_display_clock_speed =
14709 haswell_get_display_clock_speed;
14710 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014711 dev_priv->display.get_display_clock_speed =
14712 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014713 else if (IS_GEN5(dev))
14714 dev_priv->display.get_display_clock_speed =
14715 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014716 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014717 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014718 dev_priv->display.get_display_clock_speed =
14719 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014720 else if (IS_GM45(dev))
14721 dev_priv->display.get_display_clock_speed =
14722 gm45_get_display_clock_speed;
14723 else if (IS_CRESTLINE(dev))
14724 dev_priv->display.get_display_clock_speed =
14725 i965gm_get_display_clock_speed;
14726 else if (IS_PINEVIEW(dev))
14727 dev_priv->display.get_display_clock_speed =
14728 pnv_get_display_clock_speed;
14729 else if (IS_G33(dev) || IS_G4X(dev))
14730 dev_priv->display.get_display_clock_speed =
14731 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014732 else if (IS_I915G(dev))
14733 dev_priv->display.get_display_clock_speed =
14734 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014735 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014736 dev_priv->display.get_display_clock_speed =
14737 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014738 else if (IS_PINEVIEW(dev))
14739 dev_priv->display.get_display_clock_speed =
14740 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014741 else if (IS_I915GM(dev))
14742 dev_priv->display.get_display_clock_speed =
14743 i915gm_get_display_clock_speed;
14744 else if (IS_I865G(dev))
14745 dev_priv->display.get_display_clock_speed =
14746 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014747 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014748 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014749 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014750 else { /* 830 */
14751 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014752 dev_priv->display.get_display_clock_speed =
14753 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014754 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014755
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014756 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014757 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014758 } else if (IS_GEN6(dev)) {
14759 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014760 } else if (IS_IVYBRIDGE(dev)) {
14761 /* FIXME: detect B0+ stepping and use auto training */
14762 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014763 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014764 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030014765 if (IS_BROADWELL(dev))
14766 dev_priv->display.modeset_global_resources =
14767 broadwell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014768 } else if (IS_VALLEYVIEW(dev)) {
14769 dev_priv->display.modeset_global_resources =
14770 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014771 } else if (IS_BROXTON(dev)) {
14772 dev_priv->display.modeset_global_resources =
14773 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014774 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014775
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014776 switch (INTEL_INFO(dev)->gen) {
14777 case 2:
14778 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14779 break;
14780
14781 case 3:
14782 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14783 break;
14784
14785 case 4:
14786 case 5:
14787 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14788 break;
14789
14790 case 6:
14791 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14792 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014793 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014794 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014795 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14796 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014797 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014798 /* Drop through - unsupported since execlist only. */
14799 default:
14800 /* Default just returns -ENODEV to indicate unsupported */
14801 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014802 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014803
14804 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014805
14806 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014807}
14808
Jesse Barnesb690e962010-07-19 13:53:12 -070014809/*
14810 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14811 * resume, or other times. This quirk makes sure that's the case for
14812 * affected systems.
14813 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014814static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014815{
14816 struct drm_i915_private *dev_priv = dev->dev_private;
14817
14818 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014819 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014820}
14821
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014822static void quirk_pipeb_force(struct drm_device *dev)
14823{
14824 struct drm_i915_private *dev_priv = dev->dev_private;
14825
14826 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14827 DRM_INFO("applying pipe b force quirk\n");
14828}
14829
Keith Packard435793d2011-07-12 14:56:22 -070014830/*
14831 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14832 */
14833static void quirk_ssc_force_disable(struct drm_device *dev)
14834{
14835 struct drm_i915_private *dev_priv = dev->dev_private;
14836 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014837 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014838}
14839
Carsten Emde4dca20e2012-03-15 15:56:26 +010014840/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014841 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14842 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014843 */
14844static void quirk_invert_brightness(struct drm_device *dev)
14845{
14846 struct drm_i915_private *dev_priv = dev->dev_private;
14847 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014848 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014849}
14850
Scot Doyle9c72cc62014-07-03 23:27:50 +000014851/* Some VBT's incorrectly indicate no backlight is present */
14852static void quirk_backlight_present(struct drm_device *dev)
14853{
14854 struct drm_i915_private *dev_priv = dev->dev_private;
14855 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14856 DRM_INFO("applying backlight present quirk\n");
14857}
14858
Jesse Barnesb690e962010-07-19 13:53:12 -070014859struct intel_quirk {
14860 int device;
14861 int subsystem_vendor;
14862 int subsystem_device;
14863 void (*hook)(struct drm_device *dev);
14864};
14865
Egbert Eich5f85f172012-10-14 15:46:38 +020014866/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14867struct intel_dmi_quirk {
14868 void (*hook)(struct drm_device *dev);
14869 const struct dmi_system_id (*dmi_id_list)[];
14870};
14871
14872static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14873{
14874 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14875 return 1;
14876}
14877
14878static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14879 {
14880 .dmi_id_list = &(const struct dmi_system_id[]) {
14881 {
14882 .callback = intel_dmi_reverse_brightness,
14883 .ident = "NCR Corporation",
14884 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14885 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14886 },
14887 },
14888 { } /* terminating entry */
14889 },
14890 .hook = quirk_invert_brightness,
14891 },
14892};
14893
Ben Widawskyc43b5632012-04-16 14:07:40 -070014894static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014895 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14896 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14897
Jesse Barnesb690e962010-07-19 13:53:12 -070014898 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14899 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14900
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014901 /* 830 needs to leave pipe A & dpll A up */
14902 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14903
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014904 /* 830 needs to leave pipe B & dpll B up */
14905 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14906
Keith Packard435793d2011-07-12 14:56:22 -070014907 /* Lenovo U160 cannot use SSC on LVDS */
14908 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014909
14910 /* Sony Vaio Y cannot use SSC on LVDS */
14911 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014912
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014913 /* Acer Aspire 5734Z must invert backlight brightness */
14914 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14915
14916 /* Acer/eMachines G725 */
14917 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14918
14919 /* Acer/eMachines e725 */
14920 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14921
14922 /* Acer/Packard Bell NCL20 */
14923 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14924
14925 /* Acer Aspire 4736Z */
14926 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014927
14928 /* Acer Aspire 5336 */
14929 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014930
14931 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14932 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014933
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014934 /* Acer C720 Chromebook (Core i3 4005U) */
14935 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14936
jens steinb2a96012014-10-28 20:25:53 +010014937 /* Apple Macbook 2,1 (Core 2 T7400) */
14938 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
14939
Scot Doyled4967d82014-07-03 23:27:52 +000014940 /* Toshiba CB35 Chromebook (Celeron 2955U) */
14941 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000014942
14943 /* HP Chromebook 14 (Celeron 2955U) */
14944 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020014945
14946 /* Dell Chromebook 11 */
14947 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070014948};
14949
14950static void intel_init_quirks(struct drm_device *dev)
14951{
14952 struct pci_dev *d = dev->pdev;
14953 int i;
14954
14955 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
14956 struct intel_quirk *q = &intel_quirks[i];
14957
14958 if (d->device == q->device &&
14959 (d->subsystem_vendor == q->subsystem_vendor ||
14960 q->subsystem_vendor == PCI_ANY_ID) &&
14961 (d->subsystem_device == q->subsystem_device ||
14962 q->subsystem_device == PCI_ANY_ID))
14963 q->hook(dev);
14964 }
Egbert Eich5f85f172012-10-14 15:46:38 +020014965 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
14966 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
14967 intel_dmi_quirks[i].hook(dev);
14968 }
Jesse Barnesb690e962010-07-19 13:53:12 -070014969}
14970
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014971/* Disable the VGA plane that we never use */
14972static void i915_disable_vga(struct drm_device *dev)
14973{
14974 struct drm_i915_private *dev_priv = dev->dev_private;
14975 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020014976 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014977
Ville Syrjälä2b37c612014-01-22 21:32:38 +020014978 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014979 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070014980 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014981 sr1 = inb(VGA_SR_DATA);
14982 outb(sr1 | 1<<5, VGA_SR_DATA);
14983 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
14984 udelay(300);
14985
Ville Syrjälä01f5a622014-12-16 18:38:37 +020014986 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070014987 POSTING_READ(vga_reg);
14988}
14989
Daniel Vetterf8175862012-04-10 15:50:11 +020014990void intel_modeset_init_hw(struct drm_device *dev)
14991{
Ville Syrjäläb6283052015-06-03 15:45:07 +030014992 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030014993 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014994 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020014995 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020014996}
14997
Jesse Barnes79e53942008-11-07 14:24:08 -080014998void intel_modeset_init(struct drm_device *dev)
14999{
Jesse Barnes652c3932009-08-17 13:31:43 -070015000 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015001 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015002 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015003 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015004
15005 drm_mode_config_init(dev);
15006
15007 dev->mode_config.min_width = 0;
15008 dev->mode_config.min_height = 0;
15009
Dave Airlie019d96c2011-09-29 16:20:42 +010015010 dev->mode_config.preferred_depth = 24;
15011 dev->mode_config.prefer_shadow = 1;
15012
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015013 dev->mode_config.allow_fb_modifiers = true;
15014
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015015 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015016
Jesse Barnesb690e962010-07-19 13:53:12 -070015017 intel_init_quirks(dev);
15018
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015019 intel_init_pm(dev);
15020
Ben Widawskye3c74752013-04-05 13:12:39 -070015021 if (INTEL_INFO(dev)->num_pipes == 0)
15022 return;
15023
Jesse Barnese70236a2009-09-21 10:42:27 -070015024 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015025 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015026
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015027 if (IS_GEN2(dev)) {
15028 dev->mode_config.max_width = 2048;
15029 dev->mode_config.max_height = 2048;
15030 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015031 dev->mode_config.max_width = 4096;
15032 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015033 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015034 dev->mode_config.max_width = 8192;
15035 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015036 }
Damien Lespiau068be562014-03-28 14:17:49 +000015037
Ville Syrjälädc41c152014-08-13 11:57:05 +030015038 if (IS_845G(dev) || IS_I865G(dev)) {
15039 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15040 dev->mode_config.cursor_height = 1023;
15041 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015042 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15043 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15044 } else {
15045 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15046 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15047 }
15048
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015049 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015050
Zhao Yakui28c97732009-10-09 11:39:41 +080015051 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015052 INTEL_INFO(dev)->num_pipes,
15053 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015054
Damien Lespiau055e3932014-08-18 13:49:10 +010015055 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015056 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015057 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015058 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015059 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015060 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015061 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015062 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015063 }
15064
Jesse Barnesf42bb702013-12-16 16:34:23 -080015065 intel_init_dpio(dev);
15066
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015067 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015068
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015069 /* Just disable it once at startup */
15070 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015071 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015072
15073 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015074 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015075
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015076 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015077 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015078 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015079
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015080 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015081 if (!crtc->active)
15082 continue;
15083
Jesse Barnes46f297f2014-03-07 08:57:48 -080015084 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015085 * Note that reserving the BIOS fb up front prevents us
15086 * from stuffing other stolen allocations like the ring
15087 * on top. This prevents some ugliness at boot time, and
15088 * can even allow for smooth boot transitions if the BIOS
15089 * fb is large enough for the active pipe configuration.
15090 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015091 if (dev_priv->display.get_initial_plane_config) {
15092 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015093 &crtc->plane_config);
15094 /*
15095 * If the fb is shared between multiple heads, we'll
15096 * just get the first one.
15097 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015098 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015099 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015100 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015101}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015102
Daniel Vetter7fad7982012-07-04 17:51:47 +020015103static void intel_enable_pipe_a(struct drm_device *dev)
15104{
15105 struct intel_connector *connector;
15106 struct drm_connector *crt = NULL;
15107 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015108 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015109
15110 /* We can't just switch on the pipe A, we need to set things up with a
15111 * proper mode and output configuration. As a gross hack, enable pipe A
15112 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015113 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015114 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15115 crt = &connector->base;
15116 break;
15117 }
15118 }
15119
15120 if (!crt)
15121 return;
15122
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015123 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015124 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015125}
15126
Daniel Vetterfa555832012-10-10 23:14:00 +020015127static bool
15128intel_check_plane_mapping(struct intel_crtc *crtc)
15129{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015130 struct drm_device *dev = crtc->base.dev;
15131 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015132 u32 reg, val;
15133
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015134 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015135 return true;
15136
15137 reg = DSPCNTR(!crtc->plane);
15138 val = I915_READ(reg);
15139
15140 if ((val & DISPLAY_PLANE_ENABLE) &&
15141 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15142 return false;
15143
15144 return true;
15145}
15146
Daniel Vetter24929352012-07-02 20:28:59 +020015147static void intel_sanitize_crtc(struct intel_crtc *crtc)
15148{
15149 struct drm_device *dev = crtc->base.dev;
15150 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015151 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020015152
Daniel Vetter24929352012-07-02 20:28:59 +020015153 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015154 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015155 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15156
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015157 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015158 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015159 if (crtc->active) {
15160 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015161 drm_crtc_vblank_on(&crtc->base);
15162 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015163
Daniel Vetter24929352012-07-02 20:28:59 +020015164 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015165 * disable the crtc (and hence change the state) if it is wrong. Note
15166 * that gen4+ has a fixed plane -> pipe mapping. */
15167 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015168 struct intel_connector *connector;
15169 bool plane;
15170
Daniel Vetter24929352012-07-02 20:28:59 +020015171 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15172 crtc->base.base.id);
15173
15174 /* Pipe has the wrong plane attached and the plane is active.
15175 * Temporarily change the plane mapping and disable everything
15176 * ... */
15177 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015178 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015179 crtc->plane = !plane;
Maarten Lankhorst1b509252015-06-01 12:49:48 +020015180 intel_crtc_control(&crtc->base, false);
Daniel Vetter24929352012-07-02 20:28:59 +020015181 crtc->plane = plane;
15182
15183 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015184 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015185 if (connector->encoder->base.crtc != &crtc->base)
15186 continue;
15187
Egbert Eich7f1950f2014-04-25 10:56:22 +020015188 connector->base.dpms = DRM_MODE_DPMS_OFF;
15189 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015190 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015191 /* multiple connectors may have the same encoder:
15192 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015193 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020015194 if (connector->encoder->base.crtc == &crtc->base) {
15195 connector->encoder->base.crtc = NULL;
15196 connector->encoder->connectors_active = false;
15197 }
Daniel Vetter24929352012-07-02 20:28:59 +020015198
15199 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080015200 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015201 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015202 crtc->base.enabled = false;
15203 }
Daniel Vetter24929352012-07-02 20:28:59 +020015204
Daniel Vetter7fad7982012-07-04 17:51:47 +020015205 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15206 crtc->pipe == PIPE_A && !crtc->active) {
15207 /* BIOS forgot to enable pipe A, this mostly happens after
15208 * resume. Force-enable the pipe to fix this, the update_dpms
15209 * call below we restore the pipe to the right state, but leave
15210 * the required bits on. */
15211 intel_enable_pipe_a(dev);
15212 }
15213
Daniel Vetter24929352012-07-02 20:28:59 +020015214 /* Adjust the state of the output pipe according to whether we
15215 * have active connectors/encoders. */
15216 intel_crtc_update_dpms(&crtc->base);
15217
Maarten Lankhorst53d9f4e2015-06-01 12:49:52 +020015218 if (crtc->active != crtc->base.state->active) {
Daniel Vetter24929352012-07-02 20:28:59 +020015219 struct intel_encoder *encoder;
15220
15221 /* This can happen either due to bugs in the get_hw_state
15222 * functions or because the pipe is force-enabled due to the
15223 * pipe A quirk. */
15224 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15225 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015226 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015227 crtc->active ? "enabled" : "disabled");
15228
Matt Roper83d65732015-02-25 13:12:16 -080015229 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015230 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015231 crtc->base.enabled = crtc->active;
15232
15233 /* Because we only establish the connector -> encoder ->
15234 * crtc links if something is active, this means the
15235 * crtc is now deactivated. Break the links. connector
15236 * -> encoder links are only establish when things are
15237 * actually up, hence no need to break them. */
15238 WARN_ON(crtc->active);
15239
15240 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15241 WARN_ON(encoder->connectors_active);
15242 encoder->base.crtc = NULL;
15243 }
15244 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015245
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015246 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015247 /*
15248 * We start out with underrun reporting disabled to avoid races.
15249 * For correct bookkeeping mark this on active crtcs.
15250 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015251 * Also on gmch platforms we dont have any hardware bits to
15252 * disable the underrun reporting. Which means we need to start
15253 * out with underrun reporting disabled also on inactive pipes,
15254 * since otherwise we'll complain about the garbage we read when
15255 * e.g. coming up after runtime pm.
15256 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015257 * No protection against concurrent access is required - at
15258 * worst a fifo underrun happens which also sets this to false.
15259 */
15260 crtc->cpu_fifo_underrun_disabled = true;
15261 crtc->pch_fifo_underrun_disabled = true;
15262 }
Daniel Vetter24929352012-07-02 20:28:59 +020015263}
15264
15265static void intel_sanitize_encoder(struct intel_encoder *encoder)
15266{
15267 struct intel_connector *connector;
15268 struct drm_device *dev = encoder->base.dev;
15269
15270 /* We need to check both for a crtc link (meaning that the
15271 * encoder is active and trying to read from a pipe) and the
15272 * pipe itself being active. */
15273 bool has_active_crtc = encoder->base.crtc &&
15274 to_intel_crtc(encoder->base.crtc)->active;
15275
15276 if (encoder->connectors_active && !has_active_crtc) {
15277 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15278 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015279 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015280
15281 /* Connector is active, but has no active pipe. This is
15282 * fallout from our resume register restoring. Disable
15283 * the encoder manually again. */
15284 if (encoder->base.crtc) {
15285 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15286 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015287 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015288 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015289 if (encoder->post_disable)
15290 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015291 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015292 encoder->base.crtc = NULL;
15293 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015294
15295 /* Inconsistent output/port/pipe state happens presumably due to
15296 * a bug in one of the get_hw_state functions. Or someplace else
15297 * in our code, like the register restore mess on resume. Clamp
15298 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015299 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015300 if (connector->encoder != encoder)
15301 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015302 connector->base.dpms = DRM_MODE_DPMS_OFF;
15303 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015304 }
15305 }
15306 /* Enabled encoders without active connectors will be fixed in
15307 * the crtc fixup. */
15308}
15309
Imre Deak04098752014-02-18 00:02:16 +020015310void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015311{
15312 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015313 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015314
Imre Deak04098752014-02-18 00:02:16 +020015315 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15316 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15317 i915_disable_vga(dev);
15318 }
15319}
15320
15321void i915_redisable_vga(struct drm_device *dev)
15322{
15323 struct drm_i915_private *dev_priv = dev->dev_private;
15324
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015325 /* This function can be called both from intel_modeset_setup_hw_state or
15326 * at a very early point in our resume sequence, where the power well
15327 * structures are not yet restored. Since this function is at a very
15328 * paranoid "someone might have enabled VGA while we were not looking"
15329 * level, just check if the power well is enabled instead of trying to
15330 * follow the "don't touch the power well if we don't need it" policy
15331 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015332 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015333 return;
15334
Imre Deak04098752014-02-18 00:02:16 +020015335 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015336}
15337
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015338static bool primary_get_hw_state(struct intel_crtc *crtc)
15339{
15340 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15341
15342 if (!crtc->active)
15343 return false;
15344
15345 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15346}
15347
Daniel Vetter30e984d2013-06-05 13:34:17 +020015348static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015349{
15350 struct drm_i915_private *dev_priv = dev->dev_private;
15351 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015352 struct intel_crtc *crtc;
15353 struct intel_encoder *encoder;
15354 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015355 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015356
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015357 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015358 struct drm_plane *primary = crtc->base.primary;
15359 struct intel_plane_state *plane_state;
15360
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015361 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015362
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015363 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015364
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015365 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015366 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015367
Matt Roper83d65732015-02-25 13:12:16 -080015368 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015369 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015370 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015371
15372 plane_state = to_intel_plane_state(primary->state);
15373 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015374
15375 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15376 crtc->base.base.id,
15377 crtc->active ? "enabled" : "disabled");
15378 }
15379
Daniel Vetter53589012013-06-05 13:34:16 +020015380 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15381 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15382
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015383 pll->on = pll->get_hw_state(dev_priv, pll,
15384 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015385 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015386 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015387 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015388 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015389 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015390 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015391 }
Daniel Vetter53589012013-06-05 13:34:16 +020015392 }
Daniel Vetter53589012013-06-05 13:34:16 +020015393
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015394 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015395 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015396
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015397 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015398 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015399 }
15400
Damien Lespiaub2784e12014-08-05 11:29:37 +010015401 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015402 pipe = 0;
15403
15404 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015405 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15406 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015407 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015408 } else {
15409 encoder->base.crtc = NULL;
15410 }
15411
15412 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015413 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015414 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015415 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015416 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015417 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015418 }
15419
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015420 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015421 if (connector->get_hw_state(connector)) {
15422 connector->base.dpms = DRM_MODE_DPMS_ON;
15423 connector->encoder->connectors_active = true;
15424 connector->base.encoder = &connector->encoder->base;
15425 } else {
15426 connector->base.dpms = DRM_MODE_DPMS_OFF;
15427 connector->base.encoder = NULL;
15428 }
15429 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15430 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015431 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015432 connector->base.encoder ? "enabled" : "disabled");
15433 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015434}
15435
15436/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15437 * and i915 state tracking structures. */
15438void intel_modeset_setup_hw_state(struct drm_device *dev,
15439 bool force_restore)
15440{
15441 struct drm_i915_private *dev_priv = dev->dev_private;
15442 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015443 struct intel_crtc *crtc;
15444 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015445 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015446
15447 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015448
Jesse Barnesbabea612013-06-26 18:57:38 +030015449 /*
15450 * Now that we have the config, copy it to each CRTC struct
15451 * Note that this could go away if we move to using crtc_config
15452 * checking everywhere.
15453 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015454 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015455 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015456 intel_mode_from_pipe_config(&crtc->base.mode,
15457 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015458 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15459 crtc->base.base.id);
15460 drm_mode_debug_printmodeline(&crtc->base.mode);
15461 }
15462 }
15463
Daniel Vetter24929352012-07-02 20:28:59 +020015464 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015465 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015466 intel_sanitize_encoder(encoder);
15467 }
15468
Damien Lespiau055e3932014-08-18 13:49:10 +010015469 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015470 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15471 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015472 intel_dump_pipe_config(crtc, crtc->config,
15473 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015474 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015475
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015476 intel_modeset_update_connector_atomic_state(dev);
15477
Daniel Vetter35c95372013-07-17 06:55:04 +020015478 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15479 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15480
15481 if (!pll->on || pll->active)
15482 continue;
15483
15484 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15485
15486 pll->disable(dev_priv, pll);
15487 pll->on = false;
15488 }
15489
Pradeep Bhat30789992014-11-04 17:06:45 +000015490 if (IS_GEN9(dev))
15491 skl_wm_get_hw_state(dev);
15492 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015493 ilk_wm_get_hw_state(dev);
15494
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015495 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015496 i915_redisable_vga(dev);
15497
Daniel Vetterf30da182013-04-11 20:22:50 +020015498 /*
15499 * We need to use raw interfaces for restoring state to avoid
15500 * checking (bogus) intermediate states.
15501 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015502 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015503 struct drm_crtc *crtc =
15504 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015505
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015506 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015507 }
15508 } else {
15509 intel_modeset_update_staged_output_state(dev);
15510 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015511
15512 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015513}
15514
15515void intel_modeset_gem_init(struct drm_device *dev)
15516{
Jesse Barnes92122782014-10-09 12:57:42 -070015517 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015518 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015519 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015520 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015521
Imre Deakae484342014-03-31 15:10:44 +030015522 mutex_lock(&dev->struct_mutex);
15523 intel_init_gt_powersave(dev);
15524 mutex_unlock(&dev->struct_mutex);
15525
Jesse Barnes92122782014-10-09 12:57:42 -070015526 /*
15527 * There may be no VBT; and if the BIOS enabled SSC we can
15528 * just keep using it to avoid unnecessary flicker. Whereas if the
15529 * BIOS isn't using it, don't assume it will work even if the VBT
15530 * indicates as much.
15531 */
15532 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15533 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15534 DREF_SSC1_ENABLE);
15535
Chris Wilson1833b132012-05-09 11:56:28 +010015536 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015537
15538 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015539
15540 /*
15541 * Make sure any fbs we allocated at startup are properly
15542 * pinned & fenced. When we do the allocation it's too early
15543 * for this.
15544 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015545 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015546 obj = intel_fb_obj(c->primary->fb);
15547 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015548 continue;
15549
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015550 mutex_lock(&dev->struct_mutex);
15551 ret = intel_pin_and_fence_fb_obj(c->primary,
15552 c->primary->fb,
15553 c->primary->state,
15554 NULL);
15555 mutex_unlock(&dev->struct_mutex);
15556 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015557 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15558 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015559 drm_framebuffer_unreference(c->primary->fb);
15560 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015561 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015562 }
15563 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015564
15565 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015566}
15567
Imre Deak4932e2c2014-02-11 17:12:48 +020015568void intel_connector_unregister(struct intel_connector *intel_connector)
15569{
15570 struct drm_connector *connector = &intel_connector->base;
15571
15572 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015573 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015574}
15575
Jesse Barnes79e53942008-11-07 14:24:08 -080015576void intel_modeset_cleanup(struct drm_device *dev)
15577{
Jesse Barnes652c3932009-08-17 13:31:43 -070015578 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015579 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015580
Imre Deak2eb52522014-11-19 15:30:05 +020015581 intel_disable_gt_powersave(dev);
15582
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015583 intel_backlight_unregister(dev);
15584
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015585 /*
15586 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015587 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015588 * experience fancy races otherwise.
15589 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015590 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015591
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015592 /*
15593 * Due to the hpd irq storm handling the hotplug work can re-arm the
15594 * poll handlers. Hence disable polling after hpd handling is shut down.
15595 */
Keith Packardf87ea762010-10-03 19:36:26 -070015596 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015597
Jesse Barnes652c3932009-08-17 13:31:43 -070015598 mutex_lock(&dev->struct_mutex);
15599
Jesse Barnes723bfd72010-10-07 16:01:13 -070015600 intel_unregister_dsm_handler();
15601
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015602 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015603
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015604 mutex_unlock(&dev->struct_mutex);
15605
Chris Wilson1630fe72011-07-08 12:22:42 +010015606 /* flush any delayed tasks or pending work */
15607 flush_scheduled_work();
15608
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015609 /* destroy the backlight and sysfs files before encoders/connectors */
15610 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015611 struct intel_connector *intel_connector;
15612
15613 intel_connector = to_intel_connector(connector);
15614 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015615 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015616
Jesse Barnes79e53942008-11-07 14:24:08 -080015617 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015618
15619 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015620
15621 mutex_lock(&dev->struct_mutex);
15622 intel_cleanup_gt_powersave(dev);
15623 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015624}
15625
Dave Airlie28d52042009-09-21 14:33:58 +100015626/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015627 * Return which encoder is currently attached for connector.
15628 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015629struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015630{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015631 return &intel_attached_encoder(connector)->base;
15632}
Jesse Barnes79e53942008-11-07 14:24:08 -080015633
Chris Wilsondf0e9242010-09-09 16:20:55 +010015634void intel_connector_attach_encoder(struct intel_connector *connector,
15635 struct intel_encoder *encoder)
15636{
15637 connector->encoder = encoder;
15638 drm_mode_connector_attach_encoder(&connector->base,
15639 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015640}
Dave Airlie28d52042009-09-21 14:33:58 +100015641
15642/*
15643 * set vga decode state - true == enable VGA decode
15644 */
15645int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15646{
15647 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015648 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015649 u16 gmch_ctrl;
15650
Chris Wilson75fa0412014-02-07 18:37:02 -020015651 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15652 DRM_ERROR("failed to read control word\n");
15653 return -EIO;
15654 }
15655
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015656 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15657 return 0;
15658
Dave Airlie28d52042009-09-21 14:33:58 +100015659 if (state)
15660 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15661 else
15662 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015663
15664 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15665 DRM_ERROR("failed to write control word\n");
15666 return -EIO;
15667 }
15668
Dave Airlie28d52042009-09-21 14:33:58 +100015669 return 0;
15670}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015671
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015672struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015673
15674 u32 power_well_driver;
15675
Chris Wilson63b66e52013-08-08 15:12:06 +020015676 int num_transcoders;
15677
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015678 struct intel_cursor_error_state {
15679 u32 control;
15680 u32 position;
15681 u32 base;
15682 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015683 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015684
15685 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015686 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015687 u32 source;
Imre Deakf301b1e2014-04-18 15:55:04 +030015688 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015689 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015690
15691 struct intel_plane_error_state {
15692 u32 control;
15693 u32 stride;
15694 u32 size;
15695 u32 pos;
15696 u32 addr;
15697 u32 surface;
15698 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015699 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015700
15701 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015702 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015703 enum transcoder cpu_transcoder;
15704
15705 u32 conf;
15706
15707 u32 htotal;
15708 u32 hblank;
15709 u32 hsync;
15710 u32 vtotal;
15711 u32 vblank;
15712 u32 vsync;
15713 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015714};
15715
15716struct intel_display_error_state *
15717intel_display_capture_error_state(struct drm_device *dev)
15718{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015719 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015720 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015721 int transcoders[] = {
15722 TRANSCODER_A,
15723 TRANSCODER_B,
15724 TRANSCODER_C,
15725 TRANSCODER_EDP,
15726 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015727 int i;
15728
Chris Wilson63b66e52013-08-08 15:12:06 +020015729 if (INTEL_INFO(dev)->num_pipes == 0)
15730 return NULL;
15731
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015732 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015733 if (error == NULL)
15734 return NULL;
15735
Imre Deak190be112013-11-25 17:15:31 +020015736 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015737 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15738
Damien Lespiau055e3932014-08-18 13:49:10 +010015739 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015740 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015741 __intel_display_power_is_enabled(dev_priv,
15742 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015743 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015744 continue;
15745
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015746 error->cursor[i].control = I915_READ(CURCNTR(i));
15747 error->cursor[i].position = I915_READ(CURPOS(i));
15748 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015749
15750 error->plane[i].control = I915_READ(DSPCNTR(i));
15751 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015752 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015753 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015754 error->plane[i].pos = I915_READ(DSPPOS(i));
15755 }
Paulo Zanonica291362013-03-06 20:03:14 -030015756 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15757 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015758 if (INTEL_INFO(dev)->gen >= 4) {
15759 error->plane[i].surface = I915_READ(DSPSURF(i));
15760 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15761 }
15762
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015763 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e2014-04-18 15:55:04 +030015764
Sonika Jindal3abfce72014-07-21 15:23:43 +053015765 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e2014-04-18 15:55:04 +030015766 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015767 }
15768
15769 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15770 if (HAS_DDI(dev_priv->dev))
15771 error->num_transcoders++; /* Account for eDP. */
15772
15773 for (i = 0; i < error->num_transcoders; i++) {
15774 enum transcoder cpu_transcoder = transcoders[i];
15775
Imre Deakddf9c532013-11-27 22:02:02 +020015776 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015777 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015778 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015779 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015780 continue;
15781
Chris Wilson63b66e52013-08-08 15:12:06 +020015782 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15783
15784 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15785 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15786 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15787 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15788 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15789 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15790 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015791 }
15792
15793 return error;
15794}
15795
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015796#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15797
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015798void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015799intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015800 struct drm_device *dev,
15801 struct intel_display_error_state *error)
15802{
Damien Lespiau055e3932014-08-18 13:49:10 +010015803 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015804 int i;
15805
Chris Wilson63b66e52013-08-08 15:12:06 +020015806 if (!error)
15807 return;
15808
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015809 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015810 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015811 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015812 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015813 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015814 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015815 err_printf(m, " Power: %s\n",
15816 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015817 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e2014-04-18 15:55:04 +030015818 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015819
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015820 err_printf(m, "Plane [%d]:\n", i);
15821 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15822 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015823 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015824 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15825 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015826 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015827 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015828 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015829 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015830 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15831 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015832 }
15833
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015834 err_printf(m, "Cursor [%d]:\n", i);
15835 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15836 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15837 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015838 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015839
15840 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015841 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015842 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015843 err_printf(m, " Power: %s\n",
15844 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015845 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15846 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15847 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15848 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15849 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15850 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15851 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15852 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015853}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015854
15855void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15856{
15857 struct intel_crtc *crtc;
15858
15859 for_each_intel_crtc(dev, crtc) {
15860 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015861
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015862 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015863
15864 work = crtc->unpin_work;
15865
15866 if (work && work->event &&
15867 work->event->base.file_priv == file) {
15868 kfree(work->event);
15869 work->event = NULL;
15870 }
15871
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015872 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015873 }
15874}