blob: da7c886026da02c1cc6c55bf7e2207f67c83e0f9 [file] [log] [blame]
Jesse Barnes79e53942008-11-07 14:24:08 -08001/*
2 * Copyright © 2006-2007 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Eric Anholt <eric@anholt.net>
25 */
26
Daniel Vetter618563e2012-04-01 13:38:50 +020027#include <linux/dmi.h>
Jesse Barnesc1c7af62009-09-10 15:28:03 -070028#include <linux/module.h>
29#include <linux/input.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080030#include <linux/i2c.h>
Shaohua Li7662c8b2009-06-26 11:23:55 +080031#include <linux/kernel.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090032#include <linux/slab.h>
Jesse Barnes9cce37f2010-08-13 15:11:26 -070033#include <linux/vgaarb.h>
Wu Fengguange0dac652011-09-05 14:25:34 +080034#include <drm/drm_edid.h>
David Howells760285e2012-10-02 18:01:07 +010035#include <drm/drmP.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080036#include "intel_drv.h"
David Howells760285e2012-10-02 18:01:07 +010037#include <drm/i915_drm.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080038#include "i915_drv.h"
Jesse Barnese5510fa2010-07-01 16:48:37 -070039#include "i915_trace.h"
Xi Ruoyao319c1d42015-03-12 20:16:32 +080040#include <drm/drm_atomic.h>
Matt Roperc196e1d2015-01-21 16:35:48 -080041#include <drm/drm_atomic_helper.h>
David Howells760285e2012-10-02 18:01:07 +010042#include <drm/drm_dp_helper.h>
43#include <drm/drm_crtc_helper.h>
Matt Roper465c1202014-05-29 08:06:54 -070044#include <drm/drm_plane_helper.h>
45#include <drm/drm_rect.h>
Keith Packardc0f372b32011-11-16 22:24:52 -080046#include <linux/dma_remapping.h>
Jesse Barnes79e53942008-11-07 14:24:08 -080047
Matt Roper465c1202014-05-29 08:06:54 -070048/* Primary plane formats for gen <= 3 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010049static const uint32_t i8xx_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010050 DRM_FORMAT_C8,
51 DRM_FORMAT_RGB565,
Matt Roper465c1202014-05-29 08:06:54 -070052 DRM_FORMAT_XRGB1555,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010053 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070054};
55
56/* Primary plane formats for gen >= 4 */
Damien Lespiau568db4f2015-05-12 16:13:18 +010057static const uint32_t i965_primary_formats[] = {
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010058 DRM_FORMAT_C8,
59 DRM_FORMAT_RGB565,
60 DRM_FORMAT_XRGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070061 DRM_FORMAT_XBGR8888,
Damien Lespiau6c0fd452015-05-19 12:29:16 +010062 DRM_FORMAT_XRGB2101010,
63 DRM_FORMAT_XBGR2101010,
64};
65
66static const uint32_t skl_primary_formats[] = {
67 DRM_FORMAT_C8,
68 DRM_FORMAT_RGB565,
69 DRM_FORMAT_XRGB8888,
70 DRM_FORMAT_XBGR8888,
Damien Lespiau67fe7dc2015-05-15 19:06:00 +010071 DRM_FORMAT_ARGB8888,
Matt Roper465c1202014-05-29 08:06:54 -070072 DRM_FORMAT_ABGR8888,
73 DRM_FORMAT_XRGB2101010,
Matt Roper465c1202014-05-29 08:06:54 -070074 DRM_FORMAT_XBGR2101010,
Matt Roper465c1202014-05-29 08:06:54 -070075};
76
Matt Roper3d7d6512014-06-10 08:28:13 -070077/* Cursor formats */
78static const uint32_t intel_cursor_formats[] = {
79 DRM_FORMAT_ARGB8888,
80};
81
Chris Wilson6b383a72010-09-13 13:54:26 +010082static void intel_crtc_update_cursor(struct drm_crtc *crtc, bool on);
Jesse Barnes79e53942008-11-07 14:24:08 -080083
Jesse Barnesf1f644d2013-06-27 00:39:25 +030084static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020085 struct intel_crtc_state *pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +030086static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020087 struct intel_crtc_state *pipe_config);
Jesse Barnesf1f644d2013-06-27 00:39:25 +030088
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030089static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020090 struct drm_atomic_state *state);
Jesse Barneseb1bfe82014-02-12 12:26:25 -080091static int intel_framebuffer_init(struct drm_device *dev,
92 struct intel_framebuffer *ifb,
93 struct drm_mode_fb_cmd2 *mode_cmd,
94 struct drm_i915_gem_object *obj);
Daniel Vetter5b18e572014-04-24 23:55:06 +020095static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc);
96static void intel_set_pipe_timings(struct intel_crtc *intel_crtc);
Daniel Vetter29407aa2014-04-24 23:55:08 +020097static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -070098 struct intel_link_m_n *m_n,
99 struct intel_link_m_n *m2_n2);
Daniel Vetter29407aa2014-04-24 23:55:08 +0200100static void ironlake_set_pipeconf(struct drm_crtc *crtc);
Daniel Vetter229fca92014-04-24 23:55:09 +0200101static void haswell_set_pipeconf(struct drm_crtc *crtc);
102static void intel_set_pipe_csc(struct drm_crtc *crtc);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200103static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200104 const struct intel_crtc_state *pipe_config);
Ville Syrjäläd288f652014-10-28 13:20:22 +0200105static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +0200106 const struct intel_crtc_state *pipe_config);
Matt Roperea2c67b2014-12-23 10:41:52 -0800107static void intel_begin_crtc_commit(struct drm_crtc *crtc);
108static void intel_finish_crtc_commit(struct drm_crtc *crtc);
Chandra Konduru549e2bf2015-04-07 15:28:38 -0700109static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
110 struct intel_crtc_state *crtc_state);
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200111static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
112 int num_connectors);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +0300113static void intel_crtc_enable_planes(struct drm_crtc *crtc);
114static void intel_crtc_disable_planes(struct drm_crtc *crtc);
Damien Lespiaue7457a92013-08-08 22:28:59 +0100115
Dave Airlie0e32b392014-05-02 14:02:48 +1000116static struct intel_encoder *intel_find_encoder(struct intel_connector *connector, int pipe)
117{
118 if (!connector->mst_port)
119 return connector->encoder;
120 else
121 return &connector->mst_port->mst_encoders[pipe]->base;
122}
123
Jesse Barnes79e53942008-11-07 14:24:08 -0800124typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400125 int min, max;
Jesse Barnes79e53942008-11-07 14:24:08 -0800126} intel_range_t;
127
128typedef struct {
Akshay Joshi0206e352011-08-16 15:34:10 -0400129 int dot_limit;
130 int p2_slow, p2_fast;
Jesse Barnes79e53942008-11-07 14:24:08 -0800131} intel_p2_t;
132
Ma Lingd4906092009-03-18 20:13:27 +0800133typedef struct intel_limit intel_limit_t;
134struct intel_limit {
Akshay Joshi0206e352011-08-16 15:34:10 -0400135 intel_range_t dot, vco, n, m, m1, m2, p, p1;
136 intel_p2_t p2;
Ma Lingd4906092009-03-18 20:13:27 +0800137};
Jesse Barnes79e53942008-11-07 14:24:08 -0800138
Daniel Vetterd2acd212012-10-20 20:57:43 +0200139int
140intel_pch_rawclk(struct drm_device *dev)
141{
142 struct drm_i915_private *dev_priv = dev->dev_private;
143
144 WARN_ON(!HAS_PCH_SPLIT(dev));
145
146 return I915_READ(PCH_RAWCLK_FREQ) & RAWCLK_FREQ_MASK;
147}
148
Chris Wilson021357a2010-09-07 20:54:59 +0100149static inline u32 /* units of 100MHz */
150intel_fdi_link_freq(struct drm_device *dev)
151{
Chris Wilson8b99e682010-10-13 09:59:17 +0100152 if (IS_GEN5(dev)) {
153 struct drm_i915_private *dev_priv = dev->dev_private;
154 return (I915_READ(FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK) + 2;
155 } else
156 return 27;
Chris Wilson021357a2010-09-07 20:54:59 +0100157}
158
Daniel Vetter5d536e22013-07-06 12:52:06 +0200159static const intel_limit_t intel_limits_i8xx_dac = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400160 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200161 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200162 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400163 .m = { .min = 96, .max = 140 },
164 .m1 = { .min = 18, .max = 26 },
165 .m2 = { .min = 6, .max = 16 },
166 .p = { .min = 4, .max = 128 },
167 .p1 = { .min = 2, .max = 33 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700168 .p2 = { .dot_limit = 165000,
169 .p2_slow = 4, .p2_fast = 2 },
Keith Packarde4b36692009-06-05 19:22:17 -0700170};
171
Daniel Vetter5d536e22013-07-06 12:52:06 +0200172static const intel_limit_t intel_limits_i8xx_dvo = {
173 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200174 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200175 .n = { .min = 2, .max = 16 },
Daniel Vetter5d536e22013-07-06 12:52:06 +0200176 .m = { .min = 96, .max = 140 },
177 .m1 = { .min = 18, .max = 26 },
178 .m2 = { .min = 6, .max = 16 },
179 .p = { .min = 4, .max = 128 },
180 .p1 = { .min = 2, .max = 33 },
181 .p2 = { .dot_limit = 165000,
182 .p2_slow = 4, .p2_fast = 4 },
183};
184
Keith Packarde4b36692009-06-05 19:22:17 -0700185static const intel_limit_t intel_limits_i8xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400186 .dot = { .min = 25000, .max = 350000 },
Ville Syrjälä9c333712013-12-09 18:54:17 +0200187 .vco = { .min = 908000, .max = 1512000 },
Ville Syrjälä91dbe5f2013-12-09 18:54:14 +0200188 .n = { .min = 2, .max = 16 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400189 .m = { .min = 96, .max = 140 },
190 .m1 = { .min = 18, .max = 26 },
191 .m2 = { .min = 6, .max = 16 },
192 .p = { .min = 4, .max = 128 },
193 .p1 = { .min = 1, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700194 .p2 = { .dot_limit = 165000,
195 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700196};
Eric Anholt273e27c2011-03-30 13:01:10 -0700197
Keith Packarde4b36692009-06-05 19:22:17 -0700198static const intel_limit_t intel_limits_i9xx_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400199 .dot = { .min = 20000, .max = 400000 },
200 .vco = { .min = 1400000, .max = 2800000 },
201 .n = { .min = 1, .max = 6 },
202 .m = { .min = 70, .max = 120 },
Patrik Jakobsson4f7dfb62013-02-13 22:20:22 +0100203 .m1 = { .min = 8, .max = 18 },
204 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400205 .p = { .min = 5, .max = 80 },
206 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700207 .p2 = { .dot_limit = 200000,
208 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700209};
210
211static const intel_limit_t intel_limits_i9xx_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400212 .dot = { .min = 20000, .max = 400000 },
213 .vco = { .min = 1400000, .max = 2800000 },
214 .n = { .min = 1, .max = 6 },
215 .m = { .min = 70, .max = 120 },
Patrik Jakobsson53a7d2d2013-02-13 22:20:21 +0100216 .m1 = { .min = 8, .max = 18 },
217 .m2 = { .min = 3, .max = 7 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400218 .p = { .min = 7, .max = 98 },
219 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700220 .p2 = { .dot_limit = 112000,
221 .p2_slow = 14, .p2_fast = 7 },
Keith Packarde4b36692009-06-05 19:22:17 -0700222};
223
Eric Anholt273e27c2011-03-30 13:01:10 -0700224
Keith Packarde4b36692009-06-05 19:22:17 -0700225static const intel_limit_t intel_limits_g4x_sdvo = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700226 .dot = { .min = 25000, .max = 270000 },
227 .vco = { .min = 1750000, .max = 3500000},
228 .n = { .min = 1, .max = 4 },
229 .m = { .min = 104, .max = 138 },
230 .m1 = { .min = 17, .max = 23 },
231 .m2 = { .min = 5, .max = 11 },
232 .p = { .min = 10, .max = 30 },
233 .p1 = { .min = 1, .max = 3},
234 .p2 = { .dot_limit = 270000,
235 .p2_slow = 10,
236 .p2_fast = 10
Ma Ling044c7c42009-03-18 20:13:23 +0800237 },
Keith Packarde4b36692009-06-05 19:22:17 -0700238};
239
240static const intel_limit_t intel_limits_g4x_hdmi = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700241 .dot = { .min = 22000, .max = 400000 },
242 .vco = { .min = 1750000, .max = 3500000},
243 .n = { .min = 1, .max = 4 },
244 .m = { .min = 104, .max = 138 },
245 .m1 = { .min = 16, .max = 23 },
246 .m2 = { .min = 5, .max = 11 },
247 .p = { .min = 5, .max = 80 },
248 .p1 = { .min = 1, .max = 8},
249 .p2 = { .dot_limit = 165000,
250 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700251};
252
253static const intel_limit_t intel_limits_g4x_single_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700254 .dot = { .min = 20000, .max = 115000 },
255 .vco = { .min = 1750000, .max = 3500000 },
256 .n = { .min = 1, .max = 3 },
257 .m = { .min = 104, .max = 138 },
258 .m1 = { .min = 17, .max = 23 },
259 .m2 = { .min = 5, .max = 11 },
260 .p = { .min = 28, .max = 112 },
261 .p1 = { .min = 2, .max = 8 },
262 .p2 = { .dot_limit = 0,
263 .p2_slow = 14, .p2_fast = 14
Ma Ling044c7c42009-03-18 20:13:23 +0800264 },
Keith Packarde4b36692009-06-05 19:22:17 -0700265};
266
267static const intel_limit_t intel_limits_g4x_dual_channel_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700268 .dot = { .min = 80000, .max = 224000 },
269 .vco = { .min = 1750000, .max = 3500000 },
270 .n = { .min = 1, .max = 3 },
271 .m = { .min = 104, .max = 138 },
272 .m1 = { .min = 17, .max = 23 },
273 .m2 = { .min = 5, .max = 11 },
274 .p = { .min = 14, .max = 42 },
275 .p1 = { .min = 2, .max = 6 },
276 .p2 = { .dot_limit = 0,
277 .p2_slow = 7, .p2_fast = 7
Ma Ling044c7c42009-03-18 20:13:23 +0800278 },
Keith Packarde4b36692009-06-05 19:22:17 -0700279};
280
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500281static const intel_limit_t intel_limits_pineview_sdvo = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400282 .dot = { .min = 20000, .max = 400000},
283 .vco = { .min = 1700000, .max = 3500000 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700284 /* Pineview's Ncounter is a ring counter */
Akshay Joshi0206e352011-08-16 15:34:10 -0400285 .n = { .min = 3, .max = 6 },
286 .m = { .min = 2, .max = 256 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700287 /* Pineview only has one combined m divider, which we treat as m2. */
Akshay Joshi0206e352011-08-16 15:34:10 -0400288 .m1 = { .min = 0, .max = 0 },
289 .m2 = { .min = 0, .max = 254 },
290 .p = { .min = 5, .max = 80 },
291 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700292 .p2 = { .dot_limit = 200000,
293 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700294};
295
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500296static const intel_limit_t intel_limits_pineview_lvds = {
Akshay Joshi0206e352011-08-16 15:34:10 -0400297 .dot = { .min = 20000, .max = 400000 },
298 .vco = { .min = 1700000, .max = 3500000 },
299 .n = { .min = 3, .max = 6 },
300 .m = { .min = 2, .max = 256 },
301 .m1 = { .min = 0, .max = 0 },
302 .m2 = { .min = 0, .max = 254 },
303 .p = { .min = 7, .max = 112 },
304 .p1 = { .min = 1, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700305 .p2 = { .dot_limit = 112000,
306 .p2_slow = 14, .p2_fast = 14 },
Keith Packarde4b36692009-06-05 19:22:17 -0700307};
308
Eric Anholt273e27c2011-03-30 13:01:10 -0700309/* Ironlake / Sandybridge
310 *
311 * We calculate clock using (register_value + 2) for N/M1/M2, so here
312 * the range value for them is (actual_value - 2).
313 */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800314static const intel_limit_t intel_limits_ironlake_dac = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700315 .dot = { .min = 25000, .max = 350000 },
316 .vco = { .min = 1760000, .max = 3510000 },
317 .n = { .min = 1, .max = 5 },
318 .m = { .min = 79, .max = 127 },
319 .m1 = { .min = 12, .max = 22 },
320 .m2 = { .min = 5, .max = 9 },
321 .p = { .min = 5, .max = 80 },
322 .p1 = { .min = 1, .max = 8 },
323 .p2 = { .dot_limit = 225000,
324 .p2_slow = 10, .p2_fast = 5 },
Keith Packarde4b36692009-06-05 19:22:17 -0700325};
326
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800327static const intel_limit_t intel_limits_ironlake_single_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700328 .dot = { .min = 25000, .max = 350000 },
329 .vco = { .min = 1760000, .max = 3510000 },
330 .n = { .min = 1, .max = 3 },
331 .m = { .min = 79, .max = 118 },
332 .m1 = { .min = 12, .max = 22 },
333 .m2 = { .min = 5, .max = 9 },
334 .p = { .min = 28, .max = 112 },
335 .p1 = { .min = 2, .max = 8 },
336 .p2 = { .dot_limit = 225000,
337 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800338};
339
340static const intel_limit_t intel_limits_ironlake_dual_lvds = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700341 .dot = { .min = 25000, .max = 350000 },
342 .vco = { .min = 1760000, .max = 3510000 },
343 .n = { .min = 1, .max = 3 },
344 .m = { .min = 79, .max = 127 },
345 .m1 = { .min = 12, .max = 22 },
346 .m2 = { .min = 5, .max = 9 },
347 .p = { .min = 14, .max = 56 },
348 .p1 = { .min = 2, .max = 8 },
349 .p2 = { .dot_limit = 225000,
350 .p2_slow = 7, .p2_fast = 7 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800351};
352
Eric Anholt273e27c2011-03-30 13:01:10 -0700353/* LVDS 100mhz refclk limits. */
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800354static const intel_limit_t intel_limits_ironlake_single_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700355 .dot = { .min = 25000, .max = 350000 },
356 .vco = { .min = 1760000, .max = 3510000 },
357 .n = { .min = 1, .max = 2 },
358 .m = { .min = 79, .max = 126 },
359 .m1 = { .min = 12, .max = 22 },
360 .m2 = { .min = 5, .max = 9 },
361 .p = { .min = 28, .max = 112 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400362 .p1 = { .min = 2, .max = 8 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700363 .p2 = { .dot_limit = 225000,
364 .p2_slow = 14, .p2_fast = 14 },
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800365};
366
367static const intel_limit_t intel_limits_ironlake_dual_lvds_100m = {
Eric Anholt273e27c2011-03-30 13:01:10 -0700368 .dot = { .min = 25000, .max = 350000 },
369 .vco = { .min = 1760000, .max = 3510000 },
370 .n = { .min = 1, .max = 3 },
371 .m = { .min = 79, .max = 126 },
372 .m1 = { .min = 12, .max = 22 },
373 .m2 = { .min = 5, .max = 9 },
374 .p = { .min = 14, .max = 42 },
Akshay Joshi0206e352011-08-16 15:34:10 -0400375 .p1 = { .min = 2, .max = 6 },
Eric Anholt273e27c2011-03-30 13:01:10 -0700376 .p2 = { .dot_limit = 225000,
377 .p2_slow = 7, .p2_fast = 7 },
Zhao Yakui45476682009-12-31 16:06:04 +0800378};
379
Ville Syrjälädc730512013-09-24 21:26:30 +0300380static const intel_limit_t intel_limits_vlv = {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300381 /*
382 * These are the data rate limits (measured in fast clocks)
383 * since those are the strictest limits we have. The fast
384 * clock and actual rate limits are more relaxed, so checking
385 * them would make no difference.
386 */
387 .dot = { .min = 25000 * 5, .max = 270000 * 5 },
Daniel Vetter75e53982013-04-18 21:10:43 +0200388 .vco = { .min = 4000000, .max = 6000000 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700389 .n = { .min = 1, .max = 7 },
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700390 .m1 = { .min = 2, .max = 3 },
391 .m2 = { .min = 11, .max = 156 },
Ville Syrjäläb99ab662013-09-24 21:26:26 +0300392 .p1 = { .min = 2, .max = 3 },
Ville Syrjälä5fdc9c492013-09-24 21:26:29 +0300393 .p2 = { .p2_slow = 2, .p2_fast = 20 }, /* slow=min, fast=max */
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700394};
395
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300396static const intel_limit_t intel_limits_chv = {
397 /*
398 * These are the data rate limits (measured in fast clocks)
399 * since those are the strictest limits we have. The fast
400 * clock and actual rate limits are more relaxed, so checking
401 * them would make no difference.
402 */
403 .dot = { .min = 25000 * 5, .max = 540000 * 5},
Ville Syrjälä17fe1022015-02-26 21:01:52 +0200404 .vco = { .min = 4800000, .max = 6480000 },
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300405 .n = { .min = 1, .max = 1 },
406 .m1 = { .min = 2, .max = 2 },
407 .m2 = { .min = 24 << 22, .max = 175 << 22 },
408 .p1 = { .min = 2, .max = 4 },
409 .p2 = { .p2_slow = 1, .p2_fast = 14 },
410};
411
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200412static const intel_limit_t intel_limits_bxt = {
413 /* FIXME: find real dot limits */
414 .dot = { .min = 0, .max = INT_MAX },
415 .vco = { .min = 4800000, .max = 6480000 },
416 .n = { .min = 1, .max = 1 },
417 .m1 = { .min = 2, .max = 2 },
418 /* FIXME: find real m2 limits */
419 .m2 = { .min = 2 << 22, .max = 255 << 22 },
420 .p1 = { .min = 2, .max = 4 },
421 .p2 = { .p2_slow = 1, .p2_fast = 20 },
422};
423
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300424static void vlv_clock(int refclk, intel_clock_t *clock)
425{
426 clock->m = clock->m1 * clock->m2;
427 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200428 if (WARN_ON(clock->n == 0 || clock->p == 0))
429 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300430 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
431 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300432}
433
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300434/**
435 * Returns whether any output on the specified pipe is of the specified type
436 */
Damien Lespiau40935612014-10-29 11:16:59 +0000437bool intel_pipe_has_type(struct intel_crtc *crtc, enum intel_output_type type)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300438{
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300439 struct drm_device *dev = crtc->base.dev;
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300440 struct intel_encoder *encoder;
441
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +0300442 for_each_encoder_on_crtc(dev, &crtc->base, encoder)
Paulo Zanonie0638cd2013-09-24 13:52:54 -0300443 if (encoder->type == type)
444 return true;
445
446 return false;
447}
448
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200449/**
450 * Returns whether any output on the specified pipe will have the specified
451 * type after a staged modeset is complete, i.e., the same as
452 * intel_pipe_has_type() but looking at encoder->new_crtc instead of
453 * encoder->crtc.
454 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200455static bool intel_pipe_will_have_type(const struct intel_crtc_state *crtc_state,
456 int type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200457{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200458 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300459 struct drm_connector *connector;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200460 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200461 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200462 int i, num_connectors = 0;
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200463
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +0300464 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200465 if (connector_state->crtc != crtc_state->base.crtc)
466 continue;
467
468 num_connectors++;
469
470 encoder = to_intel_encoder(connector_state->best_encoder);
471 if (encoder->type == type)
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200472 return true;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200473 }
474
475 WARN_ON(num_connectors == 0);
Ander Conselvan de Oliveirad0737e12014-10-29 11:32:30 +0200476
477 return false;
478}
479
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200480static const intel_limit_t *
481intel_ironlake_limit(struct intel_crtc_state *crtc_state, int refclk)
Zhenyu Wang2c072452009-06-05 15:38:42 +0800482{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200483 struct drm_device *dev = crtc_state->base.crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800484 const intel_limit_t *limit;
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800485
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200486 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100487 if (intel_is_dual_link_lvds(dev)) {
Chris Wilson1b894b52010-12-14 20:04:54 +0000488 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800489 limit = &intel_limits_ironlake_dual_lvds_100m;
490 else
491 limit = &intel_limits_ironlake_dual_lvds;
492 } else {
Chris Wilson1b894b52010-12-14 20:04:54 +0000493 if (refclk == 100000)
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800494 limit = &intel_limits_ironlake_single_lvds_100m;
495 else
496 limit = &intel_limits_ironlake_single_lvds;
497 }
Daniel Vetterc6bb3532013-04-19 11:14:33 +0200498 } else
Zhenyu Wangb91ad0e2010-02-05 09:14:17 +0800499 limit = &intel_limits_ironlake_dac;
Zhenyu Wang2c072452009-06-05 15:38:42 +0800500
501 return limit;
502}
503
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200504static const intel_limit_t *
505intel_g4x_limit(struct intel_crtc_state *crtc_state)
Ma Ling044c7c42009-03-18 20:13:23 +0800506{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200507 struct drm_device *dev = crtc_state->base.crtc->dev;
Ma Ling044c7c42009-03-18 20:13:23 +0800508 const intel_limit_t *limit;
509
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200510 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100511 if (intel_is_dual_link_lvds(dev))
Keith Packarde4b36692009-06-05 19:22:17 -0700512 limit = &intel_limits_g4x_dual_channel_lvds;
Ma Ling044c7c42009-03-18 20:13:23 +0800513 else
Keith Packarde4b36692009-06-05 19:22:17 -0700514 limit = &intel_limits_g4x_single_channel_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200515 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI) ||
516 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_ANALOG)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700517 limit = &intel_limits_g4x_hdmi;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200518 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO)) {
Keith Packarde4b36692009-06-05 19:22:17 -0700519 limit = &intel_limits_g4x_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800520 } else /* The option is for other outputs */
Keith Packarde4b36692009-06-05 19:22:17 -0700521 limit = &intel_limits_i9xx_sdvo;
Ma Ling044c7c42009-03-18 20:13:23 +0800522
523 return limit;
524}
525
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200526static const intel_limit_t *
527intel_limit(struct intel_crtc_state *crtc_state, int refclk)
Jesse Barnes79e53942008-11-07 14:24:08 -0800528{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200529 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800530 const intel_limit_t *limit;
531
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200532 if (IS_BROXTON(dev))
533 limit = &intel_limits_bxt;
534 else if (HAS_PCH_SPLIT(dev))
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200535 limit = intel_ironlake_limit(crtc_state, refclk);
Zhenyu Wang2c072452009-06-05 15:38:42 +0800536 else if (IS_G4X(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200537 limit = intel_g4x_limit(crtc_state);
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500538 } else if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200539 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500540 limit = &intel_limits_pineview_lvds;
Shaohua Li21778322009-02-23 15:19:16 +0800541 else
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500542 limit = &intel_limits_pineview_sdvo;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300543 } else if (IS_CHERRYVIEW(dev)) {
544 limit = &intel_limits_chv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700545 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjälädc730512013-09-24 21:26:30 +0300546 limit = &intel_limits_vlv;
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100547 } else if (!IS_GEN2(dev)) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200548 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Chris Wilsona6c45cf2010-09-17 00:32:17 +0100549 limit = &intel_limits_i9xx_lvds;
550 else
551 limit = &intel_limits_i9xx_sdvo;
Jesse Barnes79e53942008-11-07 14:24:08 -0800552 } else {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200553 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Keith Packarde4b36692009-06-05 19:22:17 -0700554 limit = &intel_limits_i8xx_lvds;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200555 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Keith Packarde4b36692009-06-05 19:22:17 -0700556 limit = &intel_limits_i8xx_dvo;
Daniel Vetter5d536e22013-07-06 12:52:06 +0200557 else
558 limit = &intel_limits_i8xx_dac;
Jesse Barnes79e53942008-11-07 14:24:08 -0800559 }
560 return limit;
561}
562
Adam Jacksonf2b115e2009-12-03 17:14:42 -0500563/* m1 is reserved as 0 in Pineview, n is a ring counter */
564static void pineview_clock(int refclk, intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800565{
Shaohua Li21778322009-02-23 15:19:16 +0800566 clock->m = clock->m2 + 2;
567 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200568 if (WARN_ON(clock->n == 0 || clock->p == 0))
569 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300570 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n);
571 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Shaohua Li21778322009-02-23 15:19:16 +0800572}
573
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200574static uint32_t i9xx_dpll_compute_m(struct dpll *dpll)
575{
576 return 5 * (dpll->m1 + 2) + (dpll->m2 + 2);
577}
578
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200579static void i9xx_clock(int refclk, intel_clock_t *clock)
Shaohua Li21778322009-02-23 15:19:16 +0800580{
Daniel Vetter7429e9d2013-04-20 17:19:46 +0200581 clock->m = i9xx_dpll_compute_m(clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800582 clock->p = clock->p1 * clock->p2;
Ville Syrjäläed5ca772013-12-02 19:00:45 +0200583 if (WARN_ON(clock->n + 2 == 0 || clock->p == 0))
584 return;
Ville Syrjäläfb03ac02013-10-14 14:50:30 +0300585 clock->vco = DIV_ROUND_CLOSEST(refclk * clock->m, clock->n + 2);
586 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
Jesse Barnes79e53942008-11-07 14:24:08 -0800587}
588
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300589static void chv_clock(int refclk, intel_clock_t *clock)
590{
591 clock->m = clock->m1 * clock->m2;
592 clock->p = clock->p1 * clock->p2;
593 if (WARN_ON(clock->n == 0 || clock->p == 0))
594 return;
595 clock->vco = DIV_ROUND_CLOSEST_ULL((uint64_t)refclk * clock->m,
596 clock->n << 22);
597 clock->dot = DIV_ROUND_CLOSEST(clock->vco, clock->p);
598}
599
Jesse Barnes7c04d1d2009-02-23 15:36:40 -0800600#define INTELPllInvalid(s) do { /* DRM_DEBUG(s); */ return false; } while (0)
Jesse Barnes79e53942008-11-07 14:24:08 -0800601/**
602 * Returns whether the given set of divisors are valid for a given refclk with
603 * the given connectors.
604 */
605
Chris Wilson1b894b52010-12-14 20:04:54 +0000606static bool intel_PLL_is_valid(struct drm_device *dev,
607 const intel_limit_t *limit,
608 const intel_clock_t *clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800609{
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300610 if (clock->n < limit->n.min || limit->n.max < clock->n)
611 INTELPllInvalid("n out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800612 if (clock->p1 < limit->p1.min || limit->p1.max < clock->p1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400613 INTELPllInvalid("p1 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800614 if (clock->m2 < limit->m2.min || limit->m2.max < clock->m2)
Akshay Joshi0206e352011-08-16 15:34:10 -0400615 INTELPllInvalid("m2 out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800616 if (clock->m1 < limit->m1.min || limit->m1.max < clock->m1)
Akshay Joshi0206e352011-08-16 15:34:10 -0400617 INTELPllInvalid("m1 out of range\n");
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300618
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200619 if (!IS_PINEVIEW(dev) && !IS_VALLEYVIEW(dev) && !IS_BROXTON(dev))
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300620 if (clock->m1 <= clock->m2)
621 INTELPllInvalid("m1 <= m2\n");
622
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200623 if (!IS_VALLEYVIEW(dev) && !IS_BROXTON(dev)) {
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300624 if (clock->p < limit->p.min || limit->p.max < clock->p)
625 INTELPllInvalid("p out of range\n");
626 if (clock->m < limit->m.min || limit->m.max < clock->m)
627 INTELPllInvalid("m out of range\n");
628 }
629
Jesse Barnes79e53942008-11-07 14:24:08 -0800630 if (clock->vco < limit->vco.min || limit->vco.max < clock->vco)
Akshay Joshi0206e352011-08-16 15:34:10 -0400631 INTELPllInvalid("vco out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800632 /* XXX: We may need to be checking "Dot clock" depending on the multiplier,
633 * connector, etc., rather than just a single range.
634 */
635 if (clock->dot < limit->dot.min || limit->dot.max < clock->dot)
Akshay Joshi0206e352011-08-16 15:34:10 -0400636 INTELPllInvalid("dot out of range\n");
Jesse Barnes79e53942008-11-07 14:24:08 -0800637
638 return true;
639}
640
Ma Lingd4906092009-03-18 20:13:27 +0800641static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200642i9xx_find_best_dpll(const intel_limit_t *limit,
643 struct intel_crtc_state *crtc_state,
Sean Paulcec2f352012-01-10 15:09:36 -0800644 int target, int refclk, intel_clock_t *match_clock,
645 intel_clock_t *best_clock)
Jesse Barnes79e53942008-11-07 14:24:08 -0800646{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200647 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300648 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -0800649 intel_clock_t clock;
Jesse Barnes79e53942008-11-07 14:24:08 -0800650 int err = target;
651
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200652 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800653 /*
Daniel Vettera210b022012-11-26 17:22:08 +0100654 * For LVDS just rely on its current settings for dual-channel.
655 * We haven't figured out how to reliably set up different
656 * single/dual channel state, if we even can.
Jesse Barnes79e53942008-11-07 14:24:08 -0800657 */
Daniel Vetter1974cad2012-11-26 17:22:09 +0100658 if (intel_is_dual_link_lvds(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -0800659 clock.p2 = limit->p2.p2_fast;
660 else
661 clock.p2 = limit->p2.p2_slow;
662 } else {
663 if (target < limit->p2.dot_limit)
664 clock.p2 = limit->p2.p2_slow;
665 else
666 clock.p2 = limit->p2.p2_fast;
667 }
668
Akshay Joshi0206e352011-08-16 15:34:10 -0400669 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnes79e53942008-11-07 14:24:08 -0800670
Zhao Yakui42158662009-11-20 11:24:18 +0800671 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
672 clock.m1++) {
673 for (clock.m2 = limit->m2.min;
674 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterc0efc382013-06-03 20:56:24 +0200675 if (clock.m2 >= clock.m1)
Zhao Yakui42158662009-11-20 11:24:18 +0800676 break;
677 for (clock.n = limit->n.min;
678 clock.n <= limit->n.max; clock.n++) {
679 for (clock.p1 = limit->p1.min;
680 clock.p1 <= limit->p1.max; clock.p1++) {
Jesse Barnes79e53942008-11-07 14:24:08 -0800681 int this_err;
682
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200683 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000684 if (!intel_PLL_is_valid(dev, limit,
685 &clock))
Jesse Barnes79e53942008-11-07 14:24:08 -0800686 continue;
Sean Paulcec2f352012-01-10 15:09:36 -0800687 if (match_clock &&
688 clock.p != match_clock->p)
689 continue;
Jesse Barnes79e53942008-11-07 14:24:08 -0800690
691 this_err = abs(clock.dot - target);
692 if (this_err < err) {
693 *best_clock = clock;
694 err = this_err;
695 }
696 }
697 }
698 }
699 }
700
701 return (err != target);
702}
703
Ma Lingd4906092009-03-18 20:13:27 +0800704static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200705pnv_find_best_dpll(const intel_limit_t *limit,
706 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200707 int target, int refclk, intel_clock_t *match_clock,
708 intel_clock_t *best_clock)
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200709{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200710 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300711 struct drm_device *dev = crtc->base.dev;
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200712 intel_clock_t clock;
713 int err = target;
714
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200715 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200716 /*
717 * For LVDS just rely on its current settings for dual-channel.
718 * We haven't figured out how to reliably set up different
719 * single/dual channel state, if we even can.
720 */
721 if (intel_is_dual_link_lvds(dev))
722 clock.p2 = limit->p2.p2_fast;
723 else
724 clock.p2 = limit->p2.p2_slow;
725 } else {
726 if (target < limit->p2.dot_limit)
727 clock.p2 = limit->p2.p2_slow;
728 else
729 clock.p2 = limit->p2.p2_fast;
730 }
731
732 memset(best_clock, 0, sizeof(*best_clock));
733
734 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max;
735 clock.m1++) {
736 for (clock.m2 = limit->m2.min;
737 clock.m2 <= limit->m2.max; clock.m2++) {
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200738 for (clock.n = limit->n.min;
739 clock.n <= limit->n.max; clock.n++) {
740 for (clock.p1 = limit->p1.min;
741 clock.p1 <= limit->p1.max; clock.p1++) {
742 int this_err;
743
744 pineview_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -0800745 if (!intel_PLL_is_valid(dev, limit,
746 &clock))
747 continue;
748 if (match_clock &&
749 clock.p != match_clock->p)
750 continue;
751
752 this_err = abs(clock.dot - target);
753 if (this_err < err) {
754 *best_clock = clock;
755 err = this_err;
756 }
757 }
758 }
759 }
760 }
761
762 return (err != target);
763}
764
Ma Lingd4906092009-03-18 20:13:27 +0800765static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200766g4x_find_best_dpll(const intel_limit_t *limit,
767 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200768 int target, int refclk, intel_clock_t *match_clock,
769 intel_clock_t *best_clock)
Ma Lingd4906092009-03-18 20:13:27 +0800770{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200771 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300772 struct drm_device *dev = crtc->base.dev;
Ma Lingd4906092009-03-18 20:13:27 +0800773 intel_clock_t clock;
774 int max_n;
775 bool found;
Adam Jackson6ba770d2010-07-02 16:43:30 -0400776 /* approximately equals target * 0.00585 */
777 int err_most = (target >> 8) + (target >> 9);
Ma Lingd4906092009-03-18 20:13:27 +0800778 found = false;
779
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200780 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vetter1974cad2012-11-26 17:22:09 +0100781 if (intel_is_dual_link_lvds(dev))
Ma Lingd4906092009-03-18 20:13:27 +0800782 clock.p2 = limit->p2.p2_fast;
783 else
784 clock.p2 = limit->p2.p2_slow;
785 } else {
786 if (target < limit->p2.dot_limit)
787 clock.p2 = limit->p2.p2_slow;
788 else
789 clock.p2 = limit->p2.p2_fast;
790 }
791
792 memset(best_clock, 0, sizeof(*best_clock));
793 max_n = limit->n.max;
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200794 /* based on hardware requirement, prefer smaller n to precision */
Ma Lingd4906092009-03-18 20:13:27 +0800795 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Gilles Espinassef77f13e2010-03-29 15:41:47 +0200796 /* based on hardware requirement, prefere larger m1,m2 */
Ma Lingd4906092009-03-18 20:13:27 +0800797 for (clock.m1 = limit->m1.max;
798 clock.m1 >= limit->m1.min; clock.m1--) {
799 for (clock.m2 = limit->m2.max;
800 clock.m2 >= limit->m2.min; clock.m2--) {
801 for (clock.p1 = limit->p1.max;
802 clock.p1 >= limit->p1.min; clock.p1--) {
803 int this_err;
804
Daniel Vetterac58c3f2013-06-01 17:16:17 +0200805 i9xx_clock(refclk, &clock);
Chris Wilson1b894b52010-12-14 20:04:54 +0000806 if (!intel_PLL_is_valid(dev, limit,
807 &clock))
Ma Lingd4906092009-03-18 20:13:27 +0800808 continue;
Chris Wilson1b894b52010-12-14 20:04:54 +0000809
810 this_err = abs(clock.dot - target);
Ma Lingd4906092009-03-18 20:13:27 +0800811 if (this_err < err_most) {
812 *best_clock = clock;
813 err_most = this_err;
814 max_n = clock.n;
815 found = true;
816 }
817 }
818 }
819 }
820 }
Zhenyu Wang2c072452009-06-05 15:38:42 +0800821 return found;
822}
Ma Lingd4906092009-03-18 20:13:27 +0800823
Imre Deakd5dd62b2015-03-17 11:40:03 +0200824/*
825 * Check if the calculated PLL configuration is more optimal compared to the
826 * best configuration and error found so far. Return the calculated error.
827 */
828static bool vlv_PLL_is_optimal(struct drm_device *dev, int target_freq,
829 const intel_clock_t *calculated_clock,
830 const intel_clock_t *best_clock,
831 unsigned int best_error_ppm,
832 unsigned int *error_ppm)
833{
Imre Deak9ca3ba02015-03-17 11:40:05 +0200834 /*
835 * For CHV ignore the error and consider only the P value.
836 * Prefer a bigger P value based on HW requirements.
837 */
838 if (IS_CHERRYVIEW(dev)) {
839 *error_ppm = 0;
840
841 return calculated_clock->p > best_clock->p;
842 }
843
Imre Deak24be4e42015-03-17 11:40:04 +0200844 if (WARN_ON_ONCE(!target_freq))
845 return false;
846
Imre Deakd5dd62b2015-03-17 11:40:03 +0200847 *error_ppm = div_u64(1000000ULL *
848 abs(target_freq - calculated_clock->dot),
849 target_freq);
850 /*
851 * Prefer a better P value over a better (smaller) error if the error
852 * is small. Ensure this preference for future configurations too by
853 * setting the error to 0.
854 */
855 if (*error_ppm < 100 && calculated_clock->p > best_clock->p) {
856 *error_ppm = 0;
857
858 return true;
859 }
860
861 return *error_ppm + 10 < best_error_ppm;
862}
863
Zhenyu Wang2c072452009-06-05 15:38:42 +0800864static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200865vlv_find_best_dpll(const intel_limit_t *limit,
866 struct intel_crtc_state *crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +0200867 int target, int refclk, intel_clock_t *match_clock,
868 intel_clock_t *best_clock)
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700869{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200870 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300871 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300872 intel_clock_t clock;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300873 unsigned int bestppm = 1000000;
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300874 /* min update 19.2 MHz */
875 int max_n = min(limit->n.max, refclk / 19200);
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300876 bool found = false;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700877
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300878 target *= 5; /* fast clock */
879
880 memset(best_clock, 0, sizeof(*best_clock));
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700881
882 /* based on hardware requirement, prefer smaller n to precision */
Ville Syrjälä27e639b2013-09-24 21:26:24 +0300883 for (clock.n = limit->n.min; clock.n <= max_n; clock.n++) {
Ville Syrjälä811bbf02013-09-24 21:26:25 +0300884 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
Ville Syrjälä889059d2013-09-24 21:26:27 +0300885 for (clock.p2 = limit->p2.p2_fast; clock.p2 >= limit->p2.p2_slow;
Ville Syrjäläc1a9ae42013-09-24 21:26:23 +0300886 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300887 clock.p = clock.p1 * clock.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700888 /* based on hardware requirement, prefer bigger m1,m2 values */
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300889 for (clock.m1 = limit->m1.min; clock.m1 <= limit->m1.max; clock.m1++) {
Imre Deakd5dd62b2015-03-17 11:40:03 +0200890 unsigned int ppm;
Ville Syrjälä69e4f9002013-09-24 21:26:20 +0300891
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300892 clock.m2 = DIV_ROUND_CLOSEST(target * clock.p * clock.n,
893 refclk * clock.m1);
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300894
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300895 vlv_clock(refclk, &clock);
896
Ville Syrjäläf01b7962013-09-27 16:55:49 +0300897 if (!intel_PLL_is_valid(dev, limit,
898 &clock))
Ville Syrjälä43b0ac52013-09-24 21:26:18 +0300899 continue;
900
Imre Deakd5dd62b2015-03-17 11:40:03 +0200901 if (!vlv_PLL_is_optimal(dev, target,
902 &clock,
903 best_clock,
904 bestppm, &ppm))
905 continue;
Ville Syrjälä6b4bf1c2013-09-27 16:54:19 +0300906
Imre Deakd5dd62b2015-03-17 11:40:03 +0200907 *best_clock = clock;
908 bestppm = ppm;
909 found = true;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700910 }
911 }
912 }
913 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700914
Ville Syrjälä49e497e2013-09-24 21:26:31 +0300915 return found;
Jesse Barnesa0c4da242012-06-15 11:55:13 -0700916}
Keith Packarda4fc5ed2009-04-07 16:16:42 -0700917
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300918static bool
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200919chv_find_best_dpll(const intel_limit_t *limit,
920 struct intel_crtc_state *crtc_state,
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300921 int target, int refclk, intel_clock_t *match_clock,
922 intel_clock_t *best_clock)
923{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +0200924 struct intel_crtc *crtc = to_intel_crtc(crtc_state->base.crtc);
Ander Conselvan de Oliveiraa919ff12014-10-20 13:46:43 +0300925 struct drm_device *dev = crtc->base.dev;
Imre Deak9ca3ba02015-03-17 11:40:05 +0200926 unsigned int best_error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300927 intel_clock_t clock;
928 uint64_t m2;
929 int found = false;
930
931 memset(best_clock, 0, sizeof(*best_clock));
Imre Deak9ca3ba02015-03-17 11:40:05 +0200932 best_error_ppm = 1000000;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300933
934 /*
935 * Based on hardware doc, the n always set to 1, and m1 always
936 * set to 2. If requires to support 200Mhz refclk, we need to
937 * revisit this because n may not 1 anymore.
938 */
939 clock.n = 1, clock.m1 = 2;
940 target *= 5; /* fast clock */
941
942 for (clock.p1 = limit->p1.max; clock.p1 >= limit->p1.min; clock.p1--) {
943 for (clock.p2 = limit->p2.p2_fast;
944 clock.p2 >= limit->p2.p2_slow;
945 clock.p2 -= clock.p2 > 10 ? 2 : 1) {
Imre Deak9ca3ba02015-03-17 11:40:05 +0200946 unsigned int error_ppm;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300947
948 clock.p = clock.p1 * clock.p2;
949
950 m2 = DIV_ROUND_CLOSEST_ULL(((uint64_t)target * clock.p *
951 clock.n) << 22, refclk * clock.m1);
952
953 if (m2 > INT_MAX/clock.m1)
954 continue;
955
956 clock.m2 = m2;
957
958 chv_clock(refclk, &clock);
959
960 if (!intel_PLL_is_valid(dev, limit, &clock))
961 continue;
962
Imre Deak9ca3ba02015-03-17 11:40:05 +0200963 if (!vlv_PLL_is_optimal(dev, target, &clock, best_clock,
964 best_error_ppm, &error_ppm))
965 continue;
966
967 *best_clock = clock;
968 best_error_ppm = error_ppm;
969 found = true;
Chon Ming Leeef9348c2014-04-09 13:28:18 +0300970 }
971 }
972
973 return found;
974}
975
Imre Deak5ab7b0b2015-03-06 03:29:25 +0200976bool bxt_find_best_dpll(struct intel_crtc_state *crtc_state, int target_clock,
977 intel_clock_t *best_clock)
978{
979 int refclk = i9xx_get_refclk(crtc_state, 0);
980
981 return chv_find_best_dpll(intel_limit(crtc_state, refclk), crtc_state,
982 target_clock, refclk, NULL, best_clock);
983}
984
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300985bool intel_crtc_active(struct drm_crtc *crtc)
986{
987 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
988
989 /* Be paranoid as we can arrive here with only partial
990 * state retrieved from the hardware during setup.
991 *
Damien Lespiau241bfc32013-09-25 16:45:37 +0100992 * We can ditch the adjusted_mode.crtc_clock check as soon
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300993 * as Haswell has gained clock readout/fastboot support.
994 *
Dave Airlie66e514c2014-04-03 07:51:54 +1000995 * We can ditch the crtc->primary->fb check as soon as we can
Ville Syrjälä20ddf662013-09-04 18:25:25 +0300996 * properly reconstruct framebuffers.
Matt Roperc3d1f432015-03-09 10:19:23 -0700997 *
998 * FIXME: The intel_crtc->active here should be switched to
999 * crtc->state->active once we have proper CRTC states wired up
1000 * for atomic.
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001001 */
Matt Roperc3d1f432015-03-09 10:19:23 -07001002 return intel_crtc->active && crtc->primary->state->fb &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001003 intel_crtc->config->base.adjusted_mode.crtc_clock;
Ville Syrjälä20ddf662013-09-04 18:25:25 +03001004}
1005
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001006enum transcoder intel_pipe_to_cpu_transcoder(struct drm_i915_private *dev_priv,
1007 enum pipe pipe)
1008{
1009 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
1010 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
1011
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001012 return intel_crtc->config->cpu_transcoder;
Paulo Zanonia5c961d2012-10-24 15:59:34 -02001013}
1014
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001015static bool pipe_dsl_stopped(struct drm_device *dev, enum pipe pipe)
1016{
1017 struct drm_i915_private *dev_priv = dev->dev_private;
1018 u32 reg = PIPEDSL(pipe);
1019 u32 line1, line2;
1020 u32 line_mask;
1021
1022 if (IS_GEN2(dev))
1023 line_mask = DSL_LINEMASK_GEN2;
1024 else
1025 line_mask = DSL_LINEMASK_GEN3;
1026
1027 line1 = I915_READ(reg) & line_mask;
1028 mdelay(5);
1029 line2 = I915_READ(reg) & line_mask;
1030
1031 return line1 == line2;
1032}
1033
Keith Packardab7ad7f2010-10-03 00:33:06 -07001034/*
1035 * intel_wait_for_pipe_off - wait for pipe to turn off
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001036 * @crtc: crtc whose pipe to wait for
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001037 *
1038 * After disabling a pipe, we can't wait for vblank in the usual way,
1039 * spinning on the vblank interrupt status bit, since we won't actually
1040 * see an interrupt when the pipe is disabled.
1041 *
Keith Packardab7ad7f2010-10-03 00:33:06 -07001042 * On Gen4 and above:
1043 * wait for the pipe register state bit to turn off
1044 *
1045 * Otherwise:
1046 * wait for the display line value to settle (it usually
1047 * ends up stopping at the start of the next frame).
Chris Wilson58e10eb2010-10-03 10:56:11 +01001048 *
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001049 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001050static void intel_wait_for_pipe_off(struct intel_crtc *crtc)
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001051{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001052 struct drm_device *dev = crtc->base.dev;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001053 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001054 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03001055 enum pipe pipe = crtc->pipe;
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001056
Keith Packardab7ad7f2010-10-03 00:33:06 -07001057 if (INTEL_INFO(dev)->gen >= 4) {
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001058 int reg = PIPECONF(cpu_transcoder);
Jesse Barnes9d0498a2010-08-18 13:20:54 -07001059
Keith Packardab7ad7f2010-10-03 00:33:06 -07001060 /* Wait for the Pipe State to go off */
Chris Wilson58e10eb2010-10-03 10:56:11 +01001061 if (wait_for((I915_READ(reg) & I965_PIPECONF_ACTIVE) == 0,
1062 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001063 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001064 } else {
Keith Packardab7ad7f2010-10-03 00:33:06 -07001065 /* Wait for the display line to settle */
Ville Syrjäläfbf49ea2013-10-11 14:21:31 +03001066 if (wait_for(pipe_dsl_stopped(dev, pipe), 100))
Daniel Vetter284637d2012-07-09 09:51:57 +02001067 WARN(1, "pipe_off wait timed out\n");
Keith Packardab7ad7f2010-10-03 00:33:06 -07001068 }
Jesse Barnes79e53942008-11-07 14:24:08 -08001069}
1070
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001071/*
1072 * ibx_digital_port_connected - is the specified port connected?
1073 * @dev_priv: i915 private structure
1074 * @port: the port to test
1075 *
1076 * Returns true if @port is connected, false otherwise.
1077 */
1078bool ibx_digital_port_connected(struct drm_i915_private *dev_priv,
1079 struct intel_digital_port *port)
1080{
1081 u32 bit;
1082
Damien Lespiauc36346e2012-12-13 16:09:03 +00001083 if (HAS_PCH_IBX(dev_priv->dev)) {
Robin Schroereba905b2014-05-18 02:24:50 +02001084 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001085 case PORT_B:
1086 bit = SDE_PORTB_HOTPLUG;
1087 break;
1088 case PORT_C:
1089 bit = SDE_PORTC_HOTPLUG;
1090 break;
1091 case PORT_D:
1092 bit = SDE_PORTD_HOTPLUG;
1093 break;
1094 default:
1095 return true;
1096 }
1097 } else {
Robin Schroereba905b2014-05-18 02:24:50 +02001098 switch (port->port) {
Damien Lespiauc36346e2012-12-13 16:09:03 +00001099 case PORT_B:
1100 bit = SDE_PORTB_HOTPLUG_CPT;
1101 break;
1102 case PORT_C:
1103 bit = SDE_PORTC_HOTPLUG_CPT;
1104 break;
1105 case PORT_D:
1106 bit = SDE_PORTD_HOTPLUG_CPT;
1107 break;
1108 default:
1109 return true;
1110 }
Damien Lespiaub0ea7d32012-12-13 16:09:00 +00001111 }
1112
1113 return I915_READ(SDEISR) & bit;
1114}
1115
Jesse Barnesb24e7172011-01-04 15:09:30 -08001116static const char *state_string(bool enabled)
1117{
1118 return enabled ? "on" : "off";
1119}
1120
1121/* Only for pre-ILK configs */
Daniel Vetter55607e82013-06-16 21:42:39 +02001122void assert_pll(struct drm_i915_private *dev_priv,
1123 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001124{
1125 int reg;
1126 u32 val;
1127 bool cur_state;
1128
1129 reg = DPLL(pipe);
1130 val = I915_READ(reg);
1131 cur_state = !!(val & DPLL_VCO_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001132 I915_STATE_WARN(cur_state != state,
Jesse Barnesb24e7172011-01-04 15:09:30 -08001133 "PLL state assertion failure (expected %s, current %s)\n",
1134 state_string(state), state_string(cur_state));
1135}
Jesse Barnesb24e7172011-01-04 15:09:30 -08001136
Jani Nikula23538ef2013-08-27 15:12:22 +03001137/* XXX: the dsi pll is shared between MIPI DSI ports */
1138static void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
1139{
1140 u32 val;
1141 bool cur_state;
1142
Ville Syrjäläa5805162015-05-26 20:42:30 +03001143 mutex_lock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001144 val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03001145 mutex_unlock(&dev_priv->sb_lock);
Jani Nikula23538ef2013-08-27 15:12:22 +03001146
1147 cur_state = val & DSI_PLL_VCO_EN;
Rob Clarke2c719b2014-12-15 13:56:32 -05001148 I915_STATE_WARN(cur_state != state,
Jani Nikula23538ef2013-08-27 15:12:22 +03001149 "DSI PLL state assertion failure (expected %s, current %s)\n",
1150 state_string(state), state_string(cur_state));
1151}
1152#define assert_dsi_pll_enabled(d) assert_dsi_pll(d, true)
1153#define assert_dsi_pll_disabled(d) assert_dsi_pll(d, false)
1154
Daniel Vetter55607e82013-06-16 21:42:39 +02001155struct intel_shared_dpll *
Daniel Vettere2b78262013-06-07 23:10:03 +02001156intel_crtc_to_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes040484a2011-01-03 12:14:26 -08001157{
Daniel Vettere2b78262013-06-07 23:10:03 +02001158 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
1159
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001160 if (crtc->config->shared_dpll < 0)
Daniel Vettere2b78262013-06-07 23:10:03 +02001161 return NULL;
1162
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001163 return &dev_priv->shared_dplls[crtc->config->shared_dpll];
Daniel Vettere2b78262013-06-07 23:10:03 +02001164}
1165
Jesse Barnesb24e7172011-01-04 15:09:30 -08001166/* For ILK+ */
Daniel Vetter55607e82013-06-16 21:42:39 +02001167void assert_shared_dpll(struct drm_i915_private *dev_priv,
1168 struct intel_shared_dpll *pll,
1169 bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001170{
Jesse Barnes040484a2011-01-03 12:14:26 -08001171 bool cur_state;
Daniel Vetter53589012013-06-05 13:34:16 +02001172 struct intel_dpll_hw_state hw_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001173
Chris Wilson92b27b02012-05-20 18:10:50 +01001174 if (WARN (!pll,
Daniel Vetter46edb022013-06-05 13:34:12 +02001175 "asserting DPLL %s with no DPLL\n", state_string(state)))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001176 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001177
Daniel Vetter53589012013-06-05 13:34:16 +02001178 cur_state = pll->get_hw_state(dev_priv, pll, &hw_state);
Rob Clarke2c719b2014-12-15 13:56:32 -05001179 I915_STATE_WARN(cur_state != state,
Daniel Vetter53589012013-06-05 13:34:16 +02001180 "%s assertion failure (expected %s, current %s)\n",
1181 pll->name, state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001182}
Jesse Barnes040484a2011-01-03 12:14:26 -08001183
1184static void assert_fdi_tx(struct drm_i915_private *dev_priv,
1185 enum pipe pipe, bool state)
1186{
1187 int reg;
1188 u32 val;
1189 bool cur_state;
Paulo Zanoniad80a812012-10-24 16:06:19 -02001190 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1191 pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08001192
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001193 if (HAS_DDI(dev_priv->dev)) {
1194 /* DDI does not have a specific FDI_TX register */
Paulo Zanoniad80a812012-10-24 16:06:19 -02001195 reg = TRANS_DDI_FUNC_CTL(cpu_transcoder);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001196 val = I915_READ(reg);
Paulo Zanoniad80a812012-10-24 16:06:19 -02001197 cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001198 } else {
1199 reg = FDI_TX_CTL(pipe);
1200 val = I915_READ(reg);
1201 cur_state = !!(val & FDI_TX_ENABLE);
1202 }
Rob Clarke2c719b2014-12-15 13:56:32 -05001203 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001204 "FDI TX state assertion failure (expected %s, current %s)\n",
1205 state_string(state), state_string(cur_state));
1206}
1207#define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
1208#define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
1209
1210static void assert_fdi_rx(struct drm_i915_private *dev_priv,
1211 enum pipe pipe, bool state)
1212{
1213 int reg;
1214 u32 val;
1215 bool cur_state;
1216
Paulo Zanonid63fa0d2012-11-20 13:27:35 -02001217 reg = FDI_RX_CTL(pipe);
1218 val = I915_READ(reg);
1219 cur_state = !!(val & FDI_RX_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001220 I915_STATE_WARN(cur_state != state,
Jesse Barnes040484a2011-01-03 12:14:26 -08001221 "FDI RX state assertion failure (expected %s, current %s)\n",
1222 state_string(state), state_string(cur_state));
1223}
1224#define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
1225#define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
1226
1227static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
1228 enum pipe pipe)
1229{
1230 int reg;
1231 u32 val;
1232
1233 /* ILK FDI PLL is always enabled */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001234 if (INTEL_INFO(dev_priv->dev)->gen == 5)
Jesse Barnes040484a2011-01-03 12:14:26 -08001235 return;
1236
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001237 /* On Haswell, DDI ports are responsible for the FDI PLL setup */
Paulo Zanoniaffa9352012-11-23 15:30:39 -02001238 if (HAS_DDI(dev_priv->dev))
Eugeni Dodonovbf507ef2012-05-09 15:37:18 -03001239 return;
1240
Jesse Barnes040484a2011-01-03 12:14:26 -08001241 reg = FDI_TX_CTL(pipe);
1242 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001243 I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
Jesse Barnes040484a2011-01-03 12:14:26 -08001244}
1245
Daniel Vetter55607e82013-06-16 21:42:39 +02001246void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
1247 enum pipe pipe, bool state)
Jesse Barnes040484a2011-01-03 12:14:26 -08001248{
1249 int reg;
1250 u32 val;
Daniel Vetter55607e82013-06-16 21:42:39 +02001251 bool cur_state;
Jesse Barnes040484a2011-01-03 12:14:26 -08001252
1253 reg = FDI_RX_CTL(pipe);
1254 val = I915_READ(reg);
Daniel Vetter55607e82013-06-16 21:42:39 +02001255 cur_state = !!(val & FDI_RX_PLL_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001256 I915_STATE_WARN(cur_state != state,
Daniel Vetter55607e82013-06-16 21:42:39 +02001257 "FDI RX PLL assertion failure (expected %s, current %s)\n",
1258 state_string(state), state_string(cur_state));
Jesse Barnes040484a2011-01-03 12:14:26 -08001259}
1260
Daniel Vetterb680c372014-09-19 18:27:27 +02001261void assert_panel_unlocked(struct drm_i915_private *dev_priv,
1262 enum pipe pipe)
Jesse Barnesea0760c2011-01-04 15:09:32 -08001263{
Jani Nikulabedd4db2014-08-22 15:04:13 +03001264 struct drm_device *dev = dev_priv->dev;
1265 int pp_reg;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001266 u32 val;
1267 enum pipe panel_pipe = PIPE_A;
Thomas Jarosch0de3b482011-08-25 15:37:45 +02001268 bool locked = true;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001269
Jani Nikulabedd4db2014-08-22 15:04:13 +03001270 if (WARN_ON(HAS_DDI(dev)))
1271 return;
1272
1273 if (HAS_PCH_SPLIT(dev)) {
1274 u32 port_sel;
1275
Jesse Barnesea0760c2011-01-04 15:09:32 -08001276 pp_reg = PCH_PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001277 port_sel = I915_READ(PCH_PP_ON_DELAYS) & PANEL_PORT_SELECT_MASK;
1278
1279 if (port_sel == PANEL_PORT_SELECT_LVDS &&
1280 I915_READ(PCH_LVDS) & LVDS_PIPEB_SELECT)
1281 panel_pipe = PIPE_B;
1282 /* XXX: else fix for eDP */
1283 } else if (IS_VALLEYVIEW(dev)) {
1284 /* presumably write lock depends on pipe, not port select */
1285 pp_reg = VLV_PIPE_PP_CONTROL(pipe);
1286 panel_pipe = pipe;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001287 } else {
1288 pp_reg = PP_CONTROL;
Jani Nikulabedd4db2014-08-22 15:04:13 +03001289 if (I915_READ(LVDS) & LVDS_PIPEB_SELECT)
1290 panel_pipe = PIPE_B;
Jesse Barnesea0760c2011-01-04 15:09:32 -08001291 }
1292
1293 val = I915_READ(pp_reg);
1294 if (!(val & PANEL_POWER_ON) ||
Jani Nikulaec49ba22014-08-21 15:06:25 +03001295 ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
Jesse Barnesea0760c2011-01-04 15:09:32 -08001296 locked = false;
1297
Rob Clarke2c719b2014-12-15 13:56:32 -05001298 I915_STATE_WARN(panel_pipe == pipe && locked,
Jesse Barnesea0760c2011-01-04 15:09:32 -08001299 "panel assertion failure, pipe %c regs locked\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001300 pipe_name(pipe));
Jesse Barnesea0760c2011-01-04 15:09:32 -08001301}
1302
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001303static void assert_cursor(struct drm_i915_private *dev_priv,
1304 enum pipe pipe, bool state)
1305{
1306 struct drm_device *dev = dev_priv->dev;
1307 bool cur_state;
1308
Paulo Zanonid9d82082014-02-27 16:30:56 -03001309 if (IS_845G(dev) || IS_I865G(dev))
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001310 cur_state = I915_READ(_CURACNTR) & CURSOR_ENABLE;
Paulo Zanonid9d82082014-02-27 16:30:56 -03001311 else
Ville Syrjälä5efb3e22014-04-09 13:28:53 +03001312 cur_state = I915_READ(CURCNTR(pipe)) & CURSOR_MODE;
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001313
Rob Clarke2c719b2014-12-15 13:56:32 -05001314 I915_STATE_WARN(cur_state != state,
Jani Nikula93ce0ba2013-09-13 11:03:08 +03001315 "cursor on pipe %c assertion failure (expected %s, current %s)\n",
1316 pipe_name(pipe), state_string(state), state_string(cur_state));
1317}
1318#define assert_cursor_enabled(d, p) assert_cursor(d, p, true)
1319#define assert_cursor_disabled(d, p) assert_cursor(d, p, false)
1320
Jesse Barnesb840d907f2011-12-13 13:19:38 -08001321void assert_pipe(struct drm_i915_private *dev_priv,
1322 enum pipe pipe, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001323{
1324 int reg;
1325 u32 val;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001326 bool cur_state;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02001327 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
1328 pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08001329
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001330 /* if we need the pipe quirk it must be always on */
1331 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1332 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetter8e636782012-01-22 01:36:48 +01001333 state = true;
1334
Daniel Vetterf458ebb2014-09-30 10:56:39 +02001335 if (!intel_display_power_is_enabled(dev_priv,
Paulo Zanonib97186f2013-05-03 12:15:36 -03001336 POWER_DOMAIN_TRANSCODER(cpu_transcoder))) {
Paulo Zanoni69310162013-01-29 16:35:19 -02001337 cur_state = false;
1338 } else {
1339 reg = PIPECONF(cpu_transcoder);
1340 val = I915_READ(reg);
1341 cur_state = !!(val & PIPECONF_ENABLE);
1342 }
1343
Rob Clarke2c719b2014-12-15 13:56:32 -05001344 I915_STATE_WARN(cur_state != state,
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001345 "pipe %c assertion failure (expected %s, current %s)\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001346 pipe_name(pipe), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001347}
1348
Chris Wilson931872f2012-01-16 23:01:13 +00001349static void assert_plane(struct drm_i915_private *dev_priv,
1350 enum plane plane, bool state)
Jesse Barnesb24e7172011-01-04 15:09:30 -08001351{
1352 int reg;
1353 u32 val;
Chris Wilson931872f2012-01-16 23:01:13 +00001354 bool cur_state;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001355
1356 reg = DSPCNTR(plane);
1357 val = I915_READ(reg);
Chris Wilson931872f2012-01-16 23:01:13 +00001358 cur_state = !!(val & DISPLAY_PLANE_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001359 I915_STATE_WARN(cur_state != state,
Chris Wilson931872f2012-01-16 23:01:13 +00001360 "plane %c assertion failure (expected %s, current %s)\n",
1361 plane_name(plane), state_string(state), state_string(cur_state));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001362}
1363
Chris Wilson931872f2012-01-16 23:01:13 +00001364#define assert_plane_enabled(d, p) assert_plane(d, p, true)
1365#define assert_plane_disabled(d, p) assert_plane(d, p, false)
1366
Jesse Barnesb24e7172011-01-04 15:09:30 -08001367static void assert_planes_disabled(struct drm_i915_private *dev_priv,
1368 enum pipe pipe)
1369{
Ville Syrjälä653e1022013-06-04 13:49:05 +03001370 struct drm_device *dev = dev_priv->dev;
Jesse Barnesb24e7172011-01-04 15:09:30 -08001371 int reg, i;
1372 u32 val;
1373 int cur_pipe;
1374
Ville Syrjälä653e1022013-06-04 13:49:05 +03001375 /* Primary planes are fixed to pipes on gen4+ */
1376 if (INTEL_INFO(dev)->gen >= 4) {
Adam Jackson28c057942011-10-07 14:38:42 -04001377 reg = DSPCNTR(pipe);
1378 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001379 I915_STATE_WARN(val & DISPLAY_PLANE_ENABLE,
Adam Jackson28c057942011-10-07 14:38:42 -04001380 "plane %c assertion failure, should be disabled but not\n",
1381 plane_name(pipe));
Jesse Barnes19ec1352011-02-02 12:28:02 -08001382 return;
Adam Jackson28c057942011-10-07 14:38:42 -04001383 }
Jesse Barnes19ec1352011-02-02 12:28:02 -08001384
Jesse Barnesb24e7172011-01-04 15:09:30 -08001385 /* Need to check both planes against the pipe */
Damien Lespiau055e3932014-08-18 13:49:10 +01001386 for_each_pipe(dev_priv, i) {
Jesse Barnesb24e7172011-01-04 15:09:30 -08001387 reg = DSPCNTR(i);
1388 val = I915_READ(reg);
1389 cur_pipe = (val & DISPPLANE_SEL_PIPE_MASK) >>
1390 DISPPLANE_SEL_PIPE_SHIFT;
Rob Clarke2c719b2014-12-15 13:56:32 -05001391 I915_STATE_WARN((val & DISPLAY_PLANE_ENABLE) && pipe == cur_pipe,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001392 "plane %c assertion failure, should be off on pipe %c but is still active\n",
1393 plane_name(i), pipe_name(pipe));
Jesse Barnesb24e7172011-01-04 15:09:30 -08001394 }
1395}
1396
Jesse Barnes19332d72013-03-28 09:55:38 -07001397static void assert_sprites_disabled(struct drm_i915_private *dev_priv,
1398 enum pipe pipe)
1399{
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001400 struct drm_device *dev = dev_priv->dev;
Damien Lespiau1fe47782014-03-03 17:31:47 +00001401 int reg, sprite;
Jesse Barnes19332d72013-03-28 09:55:38 -07001402 u32 val;
1403
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001404 if (INTEL_INFO(dev)->gen >= 9) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001405 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001406 val = I915_READ(PLANE_CTL(pipe, sprite));
Rob Clarke2c719b2014-12-15 13:56:32 -05001407 I915_STATE_WARN(val & PLANE_CTL_ENABLE,
Damien Lespiau7feb8b82014-03-12 21:05:38 +00001408 "plane %d assertion failure, should be off on pipe %c but is still active\n",
1409 sprite, pipe_name(pipe));
1410 }
1411 } else if (IS_VALLEYVIEW(dev)) {
Damien Lespiau3bdcfc02015-02-28 14:54:09 +00001412 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +00001413 reg = SPCNTR(pipe, sprite);
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001414 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001415 I915_STATE_WARN(val & SP_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001416 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +00001417 sprite_name(pipe, sprite), pipe_name(pipe));
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001418 }
1419 } else if (INTEL_INFO(dev)->gen >= 7) {
1420 reg = SPRCTL(pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07001421 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001422 I915_STATE_WARN(val & SPRITE_ENABLE,
Ville Syrjälä06da8da2013-04-17 17:48:51 +03001423 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001424 plane_name(pipe), pipe_name(pipe));
1425 } else if (INTEL_INFO(dev)->gen >= 5) {
1426 reg = DVSCNTR(pipe);
1427 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001428 I915_STATE_WARN(val & DVS_ENABLE,
Ville Syrjälä20674ee2013-06-04 13:49:06 +03001429 "sprite %c assertion failure, should be off on pipe %c but is still active\n",
1430 plane_name(pipe), pipe_name(pipe));
Jesse Barnes19332d72013-03-28 09:55:38 -07001431 }
1432}
1433
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001434static void assert_vblank_disabled(struct drm_crtc *crtc)
1435{
Rob Clarke2c719b2014-12-15 13:56:32 -05001436 if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
Ville Syrjälä08c71e52014-08-06 14:49:45 +03001437 drm_crtc_vblank_put(crtc);
1438}
1439
Paulo Zanoni89eff4b2014-01-08 11:12:28 -02001440static void ibx_assert_pch_refclk_enabled(struct drm_i915_private *dev_priv)
Jesse Barnes92f25842011-01-04 15:09:34 -08001441{
1442 u32 val;
1443 bool enabled;
1444
Rob Clarke2c719b2014-12-15 13:56:32 -05001445 I915_STATE_WARN_ON(!(HAS_PCH_IBX(dev_priv->dev) || HAS_PCH_CPT(dev_priv->dev)));
Eugeni Dodonov9d82aa12012-05-09 15:37:17 -03001446
Jesse Barnes92f25842011-01-04 15:09:34 -08001447 val = I915_READ(PCH_DREF_CONTROL);
1448 enabled = !!(val & (DREF_SSC_SOURCE_MASK | DREF_NONSPREAD_SOURCE_MASK |
1449 DREF_SUPERSPREAD_SOURCE_MASK));
Rob Clarke2c719b2014-12-15 13:56:32 -05001450 I915_STATE_WARN(!enabled, "PCH refclk assertion failure, should be active but is disabled\n");
Jesse Barnes92f25842011-01-04 15:09:34 -08001451}
1452
Daniel Vetterab9412b2013-05-03 11:49:46 +02001453static void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
1454 enum pipe pipe)
Jesse Barnes92f25842011-01-04 15:09:34 -08001455{
1456 int reg;
1457 u32 val;
1458 bool enabled;
1459
Daniel Vetterab9412b2013-05-03 11:49:46 +02001460 reg = PCH_TRANSCONF(pipe);
Jesse Barnes92f25842011-01-04 15:09:34 -08001461 val = I915_READ(reg);
1462 enabled = !!(val & TRANS_ENABLE);
Rob Clarke2c719b2014-12-15 13:56:32 -05001463 I915_STATE_WARN(enabled,
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001464 "transcoder assertion failed, should be off on pipe %c but is still active\n",
1465 pipe_name(pipe));
Jesse Barnes92f25842011-01-04 15:09:34 -08001466}
1467
Keith Packard4e634382011-08-06 10:39:45 -07001468static bool dp_pipe_enabled(struct drm_i915_private *dev_priv,
1469 enum pipe pipe, u32 port_sel, u32 val)
Keith Packardf0575e92011-07-25 22:12:43 -07001470{
1471 if ((val & DP_PORT_EN) == 0)
1472 return false;
1473
1474 if (HAS_PCH_CPT(dev_priv->dev)) {
1475 u32 trans_dp_ctl_reg = TRANS_DP_CTL(pipe);
1476 u32 trans_dp_ctl = I915_READ(trans_dp_ctl_reg);
1477 if ((trans_dp_ctl & TRANS_DP_PORT_SEL_MASK) != port_sel)
1478 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001479 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1480 if ((val & DP_PIPE_MASK_CHV) != DP_PIPE_SELECT_CHV(pipe))
1481 return false;
Keith Packardf0575e92011-07-25 22:12:43 -07001482 } else {
1483 if ((val & DP_PIPE_MASK) != (pipe << 30))
1484 return false;
1485 }
1486 return true;
1487}
1488
Keith Packard1519b992011-08-06 10:35:34 -07001489static bool hdmi_pipe_enabled(struct drm_i915_private *dev_priv,
1490 enum pipe pipe, u32 val)
1491{
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001492 if ((val & SDVO_ENABLE) == 0)
Keith Packard1519b992011-08-06 10:35:34 -07001493 return false;
1494
1495 if (HAS_PCH_CPT(dev_priv->dev)) {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001496 if ((val & SDVO_PIPE_SEL_MASK_CPT) != SDVO_PIPE_SEL_CPT(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001497 return false;
Chon Ming Lee44f37d12014-04-09 13:28:21 +03001498 } else if (IS_CHERRYVIEW(dev_priv->dev)) {
1499 if ((val & SDVO_PIPE_SEL_MASK_CHV) != SDVO_PIPE_SEL_CHV(pipe))
1500 return false;
Keith Packard1519b992011-08-06 10:35:34 -07001501 } else {
Paulo Zanonidc0fa712013-02-19 16:21:46 -03001502 if ((val & SDVO_PIPE_SEL_MASK) != SDVO_PIPE_SEL(pipe))
Keith Packard1519b992011-08-06 10:35:34 -07001503 return false;
1504 }
1505 return true;
1506}
1507
1508static bool lvds_pipe_enabled(struct drm_i915_private *dev_priv,
1509 enum pipe pipe, u32 val)
1510{
1511 if ((val & LVDS_PORT_EN) == 0)
1512 return false;
1513
1514 if (HAS_PCH_CPT(dev_priv->dev)) {
1515 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1516 return false;
1517 } else {
1518 if ((val & LVDS_PIPE_MASK) != LVDS_PIPE(pipe))
1519 return false;
1520 }
1521 return true;
1522}
1523
1524static bool adpa_pipe_enabled(struct drm_i915_private *dev_priv,
1525 enum pipe pipe, u32 val)
1526{
1527 if ((val & ADPA_DAC_ENABLE) == 0)
1528 return false;
1529 if (HAS_PCH_CPT(dev_priv->dev)) {
1530 if ((val & PORT_TRANS_SEL_MASK) != PORT_TRANS_SEL_CPT(pipe))
1531 return false;
1532 } else {
1533 if ((val & ADPA_PIPE_SELECT_MASK) != ADPA_PIPE_SELECT(pipe))
1534 return false;
1535 }
1536 return true;
1537}
1538
Jesse Barnes291906f2011-02-02 12:28:03 -08001539static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
Keith Packardf0575e92011-07-25 22:12:43 -07001540 enum pipe pipe, int reg, u32 port_sel)
Jesse Barnes291906f2011-02-02 12:28:03 -08001541{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001542 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001543 I915_STATE_WARN(dp_pipe_enabled(dev_priv, pipe, port_sel, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001544 "PCH DP (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001545 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001546
Rob Clarke2c719b2014-12-15 13:56:32 -05001547 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & DP_PORT_EN) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001548 && (val & DP_PIPEB_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001549 "IBX PCH dp port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001550}
1551
1552static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
1553 enum pipe pipe, int reg)
1554{
Jesse Barnes47a05ec2011-02-07 13:46:40 -08001555 u32 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001556 I915_STATE_WARN(hdmi_pipe_enabled(dev_priv, pipe, val),
Adam Jackson23c99e72011-10-07 14:38:43 -04001557 "PCH HDMI (0x%08x) enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001558 reg, pipe_name(pipe));
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001559
Rob Clarke2c719b2014-12-15 13:56:32 -05001560 I915_STATE_WARN(HAS_PCH_IBX(dev_priv->dev) && (val & SDVO_ENABLE) == 0
Daniel Vetter75c5da22012-09-10 21:58:29 +02001561 && (val & SDVO_PIPE_B_SELECT),
Daniel Vetterde9a35a2012-06-05 11:03:40 +02001562 "IBX PCH hdmi port still using transcoder B\n");
Jesse Barnes291906f2011-02-02 12:28:03 -08001563}
1564
1565static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
1566 enum pipe pipe)
1567{
1568 int reg;
1569 u32 val;
Jesse Barnes291906f2011-02-02 12:28:03 -08001570
Keith Packardf0575e92011-07-25 22:12:43 -07001571 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_B, TRANS_DP_PORT_SEL_B);
1572 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_C, TRANS_DP_PORT_SEL_C);
1573 assert_pch_dp_disabled(dev_priv, pipe, PCH_DP_D, TRANS_DP_PORT_SEL_D);
Jesse Barnes291906f2011-02-02 12:28:03 -08001574
1575 reg = PCH_ADPA;
1576 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001577 I915_STATE_WARN(adpa_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001578 "PCH VGA enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001579 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001580
1581 reg = PCH_LVDS;
1582 val = I915_READ(reg);
Rob Clarke2c719b2014-12-15 13:56:32 -05001583 I915_STATE_WARN(lvds_pipe_enabled(dev_priv, pipe, val),
Jesse Barnes291906f2011-02-02 12:28:03 -08001584 "PCH LVDS enabled on transcoder %c, should be disabled\n",
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08001585 pipe_name(pipe));
Jesse Barnes291906f2011-02-02 12:28:03 -08001586
Paulo Zanonie2debe92013-02-18 19:00:27 -03001587 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIB);
1588 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMIC);
1589 assert_pch_hdmi_disabled(dev_priv, pipe, PCH_HDMID);
Jesse Barnes291906f2011-02-02 12:28:03 -08001590}
1591
Jesse Barnes40e9cf62013-10-03 11:35:46 -07001592static void intel_init_dpio(struct drm_device *dev)
1593{
1594 struct drm_i915_private *dev_priv = dev->dev_private;
1595
1596 if (!IS_VALLEYVIEW(dev))
1597 return;
1598
Chon Ming Leea09cadd2014-04-09 13:28:14 +03001599 /*
1600 * IOSF_PORT_DPIO is used for VLV x2 PHY (DP/HDMI B and C),
1601 * CHV x1 PHY (DP/HDMI D)
1602 * IOSF_PORT_DPIO_2 is used for CHV x2 PHY (DP/HDMI B and C)
1603 */
1604 if (IS_CHERRYVIEW(dev)) {
1605 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO_2;
1606 DPIO_PHY_IOSF_PORT(DPIO_PHY1) = IOSF_PORT_DPIO;
1607 } else {
1608 DPIO_PHY_IOSF_PORT(DPIO_PHY0) = IOSF_PORT_DPIO;
1609 }
Jesse Barnes5382f5f352013-12-16 16:34:24 -08001610}
1611
Ville Syrjäläd288f652014-10-28 13:20:22 +02001612static void vlv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001613 const struct intel_crtc_state *pipe_config)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001614{
Daniel Vetter426115c2013-07-11 22:13:42 +02001615 struct drm_device *dev = crtc->base.dev;
1616 struct drm_i915_private *dev_priv = dev->dev_private;
1617 int reg = DPLL(crtc->pipe);
Ville Syrjäläd288f652014-10-28 13:20:22 +02001618 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001619
Daniel Vetter426115c2013-07-11 22:13:42 +02001620 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02001621
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001622 /* No really, not for ILK+ */
Daniel Vetter87442f72013-06-06 00:52:17 +02001623 BUG_ON(!IS_VALLEYVIEW(dev_priv->dev));
1624
1625 /* PLL is protected by panel, make sure we can write it */
Jani Nikula6a9e7362014-08-22 15:06:35 +03001626 if (IS_MOBILE(dev_priv->dev))
Daniel Vetter426115c2013-07-11 22:13:42 +02001627 assert_panel_unlocked(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001628
Daniel Vetter426115c2013-07-11 22:13:42 +02001629 I915_WRITE(reg, dpll);
1630 POSTING_READ(reg);
1631 udelay(150);
1632
1633 if (wait_for(((I915_READ(reg) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
1634 DRM_ERROR("DPLL %d failed to lock\n", crtc->pipe);
1635
Ville Syrjäläd288f652014-10-28 13:20:22 +02001636 I915_WRITE(DPLL_MD(crtc->pipe), pipe_config->dpll_hw_state.dpll_md);
Daniel Vetter426115c2013-07-11 22:13:42 +02001637 POSTING_READ(DPLL_MD(crtc->pipe));
Daniel Vetter87442f72013-06-06 00:52:17 +02001638
1639 /* We do this three times for luck */
Daniel Vetter426115c2013-07-11 22:13:42 +02001640 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001641 POSTING_READ(reg);
1642 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001643 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001644 POSTING_READ(reg);
1645 udelay(150); /* wait for warmup */
Daniel Vetter426115c2013-07-11 22:13:42 +02001646 I915_WRITE(reg, dpll);
Daniel Vetter87442f72013-06-06 00:52:17 +02001647 POSTING_READ(reg);
1648 udelay(150); /* wait for warmup */
1649}
1650
Ville Syrjäläd288f652014-10-28 13:20:22 +02001651static void chv_enable_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02001652 const struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001653{
1654 struct drm_device *dev = crtc->base.dev;
1655 struct drm_i915_private *dev_priv = dev->dev_private;
1656 int pipe = crtc->pipe;
1657 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001658 u32 tmp;
1659
1660 assert_pipe_disabled(dev_priv, crtc->pipe);
1661
1662 BUG_ON(!IS_CHERRYVIEW(dev_priv->dev));
1663
Ville Syrjäläa5805162015-05-26 20:42:30 +03001664 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001665
1666 /* Enable back the 10bit clock to display controller */
1667 tmp = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1668 tmp |= DPIO_DCLKP_EN;
1669 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), tmp);
1670
Ville Syrjälä54433e92015-05-26 20:42:31 +03001671 mutex_unlock(&dev_priv->sb_lock);
1672
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001673 /*
1674 * Need to wait > 100ns between dclkp clock enable bit and PLL enable.
1675 */
1676 udelay(1);
1677
1678 /* Enable PLL */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001679 I915_WRITE(DPLL(pipe), pipe_config->dpll_hw_state.dpll);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001680
1681 /* Check PLL is locked */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001682 if (wait_for(((I915_READ(DPLL(pipe)) & DPLL_LOCK_VLV) == DPLL_LOCK_VLV), 1))
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001683 DRM_ERROR("PLL %d failed to lock\n", pipe);
1684
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001685 /* not sure when this should be written */
Ville Syrjäläd288f652014-10-28 13:20:22 +02001686 I915_WRITE(DPLL_MD(pipe), pipe_config->dpll_hw_state.dpll_md);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001687 POSTING_READ(DPLL_MD(pipe));
Chon Ming Lee9d556c92014-05-02 14:27:47 +03001688}
1689
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001690static int intel_num_dvo_pipes(struct drm_device *dev)
1691{
1692 struct intel_crtc *crtc;
1693 int count = 0;
1694
1695 for_each_intel_crtc(dev, crtc)
1696 count += crtc->active &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001697 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO);
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001698
1699 return count;
1700}
1701
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001702static void i9xx_enable_pll(struct intel_crtc *crtc)
Daniel Vetter87442f72013-06-06 00:52:17 +02001703{
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001704 struct drm_device *dev = crtc->base.dev;
1705 struct drm_i915_private *dev_priv = dev->dev_private;
1706 int reg = DPLL(crtc->pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001707 u32 dpll = crtc->config->dpll_hw_state.dpll;
Daniel Vetter87442f72013-06-06 00:52:17 +02001708
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001709 assert_pipe_disabled(dev_priv, crtc->pipe);
Daniel Vetter87442f72013-06-06 00:52:17 +02001710
1711 /* No really, not for ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001712 BUG_ON(INTEL_INFO(dev)->gen >= 5);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001713
1714 /* PLL is protected by panel, make sure we can write it */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001715 if (IS_MOBILE(dev) && !IS_I830(dev))
1716 assert_panel_unlocked(dev_priv, crtc->pipe);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001717
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001718 /* Enable DVO 2x clock on both PLLs if necessary */
1719 if (IS_I830(dev) && intel_num_dvo_pipes(dev) > 0) {
1720 /*
1721 * It appears to be important that we don't enable this
1722 * for the current pipe before otherwise configuring the
1723 * PLL. No idea how this should be handled if multiple
1724 * DVO outputs are enabled simultaneosly.
1725 */
1726 dpll |= DPLL_DVO_2X_MODE;
1727 I915_WRITE(DPLL(!crtc->pipe),
1728 I915_READ(DPLL(!crtc->pipe)) | DPLL_DVO_2X_MODE);
1729 }
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001730
1731 /* Wait for the clocks to stabilize. */
1732 POSTING_READ(reg);
1733 udelay(150);
1734
1735 if (INTEL_INFO(dev)->gen >= 4) {
1736 I915_WRITE(DPLL_MD(crtc->pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02001737 crtc->config->dpll_hw_state.dpll_md);
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001738 } else {
1739 /* The pixel multiplier can only be updated once the
1740 * DPLL is enabled and the clocks are stable.
1741 *
1742 * So write it again.
1743 */
1744 I915_WRITE(reg, dpll);
1745 }
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001746
1747 /* We do this three times for luck */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001748 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001749 POSTING_READ(reg);
1750 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001751 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001752 POSTING_READ(reg);
1753 udelay(150); /* wait for warmup */
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02001754 I915_WRITE(reg, dpll);
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001755 POSTING_READ(reg);
1756 udelay(150); /* wait for warmup */
1757}
1758
1759/**
Daniel Vetter50b44a42013-06-05 13:34:33 +02001760 * i9xx_disable_pll - disable a PLL
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001761 * @dev_priv: i915 private structure
1762 * @pipe: pipe PLL to disable
1763 *
1764 * Disable the PLL for @pipe, making sure the pipe is off first.
1765 *
1766 * Note! This is for pre-ILK only.
1767 */
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001768static void i9xx_disable_pll(struct intel_crtc *crtc)
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001769{
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001770 struct drm_device *dev = crtc->base.dev;
1771 struct drm_i915_private *dev_priv = dev->dev_private;
1772 enum pipe pipe = crtc->pipe;
1773
1774 /* Disable DVO 2x clock on both PLLs if necessary */
1775 if (IS_I830(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03001776 intel_pipe_has_type(crtc, INTEL_OUTPUT_DVO) &&
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03001777 intel_num_dvo_pipes(dev) == 1) {
1778 I915_WRITE(DPLL(PIPE_B),
1779 I915_READ(DPLL(PIPE_B)) & ~DPLL_DVO_2X_MODE);
1780 I915_WRITE(DPLL(PIPE_A),
1781 I915_READ(DPLL(PIPE_A)) & ~DPLL_DVO_2X_MODE);
1782 }
1783
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03001784 /* Don't disable pipe or pipe PLLs if needed */
1785 if ((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
1786 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001787 return;
1788
1789 /* Make sure the pipe isn't still relying on us */
1790 assert_pipe_disabled(dev_priv, pipe);
1791
Daniel Vetter50b44a42013-06-05 13:34:33 +02001792 I915_WRITE(DPLL(pipe), 0);
1793 POSTING_READ(DPLL(pipe));
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001794}
1795
Jesse Barnesf6071162013-10-01 10:41:38 -07001796static void vlv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1797{
1798 u32 val = 0;
1799
1800 /* Make sure the pipe isn't still relying on us */
1801 assert_pipe_disabled(dev_priv, pipe);
1802
Imre Deake5cbfbf2014-01-09 17:08:16 +02001803 /*
1804 * Leave integrated clock source and reference clock enabled for pipe B.
1805 * The latter is needed for VGA hotplug / manual detection.
1806 */
Jesse Barnesf6071162013-10-01 10:41:38 -07001807 if (pipe == PIPE_B)
Imre Deake5cbfbf2014-01-09 17:08:16 +02001808 val = DPLL_INTEGRATED_CRI_CLK_VLV | DPLL_REFA_CLK_ENABLE_VLV;
Jesse Barnesf6071162013-10-01 10:41:38 -07001809 I915_WRITE(DPLL(pipe), val);
1810 POSTING_READ(DPLL(pipe));
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001811
1812}
1813
1814static void chv_disable_pll(struct drm_i915_private *dev_priv, enum pipe pipe)
1815{
Ville Syrjäläd7520482014-04-09 13:28:59 +03001816 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001817 u32 val;
1818
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001819 /* Make sure the pipe isn't still relying on us */
1820 assert_pipe_disabled(dev_priv, pipe);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03001821
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001822 /* Set PLL en = 0 */
Ville Syrjäläd17ec4c2014-06-28 02:03:59 +03001823 val = DPLL_SSC_REF_CLOCK_CHV | DPLL_REFA_CLK_ENABLE_VLV;
Ville Syrjäläa11b0702014-04-09 13:28:57 +03001824 if (pipe != PIPE_A)
1825 val |= DPLL_INTEGRATED_CRI_CLK_VLV;
1826 I915_WRITE(DPLL(pipe), val);
1827 POSTING_READ(DPLL(pipe));
Ville Syrjäläd7520482014-04-09 13:28:59 +03001828
Ville Syrjäläa5805162015-05-26 20:42:30 +03001829 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd7520482014-04-09 13:28:59 +03001830
1831 /* Disable 10bit clock to display controller */
1832 val = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port));
1833 val &= ~DPIO_DCLKP_EN;
1834 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port), val);
1835
Ville Syrjälä61407f62014-05-27 16:32:55 +03001836 /* disable left/right clock distribution */
1837 if (pipe != PIPE_B) {
1838 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW5_CH0);
1839 val &= ~(CHV_BUFLEFTENA1_MASK | CHV_BUFRIGHTENA1_MASK);
1840 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW5_CH0, val);
1841 } else {
1842 val = vlv_dpio_read(dev_priv, pipe, _CHV_CMN_DW1_CH1);
1843 val &= ~(CHV_BUFLEFTENA2_MASK | CHV_BUFRIGHTENA2_MASK);
1844 vlv_dpio_write(dev_priv, pipe, _CHV_CMN_DW1_CH1, val);
1845 }
1846
Ville Syrjäläa5805162015-05-26 20:42:30 +03001847 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesf6071162013-10-01 10:41:38 -07001848}
1849
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001850void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001851 struct intel_digital_port *dport,
1852 unsigned int expected_mask)
Jesse Barnes89b667f2013-04-18 14:51:36 -07001853{
1854 u32 port_mask;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001855 int dpll_reg;
Jesse Barnes89b667f2013-04-18 14:51:36 -07001856
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001857 switch (dport->port) {
1858 case PORT_B:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001859 port_mask = DPLL_PORTB_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001860 dpll_reg = DPLL(0);
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001861 break;
1862 case PORT_C:
Jesse Barnes89b667f2013-04-18 14:51:36 -07001863 port_mask = DPLL_PORTC_READY_MASK;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001864 dpll_reg = DPLL(0);
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001865 expected_mask <<= 4;
Chon Ming Lee00fc31b2014-04-09 13:28:15 +03001866 break;
1867 case PORT_D:
1868 port_mask = DPLL_PORTD_READY_MASK;
1869 dpll_reg = DPIO_PHY_STATUS;
Chon Ming Leee4607fc2013-11-06 14:36:35 +08001870 break;
1871 default:
1872 BUG();
1873 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07001874
Ville Syrjälä9b6de0a2015-04-10 18:21:31 +03001875 if (wait_for((I915_READ(dpll_reg) & port_mask) == expected_mask, 1000))
1876 WARN(1, "timed out waiting for port %c ready: got 0x%x, expected 0x%x\n",
1877 port_name(dport->port), I915_READ(dpll_reg) & port_mask, expected_mask);
Jesse Barnes89b667f2013-04-18 14:51:36 -07001878}
1879
Daniel Vetterb14b1052014-04-24 23:55:13 +02001880static void intel_prepare_shared_dpll(struct intel_crtc *crtc)
1881{
1882 struct drm_device *dev = crtc->base.dev;
1883 struct drm_i915_private *dev_priv = dev->dev_private;
1884 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
1885
Chris Wilsonbe19f0f2014-05-28 16:16:42 +01001886 if (WARN_ON(pll == NULL))
1887 return;
1888
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001889 WARN_ON(!pll->config.crtc_mask);
Daniel Vetterb14b1052014-04-24 23:55:13 +02001890 if (pll->active == 0) {
1891 DRM_DEBUG_DRIVER("setting up %s\n", pll->name);
1892 WARN_ON(pll->on);
1893 assert_shared_dpll_disabled(dev_priv, pll);
1894
1895 pll->mode_set(dev_priv, pll);
1896 }
1897}
1898
Jesse Barnes63d7bbe2011-01-04 15:09:33 -08001899/**
Daniel Vetter85b38942014-04-24 23:55:14 +02001900 * intel_enable_shared_dpll - enable PCH PLL
Jesse Barnes92f25842011-01-04 15:09:34 -08001901 * @dev_priv: i915 private structure
1902 * @pipe: pipe PLL to enable
1903 *
1904 * The PCH PLL needs to be enabled before the PCH transcoder, since it
1905 * drives the transcoder clock.
1906 */
Daniel Vetter85b38942014-04-24 23:55:14 +02001907static void intel_enable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001908{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001909 struct drm_device *dev = crtc->base.dev;
1910 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001911 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes92f25842011-01-04 15:09:34 -08001912
Daniel Vetter87a875b2013-06-05 13:34:19 +02001913 if (WARN_ON(pll == NULL))
Chris Wilson48da64a2012-05-13 20:16:12 +01001914 return;
1915
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001916 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001917 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001918
Damien Lespiau74dd6922014-07-29 18:06:17 +01001919 DRM_DEBUG_KMS("enable %s (active %d, on? %d) for crtc %d\n",
Daniel Vetter46edb022013-06-05 13:34:12 +02001920 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001921 crtc->base.base.id);
Jesse Barnes92f25842011-01-04 15:09:34 -08001922
Daniel Vettercdbd2312013-06-05 13:34:03 +02001923 if (pll->active++) {
1924 WARN_ON(!pll->on);
Daniel Vettere9d69442013-06-05 13:34:15 +02001925 assert_shared_dpll_enabled(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001926 return;
1927 }
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001928 WARN_ON(pll->on);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001929
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001930 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
1931
Daniel Vetter46edb022013-06-05 13:34:12 +02001932 DRM_DEBUG_KMS("enabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001933 pll->enable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001934 pll->on = true;
Jesse Barnes92f25842011-01-04 15:09:34 -08001935}
1936
Damien Lespiauf6daaec2014-08-09 23:00:56 +01001937static void intel_disable_shared_dpll(struct intel_crtc *crtc)
Jesse Barnes92f25842011-01-04 15:09:34 -08001938{
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001939 struct drm_device *dev = crtc->base.dev;
1940 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettere2b78262013-06-07 23:10:03 +02001941 struct intel_shared_dpll *pll = intel_crtc_to_shared_dpll(crtc);
Jesse Barnes4c609cb2011-09-02 12:52:11 -07001942
Jesse Barnes92f25842011-01-04 15:09:34 -08001943 /* PCH only available on ILK+ */
Damien Lespiau3d13ef22014-02-07 19:12:47 +00001944 BUG_ON(INTEL_INFO(dev)->gen < 5);
Daniel Vetter87a875b2013-06-05 13:34:19 +02001945 if (WARN_ON(pll == NULL))
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001946 return;
1947
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +02001948 if (WARN_ON(pll->config.crtc_mask == 0))
Chris Wilson48da64a2012-05-13 20:16:12 +01001949 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001950
Daniel Vetter46edb022013-06-05 13:34:12 +02001951 DRM_DEBUG_KMS("disable %s (active %d, on? %d) for crtc %d\n",
1952 pll->name, pll->active, pll->on,
Daniel Vettere2b78262013-06-07 23:10:03 +02001953 crtc->base.base.id);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001954
Chris Wilson48da64a2012-05-13 20:16:12 +01001955 if (WARN_ON(pll->active == 0)) {
Daniel Vettere9d69442013-06-05 13:34:15 +02001956 assert_shared_dpll_disabled(dev_priv, pll);
Chris Wilson48da64a2012-05-13 20:16:12 +01001957 return;
1958 }
1959
Daniel Vettere9d69442013-06-05 13:34:15 +02001960 assert_shared_dpll_enabled(dev_priv, pll);
Daniel Vetterf4a091c2013-06-10 17:28:22 +02001961 WARN_ON(!pll->on);
Daniel Vettercdbd2312013-06-05 13:34:03 +02001962 if (--pll->active)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001963 return;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001964
Daniel Vetter46edb022013-06-05 13:34:12 +02001965 DRM_DEBUG_KMS("disabling %s\n", pll->name);
Daniel Vettere7b903d2013-06-05 13:34:14 +02001966 pll->disable(dev_priv, pll);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01001967 pll->on = false;
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -03001968
1969 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
Jesse Barnes92f25842011-01-04 15:09:34 -08001970}
1971
Paulo Zanonib8a4f402012-10-31 18:12:42 -02001972static void ironlake_enable_pch_transcoder(struct drm_i915_private *dev_priv,
1973 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08001974{
Daniel Vetter23670b322012-11-01 09:15:30 +01001975 struct drm_device *dev = dev_priv->dev;
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02001976 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vettere2b78262013-06-07 23:10:03 +02001977 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter23670b322012-11-01 09:15:30 +01001978 uint32_t reg, val, pipeconf_val;
Jesse Barnes040484a2011-01-03 12:14:26 -08001979
1980 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03001981 BUG_ON(!HAS_PCH_SPLIT(dev));
Jesse Barnes040484a2011-01-03 12:14:26 -08001982
1983 /* Make sure PCH DPLL is enabled */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02001984 assert_shared_dpll_enabled(dev_priv,
Daniel Vettere9d69442013-06-05 13:34:15 +02001985 intel_crtc_to_shared_dpll(intel_crtc));
Jesse Barnes040484a2011-01-03 12:14:26 -08001986
1987 /* FDI must be feeding us bits for PCH ports */
1988 assert_fdi_tx_enabled(dev_priv, pipe);
1989 assert_fdi_rx_enabled(dev_priv, pipe);
1990
Daniel Vetter23670b322012-11-01 09:15:30 +01001991 if (HAS_PCH_CPT(dev)) {
1992 /* Workaround: Set the timing override bit before enabling the
1993 * pch transcoder. */
1994 reg = TRANS_CHICKEN2(pipe);
1995 val = I915_READ(reg);
1996 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
1997 I915_WRITE(reg, val);
Eugeni Dodonov59c859d2012-05-09 15:37:19 -03001998 }
Daniel Vetter23670b322012-11-01 09:15:30 +01001999
Daniel Vetterab9412b2013-05-03 11:49:46 +02002000 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002001 val = I915_READ(reg);
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002002 pipeconf_val = I915_READ(PIPECONF(pipe));
Jesse Barnese9bcff52011-06-24 12:19:20 -07002003
2004 if (HAS_PCH_IBX(dev_priv->dev)) {
2005 /*
2006 * make the BPC in transcoder be consistent with
2007 * that in pipeconf reg.
2008 */
Daniel Vetterdfd07d72012-12-17 11:21:38 +01002009 val &= ~PIPECONF_BPC_MASK;
2010 val |= pipeconf_val & PIPECONF_BPC_MASK;
Jesse Barnese9bcff52011-06-24 12:19:20 -07002011 }
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002012
2013 val &= ~TRANS_INTERLACE_MASK;
2014 if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK)
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002015 if (HAS_PCH_IBX(dev_priv->dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002016 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Paulo Zanoni7c26e5c2012-02-14 17:07:09 -02002017 val |= TRANS_LEGACY_INTERLACED_ILK;
2018 else
2019 val |= TRANS_INTERLACED;
Paulo Zanoni5f7f7262012-02-03 17:47:15 -02002020 else
2021 val |= TRANS_PROGRESSIVE;
2022
Jesse Barnes040484a2011-01-03 12:14:26 -08002023 I915_WRITE(reg, val | TRANS_ENABLE);
2024 if (wait_for(I915_READ(reg) & TRANS_STATE_ENABLE, 100))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002025 DRM_ERROR("failed to enable transcoder %c\n", pipe_name(pipe));
Jesse Barnes040484a2011-01-03 12:14:26 -08002026}
2027
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002028static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
Paulo Zanoni937bb612012-10-31 18:12:47 -02002029 enum transcoder cpu_transcoder)
Jesse Barnes040484a2011-01-03 12:14:26 -08002030{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002031 u32 val, pipeconf_val;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002032
2033 /* PCH only available on ILK+ */
Ville Syrjälä55522f32014-09-03 14:09:53 +03002034 BUG_ON(!HAS_PCH_SPLIT(dev_priv->dev));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002035
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002036 /* FDI must be feeding us bits for PCH ports */
Daniel Vetter1a240d42012-11-29 22:18:51 +01002037 assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
Paulo Zanoni937bb612012-10-31 18:12:47 -02002038 assert_fdi_rx_enabled(dev_priv, TRANSCODER_A);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002039
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002040 /* Workaround: set timing override bit. */
2041 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002042 val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002043 I915_WRITE(_TRANSA_CHICKEN2, val);
2044
Paulo Zanoni25f3ef12012-10-31 18:12:49 -02002045 val = TRANS_ENABLE;
Paulo Zanoni937bb612012-10-31 18:12:47 -02002046 pipeconf_val = I915_READ(PIPECONF(cpu_transcoder));
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002047
Paulo Zanoni9a76b1c2012-10-31 18:12:48 -02002048 if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
2049 PIPECONF_INTERLACED_ILK)
Paulo Zanonia35f2672012-10-31 18:12:45 -02002050 val |= TRANS_INTERLACED;
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002051 else
2052 val |= TRANS_PROGRESSIVE;
2053
Daniel Vetterab9412b2013-05-03 11:49:46 +02002054 I915_WRITE(LPT_TRANSCONF, val);
2055 if (wait_for(I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE, 100))
Paulo Zanoni937bb612012-10-31 18:12:47 -02002056 DRM_ERROR("Failed to enable PCH transcoder\n");
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002057}
2058
Paulo Zanonib8a4f402012-10-31 18:12:42 -02002059static void ironlake_disable_pch_transcoder(struct drm_i915_private *dev_priv,
2060 enum pipe pipe)
Jesse Barnes040484a2011-01-03 12:14:26 -08002061{
Daniel Vetter23670b322012-11-01 09:15:30 +01002062 struct drm_device *dev = dev_priv->dev;
2063 uint32_t reg, val;
Jesse Barnes040484a2011-01-03 12:14:26 -08002064
2065 /* FDI relies on the transcoder */
2066 assert_fdi_tx_disabled(dev_priv, pipe);
2067 assert_fdi_rx_disabled(dev_priv, pipe);
2068
Jesse Barnes291906f2011-02-02 12:28:03 -08002069 /* Ports must be off as well */
2070 assert_pch_ports_disabled(dev_priv, pipe);
2071
Daniel Vetterab9412b2013-05-03 11:49:46 +02002072 reg = PCH_TRANSCONF(pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002073 val = I915_READ(reg);
2074 val &= ~TRANS_ENABLE;
2075 I915_WRITE(reg, val);
2076 /* wait for PCH transcoder off, transcoder state */
2077 if (wait_for((I915_READ(reg) & TRANS_STATE_ENABLE) == 0, 50))
Ville Syrjälä4bb6f1f2013-04-17 17:48:50 +03002078 DRM_ERROR("failed to disable transcoder %c\n", pipe_name(pipe));
Daniel Vetter23670b322012-11-01 09:15:30 +01002079
2080 if (!HAS_PCH_IBX(dev)) {
2081 /* Workaround: Clear the timing override chicken bit again. */
2082 reg = TRANS_CHICKEN2(pipe);
2083 val = I915_READ(reg);
2084 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
2085 I915_WRITE(reg, val);
2086 }
Jesse Barnes040484a2011-01-03 12:14:26 -08002087}
2088
Paulo Zanoniab4d9662012-10-31 18:12:55 -02002089static void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002090{
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002091 u32 val;
2092
Daniel Vetterab9412b2013-05-03 11:49:46 +02002093 val = I915_READ(LPT_TRANSCONF);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002094 val &= ~TRANS_ENABLE;
Daniel Vetterab9412b2013-05-03 11:49:46 +02002095 I915_WRITE(LPT_TRANSCONF, val);
Paulo Zanoni8fb033d2012-10-31 18:12:43 -02002096 /* wait for PCH transcoder off, transcoder state */
Daniel Vetterab9412b2013-05-03 11:49:46 +02002097 if (wait_for((I915_READ(LPT_TRANSCONF) & TRANS_STATE_ENABLE) == 0, 50))
Paulo Zanoni8a52fd92012-10-31 18:12:51 -02002098 DRM_ERROR("Failed to disable PCH transcoder\n");
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002099
2100 /* Workaround: clear timing override bit. */
2101 val = I915_READ(_TRANSA_CHICKEN2);
Daniel Vetter23670b322012-11-01 09:15:30 +01002102 val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
Paulo Zanoni223a6fd2012-10-31 18:12:52 -02002103 I915_WRITE(_TRANSA_CHICKEN2, val);
Jesse Barnes92f25842011-01-04 15:09:34 -08002104}
2105
2106/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002107 * intel_enable_pipe - enable a pipe, asserting requirements
Paulo Zanoni03722642014-01-17 13:51:09 -02002108 * @crtc: crtc responsible for the pipe
Jesse Barnesb24e7172011-01-04 15:09:30 -08002109 *
Paulo Zanoni03722642014-01-17 13:51:09 -02002110 * Enable @crtc's pipe, making sure that various hardware specific requirements
Jesse Barnesb24e7172011-01-04 15:09:30 -08002111 * are met, if applicable, e.g. PLL enabled, LVDS pairs enabled, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002112 */
Paulo Zanonie1fdc472014-01-17 13:51:12 -02002113static void intel_enable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002114{
Paulo Zanoni03722642014-01-17 13:51:09 -02002115 struct drm_device *dev = crtc->base.dev;
2116 struct drm_i915_private *dev_priv = dev->dev_private;
2117 enum pipe pipe = crtc->pipe;
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002118 enum transcoder cpu_transcoder = intel_pipe_to_cpu_transcoder(dev_priv,
2119 pipe);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002120 enum pipe pch_transcoder;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002121 int reg;
2122 u32 val;
2123
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002124 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002125 assert_cursor_disabled(dev_priv, pipe);
Daniel Vetter58c6eaa2013-04-11 16:29:09 +02002126 assert_sprites_disabled(dev_priv, pipe);
2127
Paulo Zanoni681e5812012-12-06 11:12:38 -02002128 if (HAS_PCH_LPT(dev_priv->dev))
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002129 pch_transcoder = TRANSCODER_A;
2130 else
2131 pch_transcoder = pipe;
2132
Jesse Barnesb24e7172011-01-04 15:09:30 -08002133 /*
2134 * A pipe without a PLL won't actually be able to drive bits from
2135 * a plane. On ILK+ the pipe PLLs are integrated, so we don't
2136 * need the check.
2137 */
Imre Deak50360402015-01-16 00:55:16 -08002138 if (HAS_GMCH_DISPLAY(dev_priv->dev))
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03002139 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DSI))
Jani Nikula23538ef2013-08-27 15:12:22 +03002140 assert_dsi_pll_enabled(dev_priv);
2141 else
2142 assert_pll_enabled(dev_priv, pipe);
Jesse Barnes040484a2011-01-03 12:14:26 -08002143 else {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002144 if (crtc->config->has_pch_encoder) {
Jesse Barnes040484a2011-01-03 12:14:26 -08002145 /* if driving the PCH, we need FDI enabled */
Paulo Zanonicc391bb2012-11-20 13:27:37 -02002146 assert_fdi_rx_pll_enabled(dev_priv, pch_transcoder);
Daniel Vetter1a240d42012-11-29 22:18:51 +01002147 assert_fdi_tx_pll_enabled(dev_priv,
2148 (enum pipe) cpu_transcoder);
Jesse Barnes040484a2011-01-03 12:14:26 -08002149 }
2150 /* FIXME: assert CPU port conditions for SNB+ */
2151 }
Jesse Barnesb24e7172011-01-04 15:09:30 -08002152
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002153 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002154 val = I915_READ(reg);
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002155 if (val & PIPECONF_ENABLE) {
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002156 WARN_ON(!((pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
2157 (pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE)));
Chris Wilson00d70b12011-03-17 07:18:29 +00002158 return;
Paulo Zanoni7ad25d42014-01-17 13:51:13 -02002159 }
Chris Wilson00d70b12011-03-17 07:18:29 +00002160
2161 I915_WRITE(reg, val | PIPECONF_ENABLE);
Paulo Zanoni851855d2013-12-19 19:12:29 -02002162 POSTING_READ(reg);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002163}
2164
2165/**
Chris Wilson309cfea2011-01-28 13:54:53 +00002166 * intel_disable_pipe - disable a pipe, asserting requirements
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002167 * @crtc: crtc whose pipes is to be disabled
Jesse Barnesb24e7172011-01-04 15:09:30 -08002168 *
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002169 * Disable the pipe of @crtc, making sure that various hardware
2170 * specific requirements are met, if applicable, e.g. plane
2171 * disabled, panel fitter off, etc.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002172 *
2173 * Will wait until the pipe has shut down before returning.
2174 */
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002175static void intel_disable_pipe(struct intel_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002176{
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002177 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002178 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03002179 enum pipe pipe = crtc->pipe;
Jesse Barnesb24e7172011-01-04 15:09:30 -08002180 int reg;
2181 u32 val;
2182
2183 /*
2184 * Make sure planes won't keep trying to pump pixels to us,
2185 * or we might hang the display.
2186 */
2187 assert_planes_disabled(dev_priv, pipe);
Jani Nikula93ce0ba2013-09-13 11:03:08 +03002188 assert_cursor_disabled(dev_priv, pipe);
Jesse Barnes19332d72013-03-28 09:55:38 -07002189 assert_sprites_disabled(dev_priv, pipe);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002190
Paulo Zanoni702e7a52012-10-23 18:29:59 -02002191 reg = PIPECONF(cpu_transcoder);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002192 val = I915_READ(reg);
Chris Wilson00d70b12011-03-17 07:18:29 +00002193 if ((val & PIPECONF_ENABLE) == 0)
2194 return;
2195
Ville Syrjälä67adc642014-08-15 01:21:57 +03002196 /*
2197 * Double wide has implications for planes
2198 * so best keep it disabled when not needed.
2199 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002200 if (crtc->config->double_wide)
Ville Syrjälä67adc642014-08-15 01:21:57 +03002201 val &= ~PIPECONF_DOUBLE_WIDE;
2202
2203 /* Don't disable pipe or pipe PLLs if needed */
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03002204 if (!(pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) &&
2205 !(pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Ville Syrjälä67adc642014-08-15 01:21:57 +03002206 val &= ~PIPECONF_ENABLE;
2207
2208 I915_WRITE(reg, val);
2209 if ((val & PIPECONF_ENABLE) == 0)
2210 intel_wait_for_pipe_off(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002211}
2212
2213/**
Matt Roper262ca2b2014-03-18 17:22:55 -07002214 * intel_enable_primary_hw_plane - enable the primary plane on a given pipe
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002215 * @plane: plane to be enabled
2216 * @crtc: crtc for the plane
Jesse Barnesb24e7172011-01-04 15:09:30 -08002217 *
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002218 * Enable @plane on @crtc, making sure that the pipe is running first.
Jesse Barnesb24e7172011-01-04 15:09:30 -08002219 */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002220static void intel_enable_primary_hw_plane(struct drm_plane *plane,
2221 struct drm_crtc *crtc)
Jesse Barnesb24e7172011-01-04 15:09:30 -08002222{
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002223 struct drm_device *dev = plane->dev;
2224 struct drm_i915_private *dev_priv = dev->dev_private;
2225 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002226
2227 /* If the pipe isn't enabled, we can't pump pixels and may hang */
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002228 assert_pipe_enabled(dev_priv, intel_crtc->pipe);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002229 to_intel_plane_state(plane->state)->visible = true;
Ville Syrjälä939c2fe2013-10-01 18:02:10 +03002230
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002231 dev_priv->display.update_primary_plane(crtc, plane->fb,
2232 crtc->x, crtc->y);
Jesse Barnesb24e7172011-01-04 15:09:30 -08002233}
2234
Chris Wilson693db182013-03-05 14:52:39 +00002235static bool need_vtd_wa(struct drm_device *dev)
2236{
2237#ifdef CONFIG_INTEL_IOMMU
2238 if (INTEL_INFO(dev)->gen >= 6 && intel_iommu_gfx_mapped)
2239 return true;
2240#endif
2241 return false;
2242}
2243
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002244unsigned int
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002245intel_tile_height(struct drm_device *dev, uint32_t pixel_format,
2246 uint64_t fb_format_modifier)
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002247{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002248 unsigned int tile_height;
2249 uint32_t pixel_bytes;
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002250
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002251 switch (fb_format_modifier) {
2252 case DRM_FORMAT_MOD_NONE:
2253 tile_height = 1;
2254 break;
2255 case I915_FORMAT_MOD_X_TILED:
2256 tile_height = IS_GEN2(dev) ? 16 : 8;
2257 break;
2258 case I915_FORMAT_MOD_Y_TILED:
2259 tile_height = 32;
2260 break;
2261 case I915_FORMAT_MOD_Yf_TILED:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002262 pixel_bytes = drm_format_plane_cpp(pixel_format, 0);
2263 switch (pixel_bytes) {
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002264 default:
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002265 case 1:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002266 tile_height = 64;
2267 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002268 case 2:
2269 case 4:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002270 tile_height = 32;
2271 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002272 case 8:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002273 tile_height = 16;
2274 break;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002275 case 16:
Damien Lespiaub5d0e9b2015-02-27 11:15:19 +00002276 WARN_ONCE(1,
2277 "128-bit pixels are not supported for display!");
2278 tile_height = 16;
2279 break;
2280 }
2281 break;
2282 default:
2283 MISSING_CASE(fb_format_modifier);
2284 tile_height = 1;
2285 break;
2286 }
Daniel Vetter091df6c2015-02-10 17:16:10 +00002287
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00002288 return tile_height;
2289}
2290
2291unsigned int
2292intel_fb_align_height(struct drm_device *dev, unsigned int height,
2293 uint32_t pixel_format, uint64_t fb_format_modifier)
2294{
2295 return ALIGN(height, intel_tile_height(dev, pixel_format,
2296 fb_format_modifier));
Jesse Barnesa57ce0b2014-02-07 12:10:35 -08002297}
2298
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002299static int
2300intel_fill_fb_ggtt_view(struct i915_ggtt_view *view, struct drm_framebuffer *fb,
2301 const struct drm_plane_state *plane_state)
2302{
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002303 struct intel_rotation_info *info = &view->rotation_info;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002304
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002305 *view = i915_ggtt_view_normal;
2306
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002307 if (!plane_state)
2308 return 0;
2309
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002310 if (!intel_rotation_90_or_270(plane_state->rotation))
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002311 return 0;
2312
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002313 *view = i915_ggtt_view_rotated;
Tvrtko Ursulin50470bb2015-03-23 11:10:36 +00002314
2315 info->height = fb->height;
2316 info->pixel_format = fb->pixel_format;
2317 info->pitch = fb->pitches[0];
2318 info->fb_modifier = fb->modifier[0];
2319
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002320 return 0;
2321}
2322
Chris Wilson127bd2a2010-07-23 23:32:05 +01002323int
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002324intel_pin_and_fence_fb_obj(struct drm_plane *plane,
2325 struct drm_framebuffer *fb,
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002326 const struct drm_plane_state *plane_state,
Oscar Mateoa4872ba2014-05-22 14:13:33 +01002327 struct intel_engine_cs *pipelined)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002328{
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002329 struct drm_device *dev = fb->dev;
Chris Wilsonce453d82011-02-21 14:43:56 +00002330 struct drm_i915_private *dev_priv = dev->dev_private;
Tvrtko Ursulin850c4cd2014-10-30 16:39:38 +00002331 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002332 struct i915_ggtt_view view;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002333 u32 alignment;
2334 int ret;
2335
Matt Roperebcdd392014-07-09 16:22:11 -07002336 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
2337
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002338 switch (fb->modifier[0]) {
2339 case DRM_FORMAT_MOD_NONE:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002340 if (INTEL_INFO(dev)->gen >= 9)
2341 alignment = 256 * 1024;
2342 else if (IS_BROADWATER(dev) || IS_CRESTLINE(dev))
Chris Wilson534843d2010-07-05 18:01:46 +01002343 alignment = 128 * 1024;
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002344 else if (INTEL_INFO(dev)->gen >= 4)
Chris Wilson534843d2010-07-05 18:01:46 +01002345 alignment = 4 * 1024;
2346 else
2347 alignment = 64 * 1024;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002348 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002349 case I915_FORMAT_MOD_X_TILED:
Damien Lespiau1fada4c2013-07-03 21:06:02 +01002350 if (INTEL_INFO(dev)->gen >= 9)
2351 alignment = 256 * 1024;
2352 else {
2353 /* pin() will align the object as required by fence */
2354 alignment = 0;
2355 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002356 break;
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002357 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiau1327b9a2015-02-27 11:15:20 +00002358 case I915_FORMAT_MOD_Yf_TILED:
2359 if (WARN_ONCE(INTEL_INFO(dev)->gen < 9,
2360 "Y tiling bo slipped through, driver bug!\n"))
2361 return -EINVAL;
2362 alignment = 1 * 1024 * 1024;
2363 break;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002364 default:
Tvrtko Ursulin7b911ad2015-02-10 17:16:15 +00002365 MISSING_CASE(fb->modifier[0]);
2366 return -EINVAL;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002367 }
2368
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002369 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2370 if (ret)
2371 return ret;
2372
Chris Wilson693db182013-03-05 14:52:39 +00002373 /* Note that the w/a also requires 64 PTE of padding following the
2374 * bo. We currently fill all unused PTE with the shadow page and so
2375 * we should always have valid PTE following the scanout preventing
2376 * the VT-d warning.
2377 */
2378 if (need_vtd_wa(dev) && alignment < 256 * 1024)
2379 alignment = 256 * 1024;
2380
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002381 /*
2382 * Global gtt pte registers are special registers which actually forward
2383 * writes to a chunk of system memory. Which means that there is no risk
2384 * that the register values disappear as soon as we call
2385 * intel_runtime_pm_put(), so it is correct to wrap only the
2386 * pin/unpin/fence and not more.
2387 */
2388 intel_runtime_pm_get(dev_priv);
2389
Chris Wilsonce453d82011-02-21 14:43:56 +00002390 dev_priv->mm.interruptible = false;
Tvrtko Ursuline6617332015-03-23 11:10:33 +00002391 ret = i915_gem_object_pin_to_display_plane(obj, alignment, pipelined,
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002392 &view);
Chris Wilson48b956c2010-09-14 12:50:34 +01002393 if (ret)
Chris Wilsonce453d82011-02-21 14:43:56 +00002394 goto err_interruptible;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002395
2396 /* Install a fence for tiled scan-out. Pre-i965 always needs a
2397 * fence, whereas 965+ only requires a fence if using
2398 * framebuffer compression. For simplicity, we always install
2399 * a fence as the cost is not that onerous.
2400 */
Chris Wilson06d98132012-04-17 15:31:24 +01002401 ret = i915_gem_object_get_fence(obj);
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002402 if (ret)
2403 goto err_unpin;
Chris Wilson1690e1e2011-12-14 13:57:08 +01002404
Chris Wilson9a5a53b2012-03-22 15:10:00 +00002405 i915_gem_object_pin_fence(obj);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002406
Chris Wilsonce453d82011-02-21 14:43:56 +00002407 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002408 intel_runtime_pm_put(dev_priv);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002409 return 0;
Chris Wilson48b956c2010-09-14 12:50:34 +01002410
2411err_unpin:
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002412 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilsonce453d82011-02-21 14:43:56 +00002413err_interruptible:
2414 dev_priv->mm.interruptible = true;
Paulo Zanonid6dd6842014-08-15 15:59:32 -03002415 intel_runtime_pm_put(dev_priv);
Chris Wilson48b956c2010-09-14 12:50:34 +01002416 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -05002417}
2418
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002419static void intel_unpin_fb_obj(struct drm_framebuffer *fb,
2420 const struct drm_plane_state *plane_state)
Chris Wilson1690e1e2011-12-14 13:57:08 +01002421{
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002422 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002423 struct i915_ggtt_view view;
2424 int ret;
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +00002425
Matt Roperebcdd392014-07-09 16:22:11 -07002426 WARN_ON(!mutex_is_locked(&obj->base.dev->struct_mutex));
2427
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002428 ret = intel_fill_fb_ggtt_view(&view, fb, plane_state);
2429 WARN_ONCE(ret, "Couldn't get view from plane state!");
2430
Chris Wilson1690e1e2011-12-14 13:57:08 +01002431 i915_gem_object_unpin_fence(obj);
Tvrtko Ursulinf64b98c2015-03-23 11:10:35 +00002432 i915_gem_object_unpin_from_display_plane(obj, &view);
Chris Wilson1690e1e2011-12-14 13:57:08 +01002433}
2434
Daniel Vetterc2c75132012-07-05 12:17:30 +02002435/* Computes the linear offset to the base tile and adjusts x, y. bytes per pixel
2436 * is assumed to be a power-of-two. */
Chris Wilsonbc752862013-02-21 20:04:31 +00002437unsigned long intel_gen4_compute_page_offset(int *x, int *y,
2438 unsigned int tiling_mode,
2439 unsigned int cpp,
2440 unsigned int pitch)
Daniel Vetterc2c75132012-07-05 12:17:30 +02002441{
Chris Wilsonbc752862013-02-21 20:04:31 +00002442 if (tiling_mode != I915_TILING_NONE) {
2443 unsigned int tile_rows, tiles;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002444
Chris Wilsonbc752862013-02-21 20:04:31 +00002445 tile_rows = *y / 8;
2446 *y %= 8;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002447
Chris Wilsonbc752862013-02-21 20:04:31 +00002448 tiles = *x / (512/cpp);
2449 *x %= 512/cpp;
2450
2451 return tile_rows * pitch * 8 + tiles * 4096;
2452 } else {
2453 unsigned int offset;
2454
2455 offset = *y * pitch + *x * cpp;
2456 *y = 0;
2457 *x = (offset & 4095) / cpp;
2458 return offset & -4096;
2459 }
Daniel Vetterc2c75132012-07-05 12:17:30 +02002460}
2461
Damien Lespiaub35d63f2015-01-20 12:51:50 +00002462static int i9xx_format_to_fourcc(int format)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002463{
2464 switch (format) {
2465 case DISPPLANE_8BPP:
2466 return DRM_FORMAT_C8;
2467 case DISPPLANE_BGRX555:
2468 return DRM_FORMAT_XRGB1555;
2469 case DISPPLANE_BGRX565:
2470 return DRM_FORMAT_RGB565;
2471 default:
2472 case DISPPLANE_BGRX888:
2473 return DRM_FORMAT_XRGB8888;
2474 case DISPPLANE_RGBX888:
2475 return DRM_FORMAT_XBGR8888;
2476 case DISPPLANE_BGRX101010:
2477 return DRM_FORMAT_XRGB2101010;
2478 case DISPPLANE_RGBX101010:
2479 return DRM_FORMAT_XBGR2101010;
2480 }
2481}
2482
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00002483static int skl_format_to_fourcc(int format, bool rgb_order, bool alpha)
2484{
2485 switch (format) {
2486 case PLANE_CTL_FORMAT_RGB_565:
2487 return DRM_FORMAT_RGB565;
2488 default:
2489 case PLANE_CTL_FORMAT_XRGB_8888:
2490 if (rgb_order) {
2491 if (alpha)
2492 return DRM_FORMAT_ABGR8888;
2493 else
2494 return DRM_FORMAT_XBGR8888;
2495 } else {
2496 if (alpha)
2497 return DRM_FORMAT_ARGB8888;
2498 else
2499 return DRM_FORMAT_XRGB8888;
2500 }
2501 case PLANE_CTL_FORMAT_XRGB_2101010:
2502 if (rgb_order)
2503 return DRM_FORMAT_XBGR2101010;
2504 else
2505 return DRM_FORMAT_XRGB2101010;
2506 }
2507}
2508
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002509static bool
Daniel Vetterf6936e22015-03-26 12:17:05 +01002510intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2511 struct intel_initial_plane_config *plane_config)
Jesse Barnes46f297f2014-03-07 08:57:48 -08002512{
2513 struct drm_device *dev = crtc->base.dev;
2514 struct drm_i915_gem_object *obj = NULL;
2515 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Damien Lespiau2d140302015-02-05 17:22:18 +00002516 struct drm_framebuffer *fb = &plane_config->fb->base;
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002517 u32 base_aligned = round_down(plane_config->base, PAGE_SIZE);
2518 u32 size_aligned = round_up(plane_config->base + plane_config->size,
2519 PAGE_SIZE);
2520
2521 size_aligned -= base_aligned;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002522
Chris Wilsonff2652e2014-03-10 08:07:02 +00002523 if (plane_config->size == 0)
2524 return false;
2525
Daniel Vetterf37b5c22015-02-10 23:12:27 +01002526 obj = i915_gem_object_create_stolen_for_preallocated(dev,
2527 base_aligned,
2528 base_aligned,
2529 size_aligned);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002530 if (!obj)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002531 return false;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002532
Damien Lespiau49af4492015-01-20 12:51:44 +00002533 obj->tiling_mode = plane_config->tiling;
2534 if (obj->tiling_mode == I915_TILING_X)
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002535 obj->stride = fb->pitches[0];
Jesse Barnes46f297f2014-03-07 08:57:48 -08002536
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002537 mode_cmd.pixel_format = fb->pixel_format;
2538 mode_cmd.width = fb->width;
2539 mode_cmd.height = fb->height;
2540 mode_cmd.pitches[0] = fb->pitches[0];
Daniel Vetter18c52472015-02-10 17:16:09 +00002541 mode_cmd.modifier[0] = fb->modifier[0];
2542 mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002543
2544 mutex_lock(&dev->struct_mutex);
Damien Lespiau6bf129d2015-02-05 17:22:16 +00002545 if (intel_framebuffer_init(dev, to_intel_framebuffer(fb),
Jesse Barnes484b41d2014-03-07 08:57:55 -08002546 &mode_cmd, obj)) {
Jesse Barnes46f297f2014-03-07 08:57:48 -08002547 DRM_DEBUG_KMS("intel fb init failed\n");
2548 goto out_unref_obj;
2549 }
Jesse Barnes46f297f2014-03-07 08:57:48 -08002550 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002551
Daniel Vetterf6936e22015-03-26 12:17:05 +01002552 DRM_DEBUG_KMS("initial plane fb obj %p\n", obj);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002553 return true;
Jesse Barnes46f297f2014-03-07 08:57:48 -08002554
2555out_unref_obj:
2556 drm_gem_object_unreference(&obj->base);
2557 mutex_unlock(&dev->struct_mutex);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002558 return false;
2559}
2560
Matt Roperafd65eb2015-02-03 13:10:04 -08002561/* Update plane->state->fb to match plane->fb after driver-internal updates */
2562static void
2563update_state_fb(struct drm_plane *plane)
2564{
2565 if (plane->fb == plane->state->fb)
2566 return;
2567
2568 if (plane->state->fb)
2569 drm_framebuffer_unreference(plane->state->fb);
2570 plane->state->fb = plane->fb;
2571 if (plane->state->fb)
2572 drm_framebuffer_reference(plane->state->fb);
2573}
2574
Damien Lespiau5724dbd2015-01-20 12:51:52 +00002575static void
Daniel Vetterf6936e22015-03-26 12:17:05 +01002576intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2577 struct intel_initial_plane_config *plane_config)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002578{
2579 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnesd9ceb812014-10-09 12:57:43 -07002580 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002581 struct drm_crtc *c;
2582 struct intel_crtc *i;
Matt Roper2ff8fde2014-07-08 07:50:07 -07002583 struct drm_i915_gem_object *obj;
Daniel Vetter88595ac2015-03-26 12:42:24 +01002584 struct drm_plane *primary = intel_crtc->base.primary;
2585 struct drm_framebuffer *fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002586
Damien Lespiau2d140302015-02-05 17:22:18 +00002587 if (!plane_config->fb)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002588 return;
2589
Daniel Vetterf6936e22015-03-26 12:17:05 +01002590 if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002591 fb = &plane_config->fb->base;
2592 goto valid_fb;
Damien Lespiauf55548b2015-02-05 18:30:20 +00002593 }
Jesse Barnes484b41d2014-03-07 08:57:55 -08002594
Damien Lespiau2d140302015-02-05 17:22:18 +00002595 kfree(plane_config->fb);
Jesse Barnes484b41d2014-03-07 08:57:55 -08002596
2597 /*
2598 * Failed to alloc the obj, check to see if we should share
2599 * an fb with another CRTC instead
2600 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01002601 for_each_crtc(dev, c) {
Jesse Barnes484b41d2014-03-07 08:57:55 -08002602 i = to_intel_crtc(c);
2603
2604 if (c == &intel_crtc->base)
2605 continue;
2606
Matt Roper2ff8fde2014-07-08 07:50:07 -07002607 if (!i->active)
Jesse Barnes484b41d2014-03-07 08:57:55 -08002608 continue;
2609
Daniel Vetter88595ac2015-03-26 12:42:24 +01002610 fb = c->primary->fb;
2611 if (!fb)
Matt Roper2ff8fde2014-07-08 07:50:07 -07002612 continue;
2613
Daniel Vetter88595ac2015-03-26 12:42:24 +01002614 obj = intel_fb_obj(fb);
Matt Roper2ff8fde2014-07-08 07:50:07 -07002615 if (i915_gem_obj_ggtt_offset(obj) == plane_config->base) {
Daniel Vetter88595ac2015-03-26 12:42:24 +01002616 drm_framebuffer_reference(fb);
2617 goto valid_fb;
Jesse Barnes484b41d2014-03-07 08:57:55 -08002618 }
2619 }
Daniel Vetter88595ac2015-03-26 12:42:24 +01002620
2621 return;
2622
2623valid_fb:
2624 obj = intel_fb_obj(fb);
2625 if (obj->tiling_mode != I915_TILING_NONE)
2626 dev_priv->preserve_bios_swizzle = true;
2627
2628 primary->fb = fb;
2629 primary->state->crtc = &intel_crtc->base;
2630 primary->crtc = &intel_crtc->base;
2631 update_state_fb(primary);
2632 obj->frontbuffer_bits |= INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
Jesse Barnes46f297f2014-03-07 08:57:48 -08002633}
2634
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002635static void i9xx_update_primary_plane(struct drm_crtc *crtc,
2636 struct drm_framebuffer *fb,
2637 int x, int y)
Jesse Barnes81255562010-08-02 12:07:50 -07002638{
2639 struct drm_device *dev = crtc->dev;
2640 struct drm_i915_private *dev_priv = dev->dev_private;
2641 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002642 struct drm_plane *primary = crtc->primary;
2643 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002644 struct drm_i915_gem_object *obj;
Jesse Barnes81255562010-08-02 12:07:50 -07002645 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002646 unsigned long linear_offset;
Jesse Barnes81255562010-08-02 12:07:50 -07002647 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002648 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302649 int pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002650
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002651 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002652 I915_WRITE(reg, 0);
2653 if (INTEL_INFO(dev)->gen >= 4)
2654 I915_WRITE(DSPSURF(plane), 0);
2655 else
2656 I915_WRITE(DSPADDR(plane), 0);
2657 POSTING_READ(reg);
2658 return;
2659 }
2660
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002661 obj = intel_fb_obj(fb);
2662 if (WARN_ON(obj == NULL))
2663 return;
2664
2665 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2666
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002667 dspcntr = DISPPLANE_GAMMA_ENABLE;
2668
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002669 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002670
2671 if (INTEL_INFO(dev)->gen < 4) {
2672 if (intel_crtc->pipe == PIPE_B)
2673 dspcntr |= DISPPLANE_SEL_PIPE_B;
2674
2675 /* pipesrc and dspsize control the size that is scaled from,
2676 * which should always be the user's requested size.
2677 */
2678 I915_WRITE(DSPSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002679 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2680 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002681 I915_WRITE(DSPPOS(plane), 0);
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002682 } else if (IS_CHERRYVIEW(dev) && plane == PLANE_B) {
2683 I915_WRITE(PRIMSIZE(plane),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002684 ((intel_crtc->config->pipe_src_h - 1) << 16) |
2685 (intel_crtc->config->pipe_src_w - 1));
Ville Syrjäläc14b0482014-10-16 20:52:34 +03002686 I915_WRITE(PRIMPOS(plane), 0);
2687 I915_WRITE(PRIMCNSTALPHA(plane), 0);
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002688 }
2689
Ville Syrjälä57779d02012-10-31 17:50:14 +02002690 switch (fb->pixel_format) {
2691 case DRM_FORMAT_C8:
Jesse Barnes81255562010-08-02 12:07:50 -07002692 dspcntr |= DISPPLANE_8BPP;
2693 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002694 case DRM_FORMAT_XRGB1555:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002695 dspcntr |= DISPPLANE_BGRX555;
Jesse Barnes81255562010-08-02 12:07:50 -07002696 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002697 case DRM_FORMAT_RGB565:
2698 dspcntr |= DISPPLANE_BGRX565;
2699 break;
2700 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002701 dspcntr |= DISPPLANE_BGRX888;
2702 break;
2703 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002704 dspcntr |= DISPPLANE_RGBX888;
2705 break;
2706 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002707 dspcntr |= DISPPLANE_BGRX101010;
2708 break;
2709 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002710 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes81255562010-08-02 12:07:50 -07002711 break;
2712 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002713 BUG();
Jesse Barnes81255562010-08-02 12:07:50 -07002714 }
Ville Syrjälä57779d02012-10-31 17:50:14 +02002715
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002716 if (INTEL_INFO(dev)->gen >= 4 &&
2717 obj->tiling_mode != I915_TILING_NONE)
2718 dspcntr |= DISPPLANE_TILED;
Jesse Barnes81255562010-08-02 12:07:50 -07002719
Ville Syrjäläde1aa622013-06-07 10:47:01 +03002720 if (IS_G4X(dev))
2721 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
2722
Ville Syrjäläb98971272014-08-27 16:51:22 +03002723 linear_offset = y * fb->pitches[0] + x * pixel_size;
Jesse Barnes81255562010-08-02 12:07:50 -07002724
Daniel Vetterc2c75132012-07-05 12:17:30 +02002725 if (INTEL_INFO(dev)->gen >= 4) {
2726 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002727 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002728 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002729 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002730 linear_offset -= intel_crtc->dspaddr_offset;
2731 } else {
Daniel Vettere506a0c2012-07-05 12:17:29 +02002732 intel_crtc->dspaddr_offset = linear_offset;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002733 }
Daniel Vettere506a0c2012-07-05 12:17:29 +02002734
Matt Roper8e7d6882015-01-21 16:35:41 -08002735 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302736 dspcntr |= DISPPLANE_ROTATE_180;
2737
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002738 x += (intel_crtc->config->pipe_src_w - 1);
2739 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302740
2741 /* Finding the last pixel of the last line of the display
2742 data and adding to linear_offset*/
2743 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002744 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2745 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302746 }
2747
2748 I915_WRITE(reg, dspcntr);
2749
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002750 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Chris Wilsona6c45cf2010-09-17 00:32:17 +01002751 if (INTEL_INFO(dev)->gen >= 4) {
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002752 I915_WRITE(DSPSURF(plane),
2753 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002754 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
Daniel Vettere506a0c2012-07-05 12:17:29 +02002755 I915_WRITE(DSPLINOFF(plane), linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002756 } else
Ben Widawskyf343c5f2013-07-05 14:41:04 -07002757 I915_WRITE(DSPADDR(plane), i915_gem_obj_ggtt_offset(obj) + linear_offset);
Chris Wilson5eddb702010-09-11 13:48:45 +01002758 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002759}
2760
Daniel Vetter29b9bde2014-04-24 23:55:01 +02002761static void ironlake_update_primary_plane(struct drm_crtc *crtc,
2762 struct drm_framebuffer *fb,
2763 int x, int y)
Jesse Barnes17638cd2011-06-24 12:19:23 -07002764{
2765 struct drm_device *dev = crtc->dev;
2766 struct drm_i915_private *dev_priv = dev->dev_private;
2767 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002768 struct drm_plane *primary = crtc->primary;
2769 bool visible = to_intel_plane_state(primary->state)->visible;
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002770 struct drm_i915_gem_object *obj;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002771 int plane = intel_crtc->plane;
Daniel Vettere506a0c2012-07-05 12:17:29 +02002772 unsigned long linear_offset;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002773 u32 dspcntr;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002774 u32 reg = DSPCNTR(plane);
Sonika Jindal48404c12014-08-22 14:06:04 +05302775 int pixel_size;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002776
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03002777 if (!visible || !fb) {
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002778 I915_WRITE(reg, 0);
2779 I915_WRITE(DSPSURF(plane), 0);
2780 POSTING_READ(reg);
2781 return;
2782 }
2783
Ville Syrjäläc9ba6fa2014-08-27 17:48:41 +03002784 obj = intel_fb_obj(fb);
2785 if (WARN_ON(obj == NULL))
2786 return;
2787
2788 pixel_size = drm_format_plane_cpp(fb->pixel_format, 0);
2789
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002790 dspcntr = DISPPLANE_GAMMA_ENABLE;
2791
Ville Syrjäläfdd508a62014-08-08 21:51:11 +03002792 dspcntr |= DISPLAY_PLANE_ENABLE;
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002793
2794 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
2795 dspcntr |= DISPPLANE_PIPE_CSC_ENABLE;
2796
Ville Syrjälä57779d02012-10-31 17:50:14 +02002797 switch (fb->pixel_format) {
2798 case DRM_FORMAT_C8:
Jesse Barnes17638cd2011-06-24 12:19:23 -07002799 dspcntr |= DISPPLANE_8BPP;
2800 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002801 case DRM_FORMAT_RGB565:
2802 dspcntr |= DISPPLANE_BGRX565;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002803 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +02002804 case DRM_FORMAT_XRGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002805 dspcntr |= DISPPLANE_BGRX888;
2806 break;
2807 case DRM_FORMAT_XBGR8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002808 dspcntr |= DISPPLANE_RGBX888;
2809 break;
2810 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002811 dspcntr |= DISPPLANE_BGRX101010;
2812 break;
2813 case DRM_FORMAT_XBGR2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +02002814 dspcntr |= DISPPLANE_RGBX101010;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002815 break;
2816 default:
Daniel Vetterbaba1332013-03-27 00:45:00 +01002817 BUG();
Jesse Barnes17638cd2011-06-24 12:19:23 -07002818 }
2819
2820 if (obj->tiling_mode != I915_TILING_NONE)
2821 dspcntr |= DISPPLANE_TILED;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002822
Ville Syrjäläf45651b2014-08-08 21:51:10 +03002823 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev))
Paulo Zanoni1f5d76d2013-08-23 19:51:28 -03002824 dspcntr |= DISPPLANE_TRICKLE_FEED_DISABLE;
Jesse Barnes17638cd2011-06-24 12:19:23 -07002825
Ville Syrjäläb98971272014-08-27 16:51:22 +03002826 linear_offset = y * fb->pitches[0] + x * pixel_size;
Daniel Vetterc2c75132012-07-05 12:17:30 +02002827 intel_crtc->dspaddr_offset =
Chris Wilsonbc752862013-02-21 20:04:31 +00002828 intel_gen4_compute_page_offset(&x, &y, obj->tiling_mode,
Ville Syrjäläb98971272014-08-27 16:51:22 +03002829 pixel_size,
Chris Wilsonbc752862013-02-21 20:04:31 +00002830 fb->pitches[0]);
Daniel Vetterc2c75132012-07-05 12:17:30 +02002831 linear_offset -= intel_crtc->dspaddr_offset;
Matt Roper8e7d6882015-01-21 16:35:41 -08002832 if (crtc->primary->state->rotation == BIT(DRM_ROTATE_180)) {
Sonika Jindal48404c12014-08-22 14:06:04 +05302833 dspcntr |= DISPPLANE_ROTATE_180;
2834
2835 if (!IS_HASWELL(dev) && !IS_BROADWELL(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002836 x += (intel_crtc->config->pipe_src_w - 1);
2837 y += (intel_crtc->config->pipe_src_h - 1);
Sonika Jindal48404c12014-08-22 14:06:04 +05302838
2839 /* Finding the last pixel of the last line of the display
2840 data and adding to linear_offset*/
2841 linear_offset +=
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02002842 (intel_crtc->config->pipe_src_h - 1) * fb->pitches[0] +
2843 (intel_crtc->config->pipe_src_w - 1) * pixel_size;
Sonika Jindal48404c12014-08-22 14:06:04 +05302844 }
2845 }
2846
2847 I915_WRITE(reg, dspcntr);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002848
Ville Syrjälä01f2c772011-12-20 00:06:49 +02002849 I915_WRITE(DSPSTRIDE(plane), fb->pitches[0]);
Daniel Vetter85ba7b72014-01-24 10:31:44 +01002850 I915_WRITE(DSPSURF(plane),
2851 i915_gem_obj_ggtt_offset(obj) + intel_crtc->dspaddr_offset);
Paulo Zanonib3dc6852013-11-02 21:07:33 -07002852 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiaubc1c91e2012-10-29 12:14:21 +00002853 I915_WRITE(DSPOFFSET(plane), (y << 16) | x);
2854 } else {
2855 I915_WRITE(DSPTILEOFF(plane), (y << 16) | x);
2856 I915_WRITE(DSPLINOFF(plane), linear_offset);
2857 }
Jesse Barnes17638cd2011-06-24 12:19:23 -07002858 POSTING_READ(reg);
Jesse Barnes17638cd2011-06-24 12:19:23 -07002859}
2860
Damien Lespiaub3218032015-02-27 11:15:18 +00002861u32 intel_fb_stride_alignment(struct drm_device *dev, uint64_t fb_modifier,
2862 uint32_t pixel_format)
2863{
2864 u32 bits_per_pixel = drm_format_plane_cpp(pixel_format, 0) * 8;
2865
2866 /*
2867 * The stride is either expressed as a multiple of 64 bytes
2868 * chunks for linear buffers or in number of tiles for tiled
2869 * buffers.
2870 */
2871 switch (fb_modifier) {
2872 case DRM_FORMAT_MOD_NONE:
2873 return 64;
2874 case I915_FORMAT_MOD_X_TILED:
2875 if (INTEL_INFO(dev)->gen == 2)
2876 return 128;
2877 return 512;
2878 case I915_FORMAT_MOD_Y_TILED:
2879 /* No need to check for old gens and Y tiling since this is
2880 * about the display engine and those will be blocked before
2881 * we get here.
2882 */
2883 return 128;
2884 case I915_FORMAT_MOD_Yf_TILED:
2885 if (bits_per_pixel == 8)
2886 return 64;
2887 else
2888 return 128;
2889 default:
2890 MISSING_CASE(fb_modifier);
2891 return 64;
2892 }
2893}
2894
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002895unsigned long intel_plane_obj_offset(struct intel_plane *intel_plane,
2896 struct drm_i915_gem_object *obj)
2897{
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002898 const struct i915_ggtt_view *view = &i915_ggtt_view_normal;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002899
2900 if (intel_rotation_90_or_270(intel_plane->base.state->rotation))
Joonas Lahtinen9abc4642015-03-27 13:09:22 +02002901 view = &i915_ggtt_view_rotated;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00002902
2903 return i915_gem_obj_ggtt_offset_view(obj, view);
2904}
2905
Chandra Kondurua1b22782015-04-07 15:28:45 -07002906/*
2907 * This function detaches (aka. unbinds) unused scalers in hardware
2908 */
2909void skl_detach_scalers(struct intel_crtc *intel_crtc)
2910{
2911 struct drm_device *dev;
2912 struct drm_i915_private *dev_priv;
2913 struct intel_crtc_scaler_state *scaler_state;
2914 int i;
2915
2916 if (!intel_crtc || !intel_crtc->config)
2917 return;
2918
2919 dev = intel_crtc->base.dev;
2920 dev_priv = dev->dev_private;
2921 scaler_state = &intel_crtc->config->scaler_state;
2922
2923 /* loop through and disable scalers that aren't in use */
2924 for (i = 0; i < intel_crtc->num_scalers; i++) {
2925 if (!scaler_state->scalers[i].in_use) {
2926 I915_WRITE(SKL_PS_CTRL(intel_crtc->pipe, i), 0);
2927 I915_WRITE(SKL_PS_WIN_POS(intel_crtc->pipe, i), 0);
2928 I915_WRITE(SKL_PS_WIN_SZ(intel_crtc->pipe, i), 0);
2929 DRM_DEBUG_KMS("CRTC:%d Disabled scaler id %u.%u\n",
2930 intel_crtc->base.base.id, intel_crtc->pipe, i);
2931 }
2932 }
2933}
2934
Chandra Konduru6156a452015-04-27 13:48:39 -07002935u32 skl_plane_ctl_format(uint32_t pixel_format)
2936{
Chandra Konduru6156a452015-04-27 13:48:39 -07002937 switch (pixel_format) {
Damien Lespiaud161cf72015-05-12 16:13:17 +01002938 case DRM_FORMAT_C8:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002939 return PLANE_CTL_FORMAT_INDEXED;
Chandra Konduru6156a452015-04-27 13:48:39 -07002940 case DRM_FORMAT_RGB565:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002941 return PLANE_CTL_FORMAT_RGB_565;
Chandra Konduru6156a452015-04-27 13:48:39 -07002942 case DRM_FORMAT_XBGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002943 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX;
Chandra Konduru6156a452015-04-27 13:48:39 -07002944 case DRM_FORMAT_XRGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002945 return PLANE_CTL_FORMAT_XRGB_8888;
Chandra Konduru6156a452015-04-27 13:48:39 -07002946 /*
2947 * XXX: For ARBG/ABGR formats we default to expecting scanout buffers
2948 * to be already pre-multiplied. We need to add a knob (or a different
2949 * DRM_FORMAT) for user-space to configure that.
2950 */
2951 case DRM_FORMAT_ABGR8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002952 return PLANE_CTL_FORMAT_XRGB_8888 | PLANE_CTL_ORDER_RGBX |
Chandra Konduru6156a452015-04-27 13:48:39 -07002953 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002954 case DRM_FORMAT_ARGB8888:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002955 return PLANE_CTL_FORMAT_XRGB_8888 |
Chandra Konduru6156a452015-04-27 13:48:39 -07002956 PLANE_CTL_ALPHA_SW_PREMULTIPLY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002957 case DRM_FORMAT_XRGB2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002958 return PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002959 case DRM_FORMAT_XBGR2101010:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002960 return PLANE_CTL_ORDER_RGBX | PLANE_CTL_FORMAT_XRGB_2101010;
Chandra Konduru6156a452015-04-27 13:48:39 -07002961 case DRM_FORMAT_YUYV:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002962 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YUYV;
Chandra Konduru6156a452015-04-27 13:48:39 -07002963 case DRM_FORMAT_YVYU:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002964 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_YVYU;
Chandra Konduru6156a452015-04-27 13:48:39 -07002965 case DRM_FORMAT_UYVY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002966 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_UYVY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002967 case DRM_FORMAT_VYUY:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002968 return PLANE_CTL_FORMAT_YUV422 | PLANE_CTL_YUV422_VYUY;
Chandra Konduru6156a452015-04-27 13:48:39 -07002969 default:
Damien Lespiau4249eee2015-05-12 16:13:16 +01002970 MISSING_CASE(pixel_format);
Chandra Konduru6156a452015-04-27 13:48:39 -07002971 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002972
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002973 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002974}
2975
2976u32 skl_plane_ctl_tiling(uint64_t fb_modifier)
2977{
Chandra Konduru6156a452015-04-27 13:48:39 -07002978 switch (fb_modifier) {
2979 case DRM_FORMAT_MOD_NONE:
2980 break;
2981 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002982 return PLANE_CTL_TILED_X;
Chandra Konduru6156a452015-04-27 13:48:39 -07002983 case I915_FORMAT_MOD_Y_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002984 return PLANE_CTL_TILED_Y;
Chandra Konduru6156a452015-04-27 13:48:39 -07002985 case I915_FORMAT_MOD_Yf_TILED:
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002986 return PLANE_CTL_TILED_YF;
Chandra Konduru6156a452015-04-27 13:48:39 -07002987 default:
2988 MISSING_CASE(fb_modifier);
2989 }
Damien Lespiau8cfcba42015-05-12 16:13:14 +01002990
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01002991 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07002992}
2993
2994u32 skl_plane_ctl_rotation(unsigned int rotation)
2995{
Chandra Konduru6156a452015-04-27 13:48:39 -07002996 switch (rotation) {
2997 case BIT(DRM_ROTATE_0):
2998 break;
Sonika Jindal1e8df162015-05-20 13:40:48 +05302999 /*
3000 * DRM_ROTATE_ is counter clockwise to stay compatible with Xrandr
3001 * while i915 HW rotation is clockwise, thats why this swapping.
3002 */
Chandra Konduru6156a452015-04-27 13:48:39 -07003003 case BIT(DRM_ROTATE_90):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303004 return PLANE_CTL_ROTATE_270;
Chandra Konduru6156a452015-04-27 13:48:39 -07003005 case BIT(DRM_ROTATE_180):
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003006 return PLANE_CTL_ROTATE_180;
Chandra Konduru6156a452015-04-27 13:48:39 -07003007 case BIT(DRM_ROTATE_270):
Sonika Jindal1e8df162015-05-20 13:40:48 +05303008 return PLANE_CTL_ROTATE_90;
Chandra Konduru6156a452015-04-27 13:48:39 -07003009 default:
3010 MISSING_CASE(rotation);
3011 }
3012
Damien Lespiauc34ce3d2015-05-15 15:07:02 +01003013 return 0;
Chandra Konduru6156a452015-04-27 13:48:39 -07003014}
3015
Damien Lespiau70d21f02013-07-03 21:06:04 +01003016static void skylake_update_primary_plane(struct drm_crtc *crtc,
3017 struct drm_framebuffer *fb,
3018 int x, int y)
3019{
3020 struct drm_device *dev = crtc->dev;
3021 struct drm_i915_private *dev_priv = dev->dev_private;
3022 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003023 struct drm_plane *plane = crtc->primary;
3024 bool visible = to_intel_plane_state(plane->state)->visible;
Damien Lespiau70d21f02013-07-03 21:06:04 +01003025 struct drm_i915_gem_object *obj;
3026 int pipe = intel_crtc->pipe;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303027 u32 plane_ctl, stride_div, stride;
3028 u32 tile_height, plane_offset, plane_size;
3029 unsigned int rotation;
3030 int x_offset, y_offset;
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003031 unsigned long surf_addr;
Chandra Konduru6156a452015-04-27 13:48:39 -07003032 struct intel_crtc_state *crtc_state = intel_crtc->config;
3033 struct intel_plane_state *plane_state;
3034 int src_x = 0, src_y = 0, src_w = 0, src_h = 0;
3035 int dst_x = 0, dst_y = 0, dst_w = 0, dst_h = 0;
3036 int scaler_id = -1;
3037
Chandra Konduru6156a452015-04-27 13:48:39 -07003038 plane_state = to_intel_plane_state(plane->state);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003039
Maarten Lankhorstb70709a2015-04-21 17:12:53 +03003040 if (!visible || !fb) {
Damien Lespiau70d21f02013-07-03 21:06:04 +01003041 I915_WRITE(PLANE_CTL(pipe, 0), 0);
3042 I915_WRITE(PLANE_SURF(pipe, 0), 0);
3043 POSTING_READ(PLANE_CTL(pipe, 0));
3044 return;
3045 }
3046
3047 plane_ctl = PLANE_CTL_ENABLE |
3048 PLANE_CTL_PIPE_GAMMA_ENABLE |
3049 PLANE_CTL_PIPE_CSC_ENABLE;
3050
Chandra Konduru6156a452015-04-27 13:48:39 -07003051 plane_ctl |= skl_plane_ctl_format(fb->pixel_format);
3052 plane_ctl |= skl_plane_ctl_tiling(fb->modifier[0]);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003053 plane_ctl |= PLANE_CTL_PLANE_GAMMA_DISABLE;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303054
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303055 rotation = plane->state->rotation;
Chandra Konduru6156a452015-04-27 13:48:39 -07003056 plane_ctl |= skl_plane_ctl_rotation(rotation);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003057
Damien Lespiaub3218032015-02-27 11:15:18 +00003058 obj = intel_fb_obj(fb);
3059 stride_div = intel_fb_stride_alignment(dev, fb->modifier[0],
3060 fb->pixel_format);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303061 surf_addr = intel_plane_obj_offset(to_intel_plane(plane), obj);
3062
Chandra Konduru6156a452015-04-27 13:48:39 -07003063 /*
3064 * FIXME: intel_plane_state->src, dst aren't set when transitional
3065 * update_plane helpers are called from legacy paths.
3066 * Once full atomic crtc is available, below check can be avoided.
3067 */
3068 if (drm_rect_width(&plane_state->src)) {
3069 scaler_id = plane_state->scaler_id;
3070 src_x = plane_state->src.x1 >> 16;
3071 src_y = plane_state->src.y1 >> 16;
3072 src_w = drm_rect_width(&plane_state->src) >> 16;
3073 src_h = drm_rect_height(&plane_state->src) >> 16;
3074 dst_x = plane_state->dst.x1;
3075 dst_y = plane_state->dst.y1;
3076 dst_w = drm_rect_width(&plane_state->dst);
3077 dst_h = drm_rect_height(&plane_state->dst);
3078
3079 WARN_ON(x != src_x || y != src_y);
3080 } else {
3081 src_w = intel_crtc->config->pipe_src_w;
3082 src_h = intel_crtc->config->pipe_src_h;
3083 }
3084
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303085 if (intel_rotation_90_or_270(rotation)) {
3086 /* stride = Surface height in tiles */
Chandra Konduru2614f172015-05-08 20:22:46 -07003087 tile_height = intel_tile_height(dev, fb->pixel_format,
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303088 fb->modifier[0]);
3089 stride = DIV_ROUND_UP(fb->height, tile_height);
Chandra Konduru6156a452015-04-27 13:48:39 -07003090 x_offset = stride * tile_height - y - src_h;
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303091 y_offset = x;
Chandra Konduru6156a452015-04-27 13:48:39 -07003092 plane_size = (src_w - 1) << 16 | (src_h - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303093 } else {
3094 stride = fb->pitches[0] / stride_div;
3095 x_offset = x;
3096 y_offset = y;
Chandra Konduru6156a452015-04-27 13:48:39 -07003097 plane_size = (src_h - 1) << 16 | (src_w - 1);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303098 }
3099 plane_offset = y_offset << 16 | x_offset;
Damien Lespiaub3218032015-02-27 11:15:18 +00003100
Damien Lespiau70d21f02013-07-03 21:06:04 +01003101 I915_WRITE(PLANE_CTL(pipe, 0), plane_ctl);
Sonika Jindal3b7a5112015-04-10 14:37:29 +05303102 I915_WRITE(PLANE_OFFSET(pipe, 0), plane_offset);
3103 I915_WRITE(PLANE_SIZE(pipe, 0), plane_size);
3104 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
Chandra Konduru6156a452015-04-27 13:48:39 -07003105
3106 if (scaler_id >= 0) {
3107 uint32_t ps_ctrl = 0;
3108
3109 WARN_ON(!dst_w || !dst_h);
3110 ps_ctrl = PS_SCALER_EN | PS_PLANE_SEL(0) |
3111 crtc_state->scaler_state.scalers[scaler_id].mode;
3112 I915_WRITE(SKL_PS_CTRL(pipe, scaler_id), ps_ctrl);
3113 I915_WRITE(SKL_PS_PWR_GATE(pipe, scaler_id), 0);
3114 I915_WRITE(SKL_PS_WIN_POS(pipe, scaler_id), (dst_x << 16) | dst_y);
3115 I915_WRITE(SKL_PS_WIN_SZ(pipe, scaler_id), (dst_w << 16) | dst_h);
3116 I915_WRITE(PLANE_POS(pipe, 0), 0);
3117 } else {
3118 I915_WRITE(PLANE_POS(pipe, 0), (dst_y << 16) | dst_x);
3119 }
3120
Tvrtko Ursulin121920f2015-03-23 11:10:37 +00003121 I915_WRITE(PLANE_SURF(pipe, 0), surf_addr);
Damien Lespiau70d21f02013-07-03 21:06:04 +01003122
3123 POSTING_READ(PLANE_SURF(pipe, 0));
3124}
3125
Jesse Barnes17638cd2011-06-24 12:19:23 -07003126/* Assume fb object is pinned & idle & fenced and just update base pointers */
3127static int
3128intel_pipe_set_base_atomic(struct drm_crtc *crtc, struct drm_framebuffer *fb,
3129 int x, int y, enum mode_set_atomic state)
3130{
3131 struct drm_device *dev = crtc->dev;
3132 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes17638cd2011-06-24 12:19:23 -07003133
Chris Wilson6b8e6ed2012-04-17 15:08:19 +01003134 if (dev_priv->display.disable_fbc)
3135 dev_priv->display.disable_fbc(dev);
Jesse Barnes81255562010-08-02 12:07:50 -07003136
Daniel Vetter29b9bde2014-04-24 23:55:01 +02003137 dev_priv->display.update_primary_plane(crtc, fb, x, y);
3138
3139 return 0;
Jesse Barnes81255562010-08-02 12:07:50 -07003140}
3141
Ville Syrjälä75147472014-11-24 18:28:11 +02003142static void intel_complete_page_flips(struct drm_device *dev)
Ville Syrjälä96a02912013-02-18 19:08:49 +02003143{
Ville Syrjälä96a02912013-02-18 19:08:49 +02003144 struct drm_crtc *crtc;
3145
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003146 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003147 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3148 enum plane plane = intel_crtc->plane;
3149
3150 intel_prepare_page_flip(dev, plane);
3151 intel_finish_page_flip_plane(dev, plane);
3152 }
Ville Syrjälä75147472014-11-24 18:28:11 +02003153}
3154
3155static void intel_update_primary_planes(struct drm_device *dev)
3156{
3157 struct drm_i915_private *dev_priv = dev->dev_private;
3158 struct drm_crtc *crtc;
Ville Syrjälä96a02912013-02-18 19:08:49 +02003159
Damien Lespiau70e1e0e2014-05-13 23:32:24 +01003160 for_each_crtc(dev, crtc) {
Ville Syrjälä96a02912013-02-18 19:08:49 +02003161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3162
Rob Clark51fd3712013-11-19 12:10:12 -05003163 drm_modeset_lock(&crtc->mutex, NULL);
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003164 /*
3165 * FIXME: Once we have proper support for primary planes (and
3166 * disabling them without disabling the entire crtc) allow again
Dave Airlie66e514c2014-04-03 07:51:54 +10003167 * a NULL crtc->primary->fb.
Chris Wilson947fdaadf2013-11-27 12:01:32 +00003168 */
Matt Roperf4510a22014-04-01 15:22:40 -07003169 if (intel_crtc->active && crtc->primary->fb)
Matt Roper262ca2b2014-03-18 17:22:55 -07003170 dev_priv->display.update_primary_plane(crtc,
Dave Airlie66e514c2014-04-03 07:51:54 +10003171 crtc->primary->fb,
Matt Roper262ca2b2014-03-18 17:22:55 -07003172 crtc->x,
3173 crtc->y);
Rob Clark51fd3712013-11-19 12:10:12 -05003174 drm_modeset_unlock(&crtc->mutex);
Ville Syrjälä96a02912013-02-18 19:08:49 +02003175 }
3176}
3177
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03003178void intel_crtc_reset(struct intel_crtc *crtc)
3179{
3180 struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3181
3182 if (!crtc->active)
3183 return;
3184
3185 intel_crtc_disable_planes(&crtc->base);
3186 dev_priv->display.crtc_disable(&crtc->base);
3187 dev_priv->display.crtc_enable(&crtc->base);
3188 intel_crtc_enable_planes(&crtc->base);
3189}
3190
Ville Syrjälä75147472014-11-24 18:28:11 +02003191void intel_prepare_reset(struct drm_device *dev)
3192{
3193 /* no reset support for gen2 */
3194 if (IS_GEN2(dev))
3195 return;
3196
3197 /* reset doesn't touch the display */
3198 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
3199 return;
3200
3201 drm_modeset_lock_all(dev);
Ville Syrjäläf98ce922014-11-21 21:54:30 +02003202
3203 /*
3204 * Disabling the crtcs gracefully seems nicer. Also the
3205 * g33 docs say we should at least disable all the planes.
3206 */
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02003207 intel_display_suspend(dev);
Ville Syrjälä75147472014-11-24 18:28:11 +02003208}
3209
3210void intel_finish_reset(struct drm_device *dev)
3211{
3212 struct drm_i915_private *dev_priv = to_i915(dev);
3213
3214 /*
3215 * Flips in the rings will be nuked by the reset,
3216 * so complete all pending flips so that user space
3217 * will get its events and not get stuck.
3218 */
3219 intel_complete_page_flips(dev);
3220
3221 /* no reset support for gen2 */
3222 if (IS_GEN2(dev))
3223 return;
3224
3225 /* reset doesn't touch the display */
3226 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev)) {
3227 /*
3228 * Flips in the rings have been nuked by the reset,
3229 * so update the base address of all primary
3230 * planes to the the last fb to make sure we're
3231 * showing the correct fb after a reset.
3232 */
3233 intel_update_primary_planes(dev);
3234 return;
3235 }
3236
3237 /*
3238 * The display has been reset as well,
3239 * so need a full re-initialization.
3240 */
3241 intel_runtime_pm_disable_interrupts(dev_priv);
3242 intel_runtime_pm_enable_interrupts(dev_priv);
3243
3244 intel_modeset_init_hw(dev);
3245
3246 spin_lock_irq(&dev_priv->irq_lock);
3247 if (dev_priv->display.hpd_irq_setup)
3248 dev_priv->display.hpd_irq_setup(dev);
3249 spin_unlock_irq(&dev_priv->irq_lock);
3250
3251 intel_modeset_setup_hw_state(dev, true);
3252
3253 intel_hpd_init(dev_priv);
3254
3255 drm_modeset_unlock_all(dev);
3256}
3257
Chris Wilson2e2f3512015-04-27 13:41:14 +01003258static void
Chris Wilson14667a42012-04-03 17:58:35 +01003259intel_finish_fb(struct drm_framebuffer *old_fb)
3260{
Matt Roper2ff8fde2014-07-08 07:50:07 -07003261 struct drm_i915_gem_object *obj = intel_fb_obj(old_fb);
Chris Wilson2e2f3512015-04-27 13:41:14 +01003262 struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
Chris Wilson14667a42012-04-03 17:58:35 +01003263 bool was_interruptible = dev_priv->mm.interruptible;
3264 int ret;
3265
Chris Wilson14667a42012-04-03 17:58:35 +01003266 /* Big Hammer, we also need to ensure that any pending
3267 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
3268 * current scanout is retired before unpinning the old
Chris Wilson2e2f3512015-04-27 13:41:14 +01003269 * framebuffer. Note that we rely on userspace rendering
3270 * into the buffer attached to the pipe they are waiting
3271 * on. If not, userspace generates a GPU hang with IPEHR
3272 * point to the MI_WAIT_FOR_EVENT.
Chris Wilson14667a42012-04-03 17:58:35 +01003273 *
3274 * This should only fail upon a hung GPU, in which case we
3275 * can safely continue.
3276 */
3277 dev_priv->mm.interruptible = false;
Chris Wilson2e2f3512015-04-27 13:41:14 +01003278 ret = i915_gem_object_wait_rendering(obj, true);
Chris Wilson14667a42012-04-03 17:58:35 +01003279 dev_priv->mm.interruptible = was_interruptible;
3280
Chris Wilson2e2f3512015-04-27 13:41:14 +01003281 WARN_ON(ret);
Chris Wilson14667a42012-04-03 17:58:35 +01003282}
3283
Chris Wilson7d5e3792014-03-04 13:15:08 +00003284static bool intel_crtc_has_pending_flip(struct drm_crtc *crtc)
3285{
3286 struct drm_device *dev = crtc->dev;
3287 struct drm_i915_private *dev_priv = dev->dev_private;
3288 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003289 bool pending;
3290
3291 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
3292 intel_crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
3293 return false;
3294
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003295 spin_lock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003296 pending = to_intel_crtc(crtc)->unpin_work != NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003297 spin_unlock_irq(&dev->event_lock);
Chris Wilson7d5e3792014-03-04 13:15:08 +00003298
3299 return pending;
3300}
3301
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003302static void intel_update_pipe_size(struct intel_crtc *crtc)
3303{
3304 struct drm_device *dev = crtc->base.dev;
3305 struct drm_i915_private *dev_priv = dev->dev_private;
3306 const struct drm_display_mode *adjusted_mode;
3307
3308 if (!i915.fastboot)
3309 return;
3310
3311 /*
3312 * Update pipe size and adjust fitter if needed: the reason for this is
3313 * that in compute_mode_changes we check the native mode (not the pfit
3314 * mode) to see if we can flip rather than do a full mode set. In the
3315 * fastboot case, we'll flip, but if we don't update the pipesrc and
3316 * pfit state, we'll end up with a big fb scanned out into the wrong
3317 * sized surface.
3318 *
3319 * To fix this properly, we need to hoist the checks up into
3320 * compute_mode_changes (or above), check the actual pfit state and
3321 * whether the platform allows pfit disable with pipe active, and only
3322 * then update the pipesrc and pfit state, even on the flip path.
3323 */
3324
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003325 adjusted_mode = &crtc->config->base.adjusted_mode;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003326
3327 I915_WRITE(PIPESRC(crtc->pipe),
3328 ((adjusted_mode->crtc_hdisplay - 1) << 16) |
3329 (adjusted_mode->crtc_vdisplay - 1));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003330 if (!crtc->config->pch_pfit.enabled &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03003331 (intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS) ||
3332 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))) {
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003333 I915_WRITE(PF_CTL(crtc->pipe), 0);
3334 I915_WRITE(PF_WIN_POS(crtc->pipe), 0);
3335 I915_WRITE(PF_WIN_SZ(crtc->pipe), 0);
3336 }
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003337 crtc->config->pipe_src_w = adjusted_mode->crtc_hdisplay;
3338 crtc->config->pipe_src_h = adjusted_mode->crtc_vdisplay;
Gustavo Padovane30e8f72014-09-10 12:04:17 -03003339}
3340
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003341static void intel_fdi_normal_train(struct drm_crtc *crtc)
3342{
3343 struct drm_device *dev = crtc->dev;
3344 struct drm_i915_private *dev_priv = dev->dev_private;
3345 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3346 int pipe = intel_crtc->pipe;
3347 u32 reg, temp;
3348
3349 /* enable normal train */
3350 reg = FDI_TX_CTL(pipe);
3351 temp = I915_READ(reg);
Keith Packard61e499b2011-05-17 16:13:52 -07003352 if (IS_IVYBRIDGE(dev)) {
Jesse Barnes357555c2011-04-28 15:09:55 -07003353 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3354 temp |= FDI_LINK_TRAIN_NONE_IVB | FDI_TX_ENHANCE_FRAME_ENABLE;
Keith Packard61e499b2011-05-17 16:13:52 -07003355 } else {
3356 temp &= ~FDI_LINK_TRAIN_NONE;
3357 temp |= FDI_LINK_TRAIN_NONE | FDI_TX_ENHANCE_FRAME_ENABLE;
Jesse Barnes357555c2011-04-28 15:09:55 -07003358 }
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003359 I915_WRITE(reg, temp);
3360
3361 reg = FDI_RX_CTL(pipe);
3362 temp = I915_READ(reg);
3363 if (HAS_PCH_CPT(dev)) {
3364 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3365 temp |= FDI_LINK_TRAIN_NORMAL_CPT;
3366 } else {
3367 temp &= ~FDI_LINK_TRAIN_NONE;
3368 temp |= FDI_LINK_TRAIN_NONE;
3369 }
3370 I915_WRITE(reg, temp | FDI_RX_ENHANCE_FRAME_ENABLE);
3371
3372 /* wait one idle pattern time */
3373 POSTING_READ(reg);
3374 udelay(1000);
Jesse Barnes357555c2011-04-28 15:09:55 -07003375
3376 /* IVB wants error correction enabled */
3377 if (IS_IVYBRIDGE(dev))
3378 I915_WRITE(reg, I915_READ(reg) | FDI_FS_ERRC_ENABLE |
3379 FDI_FE_ERRC_ENABLE);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08003380}
3381
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003382/* The FDI link training functions for ILK/Ibexpeak. */
3383static void ironlake_fdi_link_train(struct drm_crtc *crtc)
3384{
3385 struct drm_device *dev = crtc->dev;
3386 struct drm_i915_private *dev_priv = dev->dev_private;
3387 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3388 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003389 u32 reg, temp, tries;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003390
Ville Syrjälä1c8562f2014-04-25 22:12:07 +03003391 /* FDI needs bits from pipe first */
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003392 assert_pipe_enabled(dev_priv, pipe);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003393
Adam Jacksone1a44742010-06-25 15:32:14 -04003394 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3395 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003396 reg = FDI_RX_IMR(pipe);
3397 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003398 temp &= ~FDI_RX_SYMBOL_LOCK;
3399 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003400 I915_WRITE(reg, temp);
3401 I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003402 udelay(150);
3403
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003404 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003405 reg = FDI_TX_CTL(pipe);
3406 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003407 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003408 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003409 temp &= ~FDI_LINK_TRAIN_NONE;
3410 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003411 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003412
Chris Wilson5eddb702010-09-11 13:48:45 +01003413 reg = FDI_RX_CTL(pipe);
3414 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003415 temp &= ~FDI_LINK_TRAIN_NONE;
3416 temp |= FDI_LINK_TRAIN_PATTERN_1;
Chris Wilson5eddb702010-09-11 13:48:45 +01003417 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3418
3419 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003420 udelay(150);
3421
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003422 /* Ironlake workaround, enable clock pointer after FDI enable*/
Daniel Vetter8f5718a2012-10-31 22:52:28 +01003423 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
3424 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR |
3425 FDI_RX_PHASE_SYNC_POINTER_EN);
Jesse Barnes5b2adf82010-10-07 16:01:15 -07003426
Chris Wilson5eddb702010-09-11 13:48:45 +01003427 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003428 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003429 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003430 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3431
3432 if ((temp & FDI_RX_BIT_LOCK)) {
3433 DRM_DEBUG_KMS("FDI train 1 done.\n");
Chris Wilson5eddb702010-09-11 13:48:45 +01003434 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003435 break;
3436 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003437 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003438 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003439 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003440
3441 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003442 reg = FDI_TX_CTL(pipe);
3443 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003444 temp &= ~FDI_LINK_TRAIN_NONE;
3445 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003446 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003447
Chris Wilson5eddb702010-09-11 13:48:45 +01003448 reg = FDI_RX_CTL(pipe);
3449 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003450 temp &= ~FDI_LINK_TRAIN_NONE;
3451 temp |= FDI_LINK_TRAIN_PATTERN_2;
Chris Wilson5eddb702010-09-11 13:48:45 +01003452 I915_WRITE(reg, temp);
3453
3454 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003455 udelay(150);
3456
Chris Wilson5eddb702010-09-11 13:48:45 +01003457 reg = FDI_RX_IIR(pipe);
Adam Jacksone1a44742010-06-25 15:32:14 -04003458 for (tries = 0; tries < 5; tries++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003459 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003460 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3461
3462 if (temp & FDI_RX_SYMBOL_LOCK) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003463 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003464 DRM_DEBUG_KMS("FDI train 2 done.\n");
3465 break;
3466 }
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003467 }
Adam Jacksone1a44742010-06-25 15:32:14 -04003468 if (tries == 5)
Chris Wilson5eddb702010-09-11 13:48:45 +01003469 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003470
3471 DRM_DEBUG_KMS("FDI train done\n");
Jesse Barnes5c5313c2010-10-07 16:01:11 -07003472
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003473}
3474
Akshay Joshi0206e352011-08-16 15:34:10 -04003475static const int snb_b_fdi_train_param[] = {
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003476 FDI_LINK_TRAIN_400MV_0DB_SNB_B,
3477 FDI_LINK_TRAIN_400MV_6DB_SNB_B,
3478 FDI_LINK_TRAIN_600MV_3_5DB_SNB_B,
3479 FDI_LINK_TRAIN_800MV_0DB_SNB_B,
3480};
3481
3482/* The FDI link training functions for SNB/Cougarpoint. */
3483static void gen6_fdi_link_train(struct drm_crtc *crtc)
3484{
3485 struct drm_device *dev = crtc->dev;
3486 struct drm_i915_private *dev_priv = dev->dev_private;
3487 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3488 int pipe = intel_crtc->pipe;
Sean Paulfa37d392012-03-02 12:53:39 -05003489 u32 reg, temp, i, retry;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003490
Adam Jacksone1a44742010-06-25 15:32:14 -04003491 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3492 for train result */
Chris Wilson5eddb702010-09-11 13:48:45 +01003493 reg = FDI_RX_IMR(pipe);
3494 temp = I915_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003495 temp &= ~FDI_RX_SYMBOL_LOCK;
3496 temp &= ~FDI_RX_BIT_LOCK;
Chris Wilson5eddb702010-09-11 13:48:45 +01003497 I915_WRITE(reg, temp);
3498
3499 POSTING_READ(reg);
Adam Jacksone1a44742010-06-25 15:32:14 -04003500 udelay(150);
3501
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003502 /* enable CPU FDI TX and PCH FDI RX */
Chris Wilson5eddb702010-09-11 13:48:45 +01003503 reg = FDI_TX_CTL(pipe);
3504 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003505 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003506 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003507 temp &= ~FDI_LINK_TRAIN_NONE;
3508 temp |= FDI_LINK_TRAIN_PATTERN_1;
3509 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3510 /* SNB-B */
3511 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
Chris Wilson5eddb702010-09-11 13:48:45 +01003512 I915_WRITE(reg, temp | FDI_TX_ENABLE);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003513
Daniel Vetterd74cf322012-10-26 10:58:13 +02003514 I915_WRITE(FDI_RX_MISC(pipe),
3515 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3516
Chris Wilson5eddb702010-09-11 13:48:45 +01003517 reg = FDI_RX_CTL(pipe);
3518 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003519 if (HAS_PCH_CPT(dev)) {
3520 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3521 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3522 } else {
3523 temp &= ~FDI_LINK_TRAIN_NONE;
3524 temp |= FDI_LINK_TRAIN_PATTERN_1;
3525 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003526 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3527
3528 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003529 udelay(150);
3530
Akshay Joshi0206e352011-08-16 15:34:10 -04003531 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003532 reg = FDI_TX_CTL(pipe);
3533 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003534 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3535 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003536 I915_WRITE(reg, temp);
3537
3538 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003539 udelay(500);
3540
Sean Paulfa37d392012-03-02 12:53:39 -05003541 for (retry = 0; retry < 5; retry++) {
3542 reg = FDI_RX_IIR(pipe);
3543 temp = I915_READ(reg);
3544 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3545 if (temp & FDI_RX_BIT_LOCK) {
3546 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3547 DRM_DEBUG_KMS("FDI train 1 done.\n");
3548 break;
3549 }
3550 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003551 }
Sean Paulfa37d392012-03-02 12:53:39 -05003552 if (retry < 5)
3553 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003554 }
3555 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003556 DRM_ERROR("FDI train 1 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003557
3558 /* Train 2 */
Chris Wilson5eddb702010-09-11 13:48:45 +01003559 reg = FDI_TX_CTL(pipe);
3560 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003561 temp &= ~FDI_LINK_TRAIN_NONE;
3562 temp |= FDI_LINK_TRAIN_PATTERN_2;
3563 if (IS_GEN6(dev)) {
3564 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3565 /* SNB-B */
3566 temp |= FDI_LINK_TRAIN_400MV_0DB_SNB_B;
3567 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003568 I915_WRITE(reg, temp);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003569
Chris Wilson5eddb702010-09-11 13:48:45 +01003570 reg = FDI_RX_CTL(pipe);
3571 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003572 if (HAS_PCH_CPT(dev)) {
3573 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3574 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
3575 } else {
3576 temp &= ~FDI_LINK_TRAIN_NONE;
3577 temp |= FDI_LINK_TRAIN_PATTERN_2;
3578 }
Chris Wilson5eddb702010-09-11 13:48:45 +01003579 I915_WRITE(reg, temp);
3580
3581 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003582 udelay(150);
3583
Akshay Joshi0206e352011-08-16 15:34:10 -04003584 for (i = 0; i < 4; i++) {
Chris Wilson5eddb702010-09-11 13:48:45 +01003585 reg = FDI_TX_CTL(pipe);
3586 temp = I915_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003587 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
3588 temp |= snb_b_fdi_train_param[i];
Chris Wilson5eddb702010-09-11 13:48:45 +01003589 I915_WRITE(reg, temp);
3590
3591 POSTING_READ(reg);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003592 udelay(500);
3593
Sean Paulfa37d392012-03-02 12:53:39 -05003594 for (retry = 0; retry < 5; retry++) {
3595 reg = FDI_RX_IIR(pipe);
3596 temp = I915_READ(reg);
3597 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3598 if (temp & FDI_RX_SYMBOL_LOCK) {
3599 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3600 DRM_DEBUG_KMS("FDI train 2 done.\n");
3601 break;
3602 }
3603 udelay(50);
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003604 }
Sean Paulfa37d392012-03-02 12:53:39 -05003605 if (retry < 5)
3606 break;
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003607 }
3608 if (i == 4)
Chris Wilson5eddb702010-09-11 13:48:45 +01003609 DRM_ERROR("FDI train 2 fail!\n");
Zhenyu Wang8db9d772010-04-07 16:15:54 +08003610
3611 DRM_DEBUG_KMS("FDI train done.\n");
3612}
3613
Jesse Barnes357555c2011-04-28 15:09:55 -07003614/* Manual link training for Ivy Bridge A0 parts */
3615static void ivb_manual_fdi_link_train(struct drm_crtc *crtc)
3616{
3617 struct drm_device *dev = crtc->dev;
3618 struct drm_i915_private *dev_priv = dev->dev_private;
3619 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3620 int pipe = intel_crtc->pipe;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003621 u32 reg, temp, i, j;
Jesse Barnes357555c2011-04-28 15:09:55 -07003622
3623 /* Train 1: umask FDI RX Interrupt symbol_lock and bit_lock bit
3624 for train result */
3625 reg = FDI_RX_IMR(pipe);
3626 temp = I915_READ(reg);
3627 temp &= ~FDI_RX_SYMBOL_LOCK;
3628 temp &= ~FDI_RX_BIT_LOCK;
3629 I915_WRITE(reg, temp);
3630
3631 POSTING_READ(reg);
3632 udelay(150);
3633
Daniel Vetter01a415f2012-10-27 15:58:40 +02003634 DRM_DEBUG_KMS("FDI_RX_IIR before link train 0x%x\n",
3635 I915_READ(FDI_RX_IIR(pipe)));
3636
Jesse Barnes139ccd32013-08-19 11:04:55 -07003637 /* Try each vswing and preemphasis setting twice before moving on */
3638 for (j = 0; j < ARRAY_SIZE(snb_b_fdi_train_param) * 2; j++) {
3639 /* disable first in case we need to retry */
Jesse Barnes357555c2011-04-28 15:09:55 -07003640 reg = FDI_TX_CTL(pipe);
3641 temp = I915_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003642 temp &= ~(FDI_LINK_TRAIN_AUTO | FDI_LINK_TRAIN_NONE_IVB);
3643 temp &= ~FDI_TX_ENABLE;
3644 I915_WRITE(reg, temp);
3645
3646 reg = FDI_RX_CTL(pipe);
3647 temp = I915_READ(reg);
3648 temp &= ~FDI_LINK_TRAIN_AUTO;
3649 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3650 temp &= ~FDI_RX_ENABLE;
3651 I915_WRITE(reg, temp);
3652
3653 /* enable CPU FDI TX and PCH FDI RX */
3654 reg = FDI_TX_CTL(pipe);
3655 temp = I915_READ(reg);
3656 temp &= ~FDI_DP_PORT_WIDTH_MASK;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003657 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003658 temp |= FDI_LINK_TRAIN_PATTERN_1_IVB;
Jesse Barnes357555c2011-04-28 15:09:55 -07003659 temp &= ~FDI_LINK_TRAIN_VOL_EMP_MASK;
Jesse Barnes139ccd32013-08-19 11:04:55 -07003660 temp |= snb_b_fdi_train_param[j/2];
3661 temp |= FDI_COMPOSITE_SYNC;
3662 I915_WRITE(reg, temp | FDI_TX_ENABLE);
3663
3664 I915_WRITE(FDI_RX_MISC(pipe),
3665 FDI_RX_TP1_TO_TP2_48 | FDI_RX_FDI_DELAY_90);
3666
3667 reg = FDI_RX_CTL(pipe);
3668 temp = I915_READ(reg);
3669 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3670 temp |= FDI_COMPOSITE_SYNC;
3671 I915_WRITE(reg, temp | FDI_RX_ENABLE);
3672
3673 POSTING_READ(reg);
3674 udelay(1); /* should be 0.5us */
3675
3676 for (i = 0; i < 4; i++) {
3677 reg = FDI_RX_IIR(pipe);
3678 temp = I915_READ(reg);
3679 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
3680
3681 if (temp & FDI_RX_BIT_LOCK ||
3682 (I915_READ(reg) & FDI_RX_BIT_LOCK)) {
3683 I915_WRITE(reg, temp | FDI_RX_BIT_LOCK);
3684 DRM_DEBUG_KMS("FDI train 1 done, level %i.\n",
3685 i);
3686 break;
3687 }
3688 udelay(1); /* should be 0.5us */
3689 }
3690 if (i == 4) {
3691 DRM_DEBUG_KMS("FDI train 1 fail on vswing %d\n", j / 2);
3692 continue;
3693 }
3694
3695 /* Train 2 */
3696 reg = FDI_TX_CTL(pipe);
3697 temp = I915_READ(reg);
3698 temp &= ~FDI_LINK_TRAIN_NONE_IVB;
3699 temp |= FDI_LINK_TRAIN_PATTERN_2_IVB;
3700 I915_WRITE(reg, temp);
3701
3702 reg = FDI_RX_CTL(pipe);
3703 temp = I915_READ(reg);
3704 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3705 temp |= FDI_LINK_TRAIN_PATTERN_2_CPT;
Jesse Barnes357555c2011-04-28 15:09:55 -07003706 I915_WRITE(reg, temp);
3707
3708 POSTING_READ(reg);
Jesse Barnes139ccd32013-08-19 11:04:55 -07003709 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003710
Jesse Barnes139ccd32013-08-19 11:04:55 -07003711 for (i = 0; i < 4; i++) {
3712 reg = FDI_RX_IIR(pipe);
3713 temp = I915_READ(reg);
3714 DRM_DEBUG_KMS("FDI_RX_IIR 0x%x\n", temp);
Jesse Barnes357555c2011-04-28 15:09:55 -07003715
Jesse Barnes139ccd32013-08-19 11:04:55 -07003716 if (temp & FDI_RX_SYMBOL_LOCK ||
3717 (I915_READ(reg) & FDI_RX_SYMBOL_LOCK)) {
3718 I915_WRITE(reg, temp | FDI_RX_SYMBOL_LOCK);
3719 DRM_DEBUG_KMS("FDI train 2 done, level %i.\n",
3720 i);
3721 goto train_done;
3722 }
3723 udelay(2); /* should be 1.5us */
Jesse Barnes357555c2011-04-28 15:09:55 -07003724 }
Jesse Barnes139ccd32013-08-19 11:04:55 -07003725 if (i == 4)
3726 DRM_DEBUG_KMS("FDI train 2 fail on vswing %d\n", j / 2);
Jesse Barnes357555c2011-04-28 15:09:55 -07003727 }
Jesse Barnes357555c2011-04-28 15:09:55 -07003728
Jesse Barnes139ccd32013-08-19 11:04:55 -07003729train_done:
Jesse Barnes357555c2011-04-28 15:09:55 -07003730 DRM_DEBUG_KMS("FDI train done.\n");
3731}
3732
Daniel Vetter88cefb62012-08-12 19:27:14 +02003733static void ironlake_fdi_pll_enable(struct intel_crtc *intel_crtc)
Jesse Barnes0e23b992010-09-10 11:10:00 -07003734{
Daniel Vetter88cefb62012-08-12 19:27:14 +02003735 struct drm_device *dev = intel_crtc->base.dev;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003736 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003737 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01003738 u32 reg, temp;
Jesse Barnes0e23b992010-09-10 11:10:00 -07003739
Jesse Barnesc64e3112010-09-10 11:27:03 -07003740
Jesse Barnes0e23b992010-09-10 11:10:00 -07003741 /* enable PCH FDI RX PLL, wait warmup plus DMI latency */
Chris Wilson5eddb702010-09-11 13:48:45 +01003742 reg = FDI_RX_CTL(pipe);
3743 temp = I915_READ(reg);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02003744 temp &= ~(FDI_DP_PORT_WIDTH_MASK | (0x7 << 16));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003745 temp |= FDI_DP_PORT_WIDTH(intel_crtc->config->fdi_lanes);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003746 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Chris Wilson5eddb702010-09-11 13:48:45 +01003747 I915_WRITE(reg, temp | FDI_RX_PLL_ENABLE);
3748
3749 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003750 udelay(200);
3751
3752 /* Switch from Rawclk to PCDclk */
Chris Wilson5eddb702010-09-11 13:48:45 +01003753 temp = I915_READ(reg);
3754 I915_WRITE(reg, temp | FDI_PCDCLK);
3755
3756 POSTING_READ(reg);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003757 udelay(200);
3758
Paulo Zanoni20749732012-11-23 15:30:38 -02003759 /* Enable CPU FDI TX PLL, always on for Ironlake */
3760 reg = FDI_TX_CTL(pipe);
3761 temp = I915_READ(reg);
3762 if ((temp & FDI_TX_PLL_ENABLE) == 0) {
3763 I915_WRITE(reg, temp | FDI_TX_PLL_ENABLE);
Chris Wilson5eddb702010-09-11 13:48:45 +01003764
Paulo Zanoni20749732012-11-23 15:30:38 -02003765 POSTING_READ(reg);
3766 udelay(100);
Jesse Barnes0e23b992010-09-10 11:10:00 -07003767 }
3768}
3769
Daniel Vetter88cefb62012-08-12 19:27:14 +02003770static void ironlake_fdi_pll_disable(struct intel_crtc *intel_crtc)
3771{
3772 struct drm_device *dev = intel_crtc->base.dev;
3773 struct drm_i915_private *dev_priv = dev->dev_private;
3774 int pipe = intel_crtc->pipe;
3775 u32 reg, temp;
3776
3777 /* Switch from PCDclk to Rawclk */
3778 reg = FDI_RX_CTL(pipe);
3779 temp = I915_READ(reg);
3780 I915_WRITE(reg, temp & ~FDI_PCDCLK);
3781
3782 /* Disable CPU FDI TX PLL */
3783 reg = FDI_TX_CTL(pipe);
3784 temp = I915_READ(reg);
3785 I915_WRITE(reg, temp & ~FDI_TX_PLL_ENABLE);
3786
3787 POSTING_READ(reg);
3788 udelay(100);
3789
3790 reg = FDI_RX_CTL(pipe);
3791 temp = I915_READ(reg);
3792 I915_WRITE(reg, temp & ~FDI_RX_PLL_ENABLE);
3793
3794 /* Wait for the clocks to turn off. */
3795 POSTING_READ(reg);
3796 udelay(100);
3797}
3798
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003799static void ironlake_fdi_disable(struct drm_crtc *crtc)
3800{
3801 struct drm_device *dev = crtc->dev;
3802 struct drm_i915_private *dev_priv = dev->dev_private;
3803 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
3804 int pipe = intel_crtc->pipe;
3805 u32 reg, temp;
3806
3807 /* disable CPU FDI tx and PCH FDI rx */
3808 reg = FDI_TX_CTL(pipe);
3809 temp = I915_READ(reg);
3810 I915_WRITE(reg, temp & ~FDI_TX_ENABLE);
3811 POSTING_READ(reg);
3812
3813 reg = FDI_RX_CTL(pipe);
3814 temp = I915_READ(reg);
3815 temp &= ~(0x7 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003816 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003817 I915_WRITE(reg, temp & ~FDI_RX_ENABLE);
3818
3819 POSTING_READ(reg);
3820 udelay(100);
3821
3822 /* Ironlake workaround, disable clock pointer after downing FDI */
Robin Schroereba905b2014-05-18 02:24:50 +02003823 if (HAS_PCH_IBX(dev))
Jesse Barnes6f06ce12011-01-04 15:09:38 -08003824 I915_WRITE(FDI_RX_CHICKEN(pipe), FDI_RX_PHASE_SYNC_POINTER_OVR);
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003825
3826 /* still set train pattern 1 */
3827 reg = FDI_TX_CTL(pipe);
3828 temp = I915_READ(reg);
3829 temp &= ~FDI_LINK_TRAIN_NONE;
3830 temp |= FDI_LINK_TRAIN_PATTERN_1;
3831 I915_WRITE(reg, temp);
3832
3833 reg = FDI_RX_CTL(pipe);
3834 temp = I915_READ(reg);
3835 if (HAS_PCH_CPT(dev)) {
3836 temp &= ~FDI_LINK_TRAIN_PATTERN_MASK_CPT;
3837 temp |= FDI_LINK_TRAIN_PATTERN_1_CPT;
3838 } else {
3839 temp &= ~FDI_LINK_TRAIN_NONE;
3840 temp |= FDI_LINK_TRAIN_PATTERN_1;
3841 }
3842 /* BPC in FDI rx is consistent with that in PIPECONF */
3843 temp &= ~(0x07 << 16);
Daniel Vetterdfd07d72012-12-17 11:21:38 +01003844 temp |= (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) << 11;
Jesse Barnes0fc932b2011-01-04 15:09:37 -08003845 I915_WRITE(reg, temp);
3846
3847 POSTING_READ(reg);
3848 udelay(100);
3849}
3850
Chris Wilson5dce5b932014-01-20 10:17:36 +00003851bool intel_has_pending_fb_unpin(struct drm_device *dev)
3852{
3853 struct intel_crtc *crtc;
3854
3855 /* Note that we don't need to be called with mode_config.lock here
3856 * as our list of CRTC objects is static for the lifetime of the
3857 * device and so cannot disappear as we iterate. Similarly, we can
3858 * happily treat the predicates as racy, atomic checks as userspace
3859 * cannot claim and pin a new fb without at least acquring the
3860 * struct_mutex and so serialising with us.
3861 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01003862 for_each_intel_crtc(dev, crtc) {
Chris Wilson5dce5b932014-01-20 10:17:36 +00003863 if (atomic_read(&crtc->unpin_work_count) == 0)
3864 continue;
3865
3866 if (crtc->unpin_work)
3867 intel_wait_for_vblank(dev, crtc->pipe);
3868
3869 return true;
3870 }
3871
3872 return false;
3873}
3874
Chris Wilsond6bbafa2014-09-05 07:13:24 +01003875static void page_flip_completed(struct intel_crtc *intel_crtc)
3876{
3877 struct drm_i915_private *dev_priv = to_i915(intel_crtc->base.dev);
3878 struct intel_unpin_work *work = intel_crtc->unpin_work;
3879
3880 /* ensure that the unpin work is consistent wrt ->pending. */
3881 smp_rmb();
3882 intel_crtc->unpin_work = NULL;
3883
3884 if (work->event)
3885 drm_send_vblank_event(intel_crtc->base.dev,
3886 intel_crtc->pipe,
3887 work->event);
3888
3889 drm_crtc_vblank_put(&intel_crtc->base);
3890
3891 wake_up_all(&dev_priv->pending_flip_queue);
3892 queue_work(dev_priv->wq, &work->work);
3893
3894 trace_i915_flip_complete(intel_crtc->plane,
3895 work->pending_flip_obj);
3896}
3897
Ville Syrjälä46a55d32014-05-21 14:04:46 +03003898void intel_crtc_wait_for_pending_flips(struct drm_crtc *crtc)
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003899{
Chris Wilson0f911282012-04-17 10:05:38 +01003900 struct drm_device *dev = crtc->dev;
Chris Wilson5bb61642012-09-27 21:25:58 +01003901 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003902
Daniel Vetter2c10d572012-12-20 21:24:07 +01003903 WARN_ON(waitqueue_active(&dev_priv->pending_flip_queue));
Chris Wilson9c787942014-09-05 07:13:25 +01003904 if (WARN_ON(wait_event_timeout(dev_priv->pending_flip_queue,
3905 !intel_crtc_has_pending_flip(crtc),
3906 60*HZ) == 0)) {
3907 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter2c10d572012-12-20 21:24:07 +01003908
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003909 spin_lock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003910 if (intel_crtc->unpin_work) {
3911 WARN_ONCE(1, "Removing stuck page flip\n");
3912 page_flip_completed(intel_crtc);
3913 }
Daniel Vetter5e2d7af2014-09-15 14:55:22 +02003914 spin_unlock_irq(&dev->event_lock);
Chris Wilson9c787942014-09-05 07:13:25 +01003915 }
Chris Wilson5bb61642012-09-27 21:25:58 +01003916
Chris Wilson975d5682014-08-20 13:13:34 +01003917 if (crtc->primary->fb) {
3918 mutex_lock(&dev->struct_mutex);
3919 intel_finish_fb(crtc->primary->fb);
3920 mutex_unlock(&dev->struct_mutex);
3921 }
Chris Wilsone6c3a2a2010-09-23 23:04:43 +01003922}
3923
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003924/* Program iCLKIP clock to the desired frequency */
3925static void lpt_program_iclkip(struct drm_crtc *crtc)
3926{
3927 struct drm_device *dev = crtc->dev;
3928 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02003929 int clock = to_intel_crtc(crtc)->config->base.adjusted_mode.crtc_clock;
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003930 u32 divsel, phaseinc, auxdiv, phasedir = 0;
3931 u32 temp;
3932
Ville Syrjäläa5805162015-05-26 20:42:30 +03003933 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01003934
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003935 /* It is necessary to ungate the pixclk gate prior to programming
3936 * the divisors, and gate it back when it is done.
3937 */
3938 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_GATE);
3939
3940 /* Disable SSCCTL */
3941 intel_sbi_write(dev_priv, SBI_SSCCTL6,
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003942 intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK) |
3943 SBI_SSCCTL_DISABLE,
3944 SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003945
3946 /* 20MHz is a corner case which is out of range for the 7-bit divisor */
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003947 if (clock == 20000) {
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003948 auxdiv = 1;
3949 divsel = 0x41;
3950 phaseinc = 0x20;
3951 } else {
3952 /* The iCLK virtual clock root frequency is in MHz,
Damien Lespiau241bfc32013-09-25 16:45:37 +01003953 * but the adjusted_mode->crtc_clock in in KHz. To get the
3954 * divisors, it is necessary to divide one by another, so we
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003955 * convert the virtual clock precision to KHz here for higher
3956 * precision.
3957 */
3958 u32 iclk_virtual_root_freq = 172800 * 1000;
3959 u32 iclk_pi_range = 64;
3960 u32 desired_divisor, msb_divisor_value, pi_value;
3961
Ville Syrjälä12d7cee2013-09-04 18:25:19 +03003962 desired_divisor = (iclk_virtual_root_freq / clock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003963 msb_divisor_value = desired_divisor / iclk_pi_range;
3964 pi_value = desired_divisor % iclk_pi_range;
3965
3966 auxdiv = 0;
3967 divsel = msb_divisor_value - 2;
3968 phaseinc = pi_value;
3969 }
3970
3971 /* This should not happen with any sane values */
3972 WARN_ON(SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
3973 ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
3974 WARN_ON(SBI_SSCDIVINTPHASE_DIR(phasedir) &
3975 ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
3976
3977 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 +03003978 clock,
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003979 auxdiv,
3980 divsel,
3981 phasedir,
3982 phaseinc);
3983
3984 /* Program SSCDIVINTPHASE6 */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003985 temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003986 temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
3987 temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
3988 temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
3989 temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
3990 temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
3991 temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003992 intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003993
3994 /* Program SSCAUXDIV */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003995 temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003996 temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
3997 temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02003998 intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03003999
4000 /* Enable modulator and associated divider */
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004001 temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004002 temp &= ~SBI_SSCCTL_DISABLE;
Paulo Zanoni988d6ee2012-12-01 12:04:24 -02004003 intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004004
4005 /* Wait for initialization time */
4006 udelay(24);
4007
4008 I915_WRITE(PIXCLK_GATE, PIXCLK_GATE_UNGATE);
Daniel Vetter09153002012-12-12 14:06:44 +01004009
Ville Syrjäläa5805162015-05-26 20:42:30 +03004010 mutex_unlock(&dev_priv->sb_lock);
Eugeni Dodonove615efe2012-05-09 15:37:26 -03004011}
4012
Daniel Vetter275f01b22013-05-03 11:49:47 +02004013static void ironlake_pch_transcoder_set_timings(struct intel_crtc *crtc,
4014 enum pipe pch_transcoder)
4015{
4016 struct drm_device *dev = crtc->base.dev;
4017 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004018 enum transcoder cpu_transcoder = crtc->config->cpu_transcoder;
Daniel Vetter275f01b22013-05-03 11:49:47 +02004019
4020 I915_WRITE(PCH_TRANS_HTOTAL(pch_transcoder),
4021 I915_READ(HTOTAL(cpu_transcoder)));
4022 I915_WRITE(PCH_TRANS_HBLANK(pch_transcoder),
4023 I915_READ(HBLANK(cpu_transcoder)));
4024 I915_WRITE(PCH_TRANS_HSYNC(pch_transcoder),
4025 I915_READ(HSYNC(cpu_transcoder)));
4026
4027 I915_WRITE(PCH_TRANS_VTOTAL(pch_transcoder),
4028 I915_READ(VTOTAL(cpu_transcoder)));
4029 I915_WRITE(PCH_TRANS_VBLANK(pch_transcoder),
4030 I915_READ(VBLANK(cpu_transcoder)));
4031 I915_WRITE(PCH_TRANS_VSYNC(pch_transcoder),
4032 I915_READ(VSYNC(cpu_transcoder)));
4033 I915_WRITE(PCH_TRANS_VSYNCSHIFT(pch_transcoder),
4034 I915_READ(VSYNCSHIFT(cpu_transcoder)));
4035}
4036
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004037static void cpt_set_fdi_bc_bifurcation(struct drm_device *dev, bool enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004038{
4039 struct drm_i915_private *dev_priv = dev->dev_private;
4040 uint32_t temp;
4041
4042 temp = I915_READ(SOUTH_CHICKEN1);
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004043 if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004044 return;
4045
4046 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_B)) & FDI_RX_ENABLE);
4047 WARN_ON(I915_READ(FDI_RX_CTL(PIPE_C)) & FDI_RX_ENABLE);
4048
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004049 temp &= ~FDI_BC_BIFURCATION_SELECT;
4050 if (enable)
4051 temp |= FDI_BC_BIFURCATION_SELECT;
4052
4053 DRM_DEBUG_KMS("%sabling fdi C rx\n", enable ? "en" : "dis");
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004054 I915_WRITE(SOUTH_CHICKEN1, temp);
4055 POSTING_READ(SOUTH_CHICKEN1);
4056}
4057
4058static void ivybridge_update_fdi_bc_bifurcation(struct intel_crtc *intel_crtc)
4059{
4060 struct drm_device *dev = intel_crtc->base.dev;
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004061
4062 switch (intel_crtc->pipe) {
4063 case PIPE_A:
4064 break;
4065 case PIPE_B:
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004066 if (intel_crtc->config->fdi_lanes > 2)
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004067 cpt_set_fdi_bc_bifurcation(dev, false);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004068 else
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004069 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004070
4071 break;
4072 case PIPE_C:
Ander Conselvan de Oliveira003632d2015-03-11 13:35:43 +02004073 cpt_set_fdi_bc_bifurcation(dev, true);
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004074
4075 break;
4076 default:
4077 BUG();
4078 }
4079}
4080
Jesse Barnesf67a5592011-01-05 10:31:48 -08004081/*
4082 * Enable PCH resources required for PCH ports:
4083 * - PCH PLLs
4084 * - FDI training & RX/TX
4085 * - update transcoder timings
4086 * - DP transcoding bits
4087 * - transcoder
4088 */
4089static void ironlake_pch_enable(struct drm_crtc *crtc)
Jesse Barnes79e53942008-11-07 14:24:08 -08004090{
4091 struct drm_device *dev = crtc->dev;
Zhenyu Wang2c072452009-06-05 15:38:42 +08004092 struct drm_i915_private *dev_priv = dev->dev_private;
4093 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4094 int pipe = intel_crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004095 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07004096
Daniel Vetterab9412b2013-05-03 11:49:46 +02004097 assert_pch_transcoder_disabled(dev_priv, pipe);
Chris Wilsone7e164d2012-05-11 09:21:25 +01004098
Daniel Vetter1fbc0d72013-10-29 12:04:08 +01004099 if (IS_IVYBRIDGE(dev))
4100 ivybridge_update_fdi_bc_bifurcation(intel_crtc);
4101
Daniel Vettercd986ab2012-10-26 10:58:12 +02004102 /* Write the TU size bits before fdi link training, so that error
4103 * detection works. */
4104 I915_WRITE(FDI_RX_TUSIZE1(pipe),
4105 I915_READ(PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
4106
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004107 /* For PCH output, training FDI link */
Jesse Barnes674cf962011-04-28 14:27:04 -07004108 dev_priv->display.fdi_link_train(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004109
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004110 /* We need to program the right clock selection before writing the pixel
4111 * mutliplier into the DPLL. */
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004112 if (HAS_PCH_CPT(dev)) {
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004113 u32 sel;
Jesse Barnes4b645f12011-10-12 09:51:31 -07004114
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004115 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02004116 temp |= TRANS_DPLL_ENABLE(pipe);
4117 sel = TRANS_DPLLB_SEL(pipe);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004118 if (intel_crtc->config->shared_dpll == DPLL_ID_PCH_PLL_B)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004119 temp |= sel;
4120 else
4121 temp &= ~sel;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004122 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004123 }
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004124
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004125 /* XXX: pch pll's can be enabled any time before we enable the PCH
4126 * transcoder, and we actually should do this to not upset any PCH
4127 * transcoder that already use the clock when we share it.
4128 *
4129 * Note that enable_shared_dpll tries to do the right thing, but
4130 * get_shared_dpll unconditionally resets the pll - we need that to have
4131 * the right LVDS enable sequence. */
Daniel Vetter85b38942014-04-24 23:55:14 +02004132 intel_enable_shared_dpll(intel_crtc);
Daniel Vetter3ad8a202013-06-05 13:34:32 +02004133
Jesse Barnesd9b6cb52011-01-04 15:09:35 -08004134 /* set transcoder timing, panel must allow it */
4135 assert_panel_unlocked(dev_priv, pipe);
Daniel Vetter275f01b22013-05-03 11:49:47 +02004136 ironlake_pch_transcoder_set_timings(intel_crtc, pipe);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004137
Paulo Zanoni303b81e2012-10-31 18:12:23 -02004138 intel_fdi_normal_train(crtc);
Zhenyu Wang5e84e1a2010-10-28 16:38:08 +08004139
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004140 /* For PCH DP, enable TRANS_DP_CTL */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004141 if (HAS_PCH_CPT(dev) && intel_crtc->config->has_dp_encoder) {
Daniel Vetterdfd07d72012-12-17 11:21:38 +01004142 u32 bpc = (I915_READ(PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
Chris Wilson5eddb702010-09-11 13:48:45 +01004143 reg = TRANS_DP_CTL(pipe);
4144 temp = I915_READ(reg);
4145 temp &= ~(TRANS_DP_PORT_SEL_MASK |
Eric Anholt220cad32010-11-18 09:32:58 +08004146 TRANS_DP_SYNC_MASK |
4147 TRANS_DP_BPC_MASK);
Ville Syrjäläe3ef4472015-05-05 17:17:31 +03004148 temp |= TRANS_DP_OUTPUT_ENABLE;
Jesse Barnes9325c9f2011-06-24 12:19:21 -07004149 temp |= bpc << 9; /* same format but at 11:9 */
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004150
4151 if (crtc->mode.flags & DRM_MODE_FLAG_PHSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004152 temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004153 if (crtc->mode.flags & DRM_MODE_FLAG_PVSYNC)
Chris Wilson5eddb702010-09-11 13:48:45 +01004154 temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004155
4156 switch (intel_trans_dp_port_sel(crtc)) {
4157 case PCH_DP_B:
Chris Wilson5eddb702010-09-11 13:48:45 +01004158 temp |= TRANS_DP_PORT_SEL_B;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004159 break;
4160 case PCH_DP_C:
Chris Wilson5eddb702010-09-11 13:48:45 +01004161 temp |= TRANS_DP_PORT_SEL_C;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004162 break;
4163 case PCH_DP_D:
Chris Wilson5eddb702010-09-11 13:48:45 +01004164 temp |= TRANS_DP_PORT_SEL_D;
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004165 break;
4166 default:
Daniel Vettere95d41e2012-10-26 10:58:16 +02004167 BUG();
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004168 }
4169
Chris Wilson5eddb702010-09-11 13:48:45 +01004170 I915_WRITE(reg, temp);
Jesse Barnesc98e9dc2010-09-10 10:57:18 -07004171 }
4172
Paulo Zanonib8a4f402012-10-31 18:12:42 -02004173 ironlake_enable_pch_transcoder(dev_priv, pipe);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004174}
4175
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004176static void lpt_pch_enable(struct drm_crtc *crtc)
4177{
4178 struct drm_device *dev = crtc->dev;
4179 struct drm_i915_private *dev_priv = dev->dev_private;
4180 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004181 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004182
Daniel Vetterab9412b2013-05-03 11:49:46 +02004183 assert_pch_transcoder_disabled(dev_priv, TRANSCODER_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004184
Paulo Zanoni8c52b5e2012-10-31 18:12:24 -02004185 lpt_program_iclkip(crtc);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004186
Paulo Zanoni0540e482012-10-31 18:12:40 -02004187 /* Set transcoder timing. */
Daniel Vetter275f01b22013-05-03 11:49:47 +02004188 ironlake_pch_transcoder_set_timings(intel_crtc, PIPE_A);
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02004189
Paulo Zanoni937bb612012-10-31 18:12:47 -02004190 lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004191}
4192
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004193struct intel_shared_dpll *intel_get_shared_dpll(struct intel_crtc *crtc,
4194 struct intel_crtc_state *crtc_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004195{
Daniel Vettere2b78262013-06-07 23:10:03 +02004196 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004197 struct intel_shared_dpll *pll;
Daniel Vettere2b78262013-06-07 23:10:03 +02004198 enum intel_dpll_id i;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004199
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004200 if (HAS_PCH_IBX(dev_priv->dev)) {
4201 /* Ironlake PCH has a fixed PLL->PCH pipe mapping. */
Daniel Vetterd94ab062013-07-04 12:01:16 +02004202 i = (enum intel_dpll_id) crtc->pipe;
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004203 pll = &dev_priv->shared_dplls[i];
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004204
Daniel Vetter46edb022013-06-05 13:34:12 +02004205 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4206 crtc->base.base.id, pll->name);
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004207
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004208 WARN_ON(pll->new_config->crtc_mask);
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004209
Daniel Vetter98b6bd92012-05-20 20:00:25 +02004210 goto found;
4211 }
4212
Satheeshakrishna Mbcddf6102014-08-22 09:49:10 +05304213 if (IS_BROXTON(dev_priv->dev)) {
4214 /* PLL is attached to port in bxt */
4215 struct intel_encoder *encoder;
4216 struct intel_digital_port *intel_dig_port;
4217
4218 encoder = intel_ddi_get_crtc_new_encoder(crtc_state);
4219 if (WARN_ON(!encoder))
4220 return NULL;
4221
4222 intel_dig_port = enc_to_dig_port(&encoder->base);
4223 /* 1:1 mapping between ports and PLLs */
4224 i = (enum intel_dpll_id)intel_dig_port->port;
4225 pll = &dev_priv->shared_dplls[i];
4226 DRM_DEBUG_KMS("CRTC:%d using pre-allocated %s\n",
4227 crtc->base.base.id, pll->name);
4228 WARN_ON(pll->new_config->crtc_mask);
4229
4230 goto found;
4231 }
4232
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004233 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4234 pll = &dev_priv->shared_dplls[i];
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004235
4236 /* Only want to check enabled timings first */
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004237 if (pll->new_config->crtc_mask == 0)
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004238 continue;
4239
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004240 if (memcmp(&crtc_state->dpll_hw_state,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004241 &pll->new_config->hw_state,
4242 sizeof(pll->new_config->hw_state)) == 0) {
4243 DRM_DEBUG_KMS("CRTC:%d sharing existing %s (crtc mask 0x%08x, ative %d)\n",
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +02004244 crtc->base.base.id, pll->name,
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004245 pll->new_config->crtc_mask,
4246 pll->active);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004247 goto found;
4248 }
4249 }
4250
4251 /* Ok no matching timings, maybe there's a free one? */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02004252 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4253 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004254 if (pll->new_config->crtc_mask == 0) {
Daniel Vetter46edb022013-06-05 13:34:12 +02004255 DRM_DEBUG_KMS("CRTC:%d allocated %s\n",
4256 crtc->base.base.id, pll->name);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004257 goto found;
4258 }
4259 }
4260
4261 return NULL;
4262
4263found:
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004264 if (pll->new_config->crtc_mask == 0)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004265 pll->new_config->hw_state = crtc_state->dpll_hw_state;
Daniel Vetterf2a69f42014-05-20 15:19:19 +02004266
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02004267 crtc_state->shared_dpll = i;
Daniel Vetter46edb022013-06-05 13:34:12 +02004268 DRM_DEBUG_DRIVER("using %s for pipe %c\n", pll->name,
4269 pipe_name(crtc->pipe));
Daniel Vetter66e985c2013-06-05 13:34:20 +02004270
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004271 pll->new_config->crtc_mask |= 1 << crtc->pipe;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004272
Jesse Barnesee7b9f92012-04-20 17:11:53 +01004273 return pll;
4274}
4275
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004276/**
4277 * intel_shared_dpll_start_config - start a new PLL staged config
4278 * @dev_priv: DRM device
4279 * @clear_pipes: mask of pipes that will have their PLLs freed
4280 *
4281 * Starts a new PLL staged config, copying the current config but
4282 * releasing the references of pipes specified in clear_pipes.
4283 */
4284static int intel_shared_dpll_start_config(struct drm_i915_private *dev_priv,
4285 unsigned clear_pipes)
4286{
4287 struct intel_shared_dpll *pll;
4288 enum intel_dpll_id i;
4289
4290 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4291 pll = &dev_priv->shared_dplls[i];
4292
4293 pll->new_config = kmemdup(&pll->config, sizeof pll->config,
4294 GFP_KERNEL);
4295 if (!pll->new_config)
4296 goto cleanup;
4297
4298 pll->new_config->crtc_mask &= ~clear_pipes;
4299 }
4300
4301 return 0;
4302
4303cleanup:
4304 while (--i >= 0) {
4305 pll = &dev_priv->shared_dplls[i];
Ander Conselvan de Oliveiraf354d732014-11-07 14:07:41 +02004306 kfree(pll->new_config);
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02004307 pll->new_config = NULL;
4308 }
4309
4310 return -ENOMEM;
4311}
4312
4313static void intel_shared_dpll_commit(struct drm_i915_private *dev_priv)
4314{
4315 struct intel_shared_dpll *pll;
4316 enum intel_dpll_id i;
4317
4318 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4319 pll = &dev_priv->shared_dplls[i];
4320
4321 WARN_ON(pll->new_config == &pll->config);
4322
4323 pll->config = *pll->new_config;
4324 kfree(pll->new_config);
4325 pll->new_config = NULL;
4326 }
4327}
4328
4329static void intel_shared_dpll_abort_config(struct drm_i915_private *dev_priv)
4330{
4331 struct intel_shared_dpll *pll;
4332 enum intel_dpll_id i;
4333
4334 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
4335 pll = &dev_priv->shared_dplls[i];
4336
4337 WARN_ON(pll->new_config == &pll->config);
4338
4339 kfree(pll->new_config);
4340 pll->new_config = NULL;
4341 }
4342}
4343
Daniel Vettera1520312013-05-03 11:49:50 +02004344static void cpt_verify_modeset(struct drm_device *dev, int pipe)
Jesse Barnesd4270e52011-10-11 10:43:02 -07004345{
4346 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter23670b322012-11-01 09:15:30 +01004347 int dslreg = PIPEDSL(pipe);
Jesse Barnesd4270e52011-10-11 10:43:02 -07004348 u32 temp;
4349
4350 temp = I915_READ(dslreg);
4351 udelay(500);
4352 if (wait_for(I915_READ(dslreg) != temp, 5)) {
Jesse Barnesd4270e52011-10-11 10:43:02 -07004353 if (wait_for(I915_READ(dslreg) != temp, 5))
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03004354 DRM_ERROR("mode set failed: pipe %c stuck\n", pipe_name(pipe));
Jesse Barnesd4270e52011-10-11 10:43:02 -07004355 }
4356}
4357
Chandra Kondurua1b22782015-04-07 15:28:45 -07004358/**
4359 * skl_update_scaler_users - Stages update to crtc's scaler state
4360 * @intel_crtc: crtc
4361 * @crtc_state: crtc_state
4362 * @plane: plane (NULL indicates crtc is requesting update)
4363 * @plane_state: plane's state
4364 * @force_detach: request unconditional detachment of scaler
4365 *
4366 * This function updates scaler state for requested plane or crtc.
4367 * To request scaler usage update for a plane, caller shall pass plane pointer.
4368 * To request scaler usage update for crtc, caller shall pass plane pointer
4369 * as NULL.
4370 *
4371 * Return
4372 * 0 - scaler_usage updated successfully
4373 * error - requested scaling cannot be supported or other error condition
4374 */
4375int
4376skl_update_scaler_users(
4377 struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state,
4378 struct intel_plane *intel_plane, struct intel_plane_state *plane_state,
4379 int force_detach)
4380{
4381 int need_scaling;
4382 int idx;
4383 int src_w, src_h, dst_w, dst_h;
4384 int *scaler_id;
4385 struct drm_framebuffer *fb;
4386 struct intel_crtc_scaler_state *scaler_state;
Chandra Konduru6156a452015-04-27 13:48:39 -07004387 unsigned int rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004388
4389 if (!intel_crtc || !crtc_state)
4390 return 0;
4391
4392 scaler_state = &crtc_state->scaler_state;
4393
4394 idx = intel_plane ? drm_plane_index(&intel_plane->base) : SKL_CRTC_INDEX;
4395 fb = intel_plane ? plane_state->base.fb : NULL;
4396
4397 if (intel_plane) {
4398 src_w = drm_rect_width(&plane_state->src) >> 16;
4399 src_h = drm_rect_height(&plane_state->src) >> 16;
4400 dst_w = drm_rect_width(&plane_state->dst);
4401 dst_h = drm_rect_height(&plane_state->dst);
4402 scaler_id = &plane_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004403 rotation = plane_state->base.rotation;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004404 } else {
4405 struct drm_display_mode *adjusted_mode =
4406 &crtc_state->base.adjusted_mode;
4407 src_w = crtc_state->pipe_src_w;
4408 src_h = crtc_state->pipe_src_h;
4409 dst_w = adjusted_mode->hdisplay;
4410 dst_h = adjusted_mode->vdisplay;
4411 scaler_id = &scaler_state->scaler_id;
Chandra Konduru6156a452015-04-27 13:48:39 -07004412 rotation = DRM_ROTATE_0;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004413 }
Chandra Konduru6156a452015-04-27 13:48:39 -07004414
4415 need_scaling = intel_rotation_90_or_270(rotation) ?
4416 (src_h != dst_w || src_w != dst_h):
4417 (src_w != dst_w || src_h != dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004418
4419 /*
4420 * if plane is being disabled or scaler is no more required or force detach
4421 * - free scaler binded to this plane/crtc
4422 * - in order to do this, update crtc->scaler_usage
4423 *
4424 * Here scaler state in crtc_state is set free so that
4425 * scaler can be assigned to other user. Actual register
4426 * update to free the scaler is done in plane/panel-fit programming.
4427 * For this purpose crtc/plane_state->scaler_id isn't reset here.
4428 */
4429 if (force_detach || !need_scaling || (intel_plane &&
4430 (!fb || !plane_state->visible))) {
4431 if (*scaler_id >= 0) {
4432 scaler_state->scaler_users &= ~(1 << idx);
4433 scaler_state->scalers[*scaler_id].in_use = 0;
4434
4435 DRM_DEBUG_KMS("Staged freeing scaler id %d.%d from %s:%d "
4436 "crtc_state = %p scaler_users = 0x%x\n",
4437 intel_crtc->pipe, *scaler_id, intel_plane ? "PLANE" : "CRTC",
4438 intel_plane ? intel_plane->base.base.id :
4439 intel_crtc->base.base.id, crtc_state,
4440 scaler_state->scaler_users);
4441 *scaler_id = -1;
4442 }
4443 return 0;
4444 }
4445
4446 /* range checks */
4447 if (src_w < SKL_MIN_SRC_W || src_h < SKL_MIN_SRC_H ||
4448 dst_w < SKL_MIN_DST_W || dst_h < SKL_MIN_DST_H ||
4449
4450 src_w > SKL_MAX_SRC_W || src_h > SKL_MAX_SRC_H ||
4451 dst_w > SKL_MAX_DST_W || dst_h > SKL_MAX_DST_H) {
4452 DRM_DEBUG_KMS("%s:%d scaler_user index %u.%u: src %ux%u dst %ux%u "
4453 "size is out of scaler range\n",
4454 intel_plane ? "PLANE" : "CRTC",
4455 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4456 intel_crtc->pipe, idx, src_w, src_h, dst_w, dst_h);
4457 return -EINVAL;
4458 }
4459
4460 /* check colorkey */
Chandra Konduru225c2282015-05-18 16:18:44 -07004461 if (WARN_ON(intel_plane &&
4462 intel_plane->ckey.flags != I915_SET_COLORKEY_NONE)) {
4463 DRM_DEBUG_KMS("PLANE:%d scaling %ux%u->%ux%u not allowed with colorkey",
4464 intel_plane->base.base.id, src_w, src_h, dst_w, dst_h);
Chandra Kondurua1b22782015-04-07 15:28:45 -07004465 return -EINVAL;
4466 }
4467
4468 /* Check src format */
4469 if (intel_plane) {
4470 switch (fb->pixel_format) {
4471 case DRM_FORMAT_RGB565:
4472 case DRM_FORMAT_XBGR8888:
4473 case DRM_FORMAT_XRGB8888:
4474 case DRM_FORMAT_ABGR8888:
4475 case DRM_FORMAT_ARGB8888:
4476 case DRM_FORMAT_XRGB2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004477 case DRM_FORMAT_XBGR2101010:
Chandra Kondurua1b22782015-04-07 15:28:45 -07004478 case DRM_FORMAT_YUYV:
4479 case DRM_FORMAT_YVYU:
4480 case DRM_FORMAT_UYVY:
4481 case DRM_FORMAT_VYUY:
4482 break;
4483 default:
4484 DRM_DEBUG_KMS("PLANE:%d FB:%d unsupported scaling format 0x%x\n",
4485 intel_plane->base.base.id, fb->base.id, fb->pixel_format);
4486 return -EINVAL;
4487 }
4488 }
4489
4490 /* mark this plane as a scaler user in crtc_state */
4491 scaler_state->scaler_users |= (1 << idx);
4492 DRM_DEBUG_KMS("%s:%d staged scaling request for %ux%u->%ux%u "
4493 "crtc_state = %p scaler_users = 0x%x\n",
4494 intel_plane ? "PLANE" : "CRTC",
4495 intel_plane ? intel_plane->base.base.id : intel_crtc->base.base.id,
4496 src_w, src_h, dst_w, dst_h, crtc_state, scaler_state->scaler_users);
4497 return 0;
4498}
4499
4500static void skylake_pfit_update(struct intel_crtc *crtc, int enable)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004501{
4502 struct drm_device *dev = crtc->base.dev;
4503 struct drm_i915_private *dev_priv = dev->dev_private;
4504 int pipe = crtc->pipe;
Chandra Kondurua1b22782015-04-07 15:28:45 -07004505 struct intel_crtc_scaler_state *scaler_state =
4506 &crtc->config->scaler_state;
4507
4508 DRM_DEBUG_KMS("for crtc_state = %p\n", crtc->config);
4509
4510 /* To update pfit, first update scaler state */
4511 skl_update_scaler_users(crtc, crtc->config, NULL, NULL, !enable);
4512 intel_atomic_setup_scalers(crtc->base.dev, crtc, crtc->config);
4513 skl_detach_scalers(crtc);
4514 if (!enable)
4515 return;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004516
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004517 if (crtc->config->pch_pfit.enabled) {
Chandra Kondurua1b22782015-04-07 15:28:45 -07004518 int id;
4519
4520 if (WARN_ON(crtc->config->scaler_state.scaler_id < 0)) {
4521 DRM_ERROR("Requesting pfit without getting a scaler first\n");
4522 return;
4523 }
4524
4525 id = scaler_state->scaler_id;
4526 I915_WRITE(SKL_PS_CTRL(pipe, id), PS_SCALER_EN |
4527 PS_FILTER_MEDIUM | scaler_state->scalers[id].mode);
4528 I915_WRITE(SKL_PS_WIN_POS(pipe, id), crtc->config->pch_pfit.pos);
4529 I915_WRITE(SKL_PS_WIN_SZ(pipe, id), crtc->config->pch_pfit.size);
4530
4531 DRM_DEBUG_KMS("for crtc_state = %p scaler_id = %d\n", crtc->config, id);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00004532 }
4533}
4534
Jesse Barnesb074cec2013-04-25 12:55:02 -07004535static void ironlake_pfit_enable(struct intel_crtc *crtc)
4536{
4537 struct drm_device *dev = crtc->base.dev;
4538 struct drm_i915_private *dev_priv = dev->dev_private;
4539 int pipe = crtc->pipe;
4540
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004541 if (crtc->config->pch_pfit.enabled) {
Jesse Barnesb074cec2013-04-25 12:55:02 -07004542 /* Force use of hard-coded filter coefficients
4543 * as some pre-programmed values are broken,
4544 * e.g. x201.
4545 */
4546 if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev))
4547 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3 |
4548 PF_PIPE_SEL_IVB(pipe));
4549 else
4550 I915_WRITE(PF_CTL(pipe), PF_ENABLE | PF_FILTER_MED_3x3);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004551 I915_WRITE(PF_WIN_POS(pipe), crtc->config->pch_pfit.pos);
4552 I915_WRITE(PF_WIN_SZ(pipe), crtc->config->pch_pfit.size);
Jesse Barnes040484a2011-01-03 12:14:26 -08004553 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004554}
4555
Matt Roper4a3b8762014-12-23 10:41:51 -08004556static void intel_enable_sprite_planes(struct drm_crtc *crtc)
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004557{
4558 struct drm_device *dev = crtc->dev;
4559 enum pipe pipe = to_intel_crtc(crtc)->pipe;
Matt Roperaf2b6532014-04-01 15:22:32 -07004560 struct drm_plane *plane;
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004561 struct intel_plane *intel_plane;
4562
Matt Roperaf2b6532014-04-01 15:22:32 -07004563 drm_for_each_legacy_plane(plane, &dev->mode_config.plane_list) {
4564 intel_plane = to_intel_plane(plane);
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004565 if (intel_plane->pipe == pipe)
4566 intel_plane_restore(&intel_plane->base);
Matt Roperaf2b6532014-04-01 15:22:32 -07004567 }
Ville Syrjäläbb53d4a2013-06-04 13:49:04 +03004568}
4569
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004570void hsw_enable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004571{
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004572 struct drm_device *dev = crtc->base.dev;
4573 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid77e4532013-09-24 13:52:55 -03004574
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004575 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004576 return;
4577
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004578 /* We can only enable IPS after we enable a plane and wait for a vblank */
4579 intel_wait_for_vblank(dev, crtc->pipe);
4580
Paulo Zanonid77e4532013-09-24 13:52:55 -03004581 assert_plane_enabled(dev_priv, crtc->plane);
Ville Syrjäläcea165c2014-04-15 21:41:35 +03004582 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004583 mutex_lock(&dev_priv->rps.hw_lock);
4584 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0xc0000000));
4585 mutex_unlock(&dev_priv->rps.hw_lock);
4586 /* Quoting Art Runyan: "its not safe to expect any particular
4587 * value in IPS_CTL bit 31 after enabling IPS through the
Jesse Barnese59150d2014-01-07 13:30:45 -08004588 * mailbox." Moreover, the mailbox may return a bogus state,
4589 * so we need to just enable it and continue on.
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004590 */
4591 } else {
4592 I915_WRITE(IPS_CTL, IPS_ENABLE);
4593 /* The bit only becomes 1 in the next vblank, so this wait here
4594 * is essentially intel_wait_for_vblank. If we don't have this
4595 * and don't wait for vblanks until the end of crtc_enable, then
4596 * the HW state readout code will complain that the expected
4597 * IPS_CTL value is not the one we read. */
4598 if (wait_for(I915_READ_NOTRACE(IPS_CTL) & IPS_ENABLE, 50))
4599 DRM_ERROR("Timed out waiting for IPS enable\n");
4600 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004601}
4602
Ville Syrjälä20bc86732013-10-01 18:02:17 +03004603void hsw_disable_ips(struct intel_crtc *crtc)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004604{
4605 struct drm_device *dev = crtc->base.dev;
4606 struct drm_i915_private *dev_priv = dev->dev_private;
4607
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004608 if (!crtc->config->ips_enabled)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004609 return;
4610
4611 assert_plane_enabled(dev_priv, crtc->plane);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004612 if (IS_BROADWELL(dev)) {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004613 mutex_lock(&dev_priv->rps.hw_lock);
4614 WARN_ON(sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
4615 mutex_unlock(&dev_priv->rps.hw_lock);
Ben Widawsky23d0b132014-04-10 14:32:41 -07004616 /* wait for pcode to finish disabling IPS, which may take up to 42ms */
4617 if (wait_for((I915_READ(IPS_CTL) & IPS_ENABLE) == 0, 42))
4618 DRM_ERROR("Timed out waiting for IPS disable\n");
Jesse Barnese59150d2014-01-07 13:30:45 -08004619 } else {
Ben Widawsky2a114cc2013-11-02 21:07:47 -07004620 I915_WRITE(IPS_CTL, 0);
Jesse Barnese59150d2014-01-07 13:30:45 -08004621 POSTING_READ(IPS_CTL);
4622 }
Paulo Zanonid77e4532013-09-24 13:52:55 -03004623
4624 /* We need to wait for a vblank before we can disable the plane. */
4625 intel_wait_for_vblank(dev, crtc->pipe);
4626}
4627
4628/** Loads the palette/gamma unit for the CRTC with the prepared values */
4629static void intel_crtc_load_lut(struct drm_crtc *crtc)
4630{
4631 struct drm_device *dev = crtc->dev;
4632 struct drm_i915_private *dev_priv = dev->dev_private;
4633 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4634 enum pipe pipe = intel_crtc->pipe;
4635 int palreg = PALETTE(pipe);
4636 int i;
4637 bool reenable_ips = false;
4638
4639 /* The clocks have to be on to load the palette. */
Matt Roper83d65732015-02-25 13:12:16 -08004640 if (!crtc->state->enable || !intel_crtc->active)
Paulo Zanonid77e4532013-09-24 13:52:55 -03004641 return;
4642
Imre Deak50360402015-01-16 00:55:16 -08004643 if (HAS_GMCH_DISPLAY(dev_priv->dev)) {
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03004644 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004645 assert_dsi_pll_enabled(dev_priv);
4646 else
4647 assert_pll_enabled(dev_priv, pipe);
4648 }
4649
4650 /* use legacy palette for Ironlake */
Sonika Jindal7a1db492014-07-22 11:18:27 +05304651 if (!HAS_GMCH_DISPLAY(dev))
Paulo Zanonid77e4532013-09-24 13:52:55 -03004652 palreg = LGC_PALETTE(pipe);
4653
4654 /* Workaround : Do not read or write the pipe palette/gamma data while
4655 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
4656 */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004657 if (IS_HASWELL(dev) && intel_crtc->config->ips_enabled &&
Paulo Zanonid77e4532013-09-24 13:52:55 -03004658 ((I915_READ(GAMMA_MODE(pipe)) & GAMMA_MODE_MODE_MASK) ==
4659 GAMMA_MODE_MODE_SPLIT)) {
4660 hsw_disable_ips(intel_crtc);
4661 reenable_ips = true;
4662 }
4663
4664 for (i = 0; i < 256; i++) {
4665 I915_WRITE(palreg + 4 * i,
4666 (intel_crtc->lut_r[i] << 16) |
4667 (intel_crtc->lut_g[i] << 8) |
4668 intel_crtc->lut_b[i]);
4669 }
4670
4671 if (reenable_ips)
4672 hsw_enable_ips(intel_crtc);
4673}
4674
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004675static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004676{
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004677 if (intel_crtc->overlay) {
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004678 struct drm_device *dev = intel_crtc->base.dev;
4679 struct drm_i915_private *dev_priv = dev->dev_private;
4680
4681 mutex_lock(&dev->struct_mutex);
4682 dev_priv->mm.interruptible = false;
4683 (void) intel_overlay_switch_off(intel_crtc->overlay);
4684 dev_priv->mm.interruptible = true;
4685 mutex_unlock(&dev->struct_mutex);
4686 }
4687
4688 /* Let userspace switch the overlay on again. In most cases userspace
4689 * has to recompute where to put it anyway.
4690 */
4691}
4692
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004693/**
4694 * intel_post_enable_primary - Perform operations after enabling primary plane
4695 * @crtc: the CRTC whose primary plane was just enabled
4696 *
4697 * Performs potentially sleeping operations that must be done after the primary
4698 * plane is enabled, such as updating FBC and IPS. Note that this may be
4699 * called due to an explicit primary plane update, or due to an implicit
4700 * re-enable that is caused when a sprite plane is updated to no longer
4701 * completely hide the primary plane.
4702 */
4703static void
4704intel_post_enable_primary(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004705{
4706 struct drm_device *dev = crtc->dev;
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004707 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004708 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4709 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004710
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004711 /*
4712 * BDW signals flip done immediately if the plane
4713 * is disabled, even if the plane enable is already
4714 * armed to occur at the next vblank :(
4715 */
4716 if (IS_BROADWELL(dev))
4717 intel_wait_for_vblank(dev, pipe);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004718
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004719 /*
4720 * FIXME IPS should be fine as long as one plane is
4721 * enabled, but in practice it seems to have problems
4722 * when going from primary only to sprite only and vice
4723 * versa.
4724 */
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004725 hsw_enable_ips(intel_crtc);
4726
4727 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02004728 intel_fbc_update(dev);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004729 mutex_unlock(&dev->struct_mutex);
Daniel Vetterf99d7062014-06-19 16:01:59 +02004730
4731 /*
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004732 * Gen2 reports pipe underruns whenever all planes are disabled.
4733 * So don't enable underrun reporting before at least some planes
4734 * are enabled.
4735 * FIXME: Need to fix the logic to work when we turn off all planes
4736 * but leave the pipe running.
Daniel Vetterf99d7062014-06-19 16:01:59 +02004737 */
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004738 if (IS_GEN2(dev))
4739 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4740
4741 /* Underruns don't raise interrupts, so check manually. */
4742 if (HAS_GMCH_DISPLAY(dev))
4743 i9xx_check_fifo_underruns(dev_priv);
4744}
4745
4746/**
4747 * intel_pre_disable_primary - Perform operations before disabling primary plane
4748 * @crtc: the CRTC whose primary plane is to be disabled
4749 *
4750 * Performs potentially sleeping operations that must be done before the
4751 * primary plane is disabled, such as updating FBC and IPS. Note that this may
4752 * be called due to an explicit primary plane update, or due to an implicit
4753 * disable that is caused when a sprite plane completely hides the primary
4754 * plane.
4755 */
4756static void
4757intel_pre_disable_primary(struct drm_crtc *crtc)
4758{
4759 struct drm_device *dev = crtc->dev;
4760 struct drm_i915_private *dev_priv = dev->dev_private;
4761 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4762 int pipe = intel_crtc->pipe;
4763
4764 /*
4765 * Gen2 reports pipe underruns whenever all planes are disabled.
4766 * So diasble underrun reporting before all the planes get disabled.
4767 * FIXME: Need to fix the logic to work when we turn off all planes
4768 * but leave the pipe running.
4769 */
4770 if (IS_GEN2(dev))
4771 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4772
4773 /*
4774 * Vblank time updates from the shadow to live plane control register
4775 * are blocked if the memory self-refresh mode is active at that
4776 * moment. So to make sure the plane gets truly disabled, disable
4777 * first the self-refresh mode. The self-refresh enable bit in turn
4778 * will be checked/applied by the HW only at the next frame start
4779 * event which is after the vblank start event, so we need to have a
4780 * wait-for-vblank between disabling the plane and the pipe.
4781 */
4782 if (HAS_GMCH_DISPLAY(dev))
4783 intel_set_memory_cxsr(dev_priv, false);
4784
4785 mutex_lock(&dev->struct_mutex);
4786 if (dev_priv->fbc.crtc == intel_crtc)
4787 intel_fbc_disable(dev);
4788 mutex_unlock(&dev->struct_mutex);
4789
4790 /*
4791 * FIXME IPS should be fine as long as one plane is
4792 * enabled, but in practice it seems to have problems
4793 * when going from primary only to sprite only and vice
4794 * versa.
4795 */
4796 hsw_disable_ips(intel_crtc);
4797}
4798
4799static void intel_crtc_enable_planes(struct drm_crtc *crtc)
4800{
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004801 struct drm_device *dev = crtc->dev;
4802 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4803 int pipe = intel_crtc->pipe;
4804
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004805 intel_enable_primary_hw_plane(crtc->primary, crtc);
4806 intel_enable_sprite_planes(crtc);
4807 intel_crtc_update_cursor(crtc, true);
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004808
4809 intel_post_enable_primary(crtc);
Rodrigo Vivi2d847d42015-05-28 10:21:16 -07004810
4811 /*
4812 * FIXME: Once we grow proper nuclear flip support out of this we need
4813 * to compute the mask of flip planes precisely. For the time being
4814 * consider this a flip to a NULL plane.
4815 */
4816 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004817}
4818
Ville Syrjäläd3eedb12014-05-08 19:23:13 +03004819static void intel_crtc_disable_planes(struct drm_crtc *crtc)
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004820{
4821 struct drm_device *dev = crtc->dev;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004822 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004823 struct intel_plane *intel_plane;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004824 int pipe = intel_crtc->pipe;
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004825
4826 intel_crtc_wait_for_pending_flips(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004827
Maarten Lankhorst87d43002015-04-21 17:12:54 +03004828 intel_pre_disable_primary(crtc);
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004829
Maarten Lankhorst7cac9452015-04-21 17:12:55 +03004830 intel_crtc_dpms_overlay_disable(intel_crtc);
Maarten Lankhorst27321ae2015-04-21 17:12:52 +03004831 for_each_intel_plane(dev, intel_plane) {
4832 if (intel_plane->pipe == pipe) {
4833 struct drm_crtc *from = intel_plane->base.crtc;
4834
4835 intel_plane->disable_plane(&intel_plane->base,
4836 from ?: crtc, true);
4837 }
4838 }
Ville Syrjäläf98551a2014-05-22 17:48:06 +03004839
Daniel Vetterf99d7062014-06-19 16:01:59 +02004840 /*
4841 * FIXME: Once we grow proper nuclear flip support out of this we need
4842 * to compute the mask of flip planes precisely. For the time being
4843 * consider this a flip to a NULL plane.
4844 */
4845 intel_frontbuffer_flip(dev, INTEL_FRONTBUFFER_ALL_MASK(pipe));
Ville Syrjäläa5c4d7b2014-03-07 18:32:13 +02004846}
4847
Jesse Barnesf67a5592011-01-05 10:31:48 -08004848static void ironlake_crtc_enable(struct drm_crtc *crtc)
4849{
4850 struct drm_device *dev = crtc->dev;
4851 struct drm_i915_private *dev_priv = dev->dev_private;
4852 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02004853 struct intel_encoder *encoder;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004854 int pipe = intel_crtc->pipe;
Jesse Barnesf67a5592011-01-05 10:31:48 -08004855
Matt Roper83d65732015-02-25 13:12:16 -08004856 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02004857
Jesse Barnesf67a5592011-01-05 10:31:48 -08004858 if (intel_crtc->active)
4859 return;
4860
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004861 if (intel_crtc->config->has_pch_encoder)
Daniel Vetterb14b1052014-04-24 23:55:13 +02004862 intel_prepare_shared_dpll(intel_crtc);
4863
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004864 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304865 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004866
4867 intel_set_pipe_timings(intel_crtc);
4868
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004869 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter29407aa2014-04-24 23:55:08 +02004870 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004871 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter29407aa2014-04-24 23:55:08 +02004872 }
4873
4874 ironlake_set_pipeconf(crtc);
4875
Jesse Barnesf67a5592011-01-05 10:31:48 -08004876 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004877
Daniel Vettera72e4c92014-09-30 10:56:47 +02004878 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4879 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni86642812013-04-12 17:57:57 -03004880
Daniel Vetterf6736a12013-06-05 13:34:30 +02004881 for_each_encoder_on_crtc(dev, crtc, encoder)
Daniel Vetter952735e2013-06-05 13:34:27 +02004882 if (encoder->pre_enable)
4883 encoder->pre_enable(encoder);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004884
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004885 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterfff367c2012-10-27 15:50:28 +02004886 /* Note: FDI PLL enabling _must_ be done before we enable the
4887 * cpu pipes, hence this is separate from all the other fdi/pch
4888 * enabling. */
Daniel Vetter88cefb62012-08-12 19:27:14 +02004889 ironlake_fdi_pll_enable(intel_crtc);
Daniel Vetter46b6f812012-09-06 22:08:33 +02004890 } else {
4891 assert_fdi_tx_disabled(dev_priv, pipe);
4892 assert_fdi_rx_disabled(dev_priv, pipe);
4893 }
Jesse Barnesf67a5592011-01-05 10:31:48 -08004894
Jesse Barnesb074cec2013-04-25 12:55:02 -07004895 ironlake_pfit_enable(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004896
Jesse Barnes9c54c0d2011-06-15 23:32:33 +02004897 /*
4898 * On ILK+ LUT must be loaded before the pipe is running but with
4899 * clocks enabled
4900 */
4901 intel_crtc_load_lut(crtc);
4902
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03004903 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02004904 intel_enable_pipe(intel_crtc);
Jesse Barnesf67a5592011-01-05 10:31:48 -08004905
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004906 if (intel_crtc->config->has_pch_encoder)
Jesse Barnesf67a5592011-01-05 10:31:48 -08004907 ironlake_pch_enable(crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004908
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01004909 assert_vblank_disabled(crtc);
4910 drm_crtc_vblank_on(crtc);
4911
Daniel Vetterfa5c73b2012-07-01 23:24:36 +02004912 for_each_encoder_on_crtc(dev, crtc, encoder)
4913 encoder->enable(encoder);
Daniel Vetter61b77dd2012-07-02 00:16:19 +02004914
4915 if (HAS_PCH_CPT(dev))
Daniel Vettera1520312013-05-03 11:49:50 +02004916 cpt_verify_modeset(dev, intel_crtc->pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07004917}
4918
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004919/* IPS only exists on ULT machines and is tied to pipe A. */
4920static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
4921{
Damien Lespiauf5adf942013-06-24 18:29:34 +01004922 return HAS_IPS(crtc->base.dev) && crtc->pipe == PIPE_A;
Paulo Zanoni42db64e2013-05-31 16:33:22 -03004923}
4924
Paulo Zanonie4916942013-09-20 16:21:19 -03004925/*
4926 * This implements the workaround described in the "notes" section of the mode
4927 * set sequence documentation. When going from no pipes or single pipe to
4928 * multiple pipes, and planes are enabled after the pipe, we need to wait at
4929 * least 2 vblanks on the first pipe before enabling planes on the second pipe.
4930 */
4931static void haswell_mode_set_planes_workaround(struct intel_crtc *crtc)
4932{
4933 struct drm_device *dev = crtc->base.dev;
4934 struct intel_crtc *crtc_it, *other_active_crtc = NULL;
4935
4936 /* We want to get the other_active_crtc only if there's only 1 other
4937 * active crtc. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01004938 for_each_intel_crtc(dev, crtc_it) {
Paulo Zanonie4916942013-09-20 16:21:19 -03004939 if (!crtc_it->active || crtc_it == crtc)
4940 continue;
4941
4942 if (other_active_crtc)
4943 return;
4944
4945 other_active_crtc = crtc_it;
4946 }
4947 if (!other_active_crtc)
4948 return;
4949
4950 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4951 intel_wait_for_vblank(dev, other_active_crtc->pipe);
4952}
4953
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004954static void haswell_crtc_enable(struct drm_crtc *crtc)
4955{
4956 struct drm_device *dev = crtc->dev;
4957 struct drm_i915_private *dev_priv = dev->dev_private;
4958 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
4959 struct intel_encoder *encoder;
4960 int pipe = intel_crtc->pipe;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004961
Matt Roper83d65732015-02-25 13:12:16 -08004962 WARN_ON(!crtc->state->enable);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004963
4964 if (intel_crtc->active)
4965 return;
4966
Daniel Vetterdf8ad702014-06-25 22:02:03 +03004967 if (intel_crtc_to_shared_dpll(intel_crtc))
4968 intel_enable_shared_dpll(intel_crtc);
4969
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004970 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05304971 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter229fca92014-04-24 23:55:09 +02004972
4973 intel_set_pipe_timings(intel_crtc);
4974
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004975 if (intel_crtc->config->cpu_transcoder != TRANSCODER_EDP) {
4976 I915_WRITE(PIPE_MULT(intel_crtc->config->cpu_transcoder),
4977 intel_crtc->config->pixel_multiplier - 1);
Clint Taylorebb69c92014-09-30 10:30:22 -07004978 }
4979
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004980 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetter229fca92014-04-24 23:55:09 +02004981 intel_cpu_transcoder_set_m_n(intel_crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004982 &intel_crtc->config->fdi_m_n, NULL);
Daniel Vetter229fca92014-04-24 23:55:09 +02004983 }
4984
4985 haswell_set_pipeconf(crtc);
4986
4987 intel_set_pipe_csc(crtc);
4988
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004989 intel_crtc->active = true;
Paulo Zanoni86642812013-04-12 17:57:57 -03004990
Daniel Vettera72e4c92014-09-30 10:56:47 +02004991 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02004992 for_each_encoder_on_crtc(dev, crtc, encoder)
4993 if (encoder->pre_enable)
4994 encoder->pre_enable(encoder);
4995
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02004996 if (intel_crtc->config->has_pch_encoder) {
Daniel Vettera72e4c92014-09-30 10:56:47 +02004997 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
4998 true);
Imre Deak4fe94672014-06-25 22:01:49 +03004999 dev_priv->display.fdi_link_train(crtc);
5000 }
5001
Paulo Zanoni1f544382012-10-24 11:32:00 -02005002 intel_ddi_enable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005003
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005004 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005005 skylake_pfit_update(intel_crtc, 1);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005006 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005007 ironlake_pfit_enable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005008 else
5009 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005010
5011 /*
5012 * On ILK+ LUT must be loaded before the pipe is running but with
5013 * clocks enabled
5014 */
5015 intel_crtc_load_lut(crtc);
5016
Paulo Zanoni1f544382012-10-24 11:32:00 -02005017 intel_ddi_set_pipe_settings(crtc);
Damien Lespiau8228c252013-03-07 15:30:27 +00005018 intel_ddi_enable_transcoder_func(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005019
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03005020 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02005021 intel_enable_pipe(intel_crtc);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03005022
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005023 if (intel_crtc->config->has_pch_encoder)
Paulo Zanoni1507e5b2012-10-31 18:12:22 -02005024 lpt_pch_enable(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005025
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005026 if (intel_crtc->config->dp_encoder_is_mst)
Dave Airlie0e32b392014-05-02 14:02:48 +10005027 intel_ddi_set_vc_payload_alloc(crtc, true);
5028
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005029 assert_vblank_disabled(crtc);
5030 drm_crtc_vblank_on(crtc);
5031
Jani Nikula8807e552013-08-30 19:40:32 +03005032 for_each_encoder_on_crtc(dev, crtc, encoder) {
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005033 encoder->enable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005034 intel_opregion_notify_encoder(encoder, true);
5035 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005036
Paulo Zanonie4916942013-09-20 16:21:19 -03005037 /* If we change the relative order between pipe/planes enabling, we need
5038 * to change the workaround. */
5039 haswell_mode_set_planes_workaround(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005040}
5041
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005042static void ironlake_pfit_disable(struct intel_crtc *crtc)
5043{
5044 struct drm_device *dev = crtc->base.dev;
5045 struct drm_i915_private *dev_priv = dev->dev_private;
5046 int pipe = crtc->pipe;
5047
5048 /* To avoid upsetting the power well on haswell only disable the pfit if
5049 * it's in use. The hw state code will make sure we get this right. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005050 if (crtc->config->pch_pfit.enabled) {
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005051 I915_WRITE(PF_CTL(pipe), 0);
5052 I915_WRITE(PF_WIN_POS(pipe), 0);
5053 I915_WRITE(PF_WIN_SZ(pipe), 0);
5054 }
5055}
5056
Jesse Barnes6be4a602010-09-10 10:26:01 -07005057static void ironlake_crtc_disable(struct drm_crtc *crtc)
5058{
5059 struct drm_device *dev = crtc->dev;
5060 struct drm_i915_private *dev_priv = dev->dev_private;
5061 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02005062 struct intel_encoder *encoder;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005063 int pipe = intel_crtc->pipe;
Chris Wilson5eddb702010-09-11 13:48:45 +01005064 u32 reg, temp;
Jesse Barnes6be4a602010-09-10 10:26:01 -07005065
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005066 if (!intel_crtc->active)
5067 return;
5068
Daniel Vetterea9d7582012-07-10 10:42:52 +02005069 for_each_encoder_on_crtc(dev, crtc, encoder)
5070 encoder->disable(encoder);
5071
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005072 drm_crtc_vblank_off(crtc);
5073 assert_vblank_disabled(crtc);
5074
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005075 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005076 intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
Daniel Vetterd925c592013-06-05 13:34:04 +02005077
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005078 intel_disable_pipe(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005079
Daniel Vetter3f8dce32013-05-08 10:36:30 +02005080 ironlake_pfit_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005081
Ville Syrjälä5a74f702015-05-05 17:17:38 +03005082 if (intel_crtc->config->has_pch_encoder)
5083 ironlake_fdi_disable(crtc);
5084
Daniel Vetterbf49ec82012-09-06 22:15:40 +02005085 for_each_encoder_on_crtc(dev, crtc, encoder)
5086 if (encoder->post_disable)
5087 encoder->post_disable(encoder);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005088
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005089 if (intel_crtc->config->has_pch_encoder) {
Daniel Vetterd925c592013-06-05 13:34:04 +02005090 ironlake_disable_pch_transcoder(dev_priv, pipe);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005091
Daniel Vetterd925c592013-06-05 13:34:04 +02005092 if (HAS_PCH_CPT(dev)) {
5093 /* disable TRANS_DP_CTL */
5094 reg = TRANS_DP_CTL(pipe);
5095 temp = I915_READ(reg);
5096 temp &= ~(TRANS_DP_OUTPUT_ENABLE |
5097 TRANS_DP_PORT_SEL_MASK);
5098 temp |= TRANS_DP_PORT_SEL_NONE;
5099 I915_WRITE(reg, temp);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005100
Daniel Vetterd925c592013-06-05 13:34:04 +02005101 /* disable DPLL_SEL */
5102 temp = I915_READ(PCH_DPLL_SEL);
Daniel Vetter11887392013-06-05 13:34:09 +02005103 temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
Daniel Vetterd925c592013-06-05 13:34:04 +02005104 I915_WRITE(PCH_DPLL_SEL, temp);
Jesse Barnes9db4a9c2011-02-07 12:26:52 -08005105 }
Daniel Vetterd925c592013-06-05 13:34:04 +02005106
5107 /* disable PCH DPLL */
Daniel Vettere72f9fb2013-06-05 13:34:06 +02005108 intel_disable_shared_dpll(intel_crtc);
Daniel Vetterd925c592013-06-05 13:34:04 +02005109
5110 ironlake_fdi_pll_disable(intel_crtc);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005111 }
5112
Chris Wilsonf7abfe82010-09-13 14:19:16 +01005113 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005114 intel_update_watermarks(crtc);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005115
5116 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005117 intel_fbc_update(dev);
Ben Widawskyd1ebd8162011-04-25 20:11:50 +01005118 mutex_unlock(&dev->struct_mutex);
Jesse Barnes6be4a602010-09-10 10:26:01 -07005119}
5120
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005121static void haswell_crtc_disable(struct drm_crtc *crtc)
5122{
5123 struct drm_device *dev = crtc->dev;
5124 struct drm_i915_private *dev_priv = dev->dev_private;
5125 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5126 struct intel_encoder *encoder;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005127 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005128
5129 if (!intel_crtc->active)
5130 return;
5131
Jani Nikula8807e552013-08-30 19:40:32 +03005132 for_each_encoder_on_crtc(dev, crtc, encoder) {
5133 intel_opregion_notify_encoder(encoder, false);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005134 encoder->disable(encoder);
Jani Nikula8807e552013-08-30 19:40:32 +03005135 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005136
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01005137 drm_crtc_vblank_off(crtc);
5138 assert_vblank_disabled(crtc);
5139
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005140 if (intel_crtc->config->has_pch_encoder)
Daniel Vettera72e4c92014-09-30 10:56:47 +02005141 intel_set_pch_fifo_underrun_reporting(dev_priv, TRANSCODER_A,
5142 false);
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03005143 intel_disable_pipe(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005144
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005145 if (intel_crtc->config->dp_encoder_is_mst)
Ville Syrjäläa4bf2142014-08-18 21:27:34 +03005146 intel_ddi_set_vc_payload_alloc(crtc, false);
5147
Paulo Zanoniad80a812012-10-24 16:06:19 -02005148 intel_ddi_disable_transcoder_func(dev_priv, cpu_transcoder);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005149
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005150 if (INTEL_INFO(dev)->gen == 9)
Chandra Kondurua1b22782015-04-07 15:28:45 -07005151 skylake_pfit_update(intel_crtc, 0);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005152 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00005153 ironlake_pfit_disable(intel_crtc);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08005154 else
5155 MISSING_CASE(INTEL_INFO(dev)->gen);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005156
Paulo Zanoni1f544382012-10-24 11:32:00 -02005157 intel_ddi_disable_pipe_clock(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005158
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005159 if (intel_crtc->config->has_pch_encoder) {
Paulo Zanoniab4d9662012-10-31 18:12:55 -02005160 lpt_disable_pch_transcoder(dev_priv);
Paulo Zanoni1ad960f2012-11-01 21:05:05 -02005161 intel_ddi_fdi_disable(crtc);
Paulo Zanoni83616632012-10-23 18:29:54 -02005162 }
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005163
Imre Deak97b040a2014-06-25 22:01:50 +03005164 for_each_encoder_on_crtc(dev, crtc, encoder)
5165 if (encoder->post_disable)
5166 encoder->post_disable(encoder);
5167
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005168 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03005169 intel_update_watermarks(crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005170
5171 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02005172 intel_fbc_update(dev);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005173 mutex_unlock(&dev->struct_mutex);
Daniel Vetterdf8ad702014-06-25 22:02:03 +03005174
5175 if (intel_crtc_to_shared_dpll(intel_crtc))
5176 intel_disable_shared_dpll(intel_crtc);
Paulo Zanoni4f771f12012-10-23 18:29:51 -02005177}
5178
Jesse Barnes2dd24552013-04-25 12:55:01 -07005179static void i9xx_pfit_enable(struct intel_crtc *crtc)
5180{
5181 struct drm_device *dev = crtc->base.dev;
5182 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005183 struct intel_crtc_state *pipe_config = crtc->config;
Jesse Barnes2dd24552013-04-25 12:55:01 -07005184
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02005185 if (!pipe_config->gmch_pfit.control)
Jesse Barnes2dd24552013-04-25 12:55:01 -07005186 return;
5187
Daniel Vetterc0b03412013-05-28 12:05:54 +02005188 /*
5189 * The panel fitter should only be adjusted whilst the pipe is disabled,
5190 * according to register description and PRM.
5191 */
Jesse Barnes2dd24552013-04-25 12:55:01 -07005192 WARN_ON(I915_READ(PFIT_CONTROL) & PFIT_ENABLE);
5193 assert_pipe_disabled(dev_priv, crtc->pipe);
5194
Jesse Barnesb074cec2013-04-25 12:55:02 -07005195 I915_WRITE(PFIT_PGM_RATIOS, pipe_config->gmch_pfit.pgm_ratios);
5196 I915_WRITE(PFIT_CONTROL, pipe_config->gmch_pfit.control);
Daniel Vetter5a80c452013-04-25 22:52:18 +02005197
5198 /* Border color in case we don't scale up to the full screen. Black by
5199 * default, change to something else for debugging. */
5200 I915_WRITE(BCLRPAT(crtc->pipe), 0);
Jesse Barnes2dd24552013-04-25 12:55:01 -07005201}
5202
Dave Airlied05410f2014-06-05 13:22:59 +10005203static enum intel_display_power_domain port_to_power_domain(enum port port)
5204{
5205 switch (port) {
5206 case PORT_A:
5207 return POWER_DOMAIN_PORT_DDI_A_4_LANES;
5208 case PORT_B:
5209 return POWER_DOMAIN_PORT_DDI_B_4_LANES;
5210 case PORT_C:
5211 return POWER_DOMAIN_PORT_DDI_C_4_LANES;
5212 case PORT_D:
5213 return POWER_DOMAIN_PORT_DDI_D_4_LANES;
5214 default:
5215 WARN_ON_ONCE(1);
5216 return POWER_DOMAIN_PORT_OTHER;
5217 }
5218}
5219
Imre Deak77d22dc2014-03-05 16:20:52 +02005220#define for_each_power_domain(domain, mask) \
5221 for ((domain) = 0; (domain) < POWER_DOMAIN_NUM; (domain)++) \
5222 if ((1 << (domain)) & (mask))
5223
Imre Deak319be8a2014-03-04 19:22:57 +02005224enum intel_display_power_domain
5225intel_display_port_power_domain(struct intel_encoder *intel_encoder)
Imre Deak77d22dc2014-03-05 16:20:52 +02005226{
Imre Deak319be8a2014-03-04 19:22:57 +02005227 struct drm_device *dev = intel_encoder->base.dev;
5228 struct intel_digital_port *intel_dig_port;
5229
5230 switch (intel_encoder->type) {
5231 case INTEL_OUTPUT_UNKNOWN:
5232 /* Only DDI platforms should ever use this output type */
5233 WARN_ON_ONCE(!HAS_DDI(dev));
5234 case INTEL_OUTPUT_DISPLAYPORT:
5235 case INTEL_OUTPUT_HDMI:
5236 case INTEL_OUTPUT_EDP:
5237 intel_dig_port = enc_to_dig_port(&intel_encoder->base);
Dave Airlied05410f2014-06-05 13:22:59 +10005238 return port_to_power_domain(intel_dig_port->port);
Dave Airlie0e32b392014-05-02 14:02:48 +10005239 case INTEL_OUTPUT_DP_MST:
5240 intel_dig_port = enc_to_mst(&intel_encoder->base)->primary;
5241 return port_to_power_domain(intel_dig_port->port);
Imre Deak319be8a2014-03-04 19:22:57 +02005242 case INTEL_OUTPUT_ANALOG:
5243 return POWER_DOMAIN_PORT_CRT;
5244 case INTEL_OUTPUT_DSI:
5245 return POWER_DOMAIN_PORT_DSI;
5246 default:
5247 return POWER_DOMAIN_PORT_OTHER;
5248 }
5249}
5250
5251static unsigned long get_crtc_power_domains(struct drm_crtc *crtc)
5252{
5253 struct drm_device *dev = crtc->dev;
5254 struct intel_encoder *intel_encoder;
5255 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
5256 enum pipe pipe = intel_crtc->pipe;
Imre Deak77d22dc2014-03-05 16:20:52 +02005257 unsigned long mask;
5258 enum transcoder transcoder;
5259
5260 transcoder = intel_pipe_to_cpu_transcoder(dev->dev_private, pipe);
5261
5262 mask = BIT(POWER_DOMAIN_PIPE(pipe));
5263 mask |= BIT(POWER_DOMAIN_TRANSCODER(transcoder));
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02005264 if (intel_crtc->config->pch_pfit.enabled ||
5265 intel_crtc->config->pch_pfit.force_thru)
Imre Deak77d22dc2014-03-05 16:20:52 +02005266 mask |= BIT(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
5267
Imre Deak319be8a2014-03-04 19:22:57 +02005268 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
5269 mask |= BIT(intel_display_port_power_domain(intel_encoder));
5270
Imre Deak77d22dc2014-03-05 16:20:52 +02005271 return mask;
5272}
5273
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005274static void modeset_update_crtc_power_domains(struct drm_atomic_state *state)
Imre Deak77d22dc2014-03-05 16:20:52 +02005275{
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005276 struct drm_device *dev = state->dev;
Imre Deak77d22dc2014-03-05 16:20:52 +02005277 struct drm_i915_private *dev_priv = dev->dev_private;
5278 unsigned long pipe_domains[I915_MAX_PIPES] = { 0, };
5279 struct intel_crtc *crtc;
5280
5281 /*
5282 * First get all needed power domains, then put all unneeded, to avoid
5283 * any unnecessary toggling of the power wells.
5284 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005285 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005286 enum intel_display_power_domain domain;
5287
Matt Roper83d65732015-02-25 13:12:16 -08005288 if (!crtc->base.state->enable)
Imre Deak77d22dc2014-03-05 16:20:52 +02005289 continue;
5290
Imre Deak319be8a2014-03-04 19:22:57 +02005291 pipe_domains[crtc->pipe] = get_crtc_power_domains(&crtc->base);
Imre Deak77d22dc2014-03-05 16:20:52 +02005292
5293 for_each_power_domain(domain, pipe_domains[crtc->pipe])
5294 intel_display_power_get(dev_priv, domain);
5295 }
5296
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005297 if (dev_priv->display.modeset_global_resources)
Ander Conselvan de Oliveira679dacd2015-03-20 16:18:15 +02005298 dev_priv->display.modeset_global_resources(state);
Ville Syrjälä50f6e502014-11-06 14:49:12 +02005299
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005300 for_each_intel_crtc(dev, crtc) {
Imre Deak77d22dc2014-03-05 16:20:52 +02005301 enum intel_display_power_domain domain;
5302
5303 for_each_power_domain(domain, crtc->enabled_power_domains)
5304 intel_display_power_put(dev_priv, domain);
5305
5306 crtc->enabled_power_domains = pipe_domains[crtc->pipe];
5307 }
5308
5309 intel_display_set_init_power(dev_priv, false);
5310}
5311
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005312static void intel_update_max_cdclk(struct drm_device *dev)
5313{
5314 struct drm_i915_private *dev_priv = dev->dev_private;
5315
5316 if (IS_SKYLAKE(dev)) {
5317 u32 limit = I915_READ(SKL_DFSM) & SKL_DFSM_CDCLK_LIMIT_MASK;
5318
5319 if (limit == SKL_DFSM_CDCLK_LIMIT_675)
5320 dev_priv->max_cdclk_freq = 675000;
5321 else if (limit == SKL_DFSM_CDCLK_LIMIT_540)
5322 dev_priv->max_cdclk_freq = 540000;
5323 else if (limit == SKL_DFSM_CDCLK_LIMIT_450)
5324 dev_priv->max_cdclk_freq = 450000;
5325 else
5326 dev_priv->max_cdclk_freq = 337500;
5327 } else if (IS_BROADWELL(dev)) {
5328 /*
5329 * FIXME with extra cooling we can allow
5330 * 540 MHz for ULX and 675 Mhz for ULT.
5331 * How can we know if extra cooling is
5332 * available? PCI ID, VTB, something else?
5333 */
5334 if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
5335 dev_priv->max_cdclk_freq = 450000;
5336 else if (IS_BDW_ULX(dev))
5337 dev_priv->max_cdclk_freq = 450000;
5338 else if (IS_BDW_ULT(dev))
5339 dev_priv->max_cdclk_freq = 540000;
5340 else
5341 dev_priv->max_cdclk_freq = 675000;
5342 } else if (IS_VALLEYVIEW(dev)) {
5343 dev_priv->max_cdclk_freq = 400000;
5344 } else {
5345 /* otherwise assume cdclk is fixed */
5346 dev_priv->max_cdclk_freq = dev_priv->cdclk_freq;
5347 }
5348
5349 DRM_DEBUG_DRIVER("Max CD clock rate: %d kHz\n",
5350 dev_priv->max_cdclk_freq);
5351}
5352
5353static void intel_update_cdclk(struct drm_device *dev)
5354{
5355 struct drm_i915_private *dev_priv = dev->dev_private;
5356
5357 dev_priv->cdclk_freq = dev_priv->display.get_display_clock_speed(dev);
5358 DRM_DEBUG_DRIVER("Current CD clock rate: %d kHz\n",
5359 dev_priv->cdclk_freq);
5360
5361 /*
5362 * Program the gmbus_freq based on the cdclk frequency.
5363 * BSpec erroneously claims we should aim for 4MHz, but
5364 * in fact 1MHz is the correct frequency.
5365 */
5366 if (IS_VALLEYVIEW(dev)) {
5367 /*
5368 * Program the gmbus_freq based on the cdclk frequency.
5369 * BSpec erroneously claims we should aim for 4MHz, but
5370 * in fact 1MHz is the correct frequency.
5371 */
5372 I915_WRITE(GMBUSFREQ_VLV, DIV_ROUND_UP(dev_priv->cdclk_freq, 1000));
5373 }
5374
5375 if (dev_priv->max_cdclk_freq == 0)
5376 intel_update_max_cdclk(dev);
5377}
5378
Damien Lespiau70d0c572015-06-04 18:21:29 +01005379static void broxton_set_cdclk(struct drm_device *dev, int frequency)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305380{
5381 struct drm_i915_private *dev_priv = dev->dev_private;
5382 uint32_t divider;
5383 uint32_t ratio;
5384 uint32_t current_freq;
5385 int ret;
5386
5387 /* frequency = 19.2MHz * ratio / 2 / div{1,1.5,2,4} */
5388 switch (frequency) {
5389 case 144000:
5390 divider = BXT_CDCLK_CD2X_DIV_SEL_4;
5391 ratio = BXT_DE_PLL_RATIO(60);
5392 break;
5393 case 288000:
5394 divider = BXT_CDCLK_CD2X_DIV_SEL_2;
5395 ratio = BXT_DE_PLL_RATIO(60);
5396 break;
5397 case 384000:
5398 divider = BXT_CDCLK_CD2X_DIV_SEL_1_5;
5399 ratio = BXT_DE_PLL_RATIO(60);
5400 break;
5401 case 576000:
5402 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5403 ratio = BXT_DE_PLL_RATIO(60);
5404 break;
5405 case 624000:
5406 divider = BXT_CDCLK_CD2X_DIV_SEL_1;
5407 ratio = BXT_DE_PLL_RATIO(65);
5408 break;
5409 case 19200:
5410 /*
5411 * Bypass frequency with DE PLL disabled. Init ratio, divider
5412 * to suppress GCC warning.
5413 */
5414 ratio = 0;
5415 divider = 0;
5416 break;
5417 default:
5418 DRM_ERROR("unsupported CDCLK freq %d", frequency);
5419
5420 return;
5421 }
5422
5423 mutex_lock(&dev_priv->rps.hw_lock);
5424 /* Inform power controller of upcoming frequency change */
5425 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5426 0x80000000);
5427 mutex_unlock(&dev_priv->rps.hw_lock);
5428
5429 if (ret) {
5430 DRM_ERROR("PCode CDCLK freq change notify failed (err %d, freq %d)\n",
5431 ret, frequency);
5432 return;
5433 }
5434
5435 current_freq = I915_READ(CDCLK_CTL) & CDCLK_FREQ_DECIMAL_MASK;
5436 /* convert from .1 fixpoint MHz with -1MHz offset to kHz */
5437 current_freq = current_freq * 500 + 1000;
5438
5439 /*
5440 * DE PLL has to be disabled when
5441 * - setting to 19.2MHz (bypass, PLL isn't used)
5442 * - before setting to 624MHz (PLL needs toggling)
5443 * - before setting to any frequency from 624MHz (PLL needs toggling)
5444 */
5445 if (frequency == 19200 || frequency == 624000 ||
5446 current_freq == 624000) {
5447 I915_WRITE(BXT_DE_PLL_ENABLE, ~BXT_DE_PLL_PLL_ENABLE);
5448 /* Timeout 200us */
5449 if (wait_for(!(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK),
5450 1))
5451 DRM_ERROR("timout waiting for DE PLL unlock\n");
5452 }
5453
5454 if (frequency != 19200) {
5455 uint32_t val;
5456
5457 val = I915_READ(BXT_DE_PLL_CTL);
5458 val &= ~BXT_DE_PLL_RATIO_MASK;
5459 val |= ratio;
5460 I915_WRITE(BXT_DE_PLL_CTL, val);
5461
5462 I915_WRITE(BXT_DE_PLL_ENABLE, BXT_DE_PLL_PLL_ENABLE);
5463 /* Timeout 200us */
5464 if (wait_for(I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_LOCK, 1))
5465 DRM_ERROR("timeout waiting for DE PLL lock\n");
5466
5467 val = I915_READ(CDCLK_CTL);
5468 val &= ~BXT_CDCLK_CD2X_DIV_SEL_MASK;
5469 val |= divider;
5470 /*
5471 * Disable SSA Precharge when CD clock frequency < 500 MHz,
5472 * enable otherwise.
5473 */
5474 val &= ~BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5475 if (frequency >= 500000)
5476 val |= BXT_CDCLK_SSA_PRECHARGE_ENABLE;
5477
5478 val &= ~CDCLK_FREQ_DECIMAL_MASK;
5479 /* convert from kHz to .1 fixpoint MHz with -1MHz offset */
5480 val |= (frequency - 1000) / 500;
5481 I915_WRITE(CDCLK_CTL, val);
5482 }
5483
5484 mutex_lock(&dev_priv->rps.hw_lock);
5485 ret = sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ,
5486 DIV_ROUND_UP(frequency, 25000));
5487 mutex_unlock(&dev_priv->rps.hw_lock);
5488
5489 if (ret) {
5490 DRM_ERROR("PCode CDCLK freq set failed, (err %d, freq %d)\n",
5491 ret, frequency);
5492 return;
5493 }
5494
Damien Lespiaua47871b2015-06-04 18:21:34 +01005495 intel_update_cdclk(dev);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305496}
5497
5498void broxton_init_cdclk(struct drm_device *dev)
5499{
5500 struct drm_i915_private *dev_priv = dev->dev_private;
5501 uint32_t val;
5502
5503 /*
5504 * NDE_RSTWRN_OPT RST PCH Handshake En must always be 0b on BXT
5505 * or else the reset will hang because there is no PCH to respond.
5506 * Move the handshake programming to initialization sequence.
5507 * Previously was left up to BIOS.
5508 */
5509 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5510 val &= ~RESET_PCH_HANDSHAKE_ENABLE;
5511 I915_WRITE(HSW_NDE_RSTWRN_OPT, val);
5512
5513 /* Enable PG1 for cdclk */
5514 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5515
5516 /* check if cd clock is enabled */
5517 if (I915_READ(BXT_DE_PLL_ENABLE) & BXT_DE_PLL_PLL_ENABLE) {
5518 DRM_DEBUG_KMS("Display already initialized\n");
5519 return;
5520 }
5521
5522 /*
5523 * FIXME:
5524 * - The initial CDCLK needs to be read from VBT.
5525 * Need to make this change after VBT has changes for BXT.
5526 * - check if setting the max (or any) cdclk freq is really necessary
5527 * here, it belongs to modeset time
5528 */
5529 broxton_set_cdclk(dev, 624000);
5530
5531 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005532 POSTING_READ(DBUF_CTL);
5533
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305534 udelay(10);
5535
5536 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5537 DRM_ERROR("DBuf power enable timeout!\n");
5538}
5539
5540void broxton_uninit_cdclk(struct drm_device *dev)
5541{
5542 struct drm_i915_private *dev_priv = dev->dev_private;
5543
5544 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
Ville Syrjälä22e02c02015-05-06 14:28:57 +03005545 POSTING_READ(DBUF_CTL);
5546
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305547 udelay(10);
5548
5549 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5550 DRM_ERROR("DBuf power disable timeout!\n");
5551
5552 /* Set minimum (bypass) frequency, in effect turning off the DE PLL */
5553 broxton_set_cdclk(dev, 19200);
5554
5555 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5556}
5557
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005558static const struct skl_cdclk_entry {
5559 unsigned int freq;
5560 unsigned int vco;
5561} skl_cdclk_frequencies[] = {
5562 { .freq = 308570, .vco = 8640 },
5563 { .freq = 337500, .vco = 8100 },
5564 { .freq = 432000, .vco = 8640 },
5565 { .freq = 450000, .vco = 8100 },
5566 { .freq = 540000, .vco = 8100 },
5567 { .freq = 617140, .vco = 8640 },
5568 { .freq = 675000, .vco = 8100 },
5569};
5570
5571static unsigned int skl_cdclk_decimal(unsigned int freq)
5572{
5573 return (freq - 1000) / 500;
5574}
5575
5576static unsigned int skl_cdclk_get_vco(unsigned int freq)
5577{
5578 unsigned int i;
5579
5580 for (i = 0; i < ARRAY_SIZE(skl_cdclk_frequencies); i++) {
5581 const struct skl_cdclk_entry *e = &skl_cdclk_frequencies[i];
5582
5583 if (e->freq == freq)
5584 return e->vco;
5585 }
5586
5587 return 8100;
5588}
5589
5590static void
5591skl_dpll0_enable(struct drm_i915_private *dev_priv, unsigned int required_vco)
5592{
5593 unsigned int min_freq;
5594 u32 val;
5595
5596 /* select the minimum CDCLK before enabling DPLL 0 */
5597 val = I915_READ(CDCLK_CTL);
5598 val &= ~CDCLK_FREQ_SEL_MASK | ~CDCLK_FREQ_DECIMAL_MASK;
5599 val |= CDCLK_FREQ_337_308;
5600
5601 if (required_vco == 8640)
5602 min_freq = 308570;
5603 else
5604 min_freq = 337500;
5605
5606 val = CDCLK_FREQ_337_308 | skl_cdclk_decimal(min_freq);
5607
5608 I915_WRITE(CDCLK_CTL, val);
5609 POSTING_READ(CDCLK_CTL);
5610
5611 /*
5612 * We always enable DPLL0 with the lowest link rate possible, but still
5613 * taking into account the VCO required to operate the eDP panel at the
5614 * desired frequency. The usual DP link rates operate with a VCO of
5615 * 8100 while the eDP 1.4 alternate link rates need a VCO of 8640.
5616 * The modeset code is responsible for the selection of the exact link
5617 * rate later on, with the constraint of choosing a frequency that
5618 * works with required_vco.
5619 */
5620 val = I915_READ(DPLL_CTRL1);
5621
5622 val &= ~(DPLL_CTRL1_HDMI_MODE(SKL_DPLL0) | DPLL_CTRL1_SSC(SKL_DPLL0) |
5623 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0));
5624 val |= DPLL_CTRL1_OVERRIDE(SKL_DPLL0);
5625 if (required_vco == 8640)
5626 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_1080,
5627 SKL_DPLL0);
5628 else
5629 val |= DPLL_CTRL1_LINK_RATE(DPLL_CTRL1_LINK_RATE_810,
5630 SKL_DPLL0);
5631
5632 I915_WRITE(DPLL_CTRL1, val);
5633 POSTING_READ(DPLL_CTRL1);
5634
5635 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) | LCPLL_PLL_ENABLE);
5636
5637 if (wait_for(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK, 5))
5638 DRM_ERROR("DPLL0 not locked\n");
5639}
5640
5641static bool skl_cdclk_pcu_ready(struct drm_i915_private *dev_priv)
5642{
5643 int ret;
5644 u32 val;
5645
5646 /* inform PCU we want to change CDCLK */
5647 val = SKL_CDCLK_PREPARE_FOR_CHANGE;
5648 mutex_lock(&dev_priv->rps.hw_lock);
5649 ret = sandybridge_pcode_read(dev_priv, SKL_PCODE_CDCLK_CONTROL, &val);
5650 mutex_unlock(&dev_priv->rps.hw_lock);
5651
5652 return ret == 0 && (val & SKL_CDCLK_READY_FOR_CHANGE);
5653}
5654
5655static bool skl_cdclk_wait_for_pcu_ready(struct drm_i915_private *dev_priv)
5656{
5657 unsigned int i;
5658
5659 for (i = 0; i < 15; i++) {
5660 if (skl_cdclk_pcu_ready(dev_priv))
5661 return true;
5662 udelay(10);
5663 }
5664
5665 return false;
5666}
5667
5668static void skl_set_cdclk(struct drm_i915_private *dev_priv, unsigned int freq)
5669{
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005670 struct drm_device *dev = dev_priv->dev;
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005671 u32 freq_select, pcu_ack;
5672
5673 DRM_DEBUG_DRIVER("Changing CDCLK to %dKHz\n", freq);
5674
5675 if (!skl_cdclk_wait_for_pcu_ready(dev_priv)) {
5676 DRM_ERROR("failed to inform PCU about cdclk change\n");
5677 return;
5678 }
5679
5680 /* set CDCLK_CTL */
5681 switch(freq) {
5682 case 450000:
5683 case 432000:
5684 freq_select = CDCLK_FREQ_450_432;
5685 pcu_ack = 1;
5686 break;
5687 case 540000:
5688 freq_select = CDCLK_FREQ_540;
5689 pcu_ack = 2;
5690 break;
5691 case 308570:
5692 case 337500:
5693 default:
5694 freq_select = CDCLK_FREQ_337_308;
5695 pcu_ack = 0;
5696 break;
5697 case 617140:
5698 case 675000:
5699 freq_select = CDCLK_FREQ_675_617;
5700 pcu_ack = 3;
5701 break;
5702 }
5703
5704 I915_WRITE(CDCLK_CTL, freq_select | skl_cdclk_decimal(freq));
5705 POSTING_READ(CDCLK_CTL);
5706
5707 /* inform PCU of the change */
5708 mutex_lock(&dev_priv->rps.hw_lock);
5709 sandybridge_pcode_write(dev_priv, SKL_PCODE_CDCLK_CONTROL, pcu_ack);
5710 mutex_unlock(&dev_priv->rps.hw_lock);
Damien Lespiau560a7ae2015-06-04 18:21:33 +01005711
5712 intel_update_cdclk(dev);
Damien Lespiau5d96d8a2015-05-21 16:37:48 +01005713}
5714
5715void skl_uninit_cdclk(struct drm_i915_private *dev_priv)
5716{
5717 /* disable DBUF power */
5718 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) & ~DBUF_POWER_REQUEST);
5719 POSTING_READ(DBUF_CTL);
5720
5721 udelay(10);
5722
5723 if (I915_READ(DBUF_CTL) & DBUF_POWER_STATE)
5724 DRM_ERROR("DBuf power disable timeout\n");
5725
5726 /* disable DPLL0 */
5727 I915_WRITE(LCPLL1_CTL, I915_READ(LCPLL1_CTL) & ~LCPLL_PLL_ENABLE);
5728 if (wait_for(!(I915_READ(LCPLL1_CTL) & LCPLL_PLL_LOCK), 1))
5729 DRM_ERROR("Couldn't disable DPLL0\n");
5730
5731 intel_display_power_put(dev_priv, POWER_DOMAIN_PLLS);
5732}
5733
5734void skl_init_cdclk(struct drm_i915_private *dev_priv)
5735{
5736 u32 val;
5737 unsigned int required_vco;
5738
5739 /* enable PCH reset handshake */
5740 val = I915_READ(HSW_NDE_RSTWRN_OPT);
5741 I915_WRITE(HSW_NDE_RSTWRN_OPT, val | RESET_PCH_HANDSHAKE_ENABLE);
5742
5743 /* enable PG1 and Misc I/O */
5744 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
5745
5746 /* DPLL0 already enabed !? */
5747 if (I915_READ(LCPLL1_CTL) & LCPLL_PLL_ENABLE) {
5748 DRM_DEBUG_DRIVER("DPLL0 already running\n");
5749 return;
5750 }
5751
5752 /* enable DPLL0 */
5753 required_vco = skl_cdclk_get_vco(dev_priv->skl_boot_cdclk);
5754 skl_dpll0_enable(dev_priv, required_vco);
5755
5756 /* set CDCLK to the frequency the BIOS chose */
5757 skl_set_cdclk(dev_priv, dev_priv->skl_boot_cdclk);
5758
5759 /* enable DBUF power */
5760 I915_WRITE(DBUF_CTL, I915_READ(DBUF_CTL) | DBUF_POWER_REQUEST);
5761 POSTING_READ(DBUF_CTL);
5762
5763 udelay(10);
5764
5765 if (!(I915_READ(DBUF_CTL) & DBUF_POWER_STATE))
5766 DRM_ERROR("DBuf power enable timeout\n");
5767}
5768
Ville Syrjälädfcab172014-06-13 13:37:47 +03005769/* returns HPLL frequency in kHz */
Ville Syrjäläf8bf63f2014-06-13 13:37:54 +03005770static int valleyview_get_vco(struct drm_i915_private *dev_priv)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005771{
Jesse Barnes586f49d2013-11-04 16:06:59 -08005772 int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
Jesse Barnes30a970c2013-11-04 13:48:12 -08005773
Jesse Barnes586f49d2013-11-04 16:06:59 -08005774 /* Obtain SKU information */
Ville Syrjäläa5805162015-05-26 20:42:30 +03005775 mutex_lock(&dev_priv->sb_lock);
Jesse Barnes586f49d2013-11-04 16:06:59 -08005776 hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
5777 CCK_FUSE_HPLL_FREQ_MASK;
Ville Syrjäläa5805162015-05-26 20:42:30 +03005778 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005779
Ville Syrjälädfcab172014-06-13 13:37:47 +03005780 return vco_freq[hpll_freq] * 1000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005781}
5782
5783/* Adjust CDclk dividers to allow high res or save power if possible */
5784static void valleyview_set_cdclk(struct drm_device *dev, int cdclk)
5785{
5786 struct drm_i915_private *dev_priv = dev->dev_private;
5787 u32 val, cmd;
5788
Vandana Kannan164dfd22014-11-24 13:37:41 +05305789 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5790 != dev_priv->cdclk_freq);
Imre Deakd60c4472014-03-27 17:45:10 +02005791
Ville Syrjälädfcab172014-06-13 13:37:47 +03005792 if (cdclk >= 320000) /* jump to highest voltage for 400MHz too */
Jesse Barnes30a970c2013-11-04 13:48:12 -08005793 cmd = 2;
Ville Syrjälädfcab172014-06-13 13:37:47 +03005794 else if (cdclk == 266667)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005795 cmd = 1;
5796 else
5797 cmd = 0;
5798
5799 mutex_lock(&dev_priv->rps.hw_lock);
5800 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5801 val &= ~DSPFREQGUAR_MASK;
5802 val |= (cmd << DSPFREQGUAR_SHIFT);
5803 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5804 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5805 DSPFREQSTAT_MASK) == (cmd << DSPFREQSTAT_SHIFT),
5806 50)) {
5807 DRM_ERROR("timed out waiting for CDclk change\n");
5808 }
5809 mutex_unlock(&dev_priv->rps.hw_lock);
5810
Ville Syrjälä54433e92015-05-26 20:42:31 +03005811 mutex_lock(&dev_priv->sb_lock);
5812
Ville Syrjälädfcab172014-06-13 13:37:47 +03005813 if (cdclk == 400000) {
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005814 u32 divider;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005815
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005816 divider = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005817
Jesse Barnes30a970c2013-11-04 13:48:12 -08005818 /* adjust cdclk divider */
5819 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjälä9cf33db2014-06-13 13:37:48 +03005820 val &= ~DISPLAY_FREQUENCY_VALUES;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005821 val |= divider;
5822 vlv_cck_write(dev_priv, CCK_DISPLAY_CLOCK_CONTROL, val);
Ville Syrjäläa877e802014-06-13 13:37:52 +03005823
5824 if (wait_for((vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL) &
5825 DISPLAY_FREQUENCY_STATUS) == (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
5826 50))
5827 DRM_ERROR("timed out waiting for CDclk change\n");
Jesse Barnes30a970c2013-11-04 13:48:12 -08005828 }
5829
Jesse Barnes30a970c2013-11-04 13:48:12 -08005830 /* adjust self-refresh exit latency value */
5831 val = vlv_bunit_read(dev_priv, BUNIT_REG_BISOC);
5832 val &= ~0x7f;
5833
5834 /*
5835 * For high bandwidth configs, we set a higher latency in the bunit
5836 * so that the core display fetch happens in time to avoid underruns.
5837 */
Ville Syrjälädfcab172014-06-13 13:37:47 +03005838 if (cdclk == 400000)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005839 val |= 4500 / 250; /* 4.5 usec */
5840 else
5841 val |= 3000 / 250; /* 3.0 usec */
5842 vlv_bunit_write(dev_priv, BUNIT_REG_BISOC, val);
Ville Syrjälä54433e92015-05-26 20:42:31 +03005843
Ville Syrjäläa5805162015-05-26 20:42:30 +03005844 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005845
Ville Syrjäläb6283052015-06-03 15:45:07 +03005846 intel_update_cdclk(dev);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005847}
5848
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005849static void cherryview_set_cdclk(struct drm_device *dev, int cdclk)
5850{
5851 struct drm_i915_private *dev_priv = dev->dev_private;
5852 u32 val, cmd;
5853
Vandana Kannan164dfd22014-11-24 13:37:41 +05305854 WARN_ON(dev_priv->display.get_display_clock_speed(dev)
5855 != dev_priv->cdclk_freq);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005856
5857 switch (cdclk) {
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005858 case 333333:
5859 case 320000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005860 case 266667:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005861 case 200000:
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005862 break;
5863 default:
Daniel Vetter5f77eeb2014-12-08 16:40:10 +01005864 MISSING_CASE(cdclk);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005865 return;
5866 }
5867
Ville Syrjälä9d0d3fd2015-03-02 20:07:17 +02005868 /*
5869 * Specs are full of misinformation, but testing on actual
5870 * hardware has shown that we just need to write the desired
5871 * CCK divider into the Punit register.
5872 */
5873 cmd = DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, cdclk) - 1;
5874
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005875 mutex_lock(&dev_priv->rps.hw_lock);
5876 val = vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ);
5877 val &= ~DSPFREQGUAR_MASK_CHV;
5878 val |= (cmd << DSPFREQGUAR_SHIFT_CHV);
5879 vlv_punit_write(dev_priv, PUNIT_REG_DSPFREQ, val);
5880 if (wait_for((vlv_punit_read(dev_priv, PUNIT_REG_DSPFREQ) &
5881 DSPFREQSTAT_MASK_CHV) == (cmd << DSPFREQSTAT_SHIFT_CHV),
5882 50)) {
5883 DRM_ERROR("timed out waiting for CDclk change\n");
5884 }
5885 mutex_unlock(&dev_priv->rps.hw_lock);
5886
Ville Syrjäläb6283052015-06-03 15:45:07 +03005887 intel_update_cdclk(dev);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03005888}
5889
Jesse Barnes30a970c2013-11-04 13:48:12 -08005890static int valleyview_calc_cdclk(struct drm_i915_private *dev_priv,
5891 int max_pixclk)
5892{
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03005893 int freq_320 = (dev_priv->hpll_freq << 1) % 320000 != 0 ? 333333 : 320000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005894 int limit = IS_CHERRYVIEW(dev_priv) ? 95 : 90;
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005895
Jesse Barnes30a970c2013-11-04 13:48:12 -08005896 /*
5897 * Really only a few cases to deal with, as only 4 CDclks are supported:
5898 * 200MHz
5899 * 267MHz
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005900 * 320/333MHz (depends on HPLL freq)
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005901 * 400MHz (VLV only)
5902 * So we check to see whether we're above 90% (VLV) or 95% (CHV)
5903 * of the lower bin and adjust if needed.
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005904 *
5905 * We seem to get an unstable or solid color picture at 200MHz.
5906 * Not sure what's wrong. For now use 200MHz only when all pipes
5907 * are off.
Jesse Barnes30a970c2013-11-04 13:48:12 -08005908 */
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005909 if (!IS_CHERRYVIEW(dev_priv) &&
5910 max_pixclk > freq_320*limit/100)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005911 return 400000;
Ville Syrjälä6cca3192015-03-02 20:07:16 +02005912 else if (max_pixclk > 266667*limit/100)
Ville Syrjälä29dc7ef2014-06-13 13:37:50 +03005913 return freq_320;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005914 else if (max_pixclk > 0)
Ville Syrjälädfcab172014-06-13 13:37:47 +03005915 return 266667;
Ville Syrjäläe37c67a2014-06-13 13:37:51 +03005916 else
5917 return 200000;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005918}
5919
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305920static int broxton_calc_cdclk(struct drm_i915_private *dev_priv,
5921 int max_pixclk)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005922{
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305923 /*
5924 * FIXME:
5925 * - remove the guardband, it's not needed on BXT
5926 * - set 19.2MHz bypass frequency if there are no active pipes
5927 */
5928 if (max_pixclk > 576000*9/10)
5929 return 624000;
5930 else if (max_pixclk > 384000*9/10)
5931 return 576000;
5932 else if (max_pixclk > 288000*9/10)
5933 return 384000;
5934 else if (max_pixclk > 144000*9/10)
5935 return 288000;
5936 else
5937 return 144000;
5938}
5939
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005940/* Compute the max pixel clock for new configuration. Uses atomic state if
5941 * that's non-NULL, look at current state otherwise. */
5942static int intel_mode_max_pixclk(struct drm_device *dev,
5943 struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005944{
Jesse Barnes30a970c2013-11-04 13:48:12 -08005945 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005946 struct intel_crtc_state *crtc_state;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005947 int max_pixclk = 0;
5948
Damien Lespiaud3fcc802014-05-13 23:32:22 +01005949 for_each_intel_crtc(dev, intel_crtc) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005950 if (state)
5951 crtc_state =
5952 intel_atomic_get_crtc_state(state, intel_crtc);
5953 else
5954 crtc_state = intel_crtc->config;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005955 if (IS_ERR(crtc_state))
5956 return PTR_ERR(crtc_state);
5957
5958 if (!crtc_state->base.enable)
5959 continue;
5960
5961 max_pixclk = max(max_pixclk,
5962 crtc_state->base.adjusted_mode.crtc_clock);
Jesse Barnes30a970c2013-11-04 13:48:12 -08005963 }
5964
5965 return max_pixclk;
5966}
5967
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005968static int valleyview_modeset_global_pipes(struct drm_atomic_state *state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08005969{
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005970 struct drm_i915_private *dev_priv = to_i915(state->dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005971 struct drm_crtc *crtc;
5972 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03005973 int max_pixclk = intel_mode_max_pixclk(state->dev, state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005974 int cdclk, i;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005975
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005976 if (max_pixclk < 0)
5977 return max_pixclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005978
Vandana Kannanf8437dd12014-11-24 13:37:39 +05305979 if (IS_VALLEYVIEW(dev_priv))
5980 cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
5981 else
5982 cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
5983
5984 if (cdclk == dev_priv->cdclk_freq)
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03005985 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08005986
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005987 /* add all active pipes to the state */
5988 for_each_crtc(state->dev, crtc) {
5989 if (!crtc->state->enable)
5990 continue;
5991
5992 crtc_state = drm_atomic_get_crtc_state(state, crtc);
5993 if (IS_ERR(crtc_state))
5994 return PTR_ERR(crtc_state);
5995 }
5996
Ville Syrjälä2f2d7aa2014-01-10 11:28:08 +02005997 /* disable/enable all currently active pipes while we change cdclk */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +03005998 for_each_crtc_in_state(state, crtc, crtc_state, i)
5999 if (crtc_state->enable)
6000 crtc_state->mode_changed = true;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006001
6002 return 0;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006003}
6004
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006005static void vlv_program_pfi_credits(struct drm_i915_private *dev_priv)
6006{
6007 unsigned int credits, default_credits;
6008
6009 if (IS_CHERRYVIEW(dev_priv))
6010 default_credits = PFI_CREDIT(12);
6011 else
6012 default_credits = PFI_CREDIT(8);
6013
Vandana Kannan164dfd22014-11-24 13:37:41 +05306014 if (DIV_ROUND_CLOSEST(dev_priv->cdclk_freq, 1000) >= dev_priv->rps.cz_freq) {
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006015 /* CHV suggested value is 31 or 63 */
6016 if (IS_CHERRYVIEW(dev_priv))
6017 credits = PFI_CREDIT_31;
6018 else
6019 credits = PFI_CREDIT(15);
6020 } else {
6021 credits = default_credits;
6022 }
6023
6024 /*
6025 * WA - write default credits before re-programming
6026 * FIXME: should we also set the resend bit here?
6027 */
6028 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6029 default_credits);
6030
6031 I915_WRITE(GCI_CONTROL, VGA_FAST_MODE_DISABLE |
6032 credits | PFI_CREDIT_RESEND);
6033
6034 /*
6035 * FIXME is this guaranteed to clear
6036 * immediately or should we poll for it?
6037 */
6038 WARN_ON(I915_READ(GCI_CONTROL) & PFI_CREDIT_RESEND);
6039}
6040
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006041static void valleyview_modeset_global_resources(struct drm_atomic_state *old_state)
Jesse Barnes30a970c2013-11-04 13:48:12 -08006042{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006043 struct drm_device *dev = old_state->dev;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006044 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006045 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006046 int req_cdclk;
Jesse Barnes30a970c2013-11-04 13:48:12 -08006047
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03006048 /* The path in intel_mode_max_pixclk() with a NULL atomic state should
6049 * never fail. */
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +03006050 if (WARN_ON(max_pixclk < 0))
6051 return;
6052
6053 req_cdclk = valleyview_calc_cdclk(dev_priv, max_pixclk);
Jesse Barnes30a970c2013-11-04 13:48:12 -08006054
Vandana Kannan164dfd22014-11-24 13:37:41 +05306055 if (req_cdclk != dev_priv->cdclk_freq) {
Imre Deak738c05c2014-11-19 16:25:37 +02006056 /*
6057 * FIXME: We can end up here with all power domains off, yet
6058 * with a CDCLK frequency other than the minimum. To account
6059 * for this take the PIPE-A power domain, which covers the HW
6060 * blocks needed for the following programming. This can be
6061 * removed once it's guaranteed that we get here either with
6062 * the minimum CDCLK set, or the required power domains
6063 * enabled.
6064 */
6065 intel_display_power_get(dev_priv, POWER_DOMAIN_PIPE_A);
6066
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006067 if (IS_CHERRYVIEW(dev))
6068 cherryview_set_cdclk(dev, req_cdclk);
6069 else
6070 valleyview_set_cdclk(dev, req_cdclk);
Imre Deak738c05c2014-11-19 16:25:37 +02006071
Vidya Srinivas1e69cd72015-03-05 21:19:50 +02006072 vlv_program_pfi_credits(dev_priv);
6073
Imre Deak738c05c2014-11-19 16:25:37 +02006074 intel_display_power_put(dev_priv, POWER_DOMAIN_PIPE_A);
Ville Syrjälä383c5a62014-06-28 02:03:57 +03006075 }
Jesse Barnes30a970c2013-11-04 13:48:12 -08006076}
6077
Jesse Barnes89b667f2013-04-18 14:51:36 -07006078static void valleyview_crtc_enable(struct drm_crtc *crtc)
6079{
6080 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006081 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006082 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6083 struct intel_encoder *encoder;
6084 int pipe = intel_crtc->pipe;
Jani Nikula23538ef2013-08-27 15:12:22 +03006085 bool is_dsi;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006086
Matt Roper83d65732015-02-25 13:12:16 -08006087 WARN_ON(!crtc->state->enable);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006088
6089 if (intel_crtc->active)
6090 return;
6091
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006092 is_dsi = intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI);
Shobhit Kumar8525a232014-06-25 12:20:39 +05306093
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006094 if (!is_dsi) {
6095 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006096 chv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006097 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006098 vlv_prepare_pll(intel_crtc, intel_crtc->config);
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03006099 }
Daniel Vetter5b18e572014-04-24 23:55:06 +02006100
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006101 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306102 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006103
6104 intel_set_pipe_timings(intel_crtc);
6105
Ville Syrjäläc14b0482014-10-16 20:52:34 +03006106 if (IS_CHERRYVIEW(dev) && pipe == PIPE_B) {
6107 struct drm_i915_private *dev_priv = dev->dev_private;
6108
6109 I915_WRITE(CHV_BLEND(pipe), CHV_BLEND_LEGACY);
6110 I915_WRITE(CHV_CANVAS(pipe), 0);
6111 }
6112
Daniel Vetter5b18e572014-04-24 23:55:06 +02006113 i9xx_set_pipeconf(intel_crtc);
6114
Jesse Barnes89b667f2013-04-18 14:51:36 -07006115 intel_crtc->active = true;
Jesse Barnes89b667f2013-04-18 14:51:36 -07006116
Daniel Vettera72e4c92014-09-30 10:56:47 +02006117 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006118
Jesse Barnes89b667f2013-04-18 14:51:36 -07006119 for_each_encoder_on_crtc(dev, crtc, encoder)
6120 if (encoder->pre_pll_enable)
6121 encoder->pre_pll_enable(encoder);
6122
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006123 if (!is_dsi) {
6124 if (IS_CHERRYVIEW(dev))
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006125 chv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006126 else
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006127 vlv_enable_pll(intel_crtc, intel_crtc->config);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03006128 }
Jesse Barnes89b667f2013-04-18 14:51:36 -07006129
6130 for_each_encoder_on_crtc(dev, crtc, encoder)
6131 if (encoder->pre_enable)
6132 encoder->pre_enable(encoder);
6133
Jesse Barnes2dd24552013-04-25 12:55:01 -07006134 i9xx_pfit_enable(intel_crtc);
6135
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006136 intel_crtc_load_lut(crtc);
6137
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006138 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006139 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006140
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006141 assert_vblank_disabled(crtc);
6142 drm_crtc_vblank_on(crtc);
6143
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006144 for_each_encoder_on_crtc(dev, crtc, encoder)
6145 encoder->enable(encoder);
Jesse Barnes89b667f2013-04-18 14:51:36 -07006146}
6147
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006148static void i9xx_set_pll_dividers(struct intel_crtc *crtc)
6149{
6150 struct drm_device *dev = crtc->base.dev;
6151 struct drm_i915_private *dev_priv = dev->dev_private;
6152
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006153 I915_WRITE(FP0(crtc->pipe), crtc->config->dpll_hw_state.fp0);
6154 I915_WRITE(FP1(crtc->pipe), crtc->config->dpll_hw_state.fp1);
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006155}
6156
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006157static void i9xx_crtc_enable(struct drm_crtc *crtc)
Zhenyu Wang2c072452009-06-05 15:38:42 +08006158{
6159 struct drm_device *dev = crtc->dev;
Daniel Vettera72e4c92014-09-30 10:56:47 +02006160 struct drm_i915_private *dev_priv = to_i915(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -08006161 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006162 struct intel_encoder *encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -08006163 int pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -08006164
Matt Roper83d65732015-02-25 13:12:16 -08006165 WARN_ON(!crtc->state->enable);
Daniel Vetter08a48462012-07-02 11:43:47 +02006166
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006167 if (intel_crtc->active)
6168 return;
6169
Daniel Vetterf13c2ef2014-04-24 23:55:10 +02006170 i9xx_set_pll_dividers(intel_crtc);
6171
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006172 if (intel_crtc->config->has_dp_encoder)
Ramalingam Cfe3cd482015-02-13 15:32:59 +05306173 intel_dp_set_m_n(intel_crtc, M1_N1);
Daniel Vetter5b18e572014-04-24 23:55:06 +02006174
6175 intel_set_pipe_timings(intel_crtc);
6176
Daniel Vetter5b18e572014-04-24 23:55:06 +02006177 i9xx_set_pipeconf(intel_crtc);
6178
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006179 intel_crtc->active = true;
Chris Wilson6b383a72010-09-13 13:54:26 +01006180
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006181 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006182 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006183
Daniel Vetter66e3d5c2013-06-16 21:24:16 +02006184 for_each_encoder_on_crtc(dev, crtc, encoder)
Mika Kuoppala9d6d9f12013-02-08 16:35:38 +02006185 if (encoder->pre_enable)
6186 encoder->pre_enable(encoder);
6187
Daniel Vetterf6736a12013-06-05 13:34:30 +02006188 i9xx_enable_pll(intel_crtc);
6189
Jesse Barnes2dd24552013-04-25 12:55:01 -07006190 i9xx_pfit_enable(intel_crtc);
6191
Ville Syrjälä63cbb072013-06-04 13:48:59 +03006192 intel_crtc_load_lut(crtc);
6193
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006194 intel_update_watermarks(crtc);
Paulo Zanonie1fdc472014-01-17 13:51:12 -02006195 intel_enable_pipe(intel_crtc);
Daniel Vetterbe6a6f82014-04-15 18:41:22 +02006196
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006197 assert_vblank_disabled(crtc);
6198 drm_crtc_vblank_on(crtc);
6199
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006200 for_each_encoder_on_crtc(dev, crtc, encoder)
6201 encoder->enable(encoder);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006202}
6203
Daniel Vetter87476d62013-04-11 16:29:06 +02006204static void i9xx_pfit_disable(struct intel_crtc *crtc)
6205{
6206 struct drm_device *dev = crtc->base.dev;
6207 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter328d8e82013-05-08 10:36:31 +02006208
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02006209 if (!crtc->config->gmch_pfit.control)
Daniel Vetter328d8e82013-05-08 10:36:31 +02006210 return;
Daniel Vetter87476d62013-04-11 16:29:06 +02006211
6212 assert_pipe_disabled(dev_priv, crtc->pipe);
6213
Daniel Vetter328d8e82013-05-08 10:36:31 +02006214 DRM_DEBUG_DRIVER("disabling pfit, current: 0x%08x\n",
6215 I915_READ(PFIT_CONTROL));
6216 I915_WRITE(PFIT_CONTROL, 0);
Daniel Vetter87476d62013-04-11 16:29:06 +02006217}
6218
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006219static void i9xx_crtc_disable(struct drm_crtc *crtc)
6220{
6221 struct drm_device *dev = crtc->dev;
6222 struct drm_i915_private *dev_priv = dev->dev_private;
6223 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006224 struct intel_encoder *encoder;
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006225 int pipe = intel_crtc->pipe;
Daniel Vetteref9c3ae2012-06-29 22:40:09 +02006226
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006227 if (!intel_crtc->active)
6228 return;
6229
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006230 /*
6231 * On gen2 planes are double buffered but the pipe isn't, so we must
6232 * wait for planes to fully turn off before disabling the pipe.
Imre Deak564ed192014-06-13 14:54:21 +03006233 * We also need to wait on all gmch platforms because of the
6234 * self-refresh mode constraint explained above.
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006235 */
Imre Deak564ed192014-06-13 14:54:21 +03006236 intel_wait_for_vblank(dev, pipe);
Ville Syrjälä6304cd92014-04-25 13:30:12 +03006237
Ville Syrjälä4b3a9522014-08-14 22:04:37 +03006238 for_each_encoder_on_crtc(dev, crtc, encoder)
6239 encoder->disable(encoder);
6240
Daniel Vetterf9b61ff2015-01-07 13:54:39 +01006241 drm_crtc_vblank_off(crtc);
6242 assert_vblank_disabled(crtc);
6243
Ville Syrjälä575f7ab2014-08-15 01:21:56 +03006244 intel_disable_pipe(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006245
Daniel Vetter87476d62013-04-11 16:29:06 +02006246 i9xx_pfit_disable(intel_crtc);
Mika Kuoppala24a1f162013-02-08 16:35:37 +02006247
Jesse Barnes89b667f2013-04-18 14:51:36 -07006248 for_each_encoder_on_crtc(dev, crtc, encoder)
6249 if (encoder->post_disable)
6250 encoder->post_disable(encoder);
6251
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03006252 if (!intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_DSI)) {
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006253 if (IS_CHERRYVIEW(dev))
6254 chv_disable_pll(dev_priv, pipe);
6255 else if (IS_VALLEYVIEW(dev))
6256 vlv_disable_pll(dev_priv, pipe);
6257 else
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03006258 i9xx_disable_pll(intel_crtc);
Chon Ming Lee076ed3b2014-04-09 13:28:17 +03006259 }
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006260
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006261 if (!IS_GEN2(dev))
Daniel Vettera72e4c92014-09-30 10:56:47 +02006262 intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
Ville Syrjälä4a3436e2014-05-16 19:40:25 +03006263
Chris Wilsonf7abfe82010-09-13 14:19:16 +01006264 intel_crtc->active = false;
Ville Syrjälä46ba6142013-09-10 11:40:40 +03006265 intel_update_watermarks(crtc);
Ville Syrjäläf37fcc22013-09-10 11:39:55 +03006266
Daniel Vetterefa96242014-04-24 23:55:02 +02006267 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -02006268 intel_fbc_update(dev);
Daniel Vetterefa96242014-04-24 23:55:02 +02006269 mutex_unlock(&dev->struct_mutex);
Jesse Barnes0b8765c62010-09-10 10:31:34 -07006270}
6271
Maarten Lankhorst6b72d482015-06-01 12:49:47 +02006272/*
6273 * turn all crtc's off, but do not adjust state
6274 * This has to be paired with a call to intel_modeset_setup_hw_state.
6275 */
6276void intel_display_suspend(struct drm_device *dev)
6277{
6278 struct drm_i915_private *dev_priv = to_i915(dev);
6279 struct drm_crtc *crtc;
6280
6281 for_each_crtc(dev, crtc) {
6282 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
6283 enum intel_display_power_domain domain;
6284 unsigned long domains;
6285
6286 if (!intel_crtc->active)
6287 continue;
6288
6289 intel_crtc_disable_planes(crtc);
6290 dev_priv->display.crtc_disable(crtc);
6291
6292 domains = intel_crtc->enabled_power_domains;
6293 for_each_power_domain(domain, domains)
6294 intel_display_power_put(dev_priv, domain);
6295 intel_crtc->enabled_power_domains = 0;
6296 }
6297}
6298
Borun Fub04c5bd2014-07-12 10:02:27 +05306299/* Master function to enable/disable CRTC and corresponding power wells */
6300void intel_crtc_control(struct drm_crtc *crtc, bool enable)
Chris Wilsoncdd59982010-09-08 16:30:16 +01006301{
Chris Wilsoncdd59982010-09-08 16:30:16 +01006302 struct drm_device *dev = crtc->dev;
Jesse Barnesee7b9f92012-04-20 17:11:53 +01006303 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006304 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006305 enum intel_display_power_domain domain;
6306 unsigned long domains;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006307
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006308 if (enable) {
6309 if (!intel_crtc->active) {
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006310 domains = get_crtc_power_domains(crtc);
6311 for_each_power_domain(domain, domains)
6312 intel_display_power_get(dev_priv, domain);
6313 intel_crtc->enabled_power_domains = domains;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006314
6315 dev_priv->display.crtc_enable(crtc);
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006316 intel_crtc_enable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006317 }
6318 } else {
6319 if (intel_crtc->active) {
Maarten Lankhorstce22dba2015-04-21 17:12:56 +03006320 intel_crtc_disable_planes(crtc);
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006321 dev_priv->display.crtc_disable(crtc);
6322
Daniel Vettere1e9fb82014-06-25 22:02:04 +03006323 domains = intel_crtc->enabled_power_domains;
6324 for_each_power_domain(domain, domains)
6325 intel_display_power_put(dev_priv, domain);
6326 intel_crtc->enabled_power_domains = 0;
Daniel Vetter0e572fe2014-04-24 23:55:42 +02006327 }
6328 }
Borun Fub04c5bd2014-07-12 10:02:27 +05306329}
6330
6331/**
6332 * Sets the power management mode of the pipe and plane.
6333 */
6334void intel_crtc_update_dpms(struct drm_crtc *crtc)
6335{
6336 struct drm_device *dev = crtc->dev;
6337 struct intel_encoder *intel_encoder;
6338 bool enable = false;
6339
6340 for_each_encoder_on_crtc(dev, crtc, intel_encoder)
6341 enable |= intel_encoder->connectors_active;
6342
6343 intel_crtc_control(crtc, enable);
Ander Conselvan de Oliveira0f63cca2015-04-21 17:13:17 +03006344
6345 crtc->state->active = enable;
Daniel Vetter976f8a22012-07-08 22:34:21 +02006346}
6347
Chris Wilsonea5b2132010-08-04 13:50:23 +01006348void intel_encoder_destroy(struct drm_encoder *encoder)
6349{
Chris Wilson4ef69c72010-09-09 15:14:28 +01006350 struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
Chris Wilsonea5b2132010-08-04 13:50:23 +01006351
Chris Wilsonea5b2132010-08-04 13:50:23 +01006352 drm_encoder_cleanup(encoder);
6353 kfree(intel_encoder);
6354}
6355
Damien Lespiau92373292013-08-08 22:28:57 +01006356/* Simple dpms helper for encoders with just one connector, no cloning and only
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006357 * one kind of off state. It clamps all !ON modes to fully OFF and changes the
6358 * state of the entire output pipe. */
Damien Lespiau92373292013-08-08 22:28:57 +01006359static void intel_encoder_dpms(struct intel_encoder *encoder, int mode)
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006360{
6361 if (mode == DRM_MODE_DPMS_ON) {
6362 encoder->connectors_active = true;
6363
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006364 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006365 } else {
6366 encoder->connectors_active = false;
6367
Daniel Vetterb2cabb02012-07-01 22:42:24 +02006368 intel_crtc_update_dpms(encoder->base.crtc);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006369 }
6370}
6371
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006372/* Cross check the actual hw state with our own modeset state tracking (and it's
6373 * internal consistency). */
Daniel Vetterb9805142012-08-31 17:37:33 +02006374static void intel_connector_check_state(struct intel_connector *connector)
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006375{
6376 if (connector->get_hw_state(connector)) {
6377 struct intel_encoder *encoder = connector->encoder;
6378 struct drm_crtc *crtc;
6379 bool encoder_enabled;
6380 enum pipe pipe;
6381
6382 DRM_DEBUG_KMS("[CONNECTOR:%d:%s]\n",
6383 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +03006384 connector->base.name);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006385
Dave Airlie0e32b392014-05-02 14:02:48 +10006386 /* there is no real hw state for MST connectors */
6387 if (connector->mst_port)
6388 return;
6389
Rob Clarke2c719b2014-12-15 13:56:32 -05006390 I915_STATE_WARN(connector->base.dpms == DRM_MODE_DPMS_OFF,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006391 "wrong connector dpms state\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006392 I915_STATE_WARN(connector->base.encoder != &encoder->base,
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006393 "active connector not linked to encoder\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006394
Dave Airlie36cd7442014-05-02 13:44:18 +10006395 if (encoder) {
Rob Clarke2c719b2014-12-15 13:56:32 -05006396 I915_STATE_WARN(!encoder->connectors_active,
Dave Airlie36cd7442014-05-02 13:44:18 +10006397 "encoder->connectors_active not set\n");
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006398
Dave Airlie36cd7442014-05-02 13:44:18 +10006399 encoder_enabled = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -05006400 I915_STATE_WARN(!encoder_enabled, "encoder not enabled\n");
6401 if (I915_STATE_WARN_ON(!encoder->base.crtc))
Dave Airlie36cd7442014-05-02 13:44:18 +10006402 return;
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006403
Dave Airlie36cd7442014-05-02 13:44:18 +10006404 crtc = encoder->base.crtc;
6405
Matt Roper83d65732015-02-25 13:12:16 -08006406 I915_STATE_WARN(!crtc->state->enable,
6407 "crtc not enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05006408 I915_STATE_WARN(!to_intel_crtc(crtc)->active, "crtc not active\n");
6409 I915_STATE_WARN(pipe != to_intel_crtc(crtc)->pipe,
Dave Airlie36cd7442014-05-02 13:44:18 +10006410 "encoder active on the wrong pipe\n");
6411 }
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006412 }
6413}
6414
Ander Conselvan de Oliveira08d9bc92015-04-10 10:59:10 +03006415int intel_connector_init(struct intel_connector *connector)
6416{
6417 struct drm_connector_state *connector_state;
6418
6419 connector_state = kzalloc(sizeof *connector_state, GFP_KERNEL);
6420 if (!connector_state)
6421 return -ENOMEM;
6422
6423 connector->base.state = connector_state;
6424 return 0;
6425}
6426
6427struct intel_connector *intel_connector_alloc(void)
6428{
6429 struct intel_connector *connector;
6430
6431 connector = kzalloc(sizeof *connector, GFP_KERNEL);
6432 if (!connector)
6433 return NULL;
6434
6435 if (intel_connector_init(connector) < 0) {
6436 kfree(connector);
6437 return NULL;
6438 }
6439
6440 return connector;
6441}
6442
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006443/* Even simpler default implementation, if there's really no special case to
6444 * consider. */
6445void intel_connector_dpms(struct drm_connector *connector, int mode)
6446{
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006447 /* All the simple cases only support two dpms states. */
6448 if (mode != DRM_MODE_DPMS_ON)
6449 mode = DRM_MODE_DPMS_OFF;
6450
6451 if (mode == connector->dpms)
6452 return;
6453
6454 connector->dpms = mode;
6455
6456 /* Only need to change hw state when actually enabled */
Chris Wilsonc9976dc2013-09-29 19:15:07 +01006457 if (connector->encoder)
6458 intel_encoder_dpms(to_intel_encoder(connector->encoder), mode);
Daniel Vetter0a91ca22012-07-02 21:54:27 +02006459
Daniel Vetterb9805142012-08-31 17:37:33 +02006460 intel_modeset_check_state(connector->dev);
Daniel Vetter5ab432e2012-06-30 08:59:56 +02006461}
6462
Daniel Vetterf0947c32012-07-02 13:10:34 +02006463/* Simple connector->get_hw_state implementation for encoders that support only
6464 * one connector and no cloning and hence the encoder state determines the state
6465 * of the connector. */
6466bool intel_connector_get_hw_state(struct intel_connector *connector)
6467{
Daniel Vetter24929352012-07-02 20:28:59 +02006468 enum pipe pipe = 0;
Daniel Vetterf0947c32012-07-02 13:10:34 +02006469 struct intel_encoder *encoder = connector->encoder;
6470
6471 return encoder->get_hw_state(encoder, &pipe);
6472}
6473
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006474static int pipe_required_fdi_lanes(struct intel_crtc_state *crtc_state)
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006475{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006476 if (crtc_state->base.enable && crtc_state->has_pch_encoder)
6477 return crtc_state->fdi_lanes;
Ville Syrjäläd272ddf2015-03-11 18:52:31 +02006478
6479 return 0;
6480}
6481
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006482static int ironlake_check_fdi_lanes(struct drm_device *dev, enum pipe pipe,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006483 struct intel_crtc_state *pipe_config)
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006484{
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006485 struct drm_atomic_state *state = pipe_config->base.state;
6486 struct intel_crtc *other_crtc;
6487 struct intel_crtc_state *other_crtc_state;
6488
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006489 DRM_DEBUG_KMS("checking fdi config on pipe %c, lanes %i\n",
6490 pipe_name(pipe), pipe_config->fdi_lanes);
6491 if (pipe_config->fdi_lanes > 4) {
6492 DRM_DEBUG_KMS("invalid fdi lane config on pipe %c: %i lanes\n",
6493 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006494 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006495 }
6496
Paulo Zanonibafb6552013-11-02 21:07:44 -07006497 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006498 if (pipe_config->fdi_lanes > 2) {
6499 DRM_DEBUG_KMS("only 2 lanes on haswell, required: %i lanes\n",
6500 pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006501 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006502 } else {
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006503 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006504 }
6505 }
6506
6507 if (INTEL_INFO(dev)->num_pipes == 2)
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006508 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006509
6510 /* Ivybridge 3 pipe is really complicated */
6511 switch (pipe) {
6512 case PIPE_A:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006513 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006514 case PIPE_B:
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006515 if (pipe_config->fdi_lanes <= 2)
6516 return 0;
6517
6518 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_C));
6519 other_crtc_state =
6520 intel_atomic_get_crtc_state(state, other_crtc);
6521 if (IS_ERR(other_crtc_state))
6522 return PTR_ERR(other_crtc_state);
6523
6524 if (pipe_required_fdi_lanes(other_crtc_state) > 0) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006525 DRM_DEBUG_KMS("invalid shared fdi lane config on pipe %c: %i lanes\n",
6526 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006527 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006528 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006529 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006530 case PIPE_C:
Ville Syrjälä251cc672015-03-11 18:52:30 +02006531 if (pipe_config->fdi_lanes > 2) {
6532 DRM_DEBUG_KMS("only 2 lanes on pipe %c: required %i lanes\n",
6533 pipe_name(pipe), pipe_config->fdi_lanes);
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006534 return -EINVAL;
Ville Syrjälä251cc672015-03-11 18:52:30 +02006535 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006536
6537 other_crtc = to_intel_crtc(intel_get_crtc_for_pipe(dev, PIPE_B));
6538 other_crtc_state =
6539 intel_atomic_get_crtc_state(state, other_crtc);
6540 if (IS_ERR(other_crtc_state))
6541 return PTR_ERR(other_crtc_state);
6542
6543 if (pipe_required_fdi_lanes(other_crtc_state) > 2) {
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006544 DRM_DEBUG_KMS("fdi link B uses too many lanes to enable link C\n");
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006545 return -EINVAL;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006546 }
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006547 return 0;
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006548 default:
6549 BUG();
6550 }
6551}
6552
Daniel Vettere29c22c2013-02-21 00:00:16 +01006553#define RETRY 1
6554static int ironlake_fdi_compute_config(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006555 struct intel_crtc_state *pipe_config)
Daniel Vetter877d48d2013-04-19 11:24:43 +02006556{
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006557 struct drm_device *dev = intel_crtc->base.dev;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006558 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006559 int lane, link_bw, fdi_dotclock, ret;
6560 bool needs_recompute = false;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006561
Daniel Vettere29c22c2013-02-21 00:00:16 +01006562retry:
Daniel Vetter877d48d2013-04-19 11:24:43 +02006563 /* FDI is a binary signal running at ~2.7GHz, encoding
6564 * each output octet as 10 bits. The actual frequency
6565 * is stored as a divider into a 100MHz clock, and the
6566 * mode pixel clock is stored in units of 1KHz.
6567 * Hence the bw of each lane in terms of the mode signal
6568 * is:
6569 */
6570 link_bw = intel_fdi_link_freq(dev) * MHz(100)/KHz(1)/10;
6571
Damien Lespiau241bfc32013-09-25 16:45:37 +01006572 fdi_dotclock = adjusted_mode->crtc_clock;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006573
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006574 lane = ironlake_get_lanes_required(fdi_dotclock, link_bw,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006575 pipe_config->pipe_bpp);
6576
6577 pipe_config->fdi_lanes = lane;
6578
Daniel Vetter2bd89a02013-06-01 17:16:19 +02006579 intel_link_compute_m_n(pipe_config->pipe_bpp, lane, fdi_dotclock,
Daniel Vetter877d48d2013-04-19 11:24:43 +02006580 link_bw, &pipe_config->fdi_m_n);
Daniel Vetter1857e1d2013-04-29 19:34:16 +02006581
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006582 ret = ironlake_check_fdi_lanes(intel_crtc->base.dev,
6583 intel_crtc->pipe, pipe_config);
6584 if (ret == -EINVAL && pipe_config->pipe_bpp > 6*3) {
Daniel Vettere29c22c2013-02-21 00:00:16 +01006585 pipe_config->pipe_bpp -= 2*3;
6586 DRM_DEBUG_KMS("fdi link bw constraint, reducing pipe bpp to %i\n",
6587 pipe_config->pipe_bpp);
6588 needs_recompute = true;
6589 pipe_config->bw_constrained = true;
6590
6591 goto retry;
6592 }
6593
6594 if (needs_recompute)
6595 return RETRY;
6596
Ander Conselvan de Oliveira6d293982015-03-30 08:33:12 +03006597 return ret;
Daniel Vetter877d48d2013-04-19 11:24:43 +02006598}
6599
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006600static bool pipe_config_supports_ips(struct drm_i915_private *dev_priv,
6601 struct intel_crtc_state *pipe_config)
6602{
6603 if (pipe_config->pipe_bpp > 24)
6604 return false;
6605
6606 /* HSW can handle pixel rate up to cdclk? */
6607 if (IS_HASWELL(dev_priv->dev))
6608 return true;
6609
6610 /*
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03006611 * We compare against max which means we must take
6612 * the increased cdclk requirement into account when
6613 * calculating the new cdclk.
6614 *
6615 * Should measure whether using a lower cdclk w/o IPS
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006616 */
6617 return ilk_pipe_pixel_rate(pipe_config) <=
6618 dev_priv->max_cdclk_freq * 95 / 100;
6619}
6620
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006621static void hsw_compute_ips_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006622 struct intel_crtc_state *pipe_config)
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006623{
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006624 struct drm_device *dev = crtc->base.dev;
6625 struct drm_i915_private *dev_priv = dev->dev_private;
6626
Jani Nikulad330a952014-01-21 11:24:25 +02006627 pipe_config->ips_enabled = i915.enable_ips &&
Ville Syrjälä8cfb3402015-06-03 15:45:11 +03006628 hsw_crtc_supports_ips(crtc) &&
6629 pipe_config_supports_ips(dev_priv, pipe_config);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03006630}
6631
Daniel Vettera43f6e02013-06-07 23:10:32 +02006632static int intel_crtc_compute_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02006633 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -08006634{
Daniel Vettera43f6e02013-06-07 23:10:32 +02006635 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira8bd31e62014-10-29 11:32:33 +02006636 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02006637 struct drm_display_mode *adjusted_mode = &pipe_config->base.adjusted_mode;
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006638 int ret;
Chris Wilson89749352010-09-12 18:25:19 +01006639
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006640 /* FIXME should check pixel clock limits on all platforms */
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006641 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä44913152015-06-03 15:45:10 +03006642 int clock_limit = dev_priv->max_cdclk_freq;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006643
6644 /*
6645 * Enable pixel doubling when the dot clock
6646 * is > 90% of the (display) core speed.
6647 *
Ville Syrjäläb397c962013-09-04 18:30:06 +03006648 * GDG double wide on either pipe,
6649 * otherwise pipe A only.
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006650 */
Ville Syrjäläb397c962013-09-04 18:30:06 +03006651 if ((crtc->pipe == PIPE_A || IS_I915G(dev)) &&
Damien Lespiau241bfc32013-09-25 16:45:37 +01006652 adjusted_mode->crtc_clock > clock_limit * 9 / 10) {
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006653 clock_limit *= 2;
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03006654 pipe_config->double_wide = true;
Ville Syrjäläad3a4472013-09-04 18:30:04 +03006655 }
6656
Damien Lespiau241bfc32013-09-25 16:45:37 +01006657 if (adjusted_mode->crtc_clock > clock_limit * 9 / 10)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006658 return -EINVAL;
Zhenyu Wang2c072452009-06-05 15:38:42 +08006659 }
Chris Wilson89749352010-09-12 18:25:19 +01006660
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006661 /*
6662 * Pipe horizontal size must be even in:
6663 * - DVO ganged mode
6664 * - LVDS dual channel mode
6665 * - Double wide pipe
6666 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02006667 if ((intel_pipe_will_have_type(pipe_config, INTEL_OUTPUT_LVDS) &&
Ville Syrjälä1d1d0e22013-09-04 18:30:05 +03006668 intel_is_dual_link_lvds(dev)) || pipe_config->double_wide)
6669 pipe_config->pipe_src_w &= ~1;
6670
Damien Lespiau8693a822013-05-03 18:48:11 +01006671 /* Cantiga+ cannot handle modes with a hsync front porch of 0.
6672 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
Chris Wilson44f46b422012-06-21 13:19:59 +03006673 */
6674 if ((INTEL_INFO(dev)->gen > 4 || IS_G4X(dev)) &&
6675 adjusted_mode->hsync_start == adjusted_mode->hdisplay)
Daniel Vettere29c22c2013-02-21 00:00:16 +01006676 return -EINVAL;
Chris Wilson44f46b422012-06-21 13:19:59 +03006677
Damien Lespiauf5adf942013-06-24 18:29:34 +01006678 if (HAS_IPS(dev))
Daniel Vettera43f6e02013-06-07 23:10:32 +02006679 hsw_compute_ips_config(crtc, pipe_config);
6680
Daniel Vetter877d48d2013-04-19 11:24:43 +02006681 if (pipe_config->has_pch_encoder)
Daniel Vettera43f6e02013-06-07 23:10:32 +02006682 return ironlake_fdi_compute_config(crtc, pipe_config);
Daniel Vetter877d48d2013-04-19 11:24:43 +02006683
Chandra Kondurud03c93d2015-04-09 16:42:46 -07006684 /* FIXME: remove below call once atomic mode set is place and all crtc
6685 * related checks called from atomic_crtc_check function */
6686 ret = 0;
6687 DRM_DEBUG_KMS("intel_crtc = %p drm_state (pipe_config->base.state) = %p\n",
6688 crtc, pipe_config->base.state);
6689 ret = intel_atomic_setup_scalers(dev, crtc, pipe_config);
6690
6691 return ret;
Jesse Barnes79e53942008-11-07 14:24:08 -08006692}
6693
Ville Syrjälä1652d192015-03-31 14:12:01 +03006694static int skylake_get_display_clock_speed(struct drm_device *dev)
6695{
6696 struct drm_i915_private *dev_priv = to_i915(dev);
6697 uint32_t lcpll1 = I915_READ(LCPLL1_CTL);
6698 uint32_t cdctl = I915_READ(CDCLK_CTL);
6699 uint32_t linkrate;
6700
Damien Lespiau414355a2015-06-04 18:21:31 +01006701 if (!(lcpll1 & LCPLL_PLL_ENABLE))
Ville Syrjälä1652d192015-03-31 14:12:01 +03006702 return 24000; /* 24MHz is the cd freq with NSSC ref */
Ville Syrjälä1652d192015-03-31 14:12:01 +03006703
6704 if ((cdctl & CDCLK_FREQ_SEL_MASK) == CDCLK_FREQ_540)
6705 return 540000;
6706
6707 linkrate = (I915_READ(DPLL_CTRL1) &
Damien Lespiau71cd8422015-04-30 16:39:17 +01006708 DPLL_CTRL1_LINK_RATE_MASK(SKL_DPLL0)) >> 1;
Ville Syrjälä1652d192015-03-31 14:12:01 +03006709
Damien Lespiau71cd8422015-04-30 16:39:17 +01006710 if (linkrate == DPLL_CTRL1_LINK_RATE_2160 ||
6711 linkrate == DPLL_CTRL1_LINK_RATE_1080) {
Ville Syrjälä1652d192015-03-31 14:12:01 +03006712 /* vco 8640 */
6713 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6714 case CDCLK_FREQ_450_432:
6715 return 432000;
6716 case CDCLK_FREQ_337_308:
6717 return 308570;
6718 case CDCLK_FREQ_675_617:
6719 return 617140;
6720 default:
6721 WARN(1, "Unknown cd freq selection\n");
6722 }
6723 } else {
6724 /* vco 8100 */
6725 switch (cdctl & CDCLK_FREQ_SEL_MASK) {
6726 case CDCLK_FREQ_450_432:
6727 return 450000;
6728 case CDCLK_FREQ_337_308:
6729 return 337500;
6730 case CDCLK_FREQ_675_617:
6731 return 675000;
6732 default:
6733 WARN(1, "Unknown cd freq selection\n");
6734 }
6735 }
6736
6737 /* error case, do as if DPLL0 isn't enabled */
6738 return 24000;
6739}
6740
6741static int broadwell_get_display_clock_speed(struct drm_device *dev)
6742{
6743 struct drm_i915_private *dev_priv = dev->dev_private;
6744 uint32_t lcpll = I915_READ(LCPLL_CTL);
6745 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6746
6747 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6748 return 800000;
6749 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6750 return 450000;
6751 else if (freq == LCPLL_CLK_FREQ_450)
6752 return 450000;
6753 else if (freq == LCPLL_CLK_FREQ_54O_BDW)
6754 return 540000;
6755 else if (freq == LCPLL_CLK_FREQ_337_5_BDW)
6756 return 337500;
6757 else
6758 return 675000;
6759}
6760
6761static int haswell_get_display_clock_speed(struct drm_device *dev)
6762{
6763 struct drm_i915_private *dev_priv = dev->dev_private;
6764 uint32_t lcpll = I915_READ(LCPLL_CTL);
6765 uint32_t freq = lcpll & LCPLL_CLK_FREQ_MASK;
6766
6767 if (lcpll & LCPLL_CD_SOURCE_FCLK)
6768 return 800000;
6769 else if (I915_READ(FUSE_STRAP) & HSW_CDCLK_LIMIT)
6770 return 450000;
6771 else if (freq == LCPLL_CLK_FREQ_450)
6772 return 450000;
6773 else if (IS_HSW_ULT(dev))
6774 return 337500;
6775 else
6776 return 540000;
Jesse Barnes79e53942008-11-07 14:24:08 -08006777}
6778
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006779static int valleyview_get_display_clock_speed(struct drm_device *dev)
6780{
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006781 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006782 u32 val;
6783 int divider;
6784
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006785 if (dev_priv->hpll_freq == 0)
6786 dev_priv->hpll_freq = valleyview_get_vco(dev_priv);
6787
Ville Syrjäläa5805162015-05-26 20:42:30 +03006788 mutex_lock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006789 val = vlv_cck_read(dev_priv, CCK_DISPLAY_CLOCK_CONTROL);
Ville Syrjäläa5805162015-05-26 20:42:30 +03006790 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjäläd197b7d2014-06-13 13:37:49 +03006791
6792 divider = val & DISPLAY_FREQUENCY_VALUES;
6793
Ville Syrjälä7d007f42014-06-13 13:37:53 +03006794 WARN((val & DISPLAY_FREQUENCY_STATUS) !=
6795 (divider << DISPLAY_FREQUENCY_STATUS_SHIFT),
6796 "cdclk change in progress\n");
6797
Ville Syrjälä6bcda4f2014-10-07 17:41:22 +03006798 return DIV_ROUND_CLOSEST(dev_priv->hpll_freq << 1, divider + 1);
Jesse Barnes25eb05fc2012-03-28 13:39:23 -07006799}
6800
Ville Syrjäläb37a6432015-03-31 14:11:54 +03006801static int ilk_get_display_clock_speed(struct drm_device *dev)
6802{
6803 return 450000;
6804}
6805
Jesse Barnese70236a2009-09-21 10:42:27 -07006806static int i945_get_display_clock_speed(struct drm_device *dev)
Jesse Barnes79e53942008-11-07 14:24:08 -08006807{
Jesse Barnese70236a2009-09-21 10:42:27 -07006808 return 400000;
6809}
Jesse Barnes79e53942008-11-07 14:24:08 -08006810
Jesse Barnese70236a2009-09-21 10:42:27 -07006811static int i915_get_display_clock_speed(struct drm_device *dev)
6812{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006813 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006814}
Jesse Barnes79e53942008-11-07 14:24:08 -08006815
Jesse Barnese70236a2009-09-21 10:42:27 -07006816static int i9xx_misc_get_display_clock_speed(struct drm_device *dev)
6817{
6818 return 200000;
6819}
Jesse Barnes79e53942008-11-07 14:24:08 -08006820
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006821static int pnv_get_display_clock_speed(struct drm_device *dev)
6822{
6823 u16 gcfgc = 0;
6824
6825 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6826
6827 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6828 case GC_DISPLAY_CLOCK_267_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006829 return 266667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006830 case GC_DISPLAY_CLOCK_333_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006831 return 333333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006832 case GC_DISPLAY_CLOCK_444_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006833 return 444444;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006834 case GC_DISPLAY_CLOCK_200_MHZ_PNV:
6835 return 200000;
6836 default:
6837 DRM_ERROR("Unknown pnv display core clock 0x%04x\n", gcfgc);
6838 case GC_DISPLAY_CLOCK_133_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006839 return 133333;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006840 case GC_DISPLAY_CLOCK_167_MHZ_PNV:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006841 return 166667;
Daniel Vetter257a7ff2013-07-26 08:35:42 +02006842 }
6843}
6844
Jesse Barnese70236a2009-09-21 10:42:27 -07006845static int i915gm_get_display_clock_speed(struct drm_device *dev)
6846{
6847 u16 gcfgc = 0;
6848
6849 pci_read_config_word(dev->pdev, GCFGC, &gcfgc);
6850
6851 if (gcfgc & GC_LOW_FREQUENCY_ENABLE)
Ville Syrjäläe907f172015-03-31 14:09:47 +03006852 return 133333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006853 else {
6854 switch (gcfgc & GC_DISPLAY_CLOCK_MASK) {
6855 case GC_DISPLAY_CLOCK_333_MHZ:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006856 return 333333;
Jesse Barnese70236a2009-09-21 10:42:27 -07006857 default:
6858 case GC_DISPLAY_CLOCK_190_200_MHZ:
6859 return 190000;
6860 }
6861 }
6862}
Jesse Barnes79e53942008-11-07 14:24:08 -08006863
Jesse Barnese70236a2009-09-21 10:42:27 -07006864static int i865_get_display_clock_speed(struct drm_device *dev)
6865{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006866 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006867}
6868
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006869static int i85x_get_display_clock_speed(struct drm_device *dev)
Jesse Barnese70236a2009-09-21 10:42:27 -07006870{
6871 u16 hpllcc = 0;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006872
Ville Syrjälä65cd2b32015-05-22 11:22:32 +03006873 /*
6874 * 852GM/852GMV only supports 133 MHz and the HPLLCC
6875 * encoding is different :(
6876 * FIXME is this the right way to detect 852GM/852GMV?
6877 */
6878 if (dev->pdev->revision == 0x1)
6879 return 133333;
6880
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006881 pci_bus_read_config_word(dev->pdev->bus,
6882 PCI_DEVFN(0, 3), HPLLCC, &hpllcc);
6883
Jesse Barnese70236a2009-09-21 10:42:27 -07006884 /* Assume that the hardware is in the high speed state. This
6885 * should be the default.
6886 */
6887 switch (hpllcc & GC_CLOCK_CONTROL_MASK) {
6888 case GC_CLOCK_133_200:
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006889 case GC_CLOCK_133_200_2:
Jesse Barnese70236a2009-09-21 10:42:27 -07006890 case GC_CLOCK_100_200:
6891 return 200000;
6892 case GC_CLOCK_166_250:
6893 return 250000;
6894 case GC_CLOCK_100_133:
Ville Syrjäläe907f172015-03-31 14:09:47 +03006895 return 133333;
Ville Syrjälä1b1d2712015-05-22 11:22:31 +03006896 case GC_CLOCK_133_266:
6897 case GC_CLOCK_133_266_2:
6898 case GC_CLOCK_166_266:
6899 return 266667;
Jesse Barnese70236a2009-09-21 10:42:27 -07006900 }
6901
6902 /* Shouldn't happen */
6903 return 0;
6904}
6905
6906static int i830_get_display_clock_speed(struct drm_device *dev)
6907{
Ville Syrjäläe907f172015-03-31 14:09:47 +03006908 return 133333;
Jesse Barnes79e53942008-11-07 14:24:08 -08006909}
6910
Ville Syrjälä34edce22015-05-22 11:22:33 +03006911static unsigned int intel_hpll_vco(struct drm_device *dev)
6912{
6913 struct drm_i915_private *dev_priv = dev->dev_private;
6914 static const unsigned int blb_vco[8] = {
6915 [0] = 3200000,
6916 [1] = 4000000,
6917 [2] = 5333333,
6918 [3] = 4800000,
6919 [4] = 6400000,
6920 };
6921 static const unsigned int pnv_vco[8] = {
6922 [0] = 3200000,
6923 [1] = 4000000,
6924 [2] = 5333333,
6925 [3] = 4800000,
6926 [4] = 2666667,
6927 };
6928 static const unsigned int cl_vco[8] = {
6929 [0] = 3200000,
6930 [1] = 4000000,
6931 [2] = 5333333,
6932 [3] = 6400000,
6933 [4] = 3333333,
6934 [5] = 3566667,
6935 [6] = 4266667,
6936 };
6937 static const unsigned int elk_vco[8] = {
6938 [0] = 3200000,
6939 [1] = 4000000,
6940 [2] = 5333333,
6941 [3] = 4800000,
6942 };
6943 static const unsigned int ctg_vco[8] = {
6944 [0] = 3200000,
6945 [1] = 4000000,
6946 [2] = 5333333,
6947 [3] = 6400000,
6948 [4] = 2666667,
6949 [5] = 4266667,
6950 };
6951 const unsigned int *vco_table;
6952 unsigned int vco;
6953 uint8_t tmp = 0;
6954
6955 /* FIXME other chipsets? */
6956 if (IS_GM45(dev))
6957 vco_table = ctg_vco;
6958 else if (IS_G4X(dev))
6959 vco_table = elk_vco;
6960 else if (IS_CRESTLINE(dev))
6961 vco_table = cl_vco;
6962 else if (IS_PINEVIEW(dev))
6963 vco_table = pnv_vco;
6964 else if (IS_G33(dev))
6965 vco_table = blb_vco;
6966 else
6967 return 0;
6968
6969 tmp = I915_READ(IS_MOBILE(dev) ? HPLLVCO_MOBILE : HPLLVCO);
6970
6971 vco = vco_table[tmp & 0x7];
6972 if (vco == 0)
6973 DRM_ERROR("Bad HPLL VCO (HPLLVCO=0x%02x)\n", tmp);
6974 else
6975 DRM_DEBUG_KMS("HPLL VCO %u kHz\n", vco);
6976
6977 return vco;
6978}
6979
6980static int gm45_get_display_clock_speed(struct drm_device *dev)
6981{
6982 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
6983 uint16_t tmp = 0;
6984
6985 pci_read_config_word(dev->pdev, GCFGC, &tmp);
6986
6987 cdclk_sel = (tmp >> 12) & 0x1;
6988
6989 switch (vco) {
6990 case 2666667:
6991 case 4000000:
6992 case 5333333:
6993 return cdclk_sel ? 333333 : 222222;
6994 case 3200000:
6995 return cdclk_sel ? 320000 : 228571;
6996 default:
6997 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u, CFGC=0x%04x\n", vco, tmp);
6998 return 222222;
6999 }
7000}
7001
7002static int i965gm_get_display_clock_speed(struct drm_device *dev)
7003{
7004 static const uint8_t div_3200[] = { 16, 10, 8 };
7005 static const uint8_t div_4000[] = { 20, 12, 10 };
7006 static const uint8_t div_5333[] = { 24, 16, 14 };
7007 const uint8_t *div_table;
7008 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7009 uint16_t tmp = 0;
7010
7011 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7012
7013 cdclk_sel = ((tmp >> 8) & 0x1f) - 1;
7014
7015 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7016 goto fail;
7017
7018 switch (vco) {
7019 case 3200000:
7020 div_table = div_3200;
7021 break;
7022 case 4000000:
7023 div_table = div_4000;
7024 break;
7025 case 5333333:
7026 div_table = div_5333;
7027 break;
7028 default:
7029 goto fail;
7030 }
7031
7032 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7033
7034 fail:
7035 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%04x\n", vco, tmp);
7036 return 200000;
7037}
7038
7039static int g33_get_display_clock_speed(struct drm_device *dev)
7040{
7041 static const uint8_t div_3200[] = { 12, 10, 8, 7, 5, 16 };
7042 static const uint8_t div_4000[] = { 14, 12, 10, 8, 6, 20 };
7043 static const uint8_t div_4800[] = { 20, 14, 12, 10, 8, 24 };
7044 static const uint8_t div_5333[] = { 20, 16, 12, 12, 8, 28 };
7045 const uint8_t *div_table;
7046 unsigned int cdclk_sel, vco = intel_hpll_vco(dev);
7047 uint16_t tmp = 0;
7048
7049 pci_read_config_word(dev->pdev, GCFGC, &tmp);
7050
7051 cdclk_sel = (tmp >> 4) & 0x7;
7052
7053 if (cdclk_sel >= ARRAY_SIZE(div_3200))
7054 goto fail;
7055
7056 switch (vco) {
7057 case 3200000:
7058 div_table = div_3200;
7059 break;
7060 case 4000000:
7061 div_table = div_4000;
7062 break;
7063 case 4800000:
7064 div_table = div_4800;
7065 break;
7066 case 5333333:
7067 div_table = div_5333;
7068 break;
7069 default:
7070 goto fail;
7071 }
7072
7073 return DIV_ROUND_CLOSEST(vco, div_table[cdclk_sel]);
7074
7075 fail:
7076 DRM_ERROR("Unable to determine CDCLK. HPLL VCO=%u kHz, CFGC=0x%08x\n", vco, tmp);
7077 return 190476;
7078}
7079
Zhenyu Wang2c072452009-06-05 15:38:42 +08007080static void
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007081intel_reduce_m_n_ratio(uint32_t *num, uint32_t *den)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007082{
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007083 while (*num > DATA_LINK_M_N_MASK ||
7084 *den > DATA_LINK_M_N_MASK) {
Zhenyu Wang2c072452009-06-05 15:38:42 +08007085 *num >>= 1;
7086 *den >>= 1;
7087 }
7088}
7089
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007090static void compute_m_n(unsigned int m, unsigned int n,
7091 uint32_t *ret_m, uint32_t *ret_n)
7092{
7093 *ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
7094 *ret_m = div_u64((uint64_t) m * *ret_n, n);
7095 intel_reduce_m_n_ratio(ret_m, ret_n);
7096}
7097
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007098void
7099intel_link_compute_m_n(int bits_per_pixel, int nlanes,
7100 int pixel_clock, int link_clock,
7101 struct intel_link_m_n *m_n)
Zhenyu Wang2c072452009-06-05 15:38:42 +08007102{
Daniel Vettere69d0bc2012-11-29 15:59:36 +01007103 m_n->tu = 64;
Ville Syrjäläa65851a2013-04-23 15:03:34 +03007104
7105 compute_m_n(bits_per_pixel * pixel_clock,
7106 link_clock * nlanes * 8,
7107 &m_n->gmch_m, &m_n->gmch_n);
7108
7109 compute_m_n(pixel_clock, link_clock,
7110 &m_n->link_m, &m_n->link_n);
Zhenyu Wang2c072452009-06-05 15:38:42 +08007111}
7112
Chris Wilsona7615032011-01-12 17:04:08 +00007113static inline bool intel_panel_use_ssc(struct drm_i915_private *dev_priv)
7114{
Jani Nikulad330a952014-01-21 11:24:25 +02007115 if (i915.panel_use_ssc >= 0)
7116 return i915.panel_use_ssc != 0;
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03007117 return dev_priv->vbt.lvds_use_ssc
Keith Packard435793d2011-07-12 14:56:22 -07007118 && !(dev_priv->quirks & QUIRK_LVDS_SSC_DISABLE);
Chris Wilsona7615032011-01-12 17:04:08 +00007119}
7120
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007121static int i9xx_get_refclk(const struct intel_crtc_state *crtc_state,
7122 int num_connectors)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007123{
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007124 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007125 struct drm_i915_private *dev_priv = dev->dev_private;
7126 int refclk;
7127
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007128 WARN_ON(!crtc_state->base.state);
7129
Imre Deak5ab7b0b2015-03-06 03:29:25 +02007130 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev)) {
Daniel Vetter9a0ea492013-09-16 11:29:34 +02007131 refclk = 100000;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007132 } else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007133 intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02007134 refclk = dev_priv->vbt.lvds_ssc_freq;
7135 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n", refclk);
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007136 } else if (!IS_GEN2(dev)) {
7137 refclk = 96000;
7138 } else {
7139 refclk = 48000;
7140 }
7141
7142 return refclk;
7143}
7144
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007145static uint32_t pnv_dpll_compute_fp(struct dpll *dpll)
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007146{
Daniel Vetter7df00d72013-05-21 21:54:55 +02007147 return (1 << dpll->n) << 16 | dpll->m2;
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007148}
Daniel Vetterf47709a2013-03-28 10:42:02 +01007149
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007150static uint32_t i9xx_dpll_compute_fp(struct dpll *dpll)
7151{
7152 return dpll->n << 16 | dpll->m1 << 8 | dpll->m2;
Jesse Barnesc65d77d2011-12-15 12:30:36 -08007153}
7154
Daniel Vetterf47709a2013-03-28 10:42:02 +01007155static void i9xx_update_pll_dividers(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007156 struct intel_crtc_state *crtc_state,
Jesse Barnesa7516a02011-12-15 12:30:37 -08007157 intel_clock_t *reduced_clock)
7158{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007159 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007160 u32 fp, fp2 = 0;
7161
7162 if (IS_PINEVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007163 fp = pnv_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007164 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007165 fp2 = pnv_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007166 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007167 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007168 if (reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02007169 fp2 = i9xx_dpll_compute_fp(reduced_clock);
Jesse Barnesa7516a02011-12-15 12:30:37 -08007170 }
7171
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007172 crtc_state->dpll_hw_state.fp0 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007173
Daniel Vetterf47709a2013-03-28 10:42:02 +01007174 crtc->lowfreq_avail = false;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007175 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Rodrigo Viviab585de2015-03-24 12:40:09 -07007176 reduced_clock) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007177 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007178 crtc->lowfreq_avail = true;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007179 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007180 crtc_state->dpll_hw_state.fp1 = fp;
Jesse Barnesa7516a02011-12-15 12:30:37 -08007181 }
7182}
7183
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007184static void vlv_pllb_recal_opamp(struct drm_i915_private *dev_priv, enum pipe
7185 pipe)
Jesse Barnes89b667f2013-04-18 14:51:36 -07007186{
7187 u32 reg_val;
7188
7189 /*
7190 * PLLB opamp always calibrates to max value of 0x3f, force enable it
7191 * and set it to a reasonable value instead.
7192 */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007193 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007194 reg_val &= 0xffffff00;
7195 reg_val |= 0x00000030;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007196 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007197
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007198 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007199 reg_val &= 0x8cffffff;
7200 reg_val = 0x8c000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007201 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007202
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007203 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW9(1));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007204 reg_val &= 0xffffff00;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007205 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9(1), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007206
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007207 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_REF_DW13);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007208 reg_val &= 0x00ffffff;
7209 reg_val |= 0xb0000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007210 vlv_dpio_write(dev_priv, pipe, VLV_REF_DW13, reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007211}
7212
Daniel Vetterb5518422013-05-03 11:49:48 +02007213static void intel_pch_transcoder_set_m_n(struct intel_crtc *crtc,
7214 struct intel_link_m_n *m_n)
7215{
7216 struct drm_device *dev = crtc->base.dev;
7217 struct drm_i915_private *dev_priv = dev->dev_private;
7218 int pipe = crtc->pipe;
7219
Daniel Vettere3b95f12013-05-03 11:49:49 +02007220 I915_WRITE(PCH_TRANS_DATA_M1(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7221 I915_WRITE(PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
7222 I915_WRITE(PCH_TRANS_LINK_M1(pipe), m_n->link_m);
7223 I915_WRITE(PCH_TRANS_LINK_N1(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007224}
7225
7226static void intel_cpu_transcoder_set_m_n(struct intel_crtc *crtc,
Vandana Kannanf769cd22014-08-05 07:51:22 -07007227 struct intel_link_m_n *m_n,
7228 struct intel_link_m_n *m2_n2)
Daniel Vetterb5518422013-05-03 11:49:48 +02007229{
7230 struct drm_device *dev = crtc->base.dev;
7231 struct drm_i915_private *dev_priv = dev->dev_private;
7232 int pipe = crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007233 enum transcoder transcoder = crtc->config->cpu_transcoder;
Daniel Vetterb5518422013-05-03 11:49:48 +02007234
7235 if (INTEL_INFO(dev)->gen >= 5) {
7236 I915_WRITE(PIPE_DATA_M1(transcoder), TU_SIZE(m_n->tu) | m_n->gmch_m);
7237 I915_WRITE(PIPE_DATA_N1(transcoder), m_n->gmch_n);
7238 I915_WRITE(PIPE_LINK_M1(transcoder), m_n->link_m);
7239 I915_WRITE(PIPE_LINK_N1(transcoder), m_n->link_n);
Vandana Kannanf769cd22014-08-05 07:51:22 -07007240 /* M2_N2 registers to be set only for gen < 8 (M2_N2 available
7241 * for gen < 8) and if DRRS is supported (to make sure the
7242 * registers are not unnecessarily accessed).
7243 */
Durgadoss R44395bf2015-02-13 15:33:02 +05307244 if (m2_n2 && (IS_CHERRYVIEW(dev) || INTEL_INFO(dev)->gen < 8) &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007245 crtc->config->has_drrs) {
Vandana Kannanf769cd22014-08-05 07:51:22 -07007246 I915_WRITE(PIPE_DATA_M2(transcoder),
7247 TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
7248 I915_WRITE(PIPE_DATA_N2(transcoder), m2_n2->gmch_n);
7249 I915_WRITE(PIPE_LINK_M2(transcoder), m2_n2->link_m);
7250 I915_WRITE(PIPE_LINK_N2(transcoder), m2_n2->link_n);
7251 }
Daniel Vetterb5518422013-05-03 11:49:48 +02007252 } else {
Daniel Vettere3b95f12013-05-03 11:49:49 +02007253 I915_WRITE(PIPE_DATA_M_G4X(pipe), TU_SIZE(m_n->tu) | m_n->gmch_m);
7254 I915_WRITE(PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
7255 I915_WRITE(PIPE_LINK_M_G4X(pipe), m_n->link_m);
7256 I915_WRITE(PIPE_LINK_N_G4X(pipe), m_n->link_n);
Daniel Vetterb5518422013-05-03 11:49:48 +02007257 }
7258}
7259
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307260void intel_dp_set_m_n(struct intel_crtc *crtc, enum link_m_n_set m_n)
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007261{
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307262 struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
7263
7264 if (m_n == M1_N1) {
7265 dp_m_n = &crtc->config->dp_m_n;
7266 dp_m2_n2 = &crtc->config->dp_m2_n2;
7267 } else if (m_n == M2_N2) {
7268
7269 /*
7270 * M2_N2 registers are not supported. Hence m2_n2 divider value
7271 * needs to be programmed into M1_N1.
7272 */
7273 dp_m_n = &crtc->config->dp_m2_n2;
7274 } else {
7275 DRM_ERROR("Unsupported divider value\n");
7276 return;
7277 }
7278
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007279 if (crtc->config->has_pch_encoder)
7280 intel_pch_transcoder_set_m_n(crtc, &crtc->config->dp_m_n);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007281 else
Ramalingam Cfe3cd482015-02-13 15:32:59 +05307282 intel_cpu_transcoder_set_m_n(crtc, dp_m_n, dp_m2_n2);
Daniel Vetter03afc4a2013-04-02 23:42:31 +02007283}
7284
Ville Syrjäläd288f652014-10-28 13:20:22 +02007285static void vlv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007286 struct intel_crtc_state *pipe_config)
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007287{
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007288 u32 dpll, dpll_md;
7289
7290 /*
7291 * Enable DPIO clock input. We should never disable the reference
7292 * clock for pipe B, since VGA hotplug / manual detection depends
7293 * on it.
7294 */
7295 dpll = DPLL_EXT_BUFFER_ENABLE_VLV | DPLL_REFA_CLK_ENABLE_VLV |
7296 DPLL_VGA_MODE_DIS | DPLL_INTEGRATED_CLOCK_VLV;
7297 /* We should never disable this, set it here for state tracking */
7298 if (crtc->pipe == PIPE_B)
7299 dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
7300 dpll |= DPLL_VCO_ENABLE;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007301 pipe_config->dpll_hw_state.dpll = dpll;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007302
Ville Syrjäläd288f652014-10-28 13:20:22 +02007303 dpll_md = (pipe_config->pixel_multiplier - 1)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007304 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjäläd288f652014-10-28 13:20:22 +02007305 pipe_config->dpll_hw_state.dpll_md = dpll_md;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007306}
7307
Ville Syrjäläd288f652014-10-28 13:20:22 +02007308static void vlv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007309 const struct intel_crtc_state *pipe_config)
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007310{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007311 struct drm_device *dev = crtc->base.dev;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007312 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007313 int pipe = crtc->pipe;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007314 u32 mdiv;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007315 u32 bestn, bestm1, bestm2, bestp1, bestp2;
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007316 u32 coreclk, reg_val;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007317
Ville Syrjäläa5805162015-05-26 20:42:30 +03007318 mutex_lock(&dev_priv->sb_lock);
Daniel Vetter09153002012-12-12 14:06:44 +01007319
Ville Syrjäläd288f652014-10-28 13:20:22 +02007320 bestn = pipe_config->dpll.n;
7321 bestm1 = pipe_config->dpll.m1;
7322 bestm2 = pipe_config->dpll.m2;
7323 bestp1 = pipe_config->dpll.p1;
7324 bestp2 = pipe_config->dpll.p2;
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007325
Jesse Barnes89b667f2013-04-18 14:51:36 -07007326 /* See eDP HDMI DPIO driver vbios notes doc */
7327
7328 /* PLL B needs special handling */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007329 if (pipe == PIPE_B)
Chon Ming Lee5e69f972013-09-05 20:41:49 +08007330 vlv_pllb_recal_opamp(dev_priv, pipe);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007331
7332 /* Set up Tx target for periodic Rcomp update */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007333 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW9_BCAST, 0x0100000f);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007334
7335 /* Disable target IRef on PLL */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007336 reg_val = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW8(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007337 reg_val &= 0x00ffffff;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007338 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW8(pipe), reg_val);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007339
7340 /* Disable fast lock */
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007341 vlv_dpio_write(dev_priv, pipe, VLV_CMN_DW0, 0x610);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007342
7343 /* Set idtafcrecal before PLL is enabled */
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007344 mdiv = ((bestm1 << DPIO_M1DIV_SHIFT) | (bestm2 & DPIO_M2DIV_MASK));
7345 mdiv |= ((bestp1 << DPIO_P1_SHIFT) | (bestp2 << DPIO_P2_SHIFT));
7346 mdiv |= ((bestn << DPIO_N_SHIFT));
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007347 mdiv |= (1 << DPIO_K_SHIFT);
Jesse Barnes7df50802013-05-02 10:48:09 -07007348
7349 /*
7350 * Post divider depends on pixel clock rate, DAC vs digital (and LVDS,
7351 * but we don't support that).
7352 * Note: don't use the DAC post divider as it seems unstable.
7353 */
7354 mdiv |= (DPIO_POST_DIV_HDMIDP << DPIO_POST_DIV_SHIFT);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007355 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007356
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007357 mdiv |= DPIO_ENABLE_CALIBRATION;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007358 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW3(pipe), mdiv);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007359
Jesse Barnes89b667f2013-04-18 14:51:36 -07007360 /* Set HBR and RBR LPF coefficients */
Ville Syrjäläd288f652014-10-28 13:20:22 +02007361 if (pipe_config->port_clock == 162000 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007362 intel_pipe_has_type(crtc, INTEL_OUTPUT_ANALOG) ||
7363 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI))
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007364 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Ville Syrjälä885b01202013-07-05 19:21:38 +03007365 0x009f0003);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007366 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007367 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW10(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007368 0x00d0000f);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007369
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02007370 if (pipe_config->has_dp_encoder) {
Jesse Barnes89b667f2013-04-18 14:51:36 -07007371 /* Use SSC source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007372 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007373 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007374 0x0df40000);
7375 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007376 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007377 0x0df70000);
7378 } else { /* HDMI or VGA */
7379 /* Use bend source */
Daniel Vetterbdd4b6a2014-04-24 23:55:11 +02007380 if (pipe == PIPE_A)
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007381 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007382 0x0df70000);
7383 else
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007384 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW5(pipe),
Jesse Barnes89b667f2013-04-18 14:51:36 -07007385 0x0df40000);
7386 }
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007387
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007388 coreclk = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW7(pipe));
Jesse Barnes89b667f2013-04-18 14:51:36 -07007389 coreclk = (coreclk & 0x0000ff00) | 0x01c00000;
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007390 if (intel_pipe_has_type(crtc, INTEL_OUTPUT_DISPLAYPORT) ||
7391 intel_pipe_has_type(crtc, INTEL_OUTPUT_EDP))
Jesse Barnes89b667f2013-04-18 14:51:36 -07007392 coreclk |= 0x01000000;
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007393 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW7(pipe), coreclk);
Jesse Barnes89b667f2013-04-18 14:51:36 -07007394
Chon Ming Leeab3c7592013-11-07 10:43:30 +08007395 vlv_dpio_write(dev_priv, pipe, VLV_PLL_DW11(pipe), 0x87871000);
Ville Syrjäläa5805162015-05-26 20:42:30 +03007396 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesa0c4da242012-06-15 11:55:13 -07007397}
7398
Ville Syrjäläd288f652014-10-28 13:20:22 +02007399static void chv_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007400 struct intel_crtc_state *pipe_config)
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007401{
Ville Syrjäläd288f652014-10-28 13:20:22 +02007402 pipe_config->dpll_hw_state.dpll = DPLL_SSC_REF_CLOCK_CHV |
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007403 DPLL_REFA_CLK_ENABLE_VLV | DPLL_VGA_MODE_DIS |
7404 DPLL_VCO_ENABLE;
7405 if (crtc->pipe != PIPE_A)
Ville Syrjäläd288f652014-10-28 13:20:22 +02007406 pipe_config->dpll_hw_state.dpll |= DPLL_INTEGRATED_CRI_CLK_VLV;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007407
Ville Syrjäläd288f652014-10-28 13:20:22 +02007408 pipe_config->dpll_hw_state.dpll_md =
7409 (pipe_config->pixel_multiplier - 1) << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007410}
7411
Ville Syrjäläd288f652014-10-28 13:20:22 +02007412static void chv_prepare_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007413 const struct intel_crtc_state *pipe_config)
Ville Syrjälä1ae0d132014-06-28 02:04:00 +03007414{
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007415 struct drm_device *dev = crtc->base.dev;
7416 struct drm_i915_private *dev_priv = dev->dev_private;
7417 int pipe = crtc->pipe;
7418 int dpll_reg = DPLL(crtc->pipe);
7419 enum dpio_channel port = vlv_pipe_to_channel(pipe);
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307420 u32 loopfilter, tribuf_calcntr;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007421 u32 bestn, bestm1, bestm2, bestp1, bestp2, bestm2_frac;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307422 u32 dpio_val;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307423 int vco;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007424
Ville Syrjäläd288f652014-10-28 13:20:22 +02007425 bestn = pipe_config->dpll.n;
7426 bestm2_frac = pipe_config->dpll.m2 & 0x3fffff;
7427 bestm1 = pipe_config->dpll.m1;
7428 bestm2 = pipe_config->dpll.m2 >> 22;
7429 bestp1 = pipe_config->dpll.p1;
7430 bestp2 = pipe_config->dpll.p2;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307431 vco = pipe_config->dpll.vco;
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307432 dpio_val = 0;
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307433 loopfilter = 0;
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007434
7435 /*
7436 * Enable Refclk and SSC
7437 */
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007438 I915_WRITE(dpll_reg,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007439 pipe_config->dpll_hw_state.dpll & ~DPLL_VCO_ENABLE);
Ville Syrjäläa11b0702014-04-09 13:28:57 +03007440
Ville Syrjäläa5805162015-05-26 20:42:30 +03007441 mutex_lock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007442
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007443 /* p1 and p2 divider */
7444 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW13(port),
7445 5 << DPIO_CHV_S1_DIV_SHIFT |
7446 bestp1 << DPIO_CHV_P1_DIV_SHIFT |
7447 bestp2 << DPIO_CHV_P2_DIV_SHIFT |
7448 1 << DPIO_CHV_K_DIV_SHIFT);
7449
7450 /* Feedback post-divider - m2 */
7451 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW0(port), bestm2);
7452
7453 /* Feedback refclk divider - n and m1 */
7454 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW1(port),
7455 DPIO_CHV_M1_DIV_BY_2 |
7456 1 << DPIO_CHV_N_DIV_SHIFT);
7457
7458 /* M2 fraction division */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307459 if (bestm2_frac)
7460 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW2(port), bestm2_frac);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007461
7462 /* M2 fraction division enable */
Vijay Purushothamana945ce7e2015-03-05 19:30:57 +05307463 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
7464 dpio_val &= ~(DPIO_CHV_FEEDFWD_GAIN_MASK | DPIO_CHV_FRAC_DIV_EN);
7465 dpio_val |= (2 << DPIO_CHV_FEEDFWD_GAIN_SHIFT);
7466 if (bestm2_frac)
7467 dpio_val |= DPIO_CHV_FRAC_DIV_EN;
7468 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW3(port), dpio_val);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007469
Vijay Purushothamande3a0fd2015-03-05 19:32:06 +05307470 /* Program digital lock detect threshold */
7471 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW9(port));
7472 dpio_val &= ~(DPIO_CHV_INT_LOCK_THRESHOLD_MASK |
7473 DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE);
7474 dpio_val |= (0x5 << DPIO_CHV_INT_LOCK_THRESHOLD_SHIFT);
7475 if (!bestm2_frac)
7476 dpio_val |= DPIO_CHV_INT_LOCK_THRESHOLD_SEL_COARSE;
7477 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW9(port), dpio_val);
7478
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007479 /* Loop filter */
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307480 if (vco == 5400000) {
7481 loopfilter |= (0x3 << DPIO_CHV_PROP_COEFF_SHIFT);
7482 loopfilter |= (0x8 << DPIO_CHV_INT_COEFF_SHIFT);
7483 loopfilter |= (0x1 << DPIO_CHV_GAIN_CTRL_SHIFT);
7484 tribuf_calcntr = 0x9;
7485 } else if (vco <= 6200000) {
7486 loopfilter |= (0x5 << DPIO_CHV_PROP_COEFF_SHIFT);
7487 loopfilter |= (0xB << DPIO_CHV_INT_COEFF_SHIFT);
7488 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7489 tribuf_calcntr = 0x9;
7490 } else if (vco <= 6480000) {
7491 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7492 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7493 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7494 tribuf_calcntr = 0x8;
7495 } else {
7496 /* Not supported. Apply the same limits as in the max case */
7497 loopfilter |= (0x4 << DPIO_CHV_PROP_COEFF_SHIFT);
7498 loopfilter |= (0x9 << DPIO_CHV_INT_COEFF_SHIFT);
7499 loopfilter |= (0x3 << DPIO_CHV_GAIN_CTRL_SHIFT);
7500 tribuf_calcntr = 0;
7501 }
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007502 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW6(port), loopfilter);
7503
Ville Syrjälä968040b2015-03-11 22:52:08 +02007504 dpio_val = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW8(port));
Vijay Purushothaman9cbe40c2015-03-05 19:33:08 +05307505 dpio_val &= ~DPIO_CHV_TDC_TARGET_CNT_MASK;
7506 dpio_val |= (tribuf_calcntr << DPIO_CHV_TDC_TARGET_CNT_SHIFT);
7507 vlv_dpio_write(dev_priv, pipe, CHV_PLL_DW8(port), dpio_val);
7508
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007509 /* AFC Recal */
7510 vlv_dpio_write(dev_priv, pipe, CHV_CMN_DW14(port),
7511 vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW14(port)) |
7512 DPIO_AFC_RECAL);
7513
Ville Syrjäläa5805162015-05-26 20:42:30 +03007514 mutex_unlock(&dev_priv->sb_lock);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007515}
7516
Ville Syrjäläd288f652014-10-28 13:20:22 +02007517/**
7518 * vlv_force_pll_on - forcibly enable just the PLL
7519 * @dev_priv: i915 private structure
7520 * @pipe: pipe PLL to enable
7521 * @dpll: PLL configuration
7522 *
7523 * Enable the PLL for @pipe using the supplied @dpll config. To be used
7524 * in cases where we need the PLL enabled even when @pipe is not going to
7525 * be enabled.
7526 */
7527void vlv_force_pll_on(struct drm_device *dev, enum pipe pipe,
7528 const struct dpll *dpll)
7529{
7530 struct intel_crtc *crtc =
7531 to_intel_crtc(intel_get_crtc_for_pipe(dev, pipe));
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007532 struct intel_crtc_state pipe_config = {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007533 .base.crtc = &crtc->base,
Ville Syrjäläd288f652014-10-28 13:20:22 +02007534 .pixel_multiplier = 1,
7535 .dpll = *dpll,
7536 };
7537
7538 if (IS_CHERRYVIEW(dev)) {
7539 chv_update_pll(crtc, &pipe_config);
7540 chv_prepare_pll(crtc, &pipe_config);
7541 chv_enable_pll(crtc, &pipe_config);
7542 } else {
7543 vlv_update_pll(crtc, &pipe_config);
7544 vlv_prepare_pll(crtc, &pipe_config);
7545 vlv_enable_pll(crtc, &pipe_config);
7546 }
7547}
7548
7549/**
7550 * vlv_force_pll_off - forcibly disable just the PLL
7551 * @dev_priv: i915 private structure
7552 * @pipe: pipe PLL to disable
7553 *
7554 * Disable the PLL for @pipe. To be used in cases where we need
7555 * the PLL enabled even when @pipe is not going to be enabled.
7556 */
7557void vlv_force_pll_off(struct drm_device *dev, enum pipe pipe)
7558{
7559 if (IS_CHERRYVIEW(dev))
7560 chv_disable_pll(to_i915(dev), pipe);
7561 else
7562 vlv_disable_pll(to_i915(dev), pipe);
7563}
7564
Daniel Vetterf47709a2013-03-28 10:42:02 +01007565static void i9xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007566 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007567 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007568 int num_connectors)
7569{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007570 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007571 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007572 u32 dpll;
7573 bool is_sdvo;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007574 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007575
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007576 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307577
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007578 is_sdvo = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_SDVO) ||
7579 intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_HDMI);
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007580
7581 dpll = DPLL_VGA_MODE_DIS;
7582
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007583 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS))
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007584 dpll |= DPLLB_MODE_LVDS;
7585 else
7586 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter6cc5f342013-03-27 00:44:53 +01007587
Daniel Vetteref1b4602013-06-01 17:17:04 +02007588 if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007589 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetter198a037f2013-04-19 11:14:37 +02007590 << SDVO_MULTIPLIER_SHIFT_HIRES;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007591 }
Daniel Vetter198a037f2013-04-19 11:14:37 +02007592
7593 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007594 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vetter198a037f2013-04-19 11:14:37 +02007595
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007596 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02007597 dpll |= DPLL_SDVO_HIGH_SPEED;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007598
7599 /* compute bitmask from p1 value */
7600 if (IS_PINEVIEW(dev))
7601 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW;
7602 else {
7603 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7604 if (IS_G4X(dev) && reduced_clock)
7605 dpll |= (1 << (reduced_clock->p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
7606 }
7607 switch (clock->p2) {
7608 case 5:
7609 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
7610 break;
7611 case 7:
7612 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
7613 break;
7614 case 10:
7615 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
7616 break;
7617 case 14:
7618 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
7619 break;
7620 }
7621 if (INTEL_INFO(dev)->gen >= 4)
7622 dpll |= (6 << PLL_LOAD_PULSE_PHASE_SHIFT);
7623
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007624 if (crtc_state->sdvo_tv_clock)
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007625 dpll |= PLL_REF_INPUT_TVCLKINBC;
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007626 else if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007627 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7628 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7629 else
7630 dpll |= PLL_REF_INPUT_DREFCLK;
7631
7632 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007633 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02007634
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007635 if (INTEL_INFO(dev)->gen >= 4) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007636 u32 dpll_md = (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02007637 << DPLL_MD_UDI_MULTIPLIER_SHIFT;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007638 crtc_state->dpll_hw_state.dpll_md = dpll_md;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007639 }
7640}
7641
Daniel Vetterf47709a2013-03-28 10:42:02 +01007642static void i8xx_update_pll(struct intel_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007643 struct intel_crtc_state *crtc_state,
Daniel Vetterf47709a2013-03-28 10:42:02 +01007644 intel_clock_t *reduced_clock,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007645 int num_connectors)
7646{
Daniel Vetterf47709a2013-03-28 10:42:02 +01007647 struct drm_device *dev = crtc->base.dev;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007648 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007649 u32 dpll;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007650 struct dpll *clock = &crtc_state->dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007651
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007652 i9xx_update_pll_dividers(crtc, crtc_state, reduced_clock);
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307653
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007654 dpll = DPLL_VGA_MODE_DIS;
7655
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007656 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS)) {
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007657 dpll |= (1 << (clock->p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7658 } else {
7659 if (clock->p1 == 2)
7660 dpll |= PLL_P1_DIVIDE_BY_TWO;
7661 else
7662 dpll |= (clock->p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT;
7663 if (clock->p2 == 4)
7664 dpll |= PLL_P2_DIVIDE_BY_4;
7665 }
7666
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007667 if (!IS_I830(dev) && intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_DVO))
Daniel Vetter4a33e482013-07-06 12:52:05 +02007668 dpll |= DPLL_DVO_2X_MODE;
7669
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007670 if (intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS) &&
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007671 intel_panel_use_ssc(dev_priv) && num_connectors < 2)
7672 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
7673 else
7674 dpll |= PLL_REF_INPUT_DREFCLK;
7675
7676 dpll |= DPLL_VCO_ENABLE;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007677 crtc_state->dpll_hw_state.dpll = dpll;
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007678}
7679
Daniel Vetter8a654f32013-06-01 17:16:22 +02007680static void intel_set_pipe_timings(struct intel_crtc *intel_crtc)
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007681{
7682 struct drm_device *dev = intel_crtc->base.dev;
7683 struct drm_i915_private *dev_priv = dev->dev_private;
7684 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007685 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Daniel Vetter8a654f32013-06-01 17:16:22 +02007686 struct drm_display_mode *adjusted_mode =
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007687 &intel_crtc->config->base.adjusted_mode;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007688 uint32_t crtc_vtotal, crtc_vblank_end;
7689 int vsyncshift = 0;
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007690
7691 /* We need to be careful not to changed the adjusted mode, for otherwise
7692 * the hw state checker will get angry at the mismatch. */
7693 crtc_vtotal = adjusted_mode->crtc_vtotal;
7694 crtc_vblank_end = adjusted_mode->crtc_vblank_end;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007695
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007696 if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007697 /* the chip adds 2 halflines automatically */
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007698 crtc_vtotal -= 1;
7699 crtc_vblank_end -= 1;
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007700
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007701 if (intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjälä609aeac2014-03-28 23:29:30 +02007702 vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
7703 else
7704 vsyncshift = adjusted_mode->crtc_hsync_start -
7705 adjusted_mode->crtc_htotal / 2;
Ville Syrjälä1caea6e2014-03-28 23:29:32 +02007706 if (vsyncshift < 0)
7707 vsyncshift += adjusted_mode->crtc_htotal;
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007708 }
7709
7710 if (INTEL_INFO(dev)->gen > 3)
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007711 I915_WRITE(VSYNCSHIFT(cpu_transcoder), vsyncshift);
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007712
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007713 I915_WRITE(HTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007714 (adjusted_mode->crtc_hdisplay - 1) |
7715 ((adjusted_mode->crtc_htotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007716 I915_WRITE(HBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007717 (adjusted_mode->crtc_hblank_start - 1) |
7718 ((adjusted_mode->crtc_hblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007719 I915_WRITE(HSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007720 (adjusted_mode->crtc_hsync_start - 1) |
7721 ((adjusted_mode->crtc_hsync_end - 1) << 16));
7722
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007723 I915_WRITE(VTOTAL(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007724 (adjusted_mode->crtc_vdisplay - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007725 ((crtc_vtotal - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007726 I915_WRITE(VBLANK(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007727 (adjusted_mode->crtc_vblank_start - 1) |
Daniel Vetter4d8a62e2013-05-03 11:49:51 +02007728 ((crtc_vblank_end - 1) << 16));
Paulo Zanonife2b8f92012-10-23 18:30:02 -02007729 I915_WRITE(VSYNC(cpu_transcoder),
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007730 (adjusted_mode->crtc_vsync_start - 1) |
7731 ((adjusted_mode->crtc_vsync_end - 1) << 16));
7732
Paulo Zanonib5e508d2012-10-24 11:34:43 -02007733 /* Workaround: when the EDP input selection is B, the VTOTAL_B must be
7734 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
7735 * documented on the DDI_FUNC_CTL register description, EDP Input Select
7736 * bits. */
7737 if (IS_HASWELL(dev) && cpu_transcoder == TRANSCODER_EDP &&
7738 (pipe == PIPE_B || pipe == PIPE_C))
7739 I915_WRITE(VTOTAL(pipe), I915_READ(VTOTAL(cpu_transcoder)));
7740
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007741 /* pipesrc controls the size that is scaled from, which should
7742 * always be the user's requested size.
7743 */
7744 I915_WRITE(PIPESRC(pipe),
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007745 ((intel_crtc->config->pipe_src_w - 1) << 16) |
7746 (intel_crtc->config->pipe_src_h - 1));
Paulo Zanonib0e77b92012-10-01 18:10:53 -03007747}
7748
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007749static void intel_get_pipe_timings(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007750 struct intel_crtc_state *pipe_config)
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007751{
7752 struct drm_device *dev = crtc->base.dev;
7753 struct drm_i915_private *dev_priv = dev->dev_private;
7754 enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
7755 uint32_t tmp;
7756
7757 tmp = I915_READ(HTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007758 pipe_config->base.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
7759 pipe_config->base.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007760 tmp = I915_READ(HBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007761 pipe_config->base.adjusted_mode.crtc_hblank_start = (tmp & 0xffff) + 1;
7762 pipe_config->base.adjusted_mode.crtc_hblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007763 tmp = I915_READ(HSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007764 pipe_config->base.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
7765 pipe_config->base.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007766
7767 tmp = I915_READ(VTOTAL(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007768 pipe_config->base.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
7769 pipe_config->base.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007770 tmp = I915_READ(VBLANK(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007771 pipe_config->base.adjusted_mode.crtc_vblank_start = (tmp & 0xffff) + 1;
7772 pipe_config->base.adjusted_mode.crtc_vblank_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007773 tmp = I915_READ(VSYNC(cpu_transcoder));
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007774 pipe_config->base.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
7775 pipe_config->base.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007776
7777 if (I915_READ(PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007778 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
7779 pipe_config->base.adjusted_mode.crtc_vtotal += 1;
7780 pipe_config->base.adjusted_mode.crtc_vblank_end += 1;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007781 }
7782
7783 tmp = I915_READ(PIPESRC(crtc->pipe));
Ville Syrjälä37327ab2013-09-04 18:25:28 +03007784 pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
7785 pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
7786
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007787 pipe_config->base.mode.vdisplay = pipe_config->pipe_src_h;
7788 pipe_config->base.mode.hdisplay = pipe_config->pipe_src_w;
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02007789}
7790
Daniel Vetterf6a83282014-02-11 15:28:57 -08007791void intel_mode_from_pipe_config(struct drm_display_mode *mode,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007792 struct intel_crtc_state *pipe_config)
Jesse Barnesbabea612013-06-26 18:57:38 +03007793{
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007794 mode->hdisplay = pipe_config->base.adjusted_mode.crtc_hdisplay;
7795 mode->htotal = pipe_config->base.adjusted_mode.crtc_htotal;
7796 mode->hsync_start = pipe_config->base.adjusted_mode.crtc_hsync_start;
7797 mode->hsync_end = pipe_config->base.adjusted_mode.crtc_hsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007798
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007799 mode->vdisplay = pipe_config->base.adjusted_mode.crtc_vdisplay;
7800 mode->vtotal = pipe_config->base.adjusted_mode.crtc_vtotal;
7801 mode->vsync_start = pipe_config->base.adjusted_mode.crtc_vsync_start;
7802 mode->vsync_end = pipe_config->base.adjusted_mode.crtc_vsync_end;
Jesse Barnesbabea612013-06-26 18:57:38 +03007803
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007804 mode->flags = pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007805
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +02007806 mode->clock = pipe_config->base.adjusted_mode.crtc_clock;
7807 mode->flags |= pipe_config->base.adjusted_mode.flags;
Jesse Barnesbabea612013-06-26 18:57:38 +03007808}
7809
Daniel Vetter84b046f2013-02-19 18:48:54 +01007810static void i9xx_set_pipeconf(struct intel_crtc *intel_crtc)
7811{
7812 struct drm_device *dev = intel_crtc->base.dev;
7813 struct drm_i915_private *dev_priv = dev->dev_private;
7814 uint32_t pipeconf;
7815
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007816 pipeconf = 0;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007817
Ville Syrjäläb6b5d042014-08-15 01:22:07 +03007818 if ((intel_crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
7819 (intel_crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
7820 pipeconf |= I915_READ(PIPECONF(intel_crtc->pipe)) & PIPECONF_ENABLE;
Daniel Vetter67c72a12013-09-24 11:46:14 +02007821
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007822 if (intel_crtc->config->double_wide)
Ville Syrjäläcf532bb2013-09-04 18:30:02 +03007823 pipeconf |= PIPECONF_DOUBLE_WIDE;
Daniel Vetter84b046f2013-02-19 18:48:54 +01007824
Daniel Vetterff9ce462013-04-24 14:57:17 +02007825 /* only g4x and later have fancy bpc/dither controls */
7826 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007827 /* Bspec claims that we can't use dithering for 30bpp pipes. */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007828 if (intel_crtc->config->dither && intel_crtc->config->pipe_bpp != 30)
Daniel Vetterff9ce462013-04-24 14:57:17 +02007829 pipeconf |= PIPECONF_DITHER_EN |
7830 PIPECONF_DITHER_TYPE_SP;
7831
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007832 switch (intel_crtc->config->pipe_bpp) {
Daniel Vetterff9ce462013-04-24 14:57:17 +02007833 case 18:
7834 pipeconf |= PIPECONF_6BPC;
7835 break;
7836 case 24:
7837 pipeconf |= PIPECONF_8BPC;
7838 break;
7839 case 30:
7840 pipeconf |= PIPECONF_10BPC;
7841 break;
7842 default:
7843 /* Case prevented by intel_choose_pipe_bpp_dither. */
7844 BUG();
Daniel Vetter84b046f2013-02-19 18:48:54 +01007845 }
7846 }
7847
7848 if (HAS_PIPE_CXSR(dev)) {
7849 if (intel_crtc->lowfreq_avail) {
7850 DRM_DEBUG_KMS("enabling CxSR downclocking\n");
7851 pipeconf |= PIPECONF_CXSR_DOWNCLOCK;
7852 } else {
7853 DRM_DEBUG_KMS("disabling CxSR downclocking\n");
Daniel Vetter84b046f2013-02-19 18:48:54 +01007854 }
7855 }
7856
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007857 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007858 if (INTEL_INFO(dev)->gen < 4 ||
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03007859 intel_pipe_has_type(intel_crtc, INTEL_OUTPUT_SDVO))
Ville Syrjäläefc2cff2014-03-28 23:29:31 +02007860 pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
7861 else
7862 pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
7863 } else
Daniel Vetter84b046f2013-02-19 18:48:54 +01007864 pipeconf |= PIPECONF_PROGRESSIVE;
7865
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02007866 if (IS_VALLEYVIEW(dev) && intel_crtc->config->limited_color_range)
Daniel Vetter9f11a9e2013-06-13 00:54:58 +02007867 pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä9c8e09b2013-04-02 16:10:09 +03007868
Daniel Vetter84b046f2013-02-19 18:48:54 +01007869 I915_WRITE(PIPECONF(intel_crtc->pipe), pipeconf);
7870 POSTING_READ(PIPECONF(intel_crtc->pipe));
7871}
7872
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007873static int i9xx_crtc_compute_clock(struct intel_crtc *crtc,
7874 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08007875{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03007876 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08007877 struct drm_i915_private *dev_priv = dev->dev_private;
Eric Anholtc751ce42010-03-25 11:48:48 -07007878 int refclk, num_connectors = 0;
Jesse Barnes652c3932009-08-17 13:31:43 -07007879 intel_clock_t clock, reduced_clock;
Daniel Vettera16af722013-04-30 14:01:44 +02007880 bool ok, has_reduced_clock = false;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007881 bool is_lvds = false, is_dsi = false;
Chris Wilson5eddb702010-09-11 13:48:45 +01007882 struct intel_encoder *encoder;
Ma Lingd4906092009-03-18 20:13:27 +08007883 const intel_limit_t *limit;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007884 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007885 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007886 struct drm_connector_state *connector_state;
7887 int i;
Jesse Barnes79e53942008-11-07 14:24:08 -08007888
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03007889 memset(&crtc_state->dpll_hw_state, 0,
7890 sizeof(crtc_state->dpll_hw_state));
7891
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03007892 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02007893 if (connector_state->crtc != &crtc->base)
7894 continue;
7895
7896 encoder = to_intel_encoder(connector_state->best_encoder);
7897
Chris Wilson5eddb702010-09-11 13:48:45 +01007898 switch (encoder->type) {
Jesse Barnes79e53942008-11-07 14:24:08 -08007899 case INTEL_OUTPUT_LVDS:
7900 is_lvds = true;
7901 break;
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007902 case INTEL_OUTPUT_DSI:
7903 is_dsi = true;
7904 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02007905 default:
7906 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08007907 }
Kristian Høgsberg43565a02009-02-13 20:56:52 -05007908
Eric Anholtc751ce42010-03-25 11:48:48 -07007909 num_connectors++;
Jesse Barnes79e53942008-11-07 14:24:08 -08007910 }
7911
Jani Nikulaf2335332013-09-13 11:03:09 +03007912 if (is_dsi)
Daniel Vetter5b18e572014-04-24 23:55:06 +02007913 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08007914
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007915 if (!crtc_state->clock_set) {
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007916 refclk = i9xx_get_refclk(crtc_state, num_connectors);
Jani Nikulaf2335332013-09-13 11:03:09 +03007917
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007918 /*
7919 * Returns a set of divisors for the desired target clock with
7920 * the given refclk, or FALSE. The returned values represent
7921 * the clock equation: reflck * (5 * (m1 + 2) + (m2 + 2)) / (n +
7922 * 2) / p1 / p2.
7923 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007924 limit = intel_limit(crtc_state, refclk);
7925 ok = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007926 crtc_state->port_clock,
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007927 refclk, NULL, &clock);
Jani Nikulaf2335332013-09-13 11:03:09 +03007928 if (!ok) {
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007929 DRM_ERROR("Couldn't find PLL settings for mode!\n");
7930 return -EINVAL;
7931 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007932
Jani Nikulaf2335332013-09-13 11:03:09 +03007933 if (is_lvds && dev_priv->lvds_downclock_avail) {
7934 /*
7935 * Ensure we match the reduced clock's P to the target
7936 * clock. If the clocks don't match, we can't switch
7937 * the display clock by using the FP0/FP1. In such case
7938 * we will disable the LVDS downclock feature.
7939 */
7940 has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02007941 dev_priv->display.find_dpll(limit, crtc_state,
Jani Nikulaf2335332013-09-13 11:03:09 +03007942 dev_priv->lvds_downclock,
7943 refclk, &clock,
7944 &reduced_clock);
7945 }
7946 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007947 crtc_state->dpll.n = clock.n;
7948 crtc_state->dpll.m1 = clock.m1;
7949 crtc_state->dpll.m2 = clock.m2;
7950 crtc_state->dpll.p1 = clock.p1;
7951 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01007952 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007953
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007954 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007955 i8xx_update_pll(crtc, crtc_state,
Vijay Purushothaman2a8f64c2012-09-27 19:13:06 +05307956 has_reduced_clock ? &reduced_clock : NULL,
7957 num_connectors);
Chon Ming Lee9d556c92014-05-02 14:27:47 +03007958 } else if (IS_CHERRYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007959 chv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007960 } else if (IS_VALLEYVIEW(dev)) {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007961 vlv_update_pll(crtc, crtc_state);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007962 } else {
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02007963 i9xx_update_pll(crtc, crtc_state,
Daniel Vettereb1cbe42012-03-28 23:12:16 +02007964 has_reduced_clock ? &reduced_clock : NULL,
Robin Schroereba905b2014-05-18 02:24:50 +02007965 num_connectors);
Jani Nikulae9fd1c02013-08-27 15:12:23 +03007966 }
Eric Anholtf564048e2011-03-30 13:01:02 -07007967
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02007968 return 0;
Eric Anholtf564048e2011-03-30 13:01:02 -07007969}
7970
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007971static void i9xx_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02007972 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007973{
7974 struct drm_device *dev = crtc->base.dev;
7975 struct drm_i915_private *dev_priv = dev->dev_private;
7976 uint32_t tmp;
7977
Ville Syrjälädc9e7dec2014-01-10 14:06:45 +02007978 if (INTEL_INFO(dev)->gen <= 3 && (IS_I830(dev) || !IS_MOBILE(dev)))
7979 return;
7980
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007981 tmp = I915_READ(PFIT_CONTROL);
Daniel Vetter06922822013-07-11 13:35:40 +02007982 if (!(tmp & PFIT_ENABLE))
7983 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007984
Daniel Vetter06922822013-07-11 13:35:40 +02007985 /* Check whether the pfit is attached to our pipe. */
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007986 if (INTEL_INFO(dev)->gen < 4) {
7987 if (crtc->pipe != PIPE_B)
7988 return;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007989 } else {
7990 if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
7991 return;
7992 }
7993
Daniel Vetter06922822013-07-11 13:35:40 +02007994 pipe_config->gmch_pfit.control = tmp;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02007995 pipe_config->gmch_pfit.pgm_ratios = I915_READ(PFIT_PGM_RATIOS);
7996 if (INTEL_INFO(dev)->gen < 5)
7997 pipe_config->gmch_pfit.lvds_border_bits =
7998 I915_READ(LVDS) & LVDS_BORDER_ENABLE;
7999}
8000
Jesse Barnesacbec812013-09-20 11:29:32 -07008001static void vlv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008002 struct intel_crtc_state *pipe_config)
Jesse Barnesacbec812013-09-20 11:29:32 -07008003{
8004 struct drm_device *dev = crtc->base.dev;
8005 struct drm_i915_private *dev_priv = dev->dev_private;
8006 int pipe = pipe_config->cpu_transcoder;
8007 intel_clock_t clock;
8008 u32 mdiv;
Chris Wilson662c6ec2013-09-25 14:24:01 -07008009 int refclk = 100000;
Jesse Barnesacbec812013-09-20 11:29:32 -07008010
Shobhit Kumarf573de52014-07-30 20:32:37 +05308011 /* In case of MIPI DPLL will not even be used */
8012 if (!(pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE))
8013 return;
8014
Ville Syrjäläa5805162015-05-26 20:42:30 +03008015 mutex_lock(&dev_priv->sb_lock);
Chon Ming Leeab3c7592013-11-07 10:43:30 +08008016 mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008017 mutex_unlock(&dev_priv->sb_lock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008018
8019 clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
8020 clock.m2 = mdiv & DPIO_M2DIV_MASK;
8021 clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
8022 clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
8023 clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
8024
Ville Syrjäläf6466282013-10-14 14:50:31 +03008025 vlv_clock(refclk, &clock);
Jesse Barnesacbec812013-09-20 11:29:32 -07008026
Ville Syrjäläf6466282013-10-14 14:50:31 +03008027 /* clock.dot is the fast clock */
8028 pipe_config->port_clock = clock.dot / 5;
Jesse Barnesacbec812013-09-20 11:29:32 -07008029}
8030
Damien Lespiau5724dbd2015-01-20 12:51:52 +00008031static void
8032i9xx_get_initial_plane_config(struct intel_crtc *crtc,
8033 struct intel_initial_plane_config *plane_config)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008034{
8035 struct drm_device *dev = crtc->base.dev;
8036 struct drm_i915_private *dev_priv = dev->dev_private;
8037 u32 val, base, offset;
8038 int pipe = crtc->pipe, plane = crtc->plane;
8039 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00008040 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008041 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00008042 struct intel_framebuffer *intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008043
Damien Lespiau42a7b082015-02-05 19:35:13 +00008044 val = I915_READ(DSPCNTR(plane));
8045 if (!(val & DISPLAY_PLANE_ENABLE))
8046 return;
8047
Damien Lespiaud9806c92015-01-21 14:07:19 +00008048 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00008049 if (!intel_fb) {
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008050 DRM_DEBUG_KMS("failed to alloc fb\n");
8051 return;
8052 }
8053
Damien Lespiau1b842c82015-01-21 13:50:54 +00008054 fb = &intel_fb->base;
8055
Daniel Vetter18c52472015-02-10 17:16:09 +00008056 if (INTEL_INFO(dev)->gen >= 4) {
8057 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008058 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00008059 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
8060 }
8061 }
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008062
8063 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00008064 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008065 fb->pixel_format = fourcc;
8066 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008067
8068 if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau49af4492015-01-20 12:51:44 +00008069 if (plane_config->tiling)
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008070 offset = I915_READ(DSPTILEOFF(plane));
8071 else
8072 offset = I915_READ(DSPLINOFF(plane));
8073 base = I915_READ(DSPSURF(plane)) & 0xfffff000;
8074 } else {
8075 base = I915_READ(DSPADDR(plane));
8076 }
8077 plane_config->base = base;
8078
8079 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008080 fb->width = ((val >> 16) & 0xfff) + 1;
8081 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008082
8083 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008084 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008085
Damien Lespiaub113d5e2015-01-20 12:51:46 +00008086 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00008087 fb->pixel_format,
8088 fb->modifier[0]);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008089
Daniel Vetterf37b5c22015-02-10 23:12:27 +01008090 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008091
Damien Lespiau2844a922015-01-20 12:51:48 +00008092 DRM_DEBUG_KMS("pipe/plane %c/%d with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
8093 pipe_name(pipe), plane, fb->width, fb->height,
8094 fb->bits_per_pixel, base, fb->pitches[0],
8095 plane_config->size);
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008096
Damien Lespiau2d140302015-02-05 17:22:18 +00008097 plane_config->fb = intel_fb;
Jesse Barnes1ad292b2014-03-07 08:57:49 -08008098}
8099
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008100static void chv_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008101 struct intel_crtc_state *pipe_config)
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008102{
8103 struct drm_device *dev = crtc->base.dev;
8104 struct drm_i915_private *dev_priv = dev->dev_private;
8105 int pipe = pipe_config->cpu_transcoder;
8106 enum dpio_channel port = vlv_pipe_to_channel(pipe);
8107 intel_clock_t clock;
8108 u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2;
8109 int refclk = 100000;
8110
Ville Syrjäläa5805162015-05-26 20:42:30 +03008111 mutex_lock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008112 cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
8113 pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
8114 pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
8115 pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
Ville Syrjäläa5805162015-05-26 20:42:30 +03008116 mutex_unlock(&dev_priv->sb_lock);
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008117
8118 clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
8119 clock.m2 = ((pll_dw0 & 0xff) << 22) | (pll_dw2 & 0x3fffff);
8120 clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
8121 clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
8122 clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
8123
8124 chv_clock(refclk, &clock);
8125
8126 /* clock.dot is the fast clock */
8127 pipe_config->port_clock = clock.dot / 5;
8128}
8129
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008130static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02008131 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008132{
8133 struct drm_device *dev = crtc->base.dev;
8134 struct drm_i915_private *dev_priv = dev->dev_private;
8135 uint32_t tmp;
8136
Daniel Vetterf458ebb2014-09-30 10:56:39 +02008137 if (!intel_display_power_is_enabled(dev_priv,
8138 POWER_DOMAIN_PIPE(crtc->pipe)))
Imre Deakb5482bd2014-03-05 16:20:55 +02008139 return false;
8140
Daniel Vettere143a212013-07-04 12:01:15 +02008141 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02008142 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02008143
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008144 tmp = I915_READ(PIPECONF(crtc->pipe));
8145 if (!(tmp & PIPECONF_ENABLE))
8146 return false;
8147
Ville Syrjälä42571ae2013-09-06 23:29:00 +03008148 if (IS_G4X(dev) || IS_VALLEYVIEW(dev)) {
8149 switch (tmp & PIPECONF_BPC_MASK) {
8150 case PIPECONF_6BPC:
8151 pipe_config->pipe_bpp = 18;
8152 break;
8153 case PIPECONF_8BPC:
8154 pipe_config->pipe_bpp = 24;
8155 break;
8156 case PIPECONF_10BPC:
8157 pipe_config->pipe_bpp = 30;
8158 break;
8159 default:
8160 break;
8161 }
8162 }
8163
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02008164 if (IS_VALLEYVIEW(dev) && (tmp & PIPECONF_COLOR_RANGE_SELECT))
8165 pipe_config->limited_color_range = true;
8166
Ville Syrjälä282740f2013-09-04 18:30:03 +03008167 if (INTEL_INFO(dev)->gen < 4)
8168 pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
8169
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02008170 intel_get_pipe_timings(crtc, pipe_config);
8171
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02008172 i9xx_get_pfit_config(crtc, pipe_config);
8173
Daniel Vetter6c49f242013-06-06 12:45:25 +02008174 if (INTEL_INFO(dev)->gen >= 4) {
8175 tmp = I915_READ(DPLL_MD(crtc->pipe));
8176 pipe_config->pixel_multiplier =
8177 ((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
8178 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008179 pipe_config->dpll_hw_state.dpll_md = tmp;
Daniel Vetter6c49f242013-06-06 12:45:25 +02008180 } else if (IS_I945G(dev) || IS_I945GM(dev) || IS_G33(dev)) {
8181 tmp = I915_READ(DPLL(crtc->pipe));
8182 pipe_config->pixel_multiplier =
8183 ((tmp & SDVO_MULTIPLIER_MASK)
8184 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
8185 } else {
8186 /* Note that on i915G/GM the pixel multiplier is in the sdvo
8187 * port and will be fixed up in the encoder->get_config
8188 * function. */
8189 pipe_config->pixel_multiplier = 1;
8190 }
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008191 pipe_config->dpll_hw_state.dpll = I915_READ(DPLL(crtc->pipe));
8192 if (!IS_VALLEYVIEW(dev)) {
Ville Syrjälä1c4e0272014-09-05 21:52:42 +03008193 /*
8194 * DPLL_DVO_2X_MODE must be enabled for both DPLLs
8195 * on 830. Filter it out here so that we don't
8196 * report errors due to that.
8197 */
8198 if (IS_I830(dev))
8199 pipe_config->dpll_hw_state.dpll &= ~DPLL_DVO_2X_MODE;
8200
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008201 pipe_config->dpll_hw_state.fp0 = I915_READ(FP0(crtc->pipe));
8202 pipe_config->dpll_hw_state.fp1 = I915_READ(FP1(crtc->pipe));
Ville Syrjälä165e9012013-06-26 17:44:15 +03008203 } else {
8204 /* Mask out read-only status bits. */
8205 pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
8206 DPLL_PORTC_READY_MASK |
8207 DPLL_PORTB_READY_MASK);
Daniel Vetter8bcc2792013-06-05 13:34:28 +02008208 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02008209
Ville Syrjälä70b23a92014-04-09 13:28:22 +03008210 if (IS_CHERRYVIEW(dev))
8211 chv_crtc_clock_get(crtc, pipe_config);
8212 else if (IS_VALLEYVIEW(dev))
Jesse Barnesacbec812013-09-20 11:29:32 -07008213 vlv_crtc_clock_get(crtc, pipe_config);
8214 else
8215 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä18442d02013-09-13 16:00:08 +03008216
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01008217 return true;
8218}
8219
Paulo Zanonidde86e22012-12-01 12:04:25 -02008220static void ironlake_init_pch_refclk(struct drm_device *dev)
Jesse Barnes13d83a62011-08-03 12:59:20 -07008221{
8222 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008223 struct intel_encoder *encoder;
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008224 u32 val, final;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008225 bool has_lvds = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008226 bool has_cpu_edp = false;
Keith Packard199e5d72011-09-22 12:01:57 -07008227 bool has_panel = false;
Keith Packard99eb6a02011-09-26 14:29:12 -07008228 bool has_ck505 = false;
8229 bool can_ssc = false;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008230
8231 /* We need to take the global config into account */
Damien Lespiaub2784e12014-08-05 11:29:37 +01008232 for_each_intel_encoder(dev, encoder) {
Keith Packard199e5d72011-09-22 12:01:57 -07008233 switch (encoder->type) {
8234 case INTEL_OUTPUT_LVDS:
8235 has_panel = true;
8236 has_lvds = true;
8237 break;
8238 case INTEL_OUTPUT_EDP:
8239 has_panel = true;
Imre Deak2de69052013-05-08 13:14:04 +03008240 if (enc_to_dig_port(&encoder->base)->port == PORT_A)
Keith Packard199e5d72011-09-22 12:01:57 -07008241 has_cpu_edp = true;
8242 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008243 default:
8244 break;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008245 }
8246 }
8247
Keith Packard99eb6a02011-09-26 14:29:12 -07008248 if (HAS_PCH_IBX(dev)) {
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008249 has_ck505 = dev_priv->vbt.display_clock_mode;
Keith Packard99eb6a02011-09-26 14:29:12 -07008250 can_ssc = has_ck505;
8251 } else {
8252 has_ck505 = false;
8253 can_ssc = true;
8254 }
8255
Imre Deak2de69052013-05-08 13:14:04 +03008256 DRM_DEBUG_KMS("has_panel %d has_lvds %d has_ck505 %d\n",
8257 has_panel, has_lvds, has_ck505);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008258
8259 /* Ironlake: try to setup display ref clock before DPLL
8260 * enabling. This is only under driver's control after
8261 * PCH B stepping, previous chipset stepping should be
8262 * ignoring this setting.
8263 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008264 val = I915_READ(PCH_DREF_CONTROL);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008265
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008266 /* As we must carefully and slowly disable/enable each source in turn,
8267 * compute the final state we want first and check if we need to
8268 * make any changes at all.
8269 */
8270 final = val;
8271 final &= ~DREF_NONSPREAD_SOURCE_MASK;
Keith Packard99eb6a02011-09-26 14:29:12 -07008272 if (has_ck505)
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008273 final |= DREF_NONSPREAD_CK505_ENABLE;
Keith Packard99eb6a02011-09-26 14:29:12 -07008274 else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008275 final |= DREF_NONSPREAD_SOURCE_ENABLE;
8276
8277 final &= ~DREF_SSC_SOURCE_MASK;
8278 final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
8279 final &= ~DREF_SSC1_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008280
Keith Packard199e5d72011-09-22 12:01:57 -07008281 if (has_panel) {
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008282 final |= DREF_SSC_SOURCE_ENABLE;
8283
8284 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8285 final |= DREF_SSC1_ENABLE;
8286
8287 if (has_cpu_edp) {
8288 if (intel_panel_use_ssc(dev_priv) && can_ssc)
8289 final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
8290 else
8291 final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
8292 } else
8293 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8294 } else {
8295 final |= DREF_SSC_SOURCE_DISABLE;
8296 final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
8297 }
8298
8299 if (final == val)
8300 return;
8301
8302 /* Always enable nonspread source */
8303 val &= ~DREF_NONSPREAD_SOURCE_MASK;
8304
8305 if (has_ck505)
8306 val |= DREF_NONSPREAD_CK505_ENABLE;
8307 else
8308 val |= DREF_NONSPREAD_SOURCE_ENABLE;
8309
8310 if (has_panel) {
8311 val &= ~DREF_SSC_SOURCE_MASK;
8312 val |= DREF_SSC_SOURCE_ENABLE;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008313
Keith Packard199e5d72011-09-22 12:01:57 -07008314 /* SSC must be turned on before enabling the CPU output */
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 panel\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008317 val |= DREF_SSC1_ENABLE;
Daniel Vettere77166b2012-03-30 22:14:05 +02008318 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008319 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008320
8321 /* Get SSC going before enabling the outputs */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008322 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008323 POSTING_READ(PCH_DREF_CONTROL);
8324 udelay(200);
8325
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008326 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Jesse Barnes13d83a62011-08-03 12:59:20 -07008327
8328 /* Enable CPU source on CPU attached eDP */
Keith Packard199e5d72011-09-22 12:01:57 -07008329 if (has_cpu_edp) {
Keith Packard99eb6a02011-09-26 14:29:12 -07008330 if (intel_panel_use_ssc(dev_priv) && can_ssc) {
Keith Packard199e5d72011-09-22 12:01:57 -07008331 DRM_DEBUG_KMS("Using SSC on eDP\n");
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008332 val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
Robin Schroereba905b2014-05-18 02:24:50 +02008333 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008334 val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
Keith Packard199e5d72011-09-22 12:01:57 -07008335 } else
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008336 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008337
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008338 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008339 POSTING_READ(PCH_DREF_CONTROL);
8340 udelay(200);
8341 } else {
8342 DRM_DEBUG_KMS("Disabling SSC entirely\n");
8343
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008344 val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
Keith Packard199e5d72011-09-22 12:01:57 -07008345
8346 /* Turn off CPU output */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008347 val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008348
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008349 I915_WRITE(PCH_DREF_CONTROL, val);
Keith Packard199e5d72011-09-22 12:01:57 -07008350 POSTING_READ(PCH_DREF_CONTROL);
8351 udelay(200);
8352
8353 /* Turn off the SSC source */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008354 val &= ~DREF_SSC_SOURCE_MASK;
8355 val |= DREF_SSC_SOURCE_DISABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008356
8357 /* Turn off SSC1 */
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008358 val &= ~DREF_SSC1_ENABLE;
Keith Packard199e5d72011-09-22 12:01:57 -07008359
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008360 I915_WRITE(PCH_DREF_CONTROL, val);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008361 POSTING_READ(PCH_DREF_CONTROL);
8362 udelay(200);
8363 }
Chris Wilson74cfd7a2013-03-26 16:33:04 -07008364
8365 BUG_ON(val != final);
Jesse Barnes13d83a62011-08-03 12:59:20 -07008366}
8367
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008368static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
Paulo Zanonidde86e22012-12-01 12:04:25 -02008369{
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008370 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008371
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008372 tmp = I915_READ(SOUTH_CHICKEN2);
8373 tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
8374 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008375
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008376 if (wait_for_atomic_us(I915_READ(SOUTH_CHICKEN2) &
8377 FDI_MPHY_IOSFSB_RESET_STATUS, 100))
8378 DRM_ERROR("FDI mPHY reset assert timeout\n");
Paulo Zanonidde86e22012-12-01 12:04:25 -02008379
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008380 tmp = I915_READ(SOUTH_CHICKEN2);
8381 tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
8382 I915_WRITE(SOUTH_CHICKEN2, tmp);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008383
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008384 if (wait_for_atomic_us((I915_READ(SOUTH_CHICKEN2) &
8385 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
8386 DRM_ERROR("FDI mPHY reset de-assert timeout\n");
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008387}
8388
8389/* WaMPhyProgramming:hsw */
8390static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
8391{
8392 uint32_t tmp;
Paulo Zanonidde86e22012-12-01 12:04:25 -02008393
8394 tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
8395 tmp &= ~(0xFF << 24);
8396 tmp |= (0x12 << 24);
8397 intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
8398
Paulo Zanonidde86e22012-12-01 12:04:25 -02008399 tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
8400 tmp |= (1 << 11);
8401 intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
8402
8403 tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
8404 tmp |= (1 << 11);
8405 intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
8406
Paulo Zanonidde86e22012-12-01 12:04:25 -02008407 tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
8408 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8409 intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
8410
8411 tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
8412 tmp |= (1 << 24) | (1 << 21) | (1 << 18);
8413 intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
8414
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008415 tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
8416 tmp &= ~(7 << 13);
8417 tmp |= (5 << 13);
8418 intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008419
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008420 tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
8421 tmp &= ~(7 << 13);
8422 tmp |= (5 << 13);
8423 intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008424
8425 tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
8426 tmp &= ~0xFF;
8427 tmp |= 0x1C;
8428 intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
8429
8430 tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
8431 tmp &= ~0xFF;
8432 tmp |= 0x1C;
8433 intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
8434
8435 tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
8436 tmp &= ~(0xFF << 16);
8437 tmp |= (0x1C << 16);
8438 intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
8439
8440 tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
8441 tmp &= ~(0xFF << 16);
8442 tmp |= (0x1C << 16);
8443 intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
8444
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008445 tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
8446 tmp |= (1 << 27);
8447 intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008448
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008449 tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
8450 tmp |= (1 << 27);
8451 intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008452
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008453 tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
8454 tmp &= ~(0xF << 28);
8455 tmp |= (4 << 28);
8456 intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008457
Paulo Zanoni0ff066a2013-07-12 14:19:36 -03008458 tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
8459 tmp &= ~(0xF << 28);
8460 tmp |= (4 << 28);
8461 intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008462}
8463
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008464/* Implements 3 different sequences from BSpec chapter "Display iCLK
8465 * Programming" based on the parameters passed:
8466 * - Sequence to enable CLKOUT_DP
8467 * - Sequence to enable CLKOUT_DP without spread
8468 * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
8469 */
8470static void lpt_enable_clkout_dp(struct drm_device *dev, bool with_spread,
8471 bool with_fdi)
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008472{
8473 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008474 uint32_t reg, tmp;
8475
8476 if (WARN(with_fdi && !with_spread, "FDI requires downspread\n"))
8477 with_spread = true;
8478 if (WARN(dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE &&
8479 with_fdi, "LP PCH doesn't have FDI\n"))
8480 with_fdi = false;
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008481
Ville Syrjäläa5805162015-05-26 20:42:30 +03008482 mutex_lock(&dev_priv->sb_lock);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008483
8484 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8485 tmp &= ~SBI_SSCCTL_DISABLE;
8486 tmp |= SBI_SSCCTL_PATHALT;
8487 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8488
8489 udelay(24);
8490
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008491 if (with_spread) {
8492 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8493 tmp &= ~SBI_SSCCTL_PATHALT;
8494 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
Paulo Zanonif31f2d52013-07-18 18:51:11 -03008495
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008496 if (with_fdi) {
8497 lpt_reset_fdi_mphy(dev_priv);
8498 lpt_program_fdi_mphy(dev_priv);
8499 }
8500 }
Paulo Zanonidde86e22012-12-01 12:04:25 -02008501
Paulo Zanoni2fa86a12013-07-23 11:19:24 -03008502 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8503 SBI_GEN0 : SBI_DBUFF0;
8504 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8505 tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8506 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
Daniel Vetterc00db242013-01-22 15:33:27 +01008507
Ville Syrjäläa5805162015-05-26 20:42:30 +03008508 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanonidde86e22012-12-01 12:04:25 -02008509}
8510
Paulo Zanoni47701c32013-07-23 11:19:25 -03008511/* Sequence to disable CLKOUT_DP */
8512static void lpt_disable_clkout_dp(struct drm_device *dev)
8513{
8514 struct drm_i915_private *dev_priv = dev->dev_private;
8515 uint32_t reg, tmp;
8516
Ville Syrjäläa5805162015-05-26 20:42:30 +03008517 mutex_lock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008518
8519 reg = (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) ?
8520 SBI_GEN0 : SBI_DBUFF0;
8521 tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
8522 tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
8523 intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
8524
8525 tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
8526 if (!(tmp & SBI_SSCCTL_DISABLE)) {
8527 if (!(tmp & SBI_SSCCTL_PATHALT)) {
8528 tmp |= SBI_SSCCTL_PATHALT;
8529 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8530 udelay(32);
8531 }
8532 tmp |= SBI_SSCCTL_DISABLE;
8533 intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
8534 }
8535
Ville Syrjäläa5805162015-05-26 20:42:30 +03008536 mutex_unlock(&dev_priv->sb_lock);
Paulo Zanoni47701c32013-07-23 11:19:25 -03008537}
8538
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008539static void lpt_init_pch_refclk(struct drm_device *dev)
8540{
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008541 struct intel_encoder *encoder;
8542 bool has_vga = false;
8543
Damien Lespiaub2784e12014-08-05 11:29:37 +01008544 for_each_intel_encoder(dev, encoder) {
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008545 switch (encoder->type) {
8546 case INTEL_OUTPUT_ANALOG:
8547 has_vga = true;
8548 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008549 default:
8550 break;
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008551 }
8552 }
8553
Paulo Zanoni47701c32013-07-23 11:19:25 -03008554 if (has_vga)
8555 lpt_enable_clkout_dp(dev, true, true);
8556 else
8557 lpt_disable_clkout_dp(dev);
Paulo Zanonibf8fa3d2013-07-12 14:19:38 -03008558}
8559
Paulo Zanonidde86e22012-12-01 12:04:25 -02008560/*
8561 * Initialize reference clocks when the driver loads
8562 */
8563void intel_init_pch_refclk(struct drm_device *dev)
8564{
8565 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
8566 ironlake_init_pch_refclk(dev);
8567 else if (HAS_PCH_LPT(dev))
8568 lpt_init_pch_refclk(dev);
8569}
8570
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008571static int ironlake_get_refclk(struct intel_crtc_state *crtc_state)
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008572{
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008573 struct drm_device *dev = crtc_state->base.crtc->dev;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008574 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008575 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008576 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008577 struct drm_connector_state *connector_state;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008578 struct intel_encoder *encoder;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008579 int num_connectors = 0, i;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008580 bool is_lvds = false;
8581
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008582 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008583 if (connector_state->crtc != crtc_state->base.crtc)
8584 continue;
8585
8586 encoder = to_intel_encoder(connector_state->best_encoder);
8587
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008588 switch (encoder->type) {
8589 case INTEL_OUTPUT_LVDS:
8590 is_lvds = true;
8591 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008592 default:
8593 break;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008594 }
8595 num_connectors++;
8596 }
8597
8598 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2) {
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008599 DRM_DEBUG_KMS("using SSC reference clock of %d kHz\n",
Rodrigo Vivi41aa3442013-05-09 20:03:18 -03008600 dev_priv->vbt.lvds_ssc_freq);
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008601 return dev_priv->vbt.lvds_ssc_freq;
Jesse Barnesd9d444c2011-09-02 13:03:05 -07008602 }
8603
8604 return 120000;
8605}
8606
Daniel Vetter6ff93602013-04-19 11:24:36 +02008607static void ironlake_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanonic8203562012-09-12 10:06:29 -03008608{
8609 struct drm_i915_private *dev_priv = crtc->dev->dev_private;
8610 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8611 int pipe = intel_crtc->pipe;
8612 uint32_t val;
8613
Daniel Vetter78114072013-06-13 00:54:57 +02008614 val = 0;
Paulo Zanonic8203562012-09-12 10:06:29 -03008615
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008616 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanonic8203562012-09-12 10:06:29 -03008617 case 18:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008618 val |= PIPECONF_6BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008619 break;
8620 case 24:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008621 val |= PIPECONF_8BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008622 break;
8623 case 30:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008624 val |= PIPECONF_10BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008625 break;
8626 case 36:
Daniel Vetterdfd07d72012-12-17 11:21:38 +01008627 val |= PIPECONF_12BPC;
Paulo Zanonic8203562012-09-12 10:06:29 -03008628 break;
8629 default:
Paulo Zanonicc769b62012-09-20 18:36:03 -03008630 /* Case prevented by intel_choose_pipe_bpp_dither. */
8631 BUG();
Paulo Zanonic8203562012-09-12 10:06:29 -03008632 }
8633
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008634 if (intel_crtc->config->dither)
Paulo Zanonic8203562012-09-12 10:06:29 -03008635 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8636
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008637 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanonic8203562012-09-12 10:06:29 -03008638 val |= PIPECONF_INTERLACED_ILK;
8639 else
8640 val |= PIPECONF_PROGRESSIVE;
8641
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008642 if (intel_crtc->config->limited_color_range)
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008643 val |= PIPECONF_COLOR_RANGE_SELECT;
Ville Syrjälä3685a8f2013-01-17 16:31:28 +02008644
Paulo Zanonic8203562012-09-12 10:06:29 -03008645 I915_WRITE(PIPECONF(pipe), val);
8646 POSTING_READ(PIPECONF(pipe));
8647}
8648
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008649/*
8650 * Set up the pipe CSC unit.
8651 *
8652 * Currently only full range RGB to limited range RGB conversion
8653 * is supported, but eventually this should handle various
8654 * RGB<->YCbCr scenarios as well.
8655 */
Daniel Vetter50f3b012013-03-27 00:44:56 +01008656static void intel_set_pipe_csc(struct drm_crtc *crtc)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008657{
8658 struct drm_device *dev = crtc->dev;
8659 struct drm_i915_private *dev_priv = dev->dev_private;
8660 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
8661 int pipe = intel_crtc->pipe;
8662 uint16_t coeff = 0x7800; /* 1.0 */
8663
8664 /*
8665 * TODO: Check what kind of values actually come out of the pipe
8666 * with these coeff/postoff values and adjust to get the best
8667 * accuracy. Perhaps we even need to take the bpc value into
8668 * consideration.
8669 */
8670
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008671 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008672 coeff = ((235 - 16) * (1 << 12) / 255) & 0xff8; /* 0.xxx... */
8673
8674 /*
8675 * GY/GU and RY/RU should be the other way around according
8676 * to BSpec, but reality doesn't agree. Just set them up in
8677 * a way that results in the correct picture.
8678 */
8679 I915_WRITE(PIPE_CSC_COEFF_RY_GY(pipe), coeff << 16);
8680 I915_WRITE(PIPE_CSC_COEFF_BY(pipe), 0);
8681
8682 I915_WRITE(PIPE_CSC_COEFF_RU_GU(pipe), coeff);
8683 I915_WRITE(PIPE_CSC_COEFF_BU(pipe), 0);
8684
8685 I915_WRITE(PIPE_CSC_COEFF_RV_GV(pipe), 0);
8686 I915_WRITE(PIPE_CSC_COEFF_BV(pipe), coeff << 16);
8687
8688 I915_WRITE(PIPE_CSC_PREOFF_HI(pipe), 0);
8689 I915_WRITE(PIPE_CSC_PREOFF_ME(pipe), 0);
8690 I915_WRITE(PIPE_CSC_PREOFF_LO(pipe), 0);
8691
8692 if (INTEL_INFO(dev)->gen > 6) {
8693 uint16_t postoff = 0;
8694
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008695 if (intel_crtc->config->limited_color_range)
Ville Syrjälä32cf0cb2013-11-28 22:10:38 +02008696 postoff = (16 * (1 << 12) / 255) & 0x1fff;
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008697
8698 I915_WRITE(PIPE_CSC_POSTOFF_HI(pipe), postoff);
8699 I915_WRITE(PIPE_CSC_POSTOFF_ME(pipe), postoff);
8700 I915_WRITE(PIPE_CSC_POSTOFF_LO(pipe), postoff);
8701
8702 I915_WRITE(PIPE_CSC_MODE(pipe), 0);
8703 } else {
8704 uint32_t mode = CSC_MODE_YUV_TO_RGB;
8705
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008706 if (intel_crtc->config->limited_color_range)
Ville Syrjälä86d3efc2013-01-18 19:11:38 +02008707 mode |= CSC_BLACK_SCREEN_OFFSET;
8708
8709 I915_WRITE(PIPE_CSC_MODE(pipe), mode);
8710 }
8711}
8712
Daniel Vetter6ff93602013-04-19 11:24:36 +02008713static void haswell_set_pipeconf(struct drm_crtc *crtc)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008714{
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008715 struct drm_device *dev = crtc->dev;
8716 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008717 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008718 enum pipe pipe = intel_crtc->pipe;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008719 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008720 uint32_t val;
8721
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008722 val = 0;
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008723
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008724 if (IS_HASWELL(dev) && intel_crtc->config->dither)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008725 val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
8726
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008727 if (intel_crtc->config->base.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008728 val |= PIPECONF_INTERLACED_ILK;
8729 else
8730 val |= PIPECONF_PROGRESSIVE;
8731
Paulo Zanoni702e7a52012-10-23 18:29:59 -02008732 I915_WRITE(PIPECONF(cpu_transcoder), val);
8733 POSTING_READ(PIPECONF(cpu_transcoder));
Daniel Vetter3eff4fa2013-06-13 00:54:59 +02008734
8735 I915_WRITE(GAMMA_MODE(intel_crtc->pipe), GAMMA_MODE_MODE_8BIT);
8736 POSTING_READ(GAMMA_MODE(intel_crtc->pipe));
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008737
Satheeshakrishna M3cdf122c2014-04-08 15:46:53 +05308738 if (IS_BROADWELL(dev) || INTEL_INFO(dev)->gen >= 9) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008739 val = 0;
8740
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008741 switch (intel_crtc->config->pipe_bpp) {
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008742 case 18:
8743 val |= PIPEMISC_DITHER_6_BPC;
8744 break;
8745 case 24:
8746 val |= PIPEMISC_DITHER_8_BPC;
8747 break;
8748 case 30:
8749 val |= PIPEMISC_DITHER_10_BPC;
8750 break;
8751 case 36:
8752 val |= PIPEMISC_DITHER_12_BPC;
8753 break;
8754 default:
8755 /* Case prevented by pipe_config_set_bpp. */
8756 BUG();
8757 }
8758
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02008759 if (intel_crtc->config->dither)
Paulo Zanoni756f85c2013-11-02 21:07:38 -07008760 val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
8761
8762 I915_WRITE(PIPEMISC(pipe), val);
8763 }
Paulo Zanoniee2b0b32012-10-05 12:05:57 -03008764}
8765
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008766static bool ironlake_compute_clocks(struct drm_crtc *crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008767 struct intel_crtc_state *crtc_state,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008768 intel_clock_t *clock,
8769 bool *has_reduced_clock,
8770 intel_clock_t *reduced_clock)
8771{
8772 struct drm_device *dev = crtc->dev;
8773 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008774 int refclk;
8775 const intel_limit_t *limit;
Daniel Vettera16af722013-04-30 14:01:44 +02008776 bool ret, is_lvds = false;
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008777
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008778 is_lvds = intel_pipe_will_have_type(crtc_state, INTEL_OUTPUT_LVDS);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008779
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008780 refclk = ironlake_get_refclk(crtc_state);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008781
8782 /*
8783 * Returns a set of divisors for the desired target clock with the given
8784 * refclk, or FALSE. The returned values represent the clock equation:
8785 * reflck * (5 * (m1 + 2) + (m2 + 2)) / (n + 2) / p1 / p2.
8786 */
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008787 limit = intel_limit(crtc_state, refclk);
8788 ret = dev_priv->display.find_dpll(limit, crtc_state,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008789 crtc_state->port_clock,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008790 refclk, NULL, clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008791 if (!ret)
8792 return false;
8793
8794 if (is_lvds && dev_priv->lvds_downclock_avail) {
8795 /*
8796 * Ensure we match the reduced clock's P to the target clock.
8797 * If the clocks don't match, we can't switch the display clock
8798 * by using the FP0/FP1. In such case we will disable the LVDS
8799 * downclock feature.
8800 */
Daniel Vetteree9300b2013-06-03 22:40:22 +02008801 *has_reduced_clock =
Ander Conselvan de Oliveiraa93e2552015-03-20 16:18:17 +02008802 dev_priv->display.find_dpll(limit, crtc_state,
Daniel Vetteree9300b2013-06-03 22:40:22 +02008803 dev_priv->lvds_downclock,
8804 refclk, clock,
8805 reduced_clock);
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008806 }
8807
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008808 return true;
8809}
8810
Paulo Zanonid4b19312012-11-29 11:29:32 -02008811int ironlake_get_lanes_required(int target_clock, int link_bw, int bpp)
8812{
8813 /*
8814 * Account for spread spectrum to avoid
8815 * oversubscribing the link. Max center spread
8816 * is 2.5%; use 5% for safety's sake.
8817 */
8818 u32 bps = target_clock * bpp * 21 / 20;
Ville Syrjälä619d4d02014-02-27 14:23:14 +02008819 return DIV_ROUND_UP(bps, link_bw * 8);
Paulo Zanonid4b19312012-11-29 11:29:32 -02008820}
8821
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008822static bool ironlake_needs_fb_cb_tune(struct dpll *dpll, int factor)
Daniel Vetter6cf86a52013-04-02 23:38:10 +02008823{
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008824 return i9xx_dpll_compute_m(dpll) < factor * dpll->n;
Paulo Zanonif48d8f22012-09-20 18:36:04 -03008825}
8826
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008827static uint32_t ironlake_compute_dpll(struct intel_crtc *intel_crtc,
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008828 struct intel_crtc_state *crtc_state,
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008829 u32 *fp,
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008830 intel_clock_t *reduced_clock, u32 *fp2)
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008831{
8832 struct drm_crtc *crtc = &intel_crtc->base;
8833 struct drm_device *dev = crtc->dev;
8834 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008835 struct drm_atomic_state *state = crtc_state->base.state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008836 struct drm_connector *connector;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008837 struct drm_connector_state *connector_state;
8838 struct intel_encoder *encoder;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008839 uint32_t dpll;
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008840 int factor, num_connectors = 0, i;
Daniel Vetter09ede542013-04-30 14:01:45 +02008841 bool is_lvds = false, is_sdvo = false;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008842
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +03008843 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira55bb9992015-03-20 16:18:19 +02008844 if (connector_state->crtc != crtc_state->base.crtc)
8845 continue;
8846
8847 encoder = to_intel_encoder(connector_state->best_encoder);
8848
8849 switch (encoder->type) {
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008850 case INTEL_OUTPUT_LVDS:
8851 is_lvds = true;
8852 break;
8853 case INTEL_OUTPUT_SDVO:
8854 case INTEL_OUTPUT_HDMI:
8855 is_sdvo = true;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008856 break;
Paulo Zanoni6847d71b2014-10-27 17:47:52 -02008857 default:
8858 break;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008859 }
8860
8861 num_connectors++;
8862 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008863
Chris Wilsonc1858122010-12-03 21:35:48 +00008864 /* Enable autotuning of the PLL clock (if permissible) */
Eric Anholt8febb292011-03-30 13:01:07 -07008865 factor = 21;
8866 if (is_lvds) {
8867 if ((intel_panel_use_ssc(dev_priv) &&
Ville Syrjäläe91e9412013-12-09 18:54:16 +02008868 dev_priv->vbt.lvds_ssc_freq == 100000) ||
Daniel Vetterf0b44052013-04-04 22:20:33 +02008869 (HAS_PCH_IBX(dev) && intel_is_dual_link_lvds(dev)))
Eric Anholt8febb292011-03-30 13:01:07 -07008870 factor = 25;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008871 } else if (crtc_state->sdvo_tv_clock)
Eric Anholt8febb292011-03-30 13:01:07 -07008872 factor = 20;
Chris Wilsonc1858122010-12-03 21:35:48 +00008873
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008874 if (ironlake_needs_fb_cb_tune(&crtc_state->dpll, factor))
Daniel Vetter7d0ac5b2013-04-04 22:20:32 +02008875 *fp |= FP_CB_TUNE;
Chris Wilsonc1858122010-12-03 21:35:48 +00008876
Daniel Vetter9a7c7892013-04-04 22:20:34 +02008877 if (fp2 && (reduced_clock->m < factor * reduced_clock->n))
8878 *fp2 |= FP_CB_TUNE;
8879
Chris Wilson5eddb702010-09-11 13:48:45 +01008880 dpll = 0;
Zhenyu Wang2c072452009-06-05 15:38:42 +08008881
Eric Anholta07d6782011-03-30 13:01:08 -07008882 if (is_lvds)
8883 dpll |= DPLLB_MODE_LVDS;
8884 else
8885 dpll |= DPLLB_MODE_DAC_SERIAL;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008886
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008887 dpll |= (crtc_state->pixel_multiplier - 1)
Daniel Vetteref1b4602013-06-01 17:17:04 +02008888 << PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT;
Daniel Vetter198a037f2013-04-19 11:14:37 +02008889
8890 if (is_sdvo)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008891 dpll |= DPLL_SDVO_HIGH_SPEED;
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008892 if (crtc_state->has_dp_encoder)
Daniel Vetter4a33e482013-07-06 12:52:05 +02008893 dpll |= DPLL_SDVO_HIGH_SPEED;
Jesse Barnes79e53942008-11-07 14:24:08 -08008894
Eric Anholta07d6782011-03-30 13:01:08 -07008895 /* compute bitmask from p1 value */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008896 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA01_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008897 /* also FPA1 */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008898 dpll |= (1 << (crtc_state->dpll.p1 - 1)) << DPLL_FPA1_P1_POST_DIV_SHIFT;
Eric Anholta07d6782011-03-30 13:01:08 -07008899
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008900 switch (crtc_state->dpll.p2) {
Eric Anholta07d6782011-03-30 13:01:08 -07008901 case 5:
8902 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_5;
8903 break;
8904 case 7:
8905 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_7;
8906 break;
8907 case 10:
8908 dpll |= DPLL_DAC_SERIAL_P2_CLOCK_DIV_10;
8909 break;
8910 case 14:
8911 dpll |= DPLLB_LVDS_P2_CLOCK_DIV_14;
8912 break;
Jesse Barnes79e53942008-11-07 14:24:08 -08008913 }
8914
Daniel Vetterb4c09f32013-04-30 14:01:42 +02008915 if (is_lvds && intel_panel_use_ssc(dev_priv) && num_connectors < 2)
Kristian Høgsberg43565a02009-02-13 20:56:52 -05008916 dpll |= PLLB_REF_INPUT_SPREADSPECTRUMIN;
Jesse Barnes79e53942008-11-07 14:24:08 -08008917 else
8918 dpll |= PLL_REF_INPUT_DREFCLK;
8919
Daniel Vetter959e16d2013-06-05 13:34:21 +02008920 return dpll | DPLL_VCO_ENABLE;
Paulo Zanonide13a2e2012-09-20 18:36:05 -03008921}
8922
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008923static int ironlake_crtc_compute_clock(struct intel_crtc *crtc,
8924 struct intel_crtc_state *crtc_state)
Jesse Barnes79e53942008-11-07 14:24:08 -08008925{
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008926 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -08008927 intel_clock_t clock, reduced_clock;
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008928 u32 dpll = 0, fp = 0, fp2 = 0;
Paulo Zanonie2f12b02012-09-20 18:36:06 -03008929 bool ok, has_reduced_clock = false;
Daniel Vetter8b470472013-03-28 10:41:59 +01008930 bool is_lvds = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008931 struct intel_shared_dpll *pll;
Jesse Barnes79e53942008-11-07 14:24:08 -08008932
Ander Conselvan de Oliveiradd3cd742015-05-15 13:34:29 +03008933 memset(&crtc_state->dpll_hw_state, 0,
8934 sizeof(crtc_state->dpll_hw_state));
8935
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +03008936 is_lvds = intel_pipe_has_type(crtc, INTEL_OUTPUT_LVDS);
Jesse Barnes79e53942008-11-07 14:24:08 -08008937
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008938 WARN(!(HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev)),
8939 "Unexpected PCH type %d\n", INTEL_PCH_TYPE(dev));
8940
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008941 ok = ironlake_compute_clocks(&crtc->base, crtc_state, &clock,
Paulo Zanoni6591c6e2012-09-12 10:06:34 -03008942 &has_reduced_clock, &reduced_clock);
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008943 if (!ok && !crtc_state->clock_set) {
Jesse Barnes79e53942008-11-07 14:24:08 -08008944 DRM_ERROR("Couldn't find PLL settings for mode!\n");
8945 return -EINVAL;
8946 }
Daniel Vetterf47709a2013-03-28 10:42:02 +01008947 /* Compat-code for transition, will disappear. */
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008948 if (!crtc_state->clock_set) {
8949 crtc_state->dpll.n = clock.n;
8950 crtc_state->dpll.m1 = clock.m1;
8951 crtc_state->dpll.m2 = clock.m2;
8952 crtc_state->dpll.p1 = clock.p1;
8953 crtc_state->dpll.p2 = clock.p2;
Daniel Vetterf47709a2013-03-28 10:42:02 +01008954 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008955
Paulo Zanoni5dc52982012-10-05 12:05:56 -03008956 /* 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 +02008957 if (crtc_state->has_pch_encoder) {
8958 fp = i9xx_dpll_compute_fp(&crtc_state->dpll);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008959 if (has_reduced_clock)
Daniel Vetter7429e9d2013-04-20 17:19:46 +02008960 fp2 = i9xx_dpll_compute_fp(&reduced_clock);
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008961
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008962 dpll = ironlake_compute_dpll(crtc, crtc_state,
Daniel Vettercbbab5b2013-04-19 11:14:31 +02008963 &fp, &reduced_clock,
8964 has_reduced_clock ? &fp2 : NULL);
8965
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008966 crtc_state->dpll_hw_state.dpll = dpll;
8967 crtc_state->dpll_hw_state.fp0 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008968 if (has_reduced_clock)
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008969 crtc_state->dpll_hw_state.fp1 = fp2;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008970 else
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008971 crtc_state->dpll_hw_state.fp1 = fp;
Daniel Vetter66e985c2013-06-05 13:34:20 +02008972
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02008973 pll = intel_get_shared_dpll(crtc, crtc_state);
Jesse Barnesee7b9f92012-04-20 17:11:53 +01008974 if (pll == NULL) {
Ville Syrjälä84f44ce2013-04-17 17:48:49 +03008975 DRM_DEBUG_DRIVER("failed to find PLL for pipe %c\n",
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008976 pipe_name(crtc->pipe));
Jesse Barnes4b645f12011-10-12 09:51:31 -07008977 return -EINVAL;
8978 }
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +02008979 }
Jesse Barnes79e53942008-11-07 14:24:08 -08008980
Rodrigo Viviab585de2015-03-24 12:40:09 -07008981 if (is_lvds && has_reduced_clock)
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008982 crtc->lowfreq_avail = true;
Daniel Vetterbcd644e2013-06-05 13:34:22 +02008983 else
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03008984 crtc->lowfreq_avail = false;
Daniel Vettere2b78262013-06-07 23:10:03 +02008985
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02008986 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08008987}
8988
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008989static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
8990 struct intel_link_m_n *m_n)
Daniel Vetter72419202013-04-04 13:28:53 +02008991{
8992 struct drm_device *dev = crtc->base.dev;
8993 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008994 enum pipe pipe = crtc->pipe;
Daniel Vetter72419202013-04-04 13:28:53 +02008995
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03008996 m_n->link_m = I915_READ(PCH_TRANS_LINK_M1(pipe));
8997 m_n->link_n = I915_READ(PCH_TRANS_LINK_N1(pipe));
8998 m_n->gmch_m = I915_READ(PCH_TRANS_DATA_M1(pipe))
8999 & ~TU_SIZE_MASK;
9000 m_n->gmch_n = I915_READ(PCH_TRANS_DATA_N1(pipe));
9001 m_n->tu = ((I915_READ(PCH_TRANS_DATA_M1(pipe))
9002 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9003}
9004
9005static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
9006 enum transcoder transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009007 struct intel_link_m_n *m_n,
9008 struct intel_link_m_n *m2_n2)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009009{
9010 struct drm_device *dev = crtc->base.dev;
9011 struct drm_i915_private *dev_priv = dev->dev_private;
9012 enum pipe pipe = crtc->pipe;
9013
9014 if (INTEL_INFO(dev)->gen >= 5) {
9015 m_n->link_m = I915_READ(PIPE_LINK_M1(transcoder));
9016 m_n->link_n = I915_READ(PIPE_LINK_N1(transcoder));
9017 m_n->gmch_m = I915_READ(PIPE_DATA_M1(transcoder))
9018 & ~TU_SIZE_MASK;
9019 m_n->gmch_n = I915_READ(PIPE_DATA_N1(transcoder));
9020 m_n->tu = ((I915_READ(PIPE_DATA_M1(transcoder))
9021 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009022 /* Read M2_N2 registers only for gen < 8 (M2_N2 available for
9023 * gen < 8) and if DRRS is supported (to make sure the
9024 * registers are not unnecessarily read).
9025 */
9026 if (m2_n2 && INTEL_INFO(dev)->gen < 8 &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +02009027 crtc->config->has_drrs) {
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009028 m2_n2->link_m = I915_READ(PIPE_LINK_M2(transcoder));
9029 m2_n2->link_n = I915_READ(PIPE_LINK_N2(transcoder));
9030 m2_n2->gmch_m = I915_READ(PIPE_DATA_M2(transcoder))
9031 & ~TU_SIZE_MASK;
9032 m2_n2->gmch_n = I915_READ(PIPE_DATA_N2(transcoder));
9033 m2_n2->tu = ((I915_READ(PIPE_DATA_M2(transcoder))
9034 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9035 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009036 } else {
9037 m_n->link_m = I915_READ(PIPE_LINK_M_G4X(pipe));
9038 m_n->link_n = I915_READ(PIPE_LINK_N_G4X(pipe));
9039 m_n->gmch_m = I915_READ(PIPE_DATA_M_G4X(pipe))
9040 & ~TU_SIZE_MASK;
9041 m_n->gmch_n = I915_READ(PIPE_DATA_N_G4X(pipe));
9042 m_n->tu = ((I915_READ(PIPE_DATA_M_G4X(pipe))
9043 & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
9044 }
9045}
9046
9047void intel_dp_get_m_n(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009048 struct intel_crtc_state *pipe_config)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009049{
Ander Conselvan de Oliveira681a8502015-01-15 14:55:24 +02009050 if (pipe_config->has_pch_encoder)
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009051 intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
9052 else
9053 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009054 &pipe_config->dp_m_n,
9055 &pipe_config->dp_m2_n2);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009056}
9057
Daniel Vetter72419202013-04-04 13:28:53 +02009058static void ironlake_get_fdi_m_n_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009059 struct intel_crtc_state *pipe_config)
Daniel Vetter72419202013-04-04 13:28:53 +02009060{
Ville Syrjäläeb14cb72013-09-10 17:02:54 +03009061 intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
Vandana Kannanb95af8b2014-08-05 07:51:23 -07009062 &pipe_config->fdi_m_n, NULL);
Daniel Vetter72419202013-04-04 13:28:53 +02009063}
9064
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009065static void skylake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009066 struct intel_crtc_state *pipe_config)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009067{
9068 struct drm_device *dev = crtc->base.dev;
9069 struct drm_i915_private *dev_priv = dev->dev_private;
Chandra Kondurua1b22782015-04-07 15:28:45 -07009070 struct intel_crtc_scaler_state *scaler_state = &pipe_config->scaler_state;
9071 uint32_t ps_ctrl = 0;
9072 int id = -1;
9073 int i;
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009074
Chandra Kondurua1b22782015-04-07 15:28:45 -07009075 /* find scaler attached to this pipe */
9076 for (i = 0; i < crtc->num_scalers; i++) {
9077 ps_ctrl = I915_READ(SKL_PS_CTRL(crtc->pipe, i));
9078 if (ps_ctrl & PS_SCALER_EN && !(ps_ctrl & PS_PLANE_SEL_MASK)) {
9079 id = i;
9080 pipe_config->pch_pfit.enabled = true;
9081 pipe_config->pch_pfit.pos = I915_READ(SKL_PS_WIN_POS(crtc->pipe, i));
9082 pipe_config->pch_pfit.size = I915_READ(SKL_PS_WIN_SZ(crtc->pipe, i));
9083 break;
9084 }
9085 }
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009086
Chandra Kondurua1b22782015-04-07 15:28:45 -07009087 scaler_state->scaler_id = id;
9088 if (id >= 0) {
9089 scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
9090 } else {
9091 scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009092 }
9093}
9094
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009095static void
9096skylake_get_initial_plane_config(struct intel_crtc *crtc,
9097 struct intel_initial_plane_config *plane_config)
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009098{
9099 struct drm_device *dev = crtc->base.dev;
9100 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau40f46282015-02-27 11:15:21 +00009101 u32 val, base, offset, stride_mult, tiling;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009102 int pipe = crtc->pipe;
9103 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009104 unsigned int aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009105 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009106 struct intel_framebuffer *intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009107
Damien Lespiaud9806c92015-01-21 14:07:19 +00009108 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009109 if (!intel_fb) {
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009110 DRM_DEBUG_KMS("failed to alloc fb\n");
9111 return;
9112 }
9113
Damien Lespiau1b842c82015-01-21 13:50:54 +00009114 fb = &intel_fb->base;
9115
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009116 val = I915_READ(PLANE_CTL(pipe, 0));
Damien Lespiau42a7b082015-02-05 19:35:13 +00009117 if (!(val & PLANE_CTL_ENABLE))
9118 goto error;
9119
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009120 pixel_format = val & PLANE_CTL_FORMAT_MASK;
9121 fourcc = skl_format_to_fourcc(pixel_format,
9122 val & PLANE_CTL_ORDER_RGBX,
9123 val & PLANE_CTL_ALPHA_MASK);
9124 fb->pixel_format = fourcc;
9125 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
9126
Damien Lespiau40f46282015-02-27 11:15:21 +00009127 tiling = val & PLANE_CTL_TILED_MASK;
9128 switch (tiling) {
9129 case PLANE_CTL_TILED_LINEAR:
9130 fb->modifier[0] = DRM_FORMAT_MOD_NONE;
9131 break;
9132 case PLANE_CTL_TILED_X:
9133 plane_config->tiling = I915_TILING_X;
9134 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9135 break;
9136 case PLANE_CTL_TILED_Y:
9137 fb->modifier[0] = I915_FORMAT_MOD_Y_TILED;
9138 break;
9139 case PLANE_CTL_TILED_YF:
9140 fb->modifier[0] = I915_FORMAT_MOD_Yf_TILED;
9141 break;
9142 default:
9143 MISSING_CASE(tiling);
9144 goto error;
9145 }
9146
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009147 base = I915_READ(PLANE_SURF(pipe, 0)) & 0xfffff000;
9148 plane_config->base = base;
9149
9150 offset = I915_READ(PLANE_OFFSET(pipe, 0));
9151
9152 val = I915_READ(PLANE_SIZE(pipe, 0));
9153 fb->height = ((val >> 16) & 0xfff) + 1;
9154 fb->width = ((val >> 0) & 0x1fff) + 1;
9155
9156 val = I915_READ(PLANE_STRIDE(pipe, 0));
Damien Lespiau40f46282015-02-27 11:15:21 +00009157 stride_mult = intel_fb_stride_alignment(dev, fb->modifier[0],
9158 fb->pixel_format);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009159 fb->pitches[0] = (val & 0x3ff) * stride_mult;
9160
9161 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009162 fb->pixel_format,
9163 fb->modifier[0]);
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009164
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009165 plane_config->size = fb->pitches[0] * aligned_height;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009166
9167 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9168 pipe_name(pipe), fb->width, fb->height,
9169 fb->bits_per_pixel, base, fb->pitches[0],
9170 plane_config->size);
9171
Damien Lespiau2d140302015-02-05 17:22:18 +00009172 plane_config->fb = intel_fb;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +00009173 return;
9174
9175error:
9176 kfree(fb);
9177}
9178
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009179static void ironlake_get_pfit_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009180 struct intel_crtc_state *pipe_config)
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009181{
9182 struct drm_device *dev = crtc->base.dev;
9183 struct drm_i915_private *dev_priv = dev->dev_private;
9184 uint32_t tmp;
9185
9186 tmp = I915_READ(PF_CTL(crtc->pipe));
9187
9188 if (tmp & PF_ENABLE) {
Chris Wilsonfd4daa92013-08-27 17:04:17 +01009189 pipe_config->pch_pfit.enabled = true;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009190 pipe_config->pch_pfit.pos = I915_READ(PF_WIN_POS(crtc->pipe));
9191 pipe_config->pch_pfit.size = I915_READ(PF_WIN_SZ(crtc->pipe));
Daniel Vettercb8b2a32013-06-01 17:16:23 +02009192
9193 /* We currently do not free assignements of panel fitters on
9194 * ivb/hsw (since we don't use the higher upscaling modes which
9195 * differentiates them) so just WARN about this case for now. */
9196 if (IS_GEN7(dev)) {
9197 WARN_ON((tmp & PF_PIPE_SEL_MASK_IVB) !=
9198 PF_PIPE_SEL_IVB(crtc->pipe));
9199 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009200 }
Jesse Barnes79e53942008-11-07 14:24:08 -08009201}
9202
Damien Lespiau5724dbd2015-01-20 12:51:52 +00009203static void
9204ironlake_get_initial_plane_config(struct intel_crtc *crtc,
9205 struct intel_initial_plane_config *plane_config)
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009206{
9207 struct drm_device *dev = crtc->base.dev;
9208 struct drm_i915_private *dev_priv = dev->dev_private;
9209 u32 val, base, offset;
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009210 int pipe = crtc->pipe;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009211 int fourcc, pixel_format;
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +00009212 unsigned int aligned_height;
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009213 struct drm_framebuffer *fb;
Damien Lespiau1b842c82015-01-21 13:50:54 +00009214 struct intel_framebuffer *intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009215
Damien Lespiau42a7b082015-02-05 19:35:13 +00009216 val = I915_READ(DSPCNTR(pipe));
9217 if (!(val & DISPLAY_PLANE_ENABLE))
9218 return;
9219
Damien Lespiaud9806c92015-01-21 14:07:19 +00009220 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
Damien Lespiau1b842c82015-01-21 13:50:54 +00009221 if (!intel_fb) {
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009222 DRM_DEBUG_KMS("failed to alloc fb\n");
9223 return;
9224 }
9225
Damien Lespiau1b842c82015-01-21 13:50:54 +00009226 fb = &intel_fb->base;
9227
Daniel Vetter18c52472015-02-10 17:16:09 +00009228 if (INTEL_INFO(dev)->gen >= 4) {
9229 if (val & DISPPLANE_TILED) {
Damien Lespiau49af4492015-01-20 12:51:44 +00009230 plane_config->tiling = I915_TILING_X;
Daniel Vetter18c52472015-02-10 17:16:09 +00009231 fb->modifier[0] = I915_FORMAT_MOD_X_TILED;
9232 }
9233 }
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009234
9235 pixel_format = val & DISPPLANE_PIXFORMAT_MASK;
Damien Lespiaub35d63f2015-01-20 12:51:50 +00009236 fourcc = i9xx_format_to_fourcc(pixel_format);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009237 fb->pixel_format = fourcc;
9238 fb->bits_per_pixel = drm_format_plane_cpp(fourcc, 0) * 8;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009239
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009240 base = I915_READ(DSPSURF(pipe)) & 0xfffff000;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009241 if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009242 offset = I915_READ(DSPOFFSET(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009243 } else {
Damien Lespiau49af4492015-01-20 12:51:44 +00009244 if (plane_config->tiling)
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009245 offset = I915_READ(DSPTILEOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009246 else
Damien Lespiauaeee5a42015-01-20 12:51:47 +00009247 offset = I915_READ(DSPLINOFF(pipe));
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009248 }
9249 plane_config->base = base;
9250
9251 val = I915_READ(PIPESRC(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009252 fb->width = ((val >> 16) & 0xfff) + 1;
9253 fb->height = ((val >> 0) & 0xfff) + 1;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009254
9255 val = I915_READ(DSPSTRIDE(pipe));
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009256 fb->pitches[0] = val & 0xffffffc0;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009257
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009258 aligned_height = intel_fb_align_height(dev, fb->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +00009259 fb->pixel_format,
9260 fb->modifier[0]);
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009261
Daniel Vetterf37b5c22015-02-10 23:12:27 +01009262 plane_config->size = fb->pitches[0] * aligned_height;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009263
Damien Lespiau2844a922015-01-20 12:51:48 +00009264 DRM_DEBUG_KMS("pipe %c with fb: size=%dx%d@%d, offset=%x, pitch %d, size 0x%x\n",
9265 pipe_name(pipe), fb->width, fb->height,
9266 fb->bits_per_pixel, base, fb->pitches[0],
9267 plane_config->size);
Damien Lespiaub113d5e2015-01-20 12:51:46 +00009268
Damien Lespiau2d140302015-02-05 17:22:18 +00009269 plane_config->fb = intel_fb;
Jesse Barnes4c6baa52014-03-07 08:57:50 -08009270}
9271
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009272static bool ironlake_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009273 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009274{
9275 struct drm_device *dev = crtc->base.dev;
9276 struct drm_i915_private *dev_priv = dev->dev_private;
9277 uint32_t tmp;
9278
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009279 if (!intel_display_power_is_enabled(dev_priv,
9280 POWER_DOMAIN_PIPE(crtc->pipe)))
Paulo Zanoni930e8c92014-07-04 13:38:34 -03009281 return false;
9282
Daniel Vettere143a212013-07-04 12:01:15 +02009283 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009284 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
Daniel Vettereccb1402013-05-22 00:50:22 +02009285
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009286 tmp = I915_READ(PIPECONF(crtc->pipe));
9287 if (!(tmp & PIPECONF_ENABLE))
9288 return false;
9289
Ville Syrjälä42571ae2013-09-06 23:29:00 +03009290 switch (tmp & PIPECONF_BPC_MASK) {
9291 case PIPECONF_6BPC:
9292 pipe_config->pipe_bpp = 18;
9293 break;
9294 case PIPECONF_8BPC:
9295 pipe_config->pipe_bpp = 24;
9296 break;
9297 case PIPECONF_10BPC:
9298 pipe_config->pipe_bpp = 30;
9299 break;
9300 case PIPECONF_12BPC:
9301 pipe_config->pipe_bpp = 36;
9302 break;
9303 default:
9304 break;
9305 }
9306
Daniel Vetterb5a9fa02014-04-24 23:54:49 +02009307 if (tmp & PIPECONF_COLOR_RANGE_SELECT)
9308 pipe_config->limited_color_range = true;
9309
Daniel Vetterab9412b2013-05-03 11:49:46 +02009310 if (I915_READ(PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
Daniel Vetter66e985c2013-06-05 13:34:20 +02009311 struct intel_shared_dpll *pll;
9312
Daniel Vetter88adfff2013-03-28 10:42:01 +01009313 pipe_config->has_pch_encoder = true;
9314
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009315 tmp = I915_READ(FDI_RX_CTL(crtc->pipe));
9316 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9317 FDI_DP_PORT_WIDTH_SHIFT) + 1;
Daniel Vetter72419202013-04-04 13:28:53 +02009318
9319 ironlake_get_fdi_m_n_config(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009320
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009321 if (HAS_PCH_IBX(dev_priv->dev)) {
Daniel Vetterd94ab062013-07-04 12:01:16 +02009322 pipe_config->shared_dpll =
9323 (enum intel_dpll_id) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009324 } else {
9325 tmp = I915_READ(PCH_DPLL_SEL);
9326 if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
9327 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_B;
9328 else
9329 pipe_config->shared_dpll = DPLL_ID_PCH_PLL_A;
9330 }
Daniel Vetter66e985c2013-06-05 13:34:20 +02009331
9332 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9333
9334 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9335 &pipe_config->dpll_hw_state));
Daniel Vetterc93f54c2013-06-27 19:47:19 +02009336
9337 tmp = pipe_config->dpll_hw_state.dpll;
9338 pipe_config->pixel_multiplier =
9339 ((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
9340 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
Ville Syrjälä18442d02013-09-13 16:00:08 +03009341
9342 ironlake_pch_clock_get(crtc, pipe_config);
Daniel Vetter6c49f242013-06-06 12:45:25 +02009343 } else {
9344 pipe_config->pixel_multiplier = 1;
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009345 }
9346
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009347 intel_get_pipe_timings(crtc, pipe_config);
9348
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009349 ironlake_get_pfit_config(crtc, pipe_config);
9350
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009351 return true;
9352}
9353
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009354static void assert_can_disable_lcpll(struct drm_i915_private *dev_priv)
9355{
9356 struct drm_device *dev = dev_priv->dev;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009357 struct intel_crtc *crtc;
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009358
Damien Lespiaud3fcc802014-05-13 23:32:22 +01009359 for_each_intel_crtc(dev, crtc)
Rob Clarke2c719b2014-12-15 13:56:32 -05009360 I915_STATE_WARN(crtc->active, "CRTC for pipe %c enabled\n",
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009361 pipe_name(crtc->pipe));
9362
Rob Clarke2c719b2014-12-15 13:56:32 -05009363 I915_STATE_WARN(I915_READ(HSW_PWR_WELL_DRIVER), "Power well on\n");
9364 I915_STATE_WARN(I915_READ(SPLL_CTL) & SPLL_PLL_ENABLE, "SPLL enabled\n");
9365 I915_STATE_WARN(I915_READ(WRPLL_CTL1) & WRPLL_PLL_ENABLE, "WRPLL1 enabled\n");
9366 I915_STATE_WARN(I915_READ(WRPLL_CTL2) & WRPLL_PLL_ENABLE, "WRPLL2 enabled\n");
9367 I915_STATE_WARN(I915_READ(PCH_PP_STATUS) & PP_ON, "Panel power on\n");
9368 I915_STATE_WARN(I915_READ(BLC_PWM_CPU_CTL2) & BLM_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009369 "CPU PWM1 enabled\n");
Paulo Zanonic5107b82014-07-04 11:50:30 -03009370 if (IS_HASWELL(dev))
Rob Clarke2c719b2014-12-15 13:56:32 -05009371 I915_STATE_WARN(I915_READ(HSW_BLC_PWM2_CTL) & BLM_PWM_ENABLE,
Paulo Zanonic5107b82014-07-04 11:50:30 -03009372 "CPU PWM2 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009373 I915_STATE_WARN(I915_READ(BLC_PWM_PCH_CTL1) & BLM_PCH_PWM_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009374 "PCH PWM1 enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009375 I915_STATE_WARN(I915_READ(UTIL_PIN_CTL) & UTIL_PIN_ENABLE,
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009376 "Utility pin enabled\n");
Rob Clarke2c719b2014-12-15 13:56:32 -05009377 I915_STATE_WARN(I915_READ(PCH_GTC_CTL) & PCH_GTC_ENABLE, "PCH GTC enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009378
Paulo Zanoni9926ada2014-04-01 19:39:47 -03009379 /*
9380 * In theory we can still leave IRQs enabled, as long as only the HPD
9381 * interrupts remain enabled. We used to check for that, but since it's
9382 * gen-specific and since we only disable LCPLL after we fully disable
9383 * the interrupts, the check below should be enough.
9384 */
Rob Clarke2c719b2014-12-15 13:56:32 -05009385 I915_STATE_WARN(intel_irqs_enabled(dev_priv), "IRQs enabled\n");
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009386}
9387
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009388static uint32_t hsw_read_dcomp(struct drm_i915_private *dev_priv)
9389{
9390 struct drm_device *dev = dev_priv->dev;
9391
9392 if (IS_HASWELL(dev))
9393 return I915_READ(D_COMP_HSW);
9394 else
9395 return I915_READ(D_COMP_BDW);
9396}
9397
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009398static void hsw_write_dcomp(struct drm_i915_private *dev_priv, uint32_t val)
9399{
9400 struct drm_device *dev = dev_priv->dev;
9401
9402 if (IS_HASWELL(dev)) {
9403 mutex_lock(&dev_priv->rps.hw_lock);
9404 if (sandybridge_pcode_write(dev_priv, GEN6_PCODE_WRITE_D_COMP,
9405 val))
Paulo Zanonif475dad2014-07-04 11:59:57 -03009406 DRM_ERROR("Failed to write to D_COMP\n");
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009407 mutex_unlock(&dev_priv->rps.hw_lock);
9408 } else {
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009409 I915_WRITE(D_COMP_BDW, val);
9410 POSTING_READ(D_COMP_BDW);
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009411 }
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009412}
9413
9414/*
9415 * This function implements pieces of two sequences from BSpec:
9416 * - Sequence for display software to disable LCPLL
9417 * - Sequence for display software to allow package C8+
9418 * The steps implemented here are just the steps that actually touch the LCPLL
9419 * register. Callers should take care of disabling all the display engine
9420 * functions, doing the mode unset, fixing interrupts, etc.
9421 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009422static void hsw_disable_lcpll(struct drm_i915_private *dev_priv,
9423 bool switch_to_fclk, bool allow_power_down)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009424{
9425 uint32_t val;
9426
9427 assert_can_disable_lcpll(dev_priv);
9428
9429 val = I915_READ(LCPLL_CTL);
9430
9431 if (switch_to_fclk) {
9432 val |= LCPLL_CD_SOURCE_FCLK;
9433 I915_WRITE(LCPLL_CTL, val);
9434
9435 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9436 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9437 DRM_ERROR("Switching to FCLK failed\n");
9438
9439 val = I915_READ(LCPLL_CTL);
9440 }
9441
9442 val |= LCPLL_PLL_DISABLE;
9443 I915_WRITE(LCPLL_CTL, val);
9444 POSTING_READ(LCPLL_CTL);
9445
9446 if (wait_for((I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK) == 0, 1))
9447 DRM_ERROR("LCPLL still locked\n");
9448
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009449 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009450 val |= D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009451 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009452 ndelay(100);
9453
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009454 if (wait_for((hsw_read_dcomp(dev_priv) & D_COMP_RCOMP_IN_PROGRESS) == 0,
9455 1))
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009456 DRM_ERROR("D_COMP RCOMP still in progress\n");
9457
9458 if (allow_power_down) {
9459 val = I915_READ(LCPLL_CTL);
9460 val |= LCPLL_POWER_DOWN_ALLOW;
9461 I915_WRITE(LCPLL_CTL, val);
9462 POSTING_READ(LCPLL_CTL);
9463 }
9464}
9465
9466/*
9467 * Fully restores LCPLL, disallowing power down and switching back to LCPLL
9468 * source.
9469 */
Paulo Zanoni6ff58d52013-09-24 13:52:57 -03009470static void hsw_restore_lcpll(struct drm_i915_private *dev_priv)
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009471{
9472 uint32_t val;
9473
9474 val = I915_READ(LCPLL_CTL);
9475
9476 if ((val & (LCPLL_PLL_LOCK | LCPLL_PLL_DISABLE | LCPLL_CD_SOURCE_FCLK |
9477 LCPLL_POWER_DOWN_ALLOW)) == LCPLL_PLL_LOCK)
9478 return;
9479
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009480 /*
9481 * Make sure we're not on PC8 state before disabling PC8, otherwise
9482 * we'll hang the machine. To prevent PC8 state, just enable force_wake.
Paulo Zanonia8a8bd52014-03-07 20:08:05 -03009483 */
Mika Kuoppala59bad942015-01-16 11:34:40 +02009484 intel_uncore_forcewake_get(dev_priv, FORCEWAKE_ALL);
Paulo Zanoni215733f2013-08-19 13:18:07 -03009485
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009486 if (val & LCPLL_POWER_DOWN_ALLOW) {
9487 val &= ~LCPLL_POWER_DOWN_ALLOW;
9488 I915_WRITE(LCPLL_CTL, val);
Daniel Vetter35d8f2e2013-08-21 23:38:08 +02009489 POSTING_READ(LCPLL_CTL);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009490 }
9491
Paulo Zanoni9ccd5ae2014-07-04 11:59:58 -03009492 val = hsw_read_dcomp(dev_priv);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009493 val |= D_COMP_COMP_FORCE;
9494 val &= ~D_COMP_COMP_DISABLE;
Paulo Zanoni3c4c9b82014-03-07 20:12:36 -03009495 hsw_write_dcomp(dev_priv, val);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009496
9497 val = I915_READ(LCPLL_CTL);
9498 val &= ~LCPLL_PLL_DISABLE;
9499 I915_WRITE(LCPLL_CTL, val);
9500
9501 if (wait_for(I915_READ(LCPLL_CTL) & LCPLL_PLL_LOCK, 5))
9502 DRM_ERROR("LCPLL not locked yet\n");
9503
9504 if (val & LCPLL_CD_SOURCE_FCLK) {
9505 val = I915_READ(LCPLL_CTL);
9506 val &= ~LCPLL_CD_SOURCE_FCLK;
9507 I915_WRITE(LCPLL_CTL, val);
9508
9509 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9510 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9511 DRM_ERROR("Switching back to LCPLL failed\n");
9512 }
Paulo Zanoni215733f2013-08-19 13:18:07 -03009513
Mika Kuoppala59bad942015-01-16 11:34:40 +02009514 intel_uncore_forcewake_put(dev_priv, FORCEWAKE_ALL);
Ville Syrjäläb6283052015-06-03 15:45:07 +03009515 intel_update_cdclk(dev_priv->dev);
Paulo Zanonibe256dc2013-07-23 11:19:26 -03009516}
9517
Paulo Zanoni765dab672014-03-07 20:08:18 -03009518/*
9519 * Package states C8 and deeper are really deep PC states that can only be
9520 * reached when all the devices on the system allow it, so even if the graphics
9521 * device allows PC8+, it doesn't mean the system will actually get to these
9522 * states. Our driver only allows PC8+ when going into runtime PM.
9523 *
9524 * The requirements for PC8+ are that all the outputs are disabled, the power
9525 * well is disabled and most interrupts are disabled, and these are also
9526 * requirements for runtime PM. When these conditions are met, we manually do
9527 * the other conditions: disable the interrupts, clocks and switch LCPLL refclk
9528 * to Fclk. If we're in PC8+ and we get an non-hotplug interrupt, we can hard
9529 * hang the machine.
9530 *
9531 * When we really reach PC8 or deeper states (not just when we allow it) we lose
9532 * the state of some registers, so when we come back from PC8+ we need to
9533 * restore this state. We don't get into PC8+ if we're not in RC6, so we don't
9534 * need to take care of the registers kept by RC6. Notice that this happens even
9535 * if we don't put the device in PCI D3 state (which is what currently happens
9536 * because of the runtime PM support).
9537 *
9538 * For more, read "Display Sequences for Package C8" on the hardware
9539 * documentation.
9540 */
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009541void hsw_enable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009542{
Paulo Zanonic67a4702013-08-19 13:18:09 -03009543 struct drm_device *dev = dev_priv->dev;
9544 uint32_t val;
9545
Paulo Zanonic67a4702013-08-19 13:18:09 -03009546 DRM_DEBUG_KMS("Enabling package C8+\n");
9547
Paulo Zanonic67a4702013-08-19 13:18:09 -03009548 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9549 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9550 val &= ~PCH_LP_PARTITION_LEVEL_DISABLE;
9551 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9552 }
9553
9554 lpt_disable_clkout_dp(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009555 hsw_disable_lcpll(dev_priv, true, true);
9556}
9557
Paulo Zanonia14cb6f2014-03-07 20:08:17 -03009558void hsw_disable_pc8(struct drm_i915_private *dev_priv)
Paulo Zanonic67a4702013-08-19 13:18:09 -03009559{
9560 struct drm_device *dev = dev_priv->dev;
9561 uint32_t val;
9562
Paulo Zanonic67a4702013-08-19 13:18:09 -03009563 DRM_DEBUG_KMS("Disabling package C8+\n");
9564
9565 hsw_restore_lcpll(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009566 lpt_init_pch_refclk(dev);
9567
9568 if (dev_priv->pch_id == INTEL_PCH_LPT_LP_DEVICE_ID_TYPE) {
9569 val = I915_READ(SOUTH_DSPCLK_GATE_D);
9570 val |= PCH_LP_PARTITION_LEVEL_DISABLE;
9571 I915_WRITE(SOUTH_DSPCLK_GATE_D, val);
9572 }
9573
9574 intel_prepare_ddi(dev);
Paulo Zanonic67a4702013-08-19 13:18:09 -03009575}
9576
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009577static void broxton_modeset_global_resources(struct drm_atomic_state *old_state)
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309578{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009579 struct drm_device *dev = old_state->dev;
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309580 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +03009581 int max_pixclk = intel_mode_max_pixclk(dev, NULL);
Vandana Kannanf8437dd12014-11-24 13:37:39 +05309582 int req_cdclk;
9583
9584 /* see the comment in valleyview_modeset_global_resources */
9585 if (WARN_ON(max_pixclk < 0))
9586 return;
9587
9588 req_cdclk = broxton_calc_cdclk(dev_priv, max_pixclk);
9589
9590 if (req_cdclk != dev_priv->cdclk_freq)
9591 broxton_set_cdclk(dev, req_cdclk);
9592}
9593
Ville Syrjäläb432e5c2015-06-03 15:45:13 +03009594/* compute the max rate for new configuration */
9595static int ilk_max_pixel_rate(struct drm_i915_private *dev_priv)
9596{
9597 struct drm_device *dev = dev_priv->dev;
9598 struct intel_crtc *intel_crtc;
9599 struct drm_crtc *crtc;
9600 int max_pixel_rate = 0;
9601 int pixel_rate;
9602
9603 for_each_crtc(dev, crtc) {
9604 if (!crtc->state->enable)
9605 continue;
9606
9607 intel_crtc = to_intel_crtc(crtc);
9608 pixel_rate = ilk_pipe_pixel_rate(intel_crtc->config);
9609
9610 /* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
9611 if (IS_BROADWELL(dev) && intel_crtc->config->ips_enabled)
9612 pixel_rate = DIV_ROUND_UP(pixel_rate * 100, 95);
9613
9614 max_pixel_rate = max(max_pixel_rate, pixel_rate);
9615 }
9616
9617 return max_pixel_rate;
9618}
9619
9620static void broadwell_set_cdclk(struct drm_device *dev, int cdclk)
9621{
9622 struct drm_i915_private *dev_priv = dev->dev_private;
9623 uint32_t val, data;
9624 int ret;
9625
9626 if (WARN((I915_READ(LCPLL_CTL) &
9627 (LCPLL_PLL_DISABLE | LCPLL_PLL_LOCK |
9628 LCPLL_CD_CLOCK_DISABLE | LCPLL_ROOT_CD_CLOCK_DISABLE |
9629 LCPLL_CD2X_CLOCK_DISABLE | LCPLL_POWER_DOWN_ALLOW |
9630 LCPLL_CD_SOURCE_FCLK)) != LCPLL_PLL_LOCK,
9631 "trying to change cdclk frequency with cdclk not enabled\n"))
9632 return;
9633
9634 mutex_lock(&dev_priv->rps.hw_lock);
9635 ret = sandybridge_pcode_write(dev_priv,
9636 BDW_PCODE_DISPLAY_FREQ_CHANGE_REQ, 0x0);
9637 mutex_unlock(&dev_priv->rps.hw_lock);
9638 if (ret) {
9639 DRM_ERROR("failed to inform pcode about cdclk change\n");
9640 return;
9641 }
9642
9643 val = I915_READ(LCPLL_CTL);
9644 val |= LCPLL_CD_SOURCE_FCLK;
9645 I915_WRITE(LCPLL_CTL, val);
9646
9647 if (wait_for_atomic_us(I915_READ(LCPLL_CTL) &
9648 LCPLL_CD_SOURCE_FCLK_DONE, 1))
9649 DRM_ERROR("Switching to FCLK failed\n");
9650
9651 val = I915_READ(LCPLL_CTL);
9652 val &= ~LCPLL_CLK_FREQ_MASK;
9653
9654 switch (cdclk) {
9655 case 450000:
9656 val |= LCPLL_CLK_FREQ_450;
9657 data = 0;
9658 break;
9659 case 540000:
9660 val |= LCPLL_CLK_FREQ_54O_BDW;
9661 data = 1;
9662 break;
9663 case 337500:
9664 val |= LCPLL_CLK_FREQ_337_5_BDW;
9665 data = 2;
9666 break;
9667 case 675000:
9668 val |= LCPLL_CLK_FREQ_675_BDW;
9669 data = 3;
9670 break;
9671 default:
9672 WARN(1, "invalid cdclk frequency\n");
9673 return;
9674 }
9675
9676 I915_WRITE(LCPLL_CTL, val);
9677
9678 val = I915_READ(LCPLL_CTL);
9679 val &= ~LCPLL_CD_SOURCE_FCLK;
9680 I915_WRITE(LCPLL_CTL, val);
9681
9682 if (wait_for_atomic_us((I915_READ(LCPLL_CTL) &
9683 LCPLL_CD_SOURCE_FCLK_DONE) == 0, 1))
9684 DRM_ERROR("Switching back to LCPLL failed\n");
9685
9686 mutex_lock(&dev_priv->rps.hw_lock);
9687 sandybridge_pcode_write(dev_priv, HSW_PCODE_DE_WRITE_FREQ_REQ, data);
9688 mutex_unlock(&dev_priv->rps.hw_lock);
9689
9690 intel_update_cdclk(dev);
9691
9692 WARN(cdclk != dev_priv->cdclk_freq,
9693 "cdclk requested %d kHz but got %d kHz\n",
9694 cdclk, dev_priv->cdclk_freq);
9695}
9696
9697static int broadwell_calc_cdclk(struct drm_i915_private *dev_priv,
9698 int max_pixel_rate)
9699{
9700 int cdclk;
9701
9702 /*
9703 * FIXME should also account for plane ratio
9704 * once 64bpp pixel formats are supported.
9705 */
9706 if (max_pixel_rate > 540000)
9707 cdclk = 675000;
9708 else if (max_pixel_rate > 450000)
9709 cdclk = 540000;
9710 else if (max_pixel_rate > 337500)
9711 cdclk = 450000;
9712 else
9713 cdclk = 337500;
9714
9715 /*
9716 * FIXME move the cdclk caclulation to
9717 * compute_config() so we can fail gracegully.
9718 */
9719 if (cdclk > dev_priv->max_cdclk_freq) {
9720 DRM_ERROR("requested cdclk (%d kHz) exceeds max (%d kHz)\n",
9721 cdclk, dev_priv->max_cdclk_freq);
9722 cdclk = dev_priv->max_cdclk_freq;
9723 }
9724
9725 return cdclk;
9726}
9727
9728static int broadwell_modeset_global_pipes(struct drm_atomic_state *state)
9729{
9730 struct drm_i915_private *dev_priv = to_i915(state->dev);
9731 struct drm_crtc *crtc;
9732 struct drm_crtc_state *crtc_state;
9733 int max_pixclk = ilk_max_pixel_rate(dev_priv);
9734 int cdclk, i;
9735
9736 cdclk = broadwell_calc_cdclk(dev_priv, max_pixclk);
9737
9738 if (cdclk == dev_priv->cdclk_freq)
9739 return 0;
9740
9741 /* add all active pipes to the state */
9742 for_each_crtc(state->dev, crtc) {
9743 if (!crtc->state->enable)
9744 continue;
9745
9746 crtc_state = drm_atomic_get_crtc_state(state, crtc);
9747 if (IS_ERR(crtc_state))
9748 return PTR_ERR(crtc_state);
9749 }
9750
9751 /* disable/enable all currently active pipes while we change cdclk */
9752 for_each_crtc_in_state(state, crtc, crtc_state, i)
9753 if (crtc_state->enable)
9754 crtc_state->mode_changed = true;
9755
9756 return 0;
9757}
9758
9759static void broadwell_modeset_global_resources(struct drm_atomic_state *state)
9760{
9761 struct drm_device *dev = state->dev;
9762 struct drm_i915_private *dev_priv = dev->dev_private;
9763 int max_pixel_rate = ilk_max_pixel_rate(dev_priv);
9764 int req_cdclk = broadwell_calc_cdclk(dev_priv, max_pixel_rate);
9765
9766 if (req_cdclk != dev_priv->cdclk_freq)
9767 broadwell_set_cdclk(dev, req_cdclk);
9768}
9769
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009770static int haswell_crtc_compute_clock(struct intel_crtc *crtc,
9771 struct intel_crtc_state *crtc_state)
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -03009772{
Ander Conselvan de Oliveira190f68c2015-01-15 14:55:23 +02009773 if (!intel_ddi_pll_select(crtc, crtc_state))
Paulo Zanoni6441ab52012-10-05 12:05:58 -03009774 return -EINVAL;
Daniel Vetter716c2e52014-06-25 22:02:02 +03009775
Ander Conselvan de Oliveirac7653192014-10-20 13:46:44 +03009776 crtc->lowfreq_avail = false;
Daniel Vetter644cef32014-04-24 23:55:07 +02009777
Daniel Vetterc8f7a0d2014-04-24 23:55:04 +02009778 return 0;
Jesse Barnes79e53942008-11-07 14:24:08 -08009779}
9780
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309781static void bxt_get_ddi_pll(struct drm_i915_private *dev_priv,
9782 enum port port,
9783 struct intel_crtc_state *pipe_config)
9784{
9785 switch (port) {
9786 case PORT_A:
9787 pipe_config->ddi_pll_sel = SKL_DPLL0;
9788 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9789 break;
9790 case PORT_B:
9791 pipe_config->ddi_pll_sel = SKL_DPLL1;
9792 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9793 break;
9794 case PORT_C:
9795 pipe_config->ddi_pll_sel = SKL_DPLL2;
9796 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9797 break;
9798 default:
9799 DRM_ERROR("Incorrect port type\n");
9800 }
9801}
9802
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009803static void skylake_get_ddi_pll(struct drm_i915_private *dev_priv,
9804 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009805 struct intel_crtc_state *pipe_config)
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009806{
Damien Lespiau3148ade2014-11-21 16:14:56 +00009807 u32 temp, dpll_ctl1;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009808
9809 temp = I915_READ(DPLL_CTRL2) & DPLL_CTRL2_DDI_CLK_SEL_MASK(port);
9810 pipe_config->ddi_pll_sel = temp >> (port * 3 + 1);
9811
9812 switch (pipe_config->ddi_pll_sel) {
Damien Lespiau3148ade2014-11-21 16:14:56 +00009813 case SKL_DPLL0:
9814 /*
9815 * On SKL the eDP DPLL (DPLL0 as we don't use SSC) is not part
9816 * of the shared DPLL framework and thus needs to be read out
9817 * separately
9818 */
9819 dpll_ctl1 = I915_READ(DPLL_CTRL1);
9820 pipe_config->dpll_hw_state.ctrl1 = dpll_ctl1 & 0x3f;
9821 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009822 case SKL_DPLL1:
9823 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL1;
9824 break;
9825 case SKL_DPLL2:
9826 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL2;
9827 break;
9828 case SKL_DPLL3:
9829 pipe_config->shared_dpll = DPLL_ID_SKL_DPLL3;
9830 break;
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009831 }
9832}
9833
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009834static void haswell_get_ddi_pll(struct drm_i915_private *dev_priv,
9835 enum port port,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009836 struct intel_crtc_state *pipe_config)
Damien Lespiau7d2c8172014-07-29 18:06:18 +01009837{
9838 pipe_config->ddi_pll_sel = I915_READ(PORT_CLK_SEL(port));
9839
9840 switch (pipe_config->ddi_pll_sel) {
9841 case PORT_CLK_SEL_WRPLL1:
9842 pipe_config->shared_dpll = DPLL_ID_WRPLL1;
9843 break;
9844 case PORT_CLK_SEL_WRPLL2:
9845 pipe_config->shared_dpll = DPLL_ID_WRPLL2;
9846 break;
9847 }
9848}
9849
Daniel Vetter26804af2014-06-25 22:01:55 +03009850static void haswell_get_ddi_port_state(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009851 struct intel_crtc_state *pipe_config)
Daniel Vetter26804af2014-06-25 22:01:55 +03009852{
9853 struct drm_device *dev = crtc->base.dev;
9854 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009855 struct intel_shared_dpll *pll;
Daniel Vetter26804af2014-06-25 22:01:55 +03009856 enum port port;
9857 uint32_t tmp;
9858
9859 tmp = I915_READ(TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
9860
9861 port = (tmp & TRANS_DDI_PORT_MASK) >> TRANS_DDI_PORT_SHIFT;
9862
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009863 if (IS_SKYLAKE(dev))
9864 skylake_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M3760b592014-08-22 09:49:11 +05309865 else if (IS_BROXTON(dev))
9866 bxt_get_ddi_pll(dev_priv, port, pipe_config);
Satheeshakrishna M96b7dfb2014-11-13 14:55:17 +00009867 else
9868 haswell_get_ddi_pll(dev_priv, port, pipe_config);
Daniel Vetter9cd86932014-06-25 22:01:57 +03009869
Daniel Vetterd452c5b2014-07-04 11:27:39 -03009870 if (pipe_config->shared_dpll >= 0) {
9871 pll = &dev_priv->shared_dplls[pipe_config->shared_dpll];
9872
9873 WARN_ON(!pll->get_hw_state(dev_priv, pll,
9874 &pipe_config->dpll_hw_state));
9875 }
9876
Daniel Vetter26804af2014-06-25 22:01:55 +03009877 /*
9878 * Haswell has only FDI/PCH transcoder A. It is which is connected to
9879 * DDI E. So just check whether this pipe is wired to DDI E and whether
9880 * the PCH transcoder is on.
9881 */
Damien Lespiauca370452013-12-03 13:56:24 +00009882 if (INTEL_INFO(dev)->gen < 9 &&
9883 (port == PORT_E) && I915_READ(LPT_TRANSCONF) & TRANS_ENABLE) {
Daniel Vetter26804af2014-06-25 22:01:55 +03009884 pipe_config->has_pch_encoder = true;
9885
9886 tmp = I915_READ(FDI_RX_CTL(PIPE_A));
9887 pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
9888 FDI_DP_PORT_WIDTH_SHIFT) + 1;
9889
9890 ironlake_get_fdi_m_n_config(crtc, pipe_config);
9891 }
9892}
9893
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009894static bool haswell_get_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +02009895 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009896{
9897 struct drm_device *dev = crtc->base.dev;
9898 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009899 enum intel_display_power_domain pfit_domain;
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009900 uint32_t tmp;
9901
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009902 if (!intel_display_power_is_enabled(dev_priv,
Imre Deakb5482bd2014-03-05 16:20:55 +02009903 POWER_DOMAIN_PIPE(crtc->pipe)))
9904 return false;
9905
Daniel Vettere143a212013-07-04 12:01:15 +02009906 pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
Daniel Vetterc0d43d62013-06-07 23:11:08 +02009907 pipe_config->shared_dpll = DPLL_ID_PRIVATE;
9908
Daniel Vettereccb1402013-05-22 00:50:22 +02009909 tmp = I915_READ(TRANS_DDI_FUNC_CTL(TRANSCODER_EDP));
9910 if (tmp & TRANS_DDI_FUNC_ENABLE) {
9911 enum pipe trans_edp_pipe;
9912 switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
9913 default:
9914 WARN(1, "unknown pipe linked to edp transcoder\n");
9915 case TRANS_DDI_EDP_INPUT_A_ONOFF:
9916 case TRANS_DDI_EDP_INPUT_A_ON:
9917 trans_edp_pipe = PIPE_A;
9918 break;
9919 case TRANS_DDI_EDP_INPUT_B_ONOFF:
9920 trans_edp_pipe = PIPE_B;
9921 break;
9922 case TRANS_DDI_EDP_INPUT_C_ONOFF:
9923 trans_edp_pipe = PIPE_C;
9924 break;
9925 }
9926
9927 if (trans_edp_pipe == crtc->pipe)
9928 pipe_config->cpu_transcoder = TRANSCODER_EDP;
9929 }
9930
Daniel Vetterf458ebb2014-09-30 10:56:39 +02009931 if (!intel_display_power_is_enabled(dev_priv,
Daniel Vettereccb1402013-05-22 00:50:22 +02009932 POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
Paulo Zanoni2bfce952013-04-18 16:35:40 -03009933 return false;
9934
Daniel Vettereccb1402013-05-22 00:50:22 +02009935 tmp = I915_READ(PIPECONF(pipe_config->cpu_transcoder));
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009936 if (!(tmp & PIPECONF_ENABLE))
9937 return false;
9938
Daniel Vetter26804af2014-06-25 22:01:55 +03009939 haswell_get_ddi_port_state(crtc, pipe_config);
Daniel Vetter627eb5a2013-04-29 19:33:42 +02009940
Daniel Vetter1bd1bd82013-04-29 21:56:12 +02009941 intel_get_pipe_timings(crtc, pipe_config);
9942
Chandra Kondurua1b22782015-04-07 15:28:45 -07009943 if (INTEL_INFO(dev)->gen >= 9) {
9944 skl_init_scalers(dev, crtc, pipe_config);
9945 }
9946
Daniel Vetter2fa2fe92013-05-07 23:34:16 +02009947 pfit_domain = POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe);
Chandra Konduruaf99ced2015-05-11 14:35:47 -07009948
9949 if (INTEL_INFO(dev)->gen >= 9) {
9950 pipe_config->scaler_state.scaler_id = -1;
9951 pipe_config->scaler_state.scaler_users &= ~(1 << SKL_CRTC_INDEX);
9952 }
9953
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009954 if (intel_display_power_is_enabled(dev_priv, pfit_domain)) {
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009955 if (INTEL_INFO(dev)->gen == 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009956 skylake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009957 else if (INTEL_INFO(dev)->gen < 9)
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009958 ironlake_get_pfit_config(crtc, pipe_config);
Jesse Barnesff6d9f52015-01-21 17:19:54 -08009959 else
9960 MISSING_CASE(INTEL_INFO(dev)->gen);
Jesse Barnesbd2e2442014-11-13 17:51:47 +00009961 }
Daniel Vetter88adfff2013-03-28 10:42:01 +01009962
Jesse Barnese59150d2014-01-07 13:30:45 -08009963 if (IS_HASWELL(dev))
9964 pipe_config->ips_enabled = hsw_crtc_supports_ips(crtc) &&
9965 (I915_READ(IPS_CTL) & IPS_ENABLE);
Paulo Zanoni42db64e2013-05-31 16:33:22 -03009966
Clint Taylorebb69c92014-09-30 10:30:22 -07009967 if (pipe_config->cpu_transcoder != TRANSCODER_EDP) {
9968 pipe_config->pixel_multiplier =
9969 I915_READ(PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
9970 } else {
9971 pipe_config->pixel_multiplier = 1;
9972 }
Daniel Vetter6c49f242013-06-06 12:45:25 +02009973
Daniel Vetter0e8ffe12013-03-28 10:42:00 +01009974 return true;
9975}
9976
Chris Wilson560b85b2010-08-07 11:01:38 +01009977static void i845_update_cursor(struct drm_crtc *crtc, u32 base)
9978{
9979 struct drm_device *dev = crtc->dev;
9980 struct drm_i915_private *dev_priv = dev->dev_private;
9981 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ville Syrjälädc41c152014-08-13 11:57:05 +03009982 uint32_t cntl = 0, size = 0;
Chris Wilson560b85b2010-08-07 11:01:38 +01009983
Ville Syrjälädc41c152014-08-13 11:57:05 +03009984 if (base) {
Matt Roper3dd512f2015-02-27 10:12:00 -08009985 unsigned int width = intel_crtc->base.cursor->state->crtc_w;
9986 unsigned int height = intel_crtc->base.cursor->state->crtc_h;
Ville Syrjälädc41c152014-08-13 11:57:05 +03009987 unsigned int stride = roundup_pow_of_two(width) * 4;
9988
9989 switch (stride) {
9990 default:
9991 WARN_ONCE(1, "Invalid cursor width/stride, width=%u, stride=%u\n",
9992 width, stride);
9993 stride = 256;
9994 /* fallthrough */
9995 case 256:
9996 case 512:
9997 case 1024:
9998 case 2048:
9999 break;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010000 }
10001
Ville Syrjälädc41c152014-08-13 11:57:05 +030010002 cntl |= CURSOR_ENABLE |
10003 CURSOR_GAMMA_ENABLE |
10004 CURSOR_FORMAT_ARGB |
10005 CURSOR_STRIDE(stride);
10006
10007 size = (height << 12) | width;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010008 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010009
Ville Syrjälädc41c152014-08-13 11:57:05 +030010010 if (intel_crtc->cursor_cntl != 0 &&
10011 (intel_crtc->cursor_base != base ||
10012 intel_crtc->cursor_size != size ||
10013 intel_crtc->cursor_cntl != cntl)) {
10014 /* On these chipsets we can only modify the base/size/stride
10015 * whilst the cursor is disabled.
10016 */
10017 I915_WRITE(_CURACNTR, 0);
10018 POSTING_READ(_CURACNTR);
10019 intel_crtc->cursor_cntl = 0;
10020 }
10021
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010022 if (intel_crtc->cursor_base != base) {
Ville Syrjälädc41c152014-08-13 11:57:05 +030010023 I915_WRITE(_CURABASE, base);
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010024 intel_crtc->cursor_base = base;
10025 }
Ville Syrjälädc41c152014-08-13 11:57:05 +030010026
10027 if (intel_crtc->cursor_size != size) {
10028 I915_WRITE(CURSIZE, size);
10029 intel_crtc->cursor_size = size;
10030 }
10031
Chris Wilson4b0e3332014-05-30 16:35:26 +030010032 if (intel_crtc->cursor_cntl != cntl) {
10033 I915_WRITE(_CURACNTR, cntl);
10034 POSTING_READ(_CURACNTR);
10035 intel_crtc->cursor_cntl = cntl;
10036 }
Chris Wilson560b85b2010-08-07 11:01:38 +010010037}
10038
10039static void i9xx_update_cursor(struct drm_crtc *crtc, u32 base)
10040{
10041 struct drm_device *dev = crtc->dev;
10042 struct drm_i915_private *dev_priv = dev->dev_private;
10043 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10044 int pipe = intel_crtc->pipe;
Chris Wilson4b0e3332014-05-30 16:35:26 +030010045 uint32_t cntl;
Chris Wilson560b85b2010-08-07 11:01:38 +010010046
Chris Wilson4b0e3332014-05-30 16:35:26 +030010047 cntl = 0;
10048 if (base) {
10049 cntl = MCURSOR_GAMMA_ENABLE;
Matt Roper3dd512f2015-02-27 10:12:00 -080010050 switch (intel_crtc->base.cursor->state->crtc_w) {
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010051 case 64:
10052 cntl |= CURSOR_MODE_64_ARGB_AX;
10053 break;
10054 case 128:
10055 cntl |= CURSOR_MODE_128_ARGB_AX;
10056 break;
10057 case 256:
10058 cntl |= CURSOR_MODE_256_ARGB_AX;
10059 break;
10060 default:
Matt Roper3dd512f2015-02-27 10:12:00 -080010061 MISSING_CASE(intel_crtc->base.cursor->state->crtc_w);
Sagar Kamble4726e0b2014-03-10 17:06:23 +053010062 return;
Chris Wilson560b85b2010-08-07 11:01:38 +010010063 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010064 cntl |= pipe << 28; /* Connect to correct pipe */
Ville Syrjälä47bf17a2014-09-12 20:53:33 +030010065
10066 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
10067 cntl |= CURSOR_PIPE_CSC_ENABLE;
Chris Wilson560b85b2010-08-07 11:01:38 +010010068 }
Chris Wilson4b0e3332014-05-30 16:35:26 +030010069
Matt Roper8e7d6882015-01-21 16:35:41 -080010070 if (crtc->cursor->state->rotation == BIT(DRM_ROTATE_180))
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010071 cntl |= CURSOR_ROTATE_180;
10072
Chris Wilson4b0e3332014-05-30 16:35:26 +030010073 if (intel_crtc->cursor_cntl != cntl) {
10074 I915_WRITE(CURCNTR(pipe), cntl);
10075 POSTING_READ(CURCNTR(pipe));
10076 intel_crtc->cursor_cntl = cntl;
10077 }
10078
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010079 /* and commit changes on next vblank */
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010080 I915_WRITE(CURBASE(pipe), base);
10081 POSTING_READ(CURBASE(pipe));
Ville Syrjälä99d1f382014-09-12 20:53:32 +030010082
10083 intel_crtc->cursor_base = base;
Jesse Barnes65a21cd2011-10-12 11:10:21 -070010084}
10085
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010086/* If no-part of the cursor is visible on the framebuffer, then the GPU may hang... */
Chris Wilson6b383a72010-09-13 13:54:26 +010010087static void intel_crtc_update_cursor(struct drm_crtc *crtc,
10088 bool on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010089{
10090 struct drm_device *dev = crtc->dev;
10091 struct drm_i915_private *dev_priv = dev->dev_private;
10092 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10093 int pipe = intel_crtc->pipe;
Matt Roper3d7d6512014-06-10 08:28:13 -070010094 int x = crtc->cursor_x;
10095 int y = crtc->cursor_y;
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010096 u32 base = 0, pos = 0;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010097
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010098 if (on)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010099 base = intel_crtc->cursor_addr;
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010100
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010101 if (x >= intel_crtc->config->pipe_src_w)
Ville Syrjäläd6e4db12013-09-04 18:25:31 +030010102 base = 0;
10103
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010104 if (y >= intel_crtc->config->pipe_src_h)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010105 base = 0;
10106
10107 if (x < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010108 if (x + intel_crtc->base.cursor->state->crtc_w <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010109 base = 0;
10110
10111 pos |= CURSOR_POS_SIGN << CURSOR_X_SHIFT;
10112 x = -x;
10113 }
10114 pos |= x << CURSOR_X_SHIFT;
10115
10116 if (y < 0) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010117 if (y + intel_crtc->base.cursor->state->crtc_h <= 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010118 base = 0;
10119
10120 pos |= CURSOR_POS_SIGN << CURSOR_Y_SHIFT;
10121 y = -y;
10122 }
10123 pos |= y << CURSOR_Y_SHIFT;
10124
Chris Wilson4b0e3332014-05-30 16:35:26 +030010125 if (base == 0 && intel_crtc->cursor_base == 0)
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010126 return;
10127
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010128 I915_WRITE(CURPOS(pipe), pos);
10129
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010130 /* ILK+ do this automagically */
10131 if (HAS_GMCH_DISPLAY(dev) &&
Matt Roper8e7d6882015-01-21 16:35:41 -080010132 crtc->cursor->state->rotation == BIT(DRM_ROTATE_180)) {
Matt Roper3dd512f2015-02-27 10:12:00 -080010133 base += (intel_crtc->base.cursor->state->crtc_h *
10134 intel_crtc->base.cursor->state->crtc_w - 1) * 4;
Ville Syrjälä4398ad42014-10-23 07:41:34 -070010135 }
10136
Ville Syrjälä8ac54662014-08-12 19:39:54 +030010137 if (IS_845G(dev) || IS_I865G(dev))
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030010138 i845_update_cursor(crtc, base);
10139 else
10140 i9xx_update_cursor(crtc, base);
Chris Wilsoncda4b7d2010-07-09 08:45:04 +010010141}
10142
Ville Syrjälädc41c152014-08-13 11:57:05 +030010143static bool cursor_size_ok(struct drm_device *dev,
10144 uint32_t width, uint32_t height)
10145{
10146 if (width == 0 || height == 0)
10147 return false;
10148
10149 /*
10150 * 845g/865g are special in that they are only limited by
10151 * the width of their cursors, the height is arbitrary up to
10152 * the precision of the register. Everything else requires
10153 * square cursors, limited to a few power-of-two sizes.
10154 */
10155 if (IS_845G(dev) || IS_I865G(dev)) {
10156 if ((width & 63) != 0)
10157 return false;
10158
10159 if (width > (IS_845G(dev) ? 64 : 512))
10160 return false;
10161
10162 if (height > 1023)
10163 return false;
10164 } else {
10165 switch (width | height) {
10166 case 256:
10167 case 128:
10168 if (IS_GEN2(dev))
10169 return false;
10170 case 64:
10171 break;
10172 default:
10173 return false;
10174 }
10175 }
10176
10177 return true;
10178}
10179
Jesse Barnes79e53942008-11-07 14:24:08 -080010180static void intel_crtc_gamma_set(struct drm_crtc *crtc, u16 *red, u16 *green,
James Simmons72034252010-08-03 01:33:19 +010010181 u16 *blue, uint32_t start, uint32_t size)
Jesse Barnes79e53942008-11-07 14:24:08 -080010182{
James Simmons72034252010-08-03 01:33:19 +010010183 int end = (start + size > 256) ? 256 : start + size, i;
Jesse Barnes79e53942008-11-07 14:24:08 -080010184 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080010185
James Simmons72034252010-08-03 01:33:19 +010010186 for (i = start; i < end; i++) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010187 intel_crtc->lut_r[i] = red[i] >> 8;
10188 intel_crtc->lut_g[i] = green[i] >> 8;
10189 intel_crtc->lut_b[i] = blue[i] >> 8;
10190 }
10191
10192 intel_crtc_load_lut(crtc);
10193}
10194
Jesse Barnes79e53942008-11-07 14:24:08 -080010195/* VESA 640x480x72Hz mode to set on the pipe */
10196static struct drm_display_mode load_detect_mode = {
10197 DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
10198 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
10199};
10200
Daniel Vettera8bb6812014-02-10 18:00:39 +010010201struct drm_framebuffer *
10202__intel_framebuffer_create(struct drm_device *dev,
10203 struct drm_mode_fb_cmd2 *mode_cmd,
10204 struct drm_i915_gem_object *obj)
Chris Wilsond2dff872011-04-19 08:36:26 +010010205{
10206 struct intel_framebuffer *intel_fb;
10207 int ret;
10208
10209 intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
10210 if (!intel_fb) {
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010211 drm_gem_object_unreference(&obj->base);
Chris Wilsond2dff872011-04-19 08:36:26 +010010212 return ERR_PTR(-ENOMEM);
10213 }
10214
10215 ret = intel_framebuffer_init(dev, intel_fb, mode_cmd, obj);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010216 if (ret)
10217 goto err;
Chris Wilsond2dff872011-04-19 08:36:26 +010010218
10219 return &intel_fb->base;
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010220err:
Alexey Khoroshilov6ccb81f2014-11-08 01:41:23 +030010221 drm_gem_object_unreference(&obj->base);
Daniel Vetterdd4916c2013-10-09 21:23:51 +020010222 kfree(intel_fb);
10223
10224 return ERR_PTR(ret);
Chris Wilsond2dff872011-04-19 08:36:26 +010010225}
10226
Daniel Vetterb5ea6422014-03-02 21:18:00 +010010227static struct drm_framebuffer *
Daniel Vettera8bb6812014-02-10 18:00:39 +010010228intel_framebuffer_create(struct drm_device *dev,
10229 struct drm_mode_fb_cmd2 *mode_cmd,
10230 struct drm_i915_gem_object *obj)
10231{
10232 struct drm_framebuffer *fb;
10233 int ret;
10234
10235 ret = i915_mutex_lock_interruptible(dev);
10236 if (ret)
10237 return ERR_PTR(ret);
10238 fb = __intel_framebuffer_create(dev, mode_cmd, obj);
10239 mutex_unlock(&dev->struct_mutex);
10240
10241 return fb;
10242}
10243
Chris Wilsond2dff872011-04-19 08:36:26 +010010244static u32
10245intel_framebuffer_pitch_for_width(int width, int bpp)
10246{
10247 u32 pitch = DIV_ROUND_UP(width * bpp, 8);
10248 return ALIGN(pitch, 64);
10249}
10250
10251static u32
10252intel_framebuffer_size_for_mode(struct drm_display_mode *mode, int bpp)
10253{
10254 u32 pitch = intel_framebuffer_pitch_for_width(mode->hdisplay, bpp);
Fabian Frederick1267a262014-07-01 20:39:41 +020010255 return PAGE_ALIGN(pitch * mode->vdisplay);
Chris Wilsond2dff872011-04-19 08:36:26 +010010256}
10257
10258static struct drm_framebuffer *
10259intel_framebuffer_create_for_mode(struct drm_device *dev,
10260 struct drm_display_mode *mode,
10261 int depth, int bpp)
10262{
10263 struct drm_i915_gem_object *obj;
Chris Wilson0fed39b2012-11-05 22:25:07 +000010264 struct drm_mode_fb_cmd2 mode_cmd = { 0 };
Chris Wilsond2dff872011-04-19 08:36:26 +010010265
10266 obj = i915_gem_alloc_object(dev,
10267 intel_framebuffer_size_for_mode(mode, bpp));
10268 if (obj == NULL)
10269 return ERR_PTR(-ENOMEM);
10270
10271 mode_cmd.width = mode->hdisplay;
10272 mode_cmd.height = mode->vdisplay;
Jesse Barnes308e5bc2011-11-14 14:51:28 -080010273 mode_cmd.pitches[0] = intel_framebuffer_pitch_for_width(mode_cmd.width,
10274 bpp);
Dave Airlie5ca0c342012-02-23 15:33:40 +000010275 mode_cmd.pixel_format = drm_mode_legacy_fb_format(bpp, depth);
Chris Wilsond2dff872011-04-19 08:36:26 +010010276
10277 return intel_framebuffer_create(dev, &mode_cmd, obj);
10278}
10279
10280static struct drm_framebuffer *
10281mode_fits_in_fbdev(struct drm_device *dev,
10282 struct drm_display_mode *mode)
10283{
Daniel Vetter4520f532013-10-09 09:18:51 +020010284#ifdef CONFIG_DRM_I915_FBDEV
Chris Wilsond2dff872011-04-19 08:36:26 +010010285 struct drm_i915_private *dev_priv = dev->dev_private;
10286 struct drm_i915_gem_object *obj;
10287 struct drm_framebuffer *fb;
10288
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010289 if (!dev_priv->fbdev)
10290 return NULL;
10291
10292 if (!dev_priv->fbdev->fb)
Chris Wilsond2dff872011-04-19 08:36:26 +010010293 return NULL;
10294
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010295 obj = dev_priv->fbdev->fb->obj;
Daniel Vetter4c0e5522014-02-14 16:35:54 +010010296 BUG_ON(!obj);
Chris Wilsond2dff872011-04-19 08:36:26 +010010297
Jesse Barnes8bcd4552014-02-07 12:10:38 -080010298 fb = &dev_priv->fbdev->fb->base;
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010299 if (fb->pitches[0] < intel_framebuffer_pitch_for_width(mode->hdisplay,
10300 fb->bits_per_pixel))
Chris Wilsond2dff872011-04-19 08:36:26 +010010301 return NULL;
10302
Ville Syrjälä01f2c772011-12-20 00:06:49 +020010303 if (obj->base.size < mode->vdisplay * fb->pitches[0])
Chris Wilsond2dff872011-04-19 08:36:26 +010010304 return NULL;
10305
10306 return fb;
Daniel Vetter4520f532013-10-09 09:18:51 +020010307#else
10308 return NULL;
10309#endif
Chris Wilsond2dff872011-04-19 08:36:26 +010010310}
10311
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010312static int intel_modeset_setup_plane_state(struct drm_atomic_state *state,
10313 struct drm_crtc *crtc,
10314 struct drm_display_mode *mode,
10315 struct drm_framebuffer *fb,
10316 int x, int y)
10317{
10318 struct drm_plane_state *plane_state;
10319 int hdisplay, vdisplay;
10320 int ret;
10321
10322 plane_state = drm_atomic_get_plane_state(state, crtc->primary);
10323 if (IS_ERR(plane_state))
10324 return PTR_ERR(plane_state);
10325
10326 if (mode)
10327 drm_crtc_get_hv_timing(mode, &hdisplay, &vdisplay);
10328 else
10329 hdisplay = vdisplay = 0;
10330
10331 ret = drm_atomic_set_crtc_for_plane(plane_state, fb ? crtc : NULL);
10332 if (ret)
10333 return ret;
10334 drm_atomic_set_fb_for_plane(plane_state, fb);
10335 plane_state->crtc_x = 0;
10336 plane_state->crtc_y = 0;
10337 plane_state->crtc_w = hdisplay;
10338 plane_state->crtc_h = vdisplay;
10339 plane_state->src_x = x << 16;
10340 plane_state->src_y = y << 16;
10341 plane_state->src_w = hdisplay << 16;
10342 plane_state->src_h = vdisplay << 16;
10343
10344 return 0;
10345}
10346
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010347bool intel_get_load_detect_pipe(struct drm_connector *connector,
Chris Wilson71731882011-04-19 23:10:58 +010010348 struct drm_display_mode *mode,
Rob Clark51fd3712013-11-19 12:10:12 -050010349 struct intel_load_detect_pipe *old,
10350 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010351{
10352 struct intel_crtc *intel_crtc;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010353 struct intel_encoder *intel_encoder =
10354 intel_attached_encoder(connector);
Jesse Barnes79e53942008-11-07 14:24:08 -080010355 struct drm_crtc *possible_crtc;
Chris Wilson4ef69c72010-09-09 15:14:28 +010010356 struct drm_encoder *encoder = &intel_encoder->base;
Jesse Barnes79e53942008-11-07 14:24:08 -080010357 struct drm_crtc *crtc = NULL;
10358 struct drm_device *dev = encoder->dev;
Daniel Vetter94352cf2012-07-05 22:51:56 +020010359 struct drm_framebuffer *fb;
Rob Clark51fd3712013-11-19 12:10:12 -050010360 struct drm_mode_config *config = &dev->mode_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010361 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010362 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010363 struct intel_crtc_state *crtc_state;
Rob Clark51fd3712013-11-19 12:10:12 -050010364 int ret, i = -1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010365
Chris Wilsond2dff872011-04-19 08:36:26 +010010366 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010367 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010368 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010369
Rob Clark51fd3712013-11-19 12:10:12 -050010370retry:
10371 ret = drm_modeset_lock(&config->connection_mutex, ctx);
10372 if (ret)
10373 goto fail_unlock;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020010374
Jesse Barnes79e53942008-11-07 14:24:08 -080010375 /*
10376 * Algorithm gets a little messy:
Chris Wilson7a5e4802011-04-19 23:21:12 +010010377 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010378 * - if the connector already has an assigned crtc, use it (but make
10379 * sure it's on first)
Chris Wilson7a5e4802011-04-19 23:21:12 +010010380 *
Jesse Barnes79e53942008-11-07 14:24:08 -080010381 * - try to find the first unused crtc that can drive this connector,
10382 * and use that if we find one
Jesse Barnes79e53942008-11-07 14:24:08 -080010383 */
10384
10385 /* See if we already have a CRTC for this connector */
10386 if (encoder->crtc) {
10387 crtc = encoder->crtc;
Chris Wilson8261b192011-04-19 23:18:09 +010010388
Rob Clark51fd3712013-11-19 12:10:12 -050010389 ret = drm_modeset_lock(&crtc->mutex, ctx);
10390 if (ret)
10391 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010392 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10393 if (ret)
10394 goto fail_unlock;
Daniel Vetter7b240562012-12-12 00:35:33 +010010395
Daniel Vetter24218aa2012-08-12 19:27:11 +020010396 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010397 old->load_detect_temp = false;
10398
10399 /* Make sure the crtc and connector are running */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010400 if (connector->dpms != DRM_MODE_DPMS_ON)
10401 connector->funcs->dpms(connector, DRM_MODE_DPMS_ON);
Chris Wilson8261b192011-04-19 23:18:09 +010010402
Chris Wilson71731882011-04-19 23:10:58 +010010403 return true;
Jesse Barnes79e53942008-11-07 14:24:08 -080010404 }
10405
10406 /* Find an unused one (if possible) */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010407 for_each_crtc(dev, possible_crtc) {
Jesse Barnes79e53942008-11-07 14:24:08 -080010408 i++;
10409 if (!(encoder->possible_crtcs & (1 << i)))
10410 continue;
Matt Roper83d65732015-02-25 13:12:16 -080010411 if (possible_crtc->state->enable)
Ville Syrjäläa4592492014-08-11 13:15:36 +030010412 continue;
10413 /* This can occur when applying the pipe A quirk on resume. */
10414 if (to_intel_crtc(possible_crtc)->new_enabled)
10415 continue;
10416
10417 crtc = possible_crtc;
10418 break;
Jesse Barnes79e53942008-11-07 14:24:08 -080010419 }
10420
10421 /*
10422 * If we didn't find an unused CRTC, don't use any.
10423 */
10424 if (!crtc) {
Chris Wilson71731882011-04-19 23:10:58 +010010425 DRM_DEBUG_KMS("no pipe available for load-detect\n");
Rob Clark51fd3712013-11-19 12:10:12 -050010426 goto fail_unlock;
Jesse Barnes79e53942008-11-07 14:24:08 -080010427 }
10428
Rob Clark51fd3712013-11-19 12:10:12 -050010429 ret = drm_modeset_lock(&crtc->mutex, ctx);
10430 if (ret)
10431 goto fail_unlock;
Daniel Vetter4d02e2d2014-11-11 10:12:00 +010010432 ret = drm_modeset_lock(&crtc->primary->mutex, ctx);
10433 if (ret)
10434 goto fail_unlock;
Daniel Vetterfc303102012-07-09 10:40:58 +020010435 intel_encoder->new_crtc = to_intel_crtc(crtc);
10436 to_intel_connector(connector)->new_encoder = intel_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010437
10438 intel_crtc = to_intel_crtc(crtc);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010439 intel_crtc->new_enabled = true;
Daniel Vetter24218aa2012-08-12 19:27:11 +020010440 old->dpms_mode = connector->dpms;
Chris Wilson8261b192011-04-19 23:18:09 +010010441 old->load_detect_temp = true;
Chris Wilsond2dff872011-04-19 08:36:26 +010010442 old->release_fb = NULL;
Jesse Barnes79e53942008-11-07 14:24:08 -080010443
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010444 state = drm_atomic_state_alloc(dev);
10445 if (!state)
10446 return false;
10447
10448 state->acquire_ctx = ctx;
10449
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010450 connector_state = drm_atomic_get_connector_state(state, connector);
10451 if (IS_ERR(connector_state)) {
10452 ret = PTR_ERR(connector_state);
10453 goto fail;
10454 }
10455
10456 connector_state->crtc = crtc;
10457 connector_state->best_encoder = &intel_encoder->base;
10458
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010459 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10460 if (IS_ERR(crtc_state)) {
10461 ret = PTR_ERR(crtc_state);
10462 goto fail;
10463 }
10464
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010465 crtc_state->base.active = crtc_state->base.enable = true;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010466
Chris Wilson64927112011-04-20 07:25:26 +010010467 if (!mode)
10468 mode = &load_detect_mode;
Jesse Barnes79e53942008-11-07 14:24:08 -080010469
Chris Wilsond2dff872011-04-19 08:36:26 +010010470 /* We need a framebuffer large enough to accommodate all accesses
10471 * that the plane may generate whilst we perform load detection.
10472 * We can not rely on the fbcon either being present (we get called
10473 * during its initialisation to detect all boot displays, or it may
10474 * not even exist) or that it is large enough to satisfy the
10475 * requested mode.
10476 */
Daniel Vetter94352cf2012-07-05 22:51:56 +020010477 fb = mode_fits_in_fbdev(dev, mode);
10478 if (fb == NULL) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010479 DRM_DEBUG_KMS("creating tmp fb for load-detection\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010480 fb = intel_framebuffer_create_for_mode(dev, mode, 24, 32);
10481 old->release_fb = fb;
Chris Wilsond2dff872011-04-19 08:36:26 +010010482 } else
10483 DRM_DEBUG_KMS("reusing fbdev for load-detection framebuffer\n");
Daniel Vetter94352cf2012-07-05 22:51:56 +020010484 if (IS_ERR(fb)) {
Chris Wilsond2dff872011-04-19 08:36:26 +010010485 DRM_DEBUG_KMS("failed to allocate framebuffer for load-detection\n");
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010486 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010487 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010488
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010489 ret = intel_modeset_setup_plane_state(state, crtc, mode, fb, 0, 0);
10490 if (ret)
10491 goto fail;
10492
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030010493 drm_mode_copy(&crtc_state->base.mode, mode);
10494
10495 if (intel_set_mode(crtc, state)) {
Chris Wilson64927112011-04-20 07:25:26 +010010496 DRM_DEBUG_KMS("failed to set mode on load-detect pipe\n");
Chris Wilsond2dff872011-04-19 08:36:26 +010010497 if (old->release_fb)
10498 old->release_fb->funcs->destroy(old->release_fb);
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010499 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080010500 }
Daniel Vetter9128b042015-03-03 17:31:21 +010010501 crtc->primary->crtc = crtc;
Chris Wilson71731882011-04-19 23:10:58 +010010502
Jesse Barnes79e53942008-11-07 14:24:08 -080010503 /* let the connector get through one full cycle before testing */
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010504 intel_wait_for_vblank(dev, intel_crtc->pipe);
Chris Wilson71731882011-04-19 23:10:58 +010010505 return true;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010506
10507 fail:
Matt Roper83d65732015-02-25 13:12:16 -080010508 intel_crtc->new_enabled = crtc->state->enable;
Rob Clark51fd3712013-11-19 12:10:12 -050010509fail_unlock:
Ander Conselvan de Oliveirae5d958e2015-04-21 17:12:57 +030010510 drm_atomic_state_free(state);
10511 state = NULL;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010512
Rob Clark51fd3712013-11-19 12:10:12 -050010513 if (ret == -EDEADLK) {
10514 drm_modeset_backoff(ctx);
10515 goto retry;
10516 }
10517
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010518 return false;
Jesse Barnes79e53942008-11-07 14:24:08 -080010519}
10520
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010521void intel_release_load_detect_pipe(struct drm_connector *connector,
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020010522 struct intel_load_detect_pipe *old,
10523 struct drm_modeset_acquire_ctx *ctx)
Jesse Barnes79e53942008-11-07 14:24:08 -080010524{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010525 struct drm_device *dev = connector->dev;
Daniel Vetterd2434ab2012-08-12 21:20:10 +020010526 struct intel_encoder *intel_encoder =
10527 intel_attached_encoder(connector);
Chris Wilson4ef69c72010-09-09 15:14:28 +010010528 struct drm_encoder *encoder = &intel_encoder->base;
Daniel Vetter7b240562012-12-12 00:35:33 +010010529 struct drm_crtc *crtc = encoder->crtc;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010530 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010531 struct drm_atomic_state *state;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010532 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010533 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010534 int ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080010535
Chris Wilsond2dff872011-04-19 08:36:26 +010010536 DRM_DEBUG_KMS("[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
Jani Nikulac23cc412014-06-03 14:56:17 +030010537 connector->base.id, connector->name,
Jani Nikula8e329a032014-06-03 14:56:21 +030010538 encoder->base.id, encoder->name);
Chris Wilsond2dff872011-04-19 08:36:26 +010010539
Chris Wilson8261b192011-04-19 23:18:09 +010010540 if (old->load_detect_temp) {
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010541 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010542 if (!state)
10543 goto fail;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020010544
10545 state->acquire_ctx = ctx;
10546
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010547 connector_state = drm_atomic_get_connector_state(state, connector);
10548 if (IS_ERR(connector_state))
10549 goto fail;
10550
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010551 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
10552 if (IS_ERR(crtc_state))
10553 goto fail;
10554
Daniel Vetterfc303102012-07-09 10:40:58 +020010555 to_intel_connector(connector)->new_encoder = NULL;
10556 intel_encoder->new_crtc = NULL;
Ville Syrjälä412b61d2014-01-17 15:59:39 +020010557 intel_crtc->new_enabled = false;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010558
10559 connector_state->best_encoder = NULL;
10560 connector_state->crtc = NULL;
10561
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020010562 crtc_state->base.enable = crtc_state->base.active = false;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030010563
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030010564 ret = intel_modeset_setup_plane_state(state, crtc, NULL, NULL,
10565 0, 0);
10566 if (ret)
10567 goto fail;
10568
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030010569 ret = intel_set_mode(crtc, state);
10570 if (ret)
10571 goto fail;
Chris Wilsond2dff872011-04-19 08:36:26 +010010572
Daniel Vetter36206362012-12-10 20:42:17 +010010573 if (old->release_fb) {
10574 drm_framebuffer_unregister_private(old->release_fb);
10575 drm_framebuffer_unreference(old->release_fb);
10576 }
Chris Wilsond2dff872011-04-19 08:36:26 +010010577
Chris Wilson0622a532011-04-21 09:32:11 +010010578 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010579 }
10580
Eric Anholtc751ce42010-03-25 11:48:48 -070010581 /* Switch crtc and encoder back off if necessary */
Daniel Vetter24218aa2012-08-12 19:27:11 +020010582 if (old->dpms_mode != DRM_MODE_DPMS_ON)
10583 connector->funcs->dpms(connector, old->dpms_mode);
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020010584
10585 return;
10586fail:
10587 DRM_DEBUG_KMS("Couldn't release load detect pipe.\n");
10588 drm_atomic_state_free(state);
Jesse Barnes79e53942008-11-07 14:24:08 -080010589}
10590
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010591static int i9xx_pll_refclk(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010592 const struct intel_crtc_state *pipe_config)
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010593{
10594 struct drm_i915_private *dev_priv = dev->dev_private;
10595 u32 dpll = pipe_config->dpll_hw_state.dpll;
10596
10597 if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
Ville Syrjäläe91e9412013-12-09 18:54:16 +020010598 return dev_priv->vbt.lvds_ssc_freq;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010599 else if (HAS_PCH_SPLIT(dev))
10600 return 120000;
10601 else if (!IS_GEN2(dev))
10602 return 96000;
10603 else
10604 return 48000;
10605}
10606
Jesse Barnes79e53942008-11-07 14:24:08 -080010607/* Returns the clock of the currently programmed mode of the given pipe. */
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010608static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010609 struct intel_crtc_state *pipe_config)
Jesse Barnes79e53942008-11-07 14:24:08 -080010610{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010611 struct drm_device *dev = crtc->base.dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080010612 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010613 int pipe = pipe_config->cpu_transcoder;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010614 u32 dpll = pipe_config->dpll_hw_state.dpll;
Jesse Barnes79e53942008-11-07 14:24:08 -080010615 u32 fp;
10616 intel_clock_t clock;
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010617 int refclk = i9xx_pll_refclk(dev, pipe_config);
Jesse Barnes79e53942008-11-07 14:24:08 -080010618
10619 if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
Ville Syrjälä293623f2013-09-13 16:18:46 +030010620 fp = pipe_config->dpll_hw_state.fp0;
Jesse Barnes79e53942008-11-07 14:24:08 -080010621 else
Ville Syrjälä293623f2013-09-13 16:18:46 +030010622 fp = pipe_config->dpll_hw_state.fp1;
Jesse Barnes79e53942008-11-07 14:24:08 -080010623
10624 clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010625 if (IS_PINEVIEW(dev)) {
10626 clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
10627 clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
Shaohua Li21778322009-02-23 15:19:16 +080010628 } else {
10629 clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
10630 clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
10631 }
10632
Chris Wilsona6c45cf2010-09-17 00:32:17 +010010633 if (!IS_GEN2(dev)) {
Adam Jacksonf2b115e2009-12-03 17:14:42 -050010634 if (IS_PINEVIEW(dev))
10635 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
10636 DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
Shaohua Li21778322009-02-23 15:19:16 +080010637 else
10638 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
Jesse Barnes79e53942008-11-07 14:24:08 -080010639 DPLL_FPA01_P1_POST_DIV_SHIFT);
10640
10641 switch (dpll & DPLL_MODE_MASK) {
10642 case DPLLB_MODE_DAC_SERIAL:
10643 clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
10644 5 : 10;
10645 break;
10646 case DPLLB_MODE_LVDS:
10647 clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
10648 7 : 14;
10649 break;
10650 default:
Zhao Yakui28c97732009-10-09 11:39:41 +080010651 DRM_DEBUG_KMS("Unknown DPLL mode %08x in programmed "
Jesse Barnes79e53942008-11-07 14:24:08 -080010652 "mode\n", (int)(dpll & DPLL_MODE_MASK));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010653 return;
Jesse Barnes79e53942008-11-07 14:24:08 -080010654 }
10655
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010656 if (IS_PINEVIEW(dev))
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010657 pineview_clock(refclk, &clock);
Daniel Vetterac58c3f2013-06-01 17:16:17 +020010658 else
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010659 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010660 } else {
Ville Syrjälä0fb58222014-01-10 14:06:46 +020010661 u32 lvds = IS_I830(dev) ? 0 : I915_READ(LVDS);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010662 bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
Jesse Barnes79e53942008-11-07 14:24:08 -080010663
10664 if (is_lvds) {
10665 clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
10666 DPLL_FPA01_P1_POST_DIV_SHIFT);
Ville Syrjäläb1c560d2013-12-09 18:54:13 +020010667
10668 if (lvds & LVDS_CLKB_POWER_UP)
10669 clock.p2 = 7;
10670 else
10671 clock.p2 = 14;
Jesse Barnes79e53942008-11-07 14:24:08 -080010672 } else {
10673 if (dpll & PLL_P1_DIVIDE_BY_TWO)
10674 clock.p1 = 2;
10675 else {
10676 clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
10677 DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
10678 }
10679 if (dpll & PLL_P2_DIVIDE_BY_4)
10680 clock.p2 = 4;
10681 else
10682 clock.p2 = 2;
Jesse Barnes79e53942008-11-07 14:24:08 -080010683 }
Ville Syrjäläda4a1ef2013-09-09 14:06:37 +030010684
10685 i9xx_clock(refclk, &clock);
Jesse Barnes79e53942008-11-07 14:24:08 -080010686 }
10687
Ville Syrjälä18442d02013-09-13 16:00:08 +030010688 /*
10689 * This value includes pixel_multiplier. We will use
Damien Lespiau241bfc32013-09-25 16:45:37 +010010690 * port_clock to compute adjusted_mode.crtc_clock in the
Ville Syrjälä18442d02013-09-13 16:00:08 +030010691 * encoder's get_config() function.
10692 */
10693 pipe_config->port_clock = clock.dot;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010694}
10695
Ville Syrjälä6878da02013-09-13 15:59:11 +030010696int intel_dotclock_calculate(int link_freq,
10697 const struct intel_link_m_n *m_n)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010698{
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010699 /*
10700 * The calculation for the data clock is:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010701 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010702 * But we want to avoid losing precison if possible, so:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010703 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010704 *
10705 * and the link clock is simpler:
Ville Syrjälä1041a022013-09-06 23:28:58 +030010706 * link_clock = (m * link_clock) / n
Jesse Barnes79e53942008-11-07 14:24:08 -080010707 */
10708
Ville Syrjälä6878da02013-09-13 15:59:11 +030010709 if (!m_n->link_n)
10710 return 0;
10711
10712 return div_u64((u64)m_n->link_m * link_freq, m_n->link_n);
10713}
10714
Ville Syrjälä18442d02013-09-13 16:00:08 +030010715static void ironlake_pch_clock_get(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010716 struct intel_crtc_state *pipe_config)
Ville Syrjälä6878da02013-09-13 15:59:11 +030010717{
10718 struct drm_device *dev = crtc->base.dev;
Ville Syrjälä18442d02013-09-13 16:00:08 +030010719
10720 /* read out port_clock from the DPLL */
10721 i9xx_crtc_clock_get(crtc, pipe_config);
Ville Syrjälä6878da02013-09-13 15:59:11 +030010722
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010723 /*
Ville Syrjälä18442d02013-09-13 16:00:08 +030010724 * This value does not include pixel_multiplier.
Damien Lespiau241bfc32013-09-25 16:45:37 +010010725 * We will check that port_clock and adjusted_mode.crtc_clock
Ville Syrjälä18442d02013-09-13 16:00:08 +030010726 * agree once we know their relationship in the encoder's
10727 * get_config() function.
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010728 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020010729 pipe_config->base.adjusted_mode.crtc_clock =
Ville Syrjälä18442d02013-09-13 16:00:08 +030010730 intel_dotclock_calculate(intel_fdi_link_freq(dev) * 10000,
10731 &pipe_config->fdi_m_n);
Jesse Barnes79e53942008-11-07 14:24:08 -080010732}
10733
10734/** Returns the currently programmed mode of the given pipe. */
10735struct drm_display_mode *intel_crtc_mode_get(struct drm_device *dev,
10736 struct drm_crtc *crtc)
10737{
Jesse Barnes548f2452011-02-17 10:40:53 -080010738 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080010739 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020010740 enum transcoder cpu_transcoder = intel_crtc->config->cpu_transcoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080010741 struct drm_display_mode *mode;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020010742 struct intel_crtc_state pipe_config;
Paulo Zanonife2b8f92012-10-23 18:30:02 -020010743 int htot = I915_READ(HTOTAL(cpu_transcoder));
10744 int hsync = I915_READ(HSYNC(cpu_transcoder));
10745 int vtot = I915_READ(VTOTAL(cpu_transcoder));
10746 int vsync = I915_READ(VSYNC(cpu_transcoder));
Ville Syrjälä293623f2013-09-13 16:18:46 +030010747 enum pipe pipe = intel_crtc->pipe;
Jesse Barnes79e53942008-11-07 14:24:08 -080010748
10749 mode = kzalloc(sizeof(*mode), GFP_KERNEL);
10750 if (!mode)
10751 return NULL;
10752
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010753 /*
10754 * Construct a pipe_config sufficient for getting the clock info
10755 * back out of crtc_clock_get.
10756 *
10757 * Note, if LVDS ever uses a non-1 pixel multiplier, we'll need
10758 * to use a real value here instead.
10759 */
Ville Syrjälä293623f2013-09-13 16:18:46 +030010760 pipe_config.cpu_transcoder = (enum transcoder) pipe;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010761 pipe_config.pixel_multiplier = 1;
Ville Syrjälä293623f2013-09-13 16:18:46 +030010762 pipe_config.dpll_hw_state.dpll = I915_READ(DPLL(pipe));
10763 pipe_config.dpll_hw_state.fp0 = I915_READ(FP0(pipe));
10764 pipe_config.dpll_hw_state.fp1 = I915_READ(FP1(pipe));
Jesse Barnesf1f644d2013-06-27 00:39:25 +030010765 i9xx_crtc_clock_get(intel_crtc, &pipe_config);
10766
Ville Syrjälä773ae032013-09-23 17:48:20 +030010767 mode->clock = pipe_config.port_clock / pipe_config.pixel_multiplier;
Jesse Barnes79e53942008-11-07 14:24:08 -080010768 mode->hdisplay = (htot & 0xffff) + 1;
10769 mode->htotal = ((htot & 0xffff0000) >> 16) + 1;
10770 mode->hsync_start = (hsync & 0xffff) + 1;
10771 mode->hsync_end = ((hsync & 0xffff0000) >> 16) + 1;
10772 mode->vdisplay = (vtot & 0xffff) + 1;
10773 mode->vtotal = ((vtot & 0xffff0000) >> 16) + 1;
10774 mode->vsync_start = (vsync & 0xffff) + 1;
10775 mode->vsync_end = ((vsync & 0xffff0000) >> 16) + 1;
10776
10777 drm_mode_set_name(mode);
Jesse Barnes79e53942008-11-07 14:24:08 -080010778
10779 return mode;
10780}
10781
Jesse Barnes652c3932009-08-17 13:31:43 -070010782static void intel_decrease_pllclock(struct drm_crtc *crtc)
10783{
10784 struct drm_device *dev = crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030010785 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes652c3932009-08-17 13:31:43 -070010786 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes652c3932009-08-17 13:31:43 -070010787
Sonika Jindalbaff2962014-07-22 11:16:35 +053010788 if (!HAS_GMCH_DISPLAY(dev))
Jesse Barnes652c3932009-08-17 13:31:43 -070010789 return;
10790
10791 if (!dev_priv->lvds_downclock_avail)
10792 return;
10793
10794 /*
10795 * Since this is called by a timer, we should never get here in
10796 * the manual case.
10797 */
10798 if (!HAS_PIPE_CXSR(dev) && intel_crtc->lowfreq_avail) {
Chris Wilson074b5e12012-05-02 12:07:06 +010010799 int pipe = intel_crtc->pipe;
10800 int dpll_reg = DPLL(pipe);
Daniel Vetterdc257cf2012-05-07 11:30:46 +020010801 int dpll;
Chris Wilson074b5e12012-05-02 12:07:06 +010010802
Zhao Yakui44d98a62009-10-09 11:39:40 +080010803 DRM_DEBUG_DRIVER("downclocking LVDS\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010804
Sean Paul8ac5a6d2012-02-13 13:14:51 -050010805 assert_panel_unlocked(dev_priv, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010806
Chris Wilson074b5e12012-05-02 12:07:06 +010010807 dpll = I915_READ(dpll_reg);
Jesse Barnes652c3932009-08-17 13:31:43 -070010808 dpll |= DISPLAY_RATE_SELECT_FPA1;
10809 I915_WRITE(dpll_reg, dpll);
Jesse Barnes9d0498a2010-08-18 13:20:54 -070010810 intel_wait_for_vblank(dev, pipe);
Jesse Barnes652c3932009-08-17 13:31:43 -070010811 dpll = I915_READ(dpll_reg);
10812 if (!(dpll & DISPLAY_RATE_SELECT_FPA1))
Zhao Yakui44d98a62009-10-09 11:39:40 +080010813 DRM_DEBUG_DRIVER("failed to downclock LVDS!\n");
Jesse Barnes652c3932009-08-17 13:31:43 -070010814 }
10815
10816}
10817
Chris Wilsonf047e392012-07-21 12:31:41 +010010818void intel_mark_busy(struct drm_device *dev)
Jesse Barnes652c3932009-08-17 13:31:43 -070010819{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010820 struct drm_i915_private *dev_priv = dev->dev_private;
10821
Chris Wilsonf62a0072014-02-21 17:55:39 +000010822 if (dev_priv->mm.busy)
10823 return;
10824
Paulo Zanoni43694d62014-03-07 20:08:08 -030010825 intel_runtime_pm_get(dev_priv);
Paulo Zanonic67a4702013-08-19 13:18:09 -030010826 i915_update_gfx_val(dev_priv);
Chris Wilson43cf3bf2015-03-18 09:48:22 +000010827 if (INTEL_INFO(dev)->gen >= 6)
10828 gen6_rps_busy(dev_priv);
Chris Wilsonf62a0072014-02-21 17:55:39 +000010829 dev_priv->mm.busy = true;
Chris Wilsonf047e392012-07-21 12:31:41 +010010830}
10831
10832void intel_mark_idle(struct drm_device *dev)
10833{
Paulo Zanonic67a4702013-08-19 13:18:09 -030010834 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson725a5b52013-01-08 11:02:57 +000010835 struct drm_crtc *crtc;
10836
Chris Wilsonf62a0072014-02-21 17:55:39 +000010837 if (!dev_priv->mm.busy)
10838 return;
10839
10840 dev_priv->mm.busy = false;
10841
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010010842 for_each_crtc(dev, crtc) {
Matt Roperf4510a22014-04-01 15:22:40 -070010843 if (!crtc->primary->fb)
Chris Wilson725a5b52013-01-08 11:02:57 +000010844 continue;
10845
10846 intel_decrease_pllclock(crtc);
10847 }
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010848
Damien Lespiau3d13ef22014-02-07 19:12:47 +000010849 if (INTEL_INFO(dev)->gen >= 6)
Chris Wilsonb29c19b2013-09-25 17:34:56 +010010850 gen6_rps_idle(dev->dev_private);
Paulo Zanonibb4cdd52014-02-21 13:52:19 -030010851
Paulo Zanoni43694d62014-03-07 20:08:08 -030010852 intel_runtime_pm_put(dev_priv);
Chris Wilsonf047e392012-07-21 12:31:41 +010010853}
10854
Jesse Barnes79e53942008-11-07 14:24:08 -080010855static void intel_crtc_destroy(struct drm_crtc *crtc)
10856{
10857 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010858 struct drm_device *dev = crtc->dev;
10859 struct intel_unpin_work *work;
Daniel Vetter67e77c52010-08-20 22:26:30 +020010860
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010861 spin_lock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010862 work = intel_crtc->unpin_work;
10863 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020010864 spin_unlock_irq(&dev->event_lock);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010865
10866 if (work) {
10867 cancel_work_sync(&work->work);
10868 kfree(work);
10869 }
Jesse Barnes79e53942008-11-07 14:24:08 -080010870
10871 drm_crtc_cleanup(crtc);
Daniel Vetter67e77c52010-08-20 22:26:30 +020010872
Jesse Barnes79e53942008-11-07 14:24:08 -080010873 kfree(intel_crtc);
10874}
10875
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010876static void intel_unpin_work_fn(struct work_struct *__work)
10877{
10878 struct intel_unpin_work *work =
10879 container_of(__work, struct intel_unpin_work, work);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010880 struct drm_device *dev = work->crtc->dev;
Daniel Vetterf99d7062014-06-19 16:01:59 +020010881 enum pipe pipe = to_intel_crtc(work->crtc)->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010882
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010883 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000010884 intel_unpin_fb_obj(work->old_fb, work->crtc->primary->state);
Chris Wilson05394f32010-11-08 19:18:58 +000010885 drm_gem_object_unreference(&work->pending_flip_obj->base);
Chris Wilsond9e86c02010-11-10 16:40:20 +000010886
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020010887 intel_fbc_update(dev);
John Harrisonf06cc1b2014-11-24 18:49:37 +000010888
10889 if (work->flip_queued_req)
John Harrison146d84f2014-12-05 13:49:33 +000010890 i915_gem_request_assign(&work->flip_queued_req, NULL);
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010891 mutex_unlock(&dev->struct_mutex);
10892
Daniel Vetterf99d7062014-06-19 16:01:59 +020010893 intel_frontbuffer_flip_complete(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Chris Wilson89ed88b2015-02-16 14:31:49 +000010894 drm_framebuffer_unreference(work->old_fb);
Daniel Vetterf99d7062014-06-19 16:01:59 +020010895
Chris Wilsonb4a98e52012-11-01 09:26:26 +000010896 BUG_ON(atomic_read(&to_intel_crtc(work->crtc)->unpin_work_count) == 0);
10897 atomic_dec(&to_intel_crtc(work->crtc)->unpin_work_count);
10898
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010899 kfree(work);
10900}
10901
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010902static void do_intel_finish_page_flip(struct drm_device *dev,
Mario Kleiner49b14a52010-12-09 07:00:07 +010010903 struct drm_crtc *crtc)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010904{
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010905 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
10906 struct intel_unpin_work *work;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010907 unsigned long flags;
10908
10909 /* Ignore early vblank irqs */
10910 if (intel_crtc == NULL)
10911 return;
10912
Daniel Vetterf3260382014-09-15 14:55:23 +020010913 /*
10914 * This is called both by irq handlers and the reset code (to complete
10915 * lost pageflips) so needs the full irqsave spinlocks.
10916 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010917 spin_lock_irqsave(&dev->event_lock, flags);
10918 work = intel_crtc->unpin_work;
Chris Wilsone7d841c2012-12-03 11:36:30 +000010919
10920 /* Ensure we don't miss a work->pending update ... */
10921 smp_rmb();
10922
10923 if (work == NULL || atomic_read(&work->pending) < INTEL_FLIP_COMPLETE) {
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010924 spin_unlock_irqrestore(&dev->event_lock, flags);
10925 return;
10926 }
10927
Chris Wilsond6bbafa2014-09-05 07:13:24 +010010928 page_flip_completed(intel_crtc);
Mario Kleiner0af7e4d2010-12-08 04:07:19 +010010929
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010930 spin_unlock_irqrestore(&dev->event_lock, flags);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010931}
10932
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010933void intel_finish_page_flip(struct drm_device *dev, int pipe)
10934{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010935 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010936 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
10937
Mario Kleiner49b14a52010-12-09 07:00:07 +010010938 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010939}
10940
10941void intel_finish_page_flip_plane(struct drm_device *dev, int plane)
10942{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010943 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010944 struct drm_crtc *crtc = dev_priv->plane_to_crtc_mapping[plane];
10945
Mario Kleiner49b14a52010-12-09 07:00:07 +010010946 do_intel_finish_page_flip(dev, crtc);
Jesse Barnes1afe3e92010-03-26 10:35:20 -070010947}
10948
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010949/* Is 'a' after or equal to 'b'? */
10950static bool g4x_flip_count_after_eq(u32 a, u32 b)
10951{
10952 return !((a - b) & 0x80000000);
10953}
10954
10955static bool page_flip_finished(struct intel_crtc *crtc)
10956{
10957 struct drm_device *dev = crtc->base.dev;
10958 struct drm_i915_private *dev_priv = dev->dev_private;
10959
Ville Syrjäläbdfa7542014-05-27 21:33:09 +030010960 if (i915_reset_in_progress(&dev_priv->gpu_error) ||
10961 crtc->reset_counter != atomic_read(&dev_priv->gpu_error.reset_counter))
10962 return true;
10963
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030010964 /*
10965 * The relevant registers doen't exist on pre-ctg.
10966 * As the flip done interrupt doesn't trigger for mmio
10967 * flips on gmch platforms, a flip count check isn't
10968 * really needed there. But since ctg has the registers,
10969 * include it in the check anyway.
10970 */
10971 if (INTEL_INFO(dev)->gen < 5 && !IS_G4X(dev))
10972 return true;
10973
10974 /*
10975 * A DSPSURFLIVE check isn't enough in case the mmio and CS flips
10976 * used the same base address. In that case the mmio flip might
10977 * have completed, but the CS hasn't even executed the flip yet.
10978 *
10979 * A flip count check isn't enough as the CS might have updated
10980 * the base address just after start of vblank, but before we
10981 * managed to process the interrupt. This means we'd complete the
10982 * CS flip too soon.
10983 *
10984 * Combining both checks should get us a good enough result. It may
10985 * still happen that the CS flip has been executed, but has not
10986 * yet actually completed. But in case the base address is the same
10987 * anyway, we don't really care.
10988 */
10989 return (I915_READ(DSPSURFLIVE(crtc->plane)) & ~0xfff) ==
10990 crtc->unpin_work->gtt_offset &&
10991 g4x_flip_count_after_eq(I915_READ(PIPE_FLIPCOUNT_GM45(crtc->pipe)),
10992 crtc->unpin_work->flip_count);
10993}
10994
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010995void intel_prepare_page_flip(struct drm_device *dev, int plane)
10996{
Jani Nikulafbee40d2014-03-31 14:27:18 +030010997 struct drm_i915_private *dev_priv = dev->dev_private;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050010998 struct intel_crtc *intel_crtc =
10999 to_intel_crtc(dev_priv->plane_to_crtc_mapping[plane]);
11000 unsigned long flags;
11001
Daniel Vetterf3260382014-09-15 14:55:23 +020011002
11003 /*
11004 * This is called both by irq handlers and the reset code (to complete
11005 * lost pageflips) so needs the full irqsave spinlocks.
11006 *
11007 * NB: An MMIO update of the plane base pointer will also
Chris Wilsone7d841c2012-12-03 11:36:30 +000011008 * generate a page-flip completion irq, i.e. every modeset
11009 * is also accompanied by a spurious intel_prepare_page_flip().
11010 */
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011011 spin_lock_irqsave(&dev->event_lock, flags);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011012 if (intel_crtc->unpin_work && page_flip_finished(intel_crtc))
Chris Wilsone7d841c2012-12-03 11:36:30 +000011013 atomic_inc_not_zero(&intel_crtc->unpin_work->pending);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011014 spin_unlock_irqrestore(&dev->event_lock, flags);
11015}
11016
Robin Schroereba905b2014-05-18 02:24:50 +020011017static inline void intel_mark_page_flip_active(struct intel_crtc *intel_crtc)
Chris Wilsone7d841c2012-12-03 11:36:30 +000011018{
11019 /* Ensure that the work item is consistent when activating it ... */
11020 smp_wmb();
11021 atomic_set(&intel_crtc->unpin_work->pending, INTEL_FLIP_PENDING);
11022 /* and that it is marked active as soon as the irq could fire. */
11023 smp_wmb();
11024}
11025
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011026static int intel_gen2_queue_flip(struct drm_device *dev,
11027 struct drm_crtc *crtc,
11028 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011029 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011030 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011031 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011032{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011033 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011034 u32 flip_mask;
11035 int ret;
11036
Daniel Vetter6d90c952012-04-26 23:28:05 +020011037 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011038 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011039 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011040
11041 /* Can't queue multiple flips, so wait for the previous
11042 * one to finish before executing the next.
11043 */
11044 if (intel_crtc->plane)
11045 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11046 else
11047 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011048 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11049 intel_ring_emit(ring, MI_NOOP);
11050 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11051 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11052 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011053 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011054 intel_ring_emit(ring, 0); /* aux display base address, unused */
Chris Wilsone7d841c2012-12-03 11:36:30 +000011055
11056 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011057 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011058 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011059}
11060
11061static int intel_gen3_queue_flip(struct drm_device *dev,
11062 struct drm_crtc *crtc,
11063 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011064 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011065 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011066 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011067{
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011068 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011069 u32 flip_mask;
11070 int ret;
11071
Daniel Vetter6d90c952012-04-26 23:28:05 +020011072 ret = intel_ring_begin(ring, 6);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011073 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011074 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011075
11076 if (intel_crtc->plane)
11077 flip_mask = MI_WAIT_FOR_PLANE_B_FLIP;
11078 else
11079 flip_mask = MI_WAIT_FOR_PLANE_A_FLIP;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011080 intel_ring_emit(ring, MI_WAIT_FOR_EVENT | flip_mask);
11081 intel_ring_emit(ring, MI_NOOP);
11082 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 |
11083 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11084 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011085 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Daniel Vetter6d90c952012-04-26 23:28:05 +020011086 intel_ring_emit(ring, MI_NOOP);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011087
Chris Wilsone7d841c2012-12-03 11:36:30 +000011088 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011089 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011090 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011091}
11092
11093static int intel_gen4_queue_flip(struct drm_device *dev,
11094 struct drm_crtc *crtc,
11095 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011096 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011097 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011098 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011099{
11100 struct drm_i915_private *dev_priv = dev->dev_private;
11101 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11102 uint32_t pf, pipesrc;
11103 int ret;
11104
Daniel Vetter6d90c952012-04-26 23:28:05 +020011105 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011106 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011107 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011108
11109 /* i965+ uses the linear or tiled offsets from the
11110 * Display Registers (which do not change across a page-flip)
11111 * so we need only reprogram the base address.
11112 */
Daniel Vetter6d90c952012-04-26 23:28:05 +020011113 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11114 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11115 intel_ring_emit(ring, fb->pitches[0]);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011116 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset |
Daniel Vetterc2c75132012-07-05 12:17:30 +020011117 obj->tiling_mode);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011118
11119 /* XXX Enabling the panel-fitter across page-flip is so far
11120 * untested on non-native modes, so ignore it for now.
11121 * pf = I915_READ(pipe == 0 ? PFA_CTL_1 : PFB_CTL_1) & PF_ENABLE;
11122 */
11123 pf = 0;
11124 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011125 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011126
11127 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011128 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011129 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011130}
11131
11132static int intel_gen6_queue_flip(struct drm_device *dev,
11133 struct drm_crtc *crtc,
11134 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011135 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011136 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011137 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011138{
11139 struct drm_i915_private *dev_priv = dev->dev_private;
11140 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11141 uint32_t pf, pipesrc;
11142 int ret;
11143
Daniel Vetter6d90c952012-04-26 23:28:05 +020011144 ret = intel_ring_begin(ring, 4);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011145 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011146 return ret;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011147
Daniel Vetter6d90c952012-04-26 23:28:05 +020011148 intel_ring_emit(ring, MI_DISPLAY_FLIP |
11149 MI_DISPLAY_FLIP_PLANE(intel_crtc->plane));
11150 intel_ring_emit(ring, fb->pitches[0] | obj->tiling_mode);
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011151 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011152
Chris Wilson99d9acd2012-04-17 20:37:00 +010011153 /* Contrary to the suggestions in the documentation,
11154 * "Enable Panel Fitter" does not seem to be required when page
11155 * flipping with a non-native mode, and worse causes a normal
11156 * modeset to fail.
11157 * pf = I915_READ(PF_CTL(intel_crtc->pipe)) & PF_ENABLE;
11158 */
11159 pf = 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011160 pipesrc = I915_READ(PIPESRC(intel_crtc->pipe)) & 0x0fff0fff;
Daniel Vetter6d90c952012-04-26 23:28:05 +020011161 intel_ring_emit(ring, pf | pipesrc);
Chris Wilsone7d841c2012-12-03 11:36:30 +000011162
11163 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011164 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011165 return 0;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011166}
11167
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011168static int intel_gen7_queue_flip(struct drm_device *dev,
11169 struct drm_crtc *crtc,
11170 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011171 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011172 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011173 uint32_t flags)
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011174{
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011175 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011176 uint32_t plane_bit = 0;
Chris Wilsonffe74d72013-08-26 20:58:12 +010011177 int len, ret;
11178
Robin Schroereba905b2014-05-18 02:24:50 +020011179 switch (intel_crtc->plane) {
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011180 case PLANE_A:
11181 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_A;
11182 break;
11183 case PLANE_B:
11184 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_B;
11185 break;
11186 case PLANE_C:
11187 plane_bit = MI_DISPLAY_FLIP_IVB_PLANE_C;
11188 break;
11189 default:
11190 WARN_ONCE(1, "unknown plane in flip command\n");
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011191 return -ENODEV;
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011192 }
11193
Chris Wilsonffe74d72013-08-26 20:58:12 +010011194 len = 4;
Damien Lespiauf4768282014-04-07 20:24:34 +010011195 if (ring->id == RCS) {
Chris Wilsonffe74d72013-08-26 20:58:12 +010011196 len += 6;
Damien Lespiauf4768282014-04-07 20:24:34 +010011197 /*
11198 * On Gen 8, SRM is now taking an extra dword to accommodate
11199 * 48bits addresses, and we need a NOOP for the batch size to
11200 * stay even.
11201 */
11202 if (IS_GEN8(dev))
11203 len += 2;
11204 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011205
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011206 /*
11207 * BSpec MI_DISPLAY_FLIP for IVB:
11208 * "The full packet must be contained within the same cache line."
11209 *
11210 * Currently the LRI+SRM+MI_DISPLAY_FLIP all fit within the same
11211 * cacheline, if we ever start emitting more commands before
11212 * the MI_DISPLAY_FLIP we may need to first emit everything else,
11213 * then do the cacheline alignment, and finally emit the
11214 * MI_DISPLAY_FLIP.
11215 */
11216 ret = intel_ring_cacheline_align(ring);
11217 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011218 return ret;
Ville Syrjäläf66fab82014-02-11 19:52:06 +020011219
Chris Wilsonffe74d72013-08-26 20:58:12 +010011220 ret = intel_ring_begin(ring, len);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011221 if (ret)
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011222 return ret;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011223
Chris Wilsonffe74d72013-08-26 20:58:12 +010011224 /* Unmask the flip-done completion message. Note that the bspec says that
11225 * we should do this for both the BCS and RCS, and that we must not unmask
11226 * more than one flip event at any time (or ensure that one flip message
11227 * can be sent by waiting for flip-done prior to queueing new flips).
11228 * Experimentation says that BCS works despite DERRMR masking all
11229 * flip-done completion events and that unmasking all planes at once
11230 * for the RCS also doesn't appear to drop events. Setting the DERRMR
11231 * to zero does lead to lockups within MI_DISPLAY_FLIP.
11232 */
11233 if (ring->id == RCS) {
11234 intel_ring_emit(ring, MI_LOAD_REGISTER_IMM(1));
11235 intel_ring_emit(ring, DERRMR);
11236 intel_ring_emit(ring, ~(DERRMR_PIPEA_PRI_FLIP_DONE |
11237 DERRMR_PIPEB_PRI_FLIP_DONE |
11238 DERRMR_PIPEC_PRI_FLIP_DONE));
Damien Lespiauf4768282014-04-07 20:24:34 +010011239 if (IS_GEN8(dev))
11240 intel_ring_emit(ring, MI_STORE_REGISTER_MEM_GEN8(1) |
11241 MI_SRM_LRM_GLOBAL_GTT);
11242 else
11243 intel_ring_emit(ring, MI_STORE_REGISTER_MEM(1) |
11244 MI_SRM_LRM_GLOBAL_GTT);
Chris Wilsonffe74d72013-08-26 20:58:12 +010011245 intel_ring_emit(ring, DERRMR);
11246 intel_ring_emit(ring, ring->scratch.gtt_offset + 256);
Damien Lespiauf4768282014-04-07 20:24:34 +010011247 if (IS_GEN8(dev)) {
11248 intel_ring_emit(ring, 0);
11249 intel_ring_emit(ring, MI_NOOP);
11250 }
Chris Wilsonffe74d72013-08-26 20:58:12 +010011251 }
11252
Daniel Vettercb05d8d2012-05-23 14:02:00 +020011253 intel_ring_emit(ring, MI_DISPLAY_FLIP_I915 | plane_bit);
Ville Syrjälä01f2c772011-12-20 00:06:49 +020011254 intel_ring_emit(ring, (fb->pitches[0] | obj->tiling_mode));
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011255 intel_ring_emit(ring, intel_crtc->unpin_work->gtt_offset);
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011256 intel_ring_emit(ring, (MI_NOOP));
Chris Wilsone7d841c2012-12-03 11:36:30 +000011257
11258 intel_mark_page_flip_active(intel_crtc);
Chris Wilson09246732013-08-10 22:16:32 +010011259 __intel_ring_advance(ring);
Chris Wilson83d40922012-04-17 19:35:53 +010011260 return 0;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070011261}
11262
Sourab Gupta84c33a62014-06-02 16:47:17 +053011263static bool use_mmio_flip(struct intel_engine_cs *ring,
11264 struct drm_i915_gem_object *obj)
11265{
11266 /*
11267 * This is not being used for older platforms, because
11268 * non-availability of flip done interrupt forces us to use
11269 * CS flips. Older platforms derive flip done using some clever
11270 * tricks involving the flip_pending status bits and vblank irqs.
11271 * So using MMIO flips there would disrupt this mechanism.
11272 */
11273
Chris Wilson8e09bf82014-07-08 10:40:30 +010011274 if (ring == NULL)
11275 return true;
11276
Sourab Gupta84c33a62014-06-02 16:47:17 +053011277 if (INTEL_INFO(ring->dev)->gen < 5)
11278 return false;
11279
11280 if (i915.use_mmio_flip < 0)
11281 return false;
11282 else if (i915.use_mmio_flip > 0)
11283 return true;
Oscar Mateo14bf9932014-07-24 17:04:34 +010011284 else if (i915.enable_execlists)
11285 return true;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011286 else
Chris Wilsonb4716182015-04-27 13:41:17 +010011287 return ring != i915_gem_request_get_ring(obj->last_write_req);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011288}
11289
Damien Lespiauff944562014-11-20 14:58:16 +000011290static void skl_do_mmio_flip(struct intel_crtc *intel_crtc)
11291{
11292 struct drm_device *dev = intel_crtc->base.dev;
11293 struct drm_i915_private *dev_priv = dev->dev_private;
11294 struct drm_framebuffer *fb = intel_crtc->base.primary->fb;
Damien Lespiauff944562014-11-20 14:58:16 +000011295 const enum pipe pipe = intel_crtc->pipe;
11296 u32 ctl, stride;
11297
11298 ctl = I915_READ(PLANE_CTL(pipe, 0));
11299 ctl &= ~PLANE_CTL_TILED_MASK;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011300 switch (fb->modifier[0]) {
11301 case DRM_FORMAT_MOD_NONE:
11302 break;
11303 case I915_FORMAT_MOD_X_TILED:
Damien Lespiauff944562014-11-20 14:58:16 +000011304 ctl |= PLANE_CTL_TILED_X;
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011305 break;
11306 case I915_FORMAT_MOD_Y_TILED:
11307 ctl |= PLANE_CTL_TILED_Y;
11308 break;
11309 case I915_FORMAT_MOD_Yf_TILED:
11310 ctl |= PLANE_CTL_TILED_YF;
11311 break;
11312 default:
11313 MISSING_CASE(fb->modifier[0]);
11314 }
Damien Lespiauff944562014-11-20 14:58:16 +000011315
11316 /*
11317 * The stride is either expressed as a multiple of 64 bytes chunks for
11318 * linear buffers or in number of tiles for tiled buffers.
11319 */
Tvrtko Ursulin2ebef632015-04-20 16:22:48 +010011320 stride = fb->pitches[0] /
11321 intel_fb_stride_alignment(dev, fb->modifier[0],
11322 fb->pixel_format);
Damien Lespiauff944562014-11-20 14:58:16 +000011323
11324 /*
11325 * Both PLANE_CTL and PLANE_STRIDE are not updated on vblank but on
11326 * PLANE_SURF updates, the update is then guaranteed to be atomic.
11327 */
11328 I915_WRITE(PLANE_CTL(pipe, 0), ctl);
11329 I915_WRITE(PLANE_STRIDE(pipe, 0), stride);
11330
11331 I915_WRITE(PLANE_SURF(pipe, 0), intel_crtc->unpin_work->gtt_offset);
11332 POSTING_READ(PLANE_SURF(pipe, 0));
11333}
11334
11335static void ilk_do_mmio_flip(struct intel_crtc *intel_crtc)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011336{
11337 struct drm_device *dev = intel_crtc->base.dev;
11338 struct drm_i915_private *dev_priv = dev->dev_private;
11339 struct intel_framebuffer *intel_fb =
11340 to_intel_framebuffer(intel_crtc->base.primary->fb);
11341 struct drm_i915_gem_object *obj = intel_fb->obj;
11342 u32 dspcntr;
11343 u32 reg;
11344
Sourab Gupta84c33a62014-06-02 16:47:17 +053011345 reg = DSPCNTR(intel_crtc->plane);
11346 dspcntr = I915_READ(reg);
11347
Damien Lespiauc5d97472014-10-25 00:11:11 +010011348 if (obj->tiling_mode != I915_TILING_NONE)
11349 dspcntr |= DISPPLANE_TILED;
11350 else
11351 dspcntr &= ~DISPPLANE_TILED;
11352
Sourab Gupta84c33a62014-06-02 16:47:17 +053011353 I915_WRITE(reg, dspcntr);
11354
11355 I915_WRITE(DSPSURF(intel_crtc->plane),
11356 intel_crtc->unpin_work->gtt_offset);
11357 POSTING_READ(DSPSURF(intel_crtc->plane));
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011358
Damien Lespiauff944562014-11-20 14:58:16 +000011359}
11360
11361/*
11362 * XXX: This is the temporary way to update the plane registers until we get
11363 * around to using the usual plane update functions for MMIO flips
11364 */
11365static void intel_do_mmio_flip(struct intel_crtc *intel_crtc)
11366{
11367 struct drm_device *dev = intel_crtc->base.dev;
11368 bool atomic_update;
11369 u32 start_vbl_count;
11370
11371 intel_mark_page_flip_active(intel_crtc);
11372
11373 atomic_update = intel_pipe_update_start(intel_crtc, &start_vbl_count);
11374
11375 if (INTEL_INFO(dev)->gen >= 9)
11376 skl_do_mmio_flip(intel_crtc);
11377 else
11378 /* use_mmio_flip() retricts MMIO flips to ilk+ */
11379 ilk_do_mmio_flip(intel_crtc);
11380
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011381 if (atomic_update)
11382 intel_pipe_update_end(intel_crtc, start_vbl_count);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011383}
11384
Ander Conselvan de Oliveira9362c7c2014-10-28 15:10:14 +020011385static void intel_mmio_flip_work_func(struct work_struct *work)
Sourab Gupta84c33a62014-06-02 16:47:17 +053011386{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011387 struct intel_mmio_flip *mmio_flip =
11388 container_of(work, struct intel_mmio_flip, work);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011389
Daniel Vettereed29a52015-05-21 14:21:25 +020011390 if (mmio_flip->req)
11391 WARN_ON(__i915_wait_request(mmio_flip->req,
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011392 mmio_flip->crtc->reset_counter,
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011393 false, NULL,
11394 &mmio_flip->i915->rps.mmioflips));
Sourab Gupta84c33a62014-06-02 16:47:17 +053011395
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011396 intel_do_mmio_flip(mmio_flip->crtc);
11397
Daniel Vettereed29a52015-05-21 14:21:25 +020011398 i915_gem_request_unreference__unlocked(mmio_flip->req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011399 kfree(mmio_flip);
Sourab Gupta84c33a62014-06-02 16:47:17 +053011400}
11401
11402static int intel_queue_mmio_flip(struct drm_device *dev,
11403 struct drm_crtc *crtc,
11404 struct drm_framebuffer *fb,
11405 struct drm_i915_gem_object *obj,
11406 struct intel_engine_cs *ring,
11407 uint32_t flags)
11408{
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011409 struct intel_mmio_flip *mmio_flip;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011410
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011411 mmio_flip = kmalloc(sizeof(*mmio_flip), GFP_KERNEL);
11412 if (mmio_flip == NULL)
11413 return -ENOMEM;
Sourab Gupta84c33a62014-06-02 16:47:17 +053011414
Chris Wilsonbcafc4e2015-04-27 13:41:21 +010011415 mmio_flip->i915 = to_i915(dev);
Daniel Vettereed29a52015-05-21 14:21:25 +020011416 mmio_flip->req = i915_gem_request_reference(obj->last_write_req);
Chris Wilsonb2cfe0a2015-04-27 13:41:16 +010011417 mmio_flip->crtc = to_intel_crtc(crtc);
11418
11419 INIT_WORK(&mmio_flip->work, intel_mmio_flip_work_func);
11420 schedule_work(&mmio_flip->work);
Ander Conselvan de Oliveira536f5b52014-11-06 11:03:40 +020011421
Sourab Gupta84c33a62014-06-02 16:47:17 +053011422 return 0;
11423}
11424
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011425static int intel_default_queue_flip(struct drm_device *dev,
11426 struct drm_crtc *crtc,
11427 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011428 struct drm_i915_gem_object *obj,
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011429 struct intel_engine_cs *ring,
Keith Packarded8d1972013-07-22 18:49:58 -070011430 uint32_t flags)
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011431{
11432 return -ENODEV;
11433}
11434
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011435static bool __intel_pageflip_stall_check(struct drm_device *dev,
11436 struct drm_crtc *crtc)
11437{
11438 struct drm_i915_private *dev_priv = dev->dev_private;
11439 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
11440 struct intel_unpin_work *work = intel_crtc->unpin_work;
11441 u32 addr;
11442
11443 if (atomic_read(&work->pending) >= INTEL_FLIP_COMPLETE)
11444 return true;
11445
11446 if (!work->enable_stall_check)
11447 return false;
11448
11449 if (work->flip_ready_vblank == 0) {
Daniel Vetter3a8a9462014-11-26 14:39:48 +010011450 if (work->flip_queued_req &&
11451 !i915_gem_request_completed(work->flip_queued_req, true))
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011452 return false;
11453
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011454 work->flip_ready_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011455 }
11456
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011457 if (drm_crtc_vblank_count(crtc) - work->flip_ready_vblank < 3)
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011458 return false;
11459
11460 /* Potential stall - if we see that the flip has happened,
11461 * assume a missed interrupt. */
11462 if (INTEL_INFO(dev)->gen >= 4)
11463 addr = I915_HI_DISPBASE(I915_READ(DSPSURF(intel_crtc->plane)));
11464 else
11465 addr = I915_READ(DSPADDR(intel_crtc->plane));
11466
11467 /* There is a potential issue here with a false positive after a flip
11468 * to the same address. We could address this by checking for a
11469 * non-incrementing frame counter.
11470 */
11471 return addr == work->gtt_offset;
11472}
11473
11474void intel_check_page_flip(struct drm_device *dev, int pipe)
11475{
11476 struct drm_i915_private *dev_priv = dev->dev_private;
11477 struct drm_crtc *crtc = dev_priv->pipe_to_crtc_mapping[pipe];
11478 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011479 struct intel_unpin_work *work;
Daniel Vetterf3260382014-09-15 14:55:23 +020011480
Dave Gordon6c51d462015-03-06 15:34:26 +000011481 WARN_ON(!in_interrupt());
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011482
11483 if (crtc == NULL)
11484 return;
11485
Daniel Vetterf3260382014-09-15 14:55:23 +020011486 spin_lock(&dev->event_lock);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011487 work = intel_crtc->unpin_work;
11488 if (work != NULL && __intel_pageflip_stall_check(dev, crtc)) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011489 WARN_ONCE(1, "Kicking stuck page flip: queued at %d, now %d\n",
Chris Wilson6ad790c2015-04-07 16:20:31 +010011490 work->flip_queued_vblank, drm_vblank_count(dev, pipe));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011491 page_flip_completed(intel_crtc);
Chris Wilson6ad790c2015-04-07 16:20:31 +010011492 work = NULL;
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011493 }
Chris Wilson6ad790c2015-04-07 16:20:31 +010011494 if (work != NULL &&
11495 drm_vblank_count(dev, pipe) - work->flip_queued_vblank > 1)
11496 intel_queue_rps_boost_for_request(dev, work->flip_queued_req);
Daniel Vetterf3260382014-09-15 14:55:23 +020011497 spin_unlock(&dev->event_lock);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011498}
11499
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011500static int intel_crtc_page_flip(struct drm_crtc *crtc,
11501 struct drm_framebuffer *fb,
Keith Packarded8d1972013-07-22 18:49:58 -070011502 struct drm_pending_vblank_event *event,
11503 uint32_t page_flip_flags)
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011504{
11505 struct drm_device *dev = crtc->dev;
11506 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperf4510a22014-04-01 15:22:40 -070011507 struct drm_framebuffer *old_fb = crtc->primary->fb;
Matt Roper2ff8fde2014-07-08 07:50:07 -070011508 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011509 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Gustavo Padovan455a6802014-12-01 15:40:11 -080011510 struct drm_plane *primary = crtc->primary;
Daniel Vettera071fa02014-06-18 23:28:09 +020011511 enum pipe pipe = intel_crtc->pipe;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011512 struct intel_unpin_work *work;
Oscar Mateoa4872ba2014-05-22 14:13:33 +010011513 struct intel_engine_cs *ring;
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011514 bool mmio_flip;
Chris Wilson52e68632010-08-08 10:15:59 +010011515 int ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011516
Matt Roper2ff8fde2014-07-08 07:50:07 -070011517 /*
11518 * drm_mode_page_flip_ioctl() should already catch this, but double
11519 * check to be safe. In the future we may enable pageflipping from
11520 * a disabled primary plane.
11521 */
11522 if (WARN_ON(intel_fb_obj(old_fb) == NULL))
11523 return -EBUSY;
11524
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011525 /* Can't change pixel format via MI display flips. */
Matt Roperf4510a22014-04-01 15:22:40 -070011526 if (fb->pixel_format != crtc->primary->fb->pixel_format)
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011527 return -EINVAL;
11528
11529 /*
11530 * TILEOFF/LINOFF registers can't be changed via MI display flips.
11531 * Note that pitch changes could also affect these register.
11532 */
11533 if (INTEL_INFO(dev)->gen > 3 &&
Matt Roperf4510a22014-04-01 15:22:40 -070011534 (fb->offsets[0] != crtc->primary->fb->offsets[0] ||
11535 fb->pitches[0] != crtc->primary->fb->pitches[0]))
Ville Syrjäläe6a595d2012-05-24 21:08:59 +030011536 return -EINVAL;
11537
Chris Wilsonf900db42014-02-20 09:26:13 +000011538 if (i915_terminally_wedged(&dev_priv->gpu_error))
11539 goto out_hang;
11540
Daniel Vetterb14c5672013-09-19 12:18:32 +020011541 work = kzalloc(sizeof(*work), GFP_KERNEL);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011542 if (work == NULL)
11543 return -ENOMEM;
11544
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011545 work->event = event;
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011546 work->crtc = crtc;
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011547 work->old_fb = old_fb;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011548 INIT_WORK(&work->work, intel_unpin_work_fn);
11549
Daniel Vetter87b6b102014-05-15 15:33:46 +020011550 ret = drm_crtc_vblank_get(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011551 if (ret)
11552 goto free_work;
11553
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011554 /* We borrow the event spin lock for protecting unpin_work */
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011555 spin_lock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011556 if (intel_crtc->unpin_work) {
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011557 /* Before declaring the flip queue wedged, check if
11558 * the hardware completed the operation behind our backs.
11559 */
11560 if (__intel_pageflip_stall_check(dev, crtc)) {
11561 DRM_DEBUG_DRIVER("flip queue: previous flip completed, continuing\n");
11562 page_flip_completed(intel_crtc);
11563 } else {
11564 DRM_DEBUG_DRIVER("flip queue: crtc already busy\n");
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011565 spin_unlock_irq(&dev->event_lock);
Chris Wilson468f0b42010-05-27 13:18:13 +010011566
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011567 drm_crtc_vblank_put(crtc);
11568 kfree(work);
11569 return -EBUSY;
11570 }
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011571 }
11572 intel_crtc->unpin_work = work;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011573 spin_unlock_irq(&dev->event_lock);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011574
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011575 if (atomic_read(&intel_crtc->unpin_work_count) >= 2)
11576 flush_workqueue(dev_priv->wq);
11577
Jesse Barnes75dfca82010-02-10 15:09:44 -080011578 /* Reference the objects for the scheduled work. */
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011579 drm_framebuffer_reference(work->old_fb);
Chris Wilson05394f32010-11-08 19:18:58 +000011580 drm_gem_object_reference(&obj->base);
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011581
Matt Roperf4510a22014-04-01 15:22:40 -070011582 crtc->primary->fb = fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011583 update_state_fb(crtc->primary);
Matt Roper1ed1f962015-01-30 16:22:36 -080011584
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011585 work->pending_flip_obj = obj;
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011586
Chris Wilson89ed88b2015-02-16 14:31:49 +000011587 ret = i915_mutex_lock_interruptible(dev);
11588 if (ret)
11589 goto cleanup;
11590
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011591 atomic_inc(&intel_crtc->unpin_work_count);
Ville Syrjälä10d83732013-01-29 18:13:34 +020011592 intel_crtc->reset_counter = atomic_read(&dev_priv->gpu_error.reset_counter);
Chris Wilsone1f99ce2010-10-27 12:45:26 +010011593
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011594 if (INTEL_INFO(dev)->gen >= 5 || IS_G4X(dev))
Daniel Vettera071fa02014-06-18 23:28:09 +020011595 work->flip_count = I915_READ(PIPE_FLIPCOUNT_GM45(pipe)) + 1;
Ville Syrjälä75f7f3e2014-04-15 21:41:34 +030011596
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011597 if (IS_VALLEYVIEW(dev)) {
11598 ring = &dev_priv->ring[BCS];
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011599 if (obj->tiling_mode != intel_fb_obj(work->old_fb)->tiling_mode)
Chris Wilson8e09bf82014-07-08 10:40:30 +010011600 /* vlv: DISPLAY_FLIP fails to change tiling */
11601 ring = NULL;
Chris Wilson48bf5b22014-12-27 09:48:28 +000011602 } else if (IS_IVYBRIDGE(dev) || IS_HASWELL(dev)) {
Chris Wilson2a92d5b2014-07-08 10:40:29 +010011603 ring = &dev_priv->ring[BCS];
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011604 } else if (INTEL_INFO(dev)->gen >= 7) {
Chris Wilsonb4716182015-04-27 13:41:17 +010011605 ring = i915_gem_request_get_ring(obj->last_write_req);
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011606 if (ring == NULL || ring->id != RCS)
11607 ring = &dev_priv->ring[BCS];
11608 } else {
11609 ring = &dev_priv->ring[RCS];
11610 }
11611
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011612 mmio_flip = use_mmio_flip(ring, obj);
11613
11614 /* When using CS flips, we want to emit semaphores between rings.
11615 * However, when using mmio flips we will create a task to do the
11616 * synchronisation, so all we want here is to pin the framebuffer
11617 * into the display plane and skip any waits.
11618 */
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011619 ret = intel_pin_and_fence_fb_obj(crtc->primary, fb,
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011620 crtc->primary->state,
Chris Wilsonb4716182015-04-27 13:41:17 +010011621 mmio_flip ? i915_gem_request_get_ring(obj->last_write_req) : ring);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011622 if (ret)
11623 goto cleanup_pending;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011624
Tvrtko Ursulin121920f2015-03-23 11:10:37 +000011625 work->gtt_offset = intel_plane_obj_offset(to_intel_plane(primary), obj)
11626 + intel_crtc->dspaddr_offset;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011627
Chris Wilsoncf5d8a42015-04-07 16:20:26 +010011628 if (mmio_flip) {
Sourab Gupta84c33a62014-06-02 16:47:17 +053011629 ret = intel_queue_mmio_flip(dev, crtc, fb, obj, ring,
11630 page_flip_flags);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011631 if (ret)
11632 goto cleanup_unpin;
11633
John Harrisonf06cc1b2014-11-24 18:49:37 +000011634 i915_gem_request_assign(&work->flip_queued_req,
11635 obj->last_write_req);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011636 } else {
Chris Wilsond94b5032015-04-27 13:41:15 +010011637 if (obj->last_write_req) {
11638 ret = i915_gem_check_olr(obj->last_write_req);
11639 if (ret)
11640 goto cleanup_unpin;
11641 }
11642
Sourab Gupta84c33a62014-06-02 16:47:17 +053011643 ret = dev_priv->display.queue_flip(dev, crtc, fb, obj, ring,
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011644 page_flip_flags);
11645 if (ret)
11646 goto cleanup_unpin;
11647
John Harrisonf06cc1b2014-11-24 18:49:37 +000011648 i915_gem_request_assign(&work->flip_queued_req,
11649 intel_ring_get_request(ring));
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011650 }
11651
Daniel Vetter1e3feef2015-02-13 21:03:45 +010011652 work->flip_queued_vblank = drm_crtc_vblank_count(crtc);
Chris Wilsond6bbafa2014-09-05 07:13:24 +010011653 work->enable_stall_check = true;
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011654
Tvrtko Ursulinab8d6672015-02-02 15:44:15 +000011655 i915_gem_track_fb(intel_fb_obj(work->old_fb), obj,
Daniel Vettera071fa02014-06-18 23:28:09 +020011656 INTEL_FRONTBUFFER_PRIMARY(pipe));
11657
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020011658 intel_fbc_disable(dev);
Daniel Vetterf99d7062014-06-19 16:01:59 +020011659 intel_frontbuffer_flip_prepare(dev, INTEL_FRONTBUFFER_PRIMARY(pipe));
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011660 mutex_unlock(&dev->struct_mutex);
11661
Jesse Barnese5510fa2010-07-01 16:48:37 -070011662 trace_i915_flip_request(intel_crtc->plane, obj);
11663
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011664 return 0;
Chris Wilson96b099f2010-06-07 14:03:04 +010011665
Ville Syrjälä4fa62c82014-04-15 21:41:38 +030011666cleanup_unpin:
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000011667 intel_unpin_fb_obj(fb, crtc->primary->state);
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070011668cleanup_pending:
Chris Wilsonb4a98e52012-11-01 09:26:26 +000011669 atomic_dec(&intel_crtc->unpin_work_count);
Chris Wilson89ed88b2015-02-16 14:31:49 +000011670 mutex_unlock(&dev->struct_mutex);
11671cleanup:
Matt Roperf4510a22014-04-01 15:22:40 -070011672 crtc->primary->fb = old_fb;
Matt Roperafd65eb2015-02-03 13:10:04 -080011673 update_state_fb(crtc->primary);
Chris Wilson96b099f2010-06-07 14:03:04 +010011674
Chris Wilson89ed88b2015-02-16 14:31:49 +000011675 drm_gem_object_unreference_unlocked(&obj->base);
11676 drm_framebuffer_unreference(work->old_fb);
11677
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011678 spin_lock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011679 intel_crtc->unpin_work = NULL;
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011680 spin_unlock_irq(&dev->event_lock);
Chris Wilson96b099f2010-06-07 14:03:04 +010011681
Daniel Vetter87b6b102014-05-15 15:33:46 +020011682 drm_crtc_vblank_put(crtc);
Jesse Barnes7317c75e62011-08-29 09:45:28 -070011683free_work:
Chris Wilson96b099f2010-06-07 14:03:04 +010011684 kfree(work);
11685
Chris Wilsonf900db42014-02-20 09:26:13 +000011686 if (ret == -EIO) {
11687out_hang:
Matt Roper53a366b2014-12-23 10:41:53 -080011688 ret = intel_plane_restore(primary);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011689 if (ret == 0 && event) {
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011690 spin_lock_irq(&dev->event_lock);
Daniel Vettera071fa02014-06-18 23:28:09 +020011691 drm_send_vblank_event(dev, pipe, event);
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020011692 spin_unlock_irq(&dev->event_lock);
Chris Wilsonf0d3dad2014-09-07 16:51:12 +010011693 }
Chris Wilsonf900db42014-02-20 09:26:13 +000011694 }
Chris Wilson96b099f2010-06-07 14:03:04 +010011695 return ret;
Kristian Høgsberg6b95a202009-11-18 11:25:18 -050011696}
11697
Jani Nikula65b38e02015-04-13 11:26:56 +030011698static const struct drm_crtc_helper_funcs intel_helper_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011699 .mode_set_base_atomic = intel_pipe_set_base_atomic,
11700 .load_lut = intel_crtc_load_lut,
Matt Roperea2c67b2014-12-23 10:41:52 -080011701 .atomic_begin = intel_begin_crtc_commit,
11702 .atomic_flush = intel_finish_crtc_commit,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010011703};
11704
Daniel Vetter9a935852012-07-05 22:34:27 +020011705/**
11706 * intel_modeset_update_staged_output_state
11707 *
11708 * Updates the staged output configuration state, e.g. after we've read out the
11709 * current hw state.
11710 */
11711static void intel_modeset_update_staged_output_state(struct drm_device *dev)
11712{
Ville Syrjälä76688512014-01-10 11:28:06 +020011713 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011714 struct intel_encoder *encoder;
11715 struct intel_connector *connector;
11716
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020011717 for_each_intel_connector(dev, connector) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011718 connector->new_encoder =
11719 to_intel_encoder(connector->base.encoder);
11720 }
11721
Damien Lespiaub2784e12014-08-05 11:29:37 +010011722 for_each_intel_encoder(dev, encoder) {
Daniel Vetter9a935852012-07-05 22:34:27 +020011723 encoder->new_crtc =
11724 to_intel_crtc(encoder->base.crtc);
11725 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011726
Damien Lespiaud3fcc802014-05-13 23:32:22 +010011727 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080011728 crtc->new_enabled = crtc->base.state->enable;
Ville Syrjälä76688512014-01-10 11:28:06 +020011729 }
Daniel Vetter9a935852012-07-05 22:34:27 +020011730}
11731
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011732/* Transitional helper to copy current connector/encoder state to
11733 * connector->state. This is needed so that code that is partially
11734 * converted to atomic does the right thing.
11735 */
11736static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
11737{
11738 struct intel_connector *connector;
11739
11740 for_each_intel_connector(dev, connector) {
11741 if (connector->base.encoder) {
11742 connector->base.state->best_encoder =
11743 connector->base.encoder;
11744 connector->base.state->crtc =
11745 connector->base.encoder->crtc;
11746 } else {
11747 connector->base.state->best_encoder = NULL;
11748 connector->base.state->crtc = NULL;
11749 }
11750 }
11751}
11752
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011753/* Fixup legacy state after an atomic state swap.
Daniel Vetter9a935852012-07-05 22:34:27 +020011754 */
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011755static void intel_modeset_fixup_state(struct drm_atomic_state *state)
Daniel Vetter9a935852012-07-05 22:34:27 +020011756{
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011757 struct intel_crtc *crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011758 struct intel_encoder *encoder;
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011759 struct intel_connector *connector;
Daniel Vetter9a935852012-07-05 22:34:27 +020011760
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011761 for_each_intel_connector(state->dev, connector) {
11762 connector->base.encoder = connector->base.state->best_encoder;
11763 if (connector->base.encoder)
11764 connector->base.encoder->crtc =
11765 connector->base.state->crtc;
Daniel Vetter9a935852012-07-05 22:34:27 +020011766 }
11767
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011768 /* Update crtc of disabled encoders */
11769 for_each_intel_encoder(state->dev, encoder) {
11770 int num_connectors = 0;
11771
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011772 for_each_intel_connector(state->dev, connector)
11773 if (connector->base.encoder == &encoder->base)
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011774 num_connectors++;
11775
11776 if (num_connectors == 0)
11777 encoder->base.crtc = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020011778 }
Ville Syrjälä76688512014-01-10 11:28:06 +020011779
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030011780 for_each_intel_crtc(state->dev, crtc) {
11781 crtc->base.enabled = crtc->base.state->enable;
11782 crtc->config = to_intel_crtc_state(crtc->base.state);
Ville Syrjälä76688512014-01-10 11:28:06 +020011783 }
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020011784
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030011785 /* Copy the new configuration to the staged state, to keep the few
11786 * pieces of code that haven't been converted yet happy */
11787 intel_modeset_update_staged_output_state(state->dev);
Daniel Vetter9a935852012-07-05 22:34:27 +020011788}
11789
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011790static void
Robin Schroereba905b2014-05-18 02:24:50 +020011791connected_sink_compute_bpp(struct intel_connector *connector,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011792 struct intel_crtc_state *pipe_config)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011793{
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011794 int bpp = pipe_config->pipe_bpp;
11795
11796 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] checking for sink bpp constrains\n",
11797 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030011798 connector->base.name);
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011799
11800 /* Don't use an invalid EDID bpc value */
11801 if (connector->base.display_info.bpc &&
11802 connector->base.display_info.bpc * 3 < bpp) {
11803 DRM_DEBUG_KMS("clamping display bpp (was %d) to EDID reported max of %d\n",
11804 bpp, connector->base.display_info.bpc*3);
11805 pipe_config->pipe_bpp = connector->base.display_info.bpc*3;
11806 }
11807
11808 /* Clamp bpp to 8 on screens without EDID 1.4 */
11809 if (connector->base.display_info.bpc == 0 && bpp > 24) {
11810 DRM_DEBUG_KMS("clamping display bpp (was %d) to default limit of 24\n",
11811 bpp);
11812 pipe_config->pipe_bpp = 24;
11813 }
11814}
11815
11816static int
11817compute_baseline_pipe_bpp(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011818 struct intel_crtc_state *pipe_config)
Daniel Vetter050f7ae2013-06-02 13:26:23 +020011819{
11820 struct drm_device *dev = crtc->base.dev;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011821 struct drm_atomic_state *state;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011822 struct drm_connector *connector;
11823 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011824 int bpp, i;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011825
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011826 if ((IS_G4X(dev) || IS_VALLEYVIEW(dev)))
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011827 bpp = 10*3;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020011828 else if (INTEL_INFO(dev)->gen >= 5)
11829 bpp = 12*3;
11830 else
11831 bpp = 8*3;
11832
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011833
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011834 pipe_config->pipe_bpp = bpp;
11835
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011836 state = pipe_config->base.state;
11837
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011838 /* Clamp display bpp to EDID value */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011839 for_each_connector_in_state(state, connector, connector_state, i) {
11840 if (connector_state->crtc != &crtc->base)
Ander Conselvan de Oliveira14860172015-03-20 16:18:09 +020011841 continue;
11842
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030011843 connected_sink_compute_bpp(to_intel_connector(connector),
11844 pipe_config);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010011845 }
11846
11847 return bpp;
11848}
11849
Daniel Vetter644db712013-09-19 14:53:58 +020011850static void intel_dump_crtc_timings(const struct drm_display_mode *mode)
11851{
11852 DRM_DEBUG_KMS("crtc timings: %d %d %d %d %d %d %d %d %d, "
11853 "type: 0x%x flags: 0x%x\n",
Damien Lespiau13428302013-09-25 16:45:36 +010011854 mode->crtc_clock,
Daniel Vetter644db712013-09-19 14:53:58 +020011855 mode->crtc_hdisplay, mode->crtc_hsync_start,
11856 mode->crtc_hsync_end, mode->crtc_htotal,
11857 mode->crtc_vdisplay, mode->crtc_vsync_start,
11858 mode->crtc_vsync_end, mode->crtc_vtotal, mode->type, mode->flags);
11859}
11860
Daniel Vetterc0b03412013-05-28 12:05:54 +020011861static void intel_dump_pipe_config(struct intel_crtc *crtc,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020011862 struct intel_crtc_state *pipe_config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020011863 const char *context)
11864{
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011865 struct drm_device *dev = crtc->base.dev;
11866 struct drm_plane *plane;
11867 struct intel_plane *intel_plane;
11868 struct intel_plane_state *state;
11869 struct drm_framebuffer *fb;
11870
11871 DRM_DEBUG_KMS("[CRTC:%d]%s config %p for pipe %c\n", crtc->base.base.id,
11872 context, pipe_config, pipe_name(crtc->pipe));
Daniel Vetterc0b03412013-05-28 12:05:54 +020011873
11874 DRM_DEBUG_KMS("cpu_transcoder: %c\n", transcoder_name(pipe_config->cpu_transcoder));
11875 DRM_DEBUG_KMS("pipe bpp: %i, dithering: %i\n",
11876 pipe_config->pipe_bpp, pipe_config->dither);
11877 DRM_DEBUG_KMS("fdi/pch: %i, lanes: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11878 pipe_config->has_pch_encoder,
11879 pipe_config->fdi_lanes,
11880 pipe_config->fdi_m_n.gmch_m, pipe_config->fdi_m_n.gmch_n,
11881 pipe_config->fdi_m_n.link_m, pipe_config->fdi_m_n.link_n,
11882 pipe_config->fdi_m_n.tu);
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030011883 DRM_DEBUG_KMS("dp: %i, gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
11884 pipe_config->has_dp_encoder,
11885 pipe_config->dp_m_n.gmch_m, pipe_config->dp_m_n.gmch_n,
11886 pipe_config->dp_m_n.link_m, pipe_config->dp_m_n.link_n,
11887 pipe_config->dp_m_n.tu);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070011888
11889 DRM_DEBUG_KMS("dp: %i, gmch_m2: %u, gmch_n2: %u, link_m2: %u, link_n2: %u, tu2: %u\n",
11890 pipe_config->has_dp_encoder,
11891 pipe_config->dp_m2_n2.gmch_m,
11892 pipe_config->dp_m2_n2.gmch_n,
11893 pipe_config->dp_m2_n2.link_m,
11894 pipe_config->dp_m2_n2.link_n,
11895 pipe_config->dp_m2_n2.tu);
11896
Daniel Vetter55072d12014-11-20 16:10:28 +010011897 DRM_DEBUG_KMS("audio: %i, infoframes: %i\n",
11898 pipe_config->has_audio,
11899 pipe_config->has_infoframe);
11900
Daniel Vetterc0b03412013-05-28 12:05:54 +020011901 DRM_DEBUG_KMS("requested mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011902 drm_mode_debug_printmodeline(&pipe_config->base.mode);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011903 DRM_DEBUG_KMS("adjusted mode:\n");
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020011904 drm_mode_debug_printmodeline(&pipe_config->base.adjusted_mode);
11905 intel_dump_crtc_timings(&pipe_config->base.adjusted_mode);
Ville Syrjäläd71b8d42013-09-06 23:29:08 +030011906 DRM_DEBUG_KMS("port clock: %d\n", pipe_config->port_clock);
Ville Syrjälä37327ab2013-09-04 18:25:28 +030011907 DRM_DEBUG_KMS("pipe src size: %dx%d\n",
11908 pipe_config->pipe_src_w, pipe_config->pipe_src_h);
Tvrtko Ursulin0ec463d2015-05-13 16:51:08 +010011909 DRM_DEBUG_KMS("num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
11910 crtc->num_scalers,
11911 pipe_config->scaler_state.scaler_users,
11912 pipe_config->scaler_state.scaler_id);
Daniel Vetterc0b03412013-05-28 12:05:54 +020011913 DRM_DEBUG_KMS("gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
11914 pipe_config->gmch_pfit.control,
11915 pipe_config->gmch_pfit.pgm_ratios,
11916 pipe_config->gmch_pfit.lvds_border_bits);
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011917 DRM_DEBUG_KMS("pch pfit: pos: 0x%08x, size: 0x%08x, %s\n",
Daniel Vetterc0b03412013-05-28 12:05:54 +020011918 pipe_config->pch_pfit.pos,
Chris Wilsonfd4daa92013-08-27 17:04:17 +010011919 pipe_config->pch_pfit.size,
11920 pipe_config->pch_pfit.enabled ? "enabled" : "disabled");
Paulo Zanoni42db64e2013-05-31 16:33:22 -030011921 DRM_DEBUG_KMS("ips: %i\n", pipe_config->ips_enabled);
Ville Syrjäläcf532bb2013-09-04 18:30:02 +030011922 DRM_DEBUG_KMS("double wide: %i\n", pipe_config->double_wide);
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011923
Tvrtko Ursulin415ff0f2015-05-14 13:38:31 +010011924 if (IS_BROXTON(dev)) {
11925 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: ebb0: 0x%x, "
11926 "pll0: 0x%x, pll1: 0x%x, pll2: 0x%x, pll3: 0x%x, "
11927 "pll6: 0x%x, pll8: 0x%x, pcsdw12: 0x%x\n",
11928 pipe_config->ddi_pll_sel,
11929 pipe_config->dpll_hw_state.ebb0,
11930 pipe_config->dpll_hw_state.pll0,
11931 pipe_config->dpll_hw_state.pll1,
11932 pipe_config->dpll_hw_state.pll2,
11933 pipe_config->dpll_hw_state.pll3,
11934 pipe_config->dpll_hw_state.pll6,
11935 pipe_config->dpll_hw_state.pll8,
11936 pipe_config->dpll_hw_state.pcsdw12);
11937 } else if (IS_SKYLAKE(dev)) {
11938 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: "
11939 "ctrl1: 0x%x, cfgcr1: 0x%x, cfgcr2: 0x%x\n",
11940 pipe_config->ddi_pll_sel,
11941 pipe_config->dpll_hw_state.ctrl1,
11942 pipe_config->dpll_hw_state.cfgcr1,
11943 pipe_config->dpll_hw_state.cfgcr2);
11944 } else if (HAS_DDI(dev)) {
11945 DRM_DEBUG_KMS("ddi_pll_sel: %u; dpll_hw_state: wrpll: 0x%x\n",
11946 pipe_config->ddi_pll_sel,
11947 pipe_config->dpll_hw_state.wrpll);
11948 } else {
11949 DRM_DEBUG_KMS("dpll_hw_state: dpll: 0x%x, dpll_md: 0x%x, "
11950 "fp0: 0x%x, fp1: 0x%x\n",
11951 pipe_config->dpll_hw_state.dpll,
11952 pipe_config->dpll_hw_state.dpll_md,
11953 pipe_config->dpll_hw_state.fp0,
11954 pipe_config->dpll_hw_state.fp1);
11955 }
11956
Chandra Konduru6a60cd82015-04-07 15:28:40 -070011957 DRM_DEBUG_KMS("planes on this crtc\n");
11958 list_for_each_entry(plane, &dev->mode_config.plane_list, head) {
11959 intel_plane = to_intel_plane(plane);
11960 if (intel_plane->pipe != crtc->pipe)
11961 continue;
11962
11963 state = to_intel_plane_state(plane->state);
11964 fb = state->base.fb;
11965 if (!fb) {
11966 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d "
11967 "disabled, scaler_id = %d\n",
11968 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11969 plane->base.id, intel_plane->pipe,
11970 (crtc->base.primary == plane) ? 0 : intel_plane->plane + 1,
11971 drm_plane_index(plane), state->scaler_id);
11972 continue;
11973 }
11974
11975 DRM_DEBUG_KMS("%s PLANE:%d plane: %u.%u idx: %d enabled",
11976 plane->type == DRM_PLANE_TYPE_CURSOR ? "CURSOR" : "STANDARD",
11977 plane->base.id, intel_plane->pipe,
11978 crtc->base.primary == plane ? 0 : intel_plane->plane + 1,
11979 drm_plane_index(plane));
11980 DRM_DEBUG_KMS("\tFB:%d, fb = %ux%u format = 0x%x",
11981 fb->base.id, fb->width, fb->height, fb->pixel_format);
11982 DRM_DEBUG_KMS("\tscaler:%d src (%u, %u) %ux%u dst (%u, %u) %ux%u\n",
11983 state->scaler_id,
11984 state->src.x1 >> 16, state->src.y1 >> 16,
11985 drm_rect_width(&state->src) >> 16,
11986 drm_rect_height(&state->src) >> 16,
11987 state->dst.x1, state->dst.y1,
11988 drm_rect_width(&state->dst), drm_rect_height(&state->dst));
11989 }
Daniel Vetterc0b03412013-05-28 12:05:54 +020011990}
11991
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011992static bool encoders_cloneable(const struct intel_encoder *a,
11993 const struct intel_encoder *b)
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011994{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020011995 /* masks could be asymmetric, so check both ways */
11996 return a == b || (a->cloneable & (1 << b->type) &&
11997 b->cloneable & (1 << a->type));
11998}
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020011999
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012000static bool check_single_encoder_cloning(struct drm_atomic_state *state,
12001 struct intel_crtc *crtc,
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012002 struct intel_encoder *encoder)
12003{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012004 struct intel_encoder *source_encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012005 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012006 struct drm_connector_state *connector_state;
12007 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012008
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012009 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012010 if (connector_state->crtc != &crtc->base)
12011 continue;
12012
12013 source_encoder =
12014 to_intel_encoder(connector_state->best_encoder);
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012015 if (!encoders_cloneable(encoder, source_encoder))
12016 return false;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012017 }
12018
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012019 return true;
12020}
12021
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012022static bool check_encoder_cloning(struct drm_atomic_state *state,
12023 struct intel_crtc *crtc)
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012024{
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012025 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012026 struct drm_connector *connector;
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012027 struct drm_connector_state *connector_state;
12028 int i;
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012029
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012030 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012031 if (connector_state->crtc != &crtc->base)
12032 continue;
12033
12034 encoder = to_intel_encoder(connector_state->best_encoder);
12035 if (!check_single_encoder_cloning(state, crtc, encoder))
Ville Syrjäläbc079e82014-03-03 16:15:28 +020012036 return false;
12037 }
12038
12039 return true;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012040}
12041
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012042static bool check_digital_port_conflicts(struct drm_atomic_state *state)
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012043{
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012044 struct drm_device *dev = state->dev;
12045 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012046 struct drm_connector *connector;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012047 struct drm_connector_state *connector_state;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012048 unsigned int used_ports = 0;
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012049 int i;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012050
12051 /*
12052 * Walk the connector list instead of the encoder
12053 * list to detect the problem on ddi platforms
12054 * where there's just one encoder per digital port.
12055 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012056 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012057 if (!connector_state->best_encoder)
12058 continue;
12059
12060 encoder = to_intel_encoder(connector_state->best_encoder);
12061
12062 WARN_ON(!connector_state->crtc);
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012063
12064 switch (encoder->type) {
12065 unsigned int port_mask;
12066 case INTEL_OUTPUT_UNKNOWN:
12067 if (WARN_ON(!HAS_DDI(dev)))
12068 break;
12069 case INTEL_OUTPUT_DISPLAYPORT:
12070 case INTEL_OUTPUT_HDMI:
12071 case INTEL_OUTPUT_EDP:
12072 port_mask = 1 << enc_to_dig_port(&encoder->base)->port;
12073
12074 /* the same port mustn't appear more than once */
12075 if (used_ports & port_mask)
12076 return false;
12077
12078 used_ports |= port_mask;
12079 default:
12080 break;
12081 }
12082 }
12083
12084 return true;
12085}
12086
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012087static void
12088clear_intel_crtc_state(struct intel_crtc_state *crtc_state)
12089{
12090 struct drm_crtc_state tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012091 struct intel_crtc_scaler_state scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012092 struct intel_dpll_hw_state dpll_hw_state;
12093 enum intel_dpll_id shared_dpll;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012094 uint32_t ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012095
Ander Conselvan de Oliveira7546a382015-05-20 09:03:27 +030012096 /* FIXME: before the switch to atomic started, a new pipe_config was
12097 * kzalloc'd. Code that depends on any field being zero should be
12098 * fixed, so that the crtc_state can be safely duplicated. For now,
12099 * only fields that are know to not cause problems are preserved. */
12100
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012101 tmp_state = crtc_state->base;
Chandra Konduru663a3642015-04-07 15:28:41 -070012102 scaler_state = crtc_state->scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012103 shared_dpll = crtc_state->shared_dpll;
12104 dpll_hw_state = crtc_state->dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012105 ddi_pll_sel = crtc_state->ddi_pll_sel;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012106
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012107 memset(crtc_state, 0, sizeof *crtc_state);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012108
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012109 crtc_state->base = tmp_state;
Chandra Konduru663a3642015-04-07 15:28:41 -070012110 crtc_state->scaler_state = scaler_state;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012111 crtc_state->shared_dpll = shared_dpll;
12112 crtc_state->dpll_hw_state = dpll_hw_state;
Ander Conselvan de Oliveira8504c742015-05-15 11:51:50 +030012113 crtc_state->ddi_pll_sel = ddi_pll_sel;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012114}
12115
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012116static int
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012117intel_modeset_pipe_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012118 struct drm_atomic_state *state,
12119 struct intel_crtc_state *pipe_config)
Daniel Vetter7758a112012-07-08 19:40:39 +020012120{
Daniel Vetter7758a112012-07-08 19:40:39 +020012121 struct intel_encoder *encoder;
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012122 struct drm_connector *connector;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012123 struct drm_connector_state *connector_state;
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012124 int base_bpp, ret = -EINVAL;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012125 int i;
Daniel Vettere29c22c2013-02-21 00:00:16 +010012126 bool retry = true;
Daniel Vetter7758a112012-07-08 19:40:39 +020012127
Ander Conselvan de Oliveira98a221d2015-04-02 14:48:00 +030012128 if (!check_encoder_cloning(state, to_intel_crtc(crtc))) {
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012129 DRM_DEBUG_KMS("rejecting invalid cloning configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012130 return -EINVAL;
Daniel Vetteraccfc0c2013-05-30 15:04:25 +020012131 }
12132
Ander Conselvan de Oliveira5448a002015-04-02 14:47:59 +030012133 if (!check_digital_port_conflicts(state)) {
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012134 DRM_DEBUG_KMS("rejecting conflicting digital port configuration\n");
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012135 return -EINVAL;
Ville Syrjälä00f0b372014-12-02 14:10:46 +020012136 }
12137
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012138 clear_intel_crtc_state(pipe_config);
Daniel Vetter7758a112012-07-08 19:40:39 +020012139
Daniel Vettere143a212013-07-04 12:01:15 +020012140 pipe_config->cpu_transcoder =
12141 (enum transcoder) to_intel_crtc(crtc)->pipe;
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010012142
Imre Deak2960bc92013-07-30 13:36:32 +030012143 /*
12144 * Sanitize sync polarity flags based on requested ones. If neither
12145 * positive or negative polarity is requested, treat this as meaning
12146 * negative polarity.
12147 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012148 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012149 (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012150 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012151
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012152 if (!(pipe_config->base.adjusted_mode.flags &
Imre Deak2960bc92013-07-30 13:36:32 +030012153 (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012154 pipe_config->base.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
Imre Deak2960bc92013-07-30 13:36:32 +030012155
Daniel Vetter050f7ae2013-06-02 13:26:23 +020012156 /* Compute a starting value for pipe_config->pipe_bpp taking the source
12157 * plane pixel format and any sink constraints into account. Returns the
12158 * source plane bpp so that dithering can be selected on mismatches
12159 * after encoders and crtc also have had their say. */
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012160 base_bpp = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
12161 pipe_config);
12162 if (base_bpp < 0)
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012163 goto fail;
12164
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012165 /*
12166 * Determine the real pipe dimensions. Note that stereo modes can
12167 * increase the actual pipe size due to the frame doubling and
12168 * insertion of additional space for blanks between the frame. This
12169 * is stored in the crtc timings. We use the requested mode to do this
12170 * computation to clearly distinguish it from the adjusted mode, which
12171 * can be changed by the connectors in the below retry loop.
12172 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012173 drm_crtc_get_hv_timing(&pipe_config->base.mode,
Gustavo Padovanecb7e162014-12-01 15:40:09 -080012174 &pipe_config->pipe_src_w,
12175 &pipe_config->pipe_src_h);
Ville Syrjäläe41a56b2013-10-01 22:52:14 +030012176
Daniel Vettere29c22c2013-02-21 00:00:16 +010012177encoder_retry:
Daniel Vetteref1b4602013-06-01 17:17:04 +020012178 /* Ensure the port clock defaults are reset when retrying. */
Daniel Vetterff9a6752013-06-01 17:16:21 +020012179 pipe_config->port_clock = 0;
Daniel Vetteref1b4602013-06-01 17:17:04 +020012180 pipe_config->pixel_multiplier = 1;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012181
Daniel Vetter135c81b2013-07-21 21:37:09 +020012182 /* Fill in default crtc timings, allow encoders to overwrite them. */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012183 drm_mode_set_crtcinfo(&pipe_config->base.adjusted_mode,
12184 CRTC_STEREO_DOUBLE);
Daniel Vetter135c81b2013-07-21 21:37:09 +020012185
Daniel Vetter7758a112012-07-08 19:40:39 +020012186 /* Pass our mode to the connectors and the CRTC to give them a chance to
12187 * adjust it according to limitations or connector properties, and also
12188 * a chance to reject the mode entirely.
12189 */
Ander Conselvan de Oliveirada3ced2982015-04-21 17:12:59 +030012190 for_each_connector_in_state(state, connector, connector_state, i) {
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012191 if (connector_state->crtc != crtc)
12192 continue;
12193
12194 encoder = to_intel_encoder(connector_state->best_encoder);
12195
Daniel Vetterefea6e82013-07-21 21:36:59 +020012196 if (!(encoder->compute_config(encoder, pipe_config))) {
12197 DRM_DEBUG_KMS("Encoder config failure\n");
Daniel Vetter7758a112012-07-08 19:40:39 +020012198 goto fail;
12199 }
12200 }
12201
Daniel Vetterff9a6752013-06-01 17:16:21 +020012202 /* Set default port clock if not overwritten by the encoder. Needs to be
12203 * done afterwards in case the encoder adjusts the mode. */
12204 if (!pipe_config->port_clock)
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012205 pipe_config->port_clock = pipe_config->base.adjusted_mode.crtc_clock
Damien Lespiau241bfc32013-09-25 16:45:37 +010012206 * pipe_config->pixel_multiplier;
Daniel Vetterff9a6752013-06-01 17:16:21 +020012207
Daniel Vettera43f6e02013-06-07 23:10:32 +020012208 ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
Daniel Vettere29c22c2013-02-21 00:00:16 +010012209 if (ret < 0) {
Daniel Vetter7758a112012-07-08 19:40:39 +020012210 DRM_DEBUG_KMS("CRTC fixup failed\n");
12211 goto fail;
12212 }
Daniel Vettere29c22c2013-02-21 00:00:16 +010012213
12214 if (ret == RETRY) {
12215 if (WARN(!retry, "loop in pipe configuration computation\n")) {
12216 ret = -EINVAL;
12217 goto fail;
12218 }
12219
12220 DRM_DEBUG_KMS("CRTC bw constrained, retrying\n");
12221 retry = false;
12222 goto encoder_retry;
12223 }
12224
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012225 pipe_config->dither = pipe_config->pipe_bpp != base_bpp;
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012226 DRM_DEBUG_KMS("plane bpp: %i, pipe bpp: %i, dithering: %i\n",
Daniel Vetterd328c9d2015-04-10 16:22:37 +020012227 base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
Daniel Vetter4e53c2e2013-03-27 00:44:58 +010012228
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012229 return 0;
Daniel Vetter7758a112012-07-08 19:40:39 +020012230fail:
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012231 return ret;
Daniel Vetter7758a112012-07-08 19:40:39 +020012232}
12233
Daniel Vetterea9d7582012-07-10 10:42:52 +020012234static bool intel_crtc_in_use(struct drm_crtc *crtc)
12235{
12236 struct drm_encoder *encoder;
12237 struct drm_device *dev = crtc->dev;
12238
12239 list_for_each_entry(encoder, &dev->mode_config.encoder_list, head)
12240 if (encoder->crtc == crtc)
12241 return true;
12242
12243 return false;
12244}
12245
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012246static bool
12247needs_modeset(struct drm_crtc_state *state)
Daniel Vetterea9d7582012-07-10 10:42:52 +020012248{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012249 return state->mode_changed || state->active_changed;
12250}
12251
12252static void
12253intel_modeset_update_state(struct drm_atomic_state *state)
12254{
12255 struct drm_device *dev = state->dev;
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012256 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012257 struct intel_encoder *intel_encoder;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012258 struct drm_crtc *crtc;
12259 struct drm_crtc_state *crtc_state;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012260 struct drm_connector *connector;
12261
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012262 intel_shared_dpll_commit(dev_priv);
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012263 drm_atomic_helper_swap_state(state->dev, state);
Daniel Vetterba41c0de2014-11-03 15:04:55 +010012264
Damien Lespiaub2784e12014-08-05 11:29:37 +010012265 for_each_intel_encoder(dev, intel_encoder) {
Daniel Vetterea9d7582012-07-10 10:42:52 +020012266 if (!intel_encoder->base.crtc)
12267 continue;
12268
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012269 crtc = intel_encoder->base.crtc;
12270 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12271 if (!crtc_state || !needs_modeset(crtc->state))
12272 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012273
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012274 intel_encoder->connectors_active = false;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012275 }
12276
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030012277 intel_modeset_fixup_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012278
Ville Syrjälä76688512014-01-10 11:28:06 +020012279 /* Double check state. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012280 for_each_crtc(dev, crtc) {
12281 WARN_ON(crtc->state->enable != intel_crtc_in_use(crtc));
Daniel Vetterea9d7582012-07-10 10:42:52 +020012282 }
12283
12284 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
12285 if (!connector->encoder || !connector->encoder->crtc)
12286 continue;
12287
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012288 crtc = connector->encoder->crtc;
12289 crtc_state = drm_atomic_get_existing_crtc_state(state, crtc);
12290 if (!crtc_state || !needs_modeset(crtc->state))
12291 continue;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012292
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012293 if (crtc->state->enable) {
12294 struct drm_property *dpms_property =
12295 dev->mode_config.dpms_property;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012296
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012297 connector->dpms = DRM_MODE_DPMS_ON;
12298 drm_object_property_set_value(&connector->base, dpms_property, DRM_MODE_DPMS_ON);
Daniel Vetter68d34722012-09-06 22:08:35 +020012299
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012300 intel_encoder = to_intel_encoder(connector->encoder);
12301 intel_encoder->connectors_active = true;
12302 } else
12303 connector->dpms = DRM_MODE_DPMS_OFF;
Daniel Vetterea9d7582012-07-10 10:42:52 +020012304 }
Daniel Vetterea9d7582012-07-10 10:42:52 +020012305}
12306
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012307static bool intel_fuzzy_clock_check(int clock1, int clock2)
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012308{
Ville Syrjälä3bd26262013-09-06 23:29:02 +030012309 int diff;
Jesse Barnesf1f644d2013-06-27 00:39:25 +030012310
12311 if (clock1 == clock2)
12312 return true;
12313
12314 if (!clock1 || !clock2)
12315 return false;
12316
12317 diff = abs(clock1 - clock2);
12318
12319 if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
12320 return true;
12321
12322 return false;
12323}
12324
Daniel Vetter25c5b262012-07-08 22:08:04 +020012325#define for_each_intel_crtc_masked(dev, mask, intel_crtc) \
12326 list_for_each_entry((intel_crtc), \
12327 &(dev)->mode_config.crtc_list, \
12328 base.head) \
Daniel Vetter0973f182013-04-19 11:25:33 +020012329 if (mask & (1 <<(intel_crtc)->pipe))
Daniel Vetter25c5b262012-07-08 22:08:04 +020012330
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012331static bool
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012332intel_pipe_config_compare(struct drm_device *dev,
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012333 struct intel_crtc_state *current_config,
12334 struct intel_crtc_state *pipe_config)
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012335{
Daniel Vetter66e985c2013-06-05 13:34:20 +020012336#define PIPE_CONF_CHECK_X(name) \
12337 if (current_config->name != pipe_config->name) { \
12338 DRM_ERROR("mismatch in " #name " " \
12339 "(expected 0x%08x, found 0x%08x)\n", \
12340 current_config->name, \
12341 pipe_config->name); \
12342 return false; \
12343 }
12344
Daniel Vetter08a24032013-04-19 11:25:34 +020012345#define PIPE_CONF_CHECK_I(name) \
12346 if (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; \
Daniel Vetter88adfff2013-03-28 10:42:01 +010012352 }
12353
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012354/* This is required for BDW+ where there is only one set of registers for
12355 * switching between high and low RR.
12356 * This macro can be used whenever a comparison has to be made between one
12357 * hw state and multiple sw state variables.
12358 */
12359#define PIPE_CONF_CHECK_I_ALT(name, alt_name) \
12360 if ((current_config->name != pipe_config->name) && \
12361 (current_config->alt_name != pipe_config->name)) { \
12362 DRM_ERROR("mismatch in " #name " " \
12363 "(expected %i or %i, found %i)\n", \
12364 current_config->name, \
12365 current_config->alt_name, \
12366 pipe_config->name); \
12367 return false; \
12368 }
12369
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012370#define PIPE_CONF_CHECK_FLAGS(name, mask) \
12371 if ((current_config->name ^ pipe_config->name) & (mask)) { \
Jesse Barnes6f024882013-07-01 10:19:09 -070012372 DRM_ERROR("mismatch in " #name "(" #mask ") " \
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012373 "(expected %i, found %i)\n", \
12374 current_config->name & (mask), \
12375 pipe_config->name & (mask)); \
12376 return false; \
12377 }
12378
Ville Syrjälä5e550652013-09-06 23:29:07 +030012379#define PIPE_CONF_CHECK_CLOCK_FUZZY(name) \
12380 if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
12381 DRM_ERROR("mismatch in " #name " " \
12382 "(expected %i, found %i)\n", \
12383 current_config->name, \
12384 pipe_config->name); \
12385 return false; \
12386 }
12387
Daniel Vetterbb760062013-06-06 14:55:52 +020012388#define PIPE_CONF_QUIRK(quirk) \
12389 ((current_config->quirks | pipe_config->quirks) & (quirk))
12390
Daniel Vettereccb1402013-05-22 00:50:22 +020012391 PIPE_CONF_CHECK_I(cpu_transcoder);
12392
Daniel Vetter08a24032013-04-19 11:25:34 +020012393 PIPE_CONF_CHECK_I(has_pch_encoder);
12394 PIPE_CONF_CHECK_I(fdi_lanes);
Daniel Vetter72419202013-04-04 13:28:53 +020012395 PIPE_CONF_CHECK_I(fdi_m_n.gmch_m);
12396 PIPE_CONF_CHECK_I(fdi_m_n.gmch_n);
12397 PIPE_CONF_CHECK_I(fdi_m_n.link_m);
12398 PIPE_CONF_CHECK_I(fdi_m_n.link_n);
12399 PIPE_CONF_CHECK_I(fdi_m_n.tu);
Daniel Vetter08a24032013-04-19 11:25:34 +020012400
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012401 PIPE_CONF_CHECK_I(has_dp_encoder);
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012402
12403 if (INTEL_INFO(dev)->gen < 8) {
12404 PIPE_CONF_CHECK_I(dp_m_n.gmch_m);
12405 PIPE_CONF_CHECK_I(dp_m_n.gmch_n);
12406 PIPE_CONF_CHECK_I(dp_m_n.link_m);
12407 PIPE_CONF_CHECK_I(dp_m_n.link_n);
12408 PIPE_CONF_CHECK_I(dp_m_n.tu);
12409
12410 if (current_config->has_drrs) {
12411 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_m);
12412 PIPE_CONF_CHECK_I(dp_m2_n2.gmch_n);
12413 PIPE_CONF_CHECK_I(dp_m2_n2.link_m);
12414 PIPE_CONF_CHECK_I(dp_m2_n2.link_n);
12415 PIPE_CONF_CHECK_I(dp_m2_n2.tu);
12416 }
12417 } else {
12418 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_m, dp_m2_n2.gmch_m);
12419 PIPE_CONF_CHECK_I_ALT(dp_m_n.gmch_n, dp_m2_n2.gmch_n);
12420 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_m, dp_m2_n2.link_m);
12421 PIPE_CONF_CHECK_I_ALT(dp_m_n.link_n, dp_m2_n2.link_n);
12422 PIPE_CONF_CHECK_I_ALT(dp_m_n.tu, dp_m2_n2.tu);
12423 }
Ville Syrjäläeb14cb72013-09-10 17:02:54 +030012424
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012425 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hdisplay);
12426 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_htotal);
12427 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_start);
12428 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hblank_end);
12429 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_start);
12430 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_hsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012431
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012432 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vdisplay);
12433 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vtotal);
12434 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_start);
12435 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vblank_end);
12436 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_start);
12437 PIPE_CONF_CHECK_I(base.adjusted_mode.crtc_vsync_end);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012438
Daniel Vetterc93f54c2013-06-27 19:47:19 +020012439 PIPE_CONF_CHECK_I(pixel_multiplier);
Daniel Vetter6897b4b52014-04-24 23:54:47 +020012440 PIPE_CONF_CHECK_I(has_hdmi_sink);
Daniel Vetterb5a9fa02014-04-24 23:54:49 +020012441 if ((INTEL_INFO(dev)->gen < 8 && !IS_HASWELL(dev)) ||
12442 IS_VALLEYVIEW(dev))
12443 PIPE_CONF_CHECK_I(limited_color_range);
Jesse Barnese43823e2014-11-05 14:26:08 -080012444 PIPE_CONF_CHECK_I(has_infoframe);
Daniel Vetter6c49f242013-06-06 12:45:25 +020012445
Daniel Vetter9ed109a2014-04-24 23:54:52 +020012446 PIPE_CONF_CHECK_I(has_audio);
12447
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012448 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012449 DRM_MODE_FLAG_INTERLACE);
12450
Daniel Vetterbb760062013-06-06 14:55:52 +020012451 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012452 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012453 DRM_MODE_FLAG_PHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012454 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012455 DRM_MODE_FLAG_NHSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012456 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012457 DRM_MODE_FLAG_PVSYNC);
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012458 PIPE_CONF_CHECK_FLAGS(base.adjusted_mode.flags,
Daniel Vetterbb760062013-06-06 14:55:52 +020012459 DRM_MODE_FLAG_NVSYNC);
12460 }
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012461
Ville Syrjälä37327ab2013-09-04 18:25:28 +030012462 PIPE_CONF_CHECK_I(pipe_src_w);
12463 PIPE_CONF_CHECK_I(pipe_src_h);
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012464
Daniel Vetter99535992014-04-13 12:00:33 +020012465 /*
12466 * FIXME: BIOS likes to set up a cloned config with lvds+external
12467 * screen. Since we don't yet re-compute the pipe config when moving
12468 * just the lvds port away to another pipe the sw tracking won't match.
12469 *
12470 * Proper atomic modesets with recomputed global state will fix this.
12471 * Until then just don't check gmch state for inherited modes.
12472 */
12473 if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_INHERITED_MODE)) {
12474 PIPE_CONF_CHECK_I(gmch_pfit.control);
12475 /* pfit ratios are autocomputed by the hw on gen4+ */
12476 if (INTEL_INFO(dev)->gen < 4)
12477 PIPE_CONF_CHECK_I(gmch_pfit.pgm_ratios);
12478 PIPE_CONF_CHECK_I(gmch_pfit.lvds_border_bits);
12479 }
12480
Chris Wilsonfd4daa92013-08-27 17:04:17 +010012481 PIPE_CONF_CHECK_I(pch_pfit.enabled);
12482 if (current_config->pch_pfit.enabled) {
12483 PIPE_CONF_CHECK_I(pch_pfit.pos);
12484 PIPE_CONF_CHECK_I(pch_pfit.size);
12485 }
Daniel Vetter2fa2fe92013-05-07 23:34:16 +020012486
Chandra Kondurua1b22782015-04-07 15:28:45 -070012487 PIPE_CONF_CHECK_I(scaler_state.scaler_id);
12488
Jesse Barnese59150d2014-01-07 13:30:45 -080012489 /* BDW+ don't expose a synchronous way to read the state */
12490 if (IS_HASWELL(dev))
12491 PIPE_CONF_CHECK_I(ips_enabled);
Paulo Zanoni42db64e2013-05-31 16:33:22 -030012492
Ville Syrjälä282740f2013-09-04 18:30:03 +030012493 PIPE_CONF_CHECK_I(double_wide);
12494
Daniel Vetter26804af2014-06-25 22:01:55 +030012495 PIPE_CONF_CHECK_X(ddi_pll_sel);
12496
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012497 PIPE_CONF_CHECK_I(shared_dpll);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012498 PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
Daniel Vetter8bcc2792013-06-05 13:34:28 +020012499 PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012500 PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
12501 PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
Daniel Vetterd452c5b2014-07-04 11:27:39 -030012502 PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
Damien Lespiau3f4cd192014-11-13 14:55:21 +000012503 PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
12504 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
12505 PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
Daniel Vetterc0d43d62013-06-07 23:11:08 +020012506
Ville Syrjälä42571ae2013-09-06 23:29:00 +030012507 if (IS_G4X(dev) || INTEL_INFO(dev)->gen >= 5)
12508 PIPE_CONF_CHECK_I(pipe_bpp);
12509
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012510 PIPE_CONF_CHECK_CLOCK_FUZZY(base.adjusted_mode.crtc_clock);
Jesse Barnesa9a7e982014-01-20 14:18:04 -080012511 PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
Ville Syrjälä5e550652013-09-06 23:29:07 +030012512
Daniel Vetter66e985c2013-06-05 13:34:20 +020012513#undef PIPE_CONF_CHECK_X
Daniel Vetter08a24032013-04-19 11:25:34 +020012514#undef PIPE_CONF_CHECK_I
Vandana Kannanb95af8b2014-08-05 07:51:23 -070012515#undef PIPE_CONF_CHECK_I_ALT
Daniel Vetter1bd1bd82013-04-29 21:56:12 +020012516#undef PIPE_CONF_CHECK_FLAGS
Ville Syrjälä5e550652013-09-06 23:29:07 +030012517#undef PIPE_CONF_CHECK_CLOCK_FUZZY
Daniel Vetterbb760062013-06-06 14:55:52 +020012518#undef PIPE_CONF_QUIRK
Daniel Vetter627eb5a2013-04-29 19:33:42 +020012519
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012520 return true;
12521}
12522
Damien Lespiau08db6652014-11-04 17:06:52 +000012523static void check_wm_state(struct drm_device *dev)
12524{
12525 struct drm_i915_private *dev_priv = dev->dev_private;
12526 struct skl_ddb_allocation hw_ddb, *sw_ddb;
12527 struct intel_crtc *intel_crtc;
12528 int plane;
12529
12530 if (INTEL_INFO(dev)->gen < 9)
12531 return;
12532
12533 skl_ddb_get_hw_state(dev_priv, &hw_ddb);
12534 sw_ddb = &dev_priv->wm.skl_hw.ddb;
12535
12536 for_each_intel_crtc(dev, intel_crtc) {
12537 struct skl_ddb_entry *hw_entry, *sw_entry;
12538 const enum pipe pipe = intel_crtc->pipe;
12539
12540 if (!intel_crtc->active)
12541 continue;
12542
12543 /* planes */
Damien Lespiaudd740782015-02-28 14:54:08 +000012544 for_each_plane(dev_priv, pipe, plane) {
Damien Lespiau08db6652014-11-04 17:06:52 +000012545 hw_entry = &hw_ddb.plane[pipe][plane];
12546 sw_entry = &sw_ddb->plane[pipe][plane];
12547
12548 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12549 continue;
12550
12551 DRM_ERROR("mismatch in DDB state pipe %c plane %d "
12552 "(expected (%u,%u), found (%u,%u))\n",
12553 pipe_name(pipe), plane + 1,
12554 sw_entry->start, sw_entry->end,
12555 hw_entry->start, hw_entry->end);
12556 }
12557
12558 /* cursor */
12559 hw_entry = &hw_ddb.cursor[pipe];
12560 sw_entry = &sw_ddb->cursor[pipe];
12561
12562 if (skl_ddb_entry_equal(hw_entry, sw_entry))
12563 continue;
12564
12565 DRM_ERROR("mismatch in DDB state pipe %c cursor "
12566 "(expected (%u,%u), found (%u,%u))\n",
12567 pipe_name(pipe),
12568 sw_entry->start, sw_entry->end,
12569 hw_entry->start, hw_entry->end);
12570 }
12571}
12572
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012573static void
12574check_connector_state(struct drm_device *dev)
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012575{
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012576 struct intel_connector *connector;
12577
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012578 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012579 /* This also checks the encoder/connector hw state with the
12580 * ->get_hw_state callbacks. */
12581 intel_connector_check_state(connector);
12582
Rob Clarke2c719b2014-12-15 13:56:32 -050012583 I915_STATE_WARN(&connector->new_encoder->base != connector->base.encoder,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012584 "connector's staged encoder doesn't match current encoder\n");
12585 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012586}
12587
12588static void
12589check_encoder_state(struct drm_device *dev)
12590{
12591 struct intel_encoder *encoder;
12592 struct intel_connector *connector;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012593
Damien Lespiaub2784e12014-08-05 11:29:37 +010012594 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012595 bool enabled = false;
12596 bool active = false;
12597 enum pipe pipe, tracked_pipe;
12598
12599 DRM_DEBUG_KMS("[ENCODER:%d:%s]\n",
12600 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030012601 encoder->base.name);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012602
Rob Clarke2c719b2014-12-15 13:56:32 -050012603 I915_STATE_WARN(&encoder->new_crtc->base != encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012604 "encoder's stage crtc doesn't match current crtc\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012605 I915_STATE_WARN(encoder->connectors_active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012606 "encoder's active_connectors set, but no crtc\n");
12607
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020012608 for_each_intel_connector(dev, connector) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012609 if (connector->base.encoder != &encoder->base)
12610 continue;
12611 enabled = true;
12612 if (connector->base.dpms != DRM_MODE_DPMS_OFF)
12613 active = true;
12614 }
Dave Airlie0e32b392014-05-02 14:02:48 +100012615 /*
12616 * for MST connectors if we unplug the connector is gone
12617 * away but the encoder is still connected to a crtc
12618 * until a modeset happens in response to the hotplug.
12619 */
12620 if (!enabled && encoder->base.encoder_type == DRM_MODE_ENCODER_DPMST)
12621 continue;
12622
Rob Clarke2c719b2014-12-15 13:56:32 -050012623 I915_STATE_WARN(!!encoder->base.crtc != enabled,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012624 "encoder's enabled state mismatch "
12625 "(expected %i, found %i)\n",
12626 !!encoder->base.crtc, enabled);
Rob Clarke2c719b2014-12-15 13:56:32 -050012627 I915_STATE_WARN(active && !encoder->base.crtc,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012628 "active encoder with no crtc\n");
12629
Rob Clarke2c719b2014-12-15 13:56:32 -050012630 I915_STATE_WARN(encoder->connectors_active != active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012631 "encoder's computed active state doesn't match tracked active state "
12632 "(expected %i, found %i)\n", active, encoder->connectors_active);
12633
12634 active = encoder->get_hw_state(encoder, &pipe);
Rob Clarke2c719b2014-12-15 13:56:32 -050012635 I915_STATE_WARN(active != encoder->connectors_active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012636 "encoder's hw state doesn't match sw tracking "
12637 "(expected %i, found %i)\n",
12638 encoder->connectors_active, active);
12639
12640 if (!encoder->base.crtc)
12641 continue;
12642
12643 tracked_pipe = to_intel_crtc(encoder->base.crtc)->pipe;
Rob Clarke2c719b2014-12-15 13:56:32 -050012644 I915_STATE_WARN(active && pipe != tracked_pipe,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012645 "active encoder's pipe doesn't match"
12646 "(expected %i, found %i)\n",
12647 tracked_pipe, pipe);
12648
12649 }
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012650}
12651
12652static void
12653check_crtc_state(struct drm_device *dev)
12654{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012655 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012656 struct intel_crtc *crtc;
12657 struct intel_encoder *encoder;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012658 struct intel_crtc_state pipe_config;
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012659
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012660 for_each_intel_crtc(dev, crtc) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012661 bool enabled = false;
12662 bool active = false;
12663
Jesse Barnes045ac3b2013-05-14 17:08:26 -070012664 memset(&pipe_config, 0, sizeof(pipe_config));
12665
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012666 DRM_DEBUG_KMS("[CRTC:%d]\n",
12667 crtc->base.base.id);
12668
Matt Roper83d65732015-02-25 13:12:16 -080012669 I915_STATE_WARN(crtc->active && !crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012670 "active crtc, but not enabled in sw tracking\n");
12671
Damien Lespiaub2784e12014-08-05 11:29:37 +010012672 for_each_intel_encoder(dev, encoder) {
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012673 if (encoder->base.crtc != &crtc->base)
12674 continue;
12675 enabled = true;
12676 if (encoder->connectors_active)
12677 active = true;
12678 }
Daniel Vetter6c49f242013-06-06 12:45:25 +020012679
Rob Clarke2c719b2014-12-15 13:56:32 -050012680 I915_STATE_WARN(active != crtc->active,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012681 "crtc's computed active state doesn't match tracked active state "
12682 "(expected %i, found %i)\n", active, crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080012683 I915_STATE_WARN(enabled != crtc->base.state->enable,
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012684 "crtc's computed enabled state doesn't match tracked enabled state "
Matt Roper83d65732015-02-25 13:12:16 -080012685 "(expected %i, found %i)\n", enabled,
12686 crtc->base.state->enable);
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012687
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012688 active = dev_priv->display.get_pipe_config(crtc,
12689 &pipe_config);
Daniel Vetterd62cf622013-05-29 10:41:29 +020012690
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030012691 /* hw state is inconsistent with the pipe quirk */
12692 if ((crtc->pipe == PIPE_A && dev_priv->quirks & QUIRK_PIPEA_FORCE) ||
12693 (crtc->pipe == PIPE_B && dev_priv->quirks & QUIRK_PIPEB_FORCE))
Daniel Vetterd62cf622013-05-29 10:41:29 +020012694 active = crtc->active;
12695
Damien Lespiaub2784e12014-08-05 11:29:37 +010012696 for_each_intel_encoder(dev, encoder) {
Ville Syrjälä3eaba512013-08-05 17:57:48 +030012697 enum pipe pipe;
Daniel Vetter6c49f242013-06-06 12:45:25 +020012698 if (encoder->base.crtc != &crtc->base)
12699 continue;
Daniel Vetter1d37b682013-11-18 09:00:59 +010012700 if (encoder->get_hw_state(encoder, &pipe))
Daniel Vetter6c49f242013-06-06 12:45:25 +020012701 encoder->get_config(encoder, &pipe_config);
12702 }
12703
Rob Clarke2c719b2014-12-15 13:56:32 -050012704 I915_STATE_WARN(crtc->active != active,
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010012705 "crtc active state doesn't match with hw state "
12706 "(expected %i, found %i)\n", crtc->active, active);
12707
Daniel Vetterc0b03412013-05-28 12:05:54 +020012708 if (active &&
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012709 !intel_pipe_config_compare(dev, crtc->config, &pipe_config)) {
Rob Clarke2c719b2014-12-15 13:56:32 -050012710 I915_STATE_WARN(1, "pipe state doesn't match!\n");
Daniel Vetterc0b03412013-05-28 12:05:54 +020012711 intel_dump_pipe_config(crtc, &pipe_config,
12712 "[hw state]");
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012713 intel_dump_pipe_config(crtc, crtc->config,
Daniel Vetterc0b03412013-05-28 12:05:54 +020012714 "[sw state]");
12715 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020012716 }
12717}
12718
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012719static void
12720check_shared_dpll_state(struct drm_device *dev)
12721{
Jani Nikulafbee40d2014-03-31 14:27:18 +030012722 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012723 struct intel_crtc *crtc;
12724 struct intel_dpll_hw_state dpll_hw_state;
12725 int i;
Daniel Vetter53589012013-06-05 13:34:16 +020012726
12727 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
12728 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
12729 int enabled_crtcs = 0, active_crtcs = 0;
12730 bool active;
12731
12732 memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
12733
12734 DRM_DEBUG_KMS("%s\n", pll->name);
12735
12736 active = pll->get_hw_state(dev_priv, pll, &dpll_hw_state);
12737
Rob Clarke2c719b2014-12-15 13:56:32 -050012738 I915_STATE_WARN(pll->active > hweight32(pll->config.crtc_mask),
Daniel Vetter53589012013-06-05 13:34:16 +020012739 "more active pll users than references: %i vs %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012740 pll->active, hweight32(pll->config.crtc_mask));
Rob Clarke2c719b2014-12-15 13:56:32 -050012741 I915_STATE_WARN(pll->active && !pll->on,
Daniel Vetter53589012013-06-05 13:34:16 +020012742 "pll in active use but not on in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012743 I915_STATE_WARN(pll->on && !pll->active,
Daniel Vetter35c95372013-07-17 06:55:04 +020012744 "pll in on but not on in use in sw tracking\n");
Rob Clarke2c719b2014-12-15 13:56:32 -050012745 I915_STATE_WARN(pll->on != active,
Daniel Vetter53589012013-06-05 13:34:16 +020012746 "pll on state mismatch (expected %i, found %i)\n",
12747 pll->on, active);
12748
Damien Lespiaud3fcc802014-05-13 23:32:22 +010012749 for_each_intel_crtc(dev, crtc) {
Matt Roper83d65732015-02-25 13:12:16 -080012750 if (crtc->base.state->enable && intel_crtc_to_shared_dpll(crtc) == pll)
Daniel Vetter53589012013-06-05 13:34:16 +020012751 enabled_crtcs++;
12752 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll)
12753 active_crtcs++;
12754 }
Rob Clarke2c719b2014-12-15 13:56:32 -050012755 I915_STATE_WARN(pll->active != active_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012756 "pll active crtcs mismatch (expected %i, found %i)\n",
12757 pll->active, active_crtcs);
Rob Clarke2c719b2014-12-15 13:56:32 -050012758 I915_STATE_WARN(hweight32(pll->config.crtc_mask) != enabled_crtcs,
Daniel Vetter53589012013-06-05 13:34:16 +020012759 "pll enabled crtcs mismatch (expected %i, found %i)\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020012760 hweight32(pll->config.crtc_mask), enabled_crtcs);
Daniel Vetter66e985c2013-06-05 13:34:20 +020012761
Rob Clarke2c719b2014-12-15 13:56:32 -050012762 I915_STATE_WARN(pll->on && memcmp(&pll->config.hw_state, &dpll_hw_state,
Daniel Vetter66e985c2013-06-05 13:34:20 +020012763 sizeof(dpll_hw_state)),
12764 "pll hw state mismatch\n");
Daniel Vetter53589012013-06-05 13:34:16 +020012765 }
Daniel Vettere2e1ed42012-07-08 21:14:38 +020012766}
12767
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012768void
12769intel_modeset_check_state(struct drm_device *dev)
12770{
Damien Lespiau08db6652014-11-04 17:06:52 +000012771 check_wm_state(dev);
Daniel Vetter91d1b4b2013-06-05 13:34:18 +020012772 check_connector_state(dev);
12773 check_encoder_state(dev);
12774 check_crtc_state(dev);
12775 check_shared_dpll_state(dev);
12776}
12777
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012778void ironlake_check_encoder_dotclock(const struct intel_crtc_state *pipe_config,
Ville Syrjälä18442d02013-09-13 16:00:08 +030012779 int dotclock)
12780{
12781 /*
12782 * FDI already provided one idea for the dotclock.
12783 * Yell if the encoder disagrees.
12784 */
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012785 WARN(!intel_fuzzy_clock_check(pipe_config->base.adjusted_mode.crtc_clock, dotclock),
Ville Syrjälä18442d02013-09-13 16:00:08 +030012786 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020012787 pipe_config->base.adjusted_mode.crtc_clock, dotclock);
Ville Syrjälä18442d02013-09-13 16:00:08 +030012788}
12789
Ville Syrjälä80715b22014-05-15 20:23:23 +030012790static void update_scanline_offset(struct intel_crtc *crtc)
12791{
12792 struct drm_device *dev = crtc->base.dev;
12793
12794 /*
12795 * The scanline counter increments at the leading edge of hsync.
12796 *
12797 * On most platforms it starts counting from vtotal-1 on the
12798 * first active line. That means the scanline counter value is
12799 * always one less than what we would expect. Ie. just after
12800 * start of vblank, which also occurs at start of hsync (on the
12801 * last active line), the scanline counter will read vblank_start-1.
12802 *
12803 * On gen2 the scanline counter starts counting from 1 instead
12804 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
12805 * to keep the value positive), instead of adding one.
12806 *
12807 * On HSW+ the behaviour of the scanline counter depends on the output
12808 * type. For DP ports it behaves like most other platforms, but on HDMI
12809 * there's an extra 1 line difference. So we need to add two instead of
12810 * one to the value.
12811 */
12812 if (IS_GEN2(dev)) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020012813 const struct drm_display_mode *mode = &crtc->config->base.adjusted_mode;
Ville Syrjälä80715b22014-05-15 20:23:23 +030012814 int vtotal;
12815
12816 vtotal = mode->crtc_vtotal;
12817 if (mode->flags & DRM_MODE_FLAG_INTERLACE)
12818 vtotal /= 2;
12819
12820 crtc->scanline_offset = vtotal - 1;
12821 } else if (HAS_DDI(dev) &&
Ander Conselvan de Oliveira409ee762014-10-20 13:46:45 +030012822 intel_pipe_has_type(crtc, INTEL_OUTPUT_HDMI)) {
Ville Syrjälä80715b22014-05-15 20:23:23 +030012823 crtc->scanline_offset = 2;
12824 } else
12825 crtc->scanline_offset = 1;
12826}
12827
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020012828static struct intel_crtc_state *
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012829intel_modeset_compute_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012830 struct drm_atomic_state *state)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012831{
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012832 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira0b901872015-03-20 16:18:08 +020012833 int ret = 0;
12834
12835 ret = drm_atomic_add_affected_connectors(state, crtc);
12836 if (ret)
12837 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012838
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012839 ret = drm_atomic_helper_check_modeset(state->dev, state);
12840 if (ret)
12841 return ERR_PTR(ret);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012842
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012843 /*
12844 * Note this needs changes when we start tracking multiple modes
12845 * and crtcs. At that point we'll need to compute the whole config
12846 * (i.e. one pipe_config for each crtc) rather than just the one
12847 * for this crtc.
12848 */
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012849 pipe_config = intel_atomic_get_crtc_state(state, to_intel_crtc(crtc));
12850 if (IS_ERR(pipe_config))
12851 return pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020012852
Ander Conselvan de Oliveira4fed33f2015-04-21 17:13:03 +030012853 if (!pipe_config->base.enable)
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012854 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012855
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012856 ret = intel_modeset_pipe_config(crtc, state, pipe_config);
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012857 if (ret)
12858 return ERR_PTR(ret);
Ander Conselvan de Oliveiradb7542d2015-03-20 16:18:04 +020012859
Ander Conselvan de Oliveira8d8c9b52015-04-21 17:13:11 +030012860 /* Check things that can only be changed through modeset */
12861 if (pipe_config->has_audio !=
12862 to_intel_crtc(crtc)->config->has_audio)
12863 pipe_config->base.mode_changed = true;
12864
12865 /*
12866 * Note we have an issue here with infoframes: current code
12867 * only updates them on the full mode set path per hw
12868 * requirements. So here we should be checking for any
12869 * required changes and forcing a mode set.
12870 */
12871
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012872 intel_dump_pipe_config(to_intel_crtc(crtc), pipe_config,"[modeset]");
12873
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012874 ret = drm_atomic_helper_check_planes(state->dev, state);
12875 if (ret)
12876 return ERR_PTR(ret);
12877
Ander Conselvan de Oliveira548ee152015-04-21 17:13:02 +030012878 return pipe_config;
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012879}
12880
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012881static int __intel_set_mode_setup_plls(struct drm_atomic_state *state)
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012882{
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012883 struct drm_device *dev = state->dev;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012884 struct drm_i915_private *dev_priv = to_i915(dev);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012885 unsigned clear_pipes = 0;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012886 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012887 struct intel_crtc_state *intel_crtc_state;
12888 struct drm_crtc *crtc;
12889 struct drm_crtc_state *crtc_state;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012890 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012891 int i;
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012892
12893 if (!dev_priv->display.crtc_compute_clock)
12894 return 0;
12895
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012896 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12897 intel_crtc = to_intel_crtc(crtc);
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012898 intel_crtc_state = to_intel_crtc_state(crtc_state);
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012899
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012900 if (needs_modeset(crtc_state)) {
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012901 clear_pipes |= 1 << intel_crtc->pipe;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012902 intel_crtc_state->shared_dpll = DPLL_ID_PRIVATE;
Ander Conselvan de Oliveira4978cc92015-04-21 17:13:21 +030012903 }
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012904 }
12905
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012906 ret = intel_shared_dpll_start_config(dev_priv, clear_pipes);
12907 if (ret)
12908 goto done;
12909
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012910 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12911 if (!needs_modeset(crtc_state) || !crtc_state->enable)
Ander Conselvan de Oliveira225da592015-04-02 14:47:57 +030012912 continue;
12913
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012914 intel_crtc = to_intel_crtc(crtc);
12915 intel_crtc_state = to_intel_crtc_state(crtc_state);
12916
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012917 ret = dev_priv->display.crtc_compute_clock(intel_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012918 intel_crtc_state);
Ander Conselvan de Oliveiraed6739e2015-01-29 16:55:08 +020012919 if (ret) {
12920 intel_shared_dpll_abort_config(dev_priv);
12921 goto done;
12922 }
12923 }
12924
12925done:
12926 return ret;
12927}
12928
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012929/* Code that should eventually be part of atomic_check() */
12930static int __intel_set_mode_checks(struct drm_atomic_state *state)
12931{
12932 struct drm_device *dev = state->dev;
12933 int ret;
12934
12935 /*
12936 * See if the config requires any additional preparation, e.g.
12937 * to adjust global state with pipes off. We need to do this
12938 * here so we can get the modeset_pipe updated config for the new
12939 * mode set on this crtc. For other crtcs we need to use the
12940 * adjusted_mode bits in the crtc directly.
12941 */
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030012942 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev) || IS_BROADWELL(dev)) {
12943 if (IS_VALLEYVIEW(dev) || IS_BROXTON(dev))
12944 ret = valleyview_modeset_global_pipes(state);
12945 else
12946 ret = broadwell_modeset_global_pipes(state);
12947
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012948 if (ret)
12949 return ret;
12950 }
12951
12952 ret = __intel_set_mode_setup_plls(state);
12953 if (ret)
12954 return ret;
12955
12956 return 0;
12957}
12958
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012959static int __intel_set_mode(struct drm_crtc *modeset_crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012960 struct intel_crtc_state *pipe_config)
Daniel Vettera6778b32012-07-02 09:56:42 +020012961{
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012962 struct drm_device *dev = modeset_crtc->dev;
Jani Nikulafbee40d2014-03-31 14:27:18 +030012963 struct drm_i915_private *dev_priv = dev->dev_private;
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030012964 struct drm_atomic_state *state = pipe_config->base.state;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012965 struct drm_crtc *crtc;
12966 struct drm_crtc_state *crtc_state;
Chris Wilsonc0c36b942012-12-19 16:08:43 +000012967 int ret = 0;
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012968 int i;
Daniel Vettera6778b32012-07-02 09:56:42 +020012969
Ander Conselvan de Oliveira054518d2015-04-21 17:13:06 +030012970 ret = __intel_set_mode_checks(state);
12971 if (ret < 0)
12972 return ret;
12973
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030012974 ret = drm_atomic_helper_prepare_planes(dev, state);
12975 if (ret)
12976 return ret;
12977
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012978 for_each_crtc_in_state(state, crtc, crtc_state, i) {
12979 if (!needs_modeset(crtc_state))
12980 continue;
Daniel Vetter460da9162013-03-27 00:44:51 +010012981
Maarten Lankhorst69024de2015-06-01 12:49:46 +020012982 intel_crtc_disable_planes(crtc);
12983 dev_priv->display.crtc_disable(crtc);
12984 if (!crtc_state->enable)
12985 drm_plane_helper_disable(crtc->primary);
Daniel Vetterea9d7582012-07-10 10:42:52 +020012986 }
Daniel Vettera6778b32012-07-02 09:56:42 +020012987
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012988 /* crtc->mode is already used by the ->mode_set callbacks, hence we need
12989 * to set it here already despite that we pass it down the callchain.
Jesse Barnes7f27126e2014-11-05 14:26:06 -080012990 *
12991 * Note we'll need to fix this up when we start tracking multiple
12992 * pipes; here we assume a single modeset_pipe and only track the
12993 * single crtc and mode.
Daniel Vetter6c4c86f2012-09-10 21:58:30 +020012994 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030012995 if (pipe_config->base.enable && needs_modeset(&pipe_config->base)) {
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030012996 modeset_crtc->mode = pipe_config->base.mode;
Ville Syrjäläc326c0a2013-10-28 12:53:41 +020012997
12998 /*
12999 * Calculate and store various constants which
13000 * are later needed by vblank and swap-completion
13001 * timestamping. They are derived from true hwmode.
13002 */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013003 drm_calc_timestamping_constants(modeset_crtc,
Ander Conselvan de Oliveira2d112de2015-01-15 14:55:22 +020013004 &pipe_config->base.adjusted_mode);
Daniel Vetterb8cecdf2013-03-27 00:44:50 +010013005 }
Daniel Vetter7758a112012-07-08 19:40:39 +020013006
Daniel Vetterea9d7582012-07-10 10:42:52 +020013007 /* Only after disabling all output pipelines that will be changed can we
13008 * update the the output configuration. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013009 intel_modeset_update_state(state);
Daniel Vetterea9d7582012-07-10 10:42:52 +020013010
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013011 /* The state has been swaped above, so state actually contains the
13012 * old state now. */
13013
Ander Conselvan de Oliveira304603f2015-04-02 14:47:56 +030013014 modeset_update_crtc_power_domains(state);
Daniel Vetter47fab732012-10-26 10:58:18 +020013015
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013016 drm_atomic_helper_commit_planes(dev, state);
Daniel Vettera6778b32012-07-02 09:56:42 +020013017
13018 /* Now enable the clocks, plane, pipe, and connectors that we set up. */
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013019 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Ander Conselvan de Oliveiraa821fc42015-04-21 17:13:23 +030013020 if (!needs_modeset(crtc->state) || !crtc->state->enable)
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013021 continue;
Ville Syrjälä80715b22014-05-15 20:23:23 +030013022
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013023 update_scanline_offset(to_intel_crtc(crtc));
13024
13025 dev_priv->display.crtc_enable(crtc);
13026 intel_crtc_enable_planes(crtc);
Ville Syrjälä80715b22014-05-15 20:23:23 +030013027 }
Daniel Vettera6778b32012-07-02 09:56:42 +020013028
Daniel Vettera6778b32012-07-02 09:56:42 +020013029 /* FIXME: add subpixel order */
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013030
Ander Conselvan de Oliveirad4afb8c2015-04-21 17:13:22 +030013031 drm_atomic_helper_cleanup_planes(dev, state);
13032
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013033 drm_atomic_state_free(state);
13034
Ander Conselvan de Oliveira9eb45f22015-04-21 17:13:07 +030013035 return 0;
Daniel Vettera6778b32012-07-02 09:56:42 +020013036}
13037
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013038static int intel_set_mode_with_config(struct drm_crtc *crtc,
Ander Conselvan de Oliveira0a9ab302015-04-21 17:13:04 +030013039 struct intel_crtc_state *pipe_config)
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013040{
13041 int ret;
13042
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013043 ret = __intel_set_mode(crtc, pipe_config);
Jesse Barnes7f27126e2014-11-05 14:26:06 -080013044
13045 if (ret == 0)
13046 intel_modeset_check_state(crtc->dev);
13047
13048 return ret;
13049}
13050
Damien Lespiaue7457a92013-08-08 22:28:59 +010013051static int intel_set_mode(struct drm_crtc *crtc,
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013052 struct drm_atomic_state *state)
Daniel Vetterf30da182013-04-11 20:22:50 +020013053{
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013054 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013055 int ret = 0;
Daniel Vetterf30da182013-04-11 20:22:50 +020013056
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013057 pipe_config = intel_modeset_compute_config(crtc, state);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013058 if (IS_ERR(pipe_config)) {
13059 ret = PTR_ERR(pipe_config);
13060 goto out;
13061 }
Daniel Vetterf30da182013-04-11 20:22:50 +020013062
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013063 ret = intel_set_mode_with_config(crtc, pipe_config);
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013064 if (ret)
13065 goto out;
13066
13067out:
13068 return ret;
Daniel Vetterf30da182013-04-11 20:22:50 +020013069}
13070
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013071void intel_crtc_restore_mode(struct drm_crtc *crtc)
13072{
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013073 struct drm_device *dev = crtc->dev;
13074 struct drm_atomic_state *state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013075 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013076 struct intel_encoder *encoder;
13077 struct intel_connector *connector;
13078 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013079 struct intel_crtc_state *crtc_state;
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013080 int ret;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013081
13082 state = drm_atomic_state_alloc(dev);
13083 if (!state) {
13084 DRM_DEBUG_KMS("[CRTC:%d] mode restore failed, out of memory",
13085 crtc->base.id);
13086 return;
13087 }
13088
13089 state->acquire_ctx = dev->mode_config.acquire_ctx;
13090
13091 /* The force restore path in the HW readout code relies on the staged
13092 * config still keeping the user requested config while the actual
13093 * state has been overwritten by the configuration read from HW. We
13094 * need to copy the staged config to the atomic state, otherwise the
13095 * mode set will just reapply the state the HW is already in. */
13096 for_each_intel_encoder(dev, encoder) {
13097 if (&encoder->new_crtc->base != crtc)
13098 continue;
13099
13100 for_each_intel_connector(dev, connector) {
13101 if (connector->new_encoder != encoder)
13102 continue;
13103
13104 connector_state = drm_atomic_get_connector_state(state, &connector->base);
13105 if (IS_ERR(connector_state)) {
13106 DRM_DEBUG_KMS("Failed to add [CONNECTOR:%d:%s] to state: %ld\n",
13107 connector->base.base.id,
13108 connector->base.name,
13109 PTR_ERR(connector_state));
13110 continue;
13111 }
13112
13113 connector_state->crtc = crtc;
13114 connector_state->best_encoder = &encoder->base;
13115 }
13116 }
13117
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013118 for_each_intel_crtc(dev, intel_crtc) {
13119 if (intel_crtc->new_enabled == intel_crtc->base.enabled)
13120 continue;
13121
13122 crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
13123 if (IS_ERR(crtc_state)) {
13124 DRM_DEBUG_KMS("Failed to add [CRTC:%d] to state: %ld\n",
13125 intel_crtc->base.base.id,
13126 PTR_ERR(crtc_state));
13127 continue;
13128 }
13129
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013130 crtc_state->base.active = crtc_state->base.enable =
13131 intel_crtc->new_enabled;
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013132
13133 if (&intel_crtc->base == crtc)
13134 drm_mode_copy(&crtc_state->base.mode, &crtc->mode);
Ander Conselvan de Oliveira4be07312015-04-21 17:13:01 +030013135 }
13136
Ander Conselvan de Oliveirad3a40d12015-04-21 17:13:09 +030013137 intel_modeset_setup_plane_state(state, crtc, &crtc->mode,
13138 crtc->primary->fb, crtc->x, crtc->y);
13139
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013140 ret = intel_set_mode(crtc, state);
13141 if (ret)
13142 drm_atomic_state_free(state);
Chris Wilsonc0c36b942012-12-19 16:08:43 +000013143}
13144
Daniel Vetter25c5b262012-07-08 22:08:04 +020013145#undef for_each_intel_crtc_masked
13146
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013147static bool intel_connector_in_mode_set(struct intel_connector *connector,
13148 struct drm_mode_set *set)
13149{
13150 int ro;
13151
13152 for (ro = 0; ro < set->num_connectors; ro++)
13153 if (set->connectors[ro] == &connector->base)
13154 return true;
13155
13156 return false;
13157}
13158
Daniel Vetter2e431052012-07-04 22:42:15 +020013159static int
Daniel Vetter9a935852012-07-05 22:34:27 +020013160intel_modeset_stage_output_state(struct drm_device *dev,
13161 struct drm_mode_set *set,
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013162 struct drm_atomic_state *state)
Daniel Vetter50f56112012-07-02 09:35:43 +020013163{
Daniel Vetter9a935852012-07-05 22:34:27 +020013164 struct intel_connector *connector;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013165 struct drm_connector *drm_connector;
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013166 struct drm_connector_state *connector_state;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013167 struct drm_crtc *crtc;
13168 struct drm_crtc_state *crtc_state;
13169 int i, ret;
Daniel Vetter50f56112012-07-02 09:35:43 +020013170
Damien Lespiau9abdda72013-02-13 13:29:23 +000013171 /* The upper layers ensure that we either disable a crtc or have a list
Daniel Vetter9a935852012-07-05 22:34:27 +020013172 * of connectors. For paranoia, double-check this. */
13173 WARN_ON(!set->fb && (set->num_connectors != 0));
13174 WARN_ON(set->fb && (set->num_connectors == 0));
13175
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020013176 for_each_intel_connector(dev, connector) {
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013177 bool in_mode_set = intel_connector_in_mode_set(connector, set);
13178
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013179 if (!in_mode_set && connector->base.state->crtc != set->crtc)
13180 continue;
13181
13182 connector_state =
13183 drm_atomic_get_connector_state(state, &connector->base);
13184 if (IS_ERR(connector_state))
13185 return PTR_ERR(connector_state);
13186
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013187 if (in_mode_set) {
13188 int pipe = to_intel_crtc(set->crtc)->pipe;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013189 connector_state->best_encoder =
13190 &intel_find_encoder(connector, pipe)->base;
Daniel Vetter50f56112012-07-02 09:35:43 +020013191 }
13192
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013193 if (connector->base.state->crtc != set->crtc)
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013194 continue;
13195
Daniel Vetter9a935852012-07-05 22:34:27 +020013196 /* If we disable the crtc, disable all its connectors. Also, if
13197 * the connector is on the changing crtc but not on the new
13198 * connector list, disable it. */
Ander Conselvan de Oliveirab7885262015-04-21 17:13:14 +030013199 if (!set->fb || !in_mode_set) {
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013200 connector_state->best_encoder = NULL;
Daniel Vetter9a935852012-07-05 22:34:27 +020013201
13202 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [NOCRTC]\n",
13203 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013204 connector->base.name);
Daniel Vetter9a935852012-07-05 22:34:27 +020013205 }
Daniel Vetter9a935852012-07-05 22:34:27 +020013206 }
13207 /* connector->new_encoder is now updated for all connectors. */
13208
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013209 for_each_connector_in_state(state, drm_connector, connector_state, i) {
13210 connector = to_intel_connector(drm_connector);
Ville Syrjälä76688512014-01-10 11:28:06 +020013211
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013212 if (!connector_state->best_encoder) {
13213 ret = drm_atomic_set_crtc_for_connector(connector_state,
13214 NULL);
13215 if (ret)
13216 return ret;
13217
Daniel Vetter50f56112012-07-02 09:35:43 +020013218 continue;
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013219 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013220
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013221 if (intel_connector_in_mode_set(connector, set)) {
13222 struct drm_crtc *crtc = connector->base.state->crtc;
13223
13224 /* If this connector was in a previous crtc, add it
13225 * to the state. We might need to disable it. */
13226 if (crtc) {
13227 crtc_state =
13228 drm_atomic_get_crtc_state(state, crtc);
13229 if (IS_ERR(crtc_state))
13230 return PTR_ERR(crtc_state);
13231 }
13232
13233 ret = drm_atomic_set_crtc_for_connector(connector_state,
13234 set->crtc);
13235 if (ret)
13236 return ret;
13237 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013238
13239 /* Make sure the new CRTC will work with the encoder */
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013240 if (!drm_encoder_crtc_ok(connector_state->best_encoder,
13241 connector_state->crtc)) {
Daniel Vetter5e2b5842012-07-04 22:41:29 +020013242 return -EINVAL;
Daniel Vetter50f56112012-07-02 09:35:43 +020013243 }
Ander Conselvan de Oliveira944b0c72015-03-20 16:18:07 +020013244
Daniel Vetter9a935852012-07-05 22:34:27 +020013245 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] to [CRTC:%d]\n",
13246 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030013247 connector->base.name,
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013248 connector_state->crtc->base.id);
13249
13250 if (connector_state->best_encoder != &connector->encoder->base)
13251 connector->encoder =
13252 to_intel_encoder(connector_state->best_encoder);
Daniel Vetter9a935852012-07-05 22:34:27 +020013253 }
13254
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013255 for_each_crtc_in_state(state, crtc, crtc_state, i) {
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013256 bool has_connectors;
13257
Ander Conselvan de Oliveirad5432a9d2015-04-21 17:13:15 +030013258 ret = drm_atomic_add_affected_connectors(state, crtc);
13259 if (ret)
13260 return ret;
Paulo Zanoni5a65f352014-01-07 14:55:53 -020013261
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020013262 has_connectors = !!drm_atomic_connectors_for_crtc(state, crtc);
13263 if (has_connectors != crtc_state->enable)
13264 crtc_state->enable =
13265 crtc_state->active = has_connectors;
Ville Syrjälä76688512014-01-10 11:28:06 +020013266 }
13267
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013268 ret = intel_modeset_setup_plane_state(state, set->crtc, set->mode,
13269 set->fb, set->x, set->y);
13270 if (ret)
13271 return ret;
13272
13273 crtc_state = drm_atomic_get_crtc_state(state, set->crtc);
13274 if (IS_ERR(crtc_state))
13275 return PTR_ERR(crtc_state);
13276
13277 if (set->mode)
13278 drm_mode_copy(&crtc_state->mode, set->mode);
13279
13280 if (set->num_connectors)
13281 crtc_state->active = true;
13282
Daniel Vetter2e431052012-07-04 22:42:15 +020013283 return 0;
13284}
13285
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013286static bool primary_plane_visible(struct drm_crtc *crtc)
13287{
13288 struct intel_plane_state *plane_state =
13289 to_intel_plane_state(crtc->primary->state);
13290
13291 return plane_state->visible;
13292}
13293
Daniel Vetter2e431052012-07-04 22:42:15 +020013294static int intel_crtc_set_config(struct drm_mode_set *set)
13295{
13296 struct drm_device *dev;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013297 struct drm_atomic_state *state = NULL;
Ander Conselvan de Oliveira5cec2582015-01-15 14:55:21 +020013298 struct intel_crtc_state *pipe_config;
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013299 bool primary_plane_was_visible;
Daniel Vetter2e431052012-07-04 22:42:15 +020013300 int ret;
Daniel Vetter2e431052012-07-04 22:42:15 +020013301
Daniel Vetter8d3e3752012-07-05 16:09:09 +020013302 BUG_ON(!set);
13303 BUG_ON(!set->crtc);
13304 BUG_ON(!set->crtc->helper_private);
Daniel Vetter2e431052012-07-04 22:42:15 +020013305
Daniel Vetter7e53f3a2013-01-21 10:52:17 +010013306 /* Enforce sane interface api - has been abused by the fb helper. */
13307 BUG_ON(!set->mode && set->fb);
13308 BUG_ON(set->fb && set->num_connectors == 0);
Daniel Vetter431e50f2012-07-10 17:53:42 +020013309
Daniel Vetter2e431052012-07-04 22:42:15 +020013310 if (set->fb) {
13311 DRM_DEBUG_KMS("[CRTC:%d] [FB:%d] #connectors=%d (x y) (%i %i)\n",
13312 set->crtc->base.id, set->fb->base.id,
13313 (int)set->num_connectors, set->x, set->y);
13314 } else {
13315 DRM_DEBUG_KMS("[CRTC:%d] [NOFB]\n", set->crtc->base.id);
Daniel Vetter2e431052012-07-04 22:42:15 +020013316 }
13317
13318 dev = set->crtc->dev;
13319
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013320 state = drm_atomic_state_alloc(dev);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013321 if (!state)
13322 return -ENOMEM;
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020013323
13324 state->acquire_ctx = dev->mode_config.acquire_ctx;
13325
Ander Conselvan de Oliveira462a4252015-04-21 17:13:00 +030013326 ret = intel_modeset_stage_output_state(dev, set, state);
Daniel Vetter2e431052012-07-04 22:42:15 +020013327 if (ret)
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013328 goto out;
Daniel Vetter2e431052012-07-04 22:42:15 +020013329
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013330 pipe_config = intel_modeset_compute_config(set->crtc, state);
Jesse Barnes20664592014-11-05 14:26:09 -080013331 if (IS_ERR(pipe_config)) {
Matt Roper6ac04832014-11-17 09:59:28 -080013332 ret = PTR_ERR(pipe_config);
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013333 goto out;
Jesse Barnes20664592014-11-05 14:26:09 -080013334 }
Jesse Barnes50f52752014-11-07 13:11:00 -080013335
Jesse Barnes1f9954d2014-11-05 14:26:10 -080013336 intel_update_pipe_size(to_intel_crtc(set->crtc));
13337
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013338 primary_plane_was_visible = primary_plane_visible(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013339
Ander Conselvan de Oliveira8c7b5cc2015-04-21 17:13:19 +030013340 ret = intel_set_mode_with_config(set->crtc, pipe_config);
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013341
13342 if (ret == 0 &&
13343 pipe_config->base.enable &&
13344 pipe_config->base.planes_changed &&
13345 !needs_modeset(&pipe_config->base)) {
13346 struct intel_crtc *intel_crtc = to_intel_crtc(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013347
13348 /*
13349 * We need to make sure the primary plane is re-enabled if it
13350 * has previously been turned off.
13351 */
Ander Conselvan de Oliveirabb546622015-04-21 17:13:13 +030013352 if (ret == 0 && !primary_plane_was_visible &&
13353 primary_plane_visible(set->crtc)) {
Matt Roper3b150f02014-05-29 08:06:53 -070013354 WARN_ON(!intel_crtc->active);
Maarten Lankhorst87d43002015-04-21 17:12:54 +030013355 intel_post_enable_primary(set->crtc);
Matt Roper3b150f02014-05-29 08:06:53 -070013356 }
13357
Jesse Barnes7ca51a32014-01-07 13:50:49 -080013358 /*
13359 * In the fastboot case this may be our only check of the
13360 * state after boot. It would be better to only do it on
13361 * the first update, but we don't have a nice way of doing that
13362 * (and really, set_config isn't used much for high freq page
13363 * flipping, so increasing its cost here shouldn't be a big
13364 * deal).
13365 */
Jani Nikulad330a952014-01-21 11:24:25 +020013366 if (i915.fastboot && ret == 0)
Jesse Barnes7ca51a32014-01-07 13:50:49 -080013367 intel_modeset_check_state(set->crtc->dev);
Daniel Vetter50f56112012-07-02 09:35:43 +020013368 }
13369
Chris Wilson2d05eae2013-05-03 17:36:25 +010013370 if (ret) {
Daniel Vetterbf67dfe2013-06-25 11:06:52 +020013371 DRM_DEBUG_KMS("failed to set mode on [CRTC:%d], err = %d\n",
13372 set->crtc->base.id, ret);
Chris Wilson2d05eae2013-05-03 17:36:25 +010013373 }
Daniel Vetter50f56112012-07-02 09:35:43 +020013374
Ander Conselvan de Oliveira7cbf41d2015-04-21 17:13:16 +030013375out:
Ander Conselvan de Oliveira2bfb4622015-04-21 17:13:20 +030013376 if (ret)
13377 drm_atomic_state_free(state);
Daniel Vetter50f56112012-07-02 09:35:43 +020013378 return ret;
13379}
13380
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013381static const struct drm_crtc_funcs intel_crtc_funcs = {
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013382 .gamma_set = intel_crtc_gamma_set,
Daniel Vetter50f56112012-07-02 09:35:43 +020013383 .set_config = intel_crtc_set_config,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013384 .destroy = intel_crtc_destroy,
13385 .page_flip = intel_crtc_page_flip,
Matt Roper13568372015-01-21 16:35:47 -080013386 .atomic_duplicate_state = intel_crtc_duplicate_state,
13387 .atomic_destroy_state = intel_crtc_destroy_state,
Chris Wilsonf6e5b162011-04-12 18:06:51 +010013388};
13389
Daniel Vetter53589012013-06-05 13:34:16 +020013390static bool ibx_pch_dpll_get_hw_state(struct drm_i915_private *dev_priv,
13391 struct intel_shared_dpll *pll,
13392 struct intel_dpll_hw_state *hw_state)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013393{
Daniel Vetter53589012013-06-05 13:34:16 +020013394 uint32_t val;
13395
Daniel Vetterf458ebb2014-09-30 10:56:39 +020013396 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_PLLS))
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030013397 return false;
13398
Daniel Vetter53589012013-06-05 13:34:16 +020013399 val = I915_READ(PCH_DPLL(pll->id));
Daniel Vetter66e985c2013-06-05 13:34:20 +020013400 hw_state->dpll = val;
13401 hw_state->fp0 = I915_READ(PCH_FP0(pll->id));
13402 hw_state->fp1 = I915_READ(PCH_FP1(pll->id));
Daniel Vetter53589012013-06-05 13:34:16 +020013403
13404 return val & DPLL_VCO_ENABLE;
13405}
13406
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013407static void ibx_pch_dpll_mode_set(struct drm_i915_private *dev_priv,
13408 struct intel_shared_dpll *pll)
13409{
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013410 I915_WRITE(PCH_FP0(pll->id), pll->config.hw_state.fp0);
13411 I915_WRITE(PCH_FP1(pll->id), pll->config.hw_state.fp1);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013412}
13413
Daniel Vettere7b903d2013-06-05 13:34:14 +020013414static void ibx_pch_dpll_enable(struct drm_i915_private *dev_priv,
13415 struct intel_shared_dpll *pll)
13416{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013417 /* PCH refclock must be enabled first */
Paulo Zanoni89eff4b2014-01-08 11:12:28 -020013418 ibx_assert_pch_refclk_enabled(dev_priv);
Daniel Vettere7b903d2013-06-05 13:34:14 +020013419
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013420 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013421
13422 /* Wait for the clocks to stabilize. */
13423 POSTING_READ(PCH_DPLL(pll->id));
13424 udelay(150);
13425
13426 /* The pixel multiplier can only be updated once the
13427 * DPLL is enabled and the clocks are stable.
13428 *
13429 * So write it again.
13430 */
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020013431 I915_WRITE(PCH_DPLL(pll->id), pll->config.hw_state.dpll);
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013432 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013433 udelay(200);
13434}
13435
13436static void ibx_pch_dpll_disable(struct drm_i915_private *dev_priv,
13437 struct intel_shared_dpll *pll)
13438{
13439 struct drm_device *dev = dev_priv->dev;
13440 struct intel_crtc *crtc;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013441
13442 /* Make sure no transcoder isn't still depending on us. */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010013443 for_each_intel_crtc(dev, crtc) {
Daniel Vettere7b903d2013-06-05 13:34:14 +020013444 if (intel_crtc_to_shared_dpll(crtc) == pll)
13445 assert_pch_transcoder_disabled(dev_priv, crtc->pipe);
13446 }
13447
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013448 I915_WRITE(PCH_DPLL(pll->id), 0);
13449 POSTING_READ(PCH_DPLL(pll->id));
Daniel Vettere7b903d2013-06-05 13:34:14 +020013450 udelay(200);
13451}
13452
Daniel Vetter46edb022013-06-05 13:34:12 +020013453static char *ibx_pch_dpll_names[] = {
13454 "PCH DPLL A",
13455 "PCH DPLL B",
13456};
13457
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013458static void ibx_pch_dpll_init(struct drm_device *dev)
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013459{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013460 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013461 int i;
13462
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013463 dev_priv->num_shared_dpll = 2;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013464
Daniel Vettere72f9fb2013-06-05 13:34:06 +020013465 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
Daniel Vetter46edb022013-06-05 13:34:12 +020013466 dev_priv->shared_dplls[i].id = i;
13467 dev_priv->shared_dplls[i].name = ibx_pch_dpll_names[i];
Daniel Vetter15bdd4c2013-06-05 13:34:23 +020013468 dev_priv->shared_dplls[i].mode_set = ibx_pch_dpll_mode_set;
Daniel Vettere7b903d2013-06-05 13:34:14 +020013469 dev_priv->shared_dplls[i].enable = ibx_pch_dpll_enable;
13470 dev_priv->shared_dplls[i].disable = ibx_pch_dpll_disable;
Daniel Vetter53589012013-06-05 13:34:16 +020013471 dev_priv->shared_dplls[i].get_hw_state =
13472 ibx_pch_dpll_get_hw_state;
Jesse Barnesee7b9f92012-04-20 17:11:53 +010013473 }
13474}
13475
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013476static void intel_shared_dpll_init(struct drm_device *dev)
13477{
Daniel Vettere7b903d2013-06-05 13:34:14 +020013478 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013479
Ville Syrjäläb6283052015-06-03 15:45:07 +030013480 intel_update_cdclk(dev);
13481
Daniel Vetter9cd86932014-06-25 22:01:57 +030013482 if (HAS_DDI(dev))
13483 intel_ddi_pll_init(dev);
13484 else if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013485 ibx_pch_dpll_init(dev);
13486 else
13487 dev_priv->num_shared_dpll = 0;
13488
13489 BUG_ON(dev_priv->num_shared_dpll > I915_NUM_PLLS);
Daniel Vetter7c74ade2013-06-05 13:34:11 +020013490}
13491
Matt Roper6beb8c232014-12-01 15:40:14 -080013492/**
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013493 * intel_wm_need_update - Check whether watermarks need updating
13494 * @plane: drm plane
13495 * @state: new plane state
13496 *
13497 * Check current plane state versus the new one to determine whether
13498 * watermarks need to be recalculated.
13499 *
13500 * Returns true or false.
13501 */
13502bool intel_wm_need_update(struct drm_plane *plane,
13503 struct drm_plane_state *state)
13504{
13505 /* Update watermarks on tiling changes. */
13506 if (!plane->state->fb || !state->fb ||
13507 plane->state->fb->modifier[0] != state->fb->modifier[0] ||
13508 plane->state->rotation != state->rotation)
13509 return true;
13510
13511 return false;
13512}
13513
13514/**
Matt Roper6beb8c232014-12-01 15:40:14 -080013515 * intel_prepare_plane_fb - Prepare fb for usage on plane
13516 * @plane: drm plane to prepare for
13517 * @fb: framebuffer to prepare for presentation
13518 *
13519 * Prepares a framebuffer for usage on a display plane. Generally this
13520 * involves pinning the underlying object and updating the frontbuffer tracking
13521 * bits. Some older platforms need special physical address handling for
13522 * cursor planes.
13523 *
13524 * Returns 0 on success, negative error code on failure.
13525 */
13526int
13527intel_prepare_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013528 struct drm_framebuffer *fb,
13529 const struct drm_plane_state *new_state)
Matt Roper465c1202014-05-29 08:06:54 -070013530{
13531 struct drm_device *dev = plane->dev;
Matt Roper6beb8c232014-12-01 15:40:14 -080013532 struct intel_plane *intel_plane = to_intel_plane(plane);
13533 enum pipe pipe = intel_plane->pipe;
13534 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13535 struct drm_i915_gem_object *old_obj = intel_fb_obj(plane->fb);
13536 unsigned frontbuffer_bits = 0;
13537 int ret = 0;
Matt Roper465c1202014-05-29 08:06:54 -070013538
Matt Roperea2c67b2014-12-23 10:41:52 -080013539 if (!obj)
Matt Roper465c1202014-05-29 08:06:54 -070013540 return 0;
13541
Matt Roper6beb8c232014-12-01 15:40:14 -080013542 switch (plane->type) {
13543 case DRM_PLANE_TYPE_PRIMARY:
13544 frontbuffer_bits = INTEL_FRONTBUFFER_PRIMARY(pipe);
13545 break;
13546 case DRM_PLANE_TYPE_CURSOR:
13547 frontbuffer_bits = INTEL_FRONTBUFFER_CURSOR(pipe);
13548 break;
13549 case DRM_PLANE_TYPE_OVERLAY:
13550 frontbuffer_bits = INTEL_FRONTBUFFER_SPRITE(pipe);
13551 break;
13552 }
Matt Roper465c1202014-05-29 08:06:54 -070013553
Matt Roper4c345742014-07-09 16:22:10 -070013554 mutex_lock(&dev->struct_mutex);
Matt Roper465c1202014-05-29 08:06:54 -070013555
Matt Roper6beb8c232014-12-01 15:40:14 -080013556 if (plane->type == DRM_PLANE_TYPE_CURSOR &&
13557 INTEL_INFO(dev)->cursor_needs_physical) {
13558 int align = IS_I830(dev) ? 16 * 1024 : 256;
13559 ret = i915_gem_object_attach_phys(obj, align);
13560 if (ret)
13561 DRM_DEBUG_KMS("failed to attach phys object\n");
13562 } else {
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013563 ret = intel_pin_and_fence_fb_obj(plane, fb, new_state, NULL);
Matt Roper6beb8c232014-12-01 15:40:14 -080013564 }
13565
13566 if (ret == 0)
13567 i915_gem_track_fb(old_obj, obj, frontbuffer_bits);
13568
13569 mutex_unlock(&dev->struct_mutex);
13570
13571 return ret;
13572}
13573
Matt Roper38f3ce32014-12-02 07:45:25 -080013574/**
13575 * intel_cleanup_plane_fb - Cleans up an fb after plane use
13576 * @plane: drm plane to clean up for
13577 * @fb: old framebuffer that was on plane
13578 *
13579 * Cleans up a framebuffer that has just been removed from a plane.
13580 */
13581void
13582intel_cleanup_plane_fb(struct drm_plane *plane,
Tvrtko Ursulind136dfe2015-03-03 14:22:31 +000013583 struct drm_framebuffer *fb,
13584 const struct drm_plane_state *old_state)
Matt Roper38f3ce32014-12-02 07:45:25 -080013585{
13586 struct drm_device *dev = plane->dev;
13587 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
13588
13589 if (WARN_ON(!obj))
13590 return;
13591
13592 if (plane->type != DRM_PLANE_TYPE_CURSOR ||
13593 !INTEL_INFO(dev)->cursor_needs_physical) {
13594 mutex_lock(&dev->struct_mutex);
Tvrtko Ursulin82bc3b22015-03-23 11:10:34 +000013595 intel_unpin_fb_obj(fb, old_state);
Matt Roper38f3ce32014-12-02 07:45:25 -080013596 mutex_unlock(&dev->struct_mutex);
13597 }
Matt Roper465c1202014-05-29 08:06:54 -070013598}
13599
Chandra Konduru6156a452015-04-27 13:48:39 -070013600int
13601skl_max_scale(struct intel_crtc *intel_crtc, struct intel_crtc_state *crtc_state)
13602{
13603 int max_scale;
13604 struct drm_device *dev;
13605 struct drm_i915_private *dev_priv;
13606 int crtc_clock, cdclk;
13607
13608 if (!intel_crtc || !crtc_state)
13609 return DRM_PLANE_HELPER_NO_SCALING;
13610
13611 dev = intel_crtc->base.dev;
13612 dev_priv = dev->dev_private;
13613 crtc_clock = crtc_state->base.adjusted_mode.crtc_clock;
13614 cdclk = dev_priv->display.get_display_clock_speed(dev);
13615
13616 if (!crtc_clock || !cdclk)
13617 return DRM_PLANE_HELPER_NO_SCALING;
13618
13619 /*
13620 * skl max scale is lower of:
13621 * close to 3 but not 3, -1 is for that purpose
13622 * or
13623 * cdclk/crtc_clock
13624 */
13625 max_scale = min((1 << 16) * 3 - 1, (1 << 8) * ((cdclk << 8) / crtc_clock));
13626
13627 return max_scale;
13628}
13629
Matt Roper465c1202014-05-29 08:06:54 -070013630static int
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013631intel_check_primary_plane(struct drm_plane *plane,
13632 struct intel_plane_state *state)
Matt Roper465c1202014-05-29 08:06:54 -070013633{
Matt Roper32b7eee2014-12-24 07:59:06 -080013634 struct drm_device *dev = plane->dev;
13635 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roper2b875c22014-12-01 15:40:13 -080013636 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013637 struct intel_crtc *intel_crtc;
Chandra Konduru6156a452015-04-27 13:48:39 -070013638 struct intel_crtc_state *crtc_state;
Matt Roper2b875c22014-12-01 15:40:13 -080013639 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013640 struct drm_rect *dest = &state->dst;
13641 struct drm_rect *src = &state->src;
13642 const struct drm_rect *clip = &state->clip;
Sonika Jindald8106362015-04-10 14:37:28 +053013643 bool can_position = false;
Chandra Konduru6156a452015-04-27 13:48:39 -070013644 int max_scale = DRM_PLANE_HELPER_NO_SCALING;
13645 int min_scale = DRM_PLANE_HELPER_NO_SCALING;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013646 int ret;
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013647
Matt Roperea2c67b2014-12-23 10:41:52 -080013648 crtc = crtc ? crtc : plane->crtc;
13649 intel_crtc = to_intel_crtc(crtc);
Chandra Konduru6156a452015-04-27 13:48:39 -070013650 crtc_state = state->base.state ?
13651 intel_atomic_get_crtc_state(state->base.state, intel_crtc) : NULL;
Matt Roperea2c67b2014-12-23 10:41:52 -080013652
Chandra Konduru6156a452015-04-27 13:48:39 -070013653 if (INTEL_INFO(dev)->gen >= 9) {
Chandra Konduru225c2282015-05-18 16:18:44 -070013654 /* use scaler when colorkey is not required */
13655 if (to_intel_plane(plane)->ckey.flags == I915_SET_COLORKEY_NONE) {
13656 min_scale = 1;
13657 max_scale = skl_max_scale(intel_crtc, crtc_state);
13658 }
Sonika Jindald8106362015-04-10 14:37:28 +053013659 can_position = true;
Chandra Konduru6156a452015-04-27 13:48:39 -070013660 }
Sonika Jindald8106362015-04-10 14:37:28 +053013661
Matt Roperc59cb172014-12-01 15:40:16 -080013662 ret = drm_plane_helper_check_update(plane, crtc, fb,
13663 src, dest, clip,
Chandra Konduru6156a452015-04-27 13:48:39 -070013664 min_scale,
13665 max_scale,
Sonika Jindald8106362015-04-10 14:37:28 +053013666 can_position, true,
13667 &state->visible);
Matt Roperc59cb172014-12-01 15:40:16 -080013668 if (ret)
13669 return ret;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013670
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013671 if (intel_crtc->active) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013672 struct intel_plane_state *old_state =
13673 to_intel_plane_state(plane->state);
13674
Matt Roper32b7eee2014-12-24 07:59:06 -080013675 intel_crtc->atomic.wait_for_flips = true;
13676
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013677 /*
13678 * FBC does not work on some platforms for rotated
13679 * planes, so disable it when rotation is not 0 and
13680 * update it when rotation is set back to 0.
13681 *
13682 * FIXME: This is redundant with the fbc update done in
13683 * the primary plane enable function except that that
13684 * one is done too late. We eventually need to unify
13685 * this.
13686 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013687 if (state->visible &&
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013688 INTEL_INFO(dev)->gen <= 4 && !IS_G4X(dev) &&
Paulo Zanonie35fef22015-02-09 14:46:29 -020013689 dev_priv->fbc.crtc == intel_crtc &&
Matt Roper8e7d6882015-01-21 16:35:41 -080013690 state->base.rotation != BIT(DRM_ROTATE_0)) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013691 intel_crtc->atomic.disable_fbc = true;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013692 }
13693
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013694 if (state->visible && !old_state->visible) {
Matt Roper32b7eee2014-12-24 07:59:06 -080013695 /*
13696 * BDW signals flip done immediately if the plane
13697 * is disabled, even if the plane enable is already
13698 * armed to occur at the next vblank :(
13699 */
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030013700 if (IS_BROADWELL(dev))
Matt Roper32b7eee2014-12-24 07:59:06 -080013701 intel_crtc->atomic.wait_vblank = true;
13702 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013703
Matt Roper32b7eee2014-12-24 07:59:06 -080013704 intel_crtc->atomic.fb_bits |=
13705 INTEL_FRONTBUFFER_PRIMARY(intel_crtc->pipe);
13706
13707 intel_crtc->atomic.update_fbc = true;
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013708
Tvrtko Ursulin1fc0a8f2015-03-23 11:10:38 +000013709 if (intel_wm_need_update(plane, &state->base))
Tvrtko Ursulin0fda6562015-02-27 15:12:35 +000013710 intel_crtc->atomic.update_wm = true;
Matt Roperc59cb172014-12-01 15:40:16 -080013711 }
13712
Chandra Konduru6156a452015-04-27 13:48:39 -070013713 if (INTEL_INFO(dev)->gen >= 9) {
13714 ret = skl_update_scaler_users(intel_crtc, crtc_state,
13715 to_intel_plane(plane), state, 0);
13716 if (ret)
13717 return ret;
13718 }
13719
Matt Roperc59cb172014-12-01 15:40:16 -080013720 return 0;
Matt Roper465c1202014-05-29 08:06:54 -070013721}
13722
Sonika Jindal48404c12014-08-22 14:06:04 +053013723static void
13724intel_commit_primary_plane(struct drm_plane *plane,
13725 struct intel_plane_state *state)
13726{
Matt Roper2b875c22014-12-01 15:40:13 -080013727 struct drm_crtc *crtc = state->base.crtc;
13728 struct drm_framebuffer *fb = state->base.fb;
13729 struct drm_device *dev = plane->dev;
Sonika Jindal48404c12014-08-22 14:06:04 +053013730 struct drm_i915_private *dev_priv = dev->dev_private;
Matt Roperea2c67b2014-12-23 10:41:52 -080013731 struct intel_crtc *intel_crtc;
Sonika Jindalce54d852014-08-21 11:44:39 +053013732 struct drm_rect *src = &state->src;
Matt Ropercf4c7c12014-12-04 10:27:42 -080013733
Matt Roperea2c67b2014-12-23 10:41:52 -080013734 crtc = crtc ? crtc : plane->crtc;
13735 intel_crtc = to_intel_crtc(crtc);
13736
Matt Ropercf4c7c12014-12-04 10:27:42 -080013737 plane->fb = fb;
Sonika Jindalce54d852014-08-21 11:44:39 +053013738 crtc->x = src->x1 >> 16;
Matt Roper465c1202014-05-29 08:06:54 -070013739 crtc->y = src->y1 >> 16;
13740
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013741 if (intel_crtc->active) {
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013742 if (state->visible)
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013743 /* FIXME: kill this fastboot hack */
13744 intel_update_pipe_size(intel_crtc);
13745
Maarten Lankhorst27321ae2015-04-21 17:12:52 +030013746 dev_priv->display.update_primary_plane(crtc, plane->fb,
13747 crtc->x, crtc->y);
Matt Roper32b7eee2014-12-24 07:59:06 -080013748 }
13749}
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013750
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013751static void
13752intel_disable_primary_plane(struct drm_plane *plane,
13753 struct drm_crtc *crtc,
13754 bool force)
13755{
13756 struct drm_device *dev = plane->dev;
13757 struct drm_i915_private *dev_priv = dev->dev_private;
13758
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013759 dev_priv->display.update_primary_plane(crtc, NULL, 0, 0);
13760}
13761
Matt Roper32b7eee2014-12-24 07:59:06 -080013762static void intel_begin_crtc_commit(struct drm_crtc *crtc)
13763{
13764 struct drm_device *dev = crtc->dev;
13765 struct drm_i915_private *dev_priv = dev->dev_private;
13766 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
Matt Roperea2c67b2014-12-23 10:41:52 -080013767 struct intel_plane *intel_plane;
13768 struct drm_plane *p;
13769 unsigned fb_bits = 0;
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013770
Matt Roperea2c67b2014-12-23 10:41:52 -080013771 /* Track fb's for any planes being disabled */
13772 list_for_each_entry(p, &dev->mode_config.plane_list, head) {
13773 intel_plane = to_intel_plane(p);
13774
13775 if (intel_crtc->atomic.disabled_planes &
13776 (1 << drm_plane_index(p))) {
13777 switch (p->type) {
13778 case DRM_PLANE_TYPE_PRIMARY:
13779 fb_bits = INTEL_FRONTBUFFER_PRIMARY(intel_plane->pipe);
13780 break;
13781 case DRM_PLANE_TYPE_CURSOR:
13782 fb_bits = INTEL_FRONTBUFFER_CURSOR(intel_plane->pipe);
13783 break;
13784 case DRM_PLANE_TYPE_OVERLAY:
13785 fb_bits = INTEL_FRONTBUFFER_SPRITE(intel_plane->pipe);
13786 break;
13787 }
13788
13789 mutex_lock(&dev->struct_mutex);
13790 i915_gem_track_fb(intel_fb_obj(p->fb), NULL, fb_bits);
13791 mutex_unlock(&dev->struct_mutex);
13792 }
13793 }
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013794
Matt Roper32b7eee2014-12-24 07:59:06 -080013795 if (intel_crtc->atomic.wait_for_flips)
13796 intel_crtc_wait_for_pending_flips(crtc);
13797
13798 if (intel_crtc->atomic.disable_fbc)
13799 intel_fbc_disable(dev);
13800
13801 if (intel_crtc->atomic.pre_disable_primary)
13802 intel_pre_disable_primary(crtc);
13803
13804 if (intel_crtc->atomic.update_wm)
13805 intel_update_watermarks(crtc);
13806
13807 intel_runtime_pm_get(dev_priv);
Matt Roperc34c9ee2014-12-23 10:41:50 -080013808
13809 /* Perform vblank evasion around commit operation */
13810 if (intel_crtc->active)
13811 intel_crtc->atomic.evade =
13812 intel_pipe_update_start(intel_crtc,
13813 &intel_crtc->atomic.start_vbl_count);
Matt Roper32b7eee2014-12-24 07:59:06 -080013814}
13815
13816static void intel_finish_crtc_commit(struct drm_crtc *crtc)
13817{
13818 struct drm_device *dev = crtc->dev;
13819 struct drm_i915_private *dev_priv = dev->dev_private;
13820 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
13821 struct drm_plane *p;
13822
Matt Roperc34c9ee2014-12-23 10:41:50 -080013823 if (intel_crtc->atomic.evade)
13824 intel_pipe_update_end(intel_crtc,
13825 intel_crtc->atomic.start_vbl_count);
13826
Matt Roper32b7eee2014-12-24 07:59:06 -080013827 intel_runtime_pm_put(dev_priv);
13828
13829 if (intel_crtc->atomic.wait_vblank)
13830 intel_wait_for_vblank(dev, intel_crtc->pipe);
13831
13832 intel_frontbuffer_flip(dev, intel_crtc->atomic.fb_bits);
13833
13834 if (intel_crtc->atomic.update_fbc) {
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013835 mutex_lock(&dev->struct_mutex);
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020013836 intel_fbc_update(dev);
Gustavo Padovanccc759dc2014-09-24 14:20:22 -030013837 mutex_unlock(&dev->struct_mutex);
13838 }
Matt Roper465c1202014-05-29 08:06:54 -070013839
Matt Roper32b7eee2014-12-24 07:59:06 -080013840 if (intel_crtc->atomic.post_enable_primary)
13841 intel_post_enable_primary(crtc);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013842
Matt Roper32b7eee2014-12-24 07:59:06 -080013843 drm_for_each_legacy_plane(p, &dev->mode_config.plane_list)
13844 if (intel_crtc->atomic.update_sprite_watermarks & drm_plane_index(p))
13845 intel_update_sprite_watermarks(p, crtc, 0, 0, 0,
13846 false, false);
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013847
Matt Roper32b7eee2014-12-24 07:59:06 -080013848 memset(&intel_crtc->atomic, 0, sizeof(intel_crtc->atomic));
Gustavo Padovan3c692a42014-09-05 17:04:49 -030013849}
13850
Matt Ropercf4c7c12014-12-04 10:27:42 -080013851/**
Matt Roper4a3b8762014-12-23 10:41:51 -080013852 * intel_plane_destroy - destroy a plane
13853 * @plane: plane to destroy
Matt Ropercf4c7c12014-12-04 10:27:42 -080013854 *
Matt Roper4a3b8762014-12-23 10:41:51 -080013855 * Common destruction function for all types of planes (primary, cursor,
13856 * sprite).
Matt Ropercf4c7c12014-12-04 10:27:42 -080013857 */
Matt Roper4a3b8762014-12-23 10:41:51 -080013858void intel_plane_destroy(struct drm_plane *plane)
Matt Roper465c1202014-05-29 08:06:54 -070013859{
13860 struct intel_plane *intel_plane = to_intel_plane(plane);
13861 drm_plane_cleanup(plane);
13862 kfree(intel_plane);
13863}
13864
Matt Roper65a3fea2015-01-21 16:35:42 -080013865const struct drm_plane_funcs intel_plane_funcs = {
Matt Roper70a101f2015-04-08 18:56:53 -070013866 .update_plane = drm_atomic_helper_update_plane,
13867 .disable_plane = drm_atomic_helper_disable_plane,
Matt Roper3d7d6512014-06-10 08:28:13 -070013868 .destroy = intel_plane_destroy,
Matt Roperc196e1d2015-01-21 16:35:48 -080013869 .set_property = drm_atomic_helper_plane_set_property,
Matt Ropera98b3432015-01-21 16:35:43 -080013870 .atomic_get_property = intel_plane_atomic_get_property,
13871 .atomic_set_property = intel_plane_atomic_set_property,
Matt Roperea2c67b2014-12-23 10:41:52 -080013872 .atomic_duplicate_state = intel_plane_duplicate_state,
13873 .atomic_destroy_state = intel_plane_destroy_state,
13874
Matt Roper465c1202014-05-29 08:06:54 -070013875};
13876
13877static struct drm_plane *intel_primary_plane_create(struct drm_device *dev,
13878 int pipe)
13879{
13880 struct intel_plane *primary;
Matt Roper8e7d6882015-01-21 16:35:41 -080013881 struct intel_plane_state *state;
Matt Roper465c1202014-05-29 08:06:54 -070013882 const uint32_t *intel_primary_formats;
13883 int num_formats;
13884
13885 primary = kzalloc(sizeof(*primary), GFP_KERNEL);
13886 if (primary == NULL)
13887 return NULL;
13888
Matt Roper8e7d6882015-01-21 16:35:41 -080013889 state = intel_create_plane_state(&primary->base);
13890 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080013891 kfree(primary);
13892 return NULL;
13893 }
Matt Roper8e7d6882015-01-21 16:35:41 -080013894 primary->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080013895
Matt Roper465c1202014-05-29 08:06:54 -070013896 primary->can_scale = false;
13897 primary->max_downscale = 1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013898 if (INTEL_INFO(dev)->gen >= 9) {
13899 primary->can_scale = true;
Chandra Konduruaf99ced2015-05-11 14:35:47 -070013900 state->scaler_id = -1;
Chandra Konduru6156a452015-04-27 13:48:39 -070013901 }
Matt Roper465c1202014-05-29 08:06:54 -070013902 primary->pipe = pipe;
13903 primary->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080013904 primary->check_plane = intel_check_primary_plane;
13905 primary->commit_plane = intel_commit_primary_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030013906 primary->disable_plane = intel_disable_primary_plane;
Chandra Konduru08e221f2015-04-07 15:28:37 -070013907 primary->ckey.flags = I915_SET_COLORKEY_NONE;
Matt Roper465c1202014-05-29 08:06:54 -070013908 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4)
13909 primary->plane = !pipe;
13910
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013911 if (INTEL_INFO(dev)->gen >= 9) {
13912 intel_primary_formats = skl_primary_formats;
13913 num_formats = ARRAY_SIZE(skl_primary_formats);
13914 } else if (INTEL_INFO(dev)->gen >= 4) {
Damien Lespiau568db4f2015-05-12 16:13:18 +010013915 intel_primary_formats = i965_primary_formats;
13916 num_formats = ARRAY_SIZE(i965_primary_formats);
Damien Lespiau6c0fd452015-05-19 12:29:16 +010013917 } else {
13918 intel_primary_formats = i8xx_primary_formats;
13919 num_formats = ARRAY_SIZE(i8xx_primary_formats);
Matt Roper465c1202014-05-29 08:06:54 -070013920 }
13921
13922 drm_universal_plane_init(dev, &primary->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080013923 &intel_plane_funcs,
Matt Roper465c1202014-05-29 08:06:54 -070013924 intel_primary_formats, num_formats,
13925 DRM_PLANE_TYPE_PRIMARY);
Sonika Jindal48404c12014-08-22 14:06:04 +053013926
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013927 if (INTEL_INFO(dev)->gen >= 4)
13928 intel_create_rotation_property(dev, primary);
Sonika Jindal48404c12014-08-22 14:06:04 +053013929
Matt Roperea2c67b2014-12-23 10:41:52 -080013930 drm_plane_helper_add(&primary->base, &intel_plane_helper_funcs);
13931
Matt Roper465c1202014-05-29 08:06:54 -070013932 return &primary->base;
13933}
13934
Sonika Jindal3b7a5112015-04-10 14:37:29 +053013935void intel_create_rotation_property(struct drm_device *dev, struct intel_plane *plane)
13936{
13937 if (!dev->mode_config.rotation_property) {
13938 unsigned long flags = BIT(DRM_ROTATE_0) |
13939 BIT(DRM_ROTATE_180);
13940
13941 if (INTEL_INFO(dev)->gen >= 9)
13942 flags |= BIT(DRM_ROTATE_90) | BIT(DRM_ROTATE_270);
13943
13944 dev->mode_config.rotation_property =
13945 drm_mode_create_rotation_property(dev, flags);
13946 }
13947 if (dev->mode_config.rotation_property)
13948 drm_object_attach_property(&plane->base.base,
13949 dev->mode_config.rotation_property,
13950 plane->base.state->rotation);
13951}
13952
Matt Roper3d7d6512014-06-10 08:28:13 -070013953static int
Gustavo Padovan852e7872014-09-05 17:22:31 -030013954intel_check_cursor_plane(struct drm_plane *plane,
13955 struct intel_plane_state *state)
Matt Roper3d7d6512014-06-10 08:28:13 -070013956{
Matt Roper2b875c22014-12-01 15:40:13 -080013957 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080013958 struct drm_device *dev = plane->dev;
Matt Roper2b875c22014-12-01 15:40:13 -080013959 struct drm_framebuffer *fb = state->base.fb;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013960 struct drm_rect *dest = &state->dst;
13961 struct drm_rect *src = &state->src;
13962 const struct drm_rect *clip = &state->clip;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013963 struct drm_i915_gem_object *obj = intel_fb_obj(fb);
Matt Roperea2c67b2014-12-23 10:41:52 -080013964 struct intel_crtc *intel_crtc;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013965 unsigned stride;
13966 int ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030013967
Matt Roperea2c67b2014-12-23 10:41:52 -080013968 crtc = crtc ? crtc : plane->crtc;
13969 intel_crtc = to_intel_crtc(crtc);
13970
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013971 ret = drm_plane_helper_check_update(plane, crtc, fb,
Gustavo Padovan852e7872014-09-05 17:22:31 -030013972 src, dest, clip,
13973 DRM_PLANE_HELPER_NO_SCALING,
13974 DRM_PLANE_HELPER_NO_SCALING,
13975 true, true, &state->visible);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013976 if (ret)
13977 return ret;
13978
13979
13980 /* if we want to turn off the cursor ignore width and height */
13981 if (!obj)
Matt Roper32b7eee2014-12-24 07:59:06 -080013982 goto finish;
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013983
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013984 /* Check for which cursor types we support */
Matt Roperea2c67b2014-12-23 10:41:52 -080013985 if (!cursor_size_ok(dev, state->base.crtc_w, state->base.crtc_h)) {
13986 DRM_DEBUG("Cursor dimension %dx%d not supported\n",
13987 state->base.crtc_w, state->base.crtc_h);
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013988 return -EINVAL;
13989 }
13990
Matt Roperea2c67b2014-12-23 10:41:52 -080013991 stride = roundup_pow_of_two(state->base.crtc_w) * 4;
13992 if (obj->base.size < stride * state->base.crtc_h) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013993 DRM_DEBUG_KMS("buffer is too small\n");
13994 return -ENOMEM;
13995 }
13996
Ville Syrjälä3a656b52015-03-09 21:08:37 +020013997 if (fb->modifier[0] != DRM_FORMAT_MOD_NONE) {
Gustavo Padovan757f9a32014-09-24 14:20:24 -030013998 DRM_DEBUG_KMS("cursor cannot be tiled\n");
13999 ret = -EINVAL;
14000 }
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014001
Matt Roper32b7eee2014-12-24 07:59:06 -080014002finish:
14003 if (intel_crtc->active) {
Ville Syrjälä3749f462015-03-10 13:15:22 +020014004 if (plane->state->crtc_w != state->base.crtc_w)
Matt Roper32b7eee2014-12-24 07:59:06 -080014005 intel_crtc->atomic.update_wm = true;
14006
14007 intel_crtc->atomic.fb_bits |=
14008 INTEL_FRONTBUFFER_CURSOR(intel_crtc->pipe);
14009 }
14010
Gustavo Padovan757f9a32014-09-24 14:20:24 -030014011 return ret;
Gustavo Padovan852e7872014-09-05 17:22:31 -030014012}
14013
Matt Roperf4a2cf22014-12-01 15:40:12 -080014014static void
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014015intel_disable_cursor_plane(struct drm_plane *plane,
14016 struct drm_crtc *crtc,
14017 bool force)
14018{
14019 struct intel_crtc *intel_crtc = to_intel_crtc(crtc);
14020
14021 if (!force) {
14022 plane->fb = NULL;
14023 intel_crtc->cursor_bo = NULL;
14024 intel_crtc->cursor_addr = 0;
14025 }
14026
14027 intel_crtc_update_cursor(crtc, false);
14028}
14029
14030static void
Gustavo Padovan852e7872014-09-05 17:22:31 -030014031intel_commit_cursor_plane(struct drm_plane *plane,
14032 struct intel_plane_state *state)
14033{
Matt Roper2b875c22014-12-01 15:40:13 -080014034 struct drm_crtc *crtc = state->base.crtc;
Matt Roperea2c67b2014-12-23 10:41:52 -080014035 struct drm_device *dev = plane->dev;
14036 struct intel_crtc *intel_crtc;
Matt Roper2b875c22014-12-01 15:40:13 -080014037 struct drm_i915_gem_object *obj = intel_fb_obj(state->base.fb);
Gustavo Padovana912f122014-12-01 15:40:10 -080014038 uint32_t addr;
Matt Roper3d7d6512014-06-10 08:28:13 -070014039
Matt Roperea2c67b2014-12-23 10:41:52 -080014040 crtc = crtc ? crtc : plane->crtc;
14041 intel_crtc = to_intel_crtc(crtc);
Sonika Jindala919db92014-10-23 07:41:33 -070014042
Matt Roperea2c67b2014-12-23 10:41:52 -080014043 plane->fb = state->base.fb;
14044 crtc->cursor_x = state->base.crtc_x;
14045 crtc->cursor_y = state->base.crtc_y;
14046
Gustavo Padovana912f122014-12-01 15:40:10 -080014047 if (intel_crtc->cursor_bo == obj)
14048 goto update;
14049
Matt Roperf4a2cf22014-12-01 15:40:12 -080014050 if (!obj)
Gustavo Padovana912f122014-12-01 15:40:10 -080014051 addr = 0;
Matt Roperf4a2cf22014-12-01 15:40:12 -080014052 else if (!INTEL_INFO(dev)->cursor_needs_physical)
Gustavo Padovana912f122014-12-01 15:40:10 -080014053 addr = i915_gem_obj_ggtt_offset(obj);
Matt Roperf4a2cf22014-12-01 15:40:12 -080014054 else
Gustavo Padovana912f122014-12-01 15:40:10 -080014055 addr = obj->phys_handle->busaddr;
Gustavo Padovana912f122014-12-01 15:40:10 -080014056
Gustavo Padovana912f122014-12-01 15:40:10 -080014057 intel_crtc->cursor_addr = addr;
14058 intel_crtc->cursor_bo = obj;
14059update:
Gustavo Padovana912f122014-12-01 15:40:10 -080014060
Matt Roper32b7eee2014-12-24 07:59:06 -080014061 if (intel_crtc->active)
Gustavo Padovan852e7872014-09-05 17:22:31 -030014062 intel_crtc_update_cursor(crtc, state->visible);
Matt Roper3d7d6512014-06-10 08:28:13 -070014063}
Gustavo Padovan852e7872014-09-05 17:22:31 -030014064
Matt Roper3d7d6512014-06-10 08:28:13 -070014065static struct drm_plane *intel_cursor_plane_create(struct drm_device *dev,
14066 int pipe)
14067{
14068 struct intel_plane *cursor;
Matt Roper8e7d6882015-01-21 16:35:41 -080014069 struct intel_plane_state *state;
Matt Roper3d7d6512014-06-10 08:28:13 -070014070
14071 cursor = kzalloc(sizeof(*cursor), GFP_KERNEL);
14072 if (cursor == NULL)
14073 return NULL;
14074
Matt Roper8e7d6882015-01-21 16:35:41 -080014075 state = intel_create_plane_state(&cursor->base);
14076 if (!state) {
Matt Roperea2c67b2014-12-23 10:41:52 -080014077 kfree(cursor);
14078 return NULL;
14079 }
Matt Roper8e7d6882015-01-21 16:35:41 -080014080 cursor->base.state = &state->base;
Matt Roperea2c67b2014-12-23 10:41:52 -080014081
Matt Roper3d7d6512014-06-10 08:28:13 -070014082 cursor->can_scale = false;
14083 cursor->max_downscale = 1;
14084 cursor->pipe = pipe;
14085 cursor->plane = pipe;
Matt Roperc59cb172014-12-01 15:40:16 -080014086 cursor->check_plane = intel_check_cursor_plane;
14087 cursor->commit_plane = intel_commit_cursor_plane;
Maarten Lankhorsta8ad0d82015-04-21 17:12:51 +030014088 cursor->disable_plane = intel_disable_cursor_plane;
Matt Roper3d7d6512014-06-10 08:28:13 -070014089
14090 drm_universal_plane_init(dev, &cursor->base, 0,
Matt Roper65a3fea2015-01-21 16:35:42 -080014091 &intel_plane_funcs,
Matt Roper3d7d6512014-06-10 08:28:13 -070014092 intel_cursor_formats,
14093 ARRAY_SIZE(intel_cursor_formats),
14094 DRM_PLANE_TYPE_CURSOR);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014095
14096 if (INTEL_INFO(dev)->gen >= 4) {
14097 if (!dev->mode_config.rotation_property)
14098 dev->mode_config.rotation_property =
14099 drm_mode_create_rotation_property(dev,
14100 BIT(DRM_ROTATE_0) |
14101 BIT(DRM_ROTATE_180));
14102 if (dev->mode_config.rotation_property)
14103 drm_object_attach_property(&cursor->base.base,
14104 dev->mode_config.rotation_property,
Matt Roper8e7d6882015-01-21 16:35:41 -080014105 state->base.rotation);
Ville Syrjälä4398ad42014-10-23 07:41:34 -070014106 }
14107
Chandra Konduruaf99ced2015-05-11 14:35:47 -070014108 if (INTEL_INFO(dev)->gen >=9)
14109 state->scaler_id = -1;
14110
Matt Roperea2c67b2014-12-23 10:41:52 -080014111 drm_plane_helper_add(&cursor->base, &intel_plane_helper_funcs);
14112
Matt Roper3d7d6512014-06-10 08:28:13 -070014113 return &cursor->base;
14114}
14115
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014116static void skl_init_scalers(struct drm_device *dev, struct intel_crtc *intel_crtc,
14117 struct intel_crtc_state *crtc_state)
14118{
14119 int i;
14120 struct intel_scaler *intel_scaler;
14121 struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
14122
14123 for (i = 0; i < intel_crtc->num_scalers; i++) {
14124 intel_scaler = &scaler_state->scalers[i];
14125 intel_scaler->in_use = 0;
14126 intel_scaler->id = i;
14127
14128 intel_scaler->mode = PS_SCALER_MODE_DYN;
14129 }
14130
14131 scaler_state->scaler_id = -1;
14132}
14133
Hannes Ederb358d0a2008-12-18 21:18:47 +010014134static void intel_crtc_init(struct drm_device *dev, int pipe)
Jesse Barnes79e53942008-11-07 14:24:08 -080014135{
Jani Nikulafbee40d2014-03-31 14:27:18 +030014136 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes79e53942008-11-07 14:24:08 -080014137 struct intel_crtc *intel_crtc;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014138 struct intel_crtc_state *crtc_state = NULL;
Matt Roper3d7d6512014-06-10 08:28:13 -070014139 struct drm_plane *primary = NULL;
14140 struct drm_plane *cursor = NULL;
Matt Roper465c1202014-05-29 08:06:54 -070014141 int i, ret;
Jesse Barnes79e53942008-11-07 14:24:08 -080014142
Daniel Vetter955382f2013-09-19 14:05:45 +020014143 intel_crtc = kzalloc(sizeof(*intel_crtc), GFP_KERNEL);
Jesse Barnes79e53942008-11-07 14:24:08 -080014144 if (intel_crtc == NULL)
14145 return;
14146
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014147 crtc_state = kzalloc(sizeof(*crtc_state), GFP_KERNEL);
14148 if (!crtc_state)
14149 goto fail;
Ander Conselvan de Oliveira550acef2015-04-21 17:13:24 +030014150 intel_crtc->config = crtc_state;
14151 intel_crtc->base.state = &crtc_state->base;
Matt Roper07878242015-02-25 11:43:26 -080014152 crtc_state->base.crtc = &intel_crtc->base;
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014153
Chandra Konduru549e2bf2015-04-07 15:28:38 -070014154 /* initialize shared scalers */
14155 if (INTEL_INFO(dev)->gen >= 9) {
14156 if (pipe == PIPE_C)
14157 intel_crtc->num_scalers = 1;
14158 else
14159 intel_crtc->num_scalers = SKL_NUM_SCALERS;
14160
14161 skl_init_scalers(dev, intel_crtc, crtc_state);
14162 }
14163
Matt Roper465c1202014-05-29 08:06:54 -070014164 primary = intel_primary_plane_create(dev, pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014165 if (!primary)
14166 goto fail;
14167
14168 cursor = intel_cursor_plane_create(dev, pipe);
14169 if (!cursor)
14170 goto fail;
14171
Matt Roper465c1202014-05-29 08:06:54 -070014172 ret = drm_crtc_init_with_planes(dev, &intel_crtc->base, primary,
Matt Roper3d7d6512014-06-10 08:28:13 -070014173 cursor, &intel_crtc_funcs);
14174 if (ret)
14175 goto fail;
Jesse Barnes79e53942008-11-07 14:24:08 -080014176
14177 drm_mode_crtc_set_gamma_size(&intel_crtc->base, 256);
Jesse Barnes79e53942008-11-07 14:24:08 -080014178 for (i = 0; i < 256; i++) {
14179 intel_crtc->lut_r[i] = i;
14180 intel_crtc->lut_g[i] = i;
14181 intel_crtc->lut_b[i] = i;
14182 }
14183
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014184 /*
14185 * On gen2/3 only plane A can do fbc, but the panel fitter and lvds port
Daniel Vetter8c0f92e2014-06-16 02:08:26 +020014186 * is hooked to pipe B. Hence we want plane A feeding pipe B.
Ville Syrjälä1f1c2e22013-11-28 17:30:01 +020014187 */
Jesse Barnes80824002009-09-10 15:28:06 -070014188 intel_crtc->pipe = pipe;
14189 intel_crtc->plane = pipe;
Daniel Vetter3a77c4c2014-01-10 08:50:12 +010014190 if (HAS_FBC(dev) && INTEL_INFO(dev)->gen < 4) {
Zhao Yakui28c97732009-10-09 11:39:41 +080014191 DRM_DEBUG_KMS("swapping pipes & planes for FBC\n");
Chris Wilsone2e767a2010-09-13 16:53:12 +010014192 intel_crtc->plane = !pipe;
Jesse Barnes80824002009-09-10 15:28:06 -070014193 }
14194
Chris Wilson4b0e3332014-05-30 16:35:26 +030014195 intel_crtc->cursor_base = ~0;
14196 intel_crtc->cursor_cntl = ~0;
Ville Syrjälädc41c152014-08-13 11:57:05 +030014197 intel_crtc->cursor_size = ~0;
Ville Syrjälä8d7849d2014-04-29 13:35:46 +030014198
Jesse Barnes22fd0fa2009-12-02 13:42:53 -080014199 BUG_ON(pipe >= ARRAY_SIZE(dev_priv->plane_to_crtc_mapping) ||
14200 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] != NULL);
14201 dev_priv->plane_to_crtc_mapping[intel_crtc->plane] = &intel_crtc->base;
14202 dev_priv->pipe_to_crtc_mapping[intel_crtc->pipe] = &intel_crtc->base;
14203
Jesse Barnes79e53942008-11-07 14:24:08 -080014204 drm_crtc_helper_add(&intel_crtc->base, &intel_helper_funcs);
Daniel Vetter87b6b102014-05-15 15:33:46 +020014205
14206 WARN_ON(drm_crtc_index(&intel_crtc->base) != intel_crtc->pipe);
Matt Roper3d7d6512014-06-10 08:28:13 -070014207 return;
14208
14209fail:
14210 if (primary)
14211 drm_plane_cleanup(primary);
14212 if (cursor)
14213 drm_plane_cleanup(cursor);
Ander Conselvan de Oliveiraf5de6e02015-01-15 14:55:26 +020014214 kfree(crtc_state);
Matt Roper3d7d6512014-06-10 08:28:13 -070014215 kfree(intel_crtc);
Jesse Barnes79e53942008-11-07 14:24:08 -080014216}
14217
Jesse Barnes752aa882013-10-31 18:55:49 +020014218enum pipe intel_get_pipe_from_connector(struct intel_connector *connector)
14219{
14220 struct drm_encoder *encoder = connector->base.encoder;
Daniel Vetter6e9f7982014-05-29 23:54:47 +020014221 struct drm_device *dev = connector->base.dev;
Jesse Barnes752aa882013-10-31 18:55:49 +020014222
Rob Clark51fd3712013-11-19 12:10:12 -050014223 WARN_ON(!drm_modeset_is_locked(&dev->mode_config.connection_mutex));
Jesse Barnes752aa882013-10-31 18:55:49 +020014224
Ville Syrjäläd3babd32014-11-07 11:16:01 +020014225 if (!encoder || WARN_ON(!encoder->crtc))
Jesse Barnes752aa882013-10-31 18:55:49 +020014226 return INVALID_PIPE;
14227
14228 return to_intel_crtc(encoder->crtc)->pipe;
14229}
14230
Carl Worth08d7b3d2009-04-29 14:43:54 -070014231int intel_get_pipe_from_crtc_id(struct drm_device *dev, void *data,
Chris Wilson05394f32010-11-08 19:18:58 +000014232 struct drm_file *file)
Carl Worth08d7b3d2009-04-29 14:43:54 -070014233{
Carl Worth08d7b3d2009-04-29 14:43:54 -070014234 struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
Rob Clark7707e652014-07-17 23:30:04 -040014235 struct drm_crtc *drmmode_crtc;
Daniel Vetterc05422d2009-08-11 16:05:30 +020014236 struct intel_crtc *crtc;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014237
Rob Clark7707e652014-07-17 23:30:04 -040014238 drmmode_crtc = drm_crtc_find(dev, pipe_from_crtc_id->crtc_id);
Carl Worth08d7b3d2009-04-29 14:43:54 -070014239
Rob Clark7707e652014-07-17 23:30:04 -040014240 if (!drmmode_crtc) {
Carl Worth08d7b3d2009-04-29 14:43:54 -070014241 DRM_ERROR("no such CRTC id\n");
Ville Syrjälä3f2c2052013-10-17 13:35:03 +030014242 return -ENOENT;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014243 }
14244
Rob Clark7707e652014-07-17 23:30:04 -040014245 crtc = to_intel_crtc(drmmode_crtc);
Daniel Vetterc05422d2009-08-11 16:05:30 +020014246 pipe_from_crtc_id->pipe = crtc->pipe;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014247
Daniel Vetterc05422d2009-08-11 16:05:30 +020014248 return 0;
Carl Worth08d7b3d2009-04-29 14:43:54 -070014249}
14250
Daniel Vetter66a92782012-07-12 20:08:18 +020014251static int intel_encoder_clones(struct intel_encoder *encoder)
Jesse Barnes79e53942008-11-07 14:24:08 -080014252{
Daniel Vetter66a92782012-07-12 20:08:18 +020014253 struct drm_device *dev = encoder->base.dev;
14254 struct intel_encoder *source_encoder;
Jesse Barnes79e53942008-11-07 14:24:08 -080014255 int index_mask = 0;
Jesse Barnes79e53942008-11-07 14:24:08 -080014256 int entry = 0;
14257
Damien Lespiaub2784e12014-08-05 11:29:37 +010014258 for_each_intel_encoder(dev, source_encoder) {
Ville Syrjäläbc079e82014-03-03 16:15:28 +020014259 if (encoders_cloneable(encoder, source_encoder))
Daniel Vetter66a92782012-07-12 20:08:18 +020014260 index_mask |= (1 << entry);
14261
Jesse Barnes79e53942008-11-07 14:24:08 -080014262 entry++;
14263 }
Chris Wilson4ef69c72010-09-09 15:14:28 +010014264
Jesse Barnes79e53942008-11-07 14:24:08 -080014265 return index_mask;
14266}
14267
Chris Wilson4d302442010-12-14 19:21:29 +000014268static bool has_edp_a(struct drm_device *dev)
14269{
14270 struct drm_i915_private *dev_priv = dev->dev_private;
14271
14272 if (!IS_MOBILE(dev))
14273 return false;
14274
14275 if ((I915_READ(DP_A) & DP_DETECTED) == 0)
14276 return false;
14277
Damien Lespiaue3589902014-02-07 19:12:50 +000014278 if (IS_GEN5(dev) && (I915_READ(FUSE_STRAP) & ILK_eDP_A_DISABLE))
Chris Wilson4d302442010-12-14 19:21:29 +000014279 return false;
14280
14281 return true;
14282}
14283
Jesse Barnes84b4e042014-06-25 08:24:29 -070014284static bool intel_crt_present(struct drm_device *dev)
14285{
14286 struct drm_i915_private *dev_priv = dev->dev_private;
14287
Damien Lespiau884497e2013-12-03 13:56:23 +000014288 if (INTEL_INFO(dev)->gen >= 9)
14289 return false;
14290
Damien Lespiaucf404ce2014-10-01 20:04:15 +010014291 if (IS_HSW_ULT(dev) || IS_BDW_ULT(dev))
Jesse Barnes84b4e042014-06-25 08:24:29 -070014292 return false;
14293
14294 if (IS_CHERRYVIEW(dev))
14295 return false;
14296
14297 if (IS_VALLEYVIEW(dev) && !dev_priv->vbt.int_crt_support)
14298 return false;
14299
14300 return true;
14301}
14302
Jesse Barnes79e53942008-11-07 14:24:08 -080014303static void intel_setup_outputs(struct drm_device *dev)
14304{
Eric Anholt725e30a2009-01-22 13:01:02 -080014305 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilson4ef69c72010-09-09 15:14:28 +010014306 struct intel_encoder *encoder;
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014307 bool dpd_is_edp = false;
Jesse Barnes79e53942008-11-07 14:24:08 -080014308
Daniel Vetterc9093352013-06-06 22:22:47 +020014309 intel_lvds_init(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014310
Jesse Barnes84b4e042014-06-25 08:24:29 -070014311 if (intel_crt_present(dev))
Paulo Zanoni79935fc2012-11-20 13:27:40 -020014312 intel_crt_init(dev);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014313
Vandana Kannanc776eb22014-08-19 12:05:01 +053014314 if (IS_BROXTON(dev)) {
14315 /*
14316 * FIXME: Broxton doesn't support port detection via the
14317 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
14318 * detect the ports.
14319 */
14320 intel_ddi_init(dev, PORT_A);
14321 intel_ddi_init(dev, PORT_B);
14322 intel_ddi_init(dev, PORT_C);
14323 } else if (HAS_DDI(dev)) {
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014324 int found;
14325
Jesse Barnesde31fac2015-03-06 15:53:32 -080014326 /*
14327 * Haswell uses DDI functions to detect digital outputs.
14328 * On SKL pre-D0 the strap isn't connected, so we assume
14329 * it's there.
14330 */
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014331 found = I915_READ(DDI_BUF_CTL_A) & DDI_INIT_DISPLAY_DETECTED;
Jesse Barnesde31fac2015-03-06 15:53:32 -080014332 /* WaIgnoreDDIAStrap: skl */
14333 if (found ||
14334 (IS_SKYLAKE(dev) && INTEL_REVID(dev) < SKL_REVID_D0))
Eugeni Dodonov0e72a5b2012-05-09 15:37:27 -030014335 intel_ddi_init(dev, PORT_A);
14336
14337 /* DDI B, C and D detection is indicated by the SFUSE_STRAP
14338 * register */
14339 found = I915_READ(SFUSE_STRAP);
14340
14341 if (found & SFUSE_STRAP_DDIB_DETECTED)
14342 intel_ddi_init(dev, PORT_B);
14343 if (found & SFUSE_STRAP_DDIC_DETECTED)
14344 intel_ddi_init(dev, PORT_C);
14345 if (found & SFUSE_STRAP_DDID_DETECTED)
14346 intel_ddi_init(dev, PORT_D);
14347 } else if (HAS_PCH_SPLIT(dev)) {
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014348 int found;
Ville Syrjälä5d8a7752013-11-01 18:22:39 +020014349 dpd_is_edp = intel_dp_is_edp(dev, PORT_D);
Daniel Vetter270b3042012-10-27 15:52:05 +020014350
14351 if (has_edp_a(dev))
14352 intel_dp_init(dev, DP_A, PORT_A);
Adam Jacksoncb0953d2010-07-16 14:46:29 -040014353
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014354 if (I915_READ(PCH_HDMIB) & SDVO_DETECTED) {
Zhao Yakui461ed3c2010-03-30 15:11:33 +080014355 /* PCH SDVOB multiplex with HDMIB */
Daniel Vettereef4eac2012-03-23 23:43:35 +010014356 found = intel_sdvo_init(dev, PCH_SDVOB, true);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014357 if (!found)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014358 intel_hdmi_init(dev, PCH_HDMIB, PORT_B);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014359 if (!found && (I915_READ(PCH_DP_B) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014360 intel_dp_init(dev, PCH_DP_B, PORT_B);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014361 }
14362
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014363 if (I915_READ(PCH_HDMIC) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014364 intel_hdmi_init(dev, PCH_HDMIC, PORT_C);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014365
Paulo Zanonidc0fa712013-02-19 16:21:46 -030014366 if (!dpd_is_edp && I915_READ(PCH_HDMID) & SDVO_DETECTED)
Paulo Zanonie2debe92013-02-18 19:00:27 -030014367 intel_hdmi_init(dev, PCH_HDMID, PORT_D);
Zhenyu Wang30ad48b2009-06-05 15:38:43 +080014368
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014369 if (I915_READ(PCH_DP_C) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014370 intel_dp_init(dev, PCH_DP_C, PORT_C);
Zhenyu Wang5eb08b62009-07-24 01:00:31 +080014371
Daniel Vetter270b3042012-10-27 15:52:05 +020014372 if (I915_READ(PCH_DP_D) & DP_DETECTED)
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014373 intel_dp_init(dev, PCH_DP_D, PORT_D);
Jesse Barnes4a87d652012-06-15 11:55:16 -070014374 } else if (IS_VALLEYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014375 /*
14376 * The DP_DETECTED bit is the latched state of the DDC
14377 * SDA pin at boot. However since eDP doesn't require DDC
14378 * (no way to plug in a DP->HDMI dongle) the DDC pins for
14379 * eDP ports may have been muxed to an alternate function.
14380 * Thus we can't rely on the DP_DETECTED bit alone to detect
14381 * eDP ports. Consult the VBT as well as DP_DETECTED to
14382 * detect eDP ports.
14383 */
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014384 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIB) & SDVO_DETECTED &&
14385 !intel_dp_is_edp(dev, PORT_B))
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014386 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIB,
14387 PORT_B);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014388 if (I915_READ(VLV_DISPLAY_BASE + DP_B) & DP_DETECTED ||
14389 intel_dp_is_edp(dev, PORT_B))
14390 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_B, PORT_B);
Artem Bityutskiy585a94b2013-10-16 18:10:41 +030014391
Ville Syrjäläd2182a62015-01-09 14:21:14 +020014392 if (I915_READ(VLV_DISPLAY_BASE + GEN4_HDMIC) & SDVO_DETECTED &&
14393 !intel_dp_is_edp(dev, PORT_C))
Jesse Barnes6f6005a2013-08-09 09:34:35 -070014394 intel_hdmi_init(dev, VLV_DISPLAY_BASE + GEN4_HDMIC,
14395 PORT_C);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014396 if (I915_READ(VLV_DISPLAY_BASE + DP_C) & DP_DETECTED ||
14397 intel_dp_is_edp(dev, PORT_C))
14398 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_C, PORT_C);
Gajanan Bhat19c03922012-09-27 19:13:07 +053014399
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014400 if (IS_CHERRYVIEW(dev)) {
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014401 if (I915_READ(VLV_DISPLAY_BASE + CHV_HDMID) & SDVO_DETECTED)
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014402 intel_hdmi_init(dev, VLV_DISPLAY_BASE + CHV_HDMID,
14403 PORT_D);
Ville Syrjäläe17ac6d2014-10-09 19:37:15 +030014404 /* eDP not supported on port D, so don't check VBT */
14405 if (I915_READ(VLV_DISPLAY_BASE + DP_D) & DP_DETECTED)
14406 intel_dp_init(dev, VLV_DISPLAY_BASE + DP_D, PORT_D);
Ville Syrjälä9418c1f2014-04-09 13:28:56 +030014407 }
14408
Jani Nikula3cfca972013-08-27 15:12:26 +030014409 intel_dsi_init(dev);
Zhenyu Wang103a1962009-11-27 11:44:36 +080014410 } else if (SUPPORTS_DIGITAL_OUTPUTS(dev)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014411 bool found = false;
Eric Anholt7d573822009-01-02 13:33:00 -080014412
Paulo Zanonie2debe92013-02-18 19:00:27 -030014413 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014414 DRM_DEBUG_KMS("probing SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014415 found = intel_sdvo_init(dev, GEN3_SDVOB, true);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014416 if (!found && SUPPORTS_INTEGRATED_HDMI(dev)) {
14417 DRM_DEBUG_KMS("probing HDMI on SDVOB\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014418 intel_hdmi_init(dev, GEN4_HDMIB, PORT_B);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014419 }
Ma Ling27185ae2009-08-24 13:50:23 +080014420
Imre Deake7281ea2013-05-08 13:14:08 +030014421 if (!found && SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014422 intel_dp_init(dev, DP_B, PORT_B);
Eric Anholt725e30a2009-01-22 13:01:02 -080014423 }
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014424
14425 /* Before G4X SDVOC doesn't have its own detect register */
Kristian Høgsberg13520b02009-03-13 15:42:14 -040014426
Paulo Zanonie2debe92013-02-18 19:00:27 -030014427 if (I915_READ(GEN3_SDVOB) & SDVO_DETECTED) {
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014428 DRM_DEBUG_KMS("probing SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014429 found = intel_sdvo_init(dev, GEN3_SDVOC, false);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014430 }
Ma Ling27185ae2009-08-24 13:50:23 +080014431
Paulo Zanonie2debe92013-02-18 19:00:27 -030014432 if (!found && (I915_READ(GEN3_SDVOC) & SDVO_DETECTED)) {
Ma Ling27185ae2009-08-24 13:50:23 +080014433
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014434 if (SUPPORTS_INTEGRATED_HDMI(dev)) {
14435 DRM_DEBUG_KMS("probing HDMI on SDVOC\n");
Paulo Zanonie2debe92013-02-18 19:00:27 -030014436 intel_hdmi_init(dev, GEN4_HDMIC, PORT_C);
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014437 }
Imre Deake7281ea2013-05-08 13:14:08 +030014438 if (SUPPORTS_INTEGRATED_DP(dev))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014439 intel_dp_init(dev, DP_C, PORT_C);
Eric Anholt725e30a2009-01-22 13:01:02 -080014440 }
Ma Ling27185ae2009-08-24 13:50:23 +080014441
Jesse Barnesb01f2c32009-12-11 11:07:17 -080014442 if (SUPPORTS_INTEGRATED_DP(dev) &&
Imre Deake7281ea2013-05-08 13:14:08 +030014443 (I915_READ(DP_D) & DP_DETECTED))
Paulo Zanoniab9d7c32012-07-17 17:53:45 -030014444 intel_dp_init(dev, DP_D, PORT_D);
Eric Anholtbad720f2009-10-22 16:11:14 -070014445 } else if (IS_GEN2(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014446 intel_dvo_init(dev);
14447
Zhenyu Wang103a1962009-11-27 11:44:36 +080014448 if (SUPPORTS_TV(dev))
Jesse Barnes79e53942008-11-07 14:24:08 -080014449 intel_tv_init(dev);
14450
Rodrigo Vivi0bc12bc2014-11-14 08:52:28 -080014451 intel_psr_init(dev);
Rodrigo Vivi7c8f8a72014-06-13 05:10:03 -070014452
Damien Lespiaub2784e12014-08-05 11:29:37 +010014453 for_each_intel_encoder(dev, encoder) {
Chris Wilson4ef69c72010-09-09 15:14:28 +010014454 encoder->base.possible_crtcs = encoder->crtc_mask;
14455 encoder->base.possible_clones =
Daniel Vetter66a92782012-07-12 20:08:18 +020014456 intel_encoder_clones(encoder);
Jesse Barnes79e53942008-11-07 14:24:08 -080014457 }
Chris Wilson47356eb2011-01-11 17:06:04 +000014458
Paulo Zanonidde86e22012-12-01 12:04:25 -020014459 intel_init_pch_refclk(dev);
Daniel Vetter270b3042012-10-27 15:52:05 +020014460
14461 drm_helper_move_panel_connectors_to_head(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080014462}
14463
14464static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
14465{
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014466 struct drm_device *dev = fb->dev;
Jesse Barnes79e53942008-11-07 14:24:08 -080014467 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Jesse Barnes79e53942008-11-07 14:24:08 -080014468
Daniel Vetteref2d6332014-02-10 18:00:38 +010014469 drm_framebuffer_cleanup(fb);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014470 mutex_lock(&dev->struct_mutex);
Daniel Vetteref2d6332014-02-10 18:00:38 +010014471 WARN_ON(!intel_fb->obj->framebuffer_references--);
Ville Syrjälä60a5ca02014-06-13 11:10:53 +030014472 drm_gem_object_unreference(&intel_fb->obj->base);
14473 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080014474 kfree(intel_fb);
14475}
14476
14477static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
Chris Wilson05394f32010-11-08 19:18:58 +000014478 struct drm_file *file,
Jesse Barnes79e53942008-11-07 14:24:08 -080014479 unsigned int *handle)
14480{
14481 struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
Chris Wilson05394f32010-11-08 19:18:58 +000014482 struct drm_i915_gem_object *obj = intel_fb->obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014483
Chris Wilson05394f32010-11-08 19:18:58 +000014484 return drm_gem_handle_create(file, &obj->base, handle);
Jesse Barnes79e53942008-11-07 14:24:08 -080014485}
14486
14487static const struct drm_framebuffer_funcs intel_fb_funcs = {
14488 .destroy = intel_user_framebuffer_destroy,
14489 .create_handle = intel_user_framebuffer_create_handle,
14490};
14491
Damien Lespiaub3218032015-02-27 11:15:18 +000014492static
14493u32 intel_fb_pitch_limit(struct drm_device *dev, uint64_t fb_modifier,
14494 uint32_t pixel_format)
14495{
14496 u32 gen = INTEL_INFO(dev)->gen;
14497
14498 if (gen >= 9) {
14499 /* "The stride in bytes must not exceed the of the size of 8K
14500 * pixels and 32K bytes."
14501 */
14502 return min(8192*drm_format_plane_cpp(pixel_format, 0), 32768);
14503 } else if (gen >= 5 && !IS_VALLEYVIEW(dev)) {
14504 return 32*1024;
14505 } else if (gen >= 4) {
14506 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14507 return 16*1024;
14508 else
14509 return 32*1024;
14510 } else if (gen >= 3) {
14511 if (fb_modifier == I915_FORMAT_MOD_X_TILED)
14512 return 8*1024;
14513 else
14514 return 16*1024;
14515 } else {
14516 /* XXX DSPC is limited to 4k tiled */
14517 return 8*1024;
14518 }
14519}
14520
Daniel Vetterb5ea6422014-03-02 21:18:00 +010014521static int intel_framebuffer_init(struct drm_device *dev,
14522 struct intel_framebuffer *intel_fb,
14523 struct drm_mode_fb_cmd2 *mode_cmd,
14524 struct drm_i915_gem_object *obj)
Jesse Barnes79e53942008-11-07 14:24:08 -080014525{
Tvrtko Ursulin6761dd32015-03-23 11:10:32 +000014526 unsigned int aligned_height;
Jesse Barnes79e53942008-11-07 14:24:08 -080014527 int ret;
Damien Lespiaub3218032015-02-27 11:15:18 +000014528 u32 pitch_limit, stride_alignment;
Jesse Barnes79e53942008-11-07 14:24:08 -080014529
Daniel Vetterdd4916c2013-10-09 21:23:51 +020014530 WARN_ON(!mutex_is_locked(&dev->struct_mutex));
14531
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014532 if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
14533 /* Enforce that fb modifier and tiling mode match, but only for
14534 * X-tiled. This is needed for FBC. */
14535 if (!!(obj->tiling_mode == I915_TILING_X) !=
14536 !!(mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED)) {
14537 DRM_DEBUG("tiling_mode doesn't match fb modifier\n");
14538 return -EINVAL;
14539 }
14540 } else {
14541 if (obj->tiling_mode == I915_TILING_X)
14542 mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
14543 else if (obj->tiling_mode == I915_TILING_Y) {
14544 DRM_DEBUG("No Y tiling for legacy addfb\n");
14545 return -EINVAL;
14546 }
14547 }
14548
Tvrtko Ursulin9a8f0a12015-02-27 11:15:24 +000014549 /* Passed in modifier sanity checking. */
14550 switch (mode_cmd->modifier[0]) {
14551 case I915_FORMAT_MOD_Y_TILED:
14552 case I915_FORMAT_MOD_Yf_TILED:
14553 if (INTEL_INFO(dev)->gen < 9) {
14554 DRM_DEBUG("Unsupported tiling 0x%llx!\n",
14555 mode_cmd->modifier[0]);
14556 return -EINVAL;
14557 }
14558 case DRM_FORMAT_MOD_NONE:
14559 case I915_FORMAT_MOD_X_TILED:
14560 break;
14561 default:
Jesse Barnesc0f40422015-03-23 12:43:50 -070014562 DRM_DEBUG("Unsupported fb modifier 0x%llx!\n",
14563 mode_cmd->modifier[0]);
Chris Wilson57cd6502010-08-08 12:34:44 +010014564 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014565 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014566
Damien Lespiaub3218032015-02-27 11:15:18 +000014567 stride_alignment = intel_fb_stride_alignment(dev, mode_cmd->modifier[0],
14568 mode_cmd->pixel_format);
14569 if (mode_cmd->pitches[0] & (stride_alignment - 1)) {
14570 DRM_DEBUG("pitch (%d) must be at least %u byte aligned\n",
14571 mode_cmd->pitches[0], stride_alignment);
Chris Wilson57cd6502010-08-08 12:34:44 +010014572 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014573 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014574
Damien Lespiaub3218032015-02-27 11:15:18 +000014575 pitch_limit = intel_fb_pitch_limit(dev, mode_cmd->modifier[0],
14576 mode_cmd->pixel_format);
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014577 if (mode_cmd->pitches[0] > pitch_limit) {
Damien Lespiaub3218032015-02-27 11:15:18 +000014578 DRM_DEBUG("%s pitch (%u) must be at less than %d\n",
14579 mode_cmd->modifier[0] != DRM_FORMAT_MOD_NONE ?
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014580 "tiled" : "linear",
Chris Wilsona35cdaa2013-06-25 17:26:45 +010014581 mode_cmd->pitches[0], pitch_limit);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014582 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014583 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014584
Daniel Vetter2a80ead2015-02-10 17:16:06 +000014585 if (mode_cmd->modifier[0] == I915_FORMAT_MOD_X_TILED &&
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014586 mode_cmd->pitches[0] != obj->stride) {
14587 DRM_DEBUG("pitch (%d) must match tiling stride (%d)\n",
14588 mode_cmd->pitches[0], obj->stride);
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014589 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014590 }
Ville Syrjälä5d7bd702012-10-31 17:50:18 +020014591
Ville Syrjälä57779d02012-10-31 17:50:14 +020014592 /* Reject formats not supported by any plane early. */
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014593 switch (mode_cmd->pixel_format) {
Ville Syrjälä57779d02012-10-31 17:50:14 +020014594 case DRM_FORMAT_C8:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014595 case DRM_FORMAT_RGB565:
14596 case DRM_FORMAT_XRGB8888:
14597 case DRM_FORMAT_ARGB8888:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014598 break;
14599 case DRM_FORMAT_XRGB1555:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014600 if (INTEL_INFO(dev)->gen > 3) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014601 DRM_DEBUG("unsupported pixel format: %s\n",
14602 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014603 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014604 }
Ville Syrjälä57779d02012-10-31 17:50:14 +020014605 break;
Ville Syrjälä57779d02012-10-31 17:50:14 +020014606 case DRM_FORMAT_ABGR8888:
Damien Lespiau6c0fd452015-05-19 12:29:16 +010014607 if (!IS_VALLEYVIEW(dev) && INTEL_INFO(dev)->gen < 9) {
14608 DRM_DEBUG("unsupported pixel format: %s\n",
14609 drm_get_format_name(mode_cmd->pixel_format));
14610 return -EINVAL;
14611 }
14612 break;
14613 case DRM_FORMAT_XBGR8888:
Ville Syrjälä04b39242011-11-17 18:05:13 +020014614 case DRM_FORMAT_XRGB2101010:
Ville Syrjälä57779d02012-10-31 17:50:14 +020014615 case DRM_FORMAT_XBGR2101010:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014616 if (INTEL_INFO(dev)->gen < 4) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014617 DRM_DEBUG("unsupported pixel format: %s\n",
14618 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014619 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014620 }
Jesse Barnesb5626742011-06-24 12:19:27 -070014621 break;
Damien Lespiau75312082015-05-15 19:06:01 +010014622 case DRM_FORMAT_ABGR2101010:
14623 if (!IS_VALLEYVIEW(dev)) {
14624 DRM_DEBUG("unsupported pixel format: %s\n",
14625 drm_get_format_name(mode_cmd->pixel_format));
14626 return -EINVAL;
14627 }
14628 break;
Ville Syrjälä04b39242011-11-17 18:05:13 +020014629 case DRM_FORMAT_YUYV:
14630 case DRM_FORMAT_UYVY:
14631 case DRM_FORMAT_YVYU:
14632 case DRM_FORMAT_VYUY:
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014633 if (INTEL_INFO(dev)->gen < 5) {
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014634 DRM_DEBUG("unsupported pixel format: %s\n",
14635 drm_get_format_name(mode_cmd->pixel_format));
Ville Syrjälä57779d02012-10-31 17:50:14 +020014636 return -EINVAL;
Chris Wilsonc16ed4b2012-12-18 22:13:14 +000014637 }
Chris Wilson57cd6502010-08-08 12:34:44 +010014638 break;
14639 default:
Ville Syrjälä4ee62c72013-06-07 15:43:05 +000014640 DRM_DEBUG("unsupported pixel format: %s\n",
14641 drm_get_format_name(mode_cmd->pixel_format));
Chris Wilson57cd6502010-08-08 12:34:44 +010014642 return -EINVAL;
14643 }
14644
Ville Syrjälä90f9a332012-10-31 17:50:19 +020014645 /* FIXME need to adjust LINOFF/TILEOFF accordingly. */
14646 if (mode_cmd->offsets[0] != 0)
14647 return -EINVAL;
14648
Damien Lespiauec2c9812015-01-20 12:51:45 +000014649 aligned_height = intel_fb_align_height(dev, mode_cmd->height,
Daniel Vetter091df6c2015-02-10 17:16:10 +000014650 mode_cmd->pixel_format,
14651 mode_cmd->modifier[0]);
Daniel Vetter53155c02013-10-09 21:55:33 +020014652 /* FIXME drm helper for size checks (especially planar formats)? */
14653 if (obj->base.size < aligned_height * mode_cmd->pitches[0])
14654 return -EINVAL;
14655
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014656 drm_helper_mode_fill_fb_struct(&intel_fb->base, mode_cmd);
14657 intel_fb->obj = obj;
Daniel Vetter80075d42013-10-09 21:23:52 +020014658 intel_fb->obj->framebuffer_references++;
Daniel Vetterc7d73f62012-12-13 23:38:38 +010014659
Jesse Barnes79e53942008-11-07 14:24:08 -080014660 ret = drm_framebuffer_init(dev, &intel_fb->base, &intel_fb_funcs);
14661 if (ret) {
14662 DRM_ERROR("framebuffer init failed %d\n", ret);
14663 return ret;
14664 }
14665
Jesse Barnes79e53942008-11-07 14:24:08 -080014666 return 0;
14667}
14668
Jesse Barnes79e53942008-11-07 14:24:08 -080014669static struct drm_framebuffer *
14670intel_user_framebuffer_create(struct drm_device *dev,
14671 struct drm_file *filp,
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014672 struct drm_mode_fb_cmd2 *mode_cmd)
Jesse Barnes79e53942008-11-07 14:24:08 -080014673{
Chris Wilson05394f32010-11-08 19:18:58 +000014674 struct drm_i915_gem_object *obj;
Jesse Barnes79e53942008-11-07 14:24:08 -080014675
Jesse Barnes308e5bc2011-11-14 14:51:28 -080014676 obj = to_intel_bo(drm_gem_object_lookup(dev, filp,
14677 mode_cmd->handles[0]));
Chris Wilsonc8725222011-02-19 11:31:06 +000014678 if (&obj->base == NULL)
Chris Wilsoncce13ff2010-08-08 13:36:38 +010014679 return ERR_PTR(-ENOENT);
Jesse Barnes79e53942008-11-07 14:24:08 -080014680
Chris Wilsond2dff872011-04-19 08:36:26 +010014681 return intel_framebuffer_create(dev, mode_cmd, obj);
Jesse Barnes79e53942008-11-07 14:24:08 -080014682}
14683
Daniel Vetter4520f532013-10-09 09:18:51 +020014684#ifndef CONFIG_DRM_I915_FBDEV
Daniel Vetter0632fef2013-10-08 17:44:49 +020014685static inline void intel_fbdev_output_poll_changed(struct drm_device *dev)
Daniel Vetter4520f532013-10-09 09:18:51 +020014686{
14687}
14688#endif
14689
Jesse Barnes79e53942008-11-07 14:24:08 -080014690static const struct drm_mode_config_funcs intel_mode_funcs = {
Jesse Barnes79e53942008-11-07 14:24:08 -080014691 .fb_create = intel_user_framebuffer_create,
Daniel Vetter0632fef2013-10-08 17:44:49 +020014692 .output_poll_changed = intel_fbdev_output_poll_changed,
Matt Roper5ee67f12015-01-21 16:35:44 -080014693 .atomic_check = intel_atomic_check,
14694 .atomic_commit = intel_atomic_commit,
Jesse Barnes79e53942008-11-07 14:24:08 -080014695};
14696
Jesse Barnese70236a2009-09-21 10:42:27 -070014697/* Set up chip specific display functions */
14698static void intel_init_display(struct drm_device *dev)
14699{
14700 struct drm_i915_private *dev_priv = dev->dev_private;
14701
Daniel Vetteree9300b2013-06-03 22:40:22 +020014702 if (HAS_PCH_SPLIT(dev) || IS_G4X(dev))
14703 dev_priv->display.find_dpll = g4x_find_best_dpll;
Chon Ming Leeef9348c2014-04-09 13:28:18 +030014704 else if (IS_CHERRYVIEW(dev))
14705 dev_priv->display.find_dpll = chv_find_best_dpll;
Daniel Vetteree9300b2013-06-03 22:40:22 +020014706 else if (IS_VALLEYVIEW(dev))
14707 dev_priv->display.find_dpll = vlv_find_best_dpll;
14708 else if (IS_PINEVIEW(dev))
14709 dev_priv->display.find_dpll = pnv_find_best_dpll;
14710 else
14711 dev_priv->display.find_dpll = i9xx_find_best_dpll;
14712
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014713 if (INTEL_INFO(dev)->gen >= 9) {
14714 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014715 dev_priv->display.get_initial_plane_config =
14716 skylake_get_initial_plane_config;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014717 dev_priv->display.crtc_compute_clock =
14718 haswell_crtc_compute_clock;
14719 dev_priv->display.crtc_enable = haswell_crtc_enable;
14720 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014721 dev_priv->display.update_primary_plane =
14722 skylake_update_primary_plane;
14723 } else if (HAS_DDI(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014724 dev_priv->display.get_pipe_config = haswell_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014725 dev_priv->display.get_initial_plane_config =
14726 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira797d0252014-10-29 11:32:34 +020014727 dev_priv->display.crtc_compute_clock =
14728 haswell_crtc_compute_clock;
Paulo Zanoni4f771f12012-10-23 18:29:51 -020014729 dev_priv->display.crtc_enable = haswell_crtc_enable;
14730 dev_priv->display.crtc_disable = haswell_crtc_disable;
Damien Lespiaubc8d7df2015-01-20 12:51:51 +000014731 dev_priv->display.update_primary_plane =
14732 ironlake_update_primary_plane;
Paulo Zanoni09b4ddf2012-10-05 12:05:55 -030014733 } else if (HAS_PCH_SPLIT(dev)) {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014734 dev_priv->display.get_pipe_config = ironlake_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014735 dev_priv->display.get_initial_plane_config =
14736 ironlake_get_initial_plane_config;
Ander Conselvan de Oliveira3fb37702014-10-29 11:32:35 +020014737 dev_priv->display.crtc_compute_clock =
14738 ironlake_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014739 dev_priv->display.crtc_enable = ironlake_crtc_enable;
14740 dev_priv->display.crtc_disable = ironlake_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014741 dev_priv->display.update_primary_plane =
14742 ironlake_update_primary_plane;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014743 } else if (IS_VALLEYVIEW(dev)) {
14744 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014745 dev_priv->display.get_initial_plane_config =
14746 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014747 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Jesse Barnes89b667f2013-04-18 14:51:36 -070014748 dev_priv->display.crtc_enable = valleyview_crtc_enable;
14749 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014750 dev_priv->display.update_primary_plane =
14751 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014752 } else {
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010014753 dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
Damien Lespiau5724dbd2015-01-20 12:51:52 +000014754 dev_priv->display.get_initial_plane_config =
14755 i9xx_get_initial_plane_config;
Ander Conselvan de Oliveirad6dfee72014-10-29 11:32:36 +020014756 dev_priv->display.crtc_compute_clock = i9xx_crtc_compute_clock;
Daniel Vetter76e5a892012-06-29 22:39:33 +020014757 dev_priv->display.crtc_enable = i9xx_crtc_enable;
14758 dev_priv->display.crtc_disable = i9xx_crtc_disable;
Matt Roper262ca2b2014-03-18 17:22:55 -070014759 dev_priv->display.update_primary_plane =
14760 i9xx_update_primary_plane;
Eric Anholtf564048e2011-03-30 13:01:02 -070014761 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014762
Jesse Barnese70236a2009-09-21 10:42:27 -070014763 /* Returns the core display clock speed */
Ville Syrjälä1652d192015-03-31 14:12:01 +030014764 if (IS_SKYLAKE(dev))
14765 dev_priv->display.get_display_clock_speed =
14766 skylake_get_display_clock_speed;
14767 else if (IS_BROADWELL(dev))
14768 dev_priv->display.get_display_clock_speed =
14769 broadwell_get_display_clock_speed;
14770 else if (IS_HASWELL(dev))
14771 dev_priv->display.get_display_clock_speed =
14772 haswell_get_display_clock_speed;
14773 else if (IS_VALLEYVIEW(dev))
Jesse Barnes25eb05fc2012-03-28 13:39:23 -070014774 dev_priv->display.get_display_clock_speed =
14775 valleyview_get_display_clock_speed;
Ville Syrjäläb37a6432015-03-31 14:11:54 +030014776 else if (IS_GEN5(dev))
14777 dev_priv->display.get_display_clock_speed =
14778 ilk_get_display_clock_speed;
Ville Syrjäläa7c66cd2015-03-31 14:11:56 +030014779 else if (IS_I945G(dev) || IS_BROADWATER(dev) ||
Ville Syrjälä34edce22015-05-22 11:22:33 +030014780 IS_GEN6(dev) || IS_IVYBRIDGE(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014781 dev_priv->display.get_display_clock_speed =
14782 i945_get_display_clock_speed;
Ville Syrjälä34edce22015-05-22 11:22:33 +030014783 else if (IS_GM45(dev))
14784 dev_priv->display.get_display_clock_speed =
14785 gm45_get_display_clock_speed;
14786 else if (IS_CRESTLINE(dev))
14787 dev_priv->display.get_display_clock_speed =
14788 i965gm_get_display_clock_speed;
14789 else if (IS_PINEVIEW(dev))
14790 dev_priv->display.get_display_clock_speed =
14791 pnv_get_display_clock_speed;
14792 else if (IS_G33(dev) || IS_G4X(dev))
14793 dev_priv->display.get_display_clock_speed =
14794 g33_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014795 else if (IS_I915G(dev))
14796 dev_priv->display.get_display_clock_speed =
14797 i915_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014798 else if (IS_I945GM(dev) || IS_845G(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014799 dev_priv->display.get_display_clock_speed =
14800 i9xx_misc_get_display_clock_speed;
Daniel Vetter257a7ff2013-07-26 08:35:42 +020014801 else if (IS_PINEVIEW(dev))
14802 dev_priv->display.get_display_clock_speed =
14803 pnv_get_display_clock_speed;
Jesse Barnese70236a2009-09-21 10:42:27 -070014804 else if (IS_I915GM(dev))
14805 dev_priv->display.get_display_clock_speed =
14806 i915gm_get_display_clock_speed;
14807 else if (IS_I865G(dev))
14808 dev_priv->display.get_display_clock_speed =
14809 i865_get_display_clock_speed;
Daniel Vetterf0f8a9c2009-09-15 22:57:33 +020014810 else if (IS_I85X(dev))
Jesse Barnese70236a2009-09-21 10:42:27 -070014811 dev_priv->display.get_display_clock_speed =
Ville Syrjälä1b1d2712015-05-22 11:22:31 +030014812 i85x_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014813 else { /* 830 */
14814 WARN(!IS_I830(dev), "Unknown platform. Assuming 133 MHz CDCLK\n");
Jesse Barnese70236a2009-09-21 10:42:27 -070014815 dev_priv->display.get_display_clock_speed =
14816 i830_get_display_clock_speed;
Ville Syrjälä623e01e2015-05-22 11:22:34 +030014817 }
Jesse Barnese70236a2009-09-21 10:42:27 -070014818
Jani Nikula7c10a2b2014-10-27 16:26:43 +020014819 if (IS_GEN5(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014820 dev_priv->display.fdi_link_train = ironlake_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014821 } else if (IS_GEN6(dev)) {
14822 dev_priv->display.fdi_link_train = gen6_fdi_link_train;
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014823 } else if (IS_IVYBRIDGE(dev)) {
14824 /* FIXME: detect B0+ stepping and use auto training */
14825 dev_priv->display.fdi_link_train = ivb_manual_fdi_link_train;
Paulo Zanoni059b2fe2014-09-02 16:53:57 -030014826 } else if (IS_HASWELL(dev) || IS_BROADWELL(dev)) {
Sonika Jindal3bb11b52014-08-11 09:06:39 +053014827 dev_priv->display.fdi_link_train = hsw_fdi_link_train;
Ville Syrjäläb432e5c2015-06-03 15:45:13 +030014828 if (IS_BROADWELL(dev))
14829 dev_priv->display.modeset_global_resources =
14830 broadwell_modeset_global_resources;
Jesse Barnes30a970c2013-11-04 13:48:12 -080014831 } else if (IS_VALLEYVIEW(dev)) {
14832 dev_priv->display.modeset_global_resources =
14833 valleyview_modeset_global_resources;
Vandana Kannanf8437dd12014-11-24 13:37:39 +053014834 } else if (IS_BROXTON(dev)) {
14835 dev_priv->display.modeset_global_resources =
14836 broxton_modeset_global_resources;
Jesse Barnese70236a2009-09-21 10:42:27 -070014837 }
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014838
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014839 switch (INTEL_INFO(dev)->gen) {
14840 case 2:
14841 dev_priv->display.queue_flip = intel_gen2_queue_flip;
14842 break;
14843
14844 case 3:
14845 dev_priv->display.queue_flip = intel_gen3_queue_flip;
14846 break;
14847
14848 case 4:
14849 case 5:
14850 dev_priv->display.queue_flip = intel_gen4_queue_flip;
14851 break;
14852
14853 case 6:
14854 dev_priv->display.queue_flip = intel_gen6_queue_flip;
14855 break;
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014856 case 7:
Ben Widawsky4e0bbc32013-11-02 21:07:07 -070014857 case 8: /* FIXME(BDW): Check that the gen8 RCS flip works. */
Jesse Barnes7c9017e2011-06-16 12:18:54 -070014858 dev_priv->display.queue_flip = intel_gen7_queue_flip;
14859 break;
Damien Lespiau830c81d2014-11-13 17:51:46 +000014860 case 9:
Tvrtko Ursulinba343e02015-02-10 17:16:12 +000014861 /* Drop through - unsupported since execlist only. */
14862 default:
14863 /* Default just returns -ENODEV to indicate unsupported */
14864 dev_priv->display.queue_flip = intel_default_queue_flip;
Jesse Barnes8c9f3aa2011-06-16 09:19:13 -070014865 }
Jani Nikula7bd688c2013-11-08 16:48:56 +020014866
14867 intel_panel_init_backlight_funcs(dev);
Ville Syrjäläe39b9992014-09-04 14:53:14 +030014868
14869 mutex_init(&dev_priv->pps_mutex);
Jesse Barnese70236a2009-09-21 10:42:27 -070014870}
14871
Jesse Barnesb690e962010-07-19 13:53:12 -070014872/*
14873 * Some BIOSes insist on assuming the GPU's pipe A is enabled at suspend,
14874 * resume, or other times. This quirk makes sure that's the case for
14875 * affected systems.
14876 */
Akshay Joshi0206e352011-08-16 15:34:10 -040014877static void quirk_pipea_force(struct drm_device *dev)
Jesse Barnesb690e962010-07-19 13:53:12 -070014878{
14879 struct drm_i915_private *dev_priv = dev->dev_private;
14880
14881 dev_priv->quirks |= QUIRK_PIPEA_FORCE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014882 DRM_INFO("applying pipe a force quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014883}
14884
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014885static void quirk_pipeb_force(struct drm_device *dev)
14886{
14887 struct drm_i915_private *dev_priv = dev->dev_private;
14888
14889 dev_priv->quirks |= QUIRK_PIPEB_FORCE;
14890 DRM_INFO("applying pipe b force quirk\n");
14891}
14892
Keith Packard435793d2011-07-12 14:56:22 -070014893/*
14894 * Some machines (Lenovo U160) do not work with SSC on LVDS for some reason
14895 */
14896static void quirk_ssc_force_disable(struct drm_device *dev)
14897{
14898 struct drm_i915_private *dev_priv = dev->dev_private;
14899 dev_priv->quirks |= QUIRK_LVDS_SSC_DISABLE;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014900 DRM_INFO("applying lvds SSC disable quirk\n");
Keith Packard435793d2011-07-12 14:56:22 -070014901}
14902
Carsten Emde4dca20e2012-03-15 15:56:26 +010014903/*
Carsten Emde5a15ab52012-03-15 15:56:27 +010014904 * A machine (e.g. Acer Aspire 5734Z) may need to invert the panel backlight
14905 * brightness value
Carsten Emde4dca20e2012-03-15 15:56:26 +010014906 */
14907static void quirk_invert_brightness(struct drm_device *dev)
14908{
14909 struct drm_i915_private *dev_priv = dev->dev_private;
14910 dev_priv->quirks |= QUIRK_INVERT_BRIGHTNESS;
Daniel Vetterbc0daf42012-04-01 13:16:49 +020014911 DRM_INFO("applying inverted panel brightness quirk\n");
Jesse Barnesb690e962010-07-19 13:53:12 -070014912}
14913
Scot Doyle9c72cc62014-07-03 23:27:50 +000014914/* Some VBT's incorrectly indicate no backlight is present */
14915static void quirk_backlight_present(struct drm_device *dev)
14916{
14917 struct drm_i915_private *dev_priv = dev->dev_private;
14918 dev_priv->quirks |= QUIRK_BACKLIGHT_PRESENT;
14919 DRM_INFO("applying backlight present quirk\n");
14920}
14921
Jesse Barnesb690e962010-07-19 13:53:12 -070014922struct intel_quirk {
14923 int device;
14924 int subsystem_vendor;
14925 int subsystem_device;
14926 void (*hook)(struct drm_device *dev);
14927};
14928
Egbert Eich5f85f172012-10-14 15:46:38 +020014929/* For systems that don't have a meaningful PCI subdevice/subvendor ID */
14930struct intel_dmi_quirk {
14931 void (*hook)(struct drm_device *dev);
14932 const struct dmi_system_id (*dmi_id_list)[];
14933};
14934
14935static int intel_dmi_reverse_brightness(const struct dmi_system_id *id)
14936{
14937 DRM_INFO("Backlight polarity reversed on %s\n", id->ident);
14938 return 1;
14939}
14940
14941static const struct intel_dmi_quirk intel_dmi_quirks[] = {
14942 {
14943 .dmi_id_list = &(const struct dmi_system_id[]) {
14944 {
14945 .callback = intel_dmi_reverse_brightness,
14946 .ident = "NCR Corporation",
14947 .matches = {DMI_MATCH(DMI_SYS_VENDOR, "NCR Corporation"),
14948 DMI_MATCH(DMI_PRODUCT_NAME, ""),
14949 },
14950 },
14951 { } /* terminating entry */
14952 },
14953 .hook = quirk_invert_brightness,
14954 },
14955};
14956
Ben Widawskyc43b5632012-04-16 14:07:40 -070014957static struct intel_quirk intel_quirks[] = {
Jesse Barnesb690e962010-07-19 13:53:12 -070014958 /* Toshiba Protege R-205, S-209 needs pipe A force quirk */
14959 { 0x2592, 0x1179, 0x0001, quirk_pipea_force },
14960
Jesse Barnesb690e962010-07-19 13:53:12 -070014961 /* ThinkPad T60 needs pipe A force quirk (bug #16494) */
14962 { 0x2782, 0x17aa, 0x201a, quirk_pipea_force },
14963
Ville Syrjälä5f080c02014-08-15 01:22:06 +030014964 /* 830 needs to leave pipe A & dpll A up */
14965 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipea_force },
14966
Ville Syrjäläb6b5d042014-08-15 01:22:07 +030014967 /* 830 needs to leave pipe B & dpll B up */
14968 { 0x3577, PCI_ANY_ID, PCI_ANY_ID, quirk_pipeb_force },
14969
Keith Packard435793d2011-07-12 14:56:22 -070014970 /* Lenovo U160 cannot use SSC on LVDS */
14971 { 0x0046, 0x17aa, 0x3920, quirk_ssc_force_disable },
Michel Alexandre Salim070d3292011-07-28 18:52:06 +020014972
14973 /* Sony Vaio Y cannot use SSC on LVDS */
14974 { 0x0046, 0x104d, 0x9076, quirk_ssc_force_disable },
Carsten Emde5a15ab52012-03-15 15:56:27 +010014975
Alexander van Heukelumbe505f62013-12-28 21:00:39 +010014976 /* Acer Aspire 5734Z must invert backlight brightness */
14977 { 0x2a42, 0x1025, 0x0459, quirk_invert_brightness },
14978
14979 /* Acer/eMachines G725 */
14980 { 0x2a42, 0x1025, 0x0210, quirk_invert_brightness },
14981
14982 /* Acer/eMachines e725 */
14983 { 0x2a42, 0x1025, 0x0212, quirk_invert_brightness },
14984
14985 /* Acer/Packard Bell NCL20 */
14986 { 0x2a42, 0x1025, 0x034b, quirk_invert_brightness },
14987
14988 /* Acer Aspire 4736Z */
14989 { 0x2a42, 0x1025, 0x0260, quirk_invert_brightness },
Jani Nikula0f540c32014-01-13 17:30:34 +020014990
14991 /* Acer Aspire 5336 */
14992 { 0x2a42, 0x1025, 0x048a, quirk_invert_brightness },
Scot Doyle2e93a1a2014-07-03 23:27:51 +000014993
14994 /* Acer C720 and C720P Chromebooks (Celeron 2955U) have backlights */
14995 { 0x0a06, 0x1025, 0x0a11, quirk_backlight_present },
Scot Doyled4967d82014-07-03 23:27:52 +000014996
Scot Doyledfb3d47b2014-08-21 16:08:02 +000014997 /* Acer C720 Chromebook (Core i3 4005U) */
14998 { 0x0a16, 0x1025, 0x0a11, quirk_backlight_present },
14999
jens steinb2a96012014-10-28 20:25:53 +010015000 /* Apple Macbook 2,1 (Core 2 T7400) */
15001 { 0x27a2, 0x8086, 0x7270, quirk_backlight_present },
15002
Scot Doyled4967d82014-07-03 23:27:52 +000015003 /* Toshiba CB35 Chromebook (Celeron 2955U) */
15004 { 0x0a06, 0x1179, 0x0a88, quirk_backlight_present },
Scot Doyle724cb062014-07-11 22:16:30 +000015005
15006 /* HP Chromebook 14 (Celeron 2955U) */
15007 { 0x0a06, 0x103c, 0x21ed, quirk_backlight_present },
Jani Nikulacf6f0af2015-02-19 10:53:39 +020015008
15009 /* Dell Chromebook 11 */
15010 { 0x0a06, 0x1028, 0x0a35, quirk_backlight_present },
Jesse Barnesb690e962010-07-19 13:53:12 -070015011};
15012
15013static void intel_init_quirks(struct drm_device *dev)
15014{
15015 struct pci_dev *d = dev->pdev;
15016 int i;
15017
15018 for (i = 0; i < ARRAY_SIZE(intel_quirks); i++) {
15019 struct intel_quirk *q = &intel_quirks[i];
15020
15021 if (d->device == q->device &&
15022 (d->subsystem_vendor == q->subsystem_vendor ||
15023 q->subsystem_vendor == PCI_ANY_ID) &&
15024 (d->subsystem_device == q->subsystem_device ||
15025 q->subsystem_device == PCI_ANY_ID))
15026 q->hook(dev);
15027 }
Egbert Eich5f85f172012-10-14 15:46:38 +020015028 for (i = 0; i < ARRAY_SIZE(intel_dmi_quirks); i++) {
15029 if (dmi_check_system(*intel_dmi_quirks[i].dmi_id_list) != 0)
15030 intel_dmi_quirks[i].hook(dev);
15031 }
Jesse Barnesb690e962010-07-19 13:53:12 -070015032}
15033
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015034/* Disable the VGA plane that we never use */
15035static void i915_disable_vga(struct drm_device *dev)
15036{
15037 struct drm_i915_private *dev_priv = dev->dev_private;
15038 u8 sr1;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015039 u32 vga_reg = i915_vgacntrl_reg(dev);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015040
Ville Syrjälä2b37c612014-01-22 21:32:38 +020015041 /* WaEnableVGAAccessThroughIOPort:ctg,elk,ilk,snb,ivb,vlv,hsw */
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015042 vga_get_uninterruptible(dev->pdev, VGA_RSRC_LEGACY_IO);
Jesse Barnes3fdcf432012-04-06 11:46:27 -070015043 outb(SR01, VGA_SR_INDEX);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015044 sr1 = inb(VGA_SR_DATA);
15045 outb(sr1 | 1<<5, VGA_SR_DATA);
15046 vga_put(dev->pdev, VGA_RSRC_LEGACY_IO);
15047 udelay(300);
15048
Ville Syrjälä01f5a622014-12-16 18:38:37 +020015049 I915_WRITE(vga_reg, VGA_DISP_DISABLE);
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015050 POSTING_READ(vga_reg);
15051}
15052
Daniel Vetterf8175862012-04-10 15:50:11 +020015053void intel_modeset_init_hw(struct drm_device *dev)
15054{
Ville Syrjäläb6283052015-06-03 15:45:07 +030015055 intel_update_cdclk(dev);
Eugeni Dodonova8f78b52012-06-28 15:55:35 -030015056 intel_prepare_ddi(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015057 intel_init_clock_gating(dev);
Daniel Vetter8090c6b2012-06-24 16:42:32 +020015058 intel_enable_gt_powersave(dev);
Daniel Vetterf8175862012-04-10 15:50:11 +020015059}
15060
Jesse Barnes79e53942008-11-07 14:24:08 -080015061void intel_modeset_init(struct drm_device *dev)
15062{
Jesse Barnes652c3932009-08-17 13:31:43 -070015063 struct drm_i915_private *dev_priv = dev->dev_private;
Damien Lespiau1fe47782014-03-03 17:31:47 +000015064 int sprite, ret;
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015065 enum pipe pipe;
Jesse Barnes46f297f2014-03-07 08:57:48 -080015066 struct intel_crtc *crtc;
Jesse Barnes79e53942008-11-07 14:24:08 -080015067
15068 drm_mode_config_init(dev);
15069
15070 dev->mode_config.min_width = 0;
15071 dev->mode_config.min_height = 0;
15072
Dave Airlie019d96c2011-09-29 16:20:42 +010015073 dev->mode_config.preferred_depth = 24;
15074 dev->mode_config.prefer_shadow = 1;
15075
Tvrtko Ursulin25bab382015-02-10 17:16:16 +000015076 dev->mode_config.allow_fb_modifiers = true;
15077
Laurent Pincharte6ecefa2012-05-17 13:27:23 +020015078 dev->mode_config.funcs = &intel_mode_funcs;
Jesse Barnes79e53942008-11-07 14:24:08 -080015079
Jesse Barnesb690e962010-07-19 13:53:12 -070015080 intel_init_quirks(dev);
15081
Eugeni Dodonov1fa61102012-04-18 15:29:26 -030015082 intel_init_pm(dev);
15083
Ben Widawskye3c74752013-04-05 13:12:39 -070015084 if (INTEL_INFO(dev)->num_pipes == 0)
15085 return;
15086
Jesse Barnese70236a2009-09-21 10:42:27 -070015087 intel_init_display(dev);
Jani Nikula7c10a2b2014-10-27 16:26:43 +020015088 intel_init_audio(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015089
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015090 if (IS_GEN2(dev)) {
15091 dev->mode_config.max_width = 2048;
15092 dev->mode_config.max_height = 2048;
15093 } else if (IS_GEN3(dev)) {
Keith Packard5e4d6fa2009-07-12 23:53:17 -070015094 dev->mode_config.max_width = 4096;
15095 dev->mode_config.max_height = 4096;
Jesse Barnes79e53942008-11-07 14:24:08 -080015096 } else {
Chris Wilsona6c45cf2010-09-17 00:32:17 +010015097 dev->mode_config.max_width = 8192;
15098 dev->mode_config.max_height = 8192;
Jesse Barnes79e53942008-11-07 14:24:08 -080015099 }
Damien Lespiau068be562014-03-28 14:17:49 +000015100
Ville Syrjälädc41c152014-08-13 11:57:05 +030015101 if (IS_845G(dev) || IS_I865G(dev)) {
15102 dev->mode_config.cursor_width = IS_845G(dev) ? 64 : 512;
15103 dev->mode_config.cursor_height = 1023;
15104 } else if (IS_GEN2(dev)) {
Damien Lespiau068be562014-03-28 14:17:49 +000015105 dev->mode_config.cursor_width = GEN2_CURSOR_WIDTH;
15106 dev->mode_config.cursor_height = GEN2_CURSOR_HEIGHT;
15107 } else {
15108 dev->mode_config.cursor_width = MAX_CURSOR_WIDTH;
15109 dev->mode_config.cursor_height = MAX_CURSOR_HEIGHT;
15110 }
15111
Ben Widawsky5d4545a2013-01-17 12:45:15 -080015112 dev->mode_config.fb_base = dev_priv->gtt.mappable_base;
Jesse Barnes79e53942008-11-07 14:24:08 -080015113
Zhao Yakui28c97732009-10-09 11:39:41 +080015114 DRM_DEBUG_KMS("%d display pipe%s available.\n",
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015115 INTEL_INFO(dev)->num_pipes,
15116 INTEL_INFO(dev)->num_pipes > 1 ? "s" : "");
Jesse Barnes79e53942008-11-07 14:24:08 -080015117
Damien Lespiau055e3932014-08-18 13:49:10 +010015118 for_each_pipe(dev_priv, pipe) {
Damien Lespiau8cc87b72014-03-03 17:31:44 +000015119 intel_crtc_init(dev, pipe);
Damien Lespiau3bdcfc02015-02-28 14:54:09 +000015120 for_each_sprite(dev_priv, pipe, sprite) {
Damien Lespiau1fe47782014-03-03 17:31:47 +000015121 ret = intel_plane_init(dev, pipe, sprite);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015122 if (ret)
Ville Syrjälä06da8da2013-04-17 17:48:51 +030015123 DRM_DEBUG_KMS("pipe %c sprite %c init failed: %d\n",
Damien Lespiau1fe47782014-03-03 17:31:47 +000015124 pipe_name(pipe), sprite_name(pipe, sprite), ret);
Jesse Barnes7f1f3852013-04-02 11:22:20 -070015125 }
Jesse Barnes79e53942008-11-07 14:24:08 -080015126 }
15127
Jesse Barnesf42bb702013-12-16 16:34:23 -080015128 intel_init_dpio(dev);
15129
Daniel Vettere72f9fb2013-06-05 13:34:06 +020015130 intel_shared_dpll_init(dev);
Jesse Barnesee7b9f92012-04-20 17:11:53 +010015131
Jesse Barnes9cce37f2010-08-13 15:11:26 -070015132 /* Just disable it once at startup */
15133 i915_disable_vga(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015134 intel_setup_outputs(dev);
Chris Wilson11be49e2012-11-15 11:32:20 +000015135
15136 /* Just in case the BIOS is doing something questionable. */
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015137 intel_fbc_disable(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015138
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015139 drm_modeset_lock_all(dev);
Jesse Barnesfa9fa082014-02-11 15:28:56 -080015140 intel_modeset_setup_hw_state(dev, false);
Daniel Vetter6e9f7982014-05-29 23:54:47 +020015141 drm_modeset_unlock_all(dev);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015142
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015143 for_each_intel_crtc(dev, crtc) {
Jesse Barnes46f297f2014-03-07 08:57:48 -080015144 if (!crtc->active)
15145 continue;
15146
Jesse Barnes46f297f2014-03-07 08:57:48 -080015147 /*
Jesse Barnes46f297f2014-03-07 08:57:48 -080015148 * Note that reserving the BIOS fb up front prevents us
15149 * from stuffing other stolen allocations like the ring
15150 * on top. This prevents some ugliness at boot time, and
15151 * can even allow for smooth boot transitions if the BIOS
15152 * fb is large enough for the active pipe configuration.
15153 */
Damien Lespiau5724dbd2015-01-20 12:51:52 +000015154 if (dev_priv->display.get_initial_plane_config) {
15155 dev_priv->display.get_initial_plane_config(crtc,
Jesse Barnes46f297f2014-03-07 08:57:48 -080015156 &crtc->plane_config);
15157 /*
15158 * If the fb is shared between multiple heads, we'll
15159 * just get the first one.
15160 */
Daniel Vetterf6936e22015-03-26 12:17:05 +010015161 intel_find_initial_plane_obj(crtc, &crtc->plane_config);
Jesse Barnes46f297f2014-03-07 08:57:48 -080015162 }
Jesse Barnes46f297f2014-03-07 08:57:48 -080015163 }
Chris Wilson2c7111d2011-03-29 10:40:27 +010015164}
Jesse Barnesd5bb0812011-01-05 12:01:26 -080015165
Daniel Vetter7fad7982012-07-04 17:51:47 +020015166static void intel_enable_pipe_a(struct drm_device *dev)
15167{
15168 struct intel_connector *connector;
15169 struct drm_connector *crt = NULL;
15170 struct intel_load_detect_pipe load_detect_temp;
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015171 struct drm_modeset_acquire_ctx *ctx = dev->mode_config.acquire_ctx;
Daniel Vetter7fad7982012-07-04 17:51:47 +020015172
15173 /* We can't just switch on the pipe A, we need to set things up with a
15174 * proper mode and output configuration. As a gross hack, enable pipe A
15175 * by enabling the load detect pipe once. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015176 for_each_intel_connector(dev, connector) {
Daniel Vetter7fad7982012-07-04 17:51:47 +020015177 if (connector->encoder->type == INTEL_OUTPUT_ANALOG) {
15178 crt = &connector->base;
15179 break;
15180 }
15181 }
15182
15183 if (!crt)
15184 return;
15185
Ville Syrjälä208bf9f2014-08-11 13:15:35 +030015186 if (intel_get_load_detect_pipe(crt, NULL, &load_detect_temp, ctx))
Ander Conselvan de Oliveira49172fe2015-03-20 16:18:02 +020015187 intel_release_load_detect_pipe(crt, &load_detect_temp, ctx);
Daniel Vetter7fad7982012-07-04 17:51:47 +020015188}
15189
Daniel Vetterfa555832012-10-10 23:14:00 +020015190static bool
15191intel_check_plane_mapping(struct intel_crtc *crtc)
15192{
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015193 struct drm_device *dev = crtc->base.dev;
15194 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015195 u32 reg, val;
15196
Ben Widawsky7eb552a2013-03-13 14:05:41 -070015197 if (INTEL_INFO(dev)->num_pipes == 1)
Daniel Vetterfa555832012-10-10 23:14:00 +020015198 return true;
15199
15200 reg = DSPCNTR(!crtc->plane);
15201 val = I915_READ(reg);
15202
15203 if ((val & DISPLAY_PLANE_ENABLE) &&
15204 (!!(val & DISPPLANE_SEL_PIPE_MASK) == crtc->pipe))
15205 return false;
15206
15207 return true;
15208}
15209
Daniel Vetter24929352012-07-02 20:28:59 +020015210static void intel_sanitize_crtc(struct intel_crtc *crtc)
15211{
15212 struct drm_device *dev = crtc->base.dev;
15213 struct drm_i915_private *dev_priv = dev->dev_private;
Daniel Vetterfa555832012-10-10 23:14:00 +020015214 u32 reg;
Daniel Vetter24929352012-07-02 20:28:59 +020015215
Daniel Vetter24929352012-07-02 20:28:59 +020015216 /* Clear any frame start delays used for debugging left by the BIOS */
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015217 reg = PIPECONF(crtc->config->cpu_transcoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015218 I915_WRITE(reg, I915_READ(reg) & ~PIPECONF_FRAME_START_DELAY_MASK);
15219
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015220 /* restore vblank interrupts to correct state */
Daniel Vetter96256042015-02-13 21:03:42 +010015221 drm_crtc_vblank_reset(&crtc->base);
Ville Syrjäläd297e102014-08-06 14:50:01 +030015222 if (crtc->active) {
15223 update_scanline_offset(crtc);
Daniel Vetter96256042015-02-13 21:03:42 +010015224 drm_crtc_vblank_on(&crtc->base);
15225 }
Ville Syrjäläd3eaf882014-05-20 17:20:05 +030015226
Daniel Vetter24929352012-07-02 20:28:59 +020015227 /* We need to sanitize the plane -> pipe mapping first because this will
Daniel Vetterfa555832012-10-10 23:14:00 +020015228 * disable the crtc (and hence change the state) if it is wrong. Note
15229 * that gen4+ has a fixed plane -> pipe mapping. */
15230 if (INTEL_INFO(dev)->gen < 4 && !intel_check_plane_mapping(crtc)) {
Daniel Vetter24929352012-07-02 20:28:59 +020015231 struct intel_connector *connector;
15232 bool plane;
15233
Daniel Vetter24929352012-07-02 20:28:59 +020015234 DRM_DEBUG_KMS("[CRTC:%d] wrong plane connection detected!\n",
15235 crtc->base.base.id);
15236
15237 /* Pipe has the wrong plane attached and the plane is active.
15238 * Temporarily change the plane mapping and disable everything
15239 * ... */
15240 plane = crtc->plane;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015241 to_intel_plane_state(crtc->base.primary->state)->visible = true;
Daniel Vetter24929352012-07-02 20:28:59 +020015242 crtc->plane = !plane;
Maarten Lankhorstce22dba2015-04-21 17:12:56 +030015243 intel_crtc_disable_planes(&crtc->base);
Daniel Vetter24929352012-07-02 20:28:59 +020015244 dev_priv->display.crtc_disable(&crtc->base);
15245 crtc->plane = plane;
15246
15247 /* ... and break all links. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015248 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015249 if (connector->encoder->base.crtc != &crtc->base)
15250 continue;
15251
Egbert Eich7f1950f2014-04-25 10:56:22 +020015252 connector->base.dpms = DRM_MODE_DPMS_OFF;
15253 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015254 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015255 /* multiple connectors may have the same encoder:
15256 * handle them and break crtc link separately */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015257 for_each_intel_connector(dev, connector)
Egbert Eich7f1950f2014-04-25 10:56:22 +020015258 if (connector->encoder->base.crtc == &crtc->base) {
15259 connector->encoder->base.crtc = NULL;
15260 connector->encoder->connectors_active = false;
15261 }
Daniel Vetter24929352012-07-02 20:28:59 +020015262
15263 WARN_ON(crtc->active);
Matt Roper83d65732015-02-25 13:12:16 -080015264 crtc->base.state->enable = false;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015265 crtc->base.state->active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015266 crtc->base.enabled = false;
15267 }
Daniel Vetter24929352012-07-02 20:28:59 +020015268
Daniel Vetter7fad7982012-07-04 17:51:47 +020015269 if (dev_priv->quirks & QUIRK_PIPEA_FORCE &&
15270 crtc->pipe == PIPE_A && !crtc->active) {
15271 /* BIOS forgot to enable pipe A, this mostly happens after
15272 * resume. Force-enable the pipe to fix this, the update_dpms
15273 * call below we restore the pipe to the right state, but leave
15274 * the required bits on. */
15275 intel_enable_pipe_a(dev);
15276 }
15277
Daniel Vetter24929352012-07-02 20:28:59 +020015278 /* Adjust the state of the output pipe according to whether we
15279 * have active connectors/encoders. */
15280 intel_crtc_update_dpms(&crtc->base);
15281
Matt Roper83d65732015-02-25 13:12:16 -080015282 if (crtc->active != crtc->base.state->enable) {
Daniel Vetter24929352012-07-02 20:28:59 +020015283 struct intel_encoder *encoder;
15284
15285 /* This can happen either due to bugs in the get_hw_state
15286 * functions or because the pipe is force-enabled due to the
15287 * pipe A quirk. */
15288 DRM_DEBUG_KMS("[CRTC:%d] hw state adjusted, was %s, now %s\n",
15289 crtc->base.base.id,
Matt Roper83d65732015-02-25 13:12:16 -080015290 crtc->base.state->enable ? "enabled" : "disabled",
Daniel Vetter24929352012-07-02 20:28:59 +020015291 crtc->active ? "enabled" : "disabled");
15292
Matt Roper83d65732015-02-25 13:12:16 -080015293 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015294 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015295 crtc->base.enabled = crtc->active;
15296
15297 /* Because we only establish the connector -> encoder ->
15298 * crtc links if something is active, this means the
15299 * crtc is now deactivated. Break the links. connector
15300 * -> encoder links are only establish when things are
15301 * actually up, hence no need to break them. */
15302 WARN_ON(crtc->active);
15303
15304 for_each_encoder_on_crtc(dev, &crtc->base, encoder) {
15305 WARN_ON(encoder->connectors_active);
15306 encoder->base.crtc = NULL;
15307 }
15308 }
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015309
Ville Syrjäläa3ed6aa2014-09-03 14:09:52 +030015310 if (crtc->active || HAS_GMCH_DISPLAY(dev)) {
Daniel Vetter4cc31482014-03-24 00:01:41 +010015311 /*
15312 * We start out with underrun reporting disabled to avoid races.
15313 * For correct bookkeeping mark this on active crtcs.
15314 *
Daniel Vetterc5ab3bc2014-05-14 15:40:34 +020015315 * Also on gmch platforms we dont have any hardware bits to
15316 * disable the underrun reporting. Which means we need to start
15317 * out with underrun reporting disabled also on inactive pipes,
15318 * since otherwise we'll complain about the garbage we read when
15319 * e.g. coming up after runtime pm.
15320 *
Daniel Vetter4cc31482014-03-24 00:01:41 +010015321 * No protection against concurrent access is required - at
15322 * worst a fifo underrun happens which also sets this to false.
15323 */
15324 crtc->cpu_fifo_underrun_disabled = true;
15325 crtc->pch_fifo_underrun_disabled = true;
15326 }
Daniel Vetter24929352012-07-02 20:28:59 +020015327}
15328
15329static void intel_sanitize_encoder(struct intel_encoder *encoder)
15330{
15331 struct intel_connector *connector;
15332 struct drm_device *dev = encoder->base.dev;
15333
15334 /* We need to check both for a crtc link (meaning that the
15335 * encoder is active and trying to read from a pipe) and the
15336 * pipe itself being active. */
15337 bool has_active_crtc = encoder->base.crtc &&
15338 to_intel_crtc(encoder->base.crtc)->active;
15339
15340 if (encoder->connectors_active && !has_active_crtc) {
15341 DRM_DEBUG_KMS("[ENCODER:%d:%s] has active connectors but no active pipe!\n",
15342 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015343 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015344
15345 /* Connector is active, but has no active pipe. This is
15346 * fallout from our resume register restoring. Disable
15347 * the encoder manually again. */
15348 if (encoder->base.crtc) {
15349 DRM_DEBUG_KMS("[ENCODER:%d:%s] manually disabled\n",
15350 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015351 encoder->base.name);
Daniel Vetter24929352012-07-02 20:28:59 +020015352 encoder->disable(encoder);
Ville Syrjäläa62d1492014-06-28 02:04:01 +030015353 if (encoder->post_disable)
15354 encoder->post_disable(encoder);
Daniel Vetter24929352012-07-02 20:28:59 +020015355 }
Egbert Eich7f1950f2014-04-25 10:56:22 +020015356 encoder->base.crtc = NULL;
15357 encoder->connectors_active = false;
Daniel Vetter24929352012-07-02 20:28:59 +020015358
15359 /* Inconsistent output/port/pipe state happens presumably due to
15360 * a bug in one of the get_hw_state functions. Or someplace else
15361 * in our code, like the register restore mess on resume. Clamp
15362 * things to off as a safer default. */
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015363 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015364 if (connector->encoder != encoder)
15365 continue;
Egbert Eich7f1950f2014-04-25 10:56:22 +020015366 connector->base.dpms = DRM_MODE_DPMS_OFF;
15367 connector->base.encoder = NULL;
Daniel Vetter24929352012-07-02 20:28:59 +020015368 }
15369 }
15370 /* Enabled encoders without active connectors will be fixed in
15371 * the crtc fixup. */
15372}
15373
Imre Deak04098752014-02-18 00:02:16 +020015374void i915_redisable_vga_power_on(struct drm_device *dev)
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015375{
15376 struct drm_i915_private *dev_priv = dev->dev_private;
Ville Syrjälä766aa1c2013-01-25 21:44:46 +020015377 u32 vga_reg = i915_vgacntrl_reg(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015378
Imre Deak04098752014-02-18 00:02:16 +020015379 if (!(I915_READ(vga_reg) & VGA_DISP_DISABLE)) {
15380 DRM_DEBUG_KMS("Something enabled VGA plane, disabling it\n");
15381 i915_disable_vga(dev);
15382 }
15383}
15384
15385void i915_redisable_vga(struct drm_device *dev)
15386{
15387 struct drm_i915_private *dev_priv = dev->dev_private;
15388
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015389 /* This function can be called both from intel_modeset_setup_hw_state or
15390 * at a very early point in our resume sequence, where the power well
15391 * structures are not yet restored. Since this function is at a very
15392 * paranoid "someone might have enabled VGA while we were not looking"
15393 * level, just check if the power well is enabled instead of trying to
15394 * follow the "don't touch the power well if we don't need it" policy
15395 * the rest of the driver uses. */
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015396 if (!intel_display_power_is_enabled(dev_priv, POWER_DOMAIN_VGA))
Paulo Zanoni8dc8a272013-08-02 16:22:24 -030015397 return;
15398
Imre Deak04098752014-02-18 00:02:16 +020015399 i915_redisable_vga_power_on(dev);
Krzysztof Mazur0fde9012012-12-19 11:03:41 +010015400}
15401
Ville Syrjälä98ec7732014-04-30 17:43:01 +030015402static bool primary_get_hw_state(struct intel_crtc *crtc)
15403{
15404 struct drm_i915_private *dev_priv = crtc->base.dev->dev_private;
15405
15406 if (!crtc->active)
15407 return false;
15408
15409 return I915_READ(DSPCNTR(crtc->plane)) & DISPLAY_PLANE_ENABLE;
15410}
15411
Daniel Vetter30e984d2013-06-05 13:34:17 +020015412static void intel_modeset_readout_hw_state(struct drm_device *dev)
Daniel Vetter24929352012-07-02 20:28:59 +020015413{
15414 struct drm_i915_private *dev_priv = dev->dev_private;
15415 enum pipe pipe;
Daniel Vetter24929352012-07-02 20:28:59 +020015416 struct intel_crtc *crtc;
15417 struct intel_encoder *encoder;
15418 struct intel_connector *connector;
Daniel Vetter53589012013-06-05 13:34:16 +020015419 int i;
Daniel Vetter24929352012-07-02 20:28:59 +020015420
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015421 for_each_intel_crtc(dev, crtc) {
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015422 struct drm_plane *primary = crtc->base.primary;
15423 struct intel_plane_state *plane_state;
15424
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015425 memset(crtc->config, 0, sizeof(*crtc->config));
Daniel Vetter3b117c82013-04-17 20:15:07 +020015426
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015427 crtc->config->quirks |= PIPE_CONFIG_QUIRK_INHERITED_MODE;
Daniel Vetter99535992014-04-13 12:00:33 +020015428
Daniel Vetter0e8ffe12013-03-28 10:42:00 +010015429 crtc->active = dev_priv->display.get_pipe_config(crtc,
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015430 crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015431
Matt Roper83d65732015-02-25 13:12:16 -080015432 crtc->base.state->enable = crtc->active;
Maarten Lankhorst49d6fa22015-05-11 10:45:15 +020015433 crtc->base.state->active = crtc->active;
Daniel Vetter24929352012-07-02 20:28:59 +020015434 crtc->base.enabled = crtc->active;
Maarten Lankhorstb70709a2015-04-21 17:12:53 +030015435
15436 plane_state = to_intel_plane_state(primary->state);
15437 plane_state->visible = primary_get_hw_state(crtc);
Daniel Vetter24929352012-07-02 20:28:59 +020015438
15439 DRM_DEBUG_KMS("[CRTC:%d] hw state readout: %s\n",
15440 crtc->base.base.id,
15441 crtc->active ? "enabled" : "disabled");
15442 }
15443
Daniel Vetter53589012013-06-05 13:34:16 +020015444 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15445 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15446
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015447 pll->on = pll->get_hw_state(dev_priv, pll,
15448 &pll->config.hw_state);
Daniel Vetter53589012013-06-05 13:34:16 +020015449 pll->active = 0;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015450 pll->config.crtc_mask = 0;
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015451 for_each_intel_crtc(dev, crtc) {
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015452 if (crtc->active && intel_crtc_to_shared_dpll(crtc) == pll) {
Daniel Vetter53589012013-06-05 13:34:16 +020015453 pll->active++;
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015454 pll->config.crtc_mask |= 1 << crtc->pipe;
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015455 }
Daniel Vetter53589012013-06-05 13:34:16 +020015456 }
Daniel Vetter53589012013-06-05 13:34:16 +020015457
Ander Conselvan de Oliveira1e6f2dd2014-10-29 11:32:31 +020015458 DRM_DEBUG_KMS("%s hw state readout: crtc_mask 0x%08x, on %i\n",
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015459 pll->name, pll->config.crtc_mask, pll->on);
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015460
Ander Conselvan de Oliveira3e369b72014-10-29 11:32:32 +020015461 if (pll->config.crtc_mask)
Paulo Zanonibd2bb1b2014-07-04 11:27:38 -030015462 intel_display_power_get(dev_priv, POWER_DOMAIN_PLLS);
Daniel Vetter53589012013-06-05 13:34:16 +020015463 }
15464
Damien Lespiaub2784e12014-08-05 11:29:37 +010015465 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015466 pipe = 0;
15467
15468 if (encoder->get_hw_state(encoder, &pipe)) {
Jesse Barnes045ac3b2013-05-14 17:08:26 -070015469 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15470 encoder->base.crtc = &crtc->base;
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015471 encoder->get_config(encoder, crtc->config);
Daniel Vetter24929352012-07-02 20:28:59 +020015472 } else {
15473 encoder->base.crtc = NULL;
15474 }
15475
15476 encoder->connectors_active = false;
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015477 DRM_DEBUG_KMS("[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
Daniel Vetter24929352012-07-02 20:28:59 +020015478 encoder->base.base.id,
Jani Nikula8e329a032014-06-03 14:56:21 +030015479 encoder->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015480 encoder->base.crtc ? "enabled" : "disabled",
Damien Lespiau6f2bcce2013-10-16 12:29:54 +010015481 pipe_name(pipe));
Daniel Vetter24929352012-07-02 20:28:59 +020015482 }
15483
Ander Conselvan de Oliveira3a3371f2015-03-03 15:21:56 +020015484 for_each_intel_connector(dev, connector) {
Daniel Vetter24929352012-07-02 20:28:59 +020015485 if (connector->get_hw_state(connector)) {
15486 connector->base.dpms = DRM_MODE_DPMS_ON;
15487 connector->encoder->connectors_active = true;
15488 connector->base.encoder = &connector->encoder->base;
15489 } else {
15490 connector->base.dpms = DRM_MODE_DPMS_OFF;
15491 connector->base.encoder = NULL;
15492 }
15493 DRM_DEBUG_KMS("[CONNECTOR:%d:%s] hw state readout: %s\n",
15494 connector->base.base.id,
Jani Nikulac23cc412014-06-03 14:56:17 +030015495 connector->base.name,
Daniel Vetter24929352012-07-02 20:28:59 +020015496 connector->base.encoder ? "enabled" : "disabled");
15497 }
Daniel Vetter30e984d2013-06-05 13:34:17 +020015498}
15499
15500/* Scan out the current hw modeset state, sanitizes it and maps it into the drm
15501 * and i915 state tracking structures. */
15502void intel_modeset_setup_hw_state(struct drm_device *dev,
15503 bool force_restore)
15504{
15505 struct drm_i915_private *dev_priv = dev->dev_private;
15506 enum pipe pipe;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015507 struct intel_crtc *crtc;
15508 struct intel_encoder *encoder;
Daniel Vetter35c95372013-07-17 06:55:04 +020015509 int i;
Daniel Vetter30e984d2013-06-05 13:34:17 +020015510
15511 intel_modeset_readout_hw_state(dev);
Daniel Vetter24929352012-07-02 20:28:59 +020015512
Jesse Barnesbabea612013-06-26 18:57:38 +030015513 /*
15514 * Now that we have the config, copy it to each CRTC struct
15515 * Note that this could go away if we move to using crtc_config
15516 * checking everywhere.
15517 */
Damien Lespiaud3fcc802014-05-13 23:32:22 +010015518 for_each_intel_crtc(dev, crtc) {
Jani Nikulad330a952014-01-21 11:24:25 +020015519 if (crtc->active && i915.fastboot) {
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015520 intel_mode_from_pipe_config(&crtc->base.mode,
15521 crtc->config);
Jesse Barnesbabea612013-06-26 18:57:38 +030015522 DRM_DEBUG_KMS("[CRTC:%d] found active mode: ",
15523 crtc->base.base.id);
15524 drm_mode_debug_printmodeline(&crtc->base.mode);
15525 }
15526 }
15527
Daniel Vetter24929352012-07-02 20:28:59 +020015528 /* HW state is read out, now we need to sanitize this mess. */
Damien Lespiaub2784e12014-08-05 11:29:37 +010015529 for_each_intel_encoder(dev, encoder) {
Daniel Vetter24929352012-07-02 20:28:59 +020015530 intel_sanitize_encoder(encoder);
15531 }
15532
Damien Lespiau055e3932014-08-18 13:49:10 +010015533 for_each_pipe(dev_priv, pipe) {
Daniel Vetter24929352012-07-02 20:28:59 +020015534 crtc = to_intel_crtc(dev_priv->pipe_to_crtc_mapping[pipe]);
15535 intel_sanitize_crtc(crtc);
Ander Conselvan de Oliveira6e3c9712015-01-15 14:55:25 +020015536 intel_dump_pipe_config(crtc, crtc->config,
15537 "[setup_hw_state]");
Daniel Vetter24929352012-07-02 20:28:59 +020015538 }
Daniel Vetter9a935852012-07-05 22:34:27 +020015539
Ander Conselvan de Oliveirad29b2f92015-03-20 16:18:05 +020015540 intel_modeset_update_connector_atomic_state(dev);
15541
Daniel Vetter35c95372013-07-17 06:55:04 +020015542 for (i = 0; i < dev_priv->num_shared_dpll; i++) {
15543 struct intel_shared_dpll *pll = &dev_priv->shared_dplls[i];
15544
15545 if (!pll->on || pll->active)
15546 continue;
15547
15548 DRM_DEBUG_KMS("%s enabled but not in use, disabling\n", pll->name);
15549
15550 pll->disable(dev_priv, pll);
15551 pll->on = false;
15552 }
15553
Pradeep Bhat30789992014-11-04 17:06:45 +000015554 if (IS_GEN9(dev))
15555 skl_wm_get_hw_state(dev);
15556 else if (HAS_PCH_SPLIT(dev))
Ville Syrjälä243e6a42013-10-14 14:55:24 +030015557 ilk_wm_get_hw_state(dev);
15558
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015559 if (force_restore) {
Ville Syrjälä7d0bc1e2013-09-16 17:38:33 +030015560 i915_redisable_vga(dev);
15561
Daniel Vetterf30da182013-04-11 20:22:50 +020015562 /*
15563 * We need to use raw interfaces for restoring state to avoid
15564 * checking (bogus) intermediate states.
15565 */
Damien Lespiau055e3932014-08-18 13:49:10 +010015566 for_each_pipe(dev_priv, pipe) {
Jesse Barnesb5644d02013-03-26 13:25:27 -070015567 struct drm_crtc *crtc =
15568 dev_priv->pipe_to_crtc_mapping[pipe];
Daniel Vetterf30da182013-04-11 20:22:50 +020015569
Ander Conselvan de Oliveira83a57152015-03-20 16:18:03 +020015570 intel_crtc_restore_mode(crtc);
Daniel Vetter45e2b5f2012-11-23 18:16:34 +010015571 }
15572 } else {
15573 intel_modeset_update_staged_output_state(dev);
15574 }
Daniel Vetter8af6cf82012-07-10 09:50:11 +020015575
15576 intel_modeset_check_state(dev);
Chris Wilson2c7111d2011-03-29 10:40:27 +010015577}
15578
15579void intel_modeset_gem_init(struct drm_device *dev)
15580{
Jesse Barnes92122782014-10-09 12:57:42 -070015581 struct drm_i915_private *dev_priv = dev->dev_private;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015582 struct drm_crtc *c;
Matt Roper2ff8fde2014-07-08 07:50:07 -070015583 struct drm_i915_gem_object *obj;
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015584 int ret;
Jesse Barnes484b41d2014-03-07 08:57:55 -080015585
Imre Deakae484342014-03-31 15:10:44 +030015586 mutex_lock(&dev->struct_mutex);
15587 intel_init_gt_powersave(dev);
15588 mutex_unlock(&dev->struct_mutex);
15589
Jesse Barnes92122782014-10-09 12:57:42 -070015590 /*
15591 * There may be no VBT; and if the BIOS enabled SSC we can
15592 * just keep using it to avoid unnecessary flicker. Whereas if the
15593 * BIOS isn't using it, don't assume it will work even if the VBT
15594 * indicates as much.
15595 */
15596 if (HAS_PCH_IBX(dev) || HAS_PCH_CPT(dev))
15597 dev_priv->vbt.lvds_use_ssc = !!(I915_READ(PCH_DREF_CONTROL) &
15598 DREF_SSC1_ENABLE);
15599
Chris Wilson1833b132012-05-09 11:56:28 +010015600 intel_modeset_init_hw(dev);
Daniel Vetter02e792f2009-09-15 22:57:34 +020015601
15602 intel_setup_overlay(dev);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015603
15604 /*
15605 * Make sure any fbs we allocated at startup are properly
15606 * pinned & fenced. When we do the allocation it's too early
15607 * for this.
15608 */
Damien Lespiau70e1e0e2014-05-13 23:32:24 +010015609 for_each_crtc(dev, c) {
Matt Roper2ff8fde2014-07-08 07:50:07 -070015610 obj = intel_fb_obj(c->primary->fb);
15611 if (obj == NULL)
Jesse Barnes484b41d2014-03-07 08:57:55 -080015612 continue;
15613
Tvrtko Ursuline0d61492015-04-13 16:03:03 +010015614 mutex_lock(&dev->struct_mutex);
15615 ret = intel_pin_and_fence_fb_obj(c->primary,
15616 c->primary->fb,
15617 c->primary->state,
15618 NULL);
15619 mutex_unlock(&dev->struct_mutex);
15620 if (ret) {
Jesse Barnes484b41d2014-03-07 08:57:55 -080015621 DRM_ERROR("failed to pin boot fb on pipe %d\n",
15622 to_intel_crtc(c)->pipe);
Dave Airlie66e514c2014-04-03 07:51:54 +100015623 drm_framebuffer_unreference(c->primary->fb);
15624 c->primary->fb = NULL;
Matt Roperafd65eb2015-02-03 13:10:04 -080015625 update_state_fb(c->primary);
Jesse Barnes484b41d2014-03-07 08:57:55 -080015626 }
15627 }
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015628
15629 intel_backlight_register(dev);
Jesse Barnes79e53942008-11-07 14:24:08 -080015630}
15631
Imre Deak4932e2c2014-02-11 17:12:48 +020015632void intel_connector_unregister(struct intel_connector *intel_connector)
15633{
15634 struct drm_connector *connector = &intel_connector->base;
15635
15636 intel_panel_destroy_backlight(connector);
Thomas Wood34ea3d32014-05-29 16:57:41 +010015637 drm_connector_unregister(connector);
Imre Deak4932e2c2014-02-11 17:12:48 +020015638}
15639
Jesse Barnes79e53942008-11-07 14:24:08 -080015640void intel_modeset_cleanup(struct drm_device *dev)
15641{
Jesse Barnes652c3932009-08-17 13:31:43 -070015642 struct drm_i915_private *dev_priv = dev->dev_private;
Paulo Zanonid9255d52013-09-26 20:05:59 -030015643 struct drm_connector *connector;
Jesse Barnes652c3932009-08-17 13:31:43 -070015644
Imre Deak2eb52522014-11-19 15:30:05 +020015645 intel_disable_gt_powersave(dev);
15646
Ville Syrjälä0962c3c2014-11-07 15:19:46 +020015647 intel_backlight_unregister(dev);
15648
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015649 /*
15650 * Interrupts and polling as the first thing to avoid creating havoc.
Imre Deak2eb52522014-11-19 15:30:05 +020015651 * Too much stuff here (turning of connectors, ...) would
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015652 * experience fancy races otherwise.
15653 */
Daniel Vetter2aeb7d32014-09-30 10:56:43 +020015654 intel_irq_uninstall(dev_priv);
Jesse Barneseb21b922014-06-20 11:57:33 -070015655
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015656 /*
15657 * Due to the hpd irq storm handling the hotplug work can re-arm the
15658 * poll handlers. Hence disable polling after hpd handling is shut down.
15659 */
Keith Packardf87ea762010-10-03 19:36:26 -070015660 drm_kms_helper_poll_fini(dev);
Daniel Vetterfd0c0642013-04-24 11:13:35 +020015661
Jesse Barnes652c3932009-08-17 13:31:43 -070015662 mutex_lock(&dev->struct_mutex);
15663
Jesse Barnes723bfd72010-10-07 16:01:13 -070015664 intel_unregister_dsm_handler();
15665
Rodrigo Vivi7ff0ebc2014-12-08 14:09:10 -020015666 intel_fbc_disable(dev);
Jesse Barnese70236a2009-09-21 10:42:27 -070015667
Kristian Høgsberg69341a52009-11-11 12:19:17 -050015668 mutex_unlock(&dev->struct_mutex);
15669
Chris Wilson1630fe72011-07-08 12:22:42 +010015670 /* flush any delayed tasks or pending work */
15671 flush_scheduled_work();
15672
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015673 /* destroy the backlight and sysfs files before encoders/connectors */
15674 list_for_each_entry(connector, &dev->mode_config.connector_list, head) {
Imre Deak4932e2c2014-02-11 17:12:48 +020015675 struct intel_connector *intel_connector;
15676
15677 intel_connector = to_intel_connector(connector);
15678 intel_connector->unregister(intel_connector);
Jani Nikuladb31af1d2013-11-08 16:48:53 +020015679 }
Paulo Zanonid9255d52013-09-26 20:05:59 -030015680
Jesse Barnes79e53942008-11-07 14:24:08 -080015681 drm_mode_config_cleanup(dev);
Daniel Vetter4d7bb012012-12-18 15:24:37 +010015682
15683 intel_cleanup_overlay(dev);
Imre Deakae484342014-03-31 15:10:44 +030015684
15685 mutex_lock(&dev->struct_mutex);
15686 intel_cleanup_gt_powersave(dev);
15687 mutex_unlock(&dev->struct_mutex);
Jesse Barnes79e53942008-11-07 14:24:08 -080015688}
15689
Dave Airlie28d52042009-09-21 14:33:58 +100015690/*
Zhenyu Wangf1c79df2010-03-30 14:39:29 +080015691 * Return which encoder is currently attached for connector.
15692 */
Chris Wilsondf0e9242010-09-09 16:20:55 +010015693struct drm_encoder *intel_best_encoder(struct drm_connector *connector)
Jesse Barnes79e53942008-11-07 14:24:08 -080015694{
Chris Wilsondf0e9242010-09-09 16:20:55 +010015695 return &intel_attached_encoder(connector)->base;
15696}
Jesse Barnes79e53942008-11-07 14:24:08 -080015697
Chris Wilsondf0e9242010-09-09 16:20:55 +010015698void intel_connector_attach_encoder(struct intel_connector *connector,
15699 struct intel_encoder *encoder)
15700{
15701 connector->encoder = encoder;
15702 drm_mode_connector_attach_encoder(&connector->base,
15703 &encoder->base);
Jesse Barnes79e53942008-11-07 14:24:08 -080015704}
Dave Airlie28d52042009-09-21 14:33:58 +100015705
15706/*
15707 * set vga decode state - true == enable VGA decode
15708 */
15709int intel_modeset_vga_set_state(struct drm_device *dev, bool state)
15710{
15711 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsona885b3c2013-12-17 14:34:50 +000015712 unsigned reg = INTEL_INFO(dev)->gen >= 6 ? SNB_GMCH_CTRL : INTEL_GMCH_CTRL;
Dave Airlie28d52042009-09-21 14:33:58 +100015713 u16 gmch_ctrl;
15714
Chris Wilson75fa0412014-02-07 18:37:02 -020015715 if (pci_read_config_word(dev_priv->bridge_dev, reg, &gmch_ctrl)) {
15716 DRM_ERROR("failed to read control word\n");
15717 return -EIO;
15718 }
15719
Chris Wilsonc0cc8a52014-02-07 18:37:03 -020015720 if (!!(gmch_ctrl & INTEL_GMCH_VGA_DISABLE) == !state)
15721 return 0;
15722
Dave Airlie28d52042009-09-21 14:33:58 +100015723 if (state)
15724 gmch_ctrl &= ~INTEL_GMCH_VGA_DISABLE;
15725 else
15726 gmch_ctrl |= INTEL_GMCH_VGA_DISABLE;
Chris Wilson75fa0412014-02-07 18:37:02 -020015727
15728 if (pci_write_config_word(dev_priv->bridge_dev, reg, gmch_ctrl)) {
15729 DRM_ERROR("failed to write control word\n");
15730 return -EIO;
15731 }
15732
Dave Airlie28d52042009-09-21 14:33:58 +100015733 return 0;
15734}
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015735
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015736struct intel_display_error_state {
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015737
15738 u32 power_well_driver;
15739
Chris Wilson63b66e52013-08-08 15:12:06 +020015740 int num_transcoders;
15741
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015742 struct intel_cursor_error_state {
15743 u32 control;
15744 u32 position;
15745 u32 base;
15746 u32 size;
Damien Lespiau52331302012-08-15 19:23:25 +010015747 } cursor[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015748
15749 struct intel_pipe_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015750 bool power_domain_on;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015751 u32 source;
Imre Deakf301b1e12014-04-18 15:55:04 +030015752 u32 stat;
Damien Lespiau52331302012-08-15 19:23:25 +010015753 } pipe[I915_MAX_PIPES];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015754
15755 struct intel_plane_error_state {
15756 u32 control;
15757 u32 stride;
15758 u32 size;
15759 u32 pos;
15760 u32 addr;
15761 u32 surface;
15762 u32 tile_offset;
Damien Lespiau52331302012-08-15 19:23:25 +010015763 } plane[I915_MAX_PIPES];
Chris Wilson63b66e52013-08-08 15:12:06 +020015764
15765 struct intel_transcoder_error_state {
Imre Deakddf9c532013-11-27 22:02:02 +020015766 bool power_domain_on;
Chris Wilson63b66e52013-08-08 15:12:06 +020015767 enum transcoder cpu_transcoder;
15768
15769 u32 conf;
15770
15771 u32 htotal;
15772 u32 hblank;
15773 u32 hsync;
15774 u32 vtotal;
15775 u32 vblank;
15776 u32 vsync;
15777 } transcoder[4];
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015778};
15779
15780struct intel_display_error_state *
15781intel_display_capture_error_state(struct drm_device *dev)
15782{
Jani Nikulafbee40d2014-03-31 14:27:18 +030015783 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015784 struct intel_display_error_state *error;
Chris Wilson63b66e52013-08-08 15:12:06 +020015785 int transcoders[] = {
15786 TRANSCODER_A,
15787 TRANSCODER_B,
15788 TRANSCODER_C,
15789 TRANSCODER_EDP,
15790 };
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015791 int i;
15792
Chris Wilson63b66e52013-08-08 15:12:06 +020015793 if (INTEL_INFO(dev)->num_pipes == 0)
15794 return NULL;
15795
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015796 error = kzalloc(sizeof(*error), GFP_ATOMIC);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015797 if (error == NULL)
15798 return NULL;
15799
Imre Deak190be112013-11-25 17:15:31 +020015800 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015801 error->power_well_driver = I915_READ(HSW_PWR_WELL_DRIVER);
15802
Damien Lespiau055e3932014-08-18 13:49:10 +010015803 for_each_pipe(dev_priv, i) {
Imre Deakddf9c532013-11-27 22:02:02 +020015804 error->pipe[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015805 __intel_display_power_is_enabled(dev_priv,
15806 POWER_DOMAIN_PIPE(i));
Imre Deakddf9c532013-11-27 22:02:02 +020015807 if (!error->pipe[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015808 continue;
15809
Ville Syrjälä5efb3e22014-04-09 13:28:53 +030015810 error->cursor[i].control = I915_READ(CURCNTR(i));
15811 error->cursor[i].position = I915_READ(CURPOS(i));
15812 error->cursor[i].base = I915_READ(CURBASE(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015813
15814 error->plane[i].control = I915_READ(DSPCNTR(i));
15815 error->plane[i].stride = I915_READ(DSPSTRIDE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015816 if (INTEL_INFO(dev)->gen <= 3) {
Paulo Zanoni51889b32013-03-06 20:03:13 -030015817 error->plane[i].size = I915_READ(DSPSIZE(i));
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015818 error->plane[i].pos = I915_READ(DSPPOS(i));
15819 }
Paulo Zanonica291362013-03-06 20:03:14 -030015820 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
15821 error->plane[i].addr = I915_READ(DSPADDR(i));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015822 if (INTEL_INFO(dev)->gen >= 4) {
15823 error->plane[i].surface = I915_READ(DSPSURF(i));
15824 error->plane[i].tile_offset = I915_READ(DSPTILEOFF(i));
15825 }
15826
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015827 error->pipe[i].source = I915_READ(PIPESRC(i));
Imre Deakf301b1e12014-04-18 15:55:04 +030015828
Sonika Jindal3abfce72014-07-21 15:23:43 +053015829 if (HAS_GMCH_DISPLAY(dev))
Imre Deakf301b1e12014-04-18 15:55:04 +030015830 error->pipe[i].stat = I915_READ(PIPESTAT(i));
Chris Wilson63b66e52013-08-08 15:12:06 +020015831 }
15832
15833 error->num_transcoders = INTEL_INFO(dev)->num_pipes;
15834 if (HAS_DDI(dev_priv->dev))
15835 error->num_transcoders++; /* Account for eDP. */
15836
15837 for (i = 0; i < error->num_transcoders; i++) {
15838 enum transcoder cpu_transcoder = transcoders[i];
15839
Imre Deakddf9c532013-11-27 22:02:02 +020015840 error->transcoder[i].power_domain_on =
Daniel Vetterf458ebb2014-09-30 10:56:39 +020015841 __intel_display_power_is_enabled(dev_priv,
Paulo Zanoni38cc1da2013-12-20 15:09:41 -020015842 POWER_DOMAIN_TRANSCODER(cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015843 if (!error->transcoder[i].power_domain_on)
Paulo Zanoni9d1cb912013-11-01 13:32:08 -020015844 continue;
15845
Chris Wilson63b66e52013-08-08 15:12:06 +020015846 error->transcoder[i].cpu_transcoder = cpu_transcoder;
15847
15848 error->transcoder[i].conf = I915_READ(PIPECONF(cpu_transcoder));
15849 error->transcoder[i].htotal = I915_READ(HTOTAL(cpu_transcoder));
15850 error->transcoder[i].hblank = I915_READ(HBLANK(cpu_transcoder));
15851 error->transcoder[i].hsync = I915_READ(HSYNC(cpu_transcoder));
15852 error->transcoder[i].vtotal = I915_READ(VTOTAL(cpu_transcoder));
15853 error->transcoder[i].vblank = I915_READ(VBLANK(cpu_transcoder));
15854 error->transcoder[i].vsync = I915_READ(VSYNC(cpu_transcoder));
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015855 }
15856
15857 return error;
15858}
15859
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015860#define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
15861
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015862void
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015863intel_display_print_error_state(struct drm_i915_error_state_buf *m,
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015864 struct drm_device *dev,
15865 struct intel_display_error_state *error)
15866{
Damien Lespiau055e3932014-08-18 13:49:10 +010015867 struct drm_i915_private *dev_priv = dev->dev_private;
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015868 int i;
15869
Chris Wilson63b66e52013-08-08 15:12:06 +020015870 if (!error)
15871 return;
15872
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015873 err_printf(m, "Num Pipes: %d\n", INTEL_INFO(dev)->num_pipes);
Imre Deak190be112013-11-25 17:15:31 +020015874 if (IS_HASWELL(dev) || IS_BROADWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015875 err_printf(m, "PWR_WELL_CTL2: %08x\n",
Paulo Zanoniff57f1b2013-05-03 12:15:37 -030015876 error->power_well_driver);
Damien Lespiau055e3932014-08-18 13:49:10 +010015877 for_each_pipe(dev_priv, i) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015878 err_printf(m, "Pipe [%d]:\n", i);
Imre Deakddf9c532013-11-27 22:02:02 +020015879 err_printf(m, " Power: %s\n",
15880 error->pipe[i].power_domain_on ? "on" : "off");
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015881 err_printf(m, " SRC: %08x\n", error->pipe[i].source);
Imre Deakf301b1e12014-04-18 15:55:04 +030015882 err_printf(m, " STAT: %08x\n", error->pipe[i].stat);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015883
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015884 err_printf(m, "Plane [%d]:\n", i);
15885 err_printf(m, " CNTR: %08x\n", error->plane[i].control);
15886 err_printf(m, " STRIDE: %08x\n", error->plane[i].stride);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015887 if (INTEL_INFO(dev)->gen <= 3) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015888 err_printf(m, " SIZE: %08x\n", error->plane[i].size);
15889 err_printf(m, " POS: %08x\n", error->plane[i].pos);
Paulo Zanoni80ca3782013-03-22 14:20:57 -030015890 }
Paulo Zanoni4b71a572013-03-22 14:19:21 -030015891 if (INTEL_INFO(dev)->gen <= 7 && !IS_HASWELL(dev))
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015892 err_printf(m, " ADDR: %08x\n", error->plane[i].addr);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015893 if (INTEL_INFO(dev)->gen >= 4) {
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015894 err_printf(m, " SURF: %08x\n", error->plane[i].surface);
15895 err_printf(m, " TILEOFF: %08x\n", error->plane[i].tile_offset);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015896 }
15897
Mika Kuoppalaedc3d882013-05-23 13:55:35 +030015898 err_printf(m, "Cursor [%d]:\n", i);
15899 err_printf(m, " CNTR: %08x\n", error->cursor[i].control);
15900 err_printf(m, " POS: %08x\n", error->cursor[i].position);
15901 err_printf(m, " BASE: %08x\n", error->cursor[i].base);
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015902 }
Chris Wilson63b66e52013-08-08 15:12:06 +020015903
15904 for (i = 0; i < error->num_transcoders; i++) {
Chris Wilson1cf84bb2013-10-21 09:10:33 +010015905 err_printf(m, "CPU transcoder: %c\n",
Chris Wilson63b66e52013-08-08 15:12:06 +020015906 transcoder_name(error->transcoder[i].cpu_transcoder));
Imre Deakddf9c532013-11-27 22:02:02 +020015907 err_printf(m, " Power: %s\n",
15908 error->transcoder[i].power_domain_on ? "on" : "off");
Chris Wilson63b66e52013-08-08 15:12:06 +020015909 err_printf(m, " CONF: %08x\n", error->transcoder[i].conf);
15910 err_printf(m, " HTOTAL: %08x\n", error->transcoder[i].htotal);
15911 err_printf(m, " HBLANK: %08x\n", error->transcoder[i].hblank);
15912 err_printf(m, " HSYNC: %08x\n", error->transcoder[i].hsync);
15913 err_printf(m, " VTOTAL: %08x\n", error->transcoder[i].vtotal);
15914 err_printf(m, " VBLANK: %08x\n", error->transcoder[i].vblank);
15915 err_printf(m, " VSYNC: %08x\n", error->transcoder[i].vsync);
15916 }
Chris Wilsonc4a1d9e2010-11-21 13:12:35 +000015917}
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015918
15919void intel_modeset_preclose(struct drm_device *dev, struct drm_file *file)
15920{
15921 struct intel_crtc *crtc;
15922
15923 for_each_intel_crtc(dev, crtc) {
15924 struct intel_unpin_work *work;
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015925
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015926 spin_lock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015927
15928 work = crtc->unpin_work;
15929
15930 if (work && work->event &&
15931 work->event->base.file_priv == file) {
15932 kfree(work->event);
15933 work->event = NULL;
15934 }
15935
Daniel Vetter5e2d7af2014-09-15 14:55:22 +020015936 spin_unlock_irq(&dev->event_lock);
Ville Syrjäläe2fcdaa2014-08-06 14:02:51 +030015937 }
15938}